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