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