Fix numerous spelling mistakes.
[dragonfly/vkernel-mp.git] / usr.bin / indent / indent.c
blob861f69b5652222fe6b12a478f927d98f42f2f84c
1 /*
2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 * @(#) Copyright (c) 1985 Sun Microsystems, Inc. @(#) Copyright (c) 1976 Board of Trustees of the University of Illinois. @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
36 * @(#)indent.c 5.17 (Berkeley) 6/7/93
37 * $FreeBSD: src/usr.bin/indent/indent.c,v 1.5.2.6 2001/12/06 19:28:47 schweikh Exp $
38 * $DragonFly: src/usr.bin/indent/indent.c,v 1.4 2007/05/13 18:33:58 swildner Exp $
41 #include <sys/param.h>
42 #include <err.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <ctype.h>
49 #include "indent_globs.h"
50 #include "indent_codes.h"
51 #include "indent.h"
53 static void bakcopy(void);
55 char *in_name = "Standard Input"; /* will always point to name of input
56 * file */
57 char *out_name = "Standard Output"; /* will always point to name
58 * of output file */
59 char bakfile[MAXPATHLEN] = "";
61 int
62 main(int argc, char **argv)
65 extern int found_err; /* flag set in diagN() on error */
66 int dec_ind; /* current indentation for declarations */
67 int di_stack[20]; /* a stack of structure indentation levels */
68 int flushed_nl; /* used when buffering up comments to remember
69 * that a newline was passed over */
70 int force_nl; /* when true, code must be broken */
71 int hd_type = 0; /* used to store type of stmt for if (...),
72 * for (...), etc */
73 int i; /* local loop counter */
74 int scase; /* set to true when we see a case, so we will
75 * know what to do with the following colon */
76 int sp_sw; /* when true, we are in the expressin of
77 * if(...), while(...), etc. */
78 int squest; /* when this is positive, we have seen a ?
79 * without the matching : in a <c>?<s>:<s>
80 * construct */
81 char *t_ptr; /* used for copying tokens */
82 int type_code; /* the type of token, returned by lexi */
84 int last_else = 0; /* true iff last keyword was an else */
87 /*-----------------------------------------------*\
88 | INITIALIZATION |
89 \*-----------------------------------------------*/
92 ps.p_stack[0] = stmt; /* this is the parser's stack */
93 ps.last_nl = true; /* this is true if the last thing scanned was
94 * a newline */
95 ps.last_token = semicolon;
96 combuf = (char *) malloc(bufsize);
97 labbuf = (char *) malloc(bufsize);
98 codebuf = (char *) malloc(bufsize);
99 tokenbuf = (char *) malloc(bufsize);
100 l_com = combuf + bufsize - 5;
101 l_lab = labbuf + bufsize - 5;
102 l_code = codebuf + bufsize - 5;
103 l_token = tokenbuf + bufsize - 5;
104 combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and
105 * comment buffers */
106 combuf[1] = codebuf[1] = labbuf[1] = '\0';
107 ps.else_if = 1; /* Default else-if special processing to on */
108 s_lab = e_lab = labbuf + 1;
109 s_code = e_code = codebuf + 1;
110 s_com = e_com = combuf + 1;
111 s_token = e_token = tokenbuf + 1;
113 in_buffer = (char *) malloc(10);
114 in_buffer_limit = in_buffer + 8;
115 buf_ptr = buf_end = in_buffer;
116 line_no = 1;
117 had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
118 sp_sw = force_nl = false;
119 ps.in_or_st = false;
120 ps.bl_line = true;
121 dec_ind = 0;
122 di_stack[ps.dec_nest = 0] = 0;
123 ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
126 scase = ps.pcase = false;
127 squest = 0;
128 sc_end = 0;
129 bp_save = 0;
130 be_save = 0;
132 output = 0;
136 /*--------------------------------------------------*\
137 | COMMAND LINE SCAN |
138 \*--------------------------------------------------*/
140 #ifdef undef
141 max_col = 78; /* -l78 */
142 lineup_to_parens = 1; /* -lp */
143 ps.ljust_decl = 0; /* -ndj */
144 ps.com_ind = 33; /* -c33 */
145 star_comment_cont = 1; /* -sc */
146 ps.ind_size = 8; /* -i8 */
147 verbose = 0;
148 ps.decl_indent = 16; /* -di16 */
149 ps.indent_parameters = 1; /* -ip */
150 ps.decl_com_ind = 0; /* if this is not set to some positive value
151 * by an arg, we will set this equal to
152 * ps.com_ind */
153 btype_2 = 1; /* -br */
154 cuddle_else = 1; /* -ce */
155 ps.unindent_displace = 0; /* -d0 */
156 ps.case_indent = 0; /* -cli0 */
157 format_block_comments = 1; /* -fcb */
158 format_col1_comments = 1; /* -fc1 */
159 procnames_start_line = 1; /* -psl */
160 proc_calls_space = 0; /* -npcs */
161 comment_delimiter_on_blankline = 1; /* -cdb */
162 ps.leave_comma = 1; /* -nbc */
163 #endif
165 for (i = 1; i < argc; ++i)
166 if (strcmp(argv[i], "-npro") == 0)
167 break;
168 set_defaults();
169 if (i >= argc)
170 set_profile();
172 for (i = 1; i < argc; ++i) {
175 * look thru args (if any) for changes to defaults
177 if (argv[i][0] != '-') {/* no flag on parameter */
178 if (input == 0) { /* we must have the input file */
179 in_name = argv[i]; /* remember name of input file */
180 input = fopen(in_name, "r");
181 if (input == 0) /* check for open error */
182 err(1, "%s", in_name);
183 continue;
185 else if (output == 0) { /* we have the output file */
186 out_name = argv[i]; /* remember name of output file */
187 if (strcmp(in_name, out_name) == 0) { /* attempt to overwrite
188 * the file */
189 errx(1, "input and output files must be different");
191 output = fopen(out_name, "w");
192 if (output == 0) /* check for create error */
193 err(1, "%s", out_name);
194 continue;
196 errx(1, "unknown parameter: %s", argv[i]);
198 else
199 set_option(argv[i]);
200 } /* end of for */
201 if (input == 0)
202 input = stdin;
203 if (output == 0) {
204 if (troff || input == stdin)
205 output = stdout;
206 else {
207 out_name = in_name;
208 bakcopy();
211 if (ps.com_ind <= 1)
212 ps.com_ind = 2; /* dont put normal comments before column 2 */
213 if (troff) {
214 if (bodyf.font[0] == 0)
215 parsefont(&bodyf, "R");
216 if (scomf.font[0] == 0)
217 parsefont(&scomf, "I");
218 if (blkcomf.font[0] == 0)
219 blkcomf = scomf, blkcomf.size += 2;
220 if (boxcomf.font[0] == 0)
221 boxcomf = blkcomf;
222 if (stringf.font[0] == 0)
223 parsefont(&stringf, "L");
224 if (keywordf.font[0] == 0)
225 parsefont(&keywordf, "B");
226 writefdef(&bodyf, 'B');
227 writefdef(&scomf, 'C');
228 writefdef(&blkcomf, 'L');
229 writefdef(&boxcomf, 'X');
230 writefdef(&stringf, 'S');
231 writefdef(&keywordf, 'K');
233 if (block_comment_max_col <= 0)
234 block_comment_max_col = max_col;
235 if (ps.decl_com_ind <= 0) /* if not specified by user, set this */
236 ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
237 if (continuation_indent == 0)
238 continuation_indent = ps.ind_size;
239 fill_buffer(); /* get first batch of stuff into input buffer */
241 parse(semicolon);
243 char *p = buf_ptr;
244 int col = 1;
246 while (1) {
247 if (*p == ' ')
248 col++;
249 else if (*p == '\t')
250 col = ((col - 1) & ~7) + 9;
251 else
252 break;
253 p++;
255 if (col > ps.ind_size)
256 ps.ind_level = ps.i_l_follow = col / ps.ind_size;
258 if (troff) {
259 char *p = in_name,
260 *beg = in_name;
262 while (*p)
263 if (*p++ == '/')
264 beg = p;
265 fprintf(output, ".Fn \"%s\"\n", beg);
268 * START OF MAIN LOOP
271 while (1) { /* this is the main loop. it will go until we
272 * reach eof */
273 int is_procname;
275 type_code = lexi(); /* lexi reads one token. The actual
276 * characters read are stored in "token". lexi
277 * returns a code indicating the type of token */
278 is_procname = ps.procname[0];
281 * The following code moves everything following an if (), while (),
282 * else, etc. up to the start of the following stmt to a buffer. This
283 * allows proper handling of both kinds of brace placement.
286 flushed_nl = false;
287 while (ps.search_brace) { /* if we scanned an if(), while(),
288 * etc., we might need to copy stuff
289 * into a buffer we must loop, copying
290 * stuff into save_com, until we find
291 * the start of the stmt which follows
292 * the if, or whatever */
293 switch (type_code) {
294 case newline:
295 ++line_no;
296 flushed_nl = true;
297 case form_feed:
298 break; /* form feeds and newlines found here will be
299 * ignored */
301 case lbrace: /* this is a brace that starts the compound
302 * stmt */
303 if (sc_end == 0) { /* ignore buffering if a comment wasnt
304 * stored up */
305 ps.search_brace = false;
306 goto check_type;
308 if (btype_2) {
309 save_com[0] = '{'; /* we either want to put the brace
310 * right after the if */
311 goto sw_buffer; /* go to common code to get out of
312 * this loop */
314 case comment: /* we have a comment, so we must copy it into
315 * the buffer */
316 if (!flushed_nl || sc_end != 0) {
317 if (sc_end == 0) { /* if this is the first comment, we
318 * must set up the buffer */
319 save_com[0] = save_com[1] = ' ';
320 sc_end = &(save_com[2]);
322 else {
323 *sc_end++ = '\n'; /* add newline between
324 * comments */
325 *sc_end++ = ' ';
326 --line_no;
328 *sc_end++ = '/'; /* copy in start of comment */
329 *sc_end++ = '*';
331 for (;;) { /* loop until we get to the end of the comment */
332 *sc_end = *buf_ptr++;
333 if (buf_ptr >= buf_end)
334 fill_buffer();
336 if (*sc_end++ == '*' && *buf_ptr == '/')
337 break; /* we are at end of comment */
339 if (sc_end >= &(save_com[sc_size])) { /* check for temp buffer
340 * overflow */
341 diag2(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever.");
342 fflush(output);
343 exit(1);
346 *sc_end++ = '/'; /* add ending slash */
347 if (++buf_ptr >= buf_end) /* get past / in buffer */
348 fill_buffer();
349 break;
351 default: /* it is the start of a normal statment */
352 if (flushed_nl) /* if we flushed a newline, make sure it is
353 * put back */
354 force_nl = true;
355 if ((type_code == sp_paren && *token == 'i'
356 && last_else && ps.else_if)
357 || (type_code == sp_nparen && *token == 'e'
358 && e_code != s_code && e_code[-1] == '}'))
359 force_nl = false;
361 if (sc_end == 0) { /* ignore buffering if comment wasnt
362 * saved up */
363 ps.search_brace = false;
364 goto check_type;
366 if (force_nl) { /* if we should insert a nl here, put it into
367 * the buffer */
368 force_nl = false;
369 --line_no; /* this will be re-increased when the nl is
370 * read from the buffer */
371 *sc_end++ = '\n';
372 *sc_end++ = ' ';
373 if (verbose && !flushed_nl) /* print error msg if the line
374 * was not already broken */
375 diag2(0, "Line broken");
376 flushed_nl = false;
378 for (t_ptr = token; *t_ptr; ++t_ptr)
379 *sc_end++ = *t_ptr; /* copy token into temp buffer */
380 ps.procname[0] = 0;
382 sw_buffer:
383 ps.search_brace = false; /* stop looking for start of
384 * stmt */
385 bp_save = buf_ptr; /* save current input buffer */
386 be_save = buf_end;
387 buf_ptr = save_com; /* fix so that subsequent calls to
388 * lexi will take tokens out of
389 * save_com */
390 *sc_end++ = ' ';/* add trailing blank, just in case */
391 buf_end = sc_end;
392 sc_end = 0;
393 break;
394 } /* end of switch */
395 if (type_code != 0) /* we must make this check, just in case there
396 * was an unexpected EOF */
397 type_code = lexi(); /* read another token */
398 /* if (ps.search_brace) ps.procname[0] = 0; */
399 if ((is_procname = ps.procname[0]) && flushed_nl
400 && !procnames_start_line && ps.in_decl
401 && type_code == ident)
402 flushed_nl = 0;
403 } /* end of while (search_brace) */
404 last_else = 0;
405 check_type:
406 if (type_code == 0) { /* we got eof */
407 if (s_lab != e_lab || s_code != e_code
408 || s_com != e_com) /* must dump end of line */
409 dump_line();
410 if (ps.tos > 1) /* check for balanced braces */
411 diag2(1, "Stuff missing from end of file.");
413 if (verbose) {
414 printf("There were %d output lines and %d comments\n",
415 ps.out_lines, ps.out_coms);
416 printf("(Lines with comments)/(Lines with code): %6.3f\n",
417 (1.0 * ps.com_lines) / code_lines);
419 fflush(output);
420 exit(found_err);
422 if (
423 (type_code != comment) &&
424 (type_code != newline) &&
425 (type_code != preesc) &&
426 (type_code != form_feed)) {
427 if (force_nl &&
428 (type_code != semicolon) &&
429 (type_code != lbrace || !btype_2)) {
430 /* we should force a broken line here */
431 if (verbose && !flushed_nl)
432 diag2(0, "Line broken");
433 flushed_nl = false;
434 dump_line();
435 ps.want_blank = false; /* dont insert blank at line start */
436 force_nl = false;
438 ps.in_stmt = true; /* turn on flag which causes an extra level of
439 * indentation. this is turned off by a ; or
440 * '}' */
441 if (s_com != e_com) { /* the turkey has embedded a comment
442 * in a line. fix it */
443 *e_code++ = ' ';
444 for (t_ptr = s_com; *t_ptr; ++t_ptr) {
445 CHECK_SIZE_CODE;
446 *e_code++ = *t_ptr;
448 *e_code++ = ' ';
449 *e_code = '\0'; /* null terminate code sect */
450 ps.want_blank = false;
451 e_com = s_com;
454 else if (type_code != comment) /* preserve force_nl thru a comment */
455 force_nl = false; /* cancel forced newline after newline, form
456 * feed, etc */
460 /*-----------------------------------------------------*\
461 | do switch on type of token scanned |
462 \*-----------------------------------------------------*/
463 CHECK_SIZE_CODE;
464 switch (type_code) { /* now, decide what to do with the token */
466 case form_feed: /* found a form feed in line */
467 ps.use_ff = true; /* a form feed is treated much like a newline */
468 dump_line();
469 ps.want_blank = false;
470 break;
472 case newline:
473 if (ps.last_token != comma || ps.p_l_follow > 0
474 || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
475 dump_line();
476 ps.want_blank = false;
478 ++line_no; /* keep track of input line number */
479 break;
481 case lparen: /* got a '(' or '[' */
482 ++ps.p_l_follow; /* count parens to make Healy happy */
483 if (ps.want_blank && *token != '[' &&
484 (ps.last_token != ident || proc_calls_space
485 || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon))))
486 *e_code++ = ' ';
487 if (ps.in_decl && !ps.block_init)
488 if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) {
489 ps.dumped_decl_indent = 1;
490 sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
491 e_code += strlen(e_code);
493 else {
494 while ((e_code - s_code) < dec_ind) {
495 CHECK_SIZE_CODE;
496 *e_code++ = ' ';
498 *e_code++ = token[0];
500 else
501 *e_code++ = token[0];
502 ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;
503 if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
504 && ps.paren_indents[0] < 2 * ps.ind_size)
505 ps.paren_indents[0] = 2 * ps.ind_size;
506 ps.want_blank = false;
507 if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
509 * this is a kluge to make sure that declarations will be
510 * aligned right if proc decl has an explicit type on it, i.e.
511 * "int a(x) {..."
513 parse(semicolon); /* I said this was a kluge... */
514 ps.in_or_st = false; /* turn off flag for structure decl or
515 * initialization */
517 if (ps.sizeof_keyword)
518 ps.sizeof_mask |= 1 << ps.p_l_follow;
519 break;
521 case rparen: /* got a ')' or ']' */
522 rparen_count--;
523 if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
524 ps.last_u_d = true;
525 ps.cast_mask &= (1 << ps.p_l_follow) - 1;
526 ps.want_blank = false;
527 } else
528 ps.want_blank = true;
529 ps.sizeof_mask &= (1 << ps.p_l_follow) - 1;
530 if (--ps.p_l_follow < 0) {
531 ps.p_l_follow = 0;
532 diag3(0, "Extra %c", *token);
534 if (e_code == s_code) /* if the paren starts the line */
535 ps.paren_level = ps.p_l_follow; /* then indent it */
537 *e_code++ = token[0];
539 if (sp_sw && (ps.p_l_follow == 0)) { /* check for end of if
540 * (...), or some such */
541 sp_sw = false;
542 force_nl = true;/* must force newline after if */
543 ps.last_u_d = true; /* inform lexi that a following
544 * operator is unary */
545 ps.in_stmt = false; /* dont use stmt continuation
546 * indentation */
548 parse(hd_type); /* let parser worry about if, or whatever */
550 ps.search_brace = btype_2; /* this should insure that constructs
551 * such as main(){...} and int[]{...}
552 * have their braces put in the right
553 * place */
554 break;
556 case unary_op: /* this could be any unary operation */
557 if (ps.want_blank)
558 *e_code++ = ' ';
560 if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) {
561 sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
562 ps.dumped_decl_indent = 1;
563 e_code += strlen(e_code);
565 else {
566 char *res = token;
568 if (ps.in_decl && !ps.block_init) { /* if this is a unary op
569 * in a declaration, we
570 * should indent this
571 * token */
572 for (i = 0; token[i]; ++i); /* find length of token */
573 while ((e_code - s_code) < (dec_ind - i)) {
574 CHECK_SIZE_CODE;
575 *e_code++ = ' '; /* pad it */
578 if (troff && token[0] == '-' && token[1] == '>')
579 res = "\\(->";
580 for (t_ptr = res; *t_ptr; ++t_ptr) {
581 CHECK_SIZE_CODE;
582 *e_code++ = *t_ptr;
585 ps.want_blank = false;
586 break;
588 case binary_op: /* any binary operation */
589 if (ps.want_blank)
590 *e_code++ = ' ';
592 char *res = token;
594 if (troff)
595 switch (token[0]) {
596 case '<':
597 if (token[1] == '=')
598 res = "\\(<=";
599 break;
600 case '>':
601 if (token[1] == '=')
602 res = "\\(>=";
603 break;
604 case '!':
605 if (token[1] == '=')
606 res = "\\(!=";
607 break;
608 case '|':
609 if (token[1] == '|')
610 res = "\\(br\\(br";
611 else if (token[1] == 0)
612 res = "\\(br";
613 break;
615 for (t_ptr = res; *t_ptr; ++t_ptr) {
616 CHECK_SIZE_CODE;
617 *e_code++ = *t_ptr; /* move the operator */
620 ps.want_blank = true;
621 break;
623 case postop: /* got a trailing ++ or -- */
624 *e_code++ = token[0];
625 *e_code++ = token[1];
626 ps.want_blank = true;
627 break;
629 case question: /* got a ? */
630 squest++; /* this will be used when a later colon
631 * appears so we can distinguish the
632 * <c>?<n>:<n> construct */
633 if (ps.want_blank)
634 *e_code++ = ' ';
635 *e_code++ = '?';
636 ps.want_blank = true;
637 break;
639 case casestmt: /* got word 'case' or 'default' */
640 scase = true; /* so we can process the later colon properly */
641 goto copy_id;
643 case colon: /* got a ':' */
644 if (squest > 0) { /* it is part of the <c>?<n>: <n> construct */
645 --squest;
646 if (ps.want_blank)
647 *e_code++ = ' ';
648 *e_code++ = ':';
649 ps.want_blank = true;
650 break;
652 if (ps.in_decl) {
653 *e_code++ = ':';
654 ps.want_blank = false;
655 break;
657 ps.in_stmt = false; /* seeing a label does not imply we are in a
658 * stmt */
659 for (t_ptr = s_code; *t_ptr; ++t_ptr)
660 *e_lab++ = *t_ptr; /* turn everything so far into a label */
661 e_code = s_code;
662 *e_lab++ = ':';
663 *e_lab++ = ' ';
664 *e_lab = '\0';
666 force_nl = ps.pcase = scase; /* ps.pcase will be used by
667 * dump_line to decide how to
668 * indent the label. force_nl
669 * will force a case n: to be
670 * on a line by itself */
671 scase = false;
672 ps.want_blank = false;
673 break;
675 case semicolon: /* got a ';' */
676 ps.in_or_st = false;/* we are not in an initialization or
677 * structure declaration */
678 scase = false; /* these will only need resetting in a error */
679 squest = 0;
680 if (ps.last_token == rparen && rparen_count == 0)
681 ps.in_parameter_declaration = 0;
682 ps.cast_mask = 0;
683 ps.sizeof_mask = 0;
684 ps.block_init = 0;
685 ps.block_init_level = 0;
686 ps.just_saw_decl--;
688 if (ps.in_decl && s_code == e_code && !ps.block_init)
689 while ((e_code - s_code) < (dec_ind - 1)) {
690 CHECK_SIZE_CODE;
691 *e_code++ = ' ';
694 ps.in_decl = (ps.dec_nest > 0); /* if we were in a first level
695 * structure declaration, we
696 * arent any more */
698 if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
701 * This should be true iff there were unbalanced parens in the
702 * stmt. It is a bit complicated, because the semicolon might
703 * be in a for stmt
705 diag2(1, "Unbalanced parens");
706 ps.p_l_follow = 0;
707 if (sp_sw) { /* this is a check for a if, while, etc. with
708 * unbalanced parens */
709 sp_sw = false;
710 parse(hd_type); /* dont lose the if, or whatever */
713 *e_code++ = ';';
714 ps.want_blank = true;
715 ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in the
716 * middle of a stmt */
718 if (!sp_sw) { /* if not if for (;;) */
719 parse(semicolon); /* let parser know about end of stmt */
720 force_nl = true;/* force newline after a end of stmt */
722 break;
724 case lbrace: /* got a '{' */
725 ps.in_stmt = false; /* dont indent the {} */
726 if (!ps.block_init)
727 force_nl = true;/* force other stuff on same line as '{' onto
728 * new line */
729 else if (ps.block_init_level <= 0)
730 ps.block_init_level = 1;
731 else
732 ps.block_init_level++;
734 if (s_code != e_code && !ps.block_init) {
735 if (!btype_2) {
736 dump_line();
737 ps.want_blank = false;
739 else if (ps.in_parameter_declaration && !ps.in_or_st) {
740 ps.i_l_follow = 0;
741 dump_line();
742 ps.want_blank = false;
745 if (ps.in_parameter_declaration)
746 prefix_blankline_requested = 0;
748 if (ps.p_l_follow > 0) { /* check for preceding unbalanced
749 * parens */
750 diag2(1, "Unbalanced parens");
751 ps.p_l_follow = 0;
752 if (sp_sw) { /* check for unclosed if, for, etc. */
753 sp_sw = false;
754 parse(hd_type);
755 ps.ind_level = ps.i_l_follow;
758 if (s_code == e_code)
759 ps.ind_stmt = false; /* dont put extra indentation on line
760 * with '{' */
761 if (ps.in_decl && ps.in_or_st) { /* this is either a structure
762 * declaration or an init */
763 di_stack[ps.dec_nest++] = dec_ind;
764 /* ? dec_ind = 0; */
766 else {
767 ps.decl_on_line = false; /* we cant be in the middle of
768 * a declaration, so dont do
769 * special indentation of
770 * comments */
771 if (blanklines_after_declarations_at_proctop
772 && ps.in_parameter_declaration)
773 postfix_blankline_requested = 1;
774 ps.in_parameter_declaration = 0;
776 dec_ind = 0;
777 parse(lbrace); /* let parser know about this */
778 if (ps.want_blank) /* put a blank before '{' if '{' is not at
779 * start of line */
780 *e_code++ = ' ';
781 ps.want_blank = false;
782 *e_code++ = '{';
783 ps.just_saw_decl = 0;
784 break;
786 case rbrace: /* got a '}' */
787 if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be
788 * omitted in
789 * declarations */
790 parse(semicolon);
791 if (ps.p_l_follow) {/* check for unclosed if, for, else. */
792 diag2(1, "Unbalanced parens");
793 ps.p_l_follow = 0;
794 sp_sw = false;
796 ps.just_saw_decl = 0;
797 ps.block_init_level--;
798 if (s_code != e_code && !ps.block_init) { /* '}' must be first on
799 * line */
800 if (verbose)
801 diag2(0, "Line broken");
802 dump_line();
804 *e_code++ = '}';
805 ps.want_blank = true;
806 ps.in_stmt = ps.ind_stmt = false;
807 if (ps.dec_nest > 0) { /* we are in multi-level structure
808 * declaration */
809 dec_ind = di_stack[--ps.dec_nest];
810 if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
811 ps.just_saw_decl = 2;
812 ps.in_decl = true;
814 prefix_blankline_requested = 0;
815 parse(rbrace); /* let parser know about this */
816 ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
817 && ps.il[ps.tos] >= ps.ind_level;
818 if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
819 postfix_blankline_requested = 1;
820 break;
822 case swstmt: /* got keyword "switch" */
823 sp_sw = true;
824 hd_type = swstmt; /* keep this for when we have seen the
825 * expression */
826 goto copy_id; /* go move the token into buffer */
828 case sp_paren: /* token is if, while, for */
829 sp_sw = true; /* the interesting stuff is done after the
830 * expression is scanned */
831 hd_type = (*token == 'i' ? ifstmt :
832 (*token == 'w' ? whilestmt : forstmt));
835 * remember the type of header for later use by parser
837 goto copy_id; /* copy the token into line */
839 case sp_nparen: /* got else, do */
840 ps.in_stmt = false;
841 if (*token == 'e') {
842 if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
843 if (verbose)
844 diag2(0, "Line broken");
845 dump_line();/* make sure this starts a line */
846 ps.want_blank = false;
848 force_nl = true;/* also, following stuff must go onto new line */
849 last_else = 1;
850 parse(elselit);
852 else {
853 if (e_code != s_code) { /* make sure this starts a line */
854 if (verbose)
855 diag2(0, "Line broken");
856 dump_line();
857 ps.want_blank = false;
859 force_nl = true;/* also, following stuff must go onto new line */
860 last_else = 0;
861 parse(dolit);
863 goto copy_id; /* move the token into line */
865 case decl: /* we have a declaration type (int, register,
866 * etc.) */
867 parse(decl); /* let parser worry about indentation */
868 if (ps.last_token == rparen && ps.tos <= 1) {
869 ps.in_parameter_declaration = 1;
870 if (s_code != e_code) {
871 dump_line();
872 ps.want_blank = 0;
875 if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
876 ps.ind_level = ps.i_l_follow = 1;
877 ps.ind_stmt = 0;
879 ps.in_or_st = true; /* this might be a structure or initialization
880 * declaration */
881 ps.in_decl = ps.decl_on_line = true;
882 if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
883 ps.just_saw_decl = 2;
884 prefix_blankline_requested = 0;
885 for (i = 0; token[i++];); /* get length of token */
888 * dec_ind = e_code - s_code + (ps.decl_indent>i ? ps.decl_indent
889 * : i);
891 dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
892 goto copy_id;
894 case ident: /* got an identifier or constant */
895 if (ps.in_decl) { /* if we are in a declaration, we must indent
896 * identifier */
897 if (ps.want_blank)
898 *e_code++ = ' ';
899 ps.want_blank = false;
900 if (is_procname == 0 || !procnames_start_line) {
901 if (!ps.block_init) {
902 if (troff && !ps.dumped_decl_indent) {
903 sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7);
904 ps.dumped_decl_indent = 1;
905 e_code += strlen(e_code);
906 } else {
907 while ((e_code - s_code) < dec_ind) {
908 CHECK_SIZE_CODE;
909 *e_code++ = ' ';
913 } else {
914 if (dec_ind && s_code != e_code)
915 dump_line();
916 dec_ind = 0;
917 ps.want_blank = false;
920 else if (sp_sw && ps.p_l_follow == 0) {
921 sp_sw = false;
922 force_nl = true;
923 ps.last_u_d = true;
924 ps.in_stmt = false;
925 parse(hd_type);
927 copy_id:
928 if (ps.want_blank)
929 *e_code++ = ' ';
930 if (troff && ps.its_a_keyword) {
931 e_code = chfont(&bodyf, &keywordf, e_code);
932 for (t_ptr = token; *t_ptr; ++t_ptr) {
933 CHECK_SIZE_CODE;
934 *e_code++ = keywordf.allcaps && islower(*t_ptr)
935 ? toupper(*t_ptr) : *t_ptr;
937 e_code = chfont(&keywordf, &bodyf, e_code);
939 else
940 for (t_ptr = token; *t_ptr; ++t_ptr) {
941 CHECK_SIZE_CODE;
942 *e_code++ = *t_ptr;
944 ps.want_blank = true;
945 break;
947 case period: /* treat a period kind of like a binary
948 * operation */
949 *e_code++ = '.'; /* move the period into line */
950 ps.want_blank = false; /* dont put a blank after a period */
951 break;
953 case comma:
954 ps.want_blank = (s_code != e_code); /* only put blank after comma
955 * if comma does not start the
956 * line */
957 if (ps.in_decl && is_procname == 0 && !ps.block_init)
958 while ((e_code - s_code) < (dec_ind - 1)) {
959 CHECK_SIZE_CODE;
960 *e_code++ = ' ';
963 *e_code++ = ',';
964 if (ps.p_l_follow == 0) {
965 if (ps.block_init_level <= 0)
966 ps.block_init = 0;
967 if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8))
968 force_nl = true;
970 break;
972 case preesc: /* got the character '#' */
973 if ((s_com != e_com) ||
974 (s_lab != e_lab) ||
975 (s_code != e_code))
976 dump_line();
977 *e_lab++ = '#'; /* move whole line to 'label' buffer */
979 int in_comment = 0;
980 int com_start = 0;
981 char quote = 0;
982 int com_end = 0;
984 while (*buf_ptr == ' ' || *buf_ptr == '\t') {
985 buf_ptr++;
986 if (buf_ptr >= buf_end)
987 fill_buffer();
989 while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
990 CHECK_SIZE_LAB;
991 *e_lab = *buf_ptr++;
992 if (buf_ptr >= buf_end)
993 fill_buffer();
994 switch (*e_lab++) {
995 case BACKSLASH:
996 if (troff)
997 *e_lab++ = BACKSLASH;
998 if (!in_comment) {
999 *e_lab++ = *buf_ptr++;
1000 if (buf_ptr >= buf_end)
1001 fill_buffer();
1003 break;
1004 case '/':
1005 if (*buf_ptr == '*' && !in_comment && !quote) {
1006 in_comment = 1;
1007 *e_lab++ = *buf_ptr++;
1008 com_start = e_lab - s_lab - 2;
1010 break;
1011 case '"':
1012 if (quote == '"')
1013 quote = 0;
1014 break;
1015 case '\'':
1016 if (quote == '\'')
1017 quote = 0;
1018 break;
1019 case '*':
1020 if (*buf_ptr == '/' && in_comment) {
1021 in_comment = 0;
1022 *e_lab++ = *buf_ptr++;
1023 com_end = e_lab - s_lab;
1025 break;
1029 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1030 e_lab--;
1031 if (e_lab - s_lab == com_end && bp_save == 0) { /* comment on
1032 * preprocessor line */
1033 if (sc_end == 0) /* if this is the first comment, we
1034 * must set up the buffer */
1035 sc_end = &(save_com[0]);
1036 else {
1037 *sc_end++ = '\n'; /* add newline between
1038 * comments */
1039 *sc_end++ = ' ';
1040 --line_no;
1042 bcopy(s_lab + com_start, sc_end, com_end - com_start);
1043 sc_end += com_end - com_start;
1044 if (sc_end >= &save_com[sc_size])
1045 abort();
1046 e_lab = s_lab + com_start;
1047 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1048 e_lab--;
1049 bp_save = buf_ptr; /* save current input buffer */
1050 be_save = buf_end;
1051 buf_ptr = save_com; /* fix so that subsequent calls to
1052 * lexi will take tokens out of
1053 * save_com */
1054 *sc_end++ = ' '; /* add trailing blank, just in case */
1055 buf_end = sc_end;
1056 sc_end = 0;
1058 *e_lab = '\0'; /* null terminate line */
1059 ps.pcase = false;
1062 if (strncmp(s_lab, "#if", 3) == 0) {
1063 if (blanklines_around_conditional_compilation) {
1064 int c;
1065 prefix_blankline_requested++;
1066 while ((c = getc(input)) == '\n');
1067 ungetc(c, input);
1069 if (ifdef_level < sizeof state_stack / sizeof state_stack[0]) {
1070 match_state[ifdef_level].tos = -1;
1071 state_stack[ifdef_level++] = ps;
1073 else
1074 diag2(1, "#if stack overflow");
1076 else if (strncmp(s_lab, "#else", 5) == 0)
1077 if (ifdef_level <= 0)
1078 diag2(1, "Unmatched #else");
1079 else {
1080 match_state[ifdef_level - 1] = ps;
1081 ps = state_stack[ifdef_level - 1];
1083 else if (strncmp(s_lab, "#endif", 6) == 0) {
1084 if (ifdef_level <= 0)
1085 diag2(1, "Unmatched #endif");
1086 else {
1087 ifdef_level--;
1089 #ifdef undef
1091 * This match needs to be more intelligent before the
1092 * message is useful
1094 if (match_state[ifdef_level].tos >= 0
1095 && bcmp(&ps, &match_state[ifdef_level], sizeof ps))
1096 diag2(0, "Syntactically inconsistent #ifdef alternatives.");
1097 #endif
1099 if (blanklines_around_conditional_compilation) {
1100 postfix_blankline_requested++;
1101 n_real_blanklines = 0;
1104 break; /* subsequent processing of the newline
1105 * character will cause the line to be printed */
1107 case comment: /* we have gotten a / followed by * this is a biggie */
1108 if (flushed_nl) { /* we should force a broken line here */
1109 flushed_nl = false;
1110 dump_line();
1111 ps.want_blank = false; /* dont insert blank at line start */
1112 force_nl = false;
1114 pr_comment();
1115 break;
1116 } /* end of big switch stmt */
1118 *e_code = '\0'; /* make sure code section is null terminated */
1119 if (type_code != comment && type_code != newline && type_code != preesc)
1120 ps.last_token = type_code;
1121 } /* end of main while (1) loop */
1125 * copy input file to backup file if in_name is /blah/blah/blah/file, then
1126 * backup file will be ".Bfile" then make the backup file the input and
1127 * original input file the output
1129 static void
1130 bakcopy(void)
1132 int n,
1133 bakchn;
1134 char buff[8 * 1024];
1135 char *p;
1137 /* construct file name .Bfile */
1138 for (p = in_name; *p; p++); /* skip to end of string */
1139 while (p > in_name && *p != '/') /* find last '/' */
1140 p--;
1141 if (*p == '/')
1142 p++;
1143 sprintf(bakfile, "%s.BAK", p);
1145 /* copy in_name to backup file */
1146 bakchn = creat(bakfile, 0600);
1147 if (bakchn < 0)
1148 err(1, "%s", bakfile);
1149 while ((n = read(fileno(input), buff, sizeof buff)) != 0)
1150 if (write(bakchn, buff, n) != n)
1151 err(1, "%s", bakfile);
1152 if (n < 0)
1153 err(1, "%s", in_name);
1154 close(bakchn);
1155 fclose(input);
1157 /* re-open backup file as the input file */
1158 input = fopen(bakfile, "r");
1159 if (input == 0)
1160 err(1, "%s", bakfile);
1161 /* now the original input file will be the output */
1162 output = fopen(in_name, "w");
1163 if (output == 0) {
1164 unlink(bakfile);
1165 err(1, "%s", in_name);