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