Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / window / parser1.c
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[] = "@(#)parser1.c   8.1 (Berkeley) 6/6/93";
39 static char rcsid[] =
40   "$FreeBSD: src/usr.bin/window/parser1.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
45 p_start()
46 {
47         char flag = 1;
48
49         (void) s_gettok();
50         for (;;) {
51                 p_statementlist(flag);
52                 if (token == T_EOF || p_abort())
53                         break;
54                 flag = 0;
55                 p_synerror();
56                 while (token != T_EOL && token != T_EOF) {
57                         if (token == T_STR)
58                                 str_free(token_str);
59                         (void) s_gettok();
60                 }
61                 if (token == T_EOL)
62                         (void) s_gettok();
63                 p_clearerr();
64         }
65 }
66
67 p_statementlist(flag)
68 char flag;
69 {
70         for (; p_statement(flag) >= 0; p_clearerr())
71                 ;
72 }
73
74 p_statement(flag)
75 char flag;
76 {
77         switch (token) {
78         case T_EOL:
79                 (void) s_gettok();
80                 return 0;
81         case T_IF:
82                 return p_if(flag);
83         default:
84                 return p_expression(flag);
85         }
86 }
87
88 p_if(flag)
89 char flag;
90 {
91         struct value t;
92         char true = 0;
93
94 top:
95         (void) s_gettok();
96
97         if (p_expr(&t, flag) < 0) {
98                 p_synerror();
99                 return -1;
100         }
101         switch (t.v_type) {
102         case V_NUM:
103                 true = !true && t.v_num != 0;
104                 break;
105         case V_STR:
106                 p_error("if: Numeric value required.");
107                 str_free(t.v_str);
108         case V_ERR:
109                 flag = 0;
110                 break;
111         }
112
113         if (token != T_THEN) {
114                 p_synerror();
115                 return -1;
116         }
117
118         (void) s_gettok();
119         p_statementlist(flag && true);
120         if (p_erred())
121                 return -1;
122
123         if (token == T_ELSIF)
124                 goto top;
125
126         if (token == T_ELSE) {
127                 (void) s_gettok();
128                 p_statementlist(flag && !true);
129                 if (p_erred())
130                         return -1;
131         }
132
133         if (token == T_ENDIF) {
134                 (void) s_gettok();
135                 return 0;
136         }
137
138         p_synerror();
139         return -1;
140 }
141
142 p_expression(flag)
143 char flag;
144 {
145         struct value t;
146         char *cmd;
147         int p_function(), p_assign();
148
149         switch (token) {
150         case T_NUM:
151                 t.v_type = V_NUM;
152                 t.v_num = token_num;
153                 (void) s_gettok();
154                 break;
155         case T_STR:
156                 t.v_type = V_STR;
157                 t.v_str = token_str;
158                 (void) s_gettok();
159                 break;
160         default:
161                 if (p_expr(&t, flag) < 0)
162                         return -1;
163                 if (token == T_EOF) {
164                         val_free(t);
165                         return 0;
166                 }
167         }
168         if (token != T_ASSIGN && p_convstr(&t) < 0)
169                 return -1;
170         cmd = t.v_type == V_STR ? t.v_str : 0;
171         if ((*(token == T_ASSIGN ? p_assign : p_function))(cmd, &t, flag) < 0) {
172                 if (cmd)
173                         str_free(cmd);
174                 return -1;
175         }
176         if (cmd)
177                 str_free(cmd);
178         val_free(t);
179         if (token == T_EOL)
180                 (void) s_gettok();
181         else if (token != T_EOF) {
182                 p_synerror();
183                 return -1;
184         }
185         return 0;
186 }
187
188 p_convstr(v)
189 register struct value *v;
190 {
191         if (v->v_type != V_NUM)
192                 return 0;
193         if ((v->v_str = str_itoa(v->v_num)) == 0) {
194                 p_memerror();
195                 v->v_type = V_ERR;
196                 return -1;
197         }
198         v->v_type = V_STR;
199         return 0;
200 }
201
202 p_synerror()
203 {
204         if (!cx.x_synerred) {
205                 cx.x_synerred = cx.x_erred = 1;
206                 error("Syntax error.");
207         }
208 }
209
210 /*VARARGS1*/
211 p_error(msg, a, b, c)
212 char *msg;
213 {
214         if (!cx.x_erred) {
215                 cx.x_erred = 1;
216                 error(msg, a, b, c);
217         }
218 }
219
220 p_memerror()
221 {
222         cx.x_erred = cx.x_abort = 1;
223         error("Out of memory.");
224 }