121975Sdist /*
2*35500Sbostic  * Copyright (c) 1985 Sun Microsystems, Inc.
3*35500Sbostic  * Copyright (c) 1980 The Regents of the University of California.
433767Sbostic  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
533767Sbostic  * All rights reserved.
621975Sdist  *
733767Sbostic  * Redistribution and use in source and binary forms are permitted
834885Sbostic  * provided that the above copyright notice and this paragraph are
934885Sbostic  * duplicated in all such forms and that any documentation,
1034885Sbostic  * advertising materials, and other materials related to such
1134885Sbostic  * distribution and use acknowledge that the software was developed
12*35500Sbostic  * by the University of California, Berkeley, the University of Illinois,
13*35500Sbostic  * Urbana, and Sun Microsystems, Inc.  The name of either University
14*35500Sbostic  * or Sun Microsystems may not be used to endorse or promote products
15*35500Sbostic  * derived from this software without specific prior written permission.
1634885Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1734885Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1834885Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1933767Sbostic  *
20*35500Sbostic  *	@(#)indent_globs.h	5.7 (Berkeley) 09/15/88
2121975Sdist  */
228802Smckusick 
2324454Smckusick #include <stdio.h>
2424454Smckusick 
2524454Smckusick #define BACKSLASH '\\'
26*35500Sbostic #define bufsize 200		/* size of internal buffers */
27*35500Sbostic #define inp_bufs 600		/* size of input buffer */
28*35500Sbostic #define sc_size 5000		/* size of save_com buffer */
29*35500Sbostic #define label_offset 2		/* number of levels a label is placed to left
30*35500Sbostic 				 * of code */
318802Smckusick 
32*35500Sbostic #define tabsize 8		/* the size of a tab */
33*35500Sbostic #define tabmask 0177770		/* mask used when figuring length of lines
34*35500Sbostic 				 * with tabs */
358802Smckusick 
368802Smckusick 
378802Smckusick #define false 0
388802Smckusick #define true  1
398802Smckusick 
408802Smckusick 
41*35500Sbostic FILE       *input;		/* the fid for the input file */
42*35500Sbostic FILE       *output;		/* the output file */
438802Smckusick 
44*35500Sbostic #define check_size(name) \
45*35500Sbostic 	if (e_/**/name >= l_/**/name) { \
46*35500Sbostic 	    register nsize = l_/**/name-s_/**/name+400; \
47*35500Sbostic 	    name/**/buf = (char *) realloc(name/**/buf, nsize); \
48*35500Sbostic 	    e_/**/name = name/**/buf + (e_/**/name-s_/**/name) + 1; \
49*35500Sbostic 	    l_/**/name = name/**/buf + nsize - 5; \
50*35500Sbostic 	    s_/**/name = name/**/buf + 1; \
51*35500Sbostic 	}
528802Smckusick 
53*35500Sbostic char       *labbuf;		/* buffer for label */
54*35500Sbostic char       *s_lab;		/* start ... */
55*35500Sbostic char       *e_lab;		/* .. and end of stored label */
56*35500Sbostic char       *l_lab;		/* limit of label buffer */
578802Smckusick 
58*35500Sbostic char       *codebuf;		/* buffer for code section */
59*35500Sbostic char       *s_code;		/* start ... */
60*35500Sbostic char       *e_code;		/* .. and end of stored code */
61*35500Sbostic char       *l_code;		/* limit of code section */
628802Smckusick 
63*35500Sbostic char       *combuf;		/* buffer for comments */
64*35500Sbostic char       *s_com;		/* start ... */
65*35500Sbostic char       *e_com;		/* ... and end of stored comments */
66*35500Sbostic char       *l_com;		/* limit of comment buffer */
678802Smckusick 
68*35500Sbostic char        in_buffer[inp_bufs];/* input buffer */
69*35500Sbostic char       *buf_ptr;		/* ptr to next character to be taken from
70*35500Sbostic 				 * in_buffer */
71*35500Sbostic char       *buf_end;		/* ptr to first after last char in in_buffer */
728802Smckusick 
73*35500Sbostic char        save_com[sc_size];	/* input text is saved here when looking for
74*35500Sbostic 				 * the brace after an if, while, etc */
75*35500Sbostic char       *sc_end;		/* pointer into save_com buffer */
768802Smckusick 
77*35500Sbostic char       *bp_save;		/* saved value of buf_ptr when taking input
78*35500Sbostic 				 * from save_com */
79*35500Sbostic char       *be_save;		/* similarly saved value of buf_end */
808802Smckusick 
81*35500Sbostic char        token[bufsize];	/* the last token scanned */
828802Smckusick 
838802Smckusick 
84*35500Sbostic int         pointer_as_binop;
85*35500Sbostic int         blanklines_after_declarations;
86*35500Sbostic int         blanklines_before_blockcomments;
87*35500Sbostic int         blanklines_after_procs;
88*35500Sbostic int         blanklines_around_conditional_compilation;
89*35500Sbostic int         swallow_optional_blanklines;
90*35500Sbostic int         n_real_blanklines;
91*35500Sbostic int         prefix_blankline_requested;
92*35500Sbostic int         postfix_blankline_requested;
93*35500Sbostic int         break_comma;	/* when true and not in parens, break after a
94*35500Sbostic 				 * comma */
95*35500Sbostic int         btype_2;		/* when true, brace should be on same line as
96*35500Sbostic 				 * if, while, etc */
97*35500Sbostic float       case_ind;		/* indentation level to be used for a "case
98*35500Sbostic 				 * n:" */
99*35500Sbostic int         code_lines;		/* count of lines with code */
100*35500Sbostic int         had_eof;		/* set to true when input is exhausted */
101*35500Sbostic int         line_no;		/* the current line number. */
102*35500Sbostic int         max_col;		/* the maximum allowable line length */
103*35500Sbostic int         verbose;		/* when true, non-essential error messages are
104*35500Sbostic 				 * printed */
105*35500Sbostic int         cuddle_else;	/* true if else should cuddle up to '}' */
106*35500Sbostic int         star_comment_cont;	/* true iff comment continuation lines should
107*35500Sbostic 				 * have stars at the beginning of each line. */
108*35500Sbostic int         comment_delimiter_on_blankline;
109*35500Sbostic int         troff;		/* true iff were generating troff input */
110*35500Sbostic int         procnames_start_line;	/* if true, the names of procedures
111*35500Sbostic 					 * being defined get placed in column
112*35500Sbostic 					 * 1 (ie. a newline is placed between
113*35500Sbostic 					 * the type of the procedure and its
114*35500Sbostic 					 * name) */
115*35500Sbostic int         proc_calls_space;	/* If true, procedure calls look like:
116*35500Sbostic 				 * foo(bar) rather than foo (bar) */
117*35500Sbostic int         format_col1_comments;	/* If comments which start in column 1
118*35500Sbostic 					 * are to be magically reformatted
119*35500Sbostic 					 * (just like comments that begin in
120*35500Sbostic 					 * later columns) */
121*35500Sbostic int         inhibit_formatting;	/* true if INDENT OFF is in effect */
122*35500Sbostic int         suppress_blanklines;/* set iff following blanklines should be
123*35500Sbostic 				 * suppressed */
124*35500Sbostic int         continuation_indent;/* set to the indentation between the edge of
125*35500Sbostic 				 * code and continuation lines */
126*35500Sbostic int         lineup_to_parens;	/* if true, continued code within parens will
127*35500Sbostic 				 * be lined up to the open paren */
128*35500Sbostic int         Bill_Shannon;	/* true iff a blank should always be inserted
129*35500Sbostic 				 * after sizeof */
130*35500Sbostic int         blanklines_after_declarations_at_proctop;	/* This is vaguely
131*35500Sbostic 							 * similar to
132*35500Sbostic 							 * blanklines_after_decla
133*35500Sbostic 							 * rations except that
134*35500Sbostic 							 * it only applies to
135*35500Sbostic 							 * the first set of
136*35500Sbostic 							 * declarations in a
137*35500Sbostic 							 * procedure (just after
138*35500Sbostic 							 * the first '{') and it
139*35500Sbostic 							 * causes a blank line
140*35500Sbostic 							 * to be generated even
141*35500Sbostic 							 * if there are no
142*35500Sbostic 							 * declarations */
143*35500Sbostic int         block_comment_max_col;
144*35500Sbostic int         extra_expression_indent;	/* True if continuation lines from the
145*35500Sbostic 					 * expression part of "if(e)",
146*35500Sbostic 					 * "while(e)", "for(e;e;e)" should be
147*35500Sbostic 					 * indented an extra tab stop so that
148*35500Sbostic 					 * they don't conflict with the code
149*35500Sbostic 					 * that follows */
15024454Smckusick 
151*35500Sbostic /* -troff font state information */
15224454Smckusick 
153*35500Sbostic struct fstate {
154*35500Sbostic     char        font[4];
155*35500Sbostic     char        size;
156*35500Sbostic     int         allcaps:1;
157*35500Sbostic };
158*35500Sbostic char       *chfont();
159*35500Sbostic 
160*35500Sbostic struct fstate
161*35500Sbostic             keywordf,		/* keyword font */
162*35500Sbostic             stringf,		/* string font */
163*35500Sbostic             boxcomf,		/* Box comment font */
164*35500Sbostic             blkcomf,		/* Block comment font */
165*35500Sbostic             scomf,		/* Same line comment font */
166*35500Sbostic             bodyf;		/* major body font */
167*35500Sbostic 
168*35500Sbostic 
169*35500Sbostic #define STACKSIZE 150
170*35500Sbostic 
17124454Smckusick struct parser_state {
17224454Smckusick     int         last_token;
173*35500Sbostic     struct fstate cfont;	/* Current font */
174*35500Sbostic     int         p_stack[STACKSIZE];	/* this is the parsers stack */
175*35500Sbostic     int         il[STACKSIZE];	/* this stack stores indentation levels */
176*35500Sbostic     float       cstk[STACKSIZE];/* used to store case stmt indentation levels */
17724454Smckusick     int         box_com;	/* set to true when we are in a "boxed"
178*35500Sbostic 				 * comment. In that case, the first non-blank
179*35500Sbostic 				 * char should be lined up with the / in /* */
18024454Smckusick     int         comment_delta,
18124454Smckusick                 n_comment_delta;
18224454Smckusick     int         cast_mask;	/* indicates which close parens close off
18324454Smckusick 				 * casts */
184*35500Sbostic     int         sizeof_mask;	/* indicates which close parens close off
185*35500Sbostic 				 * sizeof''s */
18624454Smckusick     int         block_init;	/* true iff inside a block initialization */
187*35500Sbostic     int         block_init_level;	/* The level of brace nesting in an
188*35500Sbostic 					 * initialization */
189*35500Sbostic     int         last_nl;	/* this is true if the last thing scanned was
190*35500Sbostic 				 * a newline */
19124454Smckusick     int         in_or_st;	/* Will be true iff there has been a
192*35500Sbostic 				 * declarator (e.g. int or char) and no left
193*35500Sbostic 				 * paren since the last semicolon. When true,
194*35500Sbostic 				 * a '{' is starting a structure definition or
195*35500Sbostic 				 * an initialization list */
196*35500Sbostic     int         bl_line;	/* set to 1 by dump_line if the line is blank */
197*35500Sbostic     int         col_1;		/* set to true if the last token started in
198*35500Sbostic 				 * column 1 */
19924454Smckusick     int         com_col;	/* this is the column in which the current
20024454Smckusick 				 * coment should start */
201*35500Sbostic     int         com_ind;	/* the column in which comments to the right
202*35500Sbostic 				 * of code should start */
203*35500Sbostic     int         com_lines;	/* the number of lines with comments, set by
204*35500Sbostic 				 * dump_line */
205*35500Sbostic     int         dec_nest;	/* current nesting level for structure or init */
20624454Smckusick     int         decl_com_ind;	/* the column in which comments after
20724454Smckusick 				 * declarations should be put */
208*35500Sbostic     int         decl_on_line;	/* set to true if this line of code has part
209*35500Sbostic 				 * of a declaration on it */
210*35500Sbostic     int         i_l_follow;	/* the level to which ind_level should be set
211*35500Sbostic 				 * after the current line is printed */
212*35500Sbostic     int         in_decl;	/* set to true when we are in a declaration
213*35500Sbostic 				 * stmt.  The processing of braces is then
214*35500Sbostic 				 * slightly different */
21524454Smckusick     int         in_stmt;	/* set to 1 while in a stmt */
21624454Smckusick     int         ind_level;	/* the current indentation level */
21724454Smckusick     int         ind_size;	/* the size of one indentation level */
218*35500Sbostic     int         ind_stmt;	/* set to 1 if next line should have an extra
219*35500Sbostic 				 * indentation level because we are in the
220*35500Sbostic 				 * middle of a stmt */
221*35500Sbostic     int         last_u_d;	/* set to true after scanning a token which
222*35500Sbostic 				 * forces a following operator to be unary */
22324454Smckusick     int         leave_comma;	/* if true, never break declarations after
22424454Smckusick 				 * commas */
22524454Smckusick     int         ljust_decl;	/* true if declarations should be left
22624454Smckusick 				 * justified */
227*35500Sbostic     int         out_coms;	/* the number of comments processed, set by
228*35500Sbostic 				 * pr_comment */
22924454Smckusick     int         out_lines;	/* the number of lines written, set by
23024454Smckusick 				 * dump_line */
231*35500Sbostic     int         p_l_follow;	/* used to remember how to indent following
232*35500Sbostic 				 * statement */
23324454Smckusick     int         paren_level;	/* parenthesization level. used to indent
23424454Smckusick 				 * within stmts */
23524454Smckusick     short       paren_indents[20];	/* column positions of each paren */
23624454Smckusick     int         pcase;		/* set to 1 if the current line label is a
237*35500Sbostic 				 * case.  It is printed differently from a
238*35500Sbostic 				 * regular label */
239*35500Sbostic     int         search_brace;	/* set to true by parse when it is necessary
240*35500Sbostic 				 * to buffer up all info up to the start of a
241*35500Sbostic 				 * stmt after an if, while, etc */
242*35500Sbostic     int         unindent_displace;	/* comments not to the right of code
243*35500Sbostic 					 * will be placed this many
244*35500Sbostic 					 * indentation levels to the left of
245*35500Sbostic 					 * code */
246*35500Sbostic     int         use_ff;		/* set to one if the current line should be
247*35500Sbostic 				 * terminated with a form feed */
248*35500Sbostic     int         want_blank;	/* set to true when the following token should
249*35500Sbostic 				 * be prefixed by a blank. (Said prefixing is
250*35500Sbostic 				 * ignored in some cases.) */
251*35500Sbostic     int         else_if;	/* True iff else if pairs should be handled
252*35500Sbostic 				 * specially */
253*35500Sbostic     int         decl_indent;	/* column to indent declared identifiers to */
25424454Smckusick     int         its_a_keyword;
255*35500Sbostic     int         sizeof_keyword;
25624454Smckusick     int         dumped_decl_indent;
257*35500Sbostic     float       case_indent;	/* The distance to indent case labels from the
258*35500Sbostic 				 * switch statement */
25924454Smckusick     int         in_parameter_declaration;
26024454Smckusick     int         indent_parameters;
26124454Smckusick     int         tos;		/* pointer to top of stack */
26224454Smckusick     char        procname[100];	/* The name of the current procedure */
263*35500Sbostic     int         just_saw_decl;
26424454Smckusick }           ps;
26524454Smckusick 
266*35500Sbostic int         ifdef_level;
26724454Smckusick struct parser_state state_stack[5];
26824454Smckusick struct parser_state match_state[5];
269