Annotation of src/usr.bin/window/parser5.c, Revision 1.4
1.4 ! mpech 1: /* $OpenBSD: parser5.c,v 1.3 1997/02/25 00:04:15 downsj Exp $ */
1.1 deraadt 2: /* $NetBSD: parser5.c,v 1.3 1995/09/28 10:34:35 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[] = "@(#)parser5.c 8.1 (Berkeley) 6/6/93";
43: #else
1.4 ! mpech 44: static char rcsid[] = "$OpenBSD: parser5.c,v 1.3 1997/02/25 00:04:15 downsj Exp $";
1.1 deraadt 45: #endif
46: #endif /* not lint */
47:
48: #include "parser.h"
49: #include "var.h"
50:
51: /*
52: * unary $ $? + - ! ~
53: */
54: p_expr11(v, flag)
1.4 ! mpech 55: struct value *v;
1.1 deraadt 56: char flag;
57: {
58: int op;
59: char *opname;
60:
61: switch (token) {
62: case T_DOLLAR:
63: opname = "$";
64: break;
65: case T_DQ:
66: opname = "$?";
67: break;
68: case T_PLUS:
69: opname = "unary +";
70: break;
71: case T_MINUS:
72: opname = "unary -";
73: break;
74: case T_NOT:
75: opname = "!";
76: break;
77: case T_COMP:
78: opname = "~";
79: break;
80: default:
81: return p_expr12(v, flag);
82: }
83: op = token;
84: (void) s_gettok();
85: if (p_expr11(v, flag) < 0)
86: return -1;
87: switch (v->v_type) {
88: case V_NUM:
89: break;
90: case V_STR:
91: switch (op) {
92: case T_MINUS:
93: case T_NOT:
94: case T_COMP:
95: p_error("%s: Numeric operand required.", opname);
96: str_free(v->v_str);
97: v->v_type = V_ERR;
98: return 0;
99: }
100: break;
101: case V_ERR:
102: return 0;
103: }
104: switch (op) {
105: case T_DOLLAR:
106: case T_DQ:
107: if (v->v_type == V_NUM) {
108: int tmp = cx.x_type == X_BUF && cx.x_arg != 0 &&
109: v->v_num > 0 && v->v_num <= cx.x_narg;
110: if (op == T_DQ)
111: v->v_num = tmp;
112: else if (tmp)
113: *v = cx.x_arg[v->v_num - 1];
114: else {
115: p_error("%d: No such argument.", v->v_num);
116: v->v_type = V_ERR;
117: }
118: } else {
119: char *name = v->v_str;
120: struct var *r = var_lookup(name);
121: if (op == T_DQ) {
122: v->v_type = V_NUM;
123: v->v_num = r != 0;
124: } else if (r != 0)
125: *v = r->r_val;
126: else {
127: p_error("%s: Undefined variable.", name);
128: v->v_type = V_ERR;
129: }
130: str_free(name);
131: }
132: if (v->v_type == V_STR && (v->v_str = str_cpy(v->v_str)) == 0) {
133: p_memerror();
134: return -1;
135: }
136: break;
137: case T_MINUS:
138: v->v_num = - v->v_num;
139: break;
140: case T_NOT:
141: v->v_num = ! v->v_num;
142: break;
143: case T_COMP:
144: v->v_num = ~ v->v_num;
145: break;
146: }
147: return 0;
148: }
149:
150: /*
151: * string, number, ( expr )
152: * Plus function calls.
153: *
154: * Always return v_type == V_ERR when flag == 0.
155: */
156: p_expr12(v, flag)
1.4 ! mpech 157: struct value *v;
1.1 deraadt 158: char flag;
159: {
160: v->v_type = V_ERR;
161: switch (token) {
162: case T_NUM:
163: if (flag) {
164: v->v_type = V_NUM;
165: v->v_num = token_num;
166: }
167: (void) s_gettok();
168: break;
169: case T_STR:
170: if (flag) {
171: v->v_type = V_STR;
172: v->v_str = token_str;
173: } else
174: str_free(token_str);
175: (void) s_gettok();
176: break;
177: case T_LP:
178: (void) s_gettok();
179: if (p_expr(v, flag) < 0) {
180: p_synerror();
181: return -1;
182: }
183: if (token != T_RP) {
184: p_synerror();
185: val_free(*v);
186: return -1;
187: }
188: (void) s_gettok();
189: break;
190: default:
191: return -1;
192: }
193: while (token == T_LP) {
194: char *cmd;
195:
196: if (p_convstr(v) < 0)
197: return -1;
198: cmd = v->v_type == V_STR ? v->v_str : 0;
199: if (p_function(cmd, v, flag) < 0) {
200: if (cmd)
201: str_free(cmd);
202: return -1;
203: }
204: if (cmd)
205: str_free(cmd);
206: }
207: return 0;
208: }