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