1 /*
2  * Copyright (c) 1985 Sun Microsystems, Inc.
3  * Copyright (c) 1980, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  * All rights reserved.
6  *
7  * %sccs.include.redist.c%
8  *
9  *	@(#)indent_globs.h	8.1 (Berkeley) 06/06/93
10  */
11 
12 #define BACKSLASH '\\'
13 #define bufsize 200		/* size of internal buffers */
14 #define sc_size 5000		/* size of save_com buffer */
15 #define label_offset 2		/* number of levels a label is placed to left
16 				 * of code */
17 
18 #define tabsize 8		/* the size of a tab */
19 #define tabmask 0177770		/* mask used when figuring length of lines
20 				 * with tabs */
21 
22 
23 #define false 0
24 #define true  1
25 
26 
27 FILE       *input;		/* the fid for the input file */
28 FILE       *output;		/* the output file */
29 
30 #define CHECK_SIZE_CODE \
31 	if (e_code >= l_code) { \
32 	    register nsize = l_code-s_code+400; \
33 	    codebuf = (char *) realloc(codebuf, nsize); \
34 	    e_code = codebuf + (e_code-s_code) + 1; \
35 	    l_code = codebuf + nsize - 5; \
36 	    s_code = codebuf + 1; \
37 	}
38 #define CHECK_SIZE_COM \
39 	if (e_com >= l_com) { \
40 	    register nsize = l_com-s_com+400; \
41 	    combuf = (char *) realloc(combuf, nsize); \
42 	    e_com = combuf + (e_com-s_com) + 1; \
43 	    l_com = combuf + nsize - 5; \
44 	    s_com = combuf + 1; \
45 	}
46 #define CHECK_SIZE_LAB \
47 	if (e_lab >= l_lab) { \
48 	    register nsize = l_lab-s_lab+400; \
49 	    labbuf = (char *) realloc(labbuf, nsize); \
50 	    e_lab = labbuf + (e_lab-s_lab) + 1; \
51 	    l_lab = labbuf + nsize - 5; \
52 	    s_lab = labbuf + 1; \
53 	}
54 #define CHECK_SIZE_TOKEN \
55 	if (e_token >= l_token) { \
56 	    register nsize = l_token-s_token+400; \
57 	    tokenbuf = (char *) realloc(tokenbuf, nsize); \
58 	    e_token = tokenbuf + (e_token-s_token) + 1; \
59 	    l_token = tokenbuf + nsize - 5; \
60 	    s_token = tokenbuf + 1; \
61 	}
62 
63 char       *labbuf;		/* buffer for label */
64 char       *s_lab;		/* start ... */
65 char       *e_lab;		/* .. and end of stored label */
66 char       *l_lab;		/* limit of label buffer */
67 
68 char       *codebuf;		/* buffer for code section */
69 char       *s_code;		/* start ... */
70 char       *e_code;		/* .. and end of stored code */
71 char       *l_code;		/* limit of code section */
72 
73 char       *combuf;		/* buffer for comments */
74 char       *s_com;		/* start ... */
75 char       *e_com;		/* ... and end of stored comments */
76 char       *l_com;		/* limit of comment buffer */
77 
78 #define token s_token
79 char       *tokenbuf;		/* the last token scanned */
80 char	   *s_token;
81 char       *e_token;
82 char	   *l_token;
83 
84 char       *in_buffer;		/* input buffer */
85 char	   *in_buffer_limit;	/* the end of the input buffer */
86 char       *buf_ptr;		/* ptr to next character to be taken from
87 				 * in_buffer */
88 char       *buf_end;		/* ptr to first after last char in in_buffer */
89 
90 char        save_com[sc_size];	/* input text is saved here when looking for
91 				 * the brace after an if, while, etc */
92 char       *sc_end;		/* pointer into save_com buffer */
93 
94 char       *bp_save;		/* saved value of buf_ptr when taking input
95 				 * from save_com */
96 char       *be_save;		/* similarly saved value of buf_end */
97 
98 
99 int         pointer_as_binop;
100 int         blanklines_after_declarations;
101 int         blanklines_before_blockcomments;
102 int         blanklines_after_procs;
103 int         blanklines_around_conditional_compilation;
104 int         swallow_optional_blanklines;
105 int         n_real_blanklines;
106 int         prefix_blankline_requested;
107 int         postfix_blankline_requested;
108 int         break_comma;	/* when true and not in parens, break after a
109 				 * comma */
110 int         btype_2;		/* when true, brace should be on same line as
111 				 * if, while, etc */
112 float       case_ind;		/* indentation level to be used for a "case
113 				 * n:" */
114 int         code_lines;		/* count of lines with code */
115 int         had_eof;		/* set to true when input is exhausted */
116 int         line_no;		/* the current line number. */
117 int         max_col;		/* the maximum allowable line length */
118 int         verbose;		/* when true, non-essential error messages are
119 				 * printed */
120 int         cuddle_else;	/* true if else should cuddle up to '}' */
121 int         star_comment_cont;	/* true iff comment continuation lines should
122 				 * have stars at the beginning of each line. */
123 int         comment_delimiter_on_blankline;
124 int         troff;		/* true iff were generating troff input */
125 int         procnames_start_line;	/* if true, the names of procedures
126 					 * being defined get placed in column
127 					 * 1 (ie. a newline is placed between
128 					 * the type of the procedure and its
129 					 * name) */
130 int         proc_calls_space;	/* If true, procedure calls look like:
131 				 * foo(bar) rather than foo (bar) */
132 int         format_col1_comments;	/* If comments which start in column 1
133 					 * are to be magically reformatted
134 					 * (just like comments that begin in
135 					 * later columns) */
136 int         inhibit_formatting;	/* true if INDENT OFF is in effect */
137 int         suppress_blanklines;/* set iff following blanklines should be
138 				 * suppressed */
139 int         continuation_indent;/* set to the indentation between the edge of
140 				 * code and continuation lines */
141 int         lineup_to_parens;	/* if true, continued code within parens will
142 				 * be lined up to the open paren */
143 int         Bill_Shannon;	/* true iff a blank should always be inserted
144 				 * after sizeof */
145 int         blanklines_after_declarations_at_proctop;	/* This is vaguely
146 							 * similar to
147 							 * blanklines_after_decla
148 							 * rations except that
149 							 * it only applies to
150 							 * the first set of
151 							 * declarations in a
152 							 * procedure (just after
153 							 * the first '{') and it
154 							 * causes a blank line
155 							 * to be generated even
156 							 * if there are no
157 							 * declarations */
158 int         block_comment_max_col;
159 int         extra_expression_indent;	/* True if continuation lines from the
160 					 * expression part of "if(e)",
161 					 * "while(e)", "for(e;e;e)" should be
162 					 * indented an extra tab stop so that
163 					 * they don't conflict with the code
164 					 * that follows */
165 
166 /* -troff font state information */
167 
168 struct fstate {
169     char        font[4];
170     char        size;
171     int         allcaps:1;
172 };
173 char       *chfont();
174 
175 struct fstate
176             keywordf,		/* keyword font */
177             stringf,		/* string font */
178             boxcomf,		/* Box comment font */
179             blkcomf,		/* Block comment font */
180             scomf,		/* Same line comment font */
181             bodyf;		/* major body font */
182 
183 
184 #define STACKSIZE 150
185 
186 struct parser_state {
187     int         last_token;
188     struct fstate cfont;	/* Current font */
189     int         p_stack[STACKSIZE];	/* this is the parsers stack */
190     int         il[STACKSIZE];	/* this stack stores indentation levels */
191     float       cstk[STACKSIZE];/* used to store case stmt indentation levels */
192     int         box_com;	/* set to true when we are in a "boxed"
193 				 * comment. In that case, the first non-blank
194 				 * char should be lined up with the / in /* */
195     int         comment_delta,
196                 n_comment_delta;
197     int         cast_mask;	/* indicates which close parens close off
198 				 * casts */
199     int         sizeof_mask;	/* indicates which close parens close off
200 				 * sizeof''s */
201     int         block_init;	/* true iff inside a block initialization */
202     int         block_init_level;	/* The level of brace nesting in an
203 					 * initialization */
204     int         last_nl;	/* this is true if the last thing scanned was
205 				 * a newline */
206     int         in_or_st;	/* Will be true iff there has been a
207 				 * declarator (e.g. int or char) and no left
208 				 * paren since the last semicolon. When true,
209 				 * a '{' is starting a structure definition or
210 				 * an initialization list */
211     int         bl_line;	/* set to 1 by dump_line if the line is blank */
212     int         col_1;		/* set to true if the last token started in
213 				 * column 1 */
214     int         com_col;	/* this is the column in which the current
215 				 * coment should start */
216     int         com_ind;	/* the column in which comments to the right
217 				 * of code should start */
218     int         com_lines;	/* the number of lines with comments, set by
219 				 * dump_line */
220     int         dec_nest;	/* current nesting level for structure or init */
221     int         decl_com_ind;	/* the column in which comments after
222 				 * declarations should be put */
223     int         decl_on_line;	/* set to true if this line of code has part
224 				 * of a declaration on it */
225     int         i_l_follow;	/* the level to which ind_level should be set
226 				 * after the current line is printed */
227     int         in_decl;	/* set to true when we are in a declaration
228 				 * stmt.  The processing of braces is then
229 				 * slightly different */
230     int         in_stmt;	/* set to 1 while in a stmt */
231     int         ind_level;	/* the current indentation level */
232     int         ind_size;	/* the size of one indentation level */
233     int         ind_stmt;	/* set to 1 if next line should have an extra
234 				 * indentation level because we are in the
235 				 * middle of a stmt */
236     int         last_u_d;	/* set to true after scanning a token which
237 				 * forces a following operator to be unary */
238     int         leave_comma;	/* if true, never break declarations after
239 				 * commas */
240     int         ljust_decl;	/* true if declarations should be left
241 				 * justified */
242     int         out_coms;	/* the number of comments processed, set by
243 				 * pr_comment */
244     int         out_lines;	/* the number of lines written, set by
245 				 * dump_line */
246     int         p_l_follow;	/* used to remember how to indent following
247 				 * statement */
248     int         paren_level;	/* parenthesization level. used to indent
249 				 * within stmts */
250     short       paren_indents[20];	/* column positions of each paren */
251     int         pcase;		/* set to 1 if the current line label is a
252 				 * case.  It is printed differently from a
253 				 * regular label */
254     int         search_brace;	/* set to true by parse when it is necessary
255 				 * to buffer up all info up to the start of a
256 				 * stmt after an if, while, etc */
257     int         unindent_displace;	/* comments not to the right of code
258 					 * will be placed this many
259 					 * indentation levels to the left of
260 					 * code */
261     int         use_ff;		/* set to one if the current line should be
262 				 * terminated with a form feed */
263     int         want_blank;	/* set to true when the following token should
264 				 * be prefixed by a blank. (Said prefixing is
265 				 * ignored in some cases.) */
266     int         else_if;	/* True iff else if pairs should be handled
267 				 * specially */
268     int         decl_indent;	/* column to indent declared identifiers to */
269     int         its_a_keyword;
270     int         sizeof_keyword;
271     int         dumped_decl_indent;
272     float       case_indent;	/* The distance to indent case labels from the
273 				 * switch statement */
274     int         in_parameter_declaration;
275     int         indent_parameters;
276     int         tos;		/* pointer to top of stack */
277     char        procname[100];	/* The name of the current procedure */
278     int         just_saw_decl;
279 }           ps;
280 
281 int         ifdef_level;
282 int	    rparen_count;
283 struct parser_state state_stack[5];
284 struct parser_state match_state[5];
285