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