pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / indent / comment.c
bloba4625a46f24d69e349eab489acfafa9d834969a6
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.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the University of California, Berkeley, the University of Illinois,
13 * Urbana, and Sun Microsystems, Inc. The name of either University
14 * or Sun Microsystems may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 * NAME: pr_comment
24 * FUNCTION: This routine takes care of scanning and printing comments.
26 * ALGORITHM: 1) Decide where the comment should be aligned, and if lines should
27 * be broken. 2) If lines should not be broken and filled, just copy up to
28 * end of comment. 3) If lines should be filled, then scan thru input_buffer
29 * copying characters to com_buf. Remember where the last blank, tab, or
30 * newline was. When line is filled, print up to last blank and continue
31 * copying.
33 * HISTORY: November 1976 D A Willcox of CAC Initial coding 12/6/76
34 * A Willcox of CAC Modification to handle UNIX-style comments
36 */\f
39 * this routine processes comments. It makes an attempt to keep comments
40 * from going over the max line length. If a line is too long, it moves
41 * everything from the last blank to the next comment line. Blanks and tabs
42 * from the beginning of the input line are removed
46 #define PUBLIC extern
47 #include <stdlib.h>
48 #include "globs.h"
49 #include "proto.h"
52 void pr_comment()
54 int now_col; /* column we are in now */
56 int adj_max_col; /* Adjusted max_col for when we
57 * decide to spill comments
58 * over the right margin
61 char *last_bl; /* points to the last blank in
62 * the output buffer
65 char *t_ptr; /* used for moving string */
67 int unix_comment; /* tri-state variable used to
68 * decide if it is a unix-style
69 * comment. 0 means only blanks
70 * since / *, 1 means regular
71 * style comment, 2 means unix
72 * style comment
75 int break_delim = del_on_bl;
76 int l_just_saw_decl = ps.just_saw_decl;
77 /* int ps.last_nl = 0; */ /* true iff the last
78 sig thing we have seen is a nl */
80 int one_liner = 1; /* true iff this comment is a
81 one-liner */
82 adj_max_col = max_col;
83 ps.just_saw_decl = 0;
84 last_bl = 0; /* no blanks found so far */
85 ps.box_com = false; /* at first, assume that we are
86 not in a boxed comment or
87 some other comment that
88 should not be touched */
89 ++ps.out_coms; /* keep track of number of
90 comments */
91 unix_comment = 1; /* set flag to let us figure
92 out if there is a unix-style
93 comment ** DISABLED: use 0
94 to reenable this hack! */
96 /* Figure where to align and how to treat the comment */
98 if (ps.col_1 && !format_col1_comments)
99 { /* if comment starts in column
100 1 it should not be touched */
101 ps.box_com = true;
102 ps.com_col = 1;
103 } else
105 if (*buf_ptr == '-' || *buf_ptr == '*')
107 ps.box_com = true; /* a comment with a '-' or '*'
108 immediately after the / * is
109 assumed to be a boxed
110 comment */
111 break_delim = 0;
113 if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code))
115 /* klg: check only if this line is blank */
116 /* If this (*and previous lines are*) blank, dont put comment
117 way out at left */
118 ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
119 adj_max_col = bk_max_col;
120 if (ps.com_col <= 1)
121 ps.com_col = 1 + !format_col1_comments;
122 } else
124 register target_col;
125 break_delim = 0;
126 if (s_code != e_code)
127 target_col = count_spaces(code_target(), s_code);
128 else
130 target_col = 1;
131 if (s_lab != e_lab)
132 target_col = count_spaces(label_target(), s_lab);
134 ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
135 if (ps.com_col < target_col)
136 ps.com_col = ((target_col + 7) & ~7) + 1;
137 if (ps.com_col + 24 > adj_max_col)
138 adj_max_col = ps.com_col + 24;
141 if (ps.box_com)
143 buf_ptr[-2] = 0;
144 ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
145 buf_ptr[-2] = '/';
146 } else
148 ps.n_comment_delta = 0;
149 while (*buf_ptr == ' ' || *buf_ptr == '\t')
150 buf_ptr++;
152 ps.comment_delta = 0;
153 *e_com++ = '/'; /* put '/ *' into buffer */
154 *e_com++ = '*';
155 if (*buf_ptr != ' ' && !ps.box_com)
156 *e_com++ = ' ';
158 *e_com = '\0';
159 if (troff)
161 now_col = 1;
162 adj_max_col = 80;
163 } else
164 now_col = count_spaces(ps.com_col, s_com); /* figure what column we
165 would be in if we
166 printed the comment
167 now */
169 /* Start to copy the comment */
171 while (1)
172 { /* this loop will go until the
173 comment is copied */
174 if (*buf_ptr > 040 && *buf_ptr != '*')
175 ps.last_nl = 0;
176 if (e_com >= l_com)
178 register nsize = l_com - s_com + 400;
179 combuf = (char *) realloc(combuf, nsize);
180 e_com = combuf + (e_com - s_com) + 1;
181 l_com = combuf + nsize - 5;
182 s_com = combuf + 1;
184 switch (*buf_ptr)
185 { /* this checks for various spcl
186 cases */
187 case 014: /* check for a form feed */
188 if (!ps.box_com)
189 { /* in a text comment, break the
190 line here */
191 ps.use_ff = true;
192 /* fix so dump_line uses a form feed */
193 dump_line();
194 last_bl = 0;
195 *e_com++ = ' ';
196 *e_com++ = '*';
197 *e_com++ = ' ';
198 while (*++buf_ptr == ' ' || *buf_ptr == '\t');
199 } else
201 if (++buf_ptr >= buf_end)
202 fill_buffer();
203 *e_com++ = 014;
205 break;
207 case '\n':
208 if (had_eof)
209 { /* check for unexpected eof */
210 printf("Unterminated comment\n");
211 *e_com = '\0';
212 dump_line();
213 return;
215 one_liner = 0;
216 if (ps.box_com || ps.last_nl)
217 { /* if this is a boxed comment,
218 we dont ignore the newline */
219 if (s_com == e_com)
221 *e_com++ = ' ';
222 *e_com++ = ' ';
224 *e_com = '\0';
225 if (!ps.box_com && e_com - s_com > 3)
227 if (break_delim == 1 && s_com[0] == '/'
228 && s_com[1] == '*' && s_com[2] == ' ')
230 char *t = e_com;
231 break_delim = 2;
232 e_com = s_com + 2;
233 *e_com = 0;
234 if (bl_bef_bk)
235 prefix_blankline_requested = 1;
236 dump_line();
237 e_com = t;
238 s_com[0] = s_com[1] = s_com[2] = ' ';
240 dump_line();
241 if (e_com >= l_com)
243 register nsize = l_com - s_com + 400;
244 combuf = (char *) realloc(combuf, nsize);
245 e_com = combuf + (e_com - s_com) + 1;
246 l_com = combuf + nsize - 5;
247 s_com = combuf + 1;
249 *e_com++ = ' ';
250 *e_com++ = ' ';
252 dump_line();
253 now_col = ps.com_col;
254 } else
256 ps.last_nl = 1;
257 if (unix_comment != 1)
258 { /* we not are in unix_style
259 comment */
260 if (unix_comment == 0 && s_code == e_code)
262 /* if it is a UNIX-style comment, ignore the
263 requirement that previous line be blank for
264 unindention */
265 ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
266 if (ps.com_col <= 1)
267 ps.com_col = 2;
269 unix_comment = 2; /* permanently remember that we
270 are in this type of comment */
271 dump_line();
272 ++line_no;
273 now_col = ps.com_col;
274 *e_com++ = ' ';
275 /* fix so that the star at the start of the line will
276 line up */
277 do /* flush leading white space */
278 if (++buf_ptr >= buf_end)
279 fill_buffer();
280 while (*buf_ptr == ' ' || *buf_ptr == '\t');
281 break;
283 if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
284 last_bl = e_com - 1;
285 /* if there was a space at the end of the last line,
286 remember where it was */
287 else
288 { /* otherwise, insert one */
289 last_bl = e_com;
290 *e_com++ = ' ';
291 if (e_com >= l_com)
293 register nsize = l_com - s_com + 400;
294 combuf = (char *) realloc(combuf, nsize);
295 e_com = combuf + (e_com - s_com) + 1;
296 l_com = combuf + nsize - 5;
297 s_com = combuf + 1;
299 ++now_col;
302 ++line_no; /* keep track of input line
303 number */
304 if (!ps.box_com)
306 int nstar = 1;
308 { /* flush any blanks and/or tabs
309 at start of next line */
310 if (++buf_ptr >= buf_end)
311 fill_buffer();
312 if (*buf_ptr == '*' && --nstar >= 0)
314 if (++buf_ptr >= buf_end)
315 fill_buffer();
316 if (*buf_ptr == '/')
317 goto end_of_comment;
319 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
320 } else if (++buf_ptr >= buf_end)
321 fill_buffer();
322 break; /* end of case for newline */
324 case '*': /* must check for possibility
325 of being at end of comment */
326 if (++buf_ptr >= buf_end) /* get to next char after * */
327 fill_buffer();
329 if (unix_comment == 0) /* set flag to show we are not
330 in unix-style comment */
331 unix_comment = 1;
333 if (*buf_ptr == '/')
334 { /* it is the end!!! */
335 end_of_comment:
336 if (++buf_ptr >= buf_end)
337 fill_buffer();
339 if (*(e_com - 1) != ' ' && !ps.box_com)
340 { /* insure blank before end */
341 *e_com++ = ' ';
342 ++now_col;
344 if (break_delim == 1 && !one_liner && s_com[0] == '/'
345 && s_com[1] == '*' && s_com[2] == ' ')
347 char *t = e_com;
348 break_delim = 2;
349 e_com = s_com + 2;
350 *e_com = 0;
351 if (bl_bef_bk)
352 prefix_blankline_requested = 1;
353 dump_line();
354 e_com = t;
355 s_com[0] = s_com[1] = s_com[2] = ' ';
357 if (break_delim == 2 && e_com > s_com + 3
358 /* now_col > adj_max_col - 2 && !ps.box_com */ )
360 *e_com = '\0';
361 dump_line();
362 now_col = ps.com_col;
364 if (e_com >= l_com)
366 register nsize = l_com - s_com + 400;
367 combuf = (char *) realloc(combuf, nsize);
368 e_com = combuf + (e_com - s_com) + 1;
369 l_com = combuf + nsize - 5;
370 s_com = combuf + 1;
372 *e_com++ = '*';
373 *e_com++ = '/';
374 *e_com = '\0';
375 ps.just_saw_decl = l_just_saw_decl;
376 return;
377 } else
378 { /* handle isolated '*' */
379 *e_com++ = '*';
380 ++now_col;
382 break;
383 default: /* we have a random char */
384 if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
385 unix_comment = 1; /* we are not in unix-style
386 comment */
388 *e_com = *buf_ptr++;
389 if (buf_ptr >= buf_end)
390 fill_buffer();
392 if (*e_com == '\t') /* keep track of column */
393 now_col = ((now_col - 1) & tabmask) + tabsize + 1;
394 else if (*e_com == '\b') /* this is a backspace */
395 --now_col;
396 else
397 ++now_col;
399 if (*e_com == ' ' || *e_com == '\t')
400 last_bl = e_com;
401 /* remember we saw a blank */
403 ++e_com;
404 if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ')
406 /* the comment is too long, it must be broken up */
407 if (break_delim == 1 && s_com[0] == '/'
408 && s_com[1] == '*' && s_com[2] == ' ')
410 char *t = e_com;
411 break_delim = 2;
412 e_com = s_com + 2;
413 *e_com = 0;
414 if (bl_bef_bk)
415 prefix_blankline_requested = 1;
416 dump_line();
417 e_com = t;
418 s_com[0] = s_com[1] = s_com[2] = ' ';
420 if (last_bl == 0)
421 { /* we have seen no blanks */
422 last_bl = e_com; /* fake it */
423 *e_com++ = ' ';
425 *e_com = '\0'; /* print what we have */
426 *last_bl = '\0';
427 while (last_bl > s_com && last_bl[-1] < 040)
428 *--last_bl = 0;
429 e_com = last_bl;
430 dump_line();
432 *e_com++ = ' '; /* add blanks for continuation */
433 *e_com++ = ' ';
434 *e_com++ = ' ';
436 t_ptr = last_bl + 1;
437 last_bl = 0;
438 if (t_ptr >= e_com)
440 while (*t_ptr == ' ' || *t_ptr == '\t')
441 t_ptr++;
442 while (*t_ptr != '\0')
443 { /* move unprinted part of
444 comment down in buffer */
445 if (*t_ptr == ' ' || *t_ptr == '\t')
446 last_bl = e_com;
447 *e_com++ = *t_ptr++;
450 *e_com = '\0';
451 now_col = count_spaces(ps.com_col, s_com); /* recompute current
452 position */
454 break;