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