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