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