1 /*	$OpenBSD: parser2.c,v 1.5 2003/06/03 02:56:23 millert Exp $	*/
2 /*	$NetBSD: parser2.c,v 1.3 1995/09/28 10:34:32 tls Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Edward Wang at The University of California, Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)parser2.c	8.1 (Berkeley) 6/6/93";
39 #else
40 static char rcsid[] = "$OpenBSD: parser2.c,v 1.5 2003/06/03 02:56:23 millert Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include "parser.h"
45 #include "var.h"
46 #include "lcmd.h"
47 #include "alias.h"
48 
49 /*
50  * name == 0 means we don't have a function name but
51  * want to parse the arguments anyway.  flag == 0 in this case.
52  */
p_function(name,v,flag)53 p_function(name, v, flag)
54 char *name;
55 struct value *v;
56 {
57 	struct value t;
58 	struct lcmd_tab *c = 0;
59 	struct alias *a = 0;
60 	struct lcmd_arg *ap;			/* this arg */
61 	struct lcmd_arg *lp = 0;		/* list arg */
62 	int i;
63 	struct value av[LCMD_NARG + 1];
64 	struct value *vp;
65 
66 	if (name != 0)
67 		if (c = lcmd_lookup(name))
68 			name = c->lc_name;
69 		else if (a = alias_lookup(name))
70 			name = a->a_name;
71 		else {
72 			p_error("%s: No such command or alias.", name);
73 			flag = 0;
74 		}
75 
76 	for (vp = av; vp < &av[LCMD_NARG + 1]; vp++)
77 		vp->v_type = V_ERR;
78 
79 	if (token == T_LP)
80 		(void) s_gettok();
81 	i = 0;
82 	for (;;) {
83 		ap = 0;
84 		vp = 0;
85 		if (token == T_COMMA)		/* null argument */
86 			t.v_type = V_ERR;
87 		else {
88 			if (p_expr0(&t, flag) < 0)
89 				break;
90 			if (t.v_type == V_ERR)
91 				flag = 0;
92 		}
93 		if (token != T_ASSIGN) {
94 			if (i >= LCMD_NARG ||
95 			    c != 0 && (ap = lp) == 0 &&
96 			    (ap = c->lc_arg + i)->arg_name == 0) {
97 				p_error("%s: Too many arguments.", name);
98 				flag = 0;
99 			} else
100 				vp = &av[i++];
101 		} else {
102 			char *tmp;
103 			if (p_convstr(&t) < 0)
104 				goto abort;
105 			tmp = t.v_type == V_STR ? t.v_str : 0;
106 			(void) s_gettok();
107 			if (p_expr(&t, flag) < 0) {
108 				if (tmp)
109 					str_free(tmp);
110 				p_synerror();
111 				goto abort;
112 			}
113 			if (t.v_type == V_ERR)
114 				flag = 0;
115 			if (tmp) {
116 				if (c == 0) {
117 					/* an aliase */
118 					p_error("%s: Bad alias syntax.", name);
119 					flag = 0;
120 				} else {
121 					for (ap = c->lc_arg, vp = av;
122 					     ap != 0 && ap->arg_name != 0 &&
123 					     (*ap->arg_name == '\0' ||
124 					      !str_match(tmp, ap->arg_name,
125 							ap->arg_minlen));
126 					     ap++, vp++)
127 						;
128 					if (ap == 0 || ap->arg_name == 0) {
129 						p_error("%s: Unknown argument \"%s\".",
130 							name, tmp);
131 						flag = 0;
132 						ap = 0;
133 						vp = 0;
134 					}
135 				}
136 				str_free(tmp);
137 			}
138 		}
139 		if (ap != 0) {
140 			if (ap->arg_flags & ARG_LIST) {
141 				i = vp - av + 1;
142 				lp = ap;
143 			}
144 			if (vp->v_type != V_ERR) {
145 				if (*ap->arg_name)
146 					p_error("%s: Argument %d (%s) duplicated.",
147 						name, vp - av + 1,
148 						ap->arg_name);
149 				else
150 					p_error("%s: Argument %d duplicated.",
151 						name, vp - av + 1);
152 				flag = 0;
153 				vp = 0;
154 			} else if (t.v_type == V_ERR) {
155 				/* do nothing */
156 			} else if ((ap->arg_flags&ARG_TYPE) == ARG_NUM &&
157 				   t.v_type != V_NUM ||
158 				   (ap->arg_flags&ARG_TYPE) == ARG_STR &&
159 				   t.v_type != V_STR) {
160 				if (*ap->arg_name)
161 					p_error("%s: Argument %d (%s) type mismatch.",
162 						name, vp - av + 1,
163 						ap->arg_name);
164 				else
165 					p_error("%s: Argument %d type mismatch.",
166 						name, vp - av + 1);
167 				flag = 0;
168 				vp = 0;
169 			}
170 		}
171 		if (vp != 0)
172 			*vp = t;
173 		else
174 			val_free(t);
175 		if (token == T_COMMA)
176 			(void) s_gettok();
177 	}
178 
179 	if (p_erred())
180 		flag = 0;
181 	if (token == T_RP)
182 		(void) s_gettok();
183 	else if (token != T_EOL && token != T_EOF)
184 		flag = 0;		/* look for legal follow set */
185 	v->v_type = V_ERR;
186 	if (flag)
187 		if (c != 0)
188 			(*c->lc_func)(v, av);
189 		else
190 			if (a->a_flags & A_INUSE)
191 				p_error("%s: Recursive alias.", a->a_name);
192 			else {
193 				a->a_flags |= A_INUSE;
194 				if (dolongcmd(a->a_buf, av, i) < 0)
195 					p_memerror();
196 				a->a_flags &= ~A_INUSE;
197 			}
198 	if (p_abort()) {
199 		val_free(*v);
200 		v->v_type = V_ERR;
201 		goto abort;
202 	}
203 	for (vp = av; vp < &av[LCMD_NARG]; vp++)
204 		val_free(*vp);
205 	return 0;
206 abort:
207 	for (vp = av; vp < &av[LCMD_NARG]; vp++)
208 		val_free(*vp);
209 	return -1;
210 }
211 
p_assign(name,v,flag)212 p_assign(name, v, flag)
213 char *name;
214 struct value *v;
215 char flag;
216 {
217 	(void) s_gettok();
218 
219 	if (p_expr(v, flag) < 0) {
220 		p_synerror();
221 		return -1;
222 	}
223 	switch (v->v_type) {
224 	case V_STR:
225 	case V_NUM:
226 		if (flag && var_set(name, v) == 0) {
227 			p_memerror();
228 			val_free(*v);
229 			return -1;
230 		}
231 		break;
232 	}
233 	return 0;
234 }
235