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