Annotation of src/usr.bin/window/parser3.c, Revision 1.4
1.4 ! mpech 1: /* $OpenBSD: parser3.c,v 1.3 1997/02/25 00:04:13 downsj Exp $ */
1.1 deraadt 2: /* $NetBSD: parser3.c,v 1.3 1995/09/28 10:34:33 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[] = "@(#)parser3.c 8.1 (Berkeley) 6/6/93";
43: #else
1.4 ! mpech 44: static char rcsid[] = "$OpenBSD: parser3.c,v 1.3 1997/02/25 00:04:13 downsj Exp $";
1.1 deraadt 45: #endif
46: #endif /* not lint */
47:
48: #include "parser.h"
49:
50: /*
51: * =
52: * ? :
53: * ||
54: * &&
55: * |
56: * ^
57: * &
58: * == !=
59: * <= >=
60: * << >>
61: * + -
62: * * / %
63: * unary - + ~ !
64: */
65: p_expr(v, flag)
1.4 ! mpech 66: struct value *v;
1.1 deraadt 67: char flag;
68: {
69: struct value t;
70: int ret;
71:
72: if (p_expr0(&t, flag) < 0)
73: return -1;
74:
75: if (token != T_ASSIGN) {
76: *v = t;
77: return 0;
78: }
79: switch (t.v_type) {
80: case V_NUM:
81: p_error("%d: Not a variable.", t.v_num);
82: case V_ERR:
83: t.v_str = 0;
84: break;
85: }
86: ret = p_assign(t.v_str, v, flag);
87: if (t.v_str != 0)
88: str_free(t.v_str);
89: return ret;
90: }
91:
92: /*
93: * ? :
94: */
95: p_expr0(v, flag)
1.4 ! mpech 96: struct value *v;
1.1 deraadt 97: char flag;
98: {
99: struct value t;
100: char true;
101:
102: if (p_expr1(v, flag) < 0)
103: return -1;
104: if (token != T_QUEST)
105: return 0;
106: switch (v->v_type) {
107: case V_NUM:
108: true = v->v_num != 0;
109: break;
110: case V_STR:
111: p_error("?: Numeric left operand required.");
112: str_free(v->v_str);
113: v->v_type = V_ERR;
114: case V_ERR:
115: flag = 0;
116: break;
117: }
118: (void) s_gettok();
119: v->v_type = V_ERR;
120: if ((flag && true ? p_expr1(v, 1) : p_expr1(&t, 0)) < 0)
121: return -1;
122: if (token != T_COLON) {
123: val_free(*v);
124: p_synerror();
125: return -1;
126: }
127: (void) s_gettok();
128: return flag && !true ? p_expr1(v, 1) : p_expr1(&t, 0);
129: }
130:
131: /*
132: * ||
133: */
134: p_expr1(v, flag)
1.4 ! mpech 135: struct value *v;
1.1 deraadt 136: char flag;
137: {
138: char true = 0;
139:
140: if (p_expr2(v, flag) < 0)
141: return -1;
142: if (token != T_OROR)
143: return 0;
144: for (;;) {
145: switch (v->v_type) {
146: case V_NUM:
147: v->v_num = true = true || v->v_num != 0;
148: break;
149: case V_STR:
150: p_error("||: Numeric operands required.");
151: str_free(v->v_str);
152: v->v_type = V_ERR;
153: case V_ERR:
154: flag = 0;
155: break;
156: }
157: if (token != T_OROR)
158: return 0;
159: (void) s_gettok();
160: if (p_expr2(v, flag && !true) < 0)
161: return -1;
162: }
163: }
164:
165: /*
166: * &&
167: */
168: p_expr2(v, flag)
1.4 ! mpech 169: struct value *v;
1.1 deraadt 170: char flag;
171: {
172: char true = 1;
173:
174: if (p_expr3_10(3, v, flag) < 0)
175: return -1;
176: if (token != T_ANDAND)
177: return 0;
178: for (;;) {
179: switch (v->v_type) {
180: case V_NUM:
181: v->v_num = true = true && v->v_num != 0;
182: break;
183: case V_STR:
184: p_error("&&: Numeric operands required.");
185: str_free(v->v_str);
186: v->v_type = V_ERR;
187: case V_ERR:
188: flag = 0;
189: break;
190: }
191: if (token != T_ANDAND)
192: return 0;
193: (void) s_gettok();
194: if (p_expr3_10(3, v, flag && true) < 0)
195: return -1;
196: }
197: /*NOTREACHED*/
198: }