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