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