]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/window/parser5.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / window / parser5.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[] = "@(#)parser5.c   8.1 (Berkeley) 6/6/93";
39 static char rcsid[] =
40   "$FreeBSD$";
41 #endif /* not lint */
42
43 #include "parser.h"
44 #include "var.h"
45
46 /*
47  * unary $ $? + - ! ~
48  */
49 p_expr11(v, flag)
50 register struct value *v;
51 char flag;
52 {
53         int op;
54         char *opname;
55
56         switch (token) {
57         case T_DOLLAR:
58                 opname = "$";
59                 break;
60         case T_DQ:
61                 opname = "$?";
62                 break;
63         case T_PLUS:
64                 opname = "unary +";
65                 break;
66         case T_MINUS:
67                 opname = "unary -";
68                 break;
69         case T_NOT:
70                 opname = "!";
71                 break;
72         case T_COMP:
73                 opname = "~";
74                 break;
75         default:
76                 return p_expr12(v, flag);
77         }
78         op = token;
79         (void) s_gettok();
80         if (p_expr11(v, flag) < 0)
81                 return -1;
82         switch (v->v_type) {
83         case V_NUM:
84                 break;
85         case V_STR:
86                 switch (op) {
87                 case T_MINUS:
88                 case T_NOT:
89                 case T_COMP:
90                         p_error("%s: Numeric operand required.", opname);
91                         str_free(v->v_str);
92                         v->v_type = V_ERR;
93                         return 0;
94                 }
95                 break;
96         case V_ERR:
97                 return 0;
98         }
99         switch (op) {
100         case T_DOLLAR:
101         case T_DQ:
102                 if (v->v_type == V_NUM) {
103                         int tmp = cx.x_type == X_BUF && cx.x_arg != 0 &&
104                                 v->v_num > 0 && v->v_num <= cx.x_narg;
105                         if (op == T_DQ)
106                                 v->v_num = tmp;
107                         else if (tmp)
108                                 *v = cx.x_arg[v->v_num - 1];
109                         else {
110                                 p_error("%d: No such argument.", v->v_num);
111                                 v->v_type = V_ERR;
112                         }
113                 } else {
114                         char *name = v->v_str;
115                         struct var *r = var_lookup(name);
116                         if (op == T_DQ) {
117                                 v->v_type = V_NUM;
118                                 v->v_num = r != 0;
119                         } else if (r != 0)
120                                 *v = r->r_val;
121                         else {
122                                 p_error("%s: Undefined variable.", name);
123                                 v->v_type = V_ERR;
124                         }
125                         str_free(name);
126                 }
127                 if (v->v_type == V_STR && (v->v_str = str_cpy(v->v_str)) == 0) {
128                         p_memerror();
129                         return -1;
130                 }
131                 break;
132         case T_MINUS:
133                 v->v_num = - v->v_num;
134                 break;
135         case T_NOT:
136                 v->v_num = ! v->v_num;
137                 break;
138         case T_COMP:
139                 v->v_num = ~ v->v_num;
140                 break;
141         }
142         return 0;
143 }
144
145 /*
146  * string, number, ( expr )
147  * Plus function calls.
148  *
149  * Always return v_type == V_ERR when flag == 0.
150  */
151 p_expr12(v, flag)
152 register struct value *v;
153 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 }