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