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