xref: /original-bsd/usr.bin/indent/pr_comment.c (revision ec7df300)
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.1 (Berkeley) 06/04/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 PARAMETERS:
39 	None
40 
41 RETURNS:
42 	Nothing
43 
44 GLOBALS:
45 	combuf =
46 	s_com
47 	e_com =
48 
49 	buf_ptr =
50 	buf_end
51 
52 	bl_line
53 	col_1
54 	com_col =
55 	com_ind
56 	decl_com_ind
57 	decl_on_line
58 	had_eof
59 	ind_level
60 	ind_size
61 	line_no =
62 	max_col
63 	out_com =	Count number of comments
64 	unindent_displace
65 	use_ff =
66 
67 CALLS:
68 	count_spaces
69 	dump_line
70 	fill_buffer
71 	printf		(lib)
72 
73 CALLED BY:
74 	main
75 
76 HISTORY:
77 	November 1976	D A Willcox of CAC	Initial coding
78 	12/6/76		D A Willcox of CAC	Modification to handle
79 						UNIX-style comments
80 
81 */
82 
83 /* this routine processes comments.  It makes an attempt to keep comments from
84    going over the max line length.  If a line is too long, it moves everything
85    from the last blank to the next comment line.  Blanks and tabs from the
86    beginning of the input line are removed */
87 
88 #include "indent_globs.h";
89 
90 
91 pr_comment () {
92     int     now_col;
93  /* column we are in now */
94     int     box_com;
95  /* set to true when we are in a "boxed" comment. In that case, the first
96     non-blank char should be lined up with the / in /* */
97     int     col_1_com;
98  /* this comment should not be touched */
99     char   *last_bl;
100  /* points to the last blank in the output buffer */
101     char    achar;
102     char   *t_ptr; /* used for movinf string */
103     int     unix_comment;
104  /* tri-state variable used to decide if it is a unix-style comment. 0 means
105     only blanks since /*, 1 means regular style comment, 2 means unix style
106     comment */
107 
108 
109     last_bl = 0;	       /* no blanks found so far */
110     box_com = col_1_com = false;
111  /* at first, assume that we are not in a boxed comment or some other comment
112     that should not be touched */
113     ++out_coms;		       /* keep track of number of comments */
114     unix_comment = 0;	       /* set flag to let us figure out if there is a
115 			          unix-style comment */
116 
117 /*----------------------------------------------------------*\
118 |   Figure where to align and how to treat the comment
119 \*----------------------------------------------------------*/
120 
121     if (col_1) {	       /* if comment starts in column 1 it should not
122 			          be touched */
123 	col_1_com = box_com = true;
124 	com_col = 1;
125     }
126     else {
127 	if (*buf_ptr == '-')
128 	    box_com = true;    /* a comment with a '-' immediately after the /*
129 			          is assumed to be a boxed comment */
130 	if ( /* bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
131 	/* klg: check only if this line is blank */
132 	/*
133 	 * If this (*and previous lines are*) blank,
134 	 * don't put comment way out at left
135 	 */
136 	    com_col = (ind_level - unindent_displace) * ind_size + 1;
137 	    if (com_col <= 1)
138 		com_col = 2;
139 	}
140 	else {
141 	    com_col = (decl_on_line || ind_level == 0 ? decl_com_ind : com_ind);
142 	}
143     }
144 
145     *e_com++ = '/';	       /* put '/*' into buffer */
146     *e_com++ = '*';
147     if (*buf_ptr != ' ' && !box_com)
148 	*e_com++ = ' ';
149 
150     *e_com = '\0';
151     now_col = count_spaces (com_col, s_com);
152  /* figure where what column we would be in if we printed the comment now */
153 
154 
155 /*----------------------------------------------------------*\
156 |    Start to copy the comment
157 \*----------------------------------------------------------*/
158 
159     while (1) {		       /* this loop will go until the comment is copied
160 			       */
161 	switch (*buf_ptr) {    /* this checks for various spcl cases */
162 	    case 014: 	       /* check for a form feed */
163 		if (!box_com) {/* in a text comment, break the line here */
164 		    use_ff = true;
165 		/* fix so dump_line uses a form feed */
166 		    dump_line ();
167 		    last_bl = 0;
168 		    *e_com++ = ' ';
169 		    *e_com++ = ' ';
170 		    *e_com++ = ' ';
171 		    do {       /* get rid of leading blanks */
172 			if (++buf_ptr >= buf_end)
173 			    fill_buffer ();
174 		    } while (*buf_ptr == ' ' || *buf_ptr == '\t');
175 		}
176 		else {
177 		    if (++buf_ptr >= buf_end)
178 			fill_buffer ();
179 		    *e_com++ = 014;
180 		}
181 
182 		break;
183 
184 	    case '\n':
185 		if (had_eof) { /* check for unexpected eof */
186 		    printf ("Unterminated comment\n");
187 		    *e_com = '\0';
188 		    dump_line ();
189 		    return;
190 		}
191 
192 		if (box_com) { /* if this is a boxed comment, we don't ignore
193 			          the newline */
194 		    *e_com = '\0';
195 		    dump_line ();
196 		    ++line_no;
197 		    now_col = com_col;
198 
199 		    if (!col_1_com) {
200 		    /* if merely a boxed comment, we should line up first
201 		       non-blank character */
202 			do {   /* flush leading non-blanks */
203 			    if (++buf_ptr >= buf_end)
204 				fill_buffer ();
205 			} while (*buf_ptr == ' ' || *buf_ptr == '\t');
206 		    }
207 		    else {     /* make sure we at least flush the blank */
208 			if (++buf_ptr >= buf_end)
209 			    fill_buffer ();
210 		    }
211 
212 		    break;
213 		}
214 
215 		if (unix_comment != 1) {
216 		/* we are in unix_style comment */
217 		    if (unix_comment == 0 && s_code == e_code) {
218 		    /* if it is a UNIX-style comment, ignore the requirement
219 		       that pervious line be blank for unindention */
220 			com_col = (ind_level - unindent_displace) * ind_size + 1;
221 			if (com_col <= 1)
222 			    com_col = 2;
223 		    }
224 
225 		    unix_comment = 2;
226 		/* permanently remember that we are in this type of comment */
227 		    dump_line ();
228 		    ++line_no;
229 		    now_col = com_col;
230 		    *e_com++ = ' ';
231 		/* fix so that the star at the start of the line will line up
232 		*/
233 		    do	       /* flush leading white space */
234 			if (++buf_ptr >= buf_end)
235 			    fill_buffer ();
236 		    while (*buf_ptr == ' ' || *buf_ptr == '\t');
237 		    break;
238 		}
239 
240 		if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
241 		    last_bl = e_com - 1;
242 	    /* if there was a space at the end of the last line, remember where
243 	       it was */
244 		else {	       /* otherwise, insert one */
245 		    last_bl = e_com;
246 		    *e_com++ = ' ';
247 		    ++now_col;
248 		}
249 
250 		++line_no;     /* keep track of input line number */
251 		do {	       /* copy any blanks and/or tabs at start of next
252 			          line */
253 		    if (++buf_ptr >= buf_end)
254 			fill_buffer ();
255 		} while (*buf_ptr == ' ' || *buf_ptr == '\t');
256 
257 		break;	       /* end of case for newline */
258 
259 	    case '*': 	       /* must check for possibility of being at end of
260 			          comment */
261 		if (++buf_ptr >= buf_end)
262 			       /* get to next char after * */
263 		    fill_buffer ();
264 
265 		if (unix_comment == 0)
266 			       /* set flag to show we are not in unix-style
267 			          comment */
268 		    unix_comment = 1;
269 
270 		if (*buf_ptr == '/') {
271 		/* it is the end!!! */
272 		    if (++buf_ptr >= buf_end)
273 			fill_buffer ();
274 
275 		    if (*(e_com - 1) != ' ' && !box_com) {
276 		    /* insure blank before end */
277 			*e_com++ = ' ';
278 			++now_col;
279 		    }
280 
281 		    if (now_col > max_col - 2 && !box_com) {
282 		    /* check if star-slash will go over line */
283 			*e_com = '\0';
284 		    /* it will */
285 			dump_line ();
286 			now_col = com_col;
287 		    }
288 
289 		    *e_com++ = '*';
290 		/* move end of comment */
291 		    *e_com++ = '/';
292 		    *e_com = '\0';
293 		    return;    /* we is done */
294 		}	       /* end of end of comment */
295 
296 
297 		else {	       /* handle isolated '*' */
298 		    *e_com++ = '*';
299 		    ++now_col;
300 		    break;
301 		}
302 	    /* end of processing of * */
303 
304 	    default: 	       /* we have a random char */
305 		if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
306 		    unix_comment = 1;
307 	    /* we are not in unix-style comment */
308 
309 		*e_com = *buf_ptr++;
310 		if (buf_ptr >= buf_end)
311 		    fill_buffer ();
312 
313 		if (*e_com == '\t')
314 			       /* keep track of column */
315 		    now_col = ((now_col - 1) & tabmask) + tabsize + 1;
316 		else
317 		    if (*e_com == '')
318 			       /* this is a backspace */
319 			--now_col;
320 		    else
321 			++now_col;
322 
323 		if (*e_com == ' ' || *e_com == '\t')
324 		    last_bl = e_com;
325 	    /* remember we saw a blank */
326 
327 		++e_com;
328 		if (now_col > max_col && !box_com && unix_comment == 1) {
329 		/* the comment is too long, it must be broken up */
330 		    if (last_bl == 0) {
331 		    /* we have seen no blanks */
332 			printf ("%d: Comment too long\n", line_no);
333 			last_bl = e_com;
334 		    /* fake it */
335 			*e_com++ = ' ';
336 		    }
337 
338 		    *e_com = '\0';
339 		/* print what we have */
340 		    *last_bl = '\0';
341 		    e_com = last_bl;
342 		    dump_line ();
343 
344 		    *e_com++ = ' ';
345 		/* add blanks for continuation */
346 		    *e_com++ = ' ';
347 		    *e_com++ = ' ';
348 
349 		    t_ptr = last_bl + 1;
350 		    last_bl = 0;
351 		    while (*t_ptr != '\0') {
352 		    /* move unprinted pare of comment down in buffer */
353 			if (*t_ptr == ' ' || *t_ptr == '\t')
354 			    last_bl = e_com;
355 			*e_com++ = *t_ptr++;
356 		    }
357 
358 		    *e_com = '\0';
359 		    now_col = count_spaces (com_col, s_com);
360 		/* recompute current position */
361 		}	       /* end of code for splitting a comment */
362 		break;	       /* end of default case */
363 
364 
365 	}		       /* end of switch */
366 
367     }			       /* end of while (1) */
368 }		   /* end of pr_comment */
369