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