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