]> git.saurik.com Git - apple/shell_cmds.git/blame - window/parser1.c
shell_cmds-34.tar.gz
[apple/shell_cmds.git] / window / parser1.c
CommitLineData
44bd5ea7
A
1/* $NetBSD: parser1.c,v 1.4 1997/11/21 08:36:11 lukem Exp $ */
2
3/*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)parser1.c 8.1 (Berkeley) 6/6/93";
43#else
44__RCSID("$NetBSD: parser1.c,v 1.4 1997/11/21 08:36:11 lukem Exp $");
45#endif
46#endif /* not lint */
47
48#include "defs.h"
49#include "parser.h"
50
51void
52p_start()
53{
54 char flag = 1;
55
56 (void) s_gettok();
57 for (;;) {
58 p_statementlist(flag);
59 if (token == T_EOF || p_abort())
60 break;
61 flag = 0;
62 p_synerror();
63 while (token != T_EOL && token != T_EOF) {
64 if (token == T_STR)
65 str_free(token_str);
66 (void) s_gettok();
67 }
68 if (token == T_EOL)
69 (void) s_gettok();
70 p_clearerr();
71 }
72}
73
74void
75p_statementlist(flag)
76 char flag;
77{
78 for (; p_statement(flag) >= 0; p_clearerr())
79 ;
80}
81
82int
83p_statement(flag)
84 char flag;
85{
86 switch (token) {
87 case T_EOL:
88 (void) s_gettok();
89 return 0;
90 case T_IF:
91 return p_if(flag);
92 default:
93 return p_expression(flag);
94 }
95}
96
97int
98p_if(flag)
99 char flag;
100{
101 struct value t;
102 char true = 0;
103
104top:
105 (void) s_gettok();
106
107 if (p_expr(&t, flag) < 0) {
108 p_synerror();
109 return -1;
110 }
111 switch (t.v_type) {
112 case V_NUM:
113 true = !true && t.v_num != 0;
114 break;
115 case V_STR:
116 p_error("if: Numeric value required.");
117 str_free(t.v_str);
118 case V_ERR:
119 flag = 0;
120 break;
121 }
122
123 if (token != T_THEN) {
124 p_synerror();
125 return -1;
126 }
127
128 (void) s_gettok();
129 p_statementlist(flag && true);
130 if (p_erred())
131 return -1;
132
133 if (token == T_ELSIF)
134 goto top;
135
136 if (token == T_ELSE) {
137 (void) s_gettok();
138 p_statementlist(flag && !true);
139 if (p_erred())
140 return -1;
141 }
142
143 if (token == T_ENDIF) {
144 (void) s_gettok();
145 return 0;
146 }
147
148 p_synerror();
149 return -1;
150}
151
152int
153p_expression(flag)
154 char flag;
155{
156 struct value t;
157 char *cmd;
158
159 switch (token) {
160 case T_NUM:
161 t.v_type = V_NUM;
162 t.v_num = token_num;
163 (void) s_gettok();
164 break;
165 case T_STR:
166 t.v_type = V_STR;
167 t.v_str = token_str;
168 (void) s_gettok();
169 break;
170 default:
171 if (p_expr(&t, flag) < 0)
172 return -1;
173 if (token == T_EOF) {
174 val_free(t);
175 return 0;
176 }
177 }
178 if (token != T_ASSIGN && p_convstr(&t) < 0)
179 return -1;
180 cmd = t.v_type == V_STR ? t.v_str : 0;
181 if ((*(token == T_ASSIGN ? p_assign : p_function))(cmd, &t, flag) < 0) {
182 if (cmd)
183 str_free(cmd);
184 return -1;
185 }
186 if (cmd)
187 str_free(cmd);
188 val_free(t);
189 if (token == T_EOL)
190 (void) s_gettok();
191 else if (token != T_EOF) {
192 p_synerror();
193 return -1;
194 }
195 return 0;
196}
197
198int
199p_convstr(v)
200 struct value *v;
201{
202 if (v->v_type != V_NUM)
203 return 0;
204 if ((v->v_str = str_itoa(v->v_num)) == 0) {
205 p_memerror();
206 v->v_type = V_ERR;
207 return -1;
208 }
209 v->v_type = V_STR;
210 return 0;
211}
212
213void
214p_synerror()
215{
216 if (!cx.x_synerred) {
217 cx.x_synerred = cx.x_erred = 1;
218 error("Syntax error.");
219 }
220}
221
222void
223#if __STDC__
224p_error(const char *msg, ...)
225#else
226p_error(msg, ..)
227 char *msg;
228 va_dcl
229#endif
230{
231 va_list ap;
232#if __STDC__
233 va_start(ap, msg);
234#else
235 va_start(ap);
236#endif
237 if (!cx.x_erred) {
238 cx.x_erred = 1;
239 verror(msg, ap);
240 }
241 va_end(ap);
242}
243
244void
245p_memerror()
246{
247 cx.x_erred = cx.x_abort = 1;
248 error("Out of memory.");
249}