new version from James Gosling including various bug fixes
[unix-history] / usr / src / usr.bin / indent / indent_globs.h
CommitLineData
b2e768eb 1/*
30f48914
KB
2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980 The Regents of the University of California.
b0627149
KB
4 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
5 * All rights reserved.
b2e768eb 6 *
b0627149 7 * Redistribution and use in source and binary forms are permitted
b36fc510
KB
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
30f48914
KB
12 * by the University of California, Berkeley, the University of Illinois,
13 * Urbana, and Sun Microsystems, Inc. The name of either University
14 * or Sun Microsystems may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
b36fc510
KB
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
b0627149 19 *
0c8ee79d 20 * @(#)indent_globs.h 5.8 (Berkeley) %G%
b2e768eb 21 */
84262df4 22
1009bf5e
KM
23#include <stdio.h>
24
25#define BACKSLASH '\\'
30f48914 26#define bufsize 200 /* size of internal buffers */
30f48914
KB
27#define sc_size 5000 /* size of save_com buffer */
28#define label_offset 2 /* number of levels a label is placed to left
29 * of code */
84262df4 30
30f48914
KB
31#define tabsize 8 /* the size of a tab */
32#define tabmask 0177770 /* mask used when figuring length of lines
33 * with tabs */
84262df4
KM
34
35
36#define false 0
37#define true 1
38
39
30f48914
KB
40FILE *input; /* the fid for the input file */
41FILE *output; /* the output file */
42
43#define check_size(name) \
44 if (e_/**/name >= l_/**/name) { \
45 register nsize = l_/**/name-s_/**/name+400; \
46 name/**/buf = (char *) realloc(name/**/buf, nsize); \
47 e_/**/name = name/**/buf + (e_/**/name-s_/**/name) + 1; \
48 l_/**/name = name/**/buf + nsize - 5; \
49 s_/**/name = name/**/buf + 1; \
50 }
51
52char *labbuf; /* buffer for label */
53char *s_lab; /* start ... */
54char *e_lab; /* .. and end of stored label */
55char *l_lab; /* limit of label buffer */
56
57char *codebuf; /* buffer for code section */
58char *s_code; /* start ... */
59char *e_code; /* .. and end of stored code */
60char *l_code; /* limit of code section */
61
62char *combuf; /* buffer for comments */
63char *s_com; /* start ... */
64char *e_com; /* ... and end of stored comments */
65char *l_com; /* limit of comment buffer */
66
0c8ee79d
KB
67#define token s_token
68char *tokenbuf; /* the last token scanned */
69char *s_token;
70char *e_token;
71char *l_token;
72
73char *in_buffer; /* input buffer */
74char *in_buffer_limit; /* the end of the input buffer */
30f48914
KB
75char *buf_ptr; /* ptr to next character to be taken from
76 * in_buffer */
77char *buf_end; /* ptr to first after last char in in_buffer */
1009bf5e 78
30f48914
KB
79char save_com[sc_size]; /* input text is saved here when looking for
80 * the brace after an if, while, etc */
81char *sc_end; /* pointer into save_com buffer */
82
83char *bp_save; /* saved value of buf_ptr when taking input
84 * from save_com */
85char *be_save; /* similarly saved value of buf_end */
86
30f48914
KB
87
88int pointer_as_binop;
89int blanklines_after_declarations;
90int blanklines_before_blockcomments;
91int blanklines_after_procs;
92int blanklines_around_conditional_compilation;
93int swallow_optional_blanklines;
94int n_real_blanklines;
95int prefix_blankline_requested;
96int postfix_blankline_requested;
97int break_comma; /* when true and not in parens, break after a
98 * comma */
99int btype_2; /* when true, brace should be on same line as
100 * if, while, etc */
101float case_ind; /* indentation level to be used for a "case
102 * n:" */
103int code_lines; /* count of lines with code */
104int had_eof; /* set to true when input is exhausted */
105int line_no; /* the current line number. */
106int max_col; /* the maximum allowable line length */
107int verbose; /* when true, non-essential error messages are
108 * printed */
109int cuddle_else; /* true if else should cuddle up to '}' */
110int star_comment_cont; /* true iff comment continuation lines should
111 * have stars at the beginning of each line. */
112int comment_delimiter_on_blankline;
113int troff; /* true iff were generating troff input */
114int procnames_start_line; /* if true, the names of procedures
115 * being defined get placed in column
116 * 1 (ie. a newline is placed between
117 * the type of the procedure and its
118 * name) */
119int proc_calls_space; /* If true, procedure calls look like:
120 * foo(bar) rather than foo (bar) */
121int format_col1_comments; /* If comments which start in column 1
122 * are to be magically reformatted
123 * (just like comments that begin in
124 * later columns) */
125int inhibit_formatting; /* true if INDENT OFF is in effect */
126int suppress_blanklines;/* set iff following blanklines should be
127 * suppressed */
128int continuation_indent;/* set to the indentation between the edge of
129 * code and continuation lines */
130int lineup_to_parens; /* if true, continued code within parens will
131 * be lined up to the open paren */
132int Bill_Shannon; /* true iff a blank should always be inserted
133 * after sizeof */
134int blanklines_after_declarations_at_proctop; /* This is vaguely
135 * similar to
136 * blanklines_after_decla
137 * rations except that
138 * it only applies to
139 * the first set of
140 * declarations in a
141 * procedure (just after
142 * the first '{') and it
143 * causes a blank line
144 * to be generated even
145 * if there are no
146 * declarations */
147int block_comment_max_col;
148int extra_expression_indent; /* True if continuation lines from the
149 * expression part of "if(e)",
150 * "while(e)", "for(e;e;e)" should be
151 * indented an extra tab stop so that
152 * they don't conflict with the code
153 * that follows */
154
155/* -troff font state information */
156
157struct fstate {
158 char font[4];
159 char size;
160 int allcaps:1;
161};
162char *chfont();
163
164struct fstate
165 keywordf, /* keyword font */
166 stringf, /* string font */
167 boxcomf, /* Box comment font */
168 blkcomf, /* Block comment font */
169 scomf, /* Same line comment font */
170 bodyf; /* major body font */
171
172
173#define STACKSIZE 150
1009bf5e
KM
174
175struct parser_state {
176 int last_token;
30f48914
KB
177 struct fstate cfont; /* Current font */
178 int p_stack[STACKSIZE]; /* this is the parsers stack */
179 int il[STACKSIZE]; /* this stack stores indentation levels */
180 float cstk[STACKSIZE];/* used to store case stmt indentation levels */
1009bf5e 181 int box_com; /* set to true when we are in a "boxed"
30f48914
KB
182 * comment. In that case, the first non-blank
183 * char should be lined up with the / in /* */
1009bf5e
KM
184 int comment_delta,
185 n_comment_delta;
186 int cast_mask; /* indicates which close parens close off
187 * casts */
30f48914
KB
188 int sizeof_mask; /* indicates which close parens close off
189 * sizeof''s */
1009bf5e 190 int block_init; /* true iff inside a block initialization */
30f48914
KB
191 int block_init_level; /* The level of brace nesting in an
192 * initialization */
193 int last_nl; /* this is true if the last thing scanned was
194 * a newline */
1009bf5e 195 int in_or_st; /* Will be true iff there has been a
30f48914
KB
196 * declarator (e.g. int or char) and no left
197 * paren since the last semicolon. When true,
198 * a '{' is starting a structure definition or
199 * an initialization list */
200 int bl_line; /* set to 1 by dump_line if the line is blank */
201 int col_1; /* set to true if the last token started in
202 * column 1 */
1009bf5e
KM
203 int com_col; /* this is the column in which the current
204 * coment should start */
30f48914
KB
205 int com_ind; /* the column in which comments to the right
206 * of code should start */
207 int com_lines; /* the number of lines with comments, set by
208 * dump_line */
209 int dec_nest; /* current nesting level for structure or init */
1009bf5e
KM
210 int decl_com_ind; /* the column in which comments after
211 * declarations should be put */
30f48914
KB
212 int decl_on_line; /* set to true if this line of code has part
213 * of a declaration on it */
214 int i_l_follow; /* the level to which ind_level should be set
215 * after the current line is printed */
216 int in_decl; /* set to true when we are in a declaration
217 * stmt. The processing of braces is then
218 * slightly different */
1009bf5e
KM
219 int in_stmt; /* set to 1 while in a stmt */
220 int ind_level; /* the current indentation level */
221 int ind_size; /* the size of one indentation level */
30f48914
KB
222 int ind_stmt; /* set to 1 if next line should have an extra
223 * indentation level because we are in the
224 * middle of a stmt */
225 int last_u_d; /* set to true after scanning a token which
226 * forces a following operator to be unary */
1009bf5e
KM
227 int leave_comma; /* if true, never break declarations after
228 * commas */
229 int ljust_decl; /* true if declarations should be left
230 * justified */
30f48914
KB
231 int out_coms; /* the number of comments processed, set by
232 * pr_comment */
1009bf5e
KM
233 int out_lines; /* the number of lines written, set by
234 * dump_line */
30f48914
KB
235 int p_l_follow; /* used to remember how to indent following
236 * statement */
1009bf5e
KM
237 int paren_level; /* parenthesization level. used to indent
238 * within stmts */
239 short paren_indents[20]; /* column positions of each paren */
240 int pcase; /* set to 1 if the current line label is a
30f48914
KB
241 * case. It is printed differently from a
242 * regular label */
243 int search_brace; /* set to true by parse when it is necessary
244 * to buffer up all info up to the start of a
245 * stmt after an if, while, etc */
246 int unindent_displace; /* comments not to the right of code
247 * will be placed this many
248 * indentation levels to the left of
249 * code */
250 int use_ff; /* set to one if the current line should be
251 * terminated with a form feed */
252 int want_blank; /* set to true when the following token should
253 * be prefixed by a blank. (Said prefixing is
254 * ignored in some cases.) */
255 int else_if; /* True iff else if pairs should be handled
256 * specially */
257 int decl_indent; /* column to indent declared identifiers to */
1009bf5e 258 int its_a_keyword;
30f48914 259 int sizeof_keyword;
1009bf5e 260 int dumped_decl_indent;
30f48914
KB
261 float case_indent; /* The distance to indent case labels from the
262 * switch statement */
1009bf5e
KM
263 int in_parameter_declaration;
264 int indent_parameters;
265 int tos; /* pointer to top of stack */
266 char procname[100]; /* The name of the current procedure */
30f48914 267 int just_saw_decl;
1009bf5e
KM
268} ps;
269
30f48914 270int ifdef_level;
0c8ee79d 271int rparen_count;
1009bf5e
KM
272struct parser_state state_stack[5];
273struct parser_state match_state[5];