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