xref: /freebsd/usr.bin/indent/indent_globs.h (revision cc16dea626cf2fc80cde667ac4798065108e596c)
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  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)indent_globs.h	8.1 (Berkeley) 6/6/93
36  * $FreeBSD$
37  */
38 
39 #define BACKSLASH '\\'
40 #define bufsize 200		/* size of internal buffers */
41 #define sc_size 5000		/* size of save_com buffer */
42 #define label_offset 2		/* number of levels a label is placed to left
43 				 * of code */
44 
45 #define tabsize 8		/* the size of a tab */
46 #define tabmask 0177770		/* mask used when figuring length of lines
47 				 * with tabs */
48 
49 
50 #define false 0
51 #define true  1
52 
53 
54 FILE       *input;		/* the fid for the input file */
55 FILE       *output;		/* the output file */
56 
57 #define CHECK_SIZE_CODE \
58 	if (e_code >= l_code) { \
59 	    int nsize = l_code-s_code+400; \
60 	    codebuf = (char *) realloc(codebuf, nsize); \
61 	    if (codebuf == NULL) \
62 		err(1, NULL); \
63 	    e_code = codebuf + (e_code-s_code) + 1; \
64 	    l_code = codebuf + nsize - 5; \
65 	    s_code = codebuf + 1; \
66 	}
67 #define CHECK_SIZE_COM \
68 	if (e_com >= l_com) { \
69 	    int nsize = l_com-s_com+400; \
70 	    combuf = (char *) realloc(combuf, nsize); \
71 	    if (combuf == NULL) \
72 		err(1, NULL); \
73 	    e_com = combuf + (e_com-s_com) + 1; \
74 	    l_com = combuf + nsize - 5; \
75 	    s_com = combuf + 1; \
76 	}
77 #define CHECK_SIZE_LAB \
78 	if (e_lab >= l_lab) { \
79 	    int nsize = l_lab-s_lab+400; \
80 	    labbuf = (char *) realloc(labbuf, nsize); \
81 	    if (labbuf == NULL) \
82 		err(1, NULL); \
83 	    e_lab = labbuf + (e_lab-s_lab) + 1; \
84 	    l_lab = labbuf + nsize - 5; \
85 	    s_lab = labbuf + 1; \
86 	}
87 #define CHECK_SIZE_TOKEN \
88 	if (e_token >= l_token) { \
89 	    int nsize = l_token-s_token+400; \
90 	    tokenbuf = (char *) realloc(tokenbuf, nsize); \
91 	    if (tokenbuf == NULL) \
92 		err(1, NULL); \
93 	    e_token = tokenbuf + (e_token-s_token) + 1; \
94 	    l_token = tokenbuf + nsize - 5; \
95 	    s_token = tokenbuf + 1; \
96 	}
97 
98 char       *labbuf;		/* buffer for label */
99 char       *s_lab;		/* start ... */
100 char       *e_lab;		/* .. and end of stored label */
101 char       *l_lab;		/* limit of label buffer */
102 
103 char       *codebuf;		/* buffer for code section */
104 char       *s_code;		/* start ... */
105 char       *e_code;		/* .. and end of stored code */
106 char       *l_code;		/* limit of code section */
107 
108 char       *combuf;		/* buffer for comments */
109 char       *s_com;		/* start ... */
110 char       *e_com;		/* ... and end of stored comments */
111 char       *l_com;		/* limit of comment buffer */
112 
113 #define token s_token
114 char       *tokenbuf;		/* the last token scanned */
115 char	   *s_token;
116 char       *e_token;
117 char	   *l_token;
118 
119 char       *in_buffer;		/* input buffer */
120 char	   *in_buffer_limit;	/* the end of the input buffer */
121 char       *buf_ptr;		/* ptr to next character to be taken from
122 				 * in_buffer */
123 char       *buf_end;		/* ptr to first after last char in in_buffer */
124 
125 char        save_com[sc_size];	/* input text is saved here when looking for
126 				 * the brace after an if, while, etc */
127 char       *sc_end;		/* pointer into save_com buffer */
128 
129 char       *bp_save;		/* saved value of buf_ptr when taking input
130 				 * from save_com */
131 char       *be_save;		/* similarly saved value of buf_end */
132 
133 
134 int         found_err;
135 int         pointer_as_binop;
136 int         blanklines_after_declarations;
137 int         blanklines_before_blockcomments;
138 int         blanklines_after_procs;
139 int         blanklines_around_conditional_compilation;
140 int         swallow_optional_blanklines;
141 int         n_real_blanklines;
142 int         prefix_blankline_requested;
143 int         postfix_blankline_requested;
144 int         break_comma;	/* when true and not in parens, break after a
145 				 * comma */
146 int         btype_2;		/* when true, brace should be on same line as
147 				 * if, while, etc */
148 float       case_ind;		/* indentation level to be used for a "case
149 				 * n:" */
150 int         code_lines;		/* count of lines with code */
151 int         had_eof;		/* set to true when input is exhausted */
152 int         line_no;		/* the current line number. */
153 int         max_col;		/* the maximum allowable line length */
154 int         verbose;		/* when true, non-essential error messages are
155 				 * printed */
156 int         cuddle_else;	/* true if else should cuddle up to '}' */
157 int         star_comment_cont;	/* true iff comment continuation lines should
158 				 * have stars at the beginning of each line. */
159 int         comment_delimiter_on_blankline;
160 int         troff;		/* true iff were generating troff input */
161 int         procnames_start_line;	/* if true, the names of procedures
162 					 * being defined get placed in column
163 					 * 1 (ie. a newline is placed between
164 					 * the type of the procedure and its
165 					 * name) */
166 int         proc_calls_space;	/* If true, procedure calls look like:
167 				 * foo(bar) rather than foo (bar) */
168 int         format_block_comments;	/* true if comments beginning with
169 					 * `/ * \n' are to be reformatted */
170 int         format_col1_comments;	/* If comments which start in column 1
171 					 * are to be magically reformatted
172 					 * (just like comments that begin in
173 					 * later columns) */
174 int         inhibit_formatting;	/* true if INDENT OFF is in effect */
175 int         suppress_blanklines;/* set iff following blanklines should be
176 				 * suppressed */
177 int         continuation_indent;/* set to the indentation between the edge of
178 				 * code and continuation lines */
179 int         lineup_to_parens;	/* if true, continued code within parens will
180 				 * be lined up to the open paren */
181 int         Bill_Shannon;	/* true iff a blank should always be inserted
182 				 * after sizeof */
183 int         blanklines_after_declarations_at_proctop;	/* This is vaguely
184 							 * similar to
185 							 * blanklines_after_decla
186 							 * rations except that
187 							 * it only applies to
188 							 * the first set of
189 							 * declarations in a
190 							 * procedure (just after
191 							 * the first '{') and it
192 							 * causes a blank line
193 							 * to be generated even
194 							 * if there are no
195 							 * declarations */
196 int         block_comment_max_col;
197 int         extra_expression_indent;	/* true if continuation lines from the
198 					 * expression part of "if(e)",
199 					 * "while(e)", "for(e;e;e)" should be
200 					 * indented an extra tab stop so that
201 					 * they don't conflict with the code
202 					 * that follows */
203 int	    function_brace_split;	/* split function declaration and
204 					 * brace onto separate lines */
205 int	    use_tabs;			/* set true to use tabs for spacing,
206 					 * false uses all spaces */
207 int	    auto_typedefs;		/* set true to recognize identifiers
208 					 * ending in "_t" like typedefs */
209 
210 /* -troff font state information */
211 
212 struct fstate {
213     char        font[4];
214     char        size;
215     int         allcaps:1;
216 } __aligned(sizeof(int));
217 char       *chfont(struct fstate *, struct fstate *, char *);
218 
219 struct fstate
220             keywordf,		/* keyword font */
221             stringf,		/* string font */
222             boxcomf,		/* Box comment font */
223             blkcomf,		/* Block comment font */
224             scomf,		/* Same line comment font */
225             bodyf;		/* major body font */
226 
227 
228 #define STACKSIZE 150
229 
230 struct parser_state {
231     int         last_token;
232     struct fstate cfont;	/* Current font */
233     int         p_stack[STACKSIZE];	/* this is the parsers stack */
234     int         il[STACKSIZE];	/* this stack stores indentation levels */
235     float       cstk[STACKSIZE];/* used to store case stmt indentation levels */
236     int         box_com;	/* set to true when we are in a "boxed"
237 				 * comment. In that case, the first non-blank
238 				 * char should be lined up with the / in / followed by * */
239     int         comment_delta,
240                 n_comment_delta;
241     int         cast_mask;	/* indicates which close parens close off
242 				 * casts */
243     int         sizeof_mask;	/* indicates which close parens close off
244 				 * sizeof''s */
245     int         block_init;	/* true iff inside a block initialization */
246     int         block_init_level;	/* The level of brace nesting in an
247 					 * initialization */
248     int         last_nl;	/* this is true if the last thing scanned was
249 				 * a newline */
250     int         in_or_st;	/* Will be true iff there has been a
251 				 * declarator (e.g. int or char) and no left
252 				 * paren since the last semicolon. When true,
253 				 * a '{' is starting a structure definition or
254 				 * an initialization list */
255     int         bl_line;	/* set to 1 by dump_line if the line is blank */
256     int         col_1;		/* set to true if the last token started in
257 				 * column 1 */
258     int         com_col;	/* this is the column in which the current
259 				 * comment should start */
260     int         com_ind;	/* the column in which comments to the right
261 				 * of code should start */
262     int         com_lines;	/* the number of lines with comments, set by
263 				 * dump_line */
264     int         dec_nest;	/* current nesting level for structure or init */
265     int         decl_com_ind;	/* the column in which comments after
266 				 * declarations should be put */
267     int         decl_on_line;	/* set to true if this line of code has part
268 				 * of a declaration on it */
269     int         i_l_follow;	/* the level to which ind_level should be set
270 				 * after the current line is printed */
271     int         in_decl;	/* set to true when we are in a declaration
272 				 * stmt.  The processing of braces is then
273 				 * slightly different */
274     int         in_stmt;	/* set to 1 while in a stmt */
275     int         ind_level;	/* the current indentation level */
276     int         ind_size;	/* the size of one indentation level */
277     int         ind_stmt;	/* set to 1 if next line should have an extra
278 				 * indentation level because we are in the
279 				 * middle of a stmt */
280     int         last_u_d;	/* set to true after scanning a token which
281 				 * forces a following operator to be unary */
282     int         leave_comma;	/* if true, never break declarations after
283 				 * commas */
284     int         ljust_decl;	/* true if declarations should be left
285 				 * justified */
286     int         out_coms;	/* the number of comments processed, set by
287 				 * pr_comment */
288     int         out_lines;	/* the number of lines written, set by
289 				 * dump_line */
290     int         p_l_follow;	/* used to remember how to indent following
291 				 * statement */
292     int         paren_level;	/* parenthesization level. used to indent
293 				 * within statements */
294     short       paren_indents[20];	/* column positions of each paren */
295     int         pcase;		/* set to 1 if the current line label is a
296 				 * case.  It is printed differently from a
297 				 * regular label */
298     int         search_brace;	/* set to true by parse when it is necessary
299 				 * to buffer up all info up to the start of a
300 				 * stmt after an if, while, etc */
301     int         unindent_displace;	/* comments not to the right of code
302 					 * will be placed this many
303 					 * indentation levels to the left of
304 					 * code */
305     int         use_ff;		/* set to one if the current line should be
306 				 * terminated with a form feed */
307     int         want_blank;	/* set to true when the following token should
308 				 * be prefixed by a blank. (Said prefixing is
309 				 * ignored in some cases.) */
310     int         else_if;	/* True iff else if pairs should be handled
311 				 * specially */
312     int         decl_indent;	/* column to indent declared identifiers to */
313     int         local_decl_indent;	/* like decl_indent but for locals */
314     int         its_a_keyword;
315     int         sizeof_keyword;
316     int         dumped_decl_indent;
317     float       case_indent;	/* The distance to indent case labels from the
318 				 * switch statement */
319     int         in_parameter_declaration;
320     int         indent_parameters;
321     int         tos;		/* pointer to top of stack */
322     char        procname[100];	/* The name of the current procedure */
323     int         just_saw_decl;
324 }           ps;
325 
326 int         ifdef_level;
327 int	    rparen_count;
328 struct parser_state state_stack[5];
329 struct parser_state match_state[5];
330