home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / INDENT1.ZIP / COMMENT.C < prev    next >
C/C++ Source or Header  |  1991-07-04  |  13KB  |  453 lines

  1. /*
  2.  * Copyright (c) 1985 Sun Microsystems, Inc. Copyright (c) 1980 The Regents
  3.  * of the University of California. Copyright (c) 1976 Board of Trustees of
  4.  * the University of Illinois. All rights reserved.
  5.  * 
  6.  * Redistribution and use in source and binary forms are permitted provided that
  7.  * the above copyright notice and this paragraph are duplicated in all such
  8.  * forms and that any documentation, advertising materials, and other
  9.  * materials related to such distribution and use acknowledge that the
  10.  * software was developed by the University of California, Berkeley, the
  11.  * University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  12.  * either University or Sun Microsystems may not be used to endorse or
  13.  * promote products derived from this software without specific prior written
  14.  * permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  16.  * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #ifndef lint
  22. static char     sccsid[] = "@(#)pr_comment.c    5.9 (Berkeley) 9/15/88";
  23.  
  24. #endif                                    /* not lint */
  25.  
  26. #if        CC_MSC
  27. #include "malloc.h"
  28. #endif
  29.  
  30. /*
  31.  * NAME: pr_comment
  32.  * 
  33.  * FUNCTION: This routine takes care of scanning and printing comments.
  34.  * 
  35.  * ALGORITHM: 1) Decide where the comment should be aligned, and if lines should
  36.  * be broken. 2) If lines should not be broken and filled, just copy up to
  37.  * end of comment. 3) If lines should be filled, then scan thru input_buffer
  38.  * copying characters to com_buf. Remember where the last blank, tab, or
  39.  * newline was.  When line is filled, print up to last blank and continue
  40.  * copying.
  41.  * 
  42.  * HISTORY: November 1976    D A Willcox of CAC    Initial coding 12/6/76        D A
  43.  * Willcox of CAC    Modification to handle UNIX-style comments
  44.  * 
  45.  */
  46.  
  47. /*
  48.  * this routine processes comments.  It makes an attempt to keep comments
  49.  * from going over the max line length.  If a line is too long, it moves
  50.  * everything from the last blank to the next comment line.  Blanks and tabs
  51.  * from the beginning of the input line are removed
  52.  */
  53.  
  54.  
  55. #include "globs.h"
  56.  
  57.  
  58. pr_comment()
  59. {
  60.     int             now_col;            /* column we are in now */
  61.     int             adj_max_col;        /* Adjusted max_col for when we
  62.                                          * decide to spill comments over the
  63.                                          * right margin */
  64.     char           *last_bl;            /* points to the last blank in the
  65.                                          * output buffer */
  66.     char           *t_ptr;                /* used for moving string */
  67.     int             unix_comment;        /* tri-state variable used to decide
  68.                                          * if it is a unix-style comment. 0
  69.                                          * means only blanks since /*, 1
  70.                                          * means regular style comment, 2
  71.                                          * means unix style comment */
  72.     int             break_delim = comment_delimiter_on_bl;
  73.     int             l_just_saw_decl = ps.just_saw_decl;
  74.  
  75.     /*
  76.      * int         ps.last_nl = 0;    /* true iff the last significant thing
  77.      * weve seen is a newline
  78.      */
  79.     int             one_liner = 1;        /* true iff this comment is a
  80.                                          * one-liner */
  81.  
  82.     adj_max_col = max_col;
  83.     ps.just_saw_decl = 0;
  84.     last_bl = 0;                        /* no blanks found so far */
  85.     ps.box_com = false;                    /* at first, assume that we are not
  86.                                          * in a boxed comment or some other
  87.                                          * comment that should not be touched */
  88.     ++ps.out_coms;                        /* keep track of number of comments */
  89.     unix_comment = 1;                    /* set flag to let us figure out if
  90.                                          * there is a unix-style comment **
  91.                                          * DISABLED: use 0 to reenable this
  92.                                          * hack! */
  93.  
  94.     /* Figure where to align and how to treat the comment */
  95.  
  96.     if (ps.col_1 && !format_col1_comments)
  97.     {                                    /* if comment starts in column 1 it
  98.                                          * should not be touched */
  99.         ps.box_com = true;
  100.         ps.com_col = 1;
  101.     }
  102.     else
  103.     {
  104.         if (*buf_ptr == '-' || *buf_ptr == '*')
  105.         {
  106.             ps.box_com = true;            /* a comment with a '-' or '*'
  107.                                          * immediately after the /* is
  108.                                          * assumed to be a boxed comment */
  109.             break_delim = 0;
  110.         }
  111.         if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code))
  112.         {
  113.             /* klg: check only if this line is blank */
  114.  
  115.             /*
  116.              * If this (*and previous lines are*) blank, dont put comment way
  117.              * out at left
  118.              */
  119.             ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
  120.             adj_max_col = block_comment_max_col;
  121.             if (ps.com_col <= 1)
  122.                 ps.com_col = 1 + !format_col1_comments;
  123.         }
  124.         else
  125.         {
  126.             register        target_col;
  127.  
  128.             break_delim = 0;
  129.             if (s_code != e_code)
  130.                 target_col = count_spaces(compute_code_target(), s_code);
  131.             else
  132.             {
  133.                 target_col = 1;
  134.                 if (s_lab != e_lab)
  135.                     target_col = count_spaces(compute_label_target(), s_lab);
  136.             }
  137.             ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
  138.             if (ps.com_col < target_col)
  139.                 /*** ps.com_col = ((target_col + 7) & ~7) + 1; ***/
  140.                 ps.com_col = target_col +
  141.                     (tabsize - ((target_col - 1) % tabsize)); /* JHT 10/22/89 */
  142.             if (ps.com_col + 24 > adj_max_col)
  143.                 adj_max_col = ps.com_col + 24;
  144.         }
  145.     }
  146.     if (ps.box_com)
  147.     {
  148.         buf_ptr[-2] = 0;
  149.         ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
  150.         buf_ptr[-2] = '/';
  151.     }
  152.     else
  153.     {
  154.         ps.n_comment_delta = 0;
  155.         while (*buf_ptr == ' ' || *buf_ptr == '\t')
  156.             buf_ptr++;
  157.     }
  158.     ps.comment_delta = 0;
  159.     *e_com++ = '/';                        /* put '/*' into buffer */
  160.     *e_com++ = '*';
  161.     if (*buf_ptr != ' ' && !ps.box_com)
  162.         *e_com++ = ' ';
  163.  
  164.     *e_com = '\0';
  165.     if (troff)
  166.     {
  167.         now_col = 1;
  168.         adj_max_col = 80;
  169.     }
  170.     else
  171.         now_col = count_spaces(ps.com_col, s_com);        /* figure what column we
  172.                                                          * would be in if we
  173.                                                          * printed the comment
  174.                                                          * now */
  175.  
  176.     /* Start to copy the comment */
  177.  
  178.     while (1)
  179.     {                                    /* this loop will go until the
  180.                                          * comment is copied */
  181.         if (*buf_ptr > 040 && *buf_ptr != '*')
  182.             ps.last_nl = 0;
  183.         check_size(com);
  184.         switch (*buf_ptr)
  185.         {                                /* this checks for various spcl cases */
  186.         case 014:                        /* check for a form feed */
  187.             if (!ps.box_com)
  188.             {                            /* in a text comment, break the line
  189.                                          * here */
  190.                 ps.use_ff = true;
  191.                 /* fix so dump_line uses a form feed */
  192.                 dump_line();
  193.                 last_bl = 0;
  194.                 *e_com++ = ' ';
  195.                 *e_com++ = '*';
  196.                 *e_com++ = ' ';
  197.                 while (*++buf_ptr == ' ' || *buf_ptr == '\t');
  198.             }
  199.             else
  200.             {
  201.                 if (++buf_ptr >= buf_end)
  202.                     fill_buffer();
  203.                 *e_com++ = 014;
  204.             }
  205.             break;
  206.  
  207.         case '\n':
  208.             if (had_eof)
  209.             {                            /* check for unexpected eof */
  210.                 printf("Unterminated comment\n");
  211.                 *e_com = '\0';
  212.                 dump_line();
  213.                 return;
  214.             }
  215.             one_liner = 0;
  216.             if (ps.box_com || ps.last_nl)
  217.             {                            /* if this is a boxed comment, we
  218.                                          * dont ignore the newline */
  219.                 if (s_com == e_com)
  220.                 {
  221.                     *e_com++ = ' ';
  222.                     *e_com++ = ' ';
  223.                 }
  224.                 *e_com = '\0';
  225.                 if (!ps.box_com && e_com - s_com > 3)
  226.                 {
  227.                     if (break_delim == 1 && s_com[0] == '/'
  228.                         && s_com[1] == '*' && s_com[2] == ' ')
  229.                     {
  230.                         char           *t = e_com;
  231.  
  232.                         break_delim = 2;
  233.                         e_com = s_com + 2;
  234.                         *e_com = 0;
  235.                         if (bl_before_blockcomments)
  236.                             prefix_bl_requested = 1;
  237.                         dump_line();
  238.                         e_com = t;
  239.                         s_com[0] = s_com[1] = s_com[2] = ' ';
  240.                     }
  241.                     dump_line();
  242.                     check_size(com);
  243.                     *e_com++ = ' ';
  244.                     *e_com++ = ' ';
  245.                 }
  246.                 dump_line();
  247.                 now_col = ps.com_col;
  248.             }
  249.             else
  250.             {
  251.                 ps.last_nl = 1;
  252.                 if (unix_comment != 1)
  253.                 {                        /* we not are in unix_style comment */
  254.                     if (unix_comment == 0 && s_code == e_code)
  255.                     {
  256.  
  257.                         /*
  258.                          * if it is a UNIX-style comment, ignore the
  259.                          * requirement that previous line be blank for
  260.                          * unindention
  261.                          */
  262.                         ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
  263.                         if (ps.com_col <= 1)
  264.                             ps.com_col = 2;
  265.                     }
  266.                     unix_comment = 2;    /* permanently remember that we are
  267.                                          * in this type of comment */
  268.                     dump_line();
  269.                     ++line_no;
  270.                     now_col = ps.com_col;
  271.                     *e_com++ = ' ';
  272.  
  273.                     /*
  274.                      * fix so that the star at the start of the line will
  275.                      * line up
  276.                      */
  277.                     do                    /* flush leading white space */
  278.                         if (++buf_ptr >= buf_end)
  279.                             fill_buffer();
  280.                     while (*buf_ptr == ' ' || *buf_ptr == '\t');
  281.                     break;
  282.                 }
  283.                 if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
  284.                     last_bl = e_com - 1;
  285.  
  286.                 /*
  287.                  * if there was a space at the end of the last line, remember
  288.                  * where it was
  289.                  */
  290.                 else
  291.                 {                        /* otherwise, insert one */
  292.                     last_bl = e_com;
  293.                     check_size(com);
  294.                     *e_com++ = ' ';
  295.                     ++now_col;
  296.                 }
  297.             }
  298.             ++line_no;                    /* keep track of input line number */
  299.             if (!ps.box_com)
  300.             {
  301.                 int             nstar = 1;
  302.  
  303.                 do
  304.                 {                        /* flush any blanks and/or tabs at
  305.                                          * start of next line */
  306.                     if (++buf_ptr >= buf_end)
  307.                         fill_buffer();
  308.                     if (*buf_ptr == '*' && --nstar >= 0)
  309.                     {
  310.                         if (++buf_ptr >= buf_end)
  311.                             fill_buffer();
  312.                         if (*buf_ptr == '/')
  313.                             goto end_of_comment;
  314.                     }
  315.                 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
  316.             }
  317.             else if (++buf_ptr >= buf_end)
  318.                 fill_buffer();
  319.             break;                        /* end of case for newline */
  320.  
  321.         case '*':                        /* must check for possibility of
  322.                                          * being at end of comment */
  323.             if (++buf_ptr >= buf_end)    /* get to next char after * */
  324.                 fill_buffer();
  325.  
  326.             if (unix_comment == 0)        /* set flag to show we are not in
  327.                                          * unix-style comment */
  328.                 unix_comment = 1;
  329.  
  330.             if (*buf_ptr == '/')
  331.             {                            /* it is the end!!! */
  332.         end_of_comment:
  333.                 if (++buf_ptr >= buf_end)
  334.                     fill_buffer();
  335.  
  336.                 if (*(e_com - 1) != ' ' && !ps.box_com)
  337.                 {                        /* insure blank before end */
  338.                     *e_com++ = ' ';
  339.                     ++now_col;
  340.                 }
  341.                 if (break_delim == 1 && !one_liner && s_com[0] == '/'
  342.                     && s_com[1] == '*' && s_com[2] == ' ')
  343.                 {
  344.                     char           *t = e_com;
  345.  
  346.                     break_delim = 2;
  347.                     e_com = s_com + 2;
  348.                     *e_com = 0;
  349.                     if (bl_before_blockcomments)
  350.                         prefix_bl_requested = 1;
  351.                     dump_line();
  352.                     e_com = t;
  353.                     s_com[0] = s_com[1] = s_com[2] = ' ';
  354.                 }
  355.                 if (break_delim == 2 && e_com > s_com + 3
  356.                      /* now_col > adj_max_col - 2 && !ps.box_com */ )
  357.                 {
  358.                     *e_com = '\0';
  359.                     dump_line();
  360.                     now_col = ps.com_col;
  361.                 }
  362.                 check_size(com);
  363.                 *e_com++ = '*';
  364.                 *e_com++ = '/';
  365.                 *e_com = '\0';
  366.                 ps.just_saw_decl = l_just_saw_decl;
  367.                 return;
  368.             }
  369.             else
  370.             {                            /* handle isolated '*' */
  371.                 *e_com++ = '*';
  372.                 ++now_col;
  373.             }
  374.             break;
  375.         default:                        /* we have a random char */
  376.             if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
  377.                 unix_comment = 1;        /* we are not in unix-style comment */
  378.  
  379.             *e_com = *buf_ptr++;
  380.             if (buf_ptr >= buf_end)
  381.                 fill_buffer();
  382.  
  383.             if (*e_com == '\t')            /* keep track of column */
  384.                 now_col = now_col + (tabsize - ((now_col - 1) % tabsize));
  385.             else if (*e_com == '\b')    /* this is a backspace */
  386.                 --now_col;
  387.             else
  388.                 ++now_col;
  389.  
  390.             if (*e_com == ' ' || *e_com == '\t')
  391.                 last_bl = e_com;
  392.             /* remember we saw a blank */
  393.  
  394.             ++e_com;
  395.             if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ')
  396.             {
  397.  
  398.                 /*
  399.                  * the comment is too long, it must be broken up
  400.                  */
  401.                 if (break_delim == 1 && s_com[0] == '/'
  402.                     && s_com[1] == '*' && s_com[2] == ' ')
  403.                 {
  404.                     char           *t = e_com;
  405.  
  406.                     break_delim = 2;
  407.                     e_com = s_com + 2;
  408.                     *e_com = 0;
  409.                     if (bl_before_blockcomments)
  410.                         prefix_bl_requested = 1;
  411.                     dump_line();
  412.                     e_com = t;
  413.                     s_com[0] = s_com[1] = s_com[2] = ' ';
  414.                 }
  415.                 if (last_bl == 0)
  416.                 {                        /* we have seen no blanks */
  417.                     last_bl = e_com;    /* fake it */
  418.                     *e_com++ = ' ';
  419.                 }
  420.                 *e_com = '\0';            /* print what we have */
  421.                 *last_bl = '\0';
  422.                 while (last_bl > s_com && last_bl[-1] < 040)
  423.                     *--last_bl = 0;
  424.                 e_com = last_bl;
  425.                 dump_line();
  426.  
  427.                 *e_com++ = ' ';            /* add blanks for continuation */
  428.                 *e_com++ = ' ';
  429.                 *e_com++ = ' ';
  430.  
  431.                 t_ptr = last_bl + 1;
  432.                 last_bl = 0;
  433.                 if (t_ptr >= e_com)
  434.                 {
  435.                     while (*t_ptr == ' ' || *t_ptr == '\t')
  436.                         t_ptr++;
  437.                     while (*t_ptr != '\0')
  438.                     {                    /* move unprinted part of comment
  439.                                          * down in buffer */
  440.                         if (*t_ptr == ' ' || *t_ptr == '\t')
  441.                             last_bl = e_com;
  442.                         *e_com++ = *t_ptr++;
  443.                     }
  444.                 }
  445.                 *e_com = '\0';
  446.                 now_col = count_spaces(ps.com_col, s_com);        /* recompute current
  447.                                                                  * position */
  448.             }
  449.             break;
  450.         }
  451.     }
  452. }
  453.