Annotation of src/usr.bin/indent/indent_globs.h, Revision 1.8
1.8 ! deraadt 1: /* * $OpenBSD: indent_globs.h,v 1.7 2002/02/16 21:27:47 millert Exp $*/
1.1 deraadt 2: /*
3: * Copyright (c) 1985 Sun Microsystems, Inc.
1.5 pjanzen 4: * Copyright (c) 1980, 1993
5: * The Regents of the University of California.
1.1 deraadt 6: * Copyright (c) 1976 Board of Trustees of the University of Illinois.
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: *
1.5 pjanzen 37: * from: @(#)indent_globs.h 8.1 (Berkeley) 6/6/93
1.1 deraadt 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: #define tabsize 8 /* the size of a tab */
47: #define tabmask 0177770 /* mask used when figuring length of lines
48: * with tabs */
49:
50:
51: #define false 0
52: #define true 1
53:
54:
55: FILE *input; /* the fid for the input file */
56: FILE *output; /* the output file */
57:
58: #define CHECK_SIZE_CODE \
59: if (e_code >= l_code) { \
1.8 ! deraadt 60: int nsize = l_code-s_code+400; \
! 61: \
1.1 deraadt 62: codebuf = (char *) realloc(codebuf, nsize); \
1.5 pjanzen 63: if (codebuf == NULL) \
1.6 pjanzen 64: err(1, NULL); \
1.1 deraadt 65: e_code = codebuf + (e_code-s_code) + 1; \
66: l_code = codebuf + nsize - 5; \
67: s_code = codebuf + 1; \
68: }
69: #define CHECK_SIZE_COM \
70: if (e_com >= l_com) { \
1.8 ! deraadt 71: int nsize = l_com-s_com+400; \
! 72: \
1.1 deraadt 73: combuf = (char *) realloc(combuf, nsize); \
1.5 pjanzen 74: if (combuf == NULL) \
1.6 pjanzen 75: err(1, NULL); \
1.1 deraadt 76: e_com = combuf + (e_com-s_com) + 1; \
77: l_com = combuf + nsize - 5; \
78: s_com = combuf + 1; \
79: }
80: #define CHECK_SIZE_LAB \
81: if (e_lab >= l_lab) { \
1.8 ! deraadt 82: int nsize = l_lab-s_lab+400; \
! 83: \
1.1 deraadt 84: labbuf = (char *) realloc(labbuf, nsize); \
1.5 pjanzen 85: if (labbuf == NULL) \
1.6 pjanzen 86: err(1, NULL); \
1.1 deraadt 87: e_lab = labbuf + (e_lab-s_lab) + 1; \
88: l_lab = labbuf + nsize - 5; \
89: s_lab = labbuf + 1; \
90: }
91: #define CHECK_SIZE_TOKEN \
92: if (e_token >= l_token) { \
1.8 ! deraadt 93: int nsize = l_token-s_token+400; \
! 94: \
1.1 deraadt 95: tokenbuf = (char *) realloc(tokenbuf, nsize); \
1.5 pjanzen 96: if (tokenbuf == NULL) \
1.6 pjanzen 97: err(1, NULL); \
1.1 deraadt 98: e_token = tokenbuf + (e_token-s_token) + 1; \
99: l_token = tokenbuf + nsize - 5; \
100: s_token = tokenbuf + 1; \
101: }
102:
103: char *labbuf; /* buffer for label */
104: char *s_lab; /* start ... */
105: char *e_lab; /* .. and end of stored label */
106: char *l_lab; /* limit of label buffer */
107:
108: char *codebuf; /* buffer for code section */
109: char *s_code; /* start ... */
110: char *e_code; /* .. and end of stored code */
111: char *l_code; /* limit of code section */
112:
113: char *combuf; /* buffer for comments */
114: char *s_com; /* start ... */
115: char *e_com; /* ... and end of stored comments */
116: char *l_com; /* limit of comment buffer */
117:
118: #define token s_token
119: char *tokenbuf; /* the last token scanned */
120: char *s_token;
121: char *e_token;
122: char *l_token;
123:
124: char *in_buffer; /* input buffer */
125: char *in_buffer_limit; /* the end of the input buffer */
126: char *buf_ptr; /* ptr to next character to be taken from
127: * in_buffer */
128: char *buf_end; /* ptr to first after last char in in_buffer */
129:
130: char save_com[sc_size]; /* input text is saved here when looking for
131: * the brace after an if, while, etc */
132: char *sc_end; /* pointer into save_com buffer */
133:
134: char *bp_save; /* saved value of buf_ptr when taking input
135: * from save_com */
136: char *be_save; /* similarly saved value of buf_end */
137:
138:
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_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:
206: /* -troff font state information */
207:
208: struct fstate {
209: char font[4];
210: char size;
211: int allcaps:1;
212: };
213: char *chfont();
214:
215: struct fstate
216: keywordf, /* keyword font */
217: stringf, /* string font */
218: boxcomf, /* Box comment font */
219: blkcomf, /* Block comment font */
220: scomf, /* Same line comment font */
221: bodyf; /* major body font */
222:
223:
224: #define STACKSIZE 150
225:
226: struct parser_state {
227: int last_token;
228: struct fstate cfont; /* Current font */
229: int p_stack[STACKSIZE]; /* this is the parsers stack */
230: int il[STACKSIZE]; /* this stack stores indentation levels */
231: float cstk[STACKSIZE];/* used to store case stmt indentation levels */
232: int box_com; /* set to true when we are in a "boxed"
233: * comment. In that case, the first non-blank
1.3 mickey 234: * char should be lined up with the / in rem */
1.1 deraadt 235: int comment_delta,
236: n_comment_delta;
237: int cast_mask; /* indicates which close parens close off
238: * casts */
239: int sizeof_mask; /* indicates which close parens close off
240: * sizeof''s */
241: int block_init; /* true iff inside a block initialization */
242: int block_init_level; /* The level of brace nesting in an
243: * initialization */
244: int last_nl; /* this is true if the last thing scanned was
245: * a newline */
246: int in_or_st; /* Will be true iff there has been a
247: * declarator (e.g. int or char) and no left
248: * paren since the last semicolon. When true,
249: * a '{' is starting a structure definition or
250: * an initialization list */
251: int bl_line; /* set to 1 by dump_line if the line is blank */
252: int col_1; /* set to true if the last token started in
253: * column 1 */
254: int com_col; /* this is the column in which the current
255: * coment should start */
256: int com_ind; /* the column in which comments to the right
257: * of code should start */
258: int com_lines; /* the number of lines with comments, set by
259: * dump_line */
260: int dec_nest; /* current nesting level for structure or init */
261: int decl_com_ind; /* the column in which comments after
262: * declarations should be put */
263: int decl_on_line; /* set to true if this line of code has part
264: * of a declaration on it */
265: int i_l_follow; /* the level to which ind_level should be set
266: * after the current line is printed */
267: int in_decl; /* set to true when we are in a declaration
268: * stmt. The processing of braces is then
269: * slightly different */
270: int in_stmt; /* set to 1 while in a stmt */
271: int ind_level; /* the current indentation level */
272: int ind_size; /* the size of one indentation level */
273: int ind_stmt; /* set to 1 if next line should have an extra
274: * indentation level because we are in the
275: * middle of a stmt */
276: int last_u_d; /* set to true after scanning a token which
277: * forces a following operator to be unary */
278: int leave_comma; /* if true, never break declarations after
279: * commas */
280: int ljust_decl; /* true if declarations should be left
281: * justified */
282: int out_coms; /* the number of comments processed, set by
283: * pr_comment */
284: int out_lines; /* the number of lines written, set by
285: * dump_line */
286: int p_l_follow; /* used to remember how to indent following
287: * statement */
288: int paren_level; /* parenthesization level. used to indent
289: * within stmts */
290: short paren_indents[20]; /* column positions of each paren */
291: int pcase; /* set to 1 if the current line label is a
292: * case. It is printed differently from a
293: * regular label */
294: int search_brace; /* set to true by parse when it is necessary
295: * to buffer up all info up to the start of a
296: * stmt after an if, while, etc */
297: int unindent_displace; /* comments not to the right of code
298: * will be placed this many
299: * indentation levels to the left of
300: * code */
301: int use_ff; /* set to one if the current line should be
302: * terminated with a form feed */
303: int want_blank; /* set to true when the following token should
304: * be prefixed by a blank. (Said prefixing is
305: * ignored in some cases.) */
306: int else_if; /* True iff else if pairs should be handled
307: * specially */
308: int decl_indent; /* column to indent declared identifiers to */
309: int its_a_keyword;
310: int sizeof_keyword;
311: int dumped_decl_indent;
312: float case_indent; /* The distance to indent case labels from the
313: * switch statement */
314: int in_parameter_declaration;
315: int indent_parameters;
316: int tos; /* pointer to top of stack */
317: char procname[100]; /* The name of the current procedure */
318: int just_saw_decl;
319: } ps;
320:
321: int ifdef_level;
322: int rparen_count;
323: struct parser_state state_stack[5];
324: struct parser_state match_state[5];
1.5 pjanzen 325:
1.7 millert 326: int compute_code_target(void);
327: int compute_label_target(void);
328: int count_spaces(int, char *);
329: void diag(int, char *, ...);
330: void dump_line(void);
331: int eqin(char *, char *);
332: void fill_buffer(void);
333: int pad_output(int, int);
334: void scan_profile(FILE *);
335: void set_defaults(void);
336: void set_option(char *);
337: void addkey(char *, int);
338: void set_profile(void);
339: char *chfont(struct fstate *, struct fstate *, char *);
340: void parsefont(struct fstate *, char *);
341: void writefdef(struct fstate *, int);
342: int lexi(void);
343: void reduce(void);
344: void parse(int);
345: void pr_comment(void);
346: void bakcopy(void);