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