indent(1): Use a dash in the license headers.
[freebsd-src.git] / usr.bin / indent / indent.c
blob641d30576b140e3d458703c7f38614045d1a29e5
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.
36 #ifndef lint
37 static const char copyright[] =
38 "@(#) Copyright (c) 1985 Sun Microsystems, Inc.\n\
39 @(#) Copyright (c) 1976 Board of Trustees of the University of Illinois.\n\
40 @(#) Copyright (c) 1980, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
42 #endif /* not lint */
44 #if 0
45 #ifndef lint
46 static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
47 #endif /* not lint */
48 #endif
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <err.h>
55 #include <fcntl.h>
56 #include <unistd.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <ctype.h>
61 #include "indent_globs.h"
62 #include "indent_codes.h"
63 #include "indent.h"
65 static void bakcopy(void);
66 static void indent_declaration(int, int);
68 const char *in_name = "Standard Input"; /* will always point to name of input
69 * file */
70 const char *out_name = "Standard Output"; /* will always point to name
71 * of output file */
72 char bakfile[MAXPATHLEN] = "";
74 int
75 main(int argc, char **argv)
78 int dec_ind; /* current indentation for declarations */
79 int di_stack[20]; /* a stack of structure indentation levels */
80 int flushed_nl; /* used when buffering up comments to remember
81 * that a newline was passed over */
82 int force_nl; /* when true, code must be broken */
83 int hd_type = 0; /* used to store type of stmt for if (...),
84 * for (...), etc */
85 int i; /* local loop counter */
86 int scase; /* set to true when we see a case, so we will
87 * know what to do with the following colon */
88 int sp_sw; /* when true, we are in the expression of
89 * if(...), while(...), etc. */
90 int squest; /* when this is positive, we have seen a ?
91 * without the matching : in a <c>?<s>:<s>
92 * construct */
93 const char *t_ptr; /* used for copying tokens */
94 int tabs_to_var; /* true if using tabs to indent to var name */
95 int type_code; /* the type of token, returned by lexi */
97 int last_else = 0; /* true iff last keyword was an else */
100 /*-----------------------------------------------*\
101 | INITIALIZATION |
102 \*-----------------------------------------------*/
104 found_err = 0;
106 ps.p_stack[0] = stmt; /* this is the parser's stack */
107 ps.last_nl = true; /* this is true if the last thing scanned was
108 * a newline */
109 ps.last_token = semicolon;
110 combuf = (char *) malloc(bufsize);
111 if (combuf == NULL)
112 err(1, NULL);
113 labbuf = (char *) malloc(bufsize);
114 if (labbuf == NULL)
115 err(1, NULL);
116 codebuf = (char *) malloc(bufsize);
117 if (codebuf == NULL)
118 err(1, NULL);
119 tokenbuf = (char *) malloc(bufsize);
120 if (tokenbuf == NULL)
121 err(1, NULL);
122 l_com = combuf + bufsize - 5;
123 l_lab = labbuf + bufsize - 5;
124 l_code = codebuf + bufsize - 5;
125 l_token = tokenbuf + bufsize - 5;
126 combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and
127 * comment buffers */
128 combuf[1] = codebuf[1] = labbuf[1] = '\0';
129 ps.else_if = 1; /* Default else-if special processing to on */
130 s_lab = e_lab = labbuf + 1;
131 s_code = e_code = codebuf + 1;
132 s_com = e_com = combuf + 1;
133 s_token = e_token = tokenbuf + 1;
135 in_buffer = (char *) malloc(10);
136 if (in_buffer == NULL)
137 err(1, NULL);
138 in_buffer_limit = in_buffer + 8;
139 buf_ptr = buf_end = in_buffer;
140 line_no = 1;
141 had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
142 sp_sw = force_nl = false;
143 ps.in_or_st = false;
144 ps.bl_line = true;
145 dec_ind = 0;
146 di_stack[ps.dec_nest = 0] = 0;
147 ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
149 scase = ps.pcase = false;
150 squest = 0;
151 sc_end = NULL;
152 bp_save = NULL;
153 be_save = NULL;
155 output = NULL;
156 tabs_to_var = 0;
158 /*--------------------------------------------------*\
159 | COMMAND LINE SCAN |
160 \*--------------------------------------------------*/
162 #ifdef undef
163 max_col = 78; /* -l78 */
164 lineup_to_parens = 1; /* -lp */
165 ps.ljust_decl = 0; /* -ndj */
166 ps.com_ind = 33; /* -c33 */
167 star_comment_cont = 1; /* -sc */
168 ps.ind_size = 8; /* -i8 */
169 verbose = 0;
170 ps.decl_indent = 16; /* -di16 */
171 ps.local_decl_indent = -1; /* if this is not set to some nonnegative value
172 * by an arg, we will set this equal to
173 * ps.decl_ind */
174 ps.indent_parameters = 1; /* -ip */
175 ps.decl_com_ind = 0; /* if this is not set to some positive value
176 * by an arg, we will set this equal to
177 * ps.com_ind */
178 btype_2 = 1; /* -br */
179 cuddle_else = 1; /* -ce */
180 ps.unindent_displace = 0; /* -d0 */
181 ps.case_indent = 0; /* -cli0 */
182 format_block_comments = 1; /* -fcb */
183 format_col1_comments = 1; /* -fc1 */
184 procnames_start_line = 1; /* -psl */
185 proc_calls_space = 0; /* -npcs */
186 comment_delimiter_on_blankline = 1; /* -cdb */
187 ps.leave_comma = 1; /* -nbc */
188 #endif
190 for (i = 1; i < argc; ++i)
191 if (strcmp(argv[i], "-npro") == 0)
192 break;
193 set_defaults();
194 if (i >= argc)
195 set_profile();
197 for (i = 1; i < argc; ++i) {
200 * look thru args (if any) for changes to defaults
202 if (argv[i][0] != '-') {/* no flag on parameter */
203 if (input == NULL) { /* we must have the input file */
204 in_name = argv[i]; /* remember name of input file */
205 input = fopen(in_name, "r");
206 if (input == NULL) /* check for open error */
207 err(1, "%s", in_name);
208 continue;
210 else if (output == NULL) { /* we have the output file */
211 out_name = argv[i]; /* remember name of output file */
212 if (strcmp(in_name, out_name) == 0) { /* attempt to overwrite
213 * the file */
214 errx(1, "input and output files must be different");
216 output = fopen(out_name, "w");
217 if (output == NULL) /* check for create error */
218 err(1, "%s", out_name);
219 continue;
221 errx(1, "unknown parameter: %s", argv[i]);
223 else
224 set_option(argv[i]);
225 } /* end of for */
226 if (input == NULL)
227 input = stdin;
228 if (output == NULL) {
229 if (troff || input == stdin)
230 output = stdout;
231 else {
232 out_name = in_name;
233 bakcopy();
236 if (ps.com_ind <= 1)
237 ps.com_ind = 2; /* dont put normal comments before column 2 */
238 if (troff) {
239 if (bodyf.font[0] == 0)
240 parsefont(&bodyf, "R");
241 if (scomf.font[0] == 0)
242 parsefont(&scomf, "I");
243 if (blkcomf.font[0] == 0)
244 blkcomf = scomf, blkcomf.size += 2;
245 if (boxcomf.font[0] == 0)
246 boxcomf = blkcomf;
247 if (stringf.font[0] == 0)
248 parsefont(&stringf, "L");
249 if (keywordf.font[0] == 0)
250 parsefont(&keywordf, "B");
251 writefdef(&bodyf, 'B');
252 writefdef(&scomf, 'C');
253 writefdef(&blkcomf, 'L');
254 writefdef(&boxcomf, 'X');
255 writefdef(&stringf, 'S');
256 writefdef(&keywordf, 'K');
258 if (block_comment_max_col <= 0)
259 block_comment_max_col = max_col;
260 if (ps.local_decl_indent < 0) /* if not specified by user, set this */
261 ps.local_decl_indent = ps.decl_indent;
262 if (ps.decl_com_ind <= 0) /* if not specified by user, set this */
263 ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
264 if (continuation_indent == 0)
265 continuation_indent = ps.ind_size;
266 fill_buffer(); /* get first batch of stuff into input buffer */
268 parse(semicolon);
270 char *p = buf_ptr;
271 int col = 1;
273 while (1) {
274 if (*p == ' ')
275 col++;
276 else if (*p == '\t')
277 col = ((col - 1) & ~7) + 9;
278 else
279 break;
280 p++;
282 if (col > ps.ind_size)
283 ps.ind_level = ps.i_l_follow = col / ps.ind_size;
285 if (troff) {
286 const char *p = in_name,
287 *beg = in_name;
289 while (*p)
290 if (*p++ == '/')
291 beg = p;
292 fprintf(output, ".Fn \"%s\"\n", beg);
295 * START OF MAIN LOOP
298 while (1) { /* this is the main loop. it will go until we
299 * reach eof */
300 int is_procname;
302 type_code = lexi(); /* lexi reads one token. The actual
303 * characters read are stored in "token". lexi
304 * returns a code indicating the type of token */
305 is_procname = ps.procname[0];
308 * The following code moves everything following an if (), while (),
309 * else, etc. up to the start of the following stmt to a buffer. This
310 * allows proper handling of both kinds of brace placement.
313 flushed_nl = false;
314 while (ps.search_brace) { /* if we scanned an if(), while(),
315 * etc., we might need to copy stuff
316 * into a buffer we must loop, copying
317 * stuff into save_com, until we find
318 * the start of the stmt which follows
319 * the if, or whatever */
320 switch (type_code) {
321 case newline:
322 ++line_no;
323 if (sc_end != NULL)
324 goto sw_buffer; /* dump comment, if any */
325 flushed_nl = true;
326 case form_feed:
327 break; /* form feeds and newlines found here will be
328 * ignored */
330 case lbrace: /* this is a brace that starts the compound
331 * stmt */
332 if (sc_end == NULL) { /* ignore buffering if a comment wasn't
333 * stored up */
334 ps.search_brace = false;
335 goto check_type;
337 if (btype_2) {
338 save_com[0] = '{'; /* we either want to put the brace
339 * right after the if */
340 goto sw_buffer; /* go to common code to get out of
341 * this loop */
343 case comment: /* we have a comment, so we must copy it into
344 * the buffer */
345 if (!flushed_nl || sc_end != NULL) {
346 if (sc_end == NULL) { /* if this is the first comment, we
347 * must set up the buffer */
348 save_com[0] = save_com[1] = ' ';
349 sc_end = &(save_com[2]);
351 else {
352 *sc_end++ = '\n'; /* add newline between
353 * comments */
354 *sc_end++ = ' ';
355 --line_no;
357 *sc_end++ = '/'; /* copy in start of comment */
358 *sc_end++ = '*';
360 for (;;) { /* loop until we get to the end of the comment */
361 *sc_end = *buf_ptr++;
362 if (buf_ptr >= buf_end)
363 fill_buffer();
365 if (*sc_end++ == '*' && *buf_ptr == '/')
366 break; /* we are at end of comment */
368 if (sc_end >= &(save_com[sc_size])) { /* check for temp buffer
369 * overflow */
370 diag2(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
371 fflush(output);
372 exit(1);
375 *sc_end++ = '/'; /* add ending slash */
376 if (++buf_ptr >= buf_end) /* get past / in buffer */
377 fill_buffer();
378 break;
380 default: /* it is the start of a normal statement */
381 if (flushed_nl) /* if we flushed a newline, make sure it is
382 * put back */
383 force_nl = true;
384 if ((type_code == sp_paren && *token == 'i'
385 && last_else && ps.else_if)
386 || (type_code == sp_nparen && *token == 'e'
387 && e_code != s_code && e_code[-1] == '}'))
388 force_nl = false;
390 if (sc_end == NULL) { /* ignore buffering if comment wasn't
391 * saved up */
392 ps.search_brace = false;
393 goto check_type;
395 if (force_nl) { /* if we should insert a nl here, put it into
396 * the buffer */
397 force_nl = false;
398 --line_no; /* this will be re-increased when the nl is
399 * read from the buffer */
400 *sc_end++ = '\n';
401 *sc_end++ = ' ';
402 if (verbose && !flushed_nl) /* print error msg if the line
403 * was not already broken */
404 diag2(0, "Line broken");
405 flushed_nl = false;
407 for (t_ptr = token; *t_ptr; ++t_ptr)
408 *sc_end++ = *t_ptr; /* copy token into temp buffer */
409 ps.procname[0] = 0;
411 sw_buffer:
412 ps.search_brace = false; /* stop looking for start of
413 * stmt */
414 bp_save = buf_ptr; /* save current input buffer */
415 be_save = buf_end;
416 buf_ptr = save_com; /* fix so that subsequent calls to
417 * lexi will take tokens out of
418 * save_com */
419 *sc_end++ = ' ';/* add trailing blank, just in case */
420 buf_end = sc_end;
421 sc_end = NULL;
422 break;
423 } /* end of switch */
424 if (type_code != 0) /* we must make this check, just in case there
425 * was an unexpected EOF */
426 type_code = lexi(); /* read another token */
427 /* if (ps.search_brace) ps.procname[0] = 0; */
428 if ((is_procname = ps.procname[0]) && flushed_nl
429 && !procnames_start_line && ps.in_decl
430 && type_code == ident)
431 flushed_nl = 0;
432 } /* end of while (search_brace) */
433 last_else = 0;
434 check_type:
435 if (type_code == 0) { /* we got eof */
436 if (s_lab != e_lab || s_code != e_code
437 || s_com != e_com) /* must dump end of line */
438 dump_line();
439 if (ps.tos > 1) /* check for balanced braces */
440 diag2(1, "Stuff missing from end of file");
442 if (verbose) {
443 printf("There were %d output lines and %d comments\n",
444 ps.out_lines, ps.out_coms);
445 printf("(Lines with comments)/(Lines with code): %6.3f\n",
446 (1.0 * ps.com_lines) / code_lines);
448 fflush(output);
449 exit(found_err);
451 if (
452 (type_code != comment) &&
453 (type_code != newline) &&
454 (type_code != preesc) &&
455 (type_code != form_feed)) {
456 if (force_nl &&
457 (type_code != semicolon) &&
458 (type_code != lbrace || !btype_2)) {
459 /* we should force a broken line here */
460 if (verbose && !flushed_nl)
461 diag2(0, "Line broken");
462 flushed_nl = false;
463 dump_line();
464 ps.want_blank = false; /* dont insert blank at line start */
465 force_nl = false;
467 ps.in_stmt = true; /* turn on flag which causes an extra level of
468 * indentation. this is turned off by a ; or
469 * '}' */
470 if (s_com != e_com) { /* the turkey has embedded a comment
471 * in a line. fix it */
472 *e_code++ = ' ';
473 for (t_ptr = s_com; *t_ptr; ++t_ptr) {
474 CHECK_SIZE_CODE;
475 *e_code++ = *t_ptr;
477 *e_code++ = ' ';
478 *e_code = '\0'; /* null terminate code sect */
479 ps.want_blank = false;
480 e_com = s_com;
483 else if (type_code != comment) /* preserve force_nl thru a comment */
484 force_nl = false; /* cancel forced newline after newline, form
485 * feed, etc */
489 /*-----------------------------------------------------*\
490 | do switch on type of token scanned |
491 \*-----------------------------------------------------*/
492 CHECK_SIZE_CODE;
493 switch (type_code) { /* now, decide what to do with the token */
495 case form_feed: /* found a form feed in line */
496 ps.use_ff = true; /* a form feed is treated much like a newline */
497 dump_line();
498 ps.want_blank = false;
499 break;
501 case newline:
502 if (ps.last_token != comma || ps.p_l_follow > 0
503 || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
504 dump_line();
505 ps.want_blank = false;
507 ++line_no; /* keep track of input line number */
508 break;
510 case lparen: /* got a '(' or '[' */
511 ++ps.p_l_follow; /* count parens to make Healy happy */
512 if (ps.want_blank && *token != '[' &&
513 (ps.last_token != ident || proc_calls_space
514 || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon))))
515 *e_code++ = ' ';
516 ps.want_blank = false;
517 if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent &&
518 !is_procname) {
519 /* function pointer declarations */
520 if (troff) {
521 sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
522 e_code += strlen(e_code);
524 else {
525 indent_declaration(dec_ind, tabs_to_var);
527 ps.dumped_decl_indent = true;
529 if (!troff)
530 *e_code++ = token[0];
531 ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;
532 if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
533 && ps.paren_indents[0] < 2 * ps.ind_size)
534 ps.paren_indents[0] = 2 * ps.ind_size;
535 if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
537 * this is a kluge to make sure that declarations will be
538 * aligned right if proc decl has an explicit type on it, i.e.
539 * "int a(x) {..."
541 parse(semicolon); /* I said this was a kluge... */
542 ps.in_or_st = false; /* turn off flag for structure decl or
543 * initialization */
545 if (ps.sizeof_keyword)
546 ps.sizeof_mask |= 1 << ps.p_l_follow;
547 break;
549 case rparen: /* got a ')' or ']' */
550 rparen_count--;
551 if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
552 ps.last_u_d = true;
553 ps.cast_mask &= (1 << ps.p_l_follow) - 1;
554 ps.want_blank = false;
555 } else
556 ps.want_blank = true;
557 ps.sizeof_mask &= (1 << ps.p_l_follow) - 1;
558 if (--ps.p_l_follow < 0) {
559 ps.p_l_follow = 0;
560 diag3(0, "Extra %c", *token);
562 if (e_code == s_code) /* if the paren starts the line */
563 ps.paren_level = ps.p_l_follow; /* then indent it */
565 *e_code++ = token[0];
567 if (sp_sw && (ps.p_l_follow == 0)) { /* check for end of if
568 * (...), or some such */
569 sp_sw = false;
570 force_nl = true;/* must force newline after if */
571 ps.last_u_d = true; /* inform lexi that a following
572 * operator is unary */
573 ps.in_stmt = false; /* dont use stmt continuation
574 * indentation */
576 parse(hd_type); /* let parser worry about if, or whatever */
578 ps.search_brace = btype_2; /* this should insure that constructs
579 * such as main(){...} and int[]{...}
580 * have their braces put in the right
581 * place */
582 break;
584 case unary_op: /* this could be any unary operation */
585 if (!ps.dumped_decl_indent && ps.in_decl && !is_procname &&
586 !ps.block_init) {
587 /* pointer declarations */
588 if (troff) {
589 if (ps.want_blank)
590 *e_code++ = ' ';
591 sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7,
592 token);
593 e_code += strlen(e_code);
595 else {
596 /* if this is a unary op in a declaration, we should
597 * indent this token */
598 for (i = 0; token[i]; ++i)
599 /* find length of token */;
600 indent_declaration(dec_ind - i, tabs_to_var);
602 ps.dumped_decl_indent = true;
604 else if (ps.want_blank)
605 *e_code++ = ' ';
607 const char *res = token;
609 if (troff && token[0] == '-' && token[1] == '>')
610 res = "\\(->";
611 for (t_ptr = res; *t_ptr; ++t_ptr) {
612 CHECK_SIZE_CODE;
613 *e_code++ = *t_ptr;
616 ps.want_blank = false;
617 break;
619 case binary_op: /* any binary operation */
620 if (ps.want_blank)
621 *e_code++ = ' ';
623 const char *res = token;
625 if (troff)
626 switch (token[0]) {
627 case '<':
628 if (token[1] == '=')
629 res = "\\(<=";
630 break;
631 case '>':
632 if (token[1] == '=')
633 res = "\\(>=";
634 break;
635 case '!':
636 if (token[1] == '=')
637 res = "\\(!=";
638 break;
639 case '|':
640 if (token[1] == '|')
641 res = "\\(br\\(br";
642 else if (token[1] == 0)
643 res = "\\(br";
644 break;
646 for (t_ptr = res; *t_ptr; ++t_ptr) {
647 CHECK_SIZE_CODE;
648 *e_code++ = *t_ptr; /* move the operator */
651 ps.want_blank = true;
652 break;
654 case postop: /* got a trailing ++ or -- */
655 *e_code++ = token[0];
656 *e_code++ = token[1];
657 ps.want_blank = true;
658 break;
660 case question: /* got a ? */
661 squest++; /* this will be used when a later colon
662 * appears so we can distinguish the
663 * <c>?<n>:<n> construct */
664 if (ps.want_blank)
665 *e_code++ = ' ';
666 *e_code++ = '?';
667 ps.want_blank = true;
668 break;
670 case casestmt: /* got word 'case' or 'default' */
671 scase = true; /* so we can process the later colon properly */
672 goto copy_id;
674 case colon: /* got a ':' */
675 if (squest > 0) { /* it is part of the <c>?<n>: <n> construct */
676 --squest;
677 if (ps.want_blank)
678 *e_code++ = ' ';
679 *e_code++ = ':';
680 ps.want_blank = true;
681 break;
683 if (ps.in_or_st) {
684 *e_code++ = ':';
685 ps.want_blank = false;
686 break;
688 ps.in_stmt = false; /* seeing a label does not imply we are in a
689 * stmt */
690 for (t_ptr = s_code; *t_ptr; ++t_ptr)
691 *e_lab++ = *t_ptr; /* turn everything so far into a label */
692 e_code = s_code;
693 *e_lab++ = ':';
694 *e_lab++ = ' ';
695 *e_lab = '\0';
697 force_nl = ps.pcase = scase; /* ps.pcase will be used by
698 * dump_line to decide how to
699 * indent the label. force_nl
700 * will force a case n: to be
701 * on a line by itself */
702 scase = false;
703 ps.want_blank = false;
704 break;
706 case semicolon: /* got a ';' */
707 if (ps.dec_nest == 0)
708 ps.in_or_st = false;/* we are not in an initialization or
709 * structure declaration */
710 scase = false; /* these will only need resetting in an error */
711 squest = 0;
712 if (ps.last_token == rparen && rparen_count == 0)
713 ps.in_parameter_declaration = 0;
714 ps.cast_mask = 0;
715 ps.sizeof_mask = 0;
716 ps.block_init = 0;
717 ps.block_init_level = 0;
718 ps.just_saw_decl--;
720 if (ps.in_decl && s_code == e_code && !ps.block_init &&
721 !ps.dumped_decl_indent) {
722 /* indent stray semicolons in declarations */
723 indent_declaration(dec_ind - 1, tabs_to_var);
724 ps.dumped_decl_indent = true;
727 ps.in_decl = (ps.dec_nest > 0); /* if we were in a first level
728 * structure declaration, we
729 * arent any more */
731 if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
734 * This should be true iff there were unbalanced parens in the
735 * stmt. It is a bit complicated, because the semicolon might
736 * be in a for stmt
738 diag2(1, "Unbalanced parens");
739 ps.p_l_follow = 0;
740 if (sp_sw) { /* this is a check for an if, while, etc. with
741 * unbalanced parens */
742 sp_sw = false;
743 parse(hd_type); /* dont lose the if, or whatever */
746 *e_code++ = ';';
747 ps.want_blank = true;
748 ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in the
749 * middle of a stmt */
751 if (!sp_sw) { /* if not if for (;;) */
752 parse(semicolon); /* let parser know about end of stmt */
753 force_nl = true;/* force newline after an end of stmt */
755 break;
757 case lbrace: /* got a '{' */
758 ps.in_stmt = false; /* dont indent the {} */
759 if (!ps.block_init)
760 force_nl = true;/* force other stuff on same line as '{' onto
761 * new line */
762 else if (ps.block_init_level <= 0)
763 ps.block_init_level = 1;
764 else
765 ps.block_init_level++;
767 if (s_code != e_code && !ps.block_init) {
768 if (!btype_2) {
769 dump_line();
770 ps.want_blank = false;
772 else if (ps.in_parameter_declaration && !ps.in_or_st) {
773 ps.i_l_follow = 0;
774 if (function_brace_split) { /* dump the line prior to the
775 * brace ... */
776 dump_line();
777 ps.want_blank = false;
778 } else /* add a space between the decl and brace */
779 ps.want_blank = true;
782 if (ps.in_parameter_declaration)
783 prefix_blankline_requested = 0;
785 if (ps.p_l_follow > 0) { /* check for preceding unbalanced
786 * parens */
787 diag2(1, "Unbalanced parens");
788 ps.p_l_follow = 0;
789 if (sp_sw) { /* check for unclosed if, for, etc. */
790 sp_sw = false;
791 parse(hd_type);
792 ps.ind_level = ps.i_l_follow;
795 if (s_code == e_code)
796 ps.ind_stmt = false; /* dont put extra indentation on line
797 * with '{' */
798 if (ps.in_decl && ps.in_or_st) { /* this is either a structure
799 * declaration or an init */
800 di_stack[ps.dec_nest++] = dec_ind;
801 /* ? dec_ind = 0; */
803 else {
804 ps.decl_on_line = false; /* we can't be in the middle of
805 * a declaration, so don't do
806 * special indentation of
807 * comments */
808 if (blanklines_after_declarations_at_proctop
809 && ps.in_parameter_declaration)
810 postfix_blankline_requested = 1;
811 ps.in_parameter_declaration = 0;
813 dec_ind = 0;
814 parse(lbrace); /* let parser know about this */
815 if (ps.want_blank) /* put a blank before '{' if '{' is not at
816 * start of line */
817 *e_code++ = ' ';
818 ps.want_blank = false;
819 *e_code++ = '{';
820 ps.just_saw_decl = 0;
821 break;
823 case rbrace: /* got a '}' */
824 if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be
825 * omitted in
826 * declarations */
827 parse(semicolon);
828 if (ps.p_l_follow) {/* check for unclosed if, for, else. */
829 diag2(1, "Unbalanced parens");
830 ps.p_l_follow = 0;
831 sp_sw = false;
833 ps.just_saw_decl = 0;
834 ps.block_init_level--;
835 if (s_code != e_code && !ps.block_init) { /* '}' must be first on
836 * line */
837 if (verbose)
838 diag2(0, "Line broken");
839 dump_line();
841 *e_code++ = '}';
842 ps.want_blank = true;
843 ps.in_stmt = ps.ind_stmt = false;
844 if (ps.dec_nest > 0) { /* we are in multi-level structure
845 * declaration */
846 dec_ind = di_stack[--ps.dec_nest];
847 if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
848 ps.just_saw_decl = 2;
849 ps.in_decl = true;
851 prefix_blankline_requested = 0;
852 parse(rbrace); /* let parser know about this */
853 ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
854 && ps.il[ps.tos] >= ps.ind_level;
855 if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
856 postfix_blankline_requested = 1;
857 break;
859 case swstmt: /* got keyword "switch" */
860 sp_sw = true;
861 hd_type = swstmt; /* keep this for when we have seen the
862 * expression */
863 goto copy_id; /* go move the token into buffer */
865 case sp_paren: /* token is if, while, for */
866 sp_sw = true; /* the interesting stuff is done after the
867 * expression is scanned */
868 hd_type = (*token == 'i' ? ifstmt :
869 (*token == 'w' ? whilestmt : forstmt));
872 * remember the type of header for later use by parser
874 goto copy_id; /* copy the token into line */
876 case sp_nparen: /* got else, do */
877 ps.in_stmt = false;
878 if (*token == 'e') {
879 if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
880 if (verbose)
881 diag2(0, "Line broken");
882 dump_line();/* make sure this starts a line */
883 ps.want_blank = false;
885 force_nl = true;/* also, following stuff must go onto new line */
886 last_else = 1;
887 parse(elselit);
889 else {
890 if (e_code != s_code) { /* make sure this starts a line */
891 if (verbose)
892 diag2(0, "Line broken");
893 dump_line();
894 ps.want_blank = false;
896 force_nl = true;/* also, following stuff must go onto new line */
897 last_else = 0;
898 parse(dolit);
900 goto copy_id; /* move the token into line */
902 case decl: /* we have a declaration type (int, register,
903 * etc.) */
904 parse(decl); /* let parser worry about indentation */
905 if (ps.last_token == rparen && ps.tos <= 1) {
906 ps.in_parameter_declaration = 1;
907 if (s_code != e_code) {
908 dump_line();
909 ps.want_blank = 0;
912 if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
913 ps.ind_level = ps.i_l_follow = 1;
914 ps.ind_stmt = 0;
916 ps.in_or_st = true; /* this might be a structure or initialization
917 * declaration */
918 ps.in_decl = ps.decl_on_line = true;
919 if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
920 ps.just_saw_decl = 2;
921 prefix_blankline_requested = 0;
922 for (i = 0; token[i++];); /* get length of token */
924 if (ps.ind_level == 0 || ps.dec_nest > 0) {
925 /* global variable or struct member in local variable */
926 dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
927 tabs_to_var = (use_tabs ? ps.decl_indent > 0 : 0);
928 } else {
929 /* local variable */
930 dec_ind = ps.local_decl_indent > 0 ? ps.local_decl_indent : i;
931 tabs_to_var = (use_tabs ? ps.local_decl_indent > 0 : 0);
933 goto copy_id;
935 case ident: /* got an identifier or constant */
936 if (ps.in_decl) { /* if we are in a declaration, we must indent
937 * identifier */
938 if (is_procname == 0 || !procnames_start_line) {
939 if (!ps.block_init && !ps.dumped_decl_indent) {
940 if (troff) {
941 if (ps.want_blank)
942 *e_code++ = ' ';
943 sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7);
944 e_code += strlen(e_code);
945 } else
946 indent_declaration(dec_ind, tabs_to_var);
947 ps.dumped_decl_indent = true;
948 ps.want_blank = false;
950 } else {
951 if (ps.want_blank)
952 *e_code++ = ' ';
953 ps.want_blank = false;
954 if (dec_ind && s_code != e_code) {
955 *e_code = '\0';
956 dump_line();
958 dec_ind = 0;
961 else if (sp_sw && ps.p_l_follow == 0) {
962 sp_sw = false;
963 force_nl = true;
964 ps.last_u_d = true;
965 ps.in_stmt = false;
966 parse(hd_type);
968 copy_id:
969 if (ps.want_blank)
970 *e_code++ = ' ';
971 if (troff && ps.its_a_keyword) {
972 e_code = chfont(&bodyf, &keywordf, e_code);
973 for (t_ptr = token; *t_ptr; ++t_ptr) {
974 CHECK_SIZE_CODE;
975 *e_code++ = keywordf.allcaps && islower(*t_ptr)
976 ? toupper(*t_ptr) : *t_ptr;
978 e_code = chfont(&keywordf, &bodyf, e_code);
980 else
981 for (t_ptr = token; *t_ptr; ++t_ptr) {
982 CHECK_SIZE_CODE;
983 *e_code++ = *t_ptr;
985 ps.want_blank = true;
986 break;
988 case period: /* treat a period kind of like a binary
989 * operation */
990 *e_code++ = '.'; /* move the period into line */
991 ps.want_blank = false; /* dont put a blank after a period */
992 break;
994 case comma:
995 ps.want_blank = (s_code != e_code); /* only put blank after comma
996 * if comma does not start the
997 * line */
998 if (ps.in_decl && is_procname == 0 && !ps.block_init &&
999 !ps.dumped_decl_indent) {
1000 /* indent leading commas and not the actual identifiers */
1001 indent_declaration(dec_ind - 1, tabs_to_var);
1002 ps.dumped_decl_indent = true;
1004 *e_code++ = ',';
1005 if (ps.p_l_follow == 0) {
1006 if (ps.block_init_level <= 0)
1007 ps.block_init = 0;
1008 if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8))
1009 force_nl = true;
1011 break;
1013 case preesc: /* got the character '#' */
1014 if ((s_com != e_com) ||
1015 (s_lab != e_lab) ||
1016 (s_code != e_code))
1017 dump_line();
1018 *e_lab++ = '#'; /* move whole line to 'label' buffer */
1020 int in_comment = 0;
1021 int com_start = 0;
1022 char quote = 0;
1023 int com_end = 0;
1025 while (*buf_ptr == ' ' || *buf_ptr == '\t') {
1026 buf_ptr++;
1027 if (buf_ptr >= buf_end)
1028 fill_buffer();
1030 while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
1031 CHECK_SIZE_LAB;
1032 *e_lab = *buf_ptr++;
1033 if (buf_ptr >= buf_end)
1034 fill_buffer();
1035 switch (*e_lab++) {
1036 case BACKSLASH:
1037 if (troff)
1038 *e_lab++ = BACKSLASH;
1039 if (!in_comment) {
1040 *e_lab++ = *buf_ptr++;
1041 if (buf_ptr >= buf_end)
1042 fill_buffer();
1044 break;
1045 case '/':
1046 if (*buf_ptr == '*' && !in_comment && !quote) {
1047 in_comment = 1;
1048 *e_lab++ = *buf_ptr++;
1049 com_start = e_lab - s_lab - 2;
1051 break;
1052 case '"':
1053 if (quote == '"')
1054 quote = 0;
1055 break;
1056 case '\'':
1057 if (quote == '\'')
1058 quote = 0;
1059 break;
1060 case '*':
1061 if (*buf_ptr == '/' && in_comment) {
1062 in_comment = 0;
1063 *e_lab++ = *buf_ptr++;
1064 com_end = e_lab - s_lab;
1066 break;
1070 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1071 e_lab--;
1072 if (e_lab - s_lab == com_end && bp_save == NULL) {
1073 /* comment on preprocessor line */
1074 if (sc_end == NULL) /* if this is the first comment, we
1075 * must set up the buffer */
1076 sc_end = &(save_com[0]);
1077 else {
1078 *sc_end++ = '\n'; /* add newline between
1079 * comments */
1080 *sc_end++ = ' ';
1081 --line_no;
1083 bcopy(s_lab + com_start, sc_end, com_end - com_start);
1084 sc_end += com_end - com_start;
1085 if (sc_end >= &save_com[sc_size])
1086 abort();
1087 e_lab = s_lab + com_start;
1088 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1089 e_lab--;
1090 bp_save = buf_ptr; /* save current input buffer */
1091 be_save = buf_end;
1092 buf_ptr = save_com; /* fix so that subsequent calls to
1093 * lexi will take tokens out of
1094 * save_com */
1095 *sc_end++ = ' '; /* add trailing blank, just in case */
1096 buf_end = sc_end;
1097 sc_end = NULL;
1099 *e_lab = '\0'; /* null terminate line */
1100 ps.pcase = false;
1103 if (strncmp(s_lab, "#if", 3) == 0) { /* also ifdef, ifndef */
1104 if ((size_t)ifdef_level < nitems(state_stack)) {
1105 match_state[ifdef_level].tos = -1;
1106 state_stack[ifdef_level++] = ps;
1108 else
1109 diag2(1, "#if stack overflow");
1111 else if (strncmp(s_lab, "#el", 3) == 0) { /* else, elif */
1112 if (ifdef_level <= 0)
1113 diag2(1, s_lab[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
1114 else {
1115 match_state[ifdef_level - 1] = ps;
1116 ps = state_stack[ifdef_level - 1];
1119 else if (strncmp(s_lab, "#endif", 6) == 0) {
1120 if (ifdef_level <= 0)
1121 diag2(1, "Unmatched #endif");
1122 else
1123 ifdef_level--;
1124 } else {
1125 struct directives {
1126 int size;
1127 const char *string;
1129 recognized[] = {
1130 {7, "include"},
1131 {6, "define"},
1132 {5, "undef"},
1133 {4, "line"},
1134 {5, "error"},
1135 {6, "pragma"}
1137 int d = nitems(recognized);
1138 while (--d >= 0)
1139 if (strncmp(s_lab + 1, recognized[d].string, recognized[d].size) == 0)
1140 break;
1141 if (d < 0) {
1142 diag2(1, "Unrecognized cpp directive");
1143 break;
1146 if (blanklines_around_conditional_compilation) {
1147 postfix_blankline_requested++;
1148 n_real_blanklines = 0;
1150 else {
1151 postfix_blankline_requested = 0;
1152 prefix_blankline_requested = 0;
1154 break; /* subsequent processing of the newline
1155 * character will cause the line to be printed */
1157 case comment: /* we have gotten a / followed by * this is a biggie */
1158 if (flushed_nl) { /* we should force a broken line here */
1159 flushed_nl = false;
1160 dump_line();
1161 ps.want_blank = false; /* dont insert blank at line start */
1162 force_nl = false;
1164 pr_comment();
1165 break;
1166 } /* end of big switch stmt */
1168 *e_code = '\0'; /* make sure code section is null terminated */
1169 if (type_code != comment && type_code != newline && type_code != preesc)
1170 ps.last_token = type_code;
1171 } /* end of main while (1) loop */
1175 * copy input file to backup file if in_name is /blah/blah/blah/file, then
1176 * backup file will be ".Bfile" then make the backup file the input and
1177 * original input file the output
1179 static void
1180 bakcopy(void)
1182 int n,
1183 bakchn;
1184 char buff[8 * 1024];
1185 const char *p;
1187 /* construct file name .Bfile */
1188 for (p = in_name; *p; p++); /* skip to end of string */
1189 while (p > in_name && *p != '/') /* find last '/' */
1190 p--;
1191 if (*p == '/')
1192 p++;
1193 sprintf(bakfile, "%s.BAK", p);
1195 /* copy in_name to backup file */
1196 bakchn = creat(bakfile, 0600);
1197 if (bakchn < 0)
1198 err(1, "%s", bakfile);
1199 while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
1200 if (write(bakchn, buff, n) != n)
1201 err(1, "%s", bakfile);
1202 if (n < 0)
1203 err(1, "%s", in_name);
1204 close(bakchn);
1205 fclose(input);
1207 /* re-open backup file as the input file */
1208 input = fopen(bakfile, "r");
1209 if (input == NULL)
1210 err(1, "%s", bakfile);
1211 /* now the original input file will be the output */
1212 output = fopen(in_name, "w");
1213 if (output == NULL) {
1214 unlink(bakfile);
1215 err(1, "%s", in_name);
1219 static void
1220 indent_declaration(int cur_dec_ind, int tabs_to_var)
1222 int pos = e_code - s_code;
1223 char *startpos = e_code;
1226 * get the tab math right for indentations that are not multiples of 8
1228 if ((ps.ind_level * ps.ind_size) % 8 != 0) {
1229 pos += (ps.ind_level * ps.ind_size) % 8;
1230 cur_dec_ind += (ps.ind_level * ps.ind_size) % 8;
1232 if (tabs_to_var)
1233 while ((pos & ~7) + 8 <= cur_dec_ind) {
1234 CHECK_SIZE_CODE;
1235 *e_code++ = '\t';
1236 pos = (pos & ~7) + 8;
1238 while (pos < cur_dec_ind) {
1239 CHECK_SIZE_CODE;
1240 *e_code++ = ' ';
1241 pos++;
1243 if (e_code == startpos && ps.want_blank) {
1244 *e_code++ = ' ';
1245 ps.want_blank = false;