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