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