121972Sdist /* 221972Sdist * Copyright (c) 1980 Regents of the University of California. 321972Sdist * All rights reserved. The Berkeley software License Agreement 421972Sdist * specifies the terms and conditions for redistribution. 521972Sdist */ 68806Smckusick 721972Sdist #ifndef lint 8*33230Sbostic static char sccsid[] = "@(#)pr_comment.c 5.4 (Berkeley) 01/02/88"; 921972Sdist #endif not lint 1021972Sdist 1124457Smckusick /*- 1224457Smckusick * 1324457Smckusick * Copyright (C) 1976 1424457Smckusick * by the 1524457Smckusick * Board of Trustees 1624457Smckusick * of the 1724457Smckusick * University of Illinois 1824457Smckusick * 1924457Smckusick * All rights reserved 2024457Smckusick * 2124457Smckusick * 2224457Smckusick * NAME: 2324457Smckusick * pr_comment 2424457Smckusick * 2524457Smckusick * FUNCTION: 2624457Smckusick * This routine takes care of scanning and printing comments. 2724457Smckusick * 2824457Smckusick * ALGORITHM: 2924457Smckusick * 1) Decide where the comment should be aligned, and if lines should 3024457Smckusick * be broken. 3124457Smckusick * 2) If lines should not be broken and filled, just copy up to end of 3224457Smckusick * comment. 3324457Smckusick * 3) If lines should be filled, then scan thru input_buffer copying 3424457Smckusick * characters to com_buf. Remember where the last blank, tab, or 3524457Smckusick * newline was. When line is filled, print up to last blank and 3624457Smckusick * continue copying. 3724457Smckusick * 3824457Smckusick * HISTORY: 3924457Smckusick * November 1976 D A Willcox of CAC Initial coding 4024457Smckusick * 12/6/76 D A Willcox of CAC Modification to handle 4124457Smckusick * UNIX-style comments 4224457Smckusick * 4324457Smckusick */ 4424457Smckusick 458806Smckusick /* 4624457Smckusick * this routine processes comments. It makes an attempt to keep comments 4724457Smckusick * from going over the max line length. If a line is too long, it moves 4824457Smckusick * everything from the last blank to the next comment line. Blanks and 4924457Smckusick * tabs from the beginning of the input line are removed 5024457Smckusick */ 518806Smckusick 52*33230Sbostic #include "indent_globs.h" 538806Smckusick 548806Smckusick 5524457Smckusick pr_comment() 5624457Smckusick { 5724457Smckusick int now_col; /* column we are in now */ 5824457Smckusick int adj_max_col; /* Adjusted max_col for when we decide to 5924457Smckusick * spill comments over the right margin */ 6024457Smckusick int col_1_com; /* this comment should not be touched */ 6124457Smckusick char *last_bl; /* points to the last blank in the output 6224457Smckusick * buffer */ 6324457Smckusick char achar; 6424457Smckusick char *t_ptr; /* used for moving string */ 6524457Smckusick int unix_comment; /* tri-state variable used to decide if it 6624457Smckusick * is a unix-style comment. 0 means only 6724457Smckusick * blanks since /*, 1 means regular style 6824457Smckusick * comment, 2 means unix style comment */ 6924457Smckusick int break_delim = comment_delimiter_on_blankline; 7024457Smckusick int l_just_saw_decl = ps.just_saw_decl; 7124457Smckusick /* 7224457Smckusick * int ps.last_nl = 0; /* true iff the last significant 7324457Smckusick * thing weve seen is a newline 7424457Smckusick */ 7524457Smckusick int one_liner = 1; /* true iff this comment is a one-liner */ 7624457Smckusick adj_max_col = max_col; 7724457Smckusick ps.just_saw_decl = 0; 7824457Smckusick last_bl = 0; /* no blanks found so far */ 7924457Smckusick ps.box_com = col_1_com = false; /* at first, assume that we are 8024457Smckusick * not in a boxed comment or some 8124457Smckusick * other comment that should not 8224457Smckusick * be touched */ 8324457Smckusick ++ps.out_coms; /* keep track of number of comments */ 8424457Smckusick unix_comment = 1; /* set flag to let us figure out if there 8524457Smckusick * is a unix-style comment ** DISABLED: 8624457Smckusick * use 0 to reenable this hack! */ 878806Smckusick 8824457Smckusick /* Figure where to align and how to treat the comment */ 898806Smckusick 9024457Smckusick if (ps.col_1 && !format_col1_comments) { /* if comment starts in 9124457Smckusick * column 1 it should not 9224457Smckusick * be touched */ 9324457Smckusick col_1_com = ps.box_com = true; 9424457Smckusick ps.com_col = 1; 9524457Smckusick } else { 9624457Smckusick if (*buf_ptr == '-' || *buf_ptr == '*') { 9724457Smckusick ps.box_com = true; /* a comment with a '-' or '*' immediately 9824457Smckusick * after the /* is assumed to be a boxed 9924457Smckusick * comment */ 10024457Smckusick col_1_com = true; 10124457Smckusick break_delim = 0; 1028806Smckusick } 10324457Smckusick if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) { 10424457Smckusick /* klg: check only if this line is blank */ 10524457Smckusick /* 10624457Smckusick * If this (*and previous lines are*) blank, dont put comment 10724457Smckusick * way out at left 10824457Smckusick */ 10924457Smckusick ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; 11024457Smckusick adj_max_col = block_comment_max_col; 11124457Smckusick if (ps.com_col <= 1) 11224457Smckusick ps.com_col = 1 + !format_col1_comments; 11324457Smckusick } else { 11424457Smckusick register target_col; 11524457Smckusick break_delim = 0; 11624457Smckusick if (s_code != e_code) 11724457Smckusick target_col = count_spaces(compute_code_target(), s_code); 11824457Smckusick else { 11924457Smckusick target_col = 1; 12024457Smckusick if (s_lab != e_lab) 12124457Smckusick target_col = count_spaces(compute_label_target(), s_lab); 12224457Smckusick } 12324457Smckusick ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind; 12424457Smckusick if (ps.com_col < target_col) 12524457Smckusick ps.com_col = ((target_col + 7) & ~7) + 1; 12624457Smckusick if (ps.com_col + 24 > adj_max_col) 12724457Smckusick adj_max_col = ps.com_col + 24; 1288806Smckusick } 1298806Smckusick } 13024457Smckusick if (ps.box_com) { 13124457Smckusick buf_ptr[-2] = 0; 13224457Smckusick ps.n_comment_delta = 1 - count_spaces(1, in_buffer); 13324457Smckusick ps.comment_delta = 0; 13424457Smckusick buf_ptr[-2] = '/'; 13524457Smckusick } else { 13624457Smckusick ps.n_comment_delta = 0; 13724457Smckusick ps.comment_delta = 0; 13824457Smckusick while (*buf_ptr == ' ' || *buf_ptr == '\t') 13924457Smckusick buf_ptr++; 14024457Smckusick } 14124457Smckusick ps.comment_delta = 0; 14224457Smckusick *e_com++ = '/'; /* put '/*' into buffer */ 1438806Smckusick *e_com++ = '*'; 14424457Smckusick if (*buf_ptr != ' ' && !ps.box_com) 1458806Smckusick *e_com++ = ' '; 1468806Smckusick 1478806Smckusick *e_com = '\0'; 14824457Smckusick now_col = count_spaces(ps.com_col, s_com); /* figure what column we 14924457Smckusick * would be in if we 15024457Smckusick * printed the comment now */ 1518806Smckusick 15224457Smckusick /* Start to copy the comment */ 1538806Smckusick 15424457Smckusick while (1) { /* this loop will go until the comment is 15524457Smckusick * copied */ 15624457Smckusick if (*buf_ptr > 040 && *buf_ptr != '*') 15724457Smckusick ps.last_nl = 0; 15824457Smckusick switch (*buf_ptr) { /* this checks for various spcl cases */ 15924457Smckusick case 014: /* check for a form feed */ 16024457Smckusick if (!ps.box_com) { /* in a text comment, break the 16124457Smckusick * line here */ 16224457Smckusick ps.use_ff = true; 16324457Smckusick /* fix so dump_line uses a form feed */ 16424457Smckusick dump_line(); 1658806Smckusick last_bl = 0; 1668806Smckusick *e_com++ = ' '; 16724457Smckusick *e_com++ = '*'; 1688806Smckusick *e_com++ = ' '; 16924457Smckusick while (*++buf_ptr == ' ' || *buf_ptr == '\t'); 17024457Smckusick } else { 1718806Smckusick if (++buf_ptr >= buf_end) 17224457Smckusick fill_buffer(); 1738806Smckusick *e_com++ = 014; 1748806Smckusick } 1758806Smckusick break; 1768806Smckusick 17724457Smckusick case '\n': 17824457Smckusick if (had_eof) { /* check for unexpected eof */ 17924457Smckusick printf("Unterminated comment\n"); 1808806Smckusick *e_com = '\0'; 18124457Smckusick dump_line(); 1828806Smckusick return; 1838806Smckusick } 18424457Smckusick one_liner = 0; 18524457Smckusick if (ps.box_com || ps.last_nl) { /* if this is a boxed 18624457Smckusick * comment, we dont ignore 18724457Smckusick * the newline */ 18824457Smckusick if (s_com == e_com) { 18924457Smckusick *e_com++ = ' '; 19024457Smckusick *e_com++ = ' '; 19124457Smckusick } 1928806Smckusick *e_com = '\0'; 19324457Smckusick if (!ps.box_com && e_com - s_com > 3) { 19424457Smckusick if (break_delim == 1 && s_com[0] == '/' 19524457Smckusick && s_com[1] == '*' && s_com[2] == ' ') { 19624457Smckusick char *t = e_com; 19724457Smckusick break_delim = 2; 19824457Smckusick e_com = s_com + 2; 19924457Smckusick *e_com = 0; 20024457Smckusick if (blanklines_before_blockcomments) prefix_blankline_requested = 1; 20124457Smckusick dump_line(); 20224457Smckusick e_com = t; 20324457Smckusick s_com[0] = s_com[1] = s_com[2] = ' '; 20424457Smckusick } 20524457Smckusick dump_line(); 20624457Smckusick *e_com++ = ' '; 20724457Smckusick *e_com++ = ' '; 2088806Smckusick } 20924457Smckusick dump_line(); 21024457Smckusick now_col = ps.com_col; 21124457Smckusick } else { 21224457Smckusick ps.last_nl = 1; 21324457Smckusick if (unix_comment != 1) { /* we not are in 21424457Smckusick * unix_style comment */ 2158806Smckusick if (unix_comment == 0 && s_code == e_code) { 21624457Smckusick /* 21724457Smckusick * if it is a UNIX-style comment, ignore the 21824457Smckusick * requirement that previous line be blank for 21924457Smckusick * unindention 22024457Smckusick */ 22124457Smckusick ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; 22224457Smckusick if (ps.com_col <= 1) 22324457Smckusick ps.com_col = 2; 2248806Smckusick } 22524457Smckusick unix_comment = 2; /* permanently remember that we 22624457Smckusick * are in this type of comment */ 22724457Smckusick dump_line(); 2288806Smckusick ++line_no; 22924457Smckusick now_col = ps.com_col; 2308806Smckusick *e_com++ = ' '; 23124457Smckusick /* 23224457Smckusick * fix so that the star at the start of the line will 23324457Smckusick * line up 23424457Smckusick */ 23524457Smckusick do /* flush leading white space */ 2368806Smckusick if (++buf_ptr >= buf_end) 23724457Smckusick fill_buffer(); 2388806Smckusick while (*buf_ptr == ' ' || *buf_ptr == '\t'); 2398806Smckusick break; 2408806Smckusick } 2418806Smckusick if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t') 2428806Smckusick last_bl = e_com - 1; 24324457Smckusick /* 24424457Smckusick * if there was a space at the end of the last line, 24524457Smckusick * remember where it was 24624457Smckusick */ 24724457Smckusick else { /* otherwise, insert one */ 2488806Smckusick last_bl = e_com; 2498806Smckusick *e_com++ = ' '; 2508806Smckusick ++now_col; 2518806Smckusick } 25224457Smckusick } 25324457Smckusick ++line_no; /* keep track of input line number */ 25424457Smckusick if (!ps.box_com) { 25524457Smckusick int nstar = 1; 25624457Smckusick do { /* flush any blanks and/or tabs at start 25724457Smckusick * of next line */ 25824457Smckusick if (++buf_ptr >= buf_end) 25924457Smckusick fill_buffer(); 26024457Smckusick if (*buf_ptr == '*' && --nstar >= 0) { 26124457Smckusick if (++buf_ptr >= buf_end) 26224457Smckusick fill_buffer(); 26324457Smckusick if (*buf_ptr == '/') 26424457Smckusick goto end_of_comment; 26524457Smckusick } 26624457Smckusick } while (*buf_ptr == ' ' || *buf_ptr == '\t'); 26724457Smckusick } else if (++buf_ptr >= buf_end) fill_buffer(); 26824457Smckusick break; /* end of case for newline */ 2698806Smckusick 27024457Smckusick case '*': /* must check for possibility of being at 27124457Smckusick * end of comment */ 27224457Smckusick if (++buf_ptr >= buf_end) /* get to next char after * */ 27324457Smckusick fill_buffer(); 2748806Smckusick 27524457Smckusick if (unix_comment == 0) /* set flag to show we are not in 27624457Smckusick * unix-style comment */ 2778806Smckusick unix_comment = 1; 2788806Smckusick 27924457Smckusick if (*buf_ptr == '/') { /* it is the end!!! */ 28024457Smckusick end_of_comment: 2818806Smckusick if (++buf_ptr >= buf_end) 28224457Smckusick fill_buffer(); 2838806Smckusick 28424457Smckusick if (*(e_com - 1) != ' ' && !ps.box_com) { /* insure blank before 28524457Smckusick * end */ 2868806Smckusick *e_com++ = ' '; 2878806Smckusick ++now_col; 2888806Smckusick } 28924457Smckusick if (break_delim == 1 && !one_liner && s_com[0] == '/' 29024457Smckusick && s_com[1] == '*' && s_com[2] == ' ') { 29124457Smckusick char *t = e_com; 29224457Smckusick break_delim = 2; 29324457Smckusick e_com = s_com + 2; 29424457Smckusick *e_com = 0; 29524457Smckusick if (blanklines_before_blockcomments) prefix_blankline_requested = 1; 29624457Smckusick dump_line(); 29724457Smckusick e_com = t; 29824457Smckusick s_com[0] = s_com[1] = s_com[2] = ' '; 29924457Smckusick } 30024457Smckusick if (break_delim == 2 && e_com > s_com + 3 30124457Smckusick /* now_col > adj_max_col - 2 && !ps.box_com */ ) { 3028806Smckusick *e_com = '\0'; 30324457Smckusick dump_line(); 30424457Smckusick now_col = ps.com_col; 3058806Smckusick } 3068806Smckusick *e_com++ = '*'; 3078806Smckusick *e_com++ = '/'; 3088806Smckusick *e_com = '\0'; 30924457Smckusick ps.just_saw_decl = l_just_saw_decl; 31024457Smckusick return; 31124457Smckusick } else { /* handle isolated '*' */ 3128806Smckusick *e_com++ = '*'; 3138806Smckusick ++now_col; 3148806Smckusick } 31524457Smckusick break; 31624457Smckusick default: /* we have a random char */ 3178806Smckusick if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t') 31824457Smckusick unix_comment = 1; /* we are not in unix-style 31924457Smckusick * comment */ 3208806Smckusick 3218806Smckusick *e_com = *buf_ptr++; 3228806Smckusick if (buf_ptr >= buf_end) 32324457Smckusick fill_buffer(); 3248806Smckusick 32524457Smckusick if (*e_com == '\t') /* keep track of column */ 3268806Smckusick now_col = ((now_col - 1) & tabmask) + tabsize + 1; 32724457Smckusick else if (*e_com == '\b') /* this is a backspace */ 32824457Smckusick --now_col; 3298806Smckusick else 33024457Smckusick ++now_col; 3318806Smckusick 3328806Smckusick if (*e_com == ' ' || *e_com == '\t') 3338806Smckusick last_bl = e_com; 33424457Smckusick /* remember we saw a blank */ 3358806Smckusick 3368806Smckusick ++e_com; 33724457Smckusick if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') { 33824457Smckusick /* the comment is too long, it must be broken up */ 33924457Smckusick if (break_delim == 1 && s_com[0] == '/' 34024457Smckusick && s_com[1] == '*' && s_com[2] == ' ') { 34124457Smckusick char *t = e_com; 34224457Smckusick break_delim = 2; 34324457Smckusick e_com = s_com + 2; 34424457Smckusick *e_com = 0; 34524457Smckusick if (blanklines_before_blockcomments) prefix_blankline_requested = 1; 34624457Smckusick dump_line(); 34724457Smckusick e_com = t; 34824457Smckusick s_com[0] = s_com[1] = s_com[2] = ' '; 34924457Smckusick } 35024457Smckusick if (last_bl == 0) { /* we have seen no blanks */ 35124457Smckusick last_bl = e_com; /* fake it */ 3528806Smckusick *e_com++ = ' '; 3538806Smckusick } 35424457Smckusick *e_com = '\0'; /* print what we have */ 3558806Smckusick *last_bl = '\0'; 35624457Smckusick while (last_bl > s_com && last_bl[-1] < 040) 35724457Smckusick *--last_bl = 0; 3588806Smckusick e_com = last_bl; 35924457Smckusick dump_line(); 3608806Smckusick 36124457Smckusick *e_com++ = ' '; /* add blanks for continuation */ 3628806Smckusick *e_com++ = ' '; 3638806Smckusick *e_com++ = ' '; 3648806Smckusick 3658806Smckusick t_ptr = last_bl + 1; 3668806Smckusick last_bl = 0; 36724457Smckusick if (t_ptr >= e_com) { 36824457Smckusick while (*t_ptr == ' ' || *t_ptr == '\t') 36924457Smckusick t_ptr++; 37024457Smckusick while (*t_ptr != '\0') { /* move unprinted part 37124457Smckusick * of comment down in 37224457Smckusick * buffer */ 37324457Smckusick if (*t_ptr == ' ' || *t_ptr == '\t') 37424457Smckusick last_bl = e_com; 37524457Smckusick *e_com++ = *t_ptr++; 37624457Smckusick } 3778806Smckusick } 3788806Smckusick *e_com = '\0'; 37924457Smckusick now_col = count_spaces(ps.com_col, s_com); /* recompute current 38024457Smckusick * position */ 38124457Smckusick } 38224457Smckusick break; 38324457Smckusick } 38424457Smckusick } 38524457Smckusick } 386