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