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