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