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