121972Sdist /* 221972Sdist * Copyright (c) 1980 Regents of the University of California. 333767Sbostic * Copyright (c) 1976 Board of Trustees of the University of Illinois. 433767Sbostic * All rights reserved. 533767Sbostic * 633767Sbostic * Redistribution and use in source and binary forms are permitted 7*34885Sbostic * provided that the above copyright notice and this paragraph are 8*34885Sbostic * duplicated in all such forms and that any documentation, 9*34885Sbostic * advertising materials, and other materials related to such 10*34885Sbostic * distribution and use acknowledge that the software was developed 11*34885Sbostic * by the University of California, Berkeley and the University 12*34885Sbostic * of Illinois, Urbana. The name of either 13*34885Sbostic * University may not be used to endorse or promote products derived 14*34885Sbostic * from this software without specific prior written permission. 15*34885Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 16*34885Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 17*34885Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1821972Sdist */ 198806Smckusick 2021972Sdist #ifndef lint 21*34885Sbostic static char sccsid[] = "@(#)pr_comment.c 5.6 (Berkeley) 06/29/88"; 2233767Sbostic #endif /* not lint */ 2321972Sdist 2433767Sbostic /* 2524457Smckusick * NAME: 2624457Smckusick * pr_comment 2724457Smckusick * 2824457Smckusick * FUNCTION: 2924457Smckusick * This routine takes care of scanning and printing comments. 3024457Smckusick * 3124457Smckusick * ALGORITHM: 3224457Smckusick * 1) Decide where the comment should be aligned, and if lines should 3324457Smckusick * be broken. 3424457Smckusick * 2) If lines should not be broken and filled, just copy up to end of 3524457Smckusick * comment. 3624457Smckusick * 3) If lines should be filled, then scan thru input_buffer copying 3724457Smckusick * characters to com_buf. Remember where the last blank, tab, or 3824457Smckusick * newline was. When line is filled, print up to last blank and 3924457Smckusick * continue copying. 4024457Smckusick * 4124457Smckusick * HISTORY: 4224457Smckusick * November 1976 D A Willcox of CAC Initial coding 4324457Smckusick * 12/6/76 D A Willcox of CAC Modification to handle 4424457Smckusick * UNIX-style comments 4524457Smckusick * 4624457Smckusick */ 4724457Smckusick 488806Smckusick /* 4924457Smckusick * this routine processes comments. It makes an attempt to keep comments 5024457Smckusick * from going over the max line length. If a line is too long, it moves 5124457Smckusick * everything from the last blank to the next comment line. Blanks and 5224457Smckusick * tabs from the beginning of the input line are removed 5324457Smckusick */ 548806Smckusick 5533230Sbostic #include "indent_globs.h" 568806Smckusick 578806Smckusick 5824457Smckusick pr_comment() 5924457Smckusick { 6024457Smckusick int now_col; /* column we are in now */ 6124457Smckusick int adj_max_col; /* Adjusted max_col for when we decide to 6224457Smckusick * spill comments over the right margin */ 6324457Smckusick int col_1_com; /* this comment should not be touched */ 6424457Smckusick char *last_bl; /* points to the last blank in the output 6524457Smckusick * buffer */ 6624457Smckusick char achar; 6724457Smckusick char *t_ptr; /* used for moving string */ 6824457Smckusick int unix_comment; /* tri-state variable used to decide if it 6924457Smckusick * is a unix-style comment. 0 means only 7024457Smckusick * blanks since /*, 1 means regular style 7124457Smckusick * comment, 2 means unix style comment */ 7224457Smckusick int break_delim = comment_delimiter_on_blankline; 7324457Smckusick int l_just_saw_decl = ps.just_saw_decl; 7424457Smckusick /* 7524457Smckusick * int ps.last_nl = 0; /* true iff the last significant 7624457Smckusick * thing weve seen is a newline 7724457Smckusick */ 7824457Smckusick int one_liner = 1; /* true iff this comment is a one-liner */ 7924457Smckusick adj_max_col = max_col; 8024457Smckusick ps.just_saw_decl = 0; 8124457Smckusick last_bl = 0; /* no blanks found so far */ 8224457Smckusick ps.box_com = col_1_com = false; /* at first, assume that we are 8324457Smckusick * not in a boxed comment or some 8424457Smckusick * other comment that should not 8524457Smckusick * be touched */ 8624457Smckusick ++ps.out_coms; /* keep track of number of comments */ 8724457Smckusick unix_comment = 1; /* set flag to let us figure out if there 8824457Smckusick * is a unix-style comment ** DISABLED: 8924457Smckusick * use 0 to reenable this hack! */ 908806Smckusick 9124457Smckusick /* Figure where to align and how to treat the comment */ 928806Smckusick 9324457Smckusick if (ps.col_1 && !format_col1_comments) { /* if comment starts in 9424457Smckusick * column 1 it should not 9524457Smckusick * be touched */ 9624457Smckusick col_1_com = ps.box_com = true; 9724457Smckusick ps.com_col = 1; 9824457Smckusick } else { 9924457Smckusick if (*buf_ptr == '-' || *buf_ptr == '*') { 10024457Smckusick ps.box_com = true; /* a comment with a '-' or '*' immediately 10124457Smckusick * after the /* is assumed to be a boxed 10224457Smckusick * comment */ 10324457Smckusick col_1_com = true; 10424457Smckusick break_delim = 0; 1058806Smckusick } 10624457Smckusick if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) { 10724457Smckusick /* klg: check only if this line is blank */ 10824457Smckusick /* 10924457Smckusick * If this (*and previous lines are*) blank, dont put comment 11024457Smckusick * way out at left 11124457Smckusick */ 11224457Smckusick ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; 11324457Smckusick adj_max_col = block_comment_max_col; 11424457Smckusick if (ps.com_col <= 1) 11524457Smckusick ps.com_col = 1 + !format_col1_comments; 11624457Smckusick } else { 11724457Smckusick register target_col; 11824457Smckusick break_delim = 0; 11924457Smckusick if (s_code != e_code) 12024457Smckusick target_col = count_spaces(compute_code_target(), s_code); 12124457Smckusick else { 12224457Smckusick target_col = 1; 12324457Smckusick if (s_lab != e_lab) 12424457Smckusick target_col = count_spaces(compute_label_target(), s_lab); 12524457Smckusick } 12624457Smckusick ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind; 12724457Smckusick if (ps.com_col < target_col) 12824457Smckusick ps.com_col = ((target_col + 7) & ~7) + 1; 12924457Smckusick if (ps.com_col + 24 > adj_max_col) 13024457Smckusick adj_max_col = ps.com_col + 24; 1318806Smckusick } 1328806Smckusick } 13324457Smckusick if (ps.box_com) { 13424457Smckusick buf_ptr[-2] = 0; 13524457Smckusick ps.n_comment_delta = 1 - count_spaces(1, in_buffer); 13624457Smckusick ps.comment_delta = 0; 13724457Smckusick buf_ptr[-2] = '/'; 13824457Smckusick } else { 13924457Smckusick ps.n_comment_delta = 0; 14024457Smckusick ps.comment_delta = 0; 14124457Smckusick while (*buf_ptr == ' ' || *buf_ptr == '\t') 14224457Smckusick buf_ptr++; 14324457Smckusick } 14424457Smckusick ps.comment_delta = 0; 14524457Smckusick *e_com++ = '/'; /* put '/*' into buffer */ 1468806Smckusick *e_com++ = '*'; 14724457Smckusick if (*buf_ptr != ' ' && !ps.box_com) 1488806Smckusick *e_com++ = ' '; 1498806Smckusick 1508806Smckusick *e_com = '\0'; 15124457Smckusick now_col = count_spaces(ps.com_col, s_com); /* figure what column we 15224457Smckusick * would be in if we 15324457Smckusick * printed the comment now */ 1548806Smckusick 15524457Smckusick /* Start to copy the comment */ 1568806Smckusick 15724457Smckusick while (1) { /* this loop will go until the comment is 15824457Smckusick * copied */ 15924457Smckusick if (*buf_ptr > 040 && *buf_ptr != '*') 16024457Smckusick ps.last_nl = 0; 16124457Smckusick switch (*buf_ptr) { /* this checks for various spcl cases */ 16224457Smckusick case 014: /* check for a form feed */ 16324457Smckusick if (!ps.box_com) { /* in a text comment, break the 16424457Smckusick * line here */ 16524457Smckusick ps.use_ff = true; 16624457Smckusick /* fix so dump_line uses a form feed */ 16724457Smckusick dump_line(); 1688806Smckusick last_bl = 0; 1698806Smckusick *e_com++ = ' '; 17024457Smckusick *e_com++ = '*'; 1718806Smckusick *e_com++ = ' '; 17224457Smckusick while (*++buf_ptr == ' ' || *buf_ptr == '\t'); 17324457Smckusick } else { 1748806Smckusick if (++buf_ptr >= buf_end) 17524457Smckusick fill_buffer(); 1768806Smckusick *e_com++ = 014; 1778806Smckusick } 1788806Smckusick break; 1798806Smckusick 18024457Smckusick case '\n': 18124457Smckusick if (had_eof) { /* check for unexpected eof */ 18224457Smckusick printf("Unterminated comment\n"); 1838806Smckusick *e_com = '\0'; 18424457Smckusick dump_line(); 1858806Smckusick return; 1868806Smckusick } 18724457Smckusick one_liner = 0; 18824457Smckusick if (ps.box_com || ps.last_nl) { /* if this is a boxed 18924457Smckusick * comment, we dont ignore 19024457Smckusick * the newline */ 19124457Smckusick if (s_com == e_com) { 19224457Smckusick *e_com++ = ' '; 19324457Smckusick *e_com++ = ' '; 19424457Smckusick } 1958806Smckusick *e_com = '\0'; 19624457Smckusick if (!ps.box_com && e_com - s_com > 3) { 19724457Smckusick if (break_delim == 1 && s_com[0] == '/' 19824457Smckusick && s_com[1] == '*' && s_com[2] == ' ') { 19924457Smckusick char *t = e_com; 20024457Smckusick break_delim = 2; 20124457Smckusick e_com = s_com + 2; 20224457Smckusick *e_com = 0; 20324457Smckusick if (blanklines_before_blockcomments) prefix_blankline_requested = 1; 20424457Smckusick dump_line(); 20524457Smckusick e_com = t; 20624457Smckusick s_com[0] = s_com[1] = s_com[2] = ' '; 20724457Smckusick } 20824457Smckusick dump_line(); 20924457Smckusick *e_com++ = ' '; 21024457Smckusick *e_com++ = ' '; 2118806Smckusick } 21224457Smckusick dump_line(); 21324457Smckusick now_col = ps.com_col; 21424457Smckusick } else { 21524457Smckusick ps.last_nl = 1; 21624457Smckusick if (unix_comment != 1) { /* we not are in 21724457Smckusick * unix_style comment */ 2188806Smckusick if (unix_comment == 0 && s_code == e_code) { 21924457Smckusick /* 22024457Smckusick * if it is a UNIX-style comment, ignore the 22124457Smckusick * requirement that previous line be blank for 22224457Smckusick * unindention 22324457Smckusick */ 22424457Smckusick ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; 22524457Smckusick if (ps.com_col <= 1) 22624457Smckusick ps.com_col = 2; 2278806Smckusick } 22824457Smckusick unix_comment = 2; /* permanently remember that we 22924457Smckusick * are in this type of comment */ 23024457Smckusick dump_line(); 2318806Smckusick ++line_no; 23224457Smckusick now_col = ps.com_col; 2338806Smckusick *e_com++ = ' '; 23424457Smckusick /* 23524457Smckusick * fix so that the star at the start of the line will 23624457Smckusick * line up 23724457Smckusick */ 23824457Smckusick do /* flush leading white space */ 2398806Smckusick if (++buf_ptr >= buf_end) 24024457Smckusick fill_buffer(); 2418806Smckusick while (*buf_ptr == ' ' || *buf_ptr == '\t'); 2428806Smckusick break; 2438806Smckusick } 2448806Smckusick if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t') 2458806Smckusick last_bl = e_com - 1; 24624457Smckusick /* 24724457Smckusick * if there was a space at the end of the last line, 24824457Smckusick * remember where it was 24924457Smckusick */ 25024457Smckusick else { /* otherwise, insert one */ 2518806Smckusick last_bl = e_com; 2528806Smckusick *e_com++ = ' '; 2538806Smckusick ++now_col; 2548806Smckusick } 25524457Smckusick } 25624457Smckusick ++line_no; /* keep track of input line number */ 25724457Smckusick if (!ps.box_com) { 25824457Smckusick int nstar = 1; 25924457Smckusick do { /* flush any blanks and/or tabs at start 26024457Smckusick * of next line */ 26124457Smckusick if (++buf_ptr >= buf_end) 26224457Smckusick fill_buffer(); 26324457Smckusick if (*buf_ptr == '*' && --nstar >= 0) { 26424457Smckusick if (++buf_ptr >= buf_end) 26524457Smckusick fill_buffer(); 26624457Smckusick if (*buf_ptr == '/') 26724457Smckusick goto end_of_comment; 26824457Smckusick } 26924457Smckusick } while (*buf_ptr == ' ' || *buf_ptr == '\t'); 27024457Smckusick } else if (++buf_ptr >= buf_end) fill_buffer(); 27124457Smckusick break; /* end of case for newline */ 2728806Smckusick 27324457Smckusick case '*': /* must check for possibility of being at 27424457Smckusick * end of comment */ 27524457Smckusick if (++buf_ptr >= buf_end) /* get to next char after * */ 27624457Smckusick fill_buffer(); 2778806Smckusick 27824457Smckusick if (unix_comment == 0) /* set flag to show we are not in 27924457Smckusick * unix-style comment */ 2808806Smckusick unix_comment = 1; 2818806Smckusick 28224457Smckusick if (*buf_ptr == '/') { /* it is the end!!! */ 28324457Smckusick end_of_comment: 2848806Smckusick if (++buf_ptr >= buf_end) 28524457Smckusick fill_buffer(); 2868806Smckusick 28724457Smckusick if (*(e_com - 1) != ' ' && !ps.box_com) { /* insure blank before 28824457Smckusick * end */ 2898806Smckusick *e_com++ = ' '; 2908806Smckusick ++now_col; 2918806Smckusick } 29224457Smckusick if (break_delim == 1 && !one_liner && s_com[0] == '/' 29324457Smckusick && s_com[1] == '*' && s_com[2] == ' ') { 29424457Smckusick char *t = e_com; 29524457Smckusick break_delim = 2; 29624457Smckusick e_com = s_com + 2; 29724457Smckusick *e_com = 0; 29824457Smckusick if (blanklines_before_blockcomments) prefix_blankline_requested = 1; 29924457Smckusick dump_line(); 30024457Smckusick e_com = t; 30124457Smckusick s_com[0] = s_com[1] = s_com[2] = ' '; 30224457Smckusick } 30324457Smckusick if (break_delim == 2 && e_com > s_com + 3 30424457Smckusick /* now_col > adj_max_col - 2 && !ps.box_com */ ) { 3058806Smckusick *e_com = '\0'; 30624457Smckusick dump_line(); 30724457Smckusick now_col = ps.com_col; 3088806Smckusick } 3098806Smckusick *e_com++ = '*'; 3108806Smckusick *e_com++ = '/'; 3118806Smckusick *e_com = '\0'; 31224457Smckusick ps.just_saw_decl = l_just_saw_decl; 31324457Smckusick return; 31424457Smckusick } else { /* handle isolated '*' */ 3158806Smckusick *e_com++ = '*'; 3168806Smckusick ++now_col; 3178806Smckusick } 31824457Smckusick break; 31924457Smckusick default: /* we have a random char */ 3208806Smckusick if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t') 32124457Smckusick unix_comment = 1; /* we are not in unix-style 32224457Smckusick * comment */ 3238806Smckusick 3248806Smckusick *e_com = *buf_ptr++; 3258806Smckusick if (buf_ptr >= buf_end) 32624457Smckusick fill_buffer(); 3278806Smckusick 32824457Smckusick if (*e_com == '\t') /* keep track of column */ 3298806Smckusick now_col = ((now_col - 1) & tabmask) + tabsize + 1; 33024457Smckusick else if (*e_com == '\b') /* this is a backspace */ 33124457Smckusick --now_col; 3328806Smckusick else 33324457Smckusick ++now_col; 3348806Smckusick 3358806Smckusick if (*e_com == ' ' || *e_com == '\t') 3368806Smckusick last_bl = e_com; 33724457Smckusick /* remember we saw a blank */ 3388806Smckusick 3398806Smckusick ++e_com; 34024457Smckusick if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') { 34124457Smckusick /* the comment is too long, it must be broken up */ 34224457Smckusick if (break_delim == 1 && s_com[0] == '/' 34324457Smckusick && s_com[1] == '*' && s_com[2] == ' ') { 34424457Smckusick char *t = e_com; 34524457Smckusick break_delim = 2; 34624457Smckusick e_com = s_com + 2; 34724457Smckusick *e_com = 0; 34824457Smckusick if (blanklines_before_blockcomments) prefix_blankline_requested = 1; 34924457Smckusick dump_line(); 35024457Smckusick e_com = t; 35124457Smckusick s_com[0] = s_com[1] = s_com[2] = ' '; 35224457Smckusick } 35324457Smckusick if (last_bl == 0) { /* we have seen no blanks */ 35424457Smckusick last_bl = e_com; /* fake it */ 3558806Smckusick *e_com++ = ' '; 3568806Smckusick } 35724457Smckusick *e_com = '\0'; /* print what we have */ 3588806Smckusick *last_bl = '\0'; 35924457Smckusick while (last_bl > s_com && last_bl[-1] < 040) 36024457Smckusick *--last_bl = 0; 3618806Smckusick e_com = last_bl; 36224457Smckusick dump_line(); 3638806Smckusick 36424457Smckusick *e_com++ = ' '; /* add blanks for continuation */ 3658806Smckusick *e_com++ = ' '; 3668806Smckusick *e_com++ = ' '; 3678806Smckusick 3688806Smckusick t_ptr = last_bl + 1; 3698806Smckusick last_bl = 0; 37024457Smckusick if (t_ptr >= e_com) { 37124457Smckusick while (*t_ptr == ' ' || *t_ptr == '\t') 37224457Smckusick t_ptr++; 37324457Smckusick while (*t_ptr != '\0') { /* move unprinted part 37424457Smckusick * of comment down in 37524457Smckusick * buffer */ 37624457Smckusick if (*t_ptr == ' ' || *t_ptr == '\t') 37724457Smckusick last_bl = e_com; 37824457Smckusick *e_com++ = *t_ptr++; 37924457Smckusick } 3808806Smckusick } 3818806Smckusick *e_com = '\0'; 38224457Smckusick now_col = count_spaces(ps.com_col, s_com); /* recompute current 38324457Smckusick * position */ 38424457Smckusick } 38524457Smckusick break; 38624457Smckusick } 38724457Smckusick } 38824457Smckusick } 389