2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980 The Regents of the University of California.
4 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the University of California, Berkeley, the University of Illinois,
13 * Urbana, and Sun Microsystems, Inc. The name of either University
14 * or Sun Microsystems may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 /* ported to MINIX by: Robert R. Hall <hall@pnet01.cts.com> */
23 #define _POSIX_SOURCE 1
25 #define NAME_SIZE _POSIX_NAME_MAX
29 #include <sys/types.h>
39 char *in_name
= "Standard Input"; /* will always point to
41 char *out_name
= "Standard Output"; /* will always point to
42 name of output file */
50 extern int found_err
; /* flag set in diag() on error */
51 int dec_ind
; /* current indentation for
53 int di_stack
[20]; /* a stack of structure
55 int flushed_nl
; /* used when buffering up
56 comments to remember that a
57 newline was passed over */
58 int force_nl
; /* when true, code must be
60 int hd_type
; /* used to store type of stmt
61 for if (...), for (...), etc */
62 register int i
; /* local loop counter */
63 int scase
; /* set to true when we see a
64 case, so we will know what
65 to do with the following
67 int sp_sw
; /* when true, we are in the
70 int squest
; /* when this is positive, we
71 have seen a ? without the
72 matching : in a <c>?<s>:<s>
74 register char *t_ptr
; /* used for copying tokens */
75 int type_code
; /* the type of token, returned
78 int last_else
= 0; /* true iff last keyword was an
82 /*-----------------------------------------------*\
84 \*-----------------------------------------------*/
87 ps
.p_stack
[0] = stmt
; /* this is the parser's stack */
88 ps
.last_nl
= true; /* this is true if the last
89 thing scanned was a newline */
90 ps
.last_token
= semicolon
;
91 combuf
= (char *) malloc(bufsize
);
92 labbuf
= (char *) malloc(bufsize
);
93 codebuf
= (char *) malloc(bufsize
);
94 l_com
= combuf
+ bufsize
- 5;
95 l_lab
= labbuf
+ bufsize
- 5;
96 l_code
= codebuf
+ bufsize
- 5;
97 combuf
[0] = codebuf
[0] = labbuf
[0] = ' '; /* set up code, label,
98 and comment buffers */
99 combuf
[1] = codebuf
[1] = labbuf
[1] = '\0';
100 ps
.else_if
= 1; /* Default else-if special
102 s_lab
= e_lab
= labbuf
+ 1;
103 s_code
= e_code
= codebuf
+ 1;
104 s_com
= e_com
= combuf
+ 1;
106 buf_ptr
= buf_end
= in_buffer
;
108 had_eof
= ps
.in_decl
= ps
.decl_on_line
= break_comma
= false;
109 sp_sw
= force_nl
= false;
113 di_stack
[ps
.dec_nest
= 0] = 0;
114 ps
.want_blank
= ps
.in_stmt
= ps
.ind_stmt
= false;
117 scase
= ps
.pcase
= false;
127 /*--------------------------------------------------*\
128 | COMMAND LINE SCAN |
129 \*--------------------------------------------------*/
132 max_col
= 78; /* -l78 */
133 lineup_to_parens
= 1; /* -lp */
134 ps
.ljust_decl
= 0; /* -ndj */
135 ps
.com_ind
= 33; /* -c33 */
136 star_comment_cont
= 1; /* -sc */
137 ps
.ind_size
= 8; /* -i8 */
139 ps
.decl_indent
= 16; /* -di16 */
140 ps
.indent_parameters
= 1; /* -ip */
141 ps
.decl_com_ind
= 0; /* if this is not set to some
142 positive value by an arg, we
143 will set this equal to
145 btype_2
= 1; /* -br */
146 cuddle_else
= 1; /* -ce */
147 ps
.unindent_displace
= 0; /* -d0 */
148 ps
.case_indent
= 0; /* -cli0 */
149 format_col1_comments
= 1; /* -fc1 */
150 procnames_start_line
= 1; /* -psl */
151 proc_calls_space
= 0; /* -npcs */
152 comment_delimiter_on_blankline
= 1; /* -cdb */
153 ps
.leave_comma
= 1; /* -nbc */
156 for (i
= 1; i
< argc
; ++i
)
157 if (strcmp(argv
[i
], "-npro") == 0)
163 for (i
= 1; i
< argc
; ++i
)
166 /* look thru args (if any) for changes to defaults */
167 if (argv
[i
][0] != '-')
168 { /* no flag on parameter */
170 { /* we must have the input file */
171 in_name
= argv
[i
]; /* remember name of input file */
172 input
= fopen(in_name
, "r");
174 { /* check for open error */
175 fprintf(stderr
, "indent: can't open %s\n", argv
[i
]);
179 } else if (output
== 0)
180 { /* we have the output file */
181 out_name
= argv
[i
]; /* remember name of output file */
182 if (strcmp(in_name
, out_name
) == 0)
183 { /* attempt to overwrite the
185 fprintf(stderr
, "indent: input and output files must be different\n");
188 output
= fopen(out_name
, "w");
190 { /* check for create error */
191 fprintf(stderr
, "indent: can't create %s\n", argv
[i
]);
196 fprintf(stderr
, "indent: unknown parameter: %s\n", argv
[i
]);
203 fprintf(stderr
, "indent: usage: indent file [ outfile ] [ options ]\n");
215 ps
.com_ind
= 2; /* dont put normal comments
219 if (bodyf
.font
[0] == 0)
220 parsefont(&bodyf
, "R");
221 if (scomf
.font
[0] == 0)
222 parsefont(&scomf
, "I");
223 if (blkcomf
.font
[0] == 0)
224 blkcomf
= scomf
, blkcomf
.size
+= 2;
225 if (boxcomf
.font
[0] == 0)
227 if (stringf
.font
[0] == 0)
228 parsefont(&stringf
, "L");
229 if (keywordf
.font
[0] == 0)
230 parsefont(&keywordf
, "B");
231 writefdef(&bodyf
, 'B');
232 writefdef(&scomf
, 'C');
233 writefdef(&blkcomf
, 'L');
234 writefdef(&boxcomf
, 'X');
235 writefdef(&stringf
, 'S');
236 writefdef(&keywordf
, 'K');
239 bk_max_col
= max_col
;
240 if (ps
.decl_com_ind
<= 0) /* if not specified by user,
242 ps
.decl_com_ind
= ps
.ljust_decl
? (ps
.com_ind
<= 10 ? 2 : ps
.com_ind
- 8) : ps
.com_ind
;
243 if (continuation_indent
== 0)
244 continuation_indent
= ps
.ind_size
;
245 fill_buffer(); /* get first batch of stuff
250 register char *p
= buf_ptr
;
258 col
= ((col
- 1) & ~7) + 9;
263 if (col
> ps
.ind_size
)
264 ps
.ind_level
= ps
.i_l_follow
= col
/ ps
.ind_size
;
268 register char *p
= in_name
, *beg
= in_name
;
273 fprintf(output
, ".Fn \"%s\"\n", beg
);
275 /* START OF MAIN LOOP */
278 { /* this is the main loop. it
279 will go until we reach eof */
282 type_code
= lexi(); /* lexi reads one token. The
283 actual characters read are
284 stored in "token". lexi
285 returns a code indicating
287 is_procname
= ps
.procname
[0];
289 /* The following code moves everything following an if (), while
290 (), else, etc. up to the start of the following stmt to a
291 buffer. This allows proper handling of both kinds of brace
295 while (ps
.search_brace
)
296 { /* if we scanned an if(),
297 while(), etc., we might need
298 to copy stuff into a buffer
299 we must loop, copying stuff
300 into save_com, until we find
301 the start of the stmt which
302 follows the if, or whatever */
309 break; /* form feeds and newlines
310 found here will be ignored */
312 case lbrace
: /* this is a brace that starts
315 { /* ignore buffering if a
316 comment wasnt stored up */
317 ps
.search_brace
= false;
322 save_com
[0] = '{'; /* we either want to put the
323 brace right after the if */
324 goto sw_buffer
; /* go to common code to get out
328 * Something is buffered up in save_com, and -bl processing is in effect.
329 * Add the brace after the comment so it will come out on the next line.
331 flushed_nl
= 0; /* comment can start on the same line */
332 *sc_end
++ = '\n'; /* break line after comment */
336 case comment
: /* we have a comment, so we
337 must copy it into the buffer */
338 if (!flushed_nl
|| sc_end
!= 0)
341 { /* if this is the first
342 comment, we must set up the
344 save_com
[0] = save_com
[1] = ' ';
345 sc_end
= &(save_com
[2]);
348 *sc_end
++ = '\n'; /* add newline between comments */
352 *sc_end
++ = '/'; /* copy in start of comment */
356 { /* loop until we get to the end
358 *sc_end
= *buf_ptr
++;
359 if (buf_ptr
>= buf_end
)
362 if (*sc_end
++ == '*' && *buf_ptr
== '/')
363 break; /* we are at end of comment */
365 if (sc_end
>= &(save_com
[sc_size
]))
366 { /* check for temp buffer
368 diag(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever.");
373 *sc_end
++ = '/'; /* add ending slash */
374 if (++buf_ptr
>= buf_end
)/* get past / in buffer */
378 default: /* it is the start of a normal
380 if (flushed_nl
) /* if we flushed a newline,
381 make sure it is put back */
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] == '}'))
390 { /* ignore buffering if comment
392 ps
.search_brace
= false;
396 { /* if we should insert a nl
397 here, put it into the buffer */
399 --line_no
; /* this will be re-increased
400 when the nl is read from the
404 if (verbose
&& !flushed_nl
) /* print error msg if
407 diag(0, "Line broken");
410 for (t_ptr
= token
; *t_ptr
; ++t_ptr
)
411 *sc_end
++ = *t_ptr
; /* copy token into temp buffer */
415 ps
.search_brace
= false; /* stop looking for start of
417 bp_save
= buf_ptr
; /* save current input buffer */
419 buf_ptr
= save_com
; /* fix so that subsequent calls
420 to lexi will take tokens out
422 *sc_end
++ = ' '; /* add trailing blank, just in
427 } /* end of switch */
428 if (type_code
!= 0) /* we must make this check,
429 just in case there was an
431 type_code
= lexi(); /* read another token */
432 /* if (ps.search_brace) ps.procname[0] = 0; */
433 if ((is_procname
= ps
.procname
[0]) && flushed_nl
434 && !proc_str_line
&& ps
.in_decl
435 && type_code
== ident
)
437 } /* end of while (search_brace) */
442 if (s_lab
!= e_lab
|| s_code
!= e_code
443 || s_com
!= e_com
) /* must dump end of line */
445 if (ps
.tos
> 1) /* check for balanced braces */
446 diag(1, "Stuff missing from end of file.");
450 printf("There were %d output lines and %d comments\n",
451 ps
.out_lines
, ps
.out_coms
);
452 printf("(Lines with comments)/(Lines with code): %6d\n",
453 ps
.com_lines
/ code_lines
);
459 (type_code
!= comment
) &&
460 (type_code
!= newline
) &&
461 (type_code
!= preesc
) &&
462 (type_code
!= form_feed
))
465 (type_code
!= semicolon
) &&
466 (type_code
!= lbrace
|| !btype_2
))
468 /* we should force a broken line here */
469 if (verbose
&& !flushed_nl
)
470 diag(0, "Line broken");
473 ps
.want_blank
= false; /* dont insert blank at line
477 ps
.in_stmt
= true; /* turn on flag which causes an
478 extra level of indentation.
479 this is turned off by a ; or
482 { /* the turkey has embedded a
483 comment in a line. fix it */
485 for (t_ptr
= s_com
; *t_ptr
; ++t_ptr
)
487 if (e_code
>= l_code
)
489 register nsize
= l_code
- s_code
+ 400;
490 codebuf
= (char *) realloc(codebuf
, nsize
);
491 e_code
= codebuf
+ (e_code
- s_code
) + 1;
492 l_code
= codebuf
+ nsize
- 5;
493 s_code
= codebuf
+ 1;
498 *e_code
= '\0'; /* null terminate code sect */
499 ps
.want_blank
= false;
502 } else if (type_code
!= comment
) /* preserve force_nl thru a
504 force_nl
= false; /* cancel forced newline after
505 newline, form feed, etc */
509 /*-----------------------------------------------------*\
510 | do switch on type of token scanned |
511 \*-----------------------------------------------------*/
512 if (e_code
>= l_code
)
514 register nsize
= l_code
- s_code
+ 400;
515 codebuf
= (char *) realloc(codebuf
, nsize
);
516 e_code
= codebuf
+ (e_code
- s_code
) + 1;
517 l_code
= codebuf
+ nsize
- 5;
518 s_code
= codebuf
+ 1;
521 { /* now, decide what to do with
524 case form_feed
: /* found a form feed in line */
525 ps
.use_ff
= true; /* a form feed is treated much
528 ps
.want_blank
= false;
532 if (ps
.last_token
!= comma
|| ps
.p_l_follow
> 0
533 || !ps
.leave_comma
|| ps
.block_init
|| !break_comma
|| s_com
!= e_com
)
536 ps
.want_blank
= false;
538 ++line_no
; /* keep track of input line
542 case lparen
: /* got a '(' or '[' */
543 ++ps
.p_l_follow
; /* count parens to make Healy
545 if (ps
.want_blank
&& *token
!= '[' &&
546 (ps
.last_token
!= ident
|| proc_calls_space
547 || (ps
.its_a_keyword
&& (!ps
.sizeof_keyword
|| Bill_Shannon
))))
549 if (ps
.in_decl
&& !ps
.block_init
)
550 if (troff
&& !ps
.dumped_decl_indent
&& !is_procname
&& ps
.last_token
== decl
)
552 ps
.dumped_decl_indent
= 1;
553 sprintf(e_code
, "\n.Du %dp+\200p \"%s\"\n", dec_ind
* 7, token
);
554 e_code
+= strlen(e_code
);
557 while ((e_code
- s_code
) < dec_ind
)
559 if (e_code
>= l_code
)
561 register nsize
= l_code
- s_code
+ 400;
562 codebuf
= (char *) realloc(codebuf
, nsize
);
563 e_code
= codebuf
+ (e_code
- s_code
) + 1;
564 l_code
= codebuf
+ nsize
- 5;
565 s_code
= codebuf
+ 1;
569 *e_code
++ = token
[0];
572 *e_code
++ = token
[0];
573 ps
.paren_indents
[ps
.p_l_follow
- 1] = e_code
- s_code
;
574 if (sp_sw
&& ps
.p_l_follow
== 1 && ex_expr_indent
575 && ps
.paren_indents
[0] < 2 * ps
.ind_size
)
576 ps
.paren_indents
[0] = 2 * ps
.ind_size
;
577 ps
.want_blank
= false;
578 if (ps
.in_or_st
&& *token
== '(' && ps
.tos
<= 2)
580 /* this is a kluge to make sure that declarations will be
581 aligned right if proc decl has an explicit type on it,
582 i.e. "int a(x) {..." */
583 parse(semicolon
); /* I said this was a kluge... */
584 ps
.in_or_st
= false; /* turn off flag for structure
585 decl or initialization */
587 if (ps
.sizeof_keyword
)
588 ps
.sizeof_mask
|= 1 << ps
.p_l_follow
;
591 case rparen
: /* got a ')' or ']' */
592 if (ps
.cast_mask
& (1 << ps
.p_l_follow
) & ~ps
.sizeof_mask
)
595 ps
.cast_mask
&= (1 << ps
.p_l_follow
) - 1;
597 ps
.sizeof_mask
&= (1 << ps
.p_l_follow
) - 1;
598 if (--ps
.p_l_follow
< 0)
601 diag(0, "Extra %c", *token
);
603 if (e_code
== s_code
) /* if the paren starts the line */
604 ps
.paren_level
= ps
.p_l_follow
; /* then indent it */
606 *e_code
++ = token
[0];
607 ps
.want_blank
= true;
609 if (sp_sw
&& (ps
.p_l_follow
== 0))
610 { /* check for end of if (...),
613 force_nl
= true; /* must force newline after if */
614 ps
.last_u_d
= true; /* inform lexi that a following
616 ps
.in_stmt
= false; /* dont use stmt continuation
619 parse(hd_type
); /* let parser worry about if,
622 ps
.search_brace
= btype_2
; /* this should insure that
624 main(){...} and int[]{...}
625 have their braces put in the
629 case unary_op
: /* this could be any unary
634 if (troff
&& !ps
.dumped_decl_indent
&& ps
.in_decl
&& !is_procname
)
636 sprintf(e_code
, "\n.Du %dp+\200p \"%s\"\n", dec_ind
* 7, token
);
637 ps
.dumped_decl_indent
= 1;
638 e_code
+= strlen(e_code
);
643 if (ps
.in_decl
&& !ps
.block_init
)
644 { /* if this is a unary op in a
645 declaration, we should
647 for (i
= 0; token
[i
]; ++i
); /* find length of token */
648 while ((e_code
- s_code
) < (dec_ind
- i
))
650 if (e_code
>= l_code
)
652 register nsize
= l_code
- s_code
+ 400;
653 codebuf
= (char *) realloc(codebuf
, nsize
);
654 e_code
= codebuf
+ (e_code
- s_code
) + 1;
655 l_code
= codebuf
+ nsize
- 5;
656 s_code
= codebuf
+ 1;
658 *e_code
++ = ' '; /* pad it */
661 if (troff
&& token
[0] == '-' && token
[1] == '>')
663 for (t_ptr
= res
; *t_ptr
; ++t_ptr
)
665 if (e_code
>= l_code
)
667 register nsize
= l_code
- s_code
+ 400;
668 codebuf
= (char *) realloc(codebuf
, nsize
);
669 e_code
= codebuf
+ (e_code
- s_code
) + 1;
670 l_code
= codebuf
+ nsize
- 5;
671 s_code
= codebuf
+ 1;
676 ps
.want_blank
= false;
679 case binary_op
: /* any binary operation */
703 else if (token
[1] == 0)
707 for (t_ptr
= res
; *t_ptr
; ++t_ptr
)
709 if (e_code
>= l_code
)
711 register nsize
= l_code
- s_code
+ 400;
712 codebuf
= (char *) realloc(codebuf
, nsize
);
713 e_code
= codebuf
+ (e_code
- s_code
) + 1;
714 l_code
= codebuf
+ nsize
- 5;
715 s_code
= codebuf
+ 1;
717 *e_code
++ = *t_ptr
; /* move the operator */
720 ps
.want_blank
= true;
723 case postop
: /* got a trailing ++ or -- */
724 *e_code
++ = token
[0];
725 *e_code
++ = token
[1];
726 ps
.want_blank
= true;
729 case question
: /* got a ? */
730 squest
++; /* this will be used when a
731 later colon appears so we
733 <c>?<n>:<n> construct */
737 ps
.want_blank
= true;
740 case casestmt
: /* got word 'case' or 'default' */
741 scase
= true; /* so we can process the later
745 case colon
: /* got a ':' */
747 { /* it is part of the <c>?<n>:
753 ps
.want_blank
= true;
759 ps
.want_blank
= false;
762 ps
.in_stmt
= false; /* seeing a label does not
763 imply we are in a stmt */
764 for (t_ptr
= s_code
; *t_ptr
; ++t_ptr
)
765 *e_lab
++ = *t_ptr
; /* turn everything so far into
772 force_nl
= ps
.pcase
= scase
; /* ps.pcase will be used by
773 dump_line to decide how to
774 indent the label. force_nl
775 will force a case n: to be
776 on a line by itself */
778 ps
.want_blank
= false;
781 case semicolon
: /* got a ';' */
782 ps
.in_or_st
= false; /* we are not in an
783 initialization or structure
785 scase
= false; /* these will only need
786 resetting in a error */
788 if (ps
.last_token
== rparen
)
793 ps
.block_init_level
= 0;
796 if (ps
.in_decl
&& s_code
== e_code
&& !ps
.block_init
)
797 while ((e_code
- s_code
) < (dec_ind
- 1))
799 if (e_code
>= l_code
)
801 register nsize
= l_code
- s_code
+ 400;
802 codebuf
= (char *) realloc(codebuf
, nsize
);
803 e_code
= codebuf
+ (e_code
- s_code
) + 1;
804 l_code
= codebuf
+ nsize
- 5;
805 s_code
= codebuf
+ 1;
810 ps
.in_decl
= (ps
.dec_nest
> 0);/* if we were in a first level
811 structure declaration, we
814 if ((!sp_sw
|| hd_type
!= forstmt
) && ps
.p_l_follow
> 0)
817 /* This should be true iff there were unbalanced parens in
818 the stmt. It is a bit complicated, because the
819 semicolon might be in a for stmt */
820 diag(1, "Unbalanced parens");
823 { /* this is a check for a if,
824 while, etc. with unbalanced
827 parse(hd_type
); /* dont lose the if, or
832 ps
.want_blank
= true;
833 ps
.in_stmt
= (ps
.p_l_follow
> 0); /* we are no longer in
834 the middle of a stmt */
837 { /* if not if for (;;) */
838 parse(semicolon
); /* let parser know about end of
840 force_nl
= true; /* force newline after a end of
845 case lbrace
: /* got a '{' */
846 ps
.in_stmt
= false; /* dont indent the {} */
848 force_nl
= true; /* force other stuff on same
849 line as '{' onto new line */
850 else if (ps
.block_init_level
<= 0)
851 ps
.block_init_level
= 1;
853 ps
.block_init_level
++;
855 if (s_code
!= e_code
&& !ps
.block_init
)
860 ps
.want_blank
= false;
861 } else if (ps
.in_par_decl
&& !ps
.in_or_st
)
865 ps
.want_blank
= false;
869 prefix_blankline_requested
= 0;
871 if (ps
.p_l_follow
> 0)
872 { /* check for preceeding
874 diag(1, "Unbalanced parens");
877 { /* check for unclosed if, for,
881 ps
.ind_level
= ps
.i_l_follow
;
884 if (s_code
== e_code
)
885 ps
.ind_stmt
= false; /* dont put extra indentation
887 if (ps
.in_decl
&& ps
.in_or_st
)
888 { /* this is either a structure
889 declaration or an init */
890 di_stack
[ps
.dec_nest
++] = dec_ind
;
894 ps
.decl_on_line
= false; /* we cant be in the middle of
895 a declaration, so dont do
896 special indentation of
900 postfix_blankline_requested
= 1;
904 parse(lbrace
); /* let parser know about this */
905 if (ps
.want_blank
) /* put a blank before '{' if
906 '{' is not at start of line */
908 ps
.want_blank
= false;
910 ps
.just_saw_decl
= 0;
913 case rbrace
: /* got a '}' */
914 if (ps
.p_stack
[ps
.tos
] == decl
&& !ps
.block_init
) /* semicolons can be
919 { /* check for unclosed if, for,
921 diag(1, "Unbalanced parens");
925 ps
.just_saw_decl
= 0;
926 ps
.block_init_level
--;
927 if (s_code
!= e_code
&& !ps
.block_init
)
928 { /* '}' must be first on line */
930 diag(0, "Line broken");
934 ps
.want_blank
= true;
935 ps
.in_stmt
= ps
.ind_stmt
= false;
937 { /* we are in multi-level
938 structure declaration */
939 dec_ind
= di_stack
[--ps
.dec_nest
];
940 if (ps
.dec_nest
== 0 && !ps
.in_par_decl
)
941 ps
.just_saw_decl
= 2;
944 prefix_blankline_requested
= 0;
945 parse(rbrace
); /* let parser know about this */
946 ps
.search_brace
= cuddle_else
&& ps
.p_stack
[ps
.tos
] == ifhead
947 && ps
.il
[ps
.tos
] >= ps
.ind_level
;
948 if (ps
.tos
<= 1 && bl_a_procs
&& ps
.dec_nest
<= 0)
949 postfix_blankline_requested
= 1;
952 case swstmt
: /* got keyword "switch" */
954 hd_type
= swstmt
; /* keep this for when we have
955 seen the expression */
956 goto copy_id
; /* go move the token into
959 case sp_paren
: /* token is if, while, for */
960 sp_sw
= true; /* the interesting stuff is
961 done after the expression is
963 hd_type
= (*token
== 'i' ? ifstmt
:
964 (*token
== 'w' ? whilestmt
: forstmt
));
966 /* remember the type of header for later use by parser */
967 goto copy_id
; /* copy the token into line */
969 case sp_nparen
: /* got else, do */
973 if (e_code
!= s_code
&& (!cuddle_else
|| e_code
[-1] != '}'))
976 diag(0, "Line broken");
977 dump_line(); /* make sure this starts a line */
978 ps
.want_blank
= false;
980 force_nl
= true; /* also, following stuff must
986 if (e_code
!= s_code
)
987 { /* make sure this starts a line */
989 diag(0, "Line broken");
991 ps
.want_blank
= false;
993 force_nl
= true; /* also, following stuff must
998 goto copy_id
; /* move the token into line */
1000 case decl
: /* we have a declaration type
1001 (int, register, etc.) */
1002 parse(decl
); /* let parser worry about
1004 if (ps
.last_token
== rparen
&& ps
.tos
<= 1)
1007 if (s_code
!= e_code
)
1013 if (ps
.in_par_decl
&& ps
.indent_parameters
&& ps
.dec_nest
== 0)
1015 ps
.ind_level
= ps
.i_l_follow
= 1;
1018 ps
.in_or_st
= true; /* this might be a structure or
1019 initialization declaration */
1020 ps
.in_decl
= ps
.decl_on_line
= true;
1021 if ( /* !ps.in_or_st && */ ps
.dec_nest
<= 0)
1022 ps
.just_saw_decl
= 2;
1023 prefix_blankline_requested
= 0;
1024 for (i
= 0; token
[i
++];); /* get length of token */
1026 /* dec_ind = e_code - s_code + (ps.decl_indent>i ?
1027 ps.decl_indent : i); */
1028 dec_ind
= ps
.decl_indent
> 0 ? ps
.decl_indent
: i
;
1031 case ident
: /* got an identifier or
1034 { /* if we are in a declaration,
1035 we must indent identifier */
1038 ps
.want_blank
= false;
1039 if (is_procname
== 0 || !proc_str_line
)
1042 if (troff
&& !ps
.dumped_decl_indent
)
1044 sprintf(e_code
, "\n.De %dp+\200p\n", dec_ind
* 7);
1045 ps
.dumped_decl_indent
= 1;
1046 e_code
+= strlen(e_code
);
1048 while ((e_code
- s_code
) < dec_ind
)
1050 if (e_code
>= l_code
)
1052 register nsize
= l_code
- s_code
+ 400;
1053 codebuf
= (char *) realloc(codebuf
, nsize
);
1054 e_code
= codebuf
+ (e_code
- s_code
) + 1;
1055 l_code
= codebuf
+ nsize
- 5;
1056 s_code
= codebuf
+ 1;
1062 if (dec_ind
&& s_code
!= e_code
)
1065 ps
.want_blank
= false;
1067 } else if (sp_sw
&& ps
.p_l_follow
== 0)
1078 if (troff
&& ps
.its_a_keyword
)
1080 e_code
= chfont(&bodyf
, &keywordf
, e_code
);
1081 for (t_ptr
= token
; *t_ptr
; ++t_ptr
)
1083 if (e_code
>= l_code
)
1085 register nsize
= l_code
- s_code
+ 400;
1086 codebuf
= (char *) realloc(codebuf
, nsize
);
1087 e_code
= codebuf
+ (e_code
- s_code
) + 1;
1088 l_code
= codebuf
+ nsize
- 5;
1089 s_code
= codebuf
+ 1;
1091 *e_code
++ = keywordf
.allcaps
&& islower(*t_ptr
)
1092 ? toupper(*t_ptr
) : *t_ptr
;
1094 e_code
= chfont(&keywordf
, &bodyf
, e_code
);
1096 for (t_ptr
= token
; *t_ptr
; ++t_ptr
)
1098 if (e_code
>= l_code
)
1100 register nsize
= l_code
- s_code
+ 400;
1101 codebuf
= (char *) realloc(codebuf
, nsize
);
1102 e_code
= codebuf
+ (e_code
- s_code
) + 1;
1103 l_code
= codebuf
+ nsize
- 5;
1104 s_code
= codebuf
+ 1;
1108 ps
.want_blank
= true;
1111 case period
: /* treat a period kind of like
1112 a binary operation */
1113 *e_code
++ = '.'; /* move the period into line */
1114 ps
.want_blank
= false; /* dont put a blank after a
1119 ps
.want_blank
= (s_code
!= e_code
); /* only put blank after
1121 not start the line */
1122 if (ps
.in_decl
&& is_procname
== 0 && !ps
.block_init
)
1123 while ((e_code
- s_code
) < (dec_ind
- 1))
1125 if (e_code
>= l_code
)
1127 register nsize
= l_code
- s_code
+ 400;
1128 codebuf
= (char *) realloc(codebuf
, nsize
);
1129 e_code
= codebuf
+ (e_code
- s_code
) + 1;
1130 l_code
= codebuf
+ nsize
- 5;
1131 s_code
= codebuf
+ 1;
1137 if (ps
.p_l_follow
== 0)
1139 if (ps
.block_init_level
<= 0)
1141 if (break_comma
&& !ps
.leave_comma
)
1146 case preesc
: /* got the character '#' */
1147 if ((s_com
!= e_com
) ||
1151 *e_lab
++ = '#'; /* move whole line to 'label'
1159 while (*buf_ptr
!= '\n' || in_comment
)
1163 register nsize
= l_lab
- s_lab
+ 400;
1164 labbuf
= (char *) realloc(labbuf
, nsize
);
1165 e_lab
= labbuf
+ (e_lab
- s_lab
) + 1;
1166 l_lab
= labbuf
+ nsize
- 5;
1169 *e_lab
= *buf_ptr
++;
1170 if (buf_ptr
>= buf_end
)
1176 *e_lab
++ = BACKSLASH
;
1179 *e_lab
++ = *buf_ptr
++;
1180 if (buf_ptr
>= buf_end
)
1185 if (*buf_ptr
== '*' && !in_comment
&& !quote
)
1188 *e_lab
++ = *buf_ptr
++;
1189 com_start
= e_lab
- s_lab
- 2;
1201 if (*buf_ptr
== '/' && in_comment
)
1204 *e_lab
++ = *buf_ptr
++;
1205 com_end
= e_lab
- s_lab
;
1211 while (e_lab
> s_lab
&& (e_lab
[-1] == ' ' || e_lab
[-1] == '\t'))
1213 if (e_lab
- s_lab
== com_end
&& bp_save
== 0)
1214 { /* comment on preprocessor line */
1215 if (sc_end
== 0) /* if this is the first
1216 comment, we must set up the
1218 sc_end
= &(save_com
[0]);
1221 *sc_end
++ = '\n'; /* add newline between comments */
1225 memcpy(sc_end
, s_lab
+ com_start
, com_end
- com_start
);
1226 sc_end
+= com_end
- com_start
;
1227 if (sc_end
>= &save_com
[sc_size
])
1229 e_lab
= s_lab
+ com_start
;
1230 while (e_lab
> s_lab
&& (e_lab
[-1] == ' ' || e_lab
[-1] == '\t'))
1232 bp_save
= buf_ptr
; /* save current input buffer */
1234 buf_ptr
= save_com
; /* fix so that subsequent calls
1235 to lexi will take tokens out
1237 *sc_end
++ = ' '; /* add trailing blank, just in
1242 *e_lab
= '\0'; /* null terminate line */
1246 if (strncmp(s_lab
, "#if", 3) == 0)
1251 prefix_blankline_requested
++;
1252 while ((c
= getc(input
)) == '\n');
1255 if (ifdef_level
< sizeof state_stack
/ sizeof state_stack
[0])
1257 match_state
[ifdef_level
].tos
= -1;
1258 state_stack
[ifdef_level
++] = ps
;
1260 diag(1, "#if stack overflow");
1261 } else if (strncmp(s_lab
, "#else", 5) == 0)
1262 if (ifdef_level
<= 0)
1263 diag(1, "Unmatched #else");
1266 match_state
[ifdef_level
- 1] = ps
;
1267 ps
= state_stack
[ifdef_level
- 1];
1269 else if (strncmp(s_lab
, "#endif", 6) == 0)
1271 if (ifdef_level
<= 0)
1272 diag(1, "Unmatched #endif");
1278 /* This match needs to be more intelligent before the
1279 message is useful */
1280 if (match_state
[ifdef_level
].tos
>= 0
1281 && bcmp(&ps
, &match_state
[ifdef_level
], sizeof ps
))
1282 diag(0, "Syntactically inconsistant #ifdef alternatives.");
1287 postfix_blankline_requested
++;
1288 n_real_blanklines
= 0;
1291 break; /* subsequent processing of the
1292 newline character will cause
1293 the line to be printed */
1295 case comment
: /* we have gotten a / * this is
1298 { /* we should force a broken
1302 ps
.want_blank
= false; /* dont insert blank at line
1308 } /* end of big switch stmt */
1310 *e_code
= '\0'; /* make sure code section is
1312 if (type_code
!= comment
&& type_code
!= newline
&& type_code
!= preesc
)
1313 ps
.last_token
= type_code
;
1314 } /* end of main while (1) loop */
1318 * copy input file to backup file if in_name is /blah/blah/blah/file, then
1319 * backup file will be ".Bfile" then make the backup file the input and
1320 * original input file the output
1325 char buff
[8 * 1024];
1328 /* construct file name .Bfile */
1329 for (p
= in_name
; *p
; p
++); /* skip to end of string */
1330 while (p
> in_name
&& *p
!= '/') /* find last '/' */
1334 sprintf(bakfile
, "%s.BAK", p
);
1335 if (strlen(p
) >= NAME_SIZE
) *bakfile
^= 040; /* toggle char */
1337 /* copy in_name to backup file */
1338 bakchn
= creat(bakfile
, 0600);
1341 fprintf(stderr
, "indent: can't create backup file \"%s\"\n", bakfile
);
1344 while (n
= read(fileno(input
), buff
, sizeof buff
))
1345 if (write(bakchn
, buff
, n
) != n
)
1347 fprintf(stderr
, "indent: error writing backup file \"%s\"\n",
1353 fprintf(stderr
, "indent: error reading input file \"%s\"\n", in_name
);
1359 /* re-open backup file as the input file */
1360 input
= fopen(bakfile
, "r");
1363 fprintf(stderr
, "indent: can't re-open backup file\n");
1366 /* now the original input file will be the output */
1367 output
= fopen(in_name
, "w");
1370 fprintf(stderr
, "indent: can't create %s\n", in_name
);