kernel/{hammer2,tmpfs}: Fix ENOTDIR/EISDIR logic.
[dragonfly.git] / usr.bin / indent / parse.c
blob6b98e5d39260ea554750a3a401aa85dae2ba21d3
1 /*
2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980, 1993
4 * The Regents of the University of California. All rights reserved.
5 * All rights reserved.
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.
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.
31 * @(#)parse.c 8.1 (Berkeley) 6/6/93
32 * $FreeBSD: src/usr.bin/indent/parse.c,v 1.10 2003/06/15 09:28:17 charnier Exp $
35 #include <stdio.h>
36 #include "indent_globs.h"
37 #include "indent_codes.h"
38 #include "indent.h"
40 static void reduce(void);
42 void
43 parse(int tk) /* tk: the code for the construct scanned */
45 int i;
47 #ifdef debug
48 printf("%2d - %s\n", tk, token);
49 #endif
51 while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
52 /* true if we have an if without an else */
53 ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::= stmt
54 * reduction */
55 reduce(); /* see if this allows any reduction */
59 switch (tk) { /* go on and figure out what to do with the
60 * input */
62 case decl: /* scanned a declaration word */
63 ps.search_brace = btype_2;
64 /* indicate that following brace should be on same line */
65 if (ps.p_stack[ps.tos] != decl) { /* only put one declaration
66 * onto stack */
67 break_comma = true; /* while in declaration, newline should be
68 * forced after comma */
69 ps.p_stack[++ps.tos] = decl;
70 ps.il[ps.tos] = ps.i_l_follow;
72 if (ps.ljust_decl) {/* only do if we want left justified
73 * declarations */
74 ps.ind_level = 0;
75 for (i = ps.tos - 1; i > 0; --i)
76 if (ps.p_stack[i] == decl)
77 ++ps.ind_level; /* indentation is number of
78 * declaration levels deep we are */
79 ps.i_l_follow = ps.ind_level;
82 break;
84 case ifstmt: /* scanned if (...) */
85 if (ps.p_stack[ps.tos] == elsehead && ps.else_if) /* "else if ..." */
86 ps.i_l_follow = ps.il[ps.tos];
87 /* FALLTHROUGH */
88 case dolit: /* 'do' */
89 case forstmt: /* for (...) */
90 ps.p_stack[++ps.tos] = tk;
91 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
92 ++ps.i_l_follow; /* subsequent statements should be indented 1 */
93 ps.search_brace = btype_2;
94 break;
96 case lbrace: /* scanned { */
97 break_comma = false; /* don't break comma in an initial list */
98 if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
99 || ps.p_stack[ps.tos] == stmtl)
100 ++ps.i_l_follow; /* it is a random, isolated stmt group or a
101 * declaration */
102 else {
103 if (s_code == e_code) {
105 * only do this if there is nothing on the line
107 --ps.ind_level;
109 * it is a group as part of a while, for, etc.
111 if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
112 --ps.ind_level;
114 * for a switch, brace should be two levels out from the code
119 ps.p_stack[++ps.tos] = lbrace;
120 ps.il[ps.tos] = ps.ind_level;
121 ps.p_stack[++ps.tos] = stmt;
122 /* allow null stmt between braces */
123 ps.il[ps.tos] = ps.i_l_follow;
124 break;
126 case whilestmt: /* scanned while (...) */
127 if (ps.p_stack[ps.tos] == dohead) {
128 /* it is matched with do stmt */
129 ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
130 ps.p_stack[++ps.tos] = whilestmt;
131 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
133 else { /* it is a while loop */
134 ps.p_stack[++ps.tos] = whilestmt;
135 ps.il[ps.tos] = ps.i_l_follow;
136 ++ps.i_l_follow;
137 ps.search_brace = btype_2;
140 break;
142 case elselit: /* scanned an else */
144 if (ps.p_stack[ps.tos] != ifhead)
145 diag2(1, "Unmatched 'else'");
146 else {
147 ps.ind_level = ps.il[ps.tos]; /* indentation for else should
148 * be same as for if */
149 ps.i_l_follow = ps.ind_level + 1; /* everything following should
150 * be in 1 level */
151 ps.p_stack[ps.tos] = elsehead;
152 /* remember if with else */
153 ps.search_brace = btype_2 | ps.else_if;
155 break;
157 case rbrace: /* scanned a } */
158 /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
159 if (ps.p_stack[ps.tos - 1] == lbrace) {
160 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
161 ps.p_stack[ps.tos] = stmt;
163 else
164 diag2(1, "Statement nesting error");
165 break;
167 case swstmt: /* had switch (...) */
168 ps.p_stack[++ps.tos] = swstmt;
169 ps.cstk[ps.tos] = case_ind;
170 /* save current case indent level */
171 ps.il[ps.tos] = ps.i_l_follow;
172 case_ind = ps.i_l_follow + ps.case_indent; /* cases should be one
173 * level down from
174 * switch */
175 ps.i_l_follow += ps.case_indent + 1; /* statements should be two
176 * levels in */
177 ps.search_brace = btype_2;
178 break;
180 case semicolon: /* this indicates a simple stmt */
181 break_comma = false; /* turn off flag to break after commas in a
182 * declaration */
183 ps.p_stack[++ps.tos] = stmt;
184 ps.il[ps.tos] = ps.ind_level;
185 break;
187 default: /* this is an error */
188 diag2(1, "Unknown code to parser");
189 return;
192 } /* end of switch */
194 reduce(); /* see if any reduction can be done */
196 #ifdef debug
197 for (i = 1; i <= ps.tos; ++i)
198 printf("(%d %d)", ps.p_stack[i], ps.il[i]);
199 printf("\n");
200 #endif
202 return;
206 * NAME: reduce
208 * FUNCTION: Implements the reduce part of the parsing algorithm
210 * ALGORITHM: The following reductions are done. Reductions are repeated
211 * until no more are possible.
213 * Old TOS New TOS
214 * <stmt> <stmt> <stmtl>
215 * <stmtl> <stmt> <stmtl>
216 * do <stmt> "dostmt"
217 * if <stmt> "ifstmt"
218 * switch <stmt> <stmt>
219 * decl <stmt> <stmt>
220 * "ifelse" <stmt> <stmt>
221 * for <stmt> <stmt>
222 * while <stmt> <stmt>
223 * "dostmt" while <stmt>
225 * On each reduction, ps.i_l_follow (the indentation for the following line)
226 * is set to the indentation level associated with the old TOS.
228 * PARAMETERS: None
230 * RETURNS: Nothing
232 * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
234 * CALLS: None
236 * CALLED BY: parse
238 * HISTORY: initial coding November 1976 D A Willcox of CAC
241 /*----------------------------------------------*\
242 | REDUCTION PHASE |
243 \*----------------------------------------------*/
244 static void
245 reduce(void)
247 int i;
249 for (;;) { /* keep looping until there is nothing left to
250 * reduce */
252 switch (ps.p_stack[ps.tos]) {
254 case stmt:
255 switch (ps.p_stack[ps.tos - 1]) {
257 case stmt:
258 case stmtl:
259 /* stmtl stmt or stmt stmt */
260 ps.p_stack[--ps.tos] = stmtl;
261 break;
263 case dolit: /* <do> <stmt> */
264 ps.p_stack[--ps.tos] = dohead;
265 ps.i_l_follow = ps.il[ps.tos];
266 break;
268 case ifstmt:
269 /* <if> <stmt> */
270 ps.p_stack[--ps.tos] = ifhead;
271 for (i = ps.tos - 1;
273 ps.p_stack[i] != stmt
275 ps.p_stack[i] != stmtl
277 ps.p_stack[i] != lbrace
279 --i);
280 ps.i_l_follow = ps.il[i];
282 * for the time being, we will assume that there is no else on
283 * this if, and set the indentation level accordingly. If an
284 * else is scanned, it will be fixed up later
286 break;
288 case swstmt:
289 /* <switch> <stmt> */
290 case_ind = ps.cstk[ps.tos - 1];
291 /* FALLTHROUGH */
292 case decl: /* finish of a declaration */
293 case elsehead:
294 /* <<if> <stmt> else> <stmt> */
295 case forstmt:
296 /* <for> <stmt> */
297 case whilestmt:
298 /* <while> <stmt> */
299 ps.p_stack[--ps.tos] = stmt;
300 ps.i_l_follow = ps.il[ps.tos];
301 break;
303 default: /* <anything else> <stmt> */
304 return;
306 } /* end of section for <stmt> on top of stack */
307 break;
309 case whilestmt: /* while (...) on top */
310 if (ps.p_stack[ps.tos - 1] == dohead) {
311 /* it is termination of a do while */
312 ps.tos -= 2;
313 break;
315 else
316 return;
318 default: /* anything else on top */
319 return;