1*21972Sdist /* 2*21972Sdist * Copyright (c) 1980 Regents of the University of California. 3*21972Sdist * All rights reserved. The Berkeley software License Agreement 4*21972Sdist * specifies the terms and conditions for redistribution. 5*21972Sdist */ 68806Smckusick 7*21972Sdist #ifndef lint 8*21972Sdist static char sccsid[] = "@(#)pr_comment.c 5.1 (Berkeley) 06/04/85"; 9*21972Sdist #endif not lint 10*21972Sdist 118806Smckusick /* 128806Smckusick 138806Smckusick Copyright (C) 1976 148806Smckusick by the 158806Smckusick Board of Trustees 168806Smckusick of the 178806Smckusick University of Illinois 188806Smckusick 198806Smckusick All rights reserved 208806Smckusick 218806Smckusick 228806Smckusick NAME: 238806Smckusick pr_comment 248806Smckusick 258806Smckusick FUNCTION: 268806Smckusick This routine takes care of scanning and printing comments. 278806Smckusick 288806Smckusick ALGORITHM: 298806Smckusick 1) Decide where the comment should be aligned, and if lines should 308806Smckusick be broken. 318806Smckusick 2) If lines should not be broken and filled, just copy up to end of 328806Smckusick comment. 338806Smckusick 3) If lines should be filled, then scan thru input_buffer copying 348806Smckusick characters to com_buf. Remember where the last blank, tab, or 358806Smckusick newline was. When line is filled, print up to last blank and 368806Smckusick continue copying. 378806Smckusick 388806Smckusick PARAMETERS: 398806Smckusick None 408806Smckusick 418806Smckusick RETURNS: 428806Smckusick Nothing 438806Smckusick 448806Smckusick GLOBALS: 458806Smckusick combuf = 468806Smckusick s_com 478806Smckusick e_com = 488806Smckusick 498806Smckusick buf_ptr = 508806Smckusick buf_end 518806Smckusick 528806Smckusick bl_line 538806Smckusick col_1 548806Smckusick com_col = 558806Smckusick com_ind 568806Smckusick decl_com_ind 578806Smckusick decl_on_line 588806Smckusick had_eof 598806Smckusick ind_level 608806Smckusick ind_size 618806Smckusick line_no = 628806Smckusick max_col 638806Smckusick out_com = Count number of comments 648806Smckusick unindent_displace 658806Smckusick use_ff = 668806Smckusick 678806Smckusick CALLS: 688806Smckusick count_spaces 698806Smckusick dump_line 708806Smckusick fill_buffer 718806Smckusick printf (lib) 728806Smckusick 738806Smckusick CALLED BY: 748806Smckusick main 758806Smckusick 768806Smckusick HISTORY: 778806Smckusick November 1976 D A Willcox of CAC Initial coding 788806Smckusick 12/6/76 D A Willcox of CAC Modification to handle 798806Smckusick UNIX-style comments 808806Smckusick 818806Smckusick */ 828806Smckusick 838806Smckusick /* this routine processes comments. It makes an attempt to keep comments from 848806Smckusick going over the max line length. If a line is too long, it moves everything 858806Smckusick from the last blank to the next comment line. Blanks and tabs from the 868806Smckusick beginning of the input line are removed */ 878806Smckusick 888806Smckusick #include "indent_globs.h"; 898806Smckusick 908806Smckusick 918806Smckusick pr_comment () { 928806Smckusick int now_col; 938806Smckusick /* column we are in now */ 948806Smckusick int box_com; 958806Smckusick /* set to true when we are in a "boxed" comment. In that case, the first 968806Smckusick non-blank char should be lined up with the / in /* */ 978806Smckusick int col_1_com; 988806Smckusick /* this comment should not be touched */ 998806Smckusick char *last_bl; 1008806Smckusick /* points to the last blank in the output buffer */ 1018806Smckusick char achar; 1028806Smckusick char *t_ptr; /* used for movinf string */ 1038806Smckusick int unix_comment; 1048806Smckusick /* tri-state variable used to decide if it is a unix-style comment. 0 means 1058806Smckusick only blanks since /*, 1 means regular style comment, 2 means unix style 1068806Smckusick comment */ 1078806Smckusick 1088806Smckusick 1098806Smckusick last_bl = 0; /* no blanks found so far */ 1108806Smckusick box_com = col_1_com = false; 1118806Smckusick /* at first, assume that we are not in a boxed comment or some other comment 1128806Smckusick that should not be touched */ 1138806Smckusick ++out_coms; /* keep track of number of comments */ 1148806Smckusick unix_comment = 0; /* set flag to let us figure out if there is a 1158806Smckusick unix-style comment */ 1168806Smckusick 1178806Smckusick /*----------------------------------------------------------*\ 1188806Smckusick | Figure where to align and how to treat the comment 1198806Smckusick \*----------------------------------------------------------*/ 1208806Smckusick 1218806Smckusick if (col_1) { /* if comment starts in column 1 it should not 1228806Smckusick be touched */ 1238806Smckusick col_1_com = box_com = true; 1248806Smckusick com_col = 1; 1258806Smckusick } 1268806Smckusick else { 1278806Smckusick if (*buf_ptr == '-') 1288806Smckusick box_com = true; /* a comment with a '-' immediately after the /* 1298806Smckusick is assumed to be a boxed comment */ 1308806Smckusick if ( /* bl_line && */ (s_lab == e_lab) && (s_code == e_code)) { 1318806Smckusick /* klg: check only if this line is blank */ 1328806Smckusick /* 1338806Smckusick * If this (*and previous lines are*) blank, 1348806Smckusick * don't put comment way out at left 1358806Smckusick */ 1368806Smckusick com_col = (ind_level - unindent_displace) * ind_size + 1; 1378806Smckusick if (com_col <= 1) 1388806Smckusick com_col = 2; 1398806Smckusick } 1408806Smckusick else { 1418806Smckusick com_col = (decl_on_line || ind_level == 0 ? decl_com_ind : com_ind); 1428806Smckusick } 1438806Smckusick } 1448806Smckusick 1458806Smckusick *e_com++ = '/'; /* put '/*' into buffer */ 1468806Smckusick *e_com++ = '*'; 1478806Smckusick if (*buf_ptr != ' ' && !box_com) 1488806Smckusick *e_com++ = ' '; 1498806Smckusick 1508806Smckusick *e_com = '\0'; 1518806Smckusick now_col = count_spaces (com_col, s_com); 1528806Smckusick /* figure where what column we would be in if we printed the comment now */ 1538806Smckusick 1548806Smckusick 1558806Smckusick /*----------------------------------------------------------*\ 1568806Smckusick | Start to copy the comment 1578806Smckusick \*----------------------------------------------------------*/ 1588806Smckusick 1598806Smckusick while (1) { /* this loop will go until the comment is copied 1608806Smckusick */ 1618806Smckusick switch (*buf_ptr) { /* this checks for various spcl cases */ 1628806Smckusick case 014: /* check for a form feed */ 1638806Smckusick if (!box_com) {/* in a text comment, break the line here */ 1648806Smckusick use_ff = true; 1658806Smckusick /* fix so dump_line uses a form feed */ 1668806Smckusick dump_line (); 1678806Smckusick last_bl = 0; 1688806Smckusick *e_com++ = ' '; 1698806Smckusick *e_com++ = ' '; 1708806Smckusick *e_com++ = ' '; 1718806Smckusick do { /* get rid of leading blanks */ 1728806Smckusick if (++buf_ptr >= buf_end) 1738806Smckusick fill_buffer (); 1748806Smckusick } while (*buf_ptr == ' ' || *buf_ptr == '\t'); 1758806Smckusick } 1768806Smckusick else { 1778806Smckusick if (++buf_ptr >= buf_end) 1788806Smckusick fill_buffer (); 1798806Smckusick *e_com++ = 014; 1808806Smckusick } 1818806Smckusick 1828806Smckusick break; 1838806Smckusick 1848806Smckusick case '\n': 1858806Smckusick if (had_eof) { /* check for unexpected eof */ 1868806Smckusick printf ("Unterminated comment\n"); 1878806Smckusick *e_com = '\0'; 1888806Smckusick dump_line (); 1898806Smckusick return; 1908806Smckusick } 1918806Smckusick 1928806Smckusick if (box_com) { /* if this is a boxed comment, we don't ignore 1938806Smckusick the newline */ 1948806Smckusick *e_com = '\0'; 1958806Smckusick dump_line (); 1968806Smckusick ++line_no; 1978806Smckusick now_col = com_col; 1988806Smckusick 1998806Smckusick if (!col_1_com) { 2008806Smckusick /* if merely a boxed comment, we should line up first 2018806Smckusick non-blank character */ 2028806Smckusick do { /* flush leading non-blanks */ 2038806Smckusick if (++buf_ptr >= buf_end) 2048806Smckusick fill_buffer (); 2058806Smckusick } while (*buf_ptr == ' ' || *buf_ptr == '\t'); 2068806Smckusick } 2078806Smckusick else { /* make sure we at least flush the blank */ 2088806Smckusick if (++buf_ptr >= buf_end) 2098806Smckusick fill_buffer (); 2108806Smckusick } 2118806Smckusick 2128806Smckusick break; 2138806Smckusick } 2148806Smckusick 2158806Smckusick if (unix_comment != 1) { 2168806Smckusick /* we are in unix_style comment */ 2178806Smckusick if (unix_comment == 0 && s_code == e_code) { 2188806Smckusick /* if it is a UNIX-style comment, ignore the requirement 2198806Smckusick that pervious line be blank for unindention */ 2208806Smckusick com_col = (ind_level - unindent_displace) * ind_size + 1; 2218806Smckusick if (com_col <= 1) 2228806Smckusick com_col = 2; 2238806Smckusick } 2248806Smckusick 2258806Smckusick unix_comment = 2; 2268806Smckusick /* permanently remember that we are in this type of comment */ 2278806Smckusick dump_line (); 2288806Smckusick ++line_no; 2298806Smckusick now_col = com_col; 2308806Smckusick *e_com++ = ' '; 2318806Smckusick /* fix so that the star at the start of the line will line up 2328806Smckusick */ 2338806Smckusick do /* flush leading white space */ 2348806Smckusick if (++buf_ptr >= buf_end) 2358806Smckusick fill_buffer (); 2368806Smckusick while (*buf_ptr == ' ' || *buf_ptr == '\t'); 2378806Smckusick break; 2388806Smckusick } 2398806Smckusick 2408806Smckusick if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t') 2418806Smckusick last_bl = e_com - 1; 2428806Smckusick /* if there was a space at the end of the last line, remember where 2438806Smckusick it was */ 2448806Smckusick else { /* otherwise, insert one */ 2458806Smckusick last_bl = e_com; 2468806Smckusick *e_com++ = ' '; 2478806Smckusick ++now_col; 2488806Smckusick } 2498806Smckusick 2508806Smckusick ++line_no; /* keep track of input line number */ 2518806Smckusick do { /* copy any blanks and/or tabs at start of next 2528806Smckusick line */ 2538806Smckusick if (++buf_ptr >= buf_end) 2548806Smckusick fill_buffer (); 2558806Smckusick } while (*buf_ptr == ' ' || *buf_ptr == '\t'); 2568806Smckusick 2578806Smckusick break; /* end of case for newline */ 2588806Smckusick 2598806Smckusick case '*': /* must check for possibility of being at end of 2608806Smckusick comment */ 2618806Smckusick if (++buf_ptr >= buf_end) 2628806Smckusick /* get to next char after * */ 2638806Smckusick fill_buffer (); 2648806Smckusick 2658806Smckusick if (unix_comment == 0) 2668806Smckusick /* set flag to show we are not in unix-style 2678806Smckusick comment */ 2688806Smckusick unix_comment = 1; 2698806Smckusick 2708806Smckusick if (*buf_ptr == '/') { 2718806Smckusick /* it is the end!!! */ 2728806Smckusick if (++buf_ptr >= buf_end) 2738806Smckusick fill_buffer (); 2748806Smckusick 2758806Smckusick if (*(e_com - 1) != ' ' && !box_com) { 2768806Smckusick /* insure blank before end */ 2778806Smckusick *e_com++ = ' '; 2788806Smckusick ++now_col; 2798806Smckusick } 2808806Smckusick 2818806Smckusick if (now_col > max_col - 2 && !box_com) { 2828806Smckusick /* check if star-slash will go over line */ 2838806Smckusick *e_com = '\0'; 2848806Smckusick /* it will */ 2858806Smckusick dump_line (); 2868806Smckusick now_col = com_col; 2878806Smckusick } 2888806Smckusick 2898806Smckusick *e_com++ = '*'; 2908806Smckusick /* move end of comment */ 2918806Smckusick *e_com++ = '/'; 2928806Smckusick *e_com = '\0'; 2938806Smckusick return; /* we is done */ 2948806Smckusick } /* end of end of comment */ 2958806Smckusick 2968806Smckusick 2978806Smckusick else { /* handle isolated '*' */ 2988806Smckusick *e_com++ = '*'; 2998806Smckusick ++now_col; 3008806Smckusick break; 3018806Smckusick } 3028806Smckusick /* end of processing of * */ 3038806Smckusick 3048806Smckusick default: /* we have a random char */ 3058806Smckusick if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t') 3068806Smckusick unix_comment = 1; 3078806Smckusick /* we are not in unix-style comment */ 3088806Smckusick 3098806Smckusick *e_com = *buf_ptr++; 3108806Smckusick if (buf_ptr >= buf_end) 3118806Smckusick fill_buffer (); 3128806Smckusick 3138806Smckusick if (*e_com == '\t') 3148806Smckusick /* keep track of column */ 3158806Smckusick now_col = ((now_col - 1) & tabmask) + tabsize + 1; 3168806Smckusick else 3178806Smckusick if (*e_com == '') 3188806Smckusick /* this is a backspace */ 3198806Smckusick --now_col; 3208806Smckusick else 3218806Smckusick ++now_col; 3228806Smckusick 3238806Smckusick if (*e_com == ' ' || *e_com == '\t') 3248806Smckusick last_bl = e_com; 3258806Smckusick /* remember we saw a blank */ 3268806Smckusick 3278806Smckusick ++e_com; 3288806Smckusick if (now_col > max_col && !box_com && unix_comment == 1) { 3298806Smckusick /* the comment is too long, it must be broken up */ 3308806Smckusick if (last_bl == 0) { 3318806Smckusick /* we have seen no blanks */ 3328806Smckusick printf ("%d: Comment too long\n", line_no); 3338806Smckusick last_bl = e_com; 3348806Smckusick /* fake it */ 3358806Smckusick *e_com++ = ' '; 3368806Smckusick } 3378806Smckusick 3388806Smckusick *e_com = '\0'; 3398806Smckusick /* print what we have */ 3408806Smckusick *last_bl = '\0'; 3418806Smckusick e_com = last_bl; 3428806Smckusick dump_line (); 3438806Smckusick 3448806Smckusick *e_com++ = ' '; 3458806Smckusick /* add blanks for continuation */ 3468806Smckusick *e_com++ = ' '; 3478806Smckusick *e_com++ = ' '; 3488806Smckusick 3498806Smckusick t_ptr = last_bl + 1; 3508806Smckusick last_bl = 0; 3518806Smckusick while (*t_ptr != '\0') { 3528806Smckusick /* move unprinted pare of comment down in buffer */ 3538806Smckusick if (*t_ptr == ' ' || *t_ptr == '\t') 3548806Smckusick last_bl = e_com; 3558806Smckusick *e_com++ = *t_ptr++; 3568806Smckusick } 3578806Smckusick 3588806Smckusick *e_com = '\0'; 3598806Smckusick now_col = count_spaces (com_col, s_com); 3608806Smckusick /* recompute current position */ 3618806Smckusick } /* end of code for splitting a comment */ 3628806Smckusick break; /* end of default case */ 3638806Smckusick 3648806Smckusick 3658806Smckusick } /* end of switch */ 3668806Smckusick 3678806Smckusick } /* end of while (1) */ 3688806Smckusick } /* end of pr_comment */ 369