xref: /dragonfly/usr.bin/window/parser1.c (revision a361ab31)
1 /*	@(#)parser1.c	8.1 (Berkeley) 6/6/93	*/
2 /*	$NetBSD: parser1.c,v 1.6 2003/08/07 11:17:28 agc 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. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "defs.h"
37 #include "parser.h"
38 
39 void
40 p_start(void)
41 {
42 	char flag = 1;
43 
44 	(void) s_gettok();
45 	for (;;) {
46 		p_statementlist(flag);
47 		if (token == T_EOF || p_abort())
48 			break;
49 		flag = 0;
50 		p_synerror();
51 		while (token != T_EOL && token != T_EOF) {
52 			if (token == T_STR)
53 				str_free(token_str);
54 			(void) s_gettok();
55 		}
56 		if (token == T_EOL)
57 			(void) s_gettok();
58 		p_clearerr();
59 	}
60 }
61 
62 void
63 p_statementlist(char flag)
64 {
65 	for (; p_statement(flag) >= 0; p_clearerr())
66 		;
67 }
68 
69 int
70 p_statement(char flag)
71 {
72 	switch (token) {
73 	case T_EOL:
74 		(void) s_gettok();
75 		return 0;
76 	case T_IF:
77 		return p_if(flag);
78 	default:
79 		return p_expression(flag);
80 	}
81 }
82 
83 int
84 p_if(char flag)
85 {
86 	struct value t;
87 	char true = 0;
88 
89 top:
90 	(void) s_gettok();
91 
92 	if (p_expr(&t, flag) < 0) {
93 		p_synerror();
94 		return -1;
95 	}
96 	switch (t.v_type) {
97 	case V_NUM:
98 		true = !true && t.v_num != 0;
99 		break;
100 	case V_STR:
101 		p_error("if: Numeric value required.");
102 		str_free(t.v_str);
103 		/* FALLTHROUGH */
104 	case V_ERR:
105 		flag = 0;
106 		break;
107 	}
108 
109 	if (token != T_THEN) {
110 		p_synerror();
111 		return -1;
112 	}
113 
114 	(void) s_gettok();
115 	p_statementlist(flag && true);
116 	if (p_erred())
117 		return -1;
118 
119 	if (token == T_ELSIF)
120 		goto top;
121 
122 	if (token == T_ELSE) {
123 		(void) s_gettok();
124 		p_statementlist(flag && !true);
125 		if (p_erred())
126 			return -1;
127 	}
128 
129 	if (token == T_ENDIF) {
130 		(void) s_gettok();
131 		return 0;
132 	}
133 
134 	p_synerror();
135 	return -1;
136 }
137 
138 int
139 p_expression(char flag)
140 {
141 	struct value t;
142 	char *cmd;
143 
144 	switch (token) {
145 	case T_NUM:
146 		t.v_type = V_NUM;
147 		t.v_num = token_num;
148 		(void) s_gettok();
149 		break;
150 	case T_STR:
151 		t.v_type = V_STR;
152 		t.v_str = token_str;
153 		(void) s_gettok();
154 		break;
155 	default:
156 		if (p_expr(&t, flag) < 0)
157 			return -1;
158 		if (token == T_EOF) {
159 			val_free(t);
160 			return 0;
161 		}
162 	}
163 	if (token != T_ASSIGN && p_convstr(&t) < 0)
164 		return -1;
165 	cmd = t.v_type == V_STR ? t.v_str : 0;
166 	if ((*(token == T_ASSIGN ? p_assign : p_function))(cmd, &t, flag) < 0) {
167 		if (cmd)
168 			str_free(cmd);
169 		return -1;
170 	}
171 	if (cmd)
172 		str_free(cmd);
173 	val_free(t);
174 	if (token == T_EOL)
175 		(void) s_gettok();
176 	else if (token != T_EOF) {
177 		p_synerror();
178 		return -1;
179 	}
180 	return 0;
181 }
182 
183 int
184 p_convstr(struct value *v)
185 {
186 	if (v->v_type != V_NUM)
187 		return 0;
188 	if ((v->v_str = str_itoa(v->v_num)) == 0) {
189 		p_memerror();
190 		v->v_type = V_ERR;
191 		return -1;
192 	}
193 	v->v_type = V_STR;
194 	return 0;
195 }
196 
197 void
198 p_synerror(void)
199 {
200 	if (!cx.x_synerred) {
201 		cx.x_synerred = cx.x_erred = 1;
202 		error("Syntax error.");
203 	}
204 }
205 
206 void
207 p_error(const char *msg, ...)
208 {
209 	va_list ap;
210 
211 	va_start(ap, msg);
212 	if (!cx.x_erred) {
213 		cx.x_erred = 1;
214 		verror(msg, ap);
215 	}
216 	va_end(ap);
217 }
218 
219 void
220 p_memerror(void)
221 {
222 	cx.x_erred = cx.x_abort = 1;
223 	error("Out of memory.");
224 }
225