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