Merge branch 'vendor/GMP' into gcc441
[dragonfly.git] / usr.bin / window / parser5.c
1 /*      $NetBSD: parser5.c,v 1.7 2009/04/14 08:50:06 lukem Exp $        */
2
3 /*
4  * Copyright (c) 1983, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Edward Wang at The University of California, Berkeley.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)parser5.c   8.1 (Berkeley) 6/6/93";
39 #else
40 __RCSID("$NetBSD: parser5.c,v 1.7 2009/04/14 08:50:06 lukem Exp $");
41 #endif
42 #endif /* not lint */
43
44 #include "defs.h"
45 #include "parser.h"
46 #include "var.h"
47
48 /*
49  * unary $ $? + - ! ~
50  */
51 int
52 p_expr11(struct value *v, char flag)
53 {
54         int op;
55         const char *opname;
56
57         switch (token) {
58         case T_DOLLAR:
59                 opname = "$";
60                 break;
61         case T_DQ:
62                 opname = "$?";
63                 break;
64         case T_PLUS:
65                 opname = "unary +";
66                 break;
67         case T_MINUS:
68                 opname = "unary -";
69                 break;
70         case T_NOT:
71                 opname = "!";
72                 break;
73         case T_COMP:
74                 opname = "~";
75                 break;
76         default:
77                 return p_expr12(v, flag);
78         }
79         op = token;
80         (void) s_gettok();
81         if (p_expr11(v, flag) < 0)
82                 return -1;
83         switch (v->v_type) {
84         case V_NUM:
85                 break;
86         case V_STR:
87                 switch (op) {
88                 case T_MINUS:
89                 case T_NOT:
90                 case T_COMP:
91                         p_error("%s: Numeric operand required.", opname);
92                         str_free(v->v_str);
93                         v->v_type = V_ERR;
94                         return 0;
95                 }
96                 break;
97         case V_ERR:
98                 return 0;
99         }
100         switch (op) {
101         case T_DOLLAR:
102         case T_DQ:
103                 if (v->v_type == V_NUM) {
104                         int tmp = cx.x_type == X_BUF && cx.x_arg != 0 &&
105                                 v->v_num > 0 && v->v_num <= cx.x_narg;
106                         if (op == T_DQ)
107                                 v->v_num = tmp;
108                         else if (tmp)
109                                 *v = cx.x_arg[v->v_num - 1];
110                         else {
111                                 p_error("%d: No such argument.", v->v_num);
112                                 v->v_type = V_ERR;
113                         }
114                 } else {
115                         char *name = v->v_str;
116                         struct var *r = var_lookup(name);
117                         if (op == T_DQ) {
118                                 v->v_type = V_NUM;
119                                 v->v_num = r != 0;
120                         } else if (r != 0)
121                                 *v = r->r_val;
122                         else {
123                                 p_error("%s: Undefined variable.", name);
124                                 v->v_type = V_ERR;
125                         }
126                         str_free(name);
127                 }
128                 if (v->v_type == V_STR && (v->v_str = str_cpy(v->v_str)) == 0) {
129                         p_memerror();
130                         return -1;
131                 }
132                 break;
133         case T_MINUS:
134                 v->v_num = - v->v_num;
135                 break;
136         case T_NOT:
137                 v->v_num = ! v->v_num;
138                 break;
139         case T_COMP:
140                 v->v_num = ~ v->v_num;
141                 break;
142         }
143         return 0;
144 }
145
146 /*
147  * string, number, ( expr )
148  * Plus function calls.
149  *
150  * Always return v_type == V_ERR when flag == 0.
151  */
152 int
153 p_expr12(struct value *v, char flag)
154 {
155         v->v_type = V_ERR;
156         switch (token) {
157         case T_NUM:
158                 if (flag) {
159                         v->v_type = V_NUM;
160                         v->v_num = token_num;
161                 }
162                 (void) s_gettok();
163                 break;
164         case T_STR:
165                 if (flag) {
166                         v->v_type = V_STR;
167                         v->v_str = token_str;
168                 } else
169                         str_free(token_str);
170                 (void) s_gettok();
171                 break;
172         case T_LP:
173                 (void) s_gettok();
174                 if (p_expr(v, flag) < 0) {
175                         p_synerror();
176                         return -1;
177                 }
178                 if (token != T_RP) {
179                         p_synerror();
180                         val_free(*v);
181                         return -1;
182                 }
183                 (void) s_gettok();
184                 break;
185         default:
186                 return -1;
187         }
188         while (token == T_LP) {
189                 char *cmd;
190
191                 if (p_convstr(v) < 0)
192                         return -1;
193                 cmd = v->v_type == V_STR ? v->v_str : 0;
194                 if (p_function(cmd, v, flag) < 0) {
195                         if (cmd)
196                                 str_free(cmd);
197                         return -1;
198                 }
199                 if (cmd)
200                         str_free(cmd);
201         }
202         return 0;
203 }