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