home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / util / editor / gnuid16.sit / indent-1.6 / pr_comment.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-04  |  13.3 KB  |  451 lines

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