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