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
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
35 * @(#) Copyright (c) 1985 Sun Microsystems, Inc.
36 * @(#) Copyright (c) 1976 Board of Trustees of the University of Illinois.
37 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
38 * @(#)indent.c 5.17 (Berkeley) 6/7/93
39 * $FreeBSD: src/usr.bin/indent/indent.c,v 1.26 2010/03/31 16:55:47 avg Exp $
42 #include <sys/param.h>
50 #include "indent_globs.h"
51 #include "indent_codes.h"
54 static void bakcopy(void);
56 const char *in_name = "Standard Input"; /* will always point to name of input
58 const char *out_name = "Standard Output"; /* will always point to name
60 char bakfile[MAXPATHLEN] = "";
63 main(int argc, char **argv)
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 (...),
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 expression 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>
81 const char *t_ptr; /* used for copying tokens */
82 int tabs_to_var; /* true if using tabs to indent to var name */
83 int type_code; /* the type of token, returned by lexi */
85 int last_else = 0; /* true iff last keyword was an else */
88 /*-----------------------------------------------*\
90 \*-----------------------------------------------*/
94 ps.p_stack[0] = stmt; /* this is the parser's stack */
95 ps.last_nl = true; /* this is true if the last thing scanned was
97 ps.last_token = semicolon;
98 combuf = (char *) malloc(bufsize);
101 labbuf = (char *) malloc(bufsize);
104 codebuf = (char *) malloc(bufsize);
107 tokenbuf = (char *) malloc(bufsize);
108 if (tokenbuf == NULL)
110 l_com = combuf + bufsize - 5;
111 l_lab = labbuf + bufsize - 5;
112 l_code = codebuf + bufsize - 5;
113 l_token = tokenbuf + bufsize - 5;
114 combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and
116 combuf[1] = codebuf[1] = labbuf[1] = '\0';
117 ps.else_if = 1; /* Default else-if special processing to on */
118 s_lab = e_lab = labbuf + 1;
119 s_code = e_code = codebuf + 1;
120 s_com = e_com = combuf + 1;
121 s_token = e_token = tokenbuf + 1;
123 in_buffer = (char *) malloc(10);
124 if (in_buffer == NULL)
126 in_buffer_limit = in_buffer + 8;
127 buf_ptr = buf_end = in_buffer;
129 had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
130 sp_sw = force_nl = false;
134 di_stack[ps.dec_nest = 0] = 0;
135 ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
137 scase = ps.pcase = false;
146 /*--------------------------------------------------*\
147 | COMMAND LINE SCAN |
148 \*--------------------------------------------------*/
151 max_col = 78; /* -l78 */
152 lineup_to_parens = 1; /* -lp */
153 ps.ljust_decl = 0; /* -ndj */
154 ps.com_ind = 33; /* -c33 */
155 star_comment_cont = 1; /* -sc */
156 ps.ind_size = 8; /* -i8 */
158 ps.decl_indent = 16; /* -di16 */
159 ps.local_decl_indent = -1; /* if this is not set to some nonnegative value
160 * by an arg, we will set this equal to
162 ps.indent_parameters = 1; /* -ip */
163 ps.decl_com_ind = 0; /* if this is not set to some positive value
164 * by an arg, we will set this equal to
166 btype_2 = 1; /* -br */
167 cuddle_else = 1; /* -ce */
168 ps.unindent_displace = 0; /* -d0 */
169 ps.case_indent = 0; /* -cli0 */
170 format_block_comments = 1; /* -fcb */
171 format_col1_comments = 1; /* -fc1 */
172 procnames_start_line = 1; /* -psl */
173 proc_calls_space = 0; /* -npcs */
174 comment_delimiter_on_blankline = 1; /* -cdb */
175 ps.leave_comma = 1; /* -nbc */
178 for (i = 1; i < argc; ++i)
179 if (strcmp(argv[i], "-npro") == 0)
185 for (i = 1; i < argc; ++i) {
188 * look thru args (if any) for changes to defaults
190 if (argv[i][0] != '-') {/* no flag on parameter */
191 if (input == 0) { /* we must have the input file */
192 in_name = argv[i]; /* remember name of input file */
193 input = fopen(in_name, "r");
194 if (input == 0) /* check for open error */
195 err(1, "%s", in_name);
198 else if (output == 0) { /* we have the output file */
199 out_name = argv[i]; /* remember name of output file */
200 if (strcmp(in_name, out_name) == 0) { /* attempt to overwrite
202 errx(1, "input and output files must be different");
204 output = fopen(out_name, "w");
205 if (output == 0) /* check for create error */
206 err(1, "%s", out_name);
209 errx(1, "unknown parameter: %s", argv[i]);
217 if (troff || input == stdin)
225 ps.com_ind = 2; /* dont put normal comments before column 2 */
227 if (bodyf.font[0] == 0)
228 parsefont(&bodyf, "R");
229 if (scomf.font[0] == 0)
230 parsefont(&scomf, "I");
231 if (blkcomf.font[0] == 0)
232 blkcomf = scomf, blkcomf.size += 2;
233 if (boxcomf.font[0] == 0)
235 if (stringf.font[0] == 0)
236 parsefont(&stringf, "L");
237 if (keywordf.font[0] == 0)
238 parsefont(&keywordf, "B");
239 writefdef(&bodyf, 'B');
240 writefdef(&scomf, 'C');
241 writefdef(&blkcomf, 'L');
242 writefdef(&boxcomf, 'X');
243 writefdef(&stringf, 'S');
244 writefdef(&keywordf, 'K');
246 if (block_comment_max_col <= 0)
247 block_comment_max_col = max_col;
248 if (ps.local_decl_indent < 0) /* if not specified by user, set this */
249 ps.local_decl_indent = ps.decl_indent;
250 if (ps.decl_com_ind <= 0) /* if not specified by user, set this */
251 ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
252 if (continuation_indent == 0)
253 continuation_indent = ps.ind_size;
254 fill_buffer(); /* get first batch of stuff into input buffer */
265 col = ((col - 1) & ~7) + 9;
270 if (col > ps.ind_size)
271 ps.ind_level = ps.i_l_follow = col / ps.ind_size;
274 const char *p = in_name,
280 fprintf(output, ".Fn \"%s\"\n", beg);
286 while (1) { /* this is the main loop. it will go until we
290 type_code = lexi(); /* lexi reads one token. The actual
291 * characters read are stored in "token". lexi
292 * returns a code indicating the type of token */
293 is_procname = ps.procname[0];
296 * The following code moves everything following an if (), while (),
297 * else, etc. up to the start of the following stmt to a buffer. This
298 * allows proper handling of both kinds of brace placement.
302 while (ps.search_brace) { /* if we scanned an if(), while(),
303 * etc., we might need to copy stuff
304 * into a buffer we must loop, copying
305 * stuff into save_com, until we find
306 * the start of the stmt which follows
307 * the if, or whatever */
313 break; /* form feeds and newlines found here will be
316 case lbrace: /* this is a brace that starts the compound
318 if (sc_end == 0) { /* ignore buffering if a comment wasnt
320 ps.search_brace = false;
324 save_com[0] = '{'; /* we either want to put the brace
325 * right after the if */
326 goto sw_buffer; /* go to common code to get out of
329 case comment: /* we have a comment, so we must copy it into
331 if (!flushed_nl || sc_end != 0) {
332 if (sc_end == 0) { /* if this is the first comment, we
333 * must set up the buffer */
334 save_com[0] = save_com[1] = ' ';
335 sc_end = &(save_com[2]);
338 *sc_end++ = '\n'; /* add newline between
343 *sc_end++ = '/'; /* copy in start of comment */
346 for (;;) { /* loop until we get to the end of the comment */
347 *sc_end = *buf_ptr++;
348 if (buf_ptr >= buf_end)
351 if (*sc_end++ == '*' && *buf_ptr == '/')
352 break; /* we are at end of comment */
354 if (sc_end >= &(save_com[sc_size])) { /* check for temp buffer
356 diag2(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
361 *sc_end++ = '/'; /* add ending slash */
362 if (++buf_ptr >= buf_end) /* get past / in buffer */
366 default: /* it is the start of a normal statement */
367 if (flushed_nl) /* if we flushed a newline, make sure it is
370 if ((type_code == sp_paren && *token == 'i'
371 && last_else && ps.else_if)
372 || (type_code == sp_nparen && *token == 'e'
373 && e_code != s_code && e_code[-1] == '}'))
376 if (sc_end == 0) { /* ignore buffering if comment wasnt
378 ps.search_brace = false;
381 if (force_nl) { /* if we should insert a nl here, put it into
384 --line_no; /* this will be re-increased when the nl is
385 * read from the buffer */
388 if (verbose && !flushed_nl) /* print error msg if the line
389 * was not already broken */
390 diag2(0, "Line broken");
393 for (t_ptr = token; *t_ptr; ++t_ptr)
394 *sc_end++ = *t_ptr; /* copy token into temp buffer */
398 ps.search_brace = false; /* stop looking for start of
400 bp_save = buf_ptr; /* save current input buffer */
402 buf_ptr = save_com; /* fix so that subsequent calls to
403 * lexi will take tokens out of
405 *sc_end++ = ' ';/* add trailing blank, just in case */
409 } /* end of switch */
410 if (type_code != 0) /* we must make this check, just in case there
411 * was an unexpected EOF */
412 type_code = lexi(); /* read another token */
413 /* if (ps.search_brace) ps.procname[0] = 0; */
414 if ((is_procname = ps.procname[0]) && flushed_nl
415 && !procnames_start_line && ps.in_decl
416 && type_code == ident)
418 } /* end of while (search_brace) */
421 if (type_code == 0) { /* we got eof */
422 if (s_lab != e_lab || s_code != e_code
423 || s_com != e_com) /* must dump end of line */
425 if (ps.tos > 1) /* check for balanced braces */
426 diag2(1, "Stuff missing from end of file");
429 printf("There were %d output lines and %d comments\n",
430 ps.out_lines, ps.out_coms);
431 printf("(Lines with comments)/(Lines with code): %6.3f\n",
432 (1.0 * ps.com_lines) / code_lines);
438 (type_code != comment) &&
439 (type_code != newline) &&
440 (type_code != preesc) &&
441 (type_code != form_feed)) {
443 (type_code != semicolon) &&
444 (type_code != lbrace || !btype_2)) {
445 /* we should force a broken line here */
446 if (verbose && !flushed_nl)
447 diag2(0, "Line broken");
450 ps.want_blank = false; /* dont insert blank at line start */
453 ps.in_stmt = true; /* turn on flag which causes an extra level of
454 * indentation. this is turned off by a ; or
456 if (s_com != e_com) { /* the turkey has embedded a comment
457 * in a line. fix it */
459 for (t_ptr = s_com; *t_ptr; ++t_ptr) {
464 *e_code = '\0'; /* null terminate code sect */
465 ps.want_blank = false;
469 else if (type_code != comment) /* preserve force_nl thru a comment */
470 force_nl = false; /* cancel forced newline after newline, form
475 /*-----------------------------------------------------*\
476 | do switch on type of token scanned |
477 \*-----------------------------------------------------*/
479 switch (type_code) { /* now, decide what to do with the token */
481 case form_feed: /* found a form feed in line */
482 ps.use_ff = true; /* a form feed is treated much like a newline */
484 ps.want_blank = false;
488 if (ps.last_token != comma || ps.p_l_follow > 0
489 || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
491 ps.want_blank = false;
493 ++line_no; /* keep track of input line number */
496 case lparen: /* got a '(' or '[' */
497 ++ps.p_l_follow; /* count parens to make Healy happy */
498 if (ps.want_blank && *token != '[' &&
499 (ps.last_token != ident || proc_calls_space
500 || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon))))
502 if (ps.in_decl && !ps.block_init)
503 if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) {
504 ps.dumped_decl_indent = 1;
505 sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
506 e_code += strlen(e_code);
509 while ((e_code - s_code) < dec_ind) {
513 *e_code++ = token[0];
516 *e_code++ = token[0];
517 ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;
518 if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
519 && ps.paren_indents[0] < 2 * ps.ind_size)
520 ps.paren_indents[0] = 2 * ps.ind_size;
521 ps.want_blank = false;
522 if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
524 * this is a kluge to make sure that declarations will be
525 * aligned right if proc decl has an explicit type on it, i.e.
528 parse(semicolon); /* I said this was a kluge... */
529 ps.in_or_st = false; /* turn off flag for structure decl or
532 if (ps.sizeof_keyword)
533 ps.sizeof_mask |= 1 << ps.p_l_follow;
536 case rparen: /* got a ')' or ']' */
538 if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
540 ps.cast_mask &= (1 << ps.p_l_follow) - 1;
541 ps.want_blank = false;
543 ps.want_blank = true;
544 ps.sizeof_mask &= (1 << ps.p_l_follow) - 1;
545 if (--ps.p_l_follow < 0) {
547 diag3(0, "Extra %c", *token);
549 if (e_code == s_code) /* if the paren starts the line */
550 ps.paren_level = ps.p_l_follow; /* then indent it */
552 *e_code++ = token[0];
554 if (sp_sw && (ps.p_l_follow == 0)) { /* check for end of if
555 * (...), or some such */
557 force_nl = true;/* must force newline after if */
558 ps.last_u_d = true; /* inform lexi that a following
559 * operator is unary */
560 ps.in_stmt = false; /* dont use stmt continuation
563 parse(hd_type); /* let parser worry about if, or whatever */
565 ps.search_brace = btype_2; /* this should insure that constructs
566 * such as main(){...} and int[]{...}
567 * have their braces put in the right
571 case unary_op: /* this could be any unary operation */
575 if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) {
576 sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
577 ps.dumped_decl_indent = 1;
578 e_code += strlen(e_code);
581 const char *res = token;
583 if (ps.in_decl && !ps.block_init) { /* if this is a unary op
584 * in a declaration, we
587 for (i = 0; token[i]; ++i); /* find length of token */
588 while ((e_code - s_code) < (dec_ind - i)) {
590 *e_code++ = ' '; /* pad it */
593 if (troff && token[0] == '-' && token[1] == '>')
595 for (t_ptr = res; *t_ptr; ++t_ptr) {
600 ps.want_blank = false;
603 case binary_op: /* any binary operation */
607 const char *res = token;
626 else if (token[1] == 0)
630 for (t_ptr = res; *t_ptr; ++t_ptr) {
632 *e_code++ = *t_ptr; /* move the operator */
635 ps.want_blank = true;
638 case postop: /* got a trailing ++ or -- */
639 *e_code++ = token[0];
640 *e_code++ = token[1];
641 ps.want_blank = true;
644 case question: /* got a ? */
645 squest++; /* this will be used when a later colon
646 * appears so we can distinguish the
647 * <c>?<n>:<n> construct */
651 ps.want_blank = true;
654 case casestmt: /* got word 'case' or 'default' */
655 scase = true; /* so we can process the later colon properly */
658 case colon: /* got a ':' */
659 if (squest > 0) { /* it is part of the <c>?<n>: <n> construct */
664 ps.want_blank = true;
669 ps.want_blank = false;
672 ps.in_stmt = false; /* seeing a label does not imply we are in a
674 for (t_ptr = s_code; *t_ptr; ++t_ptr)
675 *e_lab++ = *t_ptr; /* turn everything so far into a label */
681 force_nl = ps.pcase = scase; /* ps.pcase will be used by
682 * dump_line to decide how to
683 * indent the label. force_nl
684 * will force a case n: to be
685 * on a line by itself */
687 ps.want_blank = false;
690 case semicolon: /* got a ';' */
691 ps.in_or_st = false;/* we are not in an initialization or
692 * structure declaration */
693 scase = false; /* these will only need resetting in an error */
695 if (ps.last_token == rparen && rparen_count == 0)
696 ps.in_parameter_declaration = 0;
700 ps.block_init_level = 0;
703 if (ps.in_decl && s_code == e_code && !ps.block_init)
704 while ((e_code - s_code) < (dec_ind - 1)) {
709 ps.in_decl = (ps.dec_nest > 0); /* if we were in a first level
710 * structure declaration, we
713 if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
716 * This should be true iff there were unbalanced parens in the
717 * stmt. It is a bit complicated, because the semicolon might
720 diag2(1, "Unbalanced parens");
722 if (sp_sw) { /* this is a check for an if, while, etc. with
723 * unbalanced parens */
725 parse(hd_type); /* dont lose the if, or whatever */
729 ps.want_blank = true;
730 ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in the
731 * middle of a stmt */
733 if (!sp_sw) { /* if not if for (;;) */
734 parse(semicolon); /* let parser know about end of stmt */
735 force_nl = true;/* force newline after an end of stmt */
739 case lbrace: /* got a '{' */
740 ps.in_stmt = false; /* dont indent the {} */
742 force_nl = true;/* force other stuff on same line as '{' onto
744 else if (ps.block_init_level <= 0)
745 ps.block_init_level = 1;
747 ps.block_init_level++;
749 if (s_code != e_code && !ps.block_init) {
752 ps.want_blank = false;
754 else if (ps.in_parameter_declaration && !ps.in_or_st) {
756 if (function_brace_split) { /* dump the line prior to the
759 ps.want_blank = false;
760 } else /* add a space between the decl and brace */
761 ps.want_blank = true;
764 if (ps.in_parameter_declaration)
765 prefix_blankline_requested = 0;
767 if (ps.p_l_follow > 0) { /* check for preceding unbalanced
769 diag2(1, "Unbalanced parens");
771 if (sp_sw) { /* check for unclosed if, for, etc. */
774 ps.ind_level = ps.i_l_follow;
777 if (s_code == e_code)
778 ps.ind_stmt = false; /* dont put extra indentation on line
780 if (ps.in_decl && ps.in_or_st) { /* this is either a structure
781 * declaration or an init */
782 di_stack[ps.dec_nest++] = dec_ind;
786 ps.decl_on_line = false; /* we cant be in the middle of
787 * a declaration, so dont do
788 * special indentation of
790 if (blanklines_after_declarations_at_proctop
791 && ps.in_parameter_declaration)
792 postfix_blankline_requested = 1;
793 ps.in_parameter_declaration = 0;
796 parse(lbrace); /* let parser know about this */
797 if (ps.want_blank) /* put a blank before '{' if '{' is not at
800 ps.want_blank = false;
802 ps.just_saw_decl = 0;
805 case rbrace: /* got a '}' */
806 if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be
810 if (ps.p_l_follow) {/* check for unclosed if, for, else. */
811 diag2(1, "Unbalanced parens");
815 ps.just_saw_decl = 0;
816 ps.block_init_level--;
817 if (s_code != e_code && !ps.block_init) { /* '}' must be first on
820 diag2(0, "Line broken");
824 ps.want_blank = true;
825 ps.in_stmt = ps.ind_stmt = false;
826 if (ps.dec_nest > 0) { /* we are in multi-level structure
828 dec_ind = di_stack[--ps.dec_nest];
829 if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
830 ps.just_saw_decl = 2;
833 prefix_blankline_requested = 0;
834 parse(rbrace); /* let parser know about this */
835 ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
836 && ps.il[ps.tos] >= ps.ind_level;
837 if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
838 postfix_blankline_requested = 1;
841 case swstmt: /* got keyword "switch" */
843 hd_type = swstmt; /* keep this for when we have seen the
845 goto copy_id; /* go move the token into buffer */
847 case sp_paren: /* token is if, while, for */
848 sp_sw = true; /* the interesting stuff is done after the
849 * expression is scanned */
850 hd_type = (*token == 'i' ? ifstmt :
851 (*token == 'w' ? whilestmt : forstmt));
854 * remember the type of header for later use by parser
856 goto copy_id; /* copy the token into line */
858 case sp_nparen: /* got else, do */
861 if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
863 diag2(0, "Line broken");
864 dump_line();/* make sure this starts a line */
865 ps.want_blank = false;
867 force_nl = true;/* also, following stuff must go onto new line */
872 if (e_code != s_code) { /* make sure this starts a line */
874 diag2(0, "Line broken");
876 ps.want_blank = false;
878 force_nl = true;/* also, following stuff must go onto new line */
882 goto copy_id; /* move the token into line */
884 case decl: /* we have a declaration type (int, register,
886 parse(decl); /* let parser worry about indentation */
887 if (ps.last_token == rparen && ps.tos <= 1) {
888 ps.in_parameter_declaration = 1;
889 if (s_code != e_code) {
894 if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
895 ps.ind_level = ps.i_l_follow = 1;
898 ps.in_or_st = true; /* this might be a structure or initialization
900 ps.in_decl = ps.decl_on_line = true;
901 if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
902 ps.just_saw_decl = 2;
903 prefix_blankline_requested = 0;
904 for (i = 0; token[i++];); /* get length of token */
906 if (ps.ind_level == 0 || ps.dec_nest > 0) {
907 /* global variable or struct member in local variable */
908 dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
909 tabs_to_var = (use_tabs ? ps.decl_indent > 0 : 0);
912 dec_ind = ps.local_decl_indent > 0 ? ps.local_decl_indent : i;
913 tabs_to_var = (use_tabs ? ps.local_decl_indent > 0 : 0);
917 case ident: /* got an identifier or constant */
918 if (ps.in_decl) { /* if we are in a declaration, we must indent
920 if (is_procname == 0 || !procnames_start_line) {
921 if (!ps.block_init) {
922 if (troff && !ps.dumped_decl_indent) {
925 ps.want_blank = false;
926 sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7);
927 ps.dumped_decl_indent = 1;
928 e_code += strlen(e_code);
934 * in order to get the tab math right for
935 * indentations that are not multiples of 8 we
936 * need to modify both startpos and dec_ind
937 * (cur_dec_ind) here by eight minus the
938 * remainder of the current starting column
939 * divided by eight. This seems to be a
940 * properly working fix
942 startpos = e_code - s_code;
943 cur_dec_ind = dec_ind;
945 if ((ps.ind_level * ps.ind_size) % 8 != 0) {
946 pos += (ps.ind_level * ps.ind_size) % 8;
947 cur_dec_ind += (ps.ind_level * ps.ind_size) % 8;
951 while ((pos & ~7) + 8 <= cur_dec_ind) {
954 pos = (pos & ~7) + 8;
957 while (pos < cur_dec_ind) {
962 if (ps.want_blank && e_code - s_code == startpos)
964 ps.want_blank = false;
970 ps.want_blank = false;
971 if (dec_ind && s_code != e_code)
976 else if (sp_sw && ps.p_l_follow == 0) {
986 if (troff && ps.its_a_keyword) {
987 e_code = chfont(&bodyf, &keywordf, e_code);
988 for (t_ptr = token; *t_ptr; ++t_ptr) {
990 *e_code++ = keywordf.allcaps && islower(*t_ptr)
991 ? toupper(*t_ptr) : *t_ptr;
993 e_code = chfont(&keywordf, &bodyf, e_code);
996 for (t_ptr = token; *t_ptr; ++t_ptr) {
1000 ps.want_blank = true;
1003 case period: /* treat a period kind of like a binary
1005 *e_code++ = '.'; /* move the period into line */
1006 ps.want_blank = false; /* dont put a blank after a period */
1010 ps.want_blank = (s_code != e_code); /* only put blank after comma
1011 * if comma does not start the
1013 if (ps.in_decl && is_procname == 0 && !ps.block_init)
1014 while ((e_code - s_code) < (dec_ind - 1)) {
1020 if (ps.p_l_follow == 0) {
1021 if (ps.block_init_level <= 0)
1023 if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8))
1028 case preesc: /* got the character '#' */
1029 if ((s_com != e_com) ||
1033 *e_lab++ = '#'; /* move whole line to 'label' buffer */
1040 while (*buf_ptr == ' ' || *buf_ptr == '\t') {
1042 if (buf_ptr >= buf_end)
1045 while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
1047 *e_lab = *buf_ptr++;
1048 if (buf_ptr >= buf_end)
1053 *e_lab++ = BACKSLASH;
1055 *e_lab++ = *buf_ptr++;
1056 if (buf_ptr >= buf_end)
1061 if (*buf_ptr == '*' && !in_comment && !quote) {
1063 *e_lab++ = *buf_ptr++;
1064 com_start = e_lab - s_lab - 2;
1076 if (*buf_ptr == '/' && in_comment) {
1078 *e_lab++ = *buf_ptr++;
1079 com_end = e_lab - s_lab;
1085 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1087 if (e_lab - s_lab == com_end && bp_save == 0) { /* comment on
1088 * preprocessor line */
1089 if (sc_end == 0) /* if this is the first comment, we
1090 * must set up the buffer */
1091 sc_end = &(save_com[0]);
1093 *sc_end++ = '\n'; /* add newline between
1098 bcopy(s_lab + com_start, sc_end, com_end - com_start);
1099 sc_end += com_end - com_start;
1100 if (sc_end >= &save_com[sc_size])
1102 e_lab = s_lab + com_start;
1103 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1105 bp_save = buf_ptr; /* save current input buffer */
1107 buf_ptr = save_com; /* fix so that subsequent calls to
1108 * lexi will take tokens out of
1110 *sc_end++ = ' '; /* add trailing blank, just in case */
1114 *e_lab = '\0'; /* null terminate line */
1118 if (strncmp(s_lab, "#if", 3) == 0) {
1119 if (blanklines_around_conditional_compilation) {
1121 prefix_blankline_requested++;
1122 while ((c = getc(input)) == '\n');
1125 if ((size_t)ifdef_level < sizeof(state_stack)/sizeof(state_stack[0])) {
1126 match_state[ifdef_level].tos = -1;
1127 state_stack[ifdef_level++] = ps;
1130 diag2(1, "#if stack overflow");
1132 else if (strncmp(s_lab, "#else", 5) == 0)
1133 if (ifdef_level <= 0)
1134 diag2(1, "Unmatched #else");
1136 match_state[ifdef_level - 1] = ps;
1137 ps = state_stack[ifdef_level - 1];
1139 else if (strncmp(s_lab, "#endif", 6) == 0) {
1140 if (ifdef_level <= 0)
1141 diag2(1, "Unmatched #endif");
1147 * This match needs to be more intelligent before the
1150 if (match_state[ifdef_level].tos >= 0
1151 && bcmp(&ps, &match_state[ifdef_level], sizeof ps))
1152 diag2(0, "Syntactically inconsistent #ifdef alternatives");
1155 if (blanklines_around_conditional_compilation) {
1156 postfix_blankline_requested++;
1157 n_real_blanklines = 0;
1160 break; /* subsequent processing of the newline
1161 * character will cause the line to be printed */
1163 case comment: /* we have gotten a / followed by * this is a biggie */
1164 if (flushed_nl) { /* we should force a broken line here */
1167 ps.want_blank = false; /* dont insert blank at line start */
1172 } /* end of big switch stmt */
1174 *e_code = '\0'; /* make sure code section is null terminated */
1175 if (type_code != comment && type_code != newline && type_code != preesc)
1176 ps.last_token = type_code;
1177 } /* end of main while (1) loop */
1181 * copy input file to backup file if in_name is /blah/blah/blah/file, then
1182 * backup file will be ".Bfile" then make the backup file the input and
1183 * original input file the output
1190 char buff[8 * 1024];
1193 /* construct file name .Bfile */
1194 for (p = in_name; *p; p++); /* skip to end of string */
1195 while (p > in_name && *p != '/') /* find last '/' */
1199 sprintf(bakfile, "%s.BAK", p);
1201 /* copy in_name to backup file */
1202 bakchn = creat(bakfile, 0600);
1204 err(1, "%s", bakfile);
1205 while ((n = read(fileno(input), buff, sizeof buff)) != 0)
1206 if (write(bakchn, buff, n) != n)
1207 err(1, "%s", bakfile);
1209 err(1, "%s", in_name);
1213 /* re-open backup file as the input file */
1214 input = fopen(bakfile, "r");
1216 err(1, "%s", bakfile);
1217 /* now the original input file will be the output */
1218 output = fopen(in_name, "w");
1221 err(1, "%s", in_name);