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
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";
46 static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
61 #include "indent_globs.h"
62 #include "indent_codes.h"
65 static void bakcopy(void);
67 const char *in_name = "Standard Input"; /* will always point to name of input
69 const char *out_name = "Standard Output"; /* will always point to name
71 char bakfile[MAXPATHLEN] = "";
74 main(int argc, char **argv)
77 int dec_ind; /* current indentation for declarations */
78 int di_stack[20]; /* a stack of structure indentation levels */
79 int flushed_nl; /* used when buffering up comments to remember
80 * that a newline was passed over */
81 int force_nl; /* when true, code must be broken */
82 int hd_type = 0; /* used to store type of stmt for if (...),
84 int i; /* local loop counter */
85 int scase; /* set to true when we see a case, so we will
86 * know what to do with the following colon */
87 int sp_sw; /* when true, we are in the expression of
88 * if(...), while(...), etc. */
89 int squest; /* when this is positive, we have seen a ?
90 * without the matching : in a <c>?<s>:<s>
92 const char *t_ptr; /* used for copying tokens */
93 int tabs_to_var; /* true if using tabs to indent to var name */
94 int type_code; /* the type of token, returned by lexi */
96 int last_else = 0; /* true iff last keyword was an else */
99 /*-----------------------------------------------*\
101 \*-----------------------------------------------*/
105 ps.p_stack[0] = stmt; /* this is the parser's stack */
106 ps.last_nl = true; /* this is true if the last thing scanned was
108 ps.last_token = semicolon;
109 combuf = (char *) malloc(bufsize);
112 labbuf = (char *) malloc(bufsize);
115 codebuf = (char *) malloc(bufsize);
118 tokenbuf = (char *) malloc(bufsize);
119 if (tokenbuf == NULL)
121 l_com = combuf + bufsize - 5;
122 l_lab = labbuf + bufsize - 5;
123 l_code = codebuf + bufsize - 5;
124 l_token = tokenbuf + bufsize - 5;
125 combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and
127 combuf[1] = codebuf[1] = labbuf[1] = '\0';
128 ps.else_if = 1; /* Default else-if special processing to on */
129 s_lab = e_lab = labbuf + 1;
130 s_code = e_code = codebuf + 1;
131 s_com = e_com = combuf + 1;
132 s_token = e_token = tokenbuf + 1;
134 in_buffer = (char *) malloc(10);
135 if (in_buffer == NULL)
137 in_buffer_limit = in_buffer + 8;
138 buf_ptr = buf_end = in_buffer;
140 had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
141 sp_sw = force_nl = false;
145 di_stack[ps.dec_nest = 0] = 0;
146 ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
148 scase = ps.pcase = false;
157 /*--------------------------------------------------*\
158 | COMMAND LINE SCAN |
159 \*--------------------------------------------------*/
162 max_col = 78; /* -l78 */
163 lineup_to_parens = 1; /* -lp */
164 ps.ljust_decl = 0; /* -ndj */
165 ps.com_ind = 33; /* -c33 */
166 star_comment_cont = 1; /* -sc */
167 ps.ind_size = 8; /* -i8 */
169 ps.decl_indent = 16; /* -di16 */
170 ps.local_decl_indent = -1; /* if this is not set to some nonnegative value
171 * by an arg, we will set this equal to
173 ps.indent_parameters = 1; /* -ip */
174 ps.decl_com_ind = 0; /* if this is not set to some positive value
175 * by an arg, we will set this equal to
177 btype_2 = 1; /* -br */
178 cuddle_else = 1; /* -ce */
179 ps.unindent_displace = 0; /* -d0 */
180 ps.case_indent = 0; /* -cli0 */
181 format_block_comments = 1; /* -fcb */
182 format_col1_comments = 1; /* -fc1 */
183 procnames_start_line = 1; /* -psl */
184 proc_calls_space = 0; /* -npcs */
185 comment_delimiter_on_blankline = 1; /* -cdb */
186 ps.leave_comma = 1; /* -nbc */
189 for (i = 1; i < argc; ++i)
190 if (strcmp(argv[i], "-npro") == 0)
196 for (i = 1; i < argc; ++i) {
199 * look thru args (if any) for changes to defaults
201 if (argv[i][0] != '-') {/* no flag on parameter */
202 if (input == NULL) { /* we must have the input file */
203 in_name = argv[i]; /* remember name of input file */
204 input = fopen(in_name, "r");
205 if (input == NULL) /* check for open error */
206 err(1, "%s", in_name);
209 else if (output == NULL) { /* we have the output file */
210 out_name = argv[i]; /* remember name of output file */
211 if (strcmp(in_name, out_name) == 0) { /* attempt to overwrite
213 errx(1, "input and output files must be different");
215 output = fopen(out_name, "w");
216 if (output == NULL) /* check for create error */
217 err(1, "%s", out_name);
220 errx(1, "unknown parameter: %s", argv[i]);
227 if (output == NULL) {
228 if (troff || input == stdin)
236 ps.com_ind = 2; /* dont put normal comments before column 2 */
238 if (bodyf.font[0] == 0)
239 parsefont(&bodyf, "R");
240 if (scomf.font[0] == 0)
241 parsefont(&scomf, "I");
242 if (blkcomf.font[0] == 0)
243 blkcomf = scomf, blkcomf.size += 2;
244 if (boxcomf.font[0] == 0)
246 if (stringf.font[0] == 0)
247 parsefont(&stringf, "L");
248 if (keywordf.font[0] == 0)
249 parsefont(&keywordf, "B");
250 writefdef(&bodyf, 'B');
251 writefdef(&scomf, 'C');
252 writefdef(&blkcomf, 'L');
253 writefdef(&boxcomf, 'X');
254 writefdef(&stringf, 'S');
255 writefdef(&keywordf, 'K');
257 if (block_comment_max_col <= 0)
258 block_comment_max_col = max_col;
259 if (ps.local_decl_indent < 0) /* if not specified by user, set this */
260 ps.local_decl_indent = ps.decl_indent;
261 if (ps.decl_com_ind <= 0) /* if not specified by user, set this */
262 ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
263 if (continuation_indent == 0)
264 continuation_indent = ps.ind_size;
265 fill_buffer(); /* get first batch of stuff into input buffer */
276 col = ((col - 1) & ~7) + 9;
281 if (col > ps.ind_size)
282 ps.ind_level = ps.i_l_follow = col / ps.ind_size;
285 const char *p = in_name,
291 fprintf(output, ".Fn \"%s\"\n", beg);
297 while (1) { /* this is the main loop. it will go until we
301 type_code = lexi(); /* lexi reads one token. The actual
302 * characters read are stored in "token". lexi
303 * returns a code indicating the type of token */
304 is_procname = ps.procname[0];
307 * The following code moves everything following an if (), while (),
308 * else, etc. up to the start of the following stmt to a buffer. This
309 * allows proper handling of both kinds of brace placement.
313 while (ps.search_brace) { /* if we scanned an if(), while(),
314 * etc., we might need to copy stuff
315 * into a buffer we must loop, copying
316 * stuff into save_com, until we find
317 * the start of the stmt which follows
318 * the if, or whatever */
323 goto sw_buffer; /* dump comment, if any */
326 break; /* form feeds and newlines found here will be
329 case lbrace: /* this is a brace that starts the compound
331 if (sc_end == NULL) { /* ignore buffering if a comment wasn't
333 ps.search_brace = false;
337 save_com[0] = '{'; /* we either want to put the brace
338 * right after the if */
339 goto sw_buffer; /* go to common code to get out of
342 case comment: /* we have a comment, so we must copy it into
344 if (!flushed_nl || sc_end != NULL) {
345 if (sc_end == NULL) { /* if this is the first comment, we
346 * must set up the buffer */
347 save_com[0] = save_com[1] = ' ';
348 sc_end = &(save_com[2]);
351 *sc_end++ = '\n'; /* add newline between
356 *sc_end++ = '/'; /* copy in start of comment */
359 for (;;) { /* loop until we get to the end of the comment */
360 *sc_end = *buf_ptr++;
361 if (buf_ptr >= buf_end)
364 if (*sc_end++ == '*' && *buf_ptr == '/')
365 break; /* we are at end of comment */
367 if (sc_end >= &(save_com[sc_size])) { /* check for temp buffer
369 diag2(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
374 *sc_end++ = '/'; /* add ending slash */
375 if (++buf_ptr >= buf_end) /* get past / in buffer */
379 default: /* it is the start of a normal statement */
380 if (flushed_nl) /* if we flushed a newline, make sure it is
383 if ((type_code == sp_paren && *token == 'i'
384 && last_else && ps.else_if)
385 || (type_code == sp_nparen && *token == 'e'
386 && e_code != s_code && e_code[-1] == '}'))
389 if (sc_end == NULL) { /* ignore buffering if comment wasn't
391 ps.search_brace = false;
394 if (force_nl) { /* if we should insert a nl here, put it into
397 --line_no; /* this will be re-increased when the nl is
398 * read from the buffer */
401 if (verbose && !flushed_nl) /* print error msg if the line
402 * was not already broken */
403 diag2(0, "Line broken");
406 for (t_ptr = token; *t_ptr; ++t_ptr)
407 *sc_end++ = *t_ptr; /* copy token into temp buffer */
411 ps.search_brace = false; /* stop looking for start of
413 bp_save = buf_ptr; /* save current input buffer */
415 buf_ptr = save_com; /* fix so that subsequent calls to
416 * lexi will take tokens out of
418 *sc_end++ = ' ';/* add trailing blank, just in case */
422 } /* end of switch */
423 if (type_code != 0) /* we must make this check, just in case there
424 * was an unexpected EOF */
425 type_code = lexi(); /* read another token */
426 /* if (ps.search_brace) ps.procname[0] = 0; */
427 if ((is_procname = ps.procname[0]) && flushed_nl
428 && !procnames_start_line && ps.in_decl
429 && type_code == ident)
431 } /* end of while (search_brace) */
434 if (type_code == 0) { /* we got eof */
435 if (s_lab != e_lab || s_code != e_code
436 || s_com != e_com) /* must dump end of line */
438 if (ps.tos > 1) /* check for balanced braces */
439 diag2(1, "Stuff missing from end of file");
442 printf("There were %d output lines and %d comments\n",
443 ps.out_lines, ps.out_coms);
444 printf("(Lines with comments)/(Lines with code): %6.3f\n",
445 (1.0 * ps.com_lines) / code_lines);
451 (type_code != comment) &&
452 (type_code != newline) &&
453 (type_code != preesc) &&
454 (type_code != form_feed)) {
456 (type_code != semicolon) &&
457 (type_code != lbrace || !btype_2)) {
458 /* we should force a broken line here */
459 if (verbose && !flushed_nl)
460 diag2(0, "Line broken");
463 ps.want_blank = false; /* dont insert blank at line start */
466 ps.in_stmt = true; /* turn on flag which causes an extra level of
467 * indentation. this is turned off by a ; or
469 if (s_com != e_com) { /* the turkey has embedded a comment
470 * in a line. fix it */
472 for (t_ptr = s_com; *t_ptr; ++t_ptr) {
477 *e_code = '\0'; /* null terminate code sect */
478 ps.want_blank = false;
482 else if (type_code != comment) /* preserve force_nl thru a comment */
483 force_nl = false; /* cancel forced newline after newline, form
488 /*-----------------------------------------------------*\
489 | do switch on type of token scanned |
490 \*-----------------------------------------------------*/
492 switch (type_code) { /* now, decide what to do with the token */
494 case form_feed: /* found a form feed in line */
495 ps.use_ff = true; /* a form feed is treated much like a newline */
497 ps.want_blank = false;
501 if (ps.last_token != comma || ps.p_l_follow > 0
502 || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
504 ps.want_blank = false;
506 ++line_no; /* keep track of input line number */
509 case lparen: /* got a '(' or '[' */
510 ++ps.p_l_follow; /* count parens to make Healy happy */
511 if (ps.want_blank && *token != '[' &&
512 (ps.last_token != ident || proc_calls_space
513 || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon))))
515 if (ps.in_decl && !ps.block_init)
516 if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) {
517 ps.dumped_decl_indent = 1;
518 sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
519 e_code += strlen(e_code);
522 while ((e_code - s_code) < dec_ind) {
526 *e_code++ = token[0];
529 *e_code++ = token[0];
530 ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;
531 if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
532 && ps.paren_indents[0] < 2 * ps.ind_size)
533 ps.paren_indents[0] = 2 * ps.ind_size;
534 ps.want_blank = false;
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.
541 parse(semicolon); /* I said this was a kluge... */
542 ps.in_or_st = false; /* turn off flag for structure decl or
545 if (ps.sizeof_keyword)
546 ps.sizeof_mask |= 1 << ps.p_l_follow;
549 case rparen: /* got a ')' or ']' */
551 if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
553 ps.cast_mask &= (1 << ps.p_l_follow) - 1;
554 ps.want_blank = false;
556 ps.want_blank = true;
557 ps.sizeof_mask &= (1 << ps.p_l_follow) - 1;
558 if (--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 */
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
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
584 case unary_op: /* this could be any unary operation */
588 if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) {
589 sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
590 ps.dumped_decl_indent = 1;
591 e_code += strlen(e_code);
594 const char *res = token;
596 if (ps.in_decl && !ps.block_init) { /* if this is a unary op
597 * in a declaration, we
600 for (i = 0; token[i]; ++i); /* find length of token */
601 while ((e_code - s_code) < (dec_ind - i)) {
603 *e_code++ = ' '; /* pad it */
606 if (troff && token[0] == '-' && token[1] == '>')
608 for (t_ptr = res; *t_ptr; ++t_ptr) {
613 ps.want_blank = false;
616 case binary_op: /* any binary operation */
620 const char *res = token;
639 else if (token[1] == 0)
643 for (t_ptr = res; *t_ptr; ++t_ptr) {
645 *e_code++ = *t_ptr; /* move the operator */
648 ps.want_blank = true;
651 case postop: /* got a trailing ++ or -- */
652 *e_code++ = token[0];
653 *e_code++ = token[1];
654 ps.want_blank = true;
657 case question: /* got a ? */
658 squest++; /* this will be used when a later colon
659 * appears so we can distinguish the
660 * <c>?<n>:<n> construct */
664 ps.want_blank = true;
667 case casestmt: /* got word 'case' or 'default' */
668 scase = true; /* so we can process the later colon properly */
671 case colon: /* got a ':' */
672 if (squest > 0) { /* it is part of the <c>?<n>: <n> construct */
677 ps.want_blank = true;
682 ps.want_blank = false;
685 ps.in_stmt = false; /* seeing a label does not imply we are in a
687 for (t_ptr = s_code; *t_ptr; ++t_ptr)
688 *e_lab++ = *t_ptr; /* turn everything so far into a label */
694 force_nl = ps.pcase = scase; /* ps.pcase will be used by
695 * dump_line to decide how to
696 * indent the label. force_nl
697 * will force a case n: to be
698 * on a line by itself */
700 ps.want_blank = false;
703 case semicolon: /* got a ';' */
704 if (ps.dec_nest == 0) {
705 /* we are not in an initialization or structure declaration */
708 scase = false; /* these will only need resetting in an error */
710 if (ps.last_token == rparen && rparen_count == 0)
711 ps.in_parameter_declaration = 0;
715 ps.block_init_level = 0;
718 if (ps.in_decl && s_code == e_code && !ps.block_init)
719 while ((e_code - s_code) < (dec_ind - 1)) {
724 ps.in_decl = (ps.dec_nest > 0); /* if we were in a first level
725 * structure declaration, we
728 if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
731 * This should be true iff there were unbalanced parens in the
732 * stmt. It is a bit complicated, because the semicolon might
735 diag2(1, "Unbalanced parens");
737 if (sp_sw) { /* this is a check for an if, while, etc. with
738 * unbalanced parens */
740 parse(hd_type); /* dont lose the if, or whatever */
744 ps.want_blank = true;
745 ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in the
746 * middle of a stmt */
748 if (!sp_sw) { /* if not if for (;;) */
749 parse(semicolon); /* let parser know about end of stmt */
750 force_nl = true;/* force newline after an end of stmt */
754 case lbrace: /* got a '{' */
755 ps.in_stmt = false; /* dont indent the {} */
757 force_nl = true;/* force other stuff on same line as '{' onto
759 else if (ps.block_init_level <= 0)
760 ps.block_init_level = 1;
762 ps.block_init_level++;
764 if (s_code != e_code && !ps.block_init) {
767 ps.want_blank = false;
769 else if (ps.in_parameter_declaration && !ps.in_or_st) {
771 if (function_brace_split) { /* dump the line prior to the
774 ps.want_blank = false;
775 } else /* add a space between the decl and brace */
776 ps.want_blank = true;
779 if (ps.in_parameter_declaration)
780 prefix_blankline_requested = 0;
782 if (ps.p_l_follow > 0) { /* check for preceding unbalanced
784 diag2(1, "Unbalanced parens");
786 if (sp_sw) { /* check for unclosed if, for, etc. */
789 ps.ind_level = ps.i_l_follow;
792 if (s_code == e_code)
793 ps.ind_stmt = false; /* dont put extra indentation on line
795 if (ps.in_decl && ps.in_or_st) { /* this is either a structure
796 * declaration or an init */
797 di_stack[ps.dec_nest++] = dec_ind;
801 ps.decl_on_line = false; /* we can't be in the middle of
802 * a declaration, so don't do
803 * special indentation of
805 if (blanklines_after_declarations_at_proctop
806 && ps.in_parameter_declaration)
807 postfix_blankline_requested = 1;
808 ps.in_parameter_declaration = 0;
811 parse(lbrace); /* let parser know about this */
812 if (ps.want_blank) /* put a blank before '{' if '{' is not at
815 ps.want_blank = false;
817 ps.just_saw_decl = 0;
820 case rbrace: /* got a '}' */
821 if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be
825 if (ps.p_l_follow) {/* check for unclosed if, for, else. */
826 diag2(1, "Unbalanced parens");
830 ps.just_saw_decl = 0;
831 ps.block_init_level--;
832 if (s_code != e_code && !ps.block_init) { /* '}' must be first on
835 diag2(0, "Line broken");
839 ps.want_blank = true;
840 ps.in_stmt = ps.ind_stmt = false;
841 if (ps.dec_nest > 0) { /* we are in multi-level structure
843 dec_ind = di_stack[--ps.dec_nest];
844 if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
845 ps.just_saw_decl = 2;
848 prefix_blankline_requested = 0;
849 parse(rbrace); /* let parser know about this */
850 ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
851 && ps.il[ps.tos] >= ps.ind_level;
852 if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
853 postfix_blankline_requested = 1;
856 case swstmt: /* got keyword "switch" */
858 hd_type = swstmt; /* keep this for when we have seen the
860 goto copy_id; /* go move the token into buffer */
862 case sp_paren: /* token is if, while, for */
863 sp_sw = true; /* the interesting stuff is done after the
864 * expression is scanned */
865 hd_type = (*token == 'i' ? ifstmt :
866 (*token == 'w' ? whilestmt : forstmt));
869 * remember the type of header for later use by parser
871 goto copy_id; /* copy the token into line */
873 case sp_nparen: /* got else, do */
876 if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
878 diag2(0, "Line broken");
879 dump_line();/* make sure this starts a line */
880 ps.want_blank = false;
882 force_nl = true;/* also, following stuff must go onto new line */
887 if (e_code != s_code) { /* make sure this starts a line */
889 diag2(0, "Line broken");
891 ps.want_blank = false;
893 force_nl = true;/* also, following stuff must go onto new line */
897 goto copy_id; /* move the token into line */
899 case decl: /* we have a declaration type (int, register,
901 parse(decl); /* let parser worry about indentation */
902 if (ps.last_token == rparen && ps.tos <= 1) {
903 ps.in_parameter_declaration = 1;
904 if (s_code != e_code) {
909 if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
910 ps.ind_level = ps.i_l_follow = 1;
913 ps.in_or_st = true; /* this might be a structure or initialization
915 ps.in_decl = ps.decl_on_line = true;
916 if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
917 ps.just_saw_decl = 2;
918 prefix_blankline_requested = 0;
919 for (i = 0; token[i++];); /* get length of token */
921 if (ps.ind_level == 0 || ps.dec_nest > 0) {
922 /* global variable or struct member in local variable */
923 dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
924 tabs_to_var = (use_tabs ? ps.decl_indent > 0 : 0);
927 dec_ind = ps.local_decl_indent > 0 ? ps.local_decl_indent : i;
928 tabs_to_var = (use_tabs ? ps.local_decl_indent > 0 : 0);
932 case ident: /* got an identifier or constant */
933 if (ps.in_decl) { /* if we are in a declaration, we must indent
935 if (is_procname == 0 || !procnames_start_line) {
936 if (!ps.block_init) {
937 if (troff && !ps.dumped_decl_indent) {
940 ps.want_blank = false;
941 sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7);
942 ps.dumped_decl_indent = 1;
943 e_code += strlen(e_code);
949 * in order to get the tab math right for
950 * indentations that are not multiples of 8 we
951 * need to modify both startpos and dec_ind
952 * (cur_dec_ind) here by eight minus the
953 * remainder of the current starting column
954 * divided by eight. This seems to be a
955 * properly working fix
957 startpos = e_code - s_code;
958 cur_dec_ind = dec_ind;
960 if ((ps.ind_level * ps.ind_size) % 8 != 0) {
961 pos += (ps.ind_level * ps.ind_size) % 8;
962 cur_dec_ind += (ps.ind_level * ps.ind_size) % 8;
966 while ((pos & ~7) + 8 <= cur_dec_ind) {
969 pos = (pos & ~7) + 8;
972 while (pos < cur_dec_ind) {
977 if (ps.want_blank && e_code - s_code == startpos)
979 ps.want_blank = false;
985 ps.want_blank = false;
986 if (dec_ind && s_code != e_code) {
993 else if (sp_sw && ps.p_l_follow == 0) {
1003 if (troff && ps.its_a_keyword) {
1004 e_code = chfont(&bodyf, &keywordf, e_code);
1005 for (t_ptr = token; *t_ptr; ++t_ptr) {
1007 *e_code++ = keywordf.allcaps && islower(*t_ptr)
1008 ? toupper(*t_ptr) : *t_ptr;
1010 e_code = chfont(&keywordf, &bodyf, e_code);
1013 for (t_ptr = token; *t_ptr; ++t_ptr) {
1017 ps.want_blank = true;
1020 case period: /* treat a period kind of like a binary
1022 *e_code++ = '.'; /* move the period into line */
1023 ps.want_blank = false; /* dont put a blank after a period */
1027 ps.want_blank = (s_code != e_code); /* only put blank after comma
1028 * if comma does not start the
1030 if (ps.in_decl && is_procname == 0 && !ps.block_init)
1031 while ((e_code - s_code) < (dec_ind - 1)) {
1037 if (ps.p_l_follow == 0) {
1038 if (ps.block_init_level <= 0)
1040 if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8))
1045 case preesc: /* got the character '#' */
1046 if ((s_com != e_com) ||
1050 *e_lab++ = '#'; /* move whole line to 'label' buffer */
1057 while (*buf_ptr == ' ' || *buf_ptr == '\t') {
1059 if (buf_ptr >= buf_end)
1062 while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
1064 *e_lab = *buf_ptr++;
1065 if (buf_ptr >= buf_end)
1070 *e_lab++ = BACKSLASH;
1072 *e_lab++ = *buf_ptr++;
1073 if (buf_ptr >= buf_end)
1078 if (*buf_ptr == '*' && !in_comment && !quote) {
1080 *e_lab++ = *buf_ptr++;
1081 com_start = e_lab - s_lab - 2;
1093 if (*buf_ptr == '/' && in_comment) {
1095 *e_lab++ = *buf_ptr++;
1096 com_end = e_lab - s_lab;
1102 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1104 /* comment on preprocessor line */
1105 if (e_lab - s_lab == com_end && bp_save == NULL) {
1106 if (sc_end == NULL) /* if this is the first comment, we
1107 * must set up the buffer */
1108 sc_end = &(save_com[0]);
1110 *sc_end++ = '\n'; /* add newline between
1115 bcopy(s_lab + com_start, sc_end, com_end - com_start);
1116 sc_end += com_end - com_start;
1117 if (sc_end >= &save_com[sc_size])
1119 e_lab = s_lab + com_start;
1120 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1122 bp_save = buf_ptr; /* save current input buffer */
1124 buf_ptr = save_com; /* fix so that subsequent calls to
1125 * lexi will take tokens out of
1127 *sc_end++ = ' '; /* add trailing blank, just in case */
1131 *e_lab = '\0'; /* null terminate line */
1135 if (strncmp(s_lab, "#if", 3) == 0) {
1136 if (blanklines_around_conditional_compilation) {
1138 prefix_blankline_requested++;
1139 while ((c = getc(input)) == '\n');
1142 if ((size_t)ifdef_level < sizeof(state_stack)/sizeof(state_stack[0])) {
1143 match_state[ifdef_level].tos = -1;
1144 state_stack[ifdef_level++] = ps;
1147 diag2(1, "#if stack overflow");
1149 else if (strncmp(s_lab, "#else", 5) == 0)
1150 if (ifdef_level <= 0)
1151 diag2(1, "Unmatched #else");
1153 match_state[ifdef_level - 1] = ps;
1154 ps = state_stack[ifdef_level - 1];
1156 else if (strncmp(s_lab, "#endif", 6) == 0) {
1157 if (ifdef_level <= 0)
1158 diag2(1, "Unmatched #endif");
1164 * This match needs to be more intelligent before the
1167 if (match_state[ifdef_level].tos >= 0
1168 && bcmp(&ps, &match_state[ifdef_level], sizeof ps))
1169 diag2(0, "Syntactically inconsistent #ifdef alternatives");
1172 if (blanklines_around_conditional_compilation) {
1173 postfix_blankline_requested++;
1174 n_real_blanklines = 0;
1177 break; /* subsequent processing of the newline
1178 * character will cause the line to be printed */
1180 case comment: /* we have gotten a / followed by * this is a biggie */
1181 if (flushed_nl) { /* we should force a broken line here */
1184 ps.want_blank = false; /* dont insert blank at line start */
1189 } /* end of big switch stmt */
1191 *e_code = '\0'; /* make sure code section is null terminated */
1192 if (type_code != comment && type_code != newline && type_code != preesc)
1193 ps.last_token = type_code;
1194 } /* end of main while (1) loop */
1198 * copy input file to backup file if in_name is /blah/blah/blah/file, then
1199 * backup file will be ".Bfile" then make the backup file the input and
1200 * original input file the output
1207 char buff[8 * 1024];
1210 /* construct file name .Bfile */
1211 for (p = in_name; *p; p++); /* skip to end of string */
1212 while (p > in_name && *p != '/') /* find last '/' */
1216 sprintf(bakfile, "%s.BAK", p);
1218 /* copy in_name to backup file */
1219 bakchn = creat(bakfile, 0600);
1221 err(1, "%s", bakfile);
1222 while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
1223 if (write(bakchn, buff, n) != n)
1224 err(1, "%s", bakfile);
1226 err(1, "%s", in_name);
1230 /* re-open backup file as the input file */
1231 input = fopen(bakfile, "r");
1233 err(1, "%s", bakfile);
1234 /* now the original input file will be the output */
1235 output = fopen(in_name, "w");
1236 if (output == NULL) {
1238 err(1, "%s", in_name);