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