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