]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/window/parser4.c
This commit was generated by cvs2svn to compensate for changes in r75264,
[FreeBSD/FreeBSD.git] / usr.bin / window / parser4.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[] = "@(#)parser4.c   8.1 (Berkeley) 6/6/93";
39 static char rcsid[] = "@(#)$FreeBSD$";
40 #endif /* not lint */
41
42 #include <string.h>
43 #include "parser.h"
44
45 /*
46  * |            3
47  * ^            4
48  * &            5
49  * == !=        6
50  * < <= > >=    7
51  * << >>        8
52  * + -          9
53  * * / %        10
54  */
55 p_expr3_10(level, v, flag)
56 register struct value *v;
57 char flag;
58 {
59         struct value l, r;
60         int op;
61         char *opname;
62
63         if ((level == 10 ? p_expr11(v, flag)
64              : p_expr3_10(level + 1, v, flag)) < 0)
65                 return -1;
66         for (;;) {
67                 switch (level) {
68                 case 3:
69                         if (token != T_OR)
70                                 return 0;
71                         opname = "|";
72                         break;
73                 case 4:
74                         if (token != T_XOR)
75                                 return 0;
76                         opname = "^";
77                         break;
78                 case 5:
79                         if (token != T_AND)
80                                 return 0;
81                         opname = "&";
82                         break;
83                 case 6:
84                         if (token == T_EQ)
85                                 opname = "==";
86                         else if (token == T_NE)
87                                 opname = "!=";
88                         else
89                                 return 0;
90                         break;
91                 case 7:
92                         switch (token) {
93                         case T_LT:
94                                 opname = "<";
95                                 break;
96                         case T_LE:
97                                 opname = "<=";
98                                 break;
99                         case T_GT:
100                                 opname = ">";
101                                 break;
102                         case T_GE:
103                                 opname = ">=";
104                                 break;
105                         default:
106                                 return 0;
107                         }
108                         break;
109                 case 8:
110                         if (token == T_LS)
111                                 opname = "<<";
112                         else if (token == T_RS)
113                                 opname = ">>";
114                         else
115                                 return 0;
116                         break;
117                 case 9:
118                         if (token == T_PLUS)
119                                 opname = "+";
120                         else if (token == T_MINUS)
121                                 opname = "-";
122                         else
123                                 return 0;
124                         break;
125                 case 10:
126                         switch (token) {
127                         case T_MUL:
128                                 opname = "*";
129                                 break;
130                         case T_DIV:
131                                 opname = "/";
132                                 break;
133                         case T_MOD:
134                                 opname = "%";
135                                 break;
136                         default:
137                                 return 0;
138                         }
139                         break;
140                 }
141                 l = *v;
142                 if (l.v_type == V_ERR)
143                         flag = 0;
144
145                 op = token;
146                 (void) s_gettok();
147                 if ((level == 10 ? p_expr11(&r, flag)
148                      : p_expr3_10(level + 1, &r, flag)) < 0) {
149                         p_synerror();
150                         val_free(l);
151                         return -1;
152                 }
153
154                 if (r.v_type == V_ERR)
155                         flag = 0;
156                 else switch (op) {
157                 case T_EQ:
158                 case T_NE:
159                 case T_LT:
160                 case T_LE:
161                 case T_GT:
162                 case T_GE:
163                 case T_PLUS:
164                         if (l.v_type == V_STR) {
165                                 if (r.v_type == V_NUM)
166                                         if (p_convstr(&r) < 0)
167                                                 flag = 0;
168                         } else
169                                 if (r.v_type == V_STR)
170                                         if (p_convstr(&l) < 0)
171                                                 flag = 0;
172                         break;
173                 case T_LS:
174                 case T_RS:
175                         if (r.v_type == V_STR) {
176                                 char *p = r.v_str;
177                                 r.v_type = V_NUM;
178                                 r.v_num = strlen(p);
179                                 str_free(p);
180                         }
181                         break;
182                 case T_OR:
183                 case T_XOR:
184                 case T_AND:
185                 case T_MINUS:
186                 case T_MUL:
187                 case T_DIV:
188                 case T_MOD:
189                 default:
190                         if (l.v_type == V_STR || r.v_type == V_STR) {
191                                 p_error("%s: Numeric operands required.",
192                                         opname);
193                                 flag = 0;
194                         }
195                 }
196                 if (!flag) {
197                         val_free(l);
198                         val_free(r);
199                         v->v_type = V_ERR;
200                         if (p_abort())
201                                 return -1;
202                         continue;
203                 }
204
205                 v->v_type = V_NUM;
206                 switch (op) {
207                 case T_EQ:
208                 case T_NE:
209                 case T_LT:
210                 case T_LE:
211                 case T_GT:
212                 case T_GE:
213                         if (l.v_type == V_STR) {
214                                 int tmp = strcmp(l.v_str, r.v_str);
215                                 str_free(l.v_str);
216                                 str_free(r.v_str);
217                                 l.v_type = V_NUM;
218                                 l.v_num = tmp;
219                                 r.v_type = V_NUM;
220                                 r.v_num = 0;
221                         }
222                         break;
223                 }
224                 switch (op) {
225                 case T_OR:
226                         v->v_num = l.v_num | r.v_num;
227                         break;
228                 case T_XOR:
229                         v->v_num = l.v_num ^ r.v_num;
230                         break;
231                 case T_AND:
232                         v->v_num = l.v_num & r.v_num;
233                         break;
234                 case T_EQ:
235                         v->v_num = l.v_num == r.v_num;
236                         break;
237                 case T_NE:
238                         v->v_num = l.v_num != r.v_num;
239                         break;
240                 case T_LT:
241                         v->v_num = l.v_num < r.v_num;
242                         break;
243                 case T_LE:
244                         v->v_num = l.v_num <= r.v_num;
245                         break;
246                 case T_GT:
247                         v->v_num = l.v_num > r.v_num;
248                         break;
249                 case T_GE:
250                         v->v_num = l.v_num >= r.v_num;
251                         break;
252                 case T_LS:
253                         if (l.v_type == V_STR) {
254                                 int i;
255                                 if ((i = strlen(l.v_str)) > r.v_num)
256                                         i = r.v_num;
257                                 v->v_str = str_ncpy(l.v_str, i);
258                                 v->v_type = V_STR;
259                         } else
260                                 v->v_num = l.v_num << r.v_num;
261                         break;
262                 case T_RS:
263                         if (l.v_type == V_STR) {
264                                 int i;
265                                 if ((i = strlen(l.v_str)) > r.v_num)
266                                         i -= r.v_num;
267                                 else
268                                         i = 0;
269                                 v->v_str = str_cpy(l.v_str + i);
270                                 v->v_type = V_STR;
271                         } else
272                                 v->v_num = l.v_num >> r.v_num;
273                         break;
274                 case T_PLUS:
275                         if (l.v_type == V_STR) {
276                                 v->v_str = str_cat(l.v_str, r.v_str);
277                                 v->v_type = V_STR;
278                         } else
279                                 v->v_num = l.v_num + r.v_num;
280                         break;
281                 case T_MINUS:
282                         v->v_num = l.v_num - r.v_num;
283                         break;
284                 case T_MUL:
285                         v->v_num = l.v_num * r.v_num;
286                         break;
287                 case T_DIV:
288                         v->v_num = l.v_num / r.v_num;
289                         break;
290                 case T_MOD:
291                         v->v_num = l.v_num % r.v_num;
292                         break;
293                 }
294                 val_free(l);
295                 val_free(r);
296         }
297         /*NOTREACHED*/
298 }