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