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