121972Sdist /* 235500Sbostic * Copyright (c) 1985 Sun Microsystems, Inc. 335500Sbostic * Copyright (c) 1980 The Regents of the University of California. 433767Sbostic * Copyright (c) 1976 Board of Trustees of the University of Illinois. 533767Sbostic * All rights reserved. 633767Sbostic * 7*42688Sbostic * %sccs.include.redist.c% 821972Sdist */ 98806Smckusick 1021972Sdist #ifndef lint 11*42688Sbostic static char sccsid[] = "@(#)pr_comment.c 5.11 (Berkeley) 06/01/90"; 1233767Sbostic #endif /* not lint */ 1321972Sdist 1433767Sbostic /* 1524457Smckusick * NAME: 1624457Smckusick * pr_comment 1724457Smckusick * 1824457Smckusick * FUNCTION: 1924457Smckusick * This routine takes care of scanning and printing comments. 2024457Smckusick * 2124457Smckusick * ALGORITHM: 2224457Smckusick * 1) Decide where the comment should be aligned, and if lines should 2324457Smckusick * be broken. 2424457Smckusick * 2) If lines should not be broken and filled, just copy up to end of 2524457Smckusick * comment. 2624457Smckusick * 3) If lines should be filled, then scan thru input_buffer copying 2724457Smckusick * characters to com_buf. Remember where the last blank, tab, or 2835500Sbostic * newline was. When line is filled, print up to last blank and 2924457Smckusick * continue copying. 3024457Smckusick * 3124457Smckusick * HISTORY: 3224457Smckusick * November 1976 D A Willcox of CAC Initial coding 3335500Sbostic * 12/6/76 D A Willcox of CAC Modification to handle 3424457Smckusick * UNIX-style comments 3524457Smckusick * 3624457Smckusick */ 3724457Smckusick 388806Smckusick /* 3935500Sbostic * this routine processes comments. It makes an attempt to keep comments from 4035500Sbostic * going over the max line length. If a line is too long, it moves everything 4135500Sbostic * from the last blank to the next comment line. Blanks and tabs from the 4235500Sbostic * beginning of the input line are removed 4324457Smckusick */ 448806Smckusick 458806Smckusick 4635501Sbostic #include "indent_globs.h" 478806Smckusick 4835500Sbostic 4924457Smckusick pr_comment() 5024457Smckusick { 5124457Smckusick int now_col; /* column we are in now */ 5224457Smckusick int adj_max_col; /* Adjusted max_col for when we decide to 5324457Smckusick * spill comments over the right margin */ 5424457Smckusick char *last_bl; /* points to the last blank in the output 5524457Smckusick * buffer */ 5624457Smckusick char *t_ptr; /* used for moving string */ 5735500Sbostic int unix_comment; /* tri-state variable used to decide if it is 5835500Sbostic * a unix-style comment. 0 means only blanks 5935500Sbostic * since /*, 1 means regular style comment, 2 6035500Sbostic * means unix style comment */ 6124457Smckusick int break_delim = comment_delimiter_on_blankline; 6235500Sbostic int l_just_saw_decl = ps.just_saw_decl; 6324457Smckusick /* 6435500Sbostic * int ps.last_nl = 0; /* true iff the last significant thing 6535500Sbostic * weve seen is a newline 6624457Smckusick */ 6724457Smckusick int one_liner = 1; /* true iff this comment is a one-liner */ 6824457Smckusick adj_max_col = max_col; 6924457Smckusick ps.just_saw_decl = 0; 7024457Smckusick last_bl = 0; /* no blanks found so far */ 7135506Sbostic ps.box_com = false; /* at first, assume that we are not in 7235500Sbostic * a boxed comment or some other 7335500Sbostic * comment that should not be touched */ 7424457Smckusick ++ps.out_coms; /* keep track of number of comments */ 7535500Sbostic unix_comment = 1; /* set flag to let us figure out if there is a 7635500Sbostic * unix-style comment ** DISABLED: use 0 to 7735500Sbostic * reenable this hack! */ 788806Smckusick 7924457Smckusick /* Figure where to align and how to treat the comment */ 808806Smckusick 8135500Sbostic if (ps.col_1 && !format_col1_comments) { /* if comment starts in column 8235500Sbostic * 1 it should not be touched */ 8335506Sbostic ps.box_com = true; 8424457Smckusick ps.com_col = 1; 8535500Sbostic } 8635500Sbostic else { 8724457Smckusick if (*buf_ptr == '-' || *buf_ptr == '*') { 8824457Smckusick ps.box_com = true; /* a comment with a '-' or '*' immediately 8924457Smckusick * after the /* is assumed to be a boxed 9024457Smckusick * comment */ 9124457Smckusick break_delim = 0; 928806Smckusick } 9324457Smckusick if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) { 9424457Smckusick /* klg: check only if this line is blank */ 9524457Smckusick /* 9635500Sbostic * If this (*and previous lines are*) blank, dont put comment way 9735500Sbostic * out at left 9824457Smckusick */ 9924457Smckusick ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; 10024457Smckusick adj_max_col = block_comment_max_col; 10124457Smckusick if (ps.com_col <= 1) 10224457Smckusick ps.com_col = 1 + !format_col1_comments; 10335500Sbostic } 10435500Sbostic else { 10524457Smckusick register target_col; 10624457Smckusick break_delim = 0; 10724457Smckusick if (s_code != e_code) 10824457Smckusick target_col = count_spaces(compute_code_target(), s_code); 10924457Smckusick else { 11024457Smckusick target_col = 1; 11124457Smckusick if (s_lab != e_lab) 11224457Smckusick target_col = count_spaces(compute_label_target(), s_lab); 11324457Smckusick } 11424457Smckusick ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind; 11524457Smckusick if (ps.com_col < target_col) 11624457Smckusick ps.com_col = ((target_col + 7) & ~7) + 1; 11724457Smckusick if (ps.com_col + 24 > adj_max_col) 11824457Smckusick adj_max_col = ps.com_col + 24; 1198806Smckusick } 1208806Smckusick } 12124457Smckusick if (ps.box_com) { 12224457Smckusick buf_ptr[-2] = 0; 12324457Smckusick ps.n_comment_delta = 1 - count_spaces(1, in_buffer); 12424457Smckusick buf_ptr[-2] = '/'; 12535500Sbostic } 12635500Sbostic else { 12724457Smckusick ps.n_comment_delta = 0; 12824457Smckusick while (*buf_ptr == ' ' || *buf_ptr == '\t') 12924457Smckusick buf_ptr++; 13024457Smckusick } 13124457Smckusick ps.comment_delta = 0; 13224457Smckusick *e_com++ = '/'; /* put '/*' into buffer */ 1338806Smckusick *e_com++ = '*'; 13424457Smckusick if (*buf_ptr != ' ' && !ps.box_com) 1358806Smckusick *e_com++ = ' '; 1368806Smckusick 1378806Smckusick *e_com = '\0'; 13835500Sbostic if (troff) { 13935500Sbostic now_col = 1; 14035500Sbostic adj_max_col = 80; 14135500Sbostic } 14235500Sbostic else 14335500Sbostic now_col = count_spaces(ps.com_col, s_com); /* figure what column we 14435500Sbostic * would be in if we 14535500Sbostic * printed the comment 14635500Sbostic * now */ 1478806Smckusick 14824457Smckusick /* Start to copy the comment */ 1498806Smckusick 15024457Smckusick while (1) { /* this loop will go until the comment is 15124457Smckusick * copied */ 15224457Smckusick if (*buf_ptr > 040 && *buf_ptr != '*') 15324457Smckusick ps.last_nl = 0; 15440275Sbostic CHECK_SIZE_COM; 15524457Smckusick switch (*buf_ptr) { /* this checks for various spcl cases */ 15635500Sbostic case 014: /* check for a form feed */ 15735500Sbostic if (!ps.box_com) { /* in a text comment, break the line here */ 15835500Sbostic ps.use_ff = true; 15935500Sbostic /* fix so dump_line uses a form feed */ 16035500Sbostic dump_line(); 16135500Sbostic last_bl = 0; 16235500Sbostic *e_com++ = ' '; 16335500Sbostic *e_com++ = '*'; 16435500Sbostic *e_com++ = ' '; 16535500Sbostic while (*++buf_ptr == ' ' || *buf_ptr == '\t'); 16635500Sbostic } 16735500Sbostic else { 16835500Sbostic if (++buf_ptr >= buf_end) 16935500Sbostic fill_buffer(); 17035500Sbostic *e_com++ = 014; 17135500Sbostic } 17235500Sbostic break; 17335500Sbostic 17435500Sbostic case '\n': 17535500Sbostic if (had_eof) { /* check for unexpected eof */ 17635500Sbostic printf("Unterminated comment\n"); 17735500Sbostic *e_com = '\0'; 17835500Sbostic dump_line(); 17935500Sbostic return; 18035500Sbostic } 18135500Sbostic one_liner = 0; 18235500Sbostic if (ps.box_com || ps.last_nl) { /* if this is a boxed comment, 18335500Sbostic * we dont ignore the newline */ 18435500Sbostic if (s_com == e_com) { 1858806Smckusick *e_com++ = ' '; 1868806Smckusick *e_com++ = ' '; 1878806Smckusick } 18835500Sbostic *e_com = '\0'; 18935500Sbostic if (!ps.box_com && e_com - s_com > 3) { 19035500Sbostic if (break_delim == 1 && s_com[0] == '/' 19124457Smckusick && s_com[1] == '*' && s_com[2] == ' ') { 19235500Sbostic char *t = e_com; 19335500Sbostic break_delim = 2; 19435500Sbostic e_com = s_com + 2; 19535500Sbostic *e_com = 0; 19635500Sbostic if (blanklines_before_blockcomments) 19735500Sbostic prefix_blankline_requested = 1; 19824457Smckusick dump_line(); 19935500Sbostic e_com = t; 20035500Sbostic s_com[0] = s_com[1] = s_com[2] = ' '; 2018806Smckusick } 20224457Smckusick dump_line(); 20340275Sbostic CHECK_SIZE_COM; 20435500Sbostic *e_com++ = ' '; 20535500Sbostic *e_com++ = ' '; 20635500Sbostic } 20735500Sbostic dump_line(); 20835500Sbostic now_col = ps.com_col; 20935500Sbostic } 21035500Sbostic else { 21124457Smckusick ps.last_nl = 1; 21235500Sbostic if (unix_comment != 1) { /* we not are in unix_style 21335500Sbostic * comment */ 2148806Smckusick if (unix_comment == 0 && s_code == e_code) { 21524457Smckusick /* 21624457Smckusick * if it is a UNIX-style comment, ignore the 21724457Smckusick * requirement that previous line be blank for 21835500Sbostic * unindention 21924457Smckusick */ 22024457Smckusick ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; 22124457Smckusick if (ps.com_col <= 1) 22224457Smckusick ps.com_col = 2; 2238806Smckusick } 22435500Sbostic unix_comment = 2; /* permanently remember that we are in 22535500Sbostic * this type of comment */ 22624457Smckusick dump_line(); 2278806Smckusick ++line_no; 22824457Smckusick now_col = ps.com_col; 2298806Smckusick *e_com++ = ' '; 23024457Smckusick /* 23135500Sbostic * fix so that the star at the start of the line will line 23235500Sbostic * up 23324457Smckusick */ 23424457Smckusick do /* flush leading white space */ 2358806Smckusick if (++buf_ptr >= buf_end) 23624457Smckusick fill_buffer(); 2378806Smckusick while (*buf_ptr == ' ' || *buf_ptr == '\t'); 2388806Smckusick break; 2398806Smckusick } 2408806Smckusick if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t') 2418806Smckusick last_bl = e_com - 1; 24224457Smckusick /* 24335500Sbostic * if there was a space at the end of the last line, remember 24435500Sbostic * where it was 24524457Smckusick */ 24624457Smckusick else { /* otherwise, insert one */ 2478806Smckusick last_bl = e_com; 24840275Sbostic CHECK_SIZE_COM; 2498806Smckusick *e_com++ = ' '; 2508806Smckusick ++now_col; 2518806Smckusick } 25235500Sbostic } 25335500Sbostic ++line_no; /* keep track of input line number */ 25435500Sbostic if (!ps.box_com) { 25535500Sbostic int nstar = 1; 25635500Sbostic do { /* flush any blanks and/or tabs at start of 25735500Sbostic * next line */ 25835500Sbostic if (++buf_ptr >= buf_end) 25935500Sbostic fill_buffer(); 26035500Sbostic if (*buf_ptr == '*' && --nstar >= 0) { 26124457Smckusick if (++buf_ptr >= buf_end) 26224457Smckusick fill_buffer(); 26335500Sbostic if (*buf_ptr == '/') 26435500Sbostic goto end_of_comment; 26535500Sbostic } 26635500Sbostic } while (*buf_ptr == ' ' || *buf_ptr == '\t'); 26735500Sbostic } 26835500Sbostic else if (++buf_ptr >= buf_end) 26935500Sbostic fill_buffer(); 27035500Sbostic break; /* end of case for newline */ 2718806Smckusick 27235500Sbostic case '*': /* must check for possibility of being at end 27335500Sbostic * of comment */ 27435500Sbostic if (++buf_ptr >= buf_end) /* get to next char after * */ 27535500Sbostic fill_buffer(); 2768806Smckusick 27735500Sbostic if (unix_comment == 0) /* set flag to show we are not in 27824457Smckusick * unix-style comment */ 27935500Sbostic unix_comment = 1; 2808806Smckusick 28135500Sbostic if (*buf_ptr == '/') { /* it is the end!!! */ 28235500Sbostic end_of_comment: 28335500Sbostic if (++buf_ptr >= buf_end) 28435500Sbostic fill_buffer(); 2858806Smckusick 28635500Sbostic if (*(e_com - 1) != ' ' && !ps.box_com) { /* insure blank before 28724457Smckusick * end */ 28835500Sbostic *e_com++ = ' '; 28935500Sbostic ++now_col; 29035500Sbostic } 29135500Sbostic if (break_delim == 1 && !one_liner && s_com[0] == '/' 29224457Smckusick && s_com[1] == '*' && s_com[2] == ' ') { 29335500Sbostic char *t = e_com; 29435500Sbostic break_delim = 2; 29535500Sbostic e_com = s_com + 2; 29635500Sbostic *e_com = 0; 29735500Sbostic if (blanklines_before_blockcomments) 29835500Sbostic prefix_blankline_requested = 1; 29935500Sbostic dump_line(); 30035500Sbostic e_com = t; 30135500Sbostic s_com[0] = s_com[1] = s_com[2] = ' '; 30235500Sbostic } 30335500Sbostic if (break_delim == 2 && e_com > s_com + 3 30424457Smckusick /* now_col > adj_max_col - 2 && !ps.box_com */ ) { 3058806Smckusick *e_com = '\0'; 30635500Sbostic dump_line(); 30735500Sbostic now_col = ps.com_col; 3088806Smckusick } 30940275Sbostic CHECK_SIZE_COM; 31035500Sbostic *e_com++ = '*'; 31135500Sbostic *e_com++ = '/'; 31235500Sbostic *e_com = '\0'; 31335500Sbostic ps.just_saw_decl = l_just_saw_decl; 31435500Sbostic return; 31535500Sbostic } 31635500Sbostic else { /* handle isolated '*' */ 31735500Sbostic *e_com++ = '*'; 31835500Sbostic ++now_col; 31935500Sbostic } 32035500Sbostic break; 32135500Sbostic default: /* we have a random char */ 32235500Sbostic if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t') 32335500Sbostic unix_comment = 1; /* we are not in unix-style comment */ 3248806Smckusick 32535500Sbostic *e_com = *buf_ptr++; 32635500Sbostic if (buf_ptr >= buf_end) 32735500Sbostic fill_buffer(); 3288806Smckusick 32935500Sbostic if (*e_com == '\t') /* keep track of column */ 33035500Sbostic now_col = ((now_col - 1) & tabmask) + tabsize + 1; 33135500Sbostic else if (*e_com == '\b') /* this is a backspace */ 33235500Sbostic --now_col; 33335500Sbostic else 33435500Sbostic ++now_col; 3358806Smckusick 33635500Sbostic if (*e_com == ' ' || *e_com == '\t') 33735500Sbostic last_bl = e_com; 33835500Sbostic /* remember we saw a blank */ 3398806Smckusick 34035500Sbostic ++e_com; 34135500Sbostic if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') { 34235500Sbostic /* 34335500Sbostic * the comment is too long, it must be broken up 34435500Sbostic */ 34535500Sbostic if (break_delim == 1 && s_com[0] == '/' 34624457Smckusick && s_com[1] == '*' && s_com[2] == ' ') { 34735500Sbostic char *t = e_com; 34835500Sbostic break_delim = 2; 34935500Sbostic e_com = s_com + 2; 35035500Sbostic *e_com = 0; 35135500Sbostic if (blanklines_before_blockcomments) 35235500Sbostic prefix_blankline_requested = 1; 35324457Smckusick dump_line(); 35435500Sbostic e_com = t; 35535500Sbostic s_com[0] = s_com[1] = s_com[2] = ' '; 35635500Sbostic } 35735500Sbostic if (last_bl == 0) { /* we have seen no blanks */ 35835500Sbostic last_bl = e_com; /* fake it */ 3598806Smckusick *e_com++ = ' '; 36035500Sbostic } 36135500Sbostic *e_com = '\0'; /* print what we have */ 36235500Sbostic *last_bl = '\0'; 36335500Sbostic while (last_bl > s_com && last_bl[-1] < 040) 36435500Sbostic *--last_bl = 0; 36535500Sbostic e_com = last_bl; 36635500Sbostic dump_line(); 3678806Smckusick 36835500Sbostic *e_com++ = ' '; /* add blanks for continuation */ 36935500Sbostic *e_com++ = ' '; 37035500Sbostic *e_com++ = ' '; 37135500Sbostic 37235500Sbostic t_ptr = last_bl + 1; 37335500Sbostic last_bl = 0; 37435500Sbostic if (t_ptr >= e_com) { 37535500Sbostic while (*t_ptr == ' ' || *t_ptr == '\t') 37635500Sbostic t_ptr++; 37735500Sbostic while (*t_ptr != '\0') { /* move unprinted part of 37835500Sbostic * comment down in buffer */ 37935500Sbostic if (*t_ptr == ' ' || *t_ptr == '\t') 38035500Sbostic last_bl = e_com; 38135500Sbostic *e_com++ = *t_ptr++; 3828806Smckusick } 38335500Sbostic } 38435500Sbostic *e_com = '\0'; 38535500Sbostic now_col = count_spaces(ps.com_col, s_com); /* recompute current 38624457Smckusick * position */ 38735500Sbostic } 38835500Sbostic break; 38924457Smckusick } 39024457Smckusick } 39124457Smckusick } 392