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