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