]> git.saurik.com Git - apple/shell_cmds.git/blob - window/parser2.c
963b8b9c4ba5c959adcc04c4f6fdda9378476d6c
[apple/shell_cmds.git] / window / parser2.c
1 /* $NetBSD: parser2.c,v 1.5 1998/08/25 20:59:42 ross 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
42 static char sccsid[] = "@(#)parser2.c 8.1 (Berkeley) 6/6/93";
43 #else
44 __RCSID("$NetBSD: parser2.c,v 1.5 1998/08/25 20:59:42 ross Exp $");
45 #endif
46 #endif /* not lint */
47
48 #define EXTERN
49 #include "ww.h"
50 #include "defs.h"
51 #include "alias.h"
52 #undef EXTERN
53 #include "parser.h"
54 #include "var.h"
55 #include "lcmd.h"
56
57 /*
58 * name == 0 means we don't have a function name but
59 * want to parse the arguments anyway. flag == 0 in this case.
60 */
61 int
62 p_function(name, v, flag)
63 char *name;
64 struct value *v;
65 int flag;
66 {
67 struct value t;
68 struct lcmd_tab *c = 0;
69 struct alias *a = 0;
70 struct lcmd_arg *ap; /* this arg */
71 struct lcmd_arg *lp = 0; /* list arg */
72 int i;
73 struct value av[LCMD_NARG + 1];
74 struct value *vp;
75
76 if (name != 0) {
77 if ((c = lcmd_lookup(name)))
78 name = c->lc_name;
79 else if ((a = alias_lookup(name)))
80 name = a->a_name;
81 else {
82 p_error("%s: No such command or alias.", name);
83 flag = 0;
84 }
85 }
86 for (vp = av; vp < &av[LCMD_NARG + 1]; vp++)
87 vp->v_type = V_ERR;
88
89 if (token == T_LP)
90 (void) s_gettok();
91 i = 0;
92 for (;;) {
93 ap = 0;
94 vp = 0;
95 if (token == T_COMMA) /* null argument */
96 t.v_type = V_ERR;
97 else {
98 if (p_expr0(&t, flag) < 0)
99 break;
100 if (t.v_type == V_ERR)
101 flag = 0;
102 }
103 if (token != T_ASSIGN) {
104 if (i >= LCMD_NARG ||
105 (c != 0 && (ap = lp) == 0 &&
106 (ap = c->lc_arg + i)->arg_name == 0)) {
107 p_error("%s: Too many arguments.", name);
108 flag = 0;
109 } else
110 vp = &av[i++];
111 } else {
112 char *tmp;
113 if (p_convstr(&t) < 0)
114 goto abort;
115 tmp = t.v_type == V_STR ? t.v_str : 0;
116 (void) s_gettok();
117 if (p_expr(&t, flag) < 0) {
118 if (tmp)
119 str_free(tmp);
120 p_synerror();
121 goto abort;
122 }
123 if (t.v_type == V_ERR)
124 flag = 0;
125 if (tmp) {
126 if (c == 0) {
127 /* an aliase */
128 p_error("%s: Bad alias syntax.", name);
129 flag = 0;
130 } else {
131 for (ap = c->lc_arg, vp = av;
132 ap != 0 && ap->arg_name != 0 &&
133 (*ap->arg_name == '\0' ||
134 !str_match(tmp, ap->arg_name,
135 ap->arg_minlen));
136 ap++, vp++)
137 ;
138 if (ap == 0 || ap->arg_name == 0) {
139 p_error("%s: Unknown argument \"%s\".",
140 name, tmp);
141 flag = 0;
142 ap = 0;
143 vp = 0;
144 }
145 }
146 str_free(tmp);
147 }
148 }
149 if (ap != 0) {
150 if (ap->arg_flags & ARG_LIST) {
151 i = vp - av + 1;
152 lp = ap;
153 }
154 if (vp->v_type != V_ERR) {
155 if (*ap->arg_name)
156 p_error("%s: Argument %d (%s) duplicated.",
157 name, vp - av + 1,
158 ap->arg_name);
159 else
160 p_error("%s: Argument %d duplicated.",
161 name, vp - av + 1);
162 flag = 0;
163 vp = 0;
164 } else if (t.v_type == V_ERR) {
165 /* do nothing */
166 } else if (((ap->arg_flags&ARG_TYPE) == ARG_NUM &&
167 t.v_type != V_NUM) ||
168 ((ap->arg_flags&ARG_TYPE) == ARG_STR &&
169 t.v_type != V_STR)) {
170 if (*ap->arg_name)
171 p_error("%s: Argument %d (%s) type mismatch.",
172 name, vp - av + 1,
173 ap->arg_name);
174 else
175 p_error("%s: Argument %d type mismatch.",
176 name, vp - av + 1);
177 flag = 0;
178 vp = 0;
179 }
180 }
181 if (vp != 0)
182 *vp = t;
183 else
184 val_free(t);
185 if (token == T_COMMA)
186 (void) s_gettok();
187 }
188
189 if (p_erred())
190 flag = 0;
191 if (token == T_RP)
192 (void) s_gettok();
193 else if (token != T_EOL && token != T_EOF)
194 flag = 0; /* look for legal follow set */
195 v->v_type = V_ERR;
196 if (flag) {
197 if (c != 0)
198 (*c->lc_func)(v, av);
199 else {
200 if (a->a_flags & A_INUSE)
201 p_error("%s: Recursive alias.", a->a_name);
202 else {
203 a->a_flags |= A_INUSE;
204 if (dolongcmd(a->a_buf, av, i) < 0)
205 p_memerror();
206 a->a_flags &= ~A_INUSE;
207 }
208 }
209 }
210 if (p_abort()) {
211 val_free(*v);
212 v->v_type = V_ERR;
213 goto abort;
214 }
215 for (vp = av; vp < &av[LCMD_NARG]; vp++)
216 val_free(*vp);
217 return 0;
218 abort:
219 for (vp = av; vp < &av[LCMD_NARG]; vp++)
220 val_free(*vp);
221 return -1;
222 }
223
224 int
225 p_assign(name, v, flag)
226 char *name;
227 struct value *v;
228 char flag;
229 {
230 (void) s_gettok();
231
232 if (p_expr(v, flag) < 0) {
233 p_synerror();
234 return -1;
235 }
236 switch (v->v_type) {
237 case V_STR:
238 case V_NUM:
239 if (flag && var_set(name, v) == 0) {
240 p_memerror();
241 val_free(*v);
242 return -1;
243 }
244 break;
245 }
246 return 0;
247 }