1 /* $NetBSD: parser2.c,v 1.11 2009/04/14 08:50:06 lukem Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid[] = "@(#)parser2.c 8.1 (Berkeley) 6/6/93";
40 __RCSID("$NetBSD: parser2.c,v 1.11 2009/04/14 08:50:06 lukem Exp $");
54 * name == 0 means we don't have a function name but
55 * want to parse the arguments anyway. flag == 0 in this case.
58 p_function(const char *name, struct value *v, int flag)
61 struct lcmd_tab *c = 0;
63 struct lcmd_arg *ap; /* this arg */
64 struct lcmd_arg *lp = 0; /* list arg */
66 struct value av[LCMD_NARG + 1];
70 if ((c = lcmd_lookup(name)))
72 else if ((a = alias_lookup(name)))
75 p_error("%s: No such command or alias.", name);
79 for (vp = av; vp < &av[LCMD_NARG + 1]; vp++)
88 if (token == T_COMMA) /* null argument */
91 if (p_expr0(&t, flag) < 0)
93 if (t.v_type == V_ERR)
96 if (token != T_ASSIGN) {
98 (c != 0 && (ap = lp) == 0 &&
99 (ap = c->lc_arg + i)->arg_name == 0)) {
100 p_error("%s: Too many arguments.", name);
106 if (p_convstr(&t) < 0)
108 tmp = t.v_type == V_STR ? t.v_str : 0;
110 if (p_expr(&t, flag) < 0) {
116 if (t.v_type == V_ERR)
121 p_error("%s: Bad alias syntax.", name);
124 for (ap = c->lc_arg, vp = av;
125 ap != 0 && ap->arg_name != 0 &&
126 (*ap->arg_name == '\0' ||
127 !str_match(tmp, ap->arg_name,
131 if (ap == 0 || ap->arg_name == 0) {
132 p_error("%s: Unknown argument \"%s\".",
143 if (ap->arg_flags & ARG_LIST) {
147 if (vp && vp->v_type != V_ERR) {
149 p_error("%s: Argument %d (%s) duplicated.",
150 name, (int)(vp - av + 1),
153 p_error("%s: Argument %d duplicated.",
154 name, (int)(vp - av + 1));
157 } else if (t.v_type == V_ERR) {
159 } else if (((ap->arg_flags&ARG_TYPE) == ARG_NUM &&
160 t.v_type != V_NUM) ||
161 ((ap->arg_flags&ARG_TYPE) == ARG_STR &&
162 t.v_type != V_STR)) {
164 p_error("%s: Argument %d (%s) type mismatch.",
165 name, (int)(vp - av + 1),
168 p_error("%s: Argument %d type mismatch.",
169 name, (int)(vp - av + 1));
178 if (token == T_COMMA)
186 else if (token != T_EOL && token != T_EOF)
187 flag = 0; /* look for legal follow set */
191 (*c->lc_func)(v, av);
193 if (a->a_flags & A_INUSE)
194 p_error("%s: Recursive alias.", a->a_name);
196 a->a_flags |= A_INUSE;
197 if (dolongcmd(a->a_buf, av, i) < 0)
199 a->a_flags &= ~A_INUSE;
208 for (vp = av; vp < &av[LCMD_NARG]; vp++)
212 for (vp = av; vp < &av[LCMD_NARG]; vp++)
218 p_assign(const char *name, struct value *v, int flag)
222 if (p_expr(v, flag) < 0) {
229 if (name && flag && var_set(name, v) == 0) {