unstack - fix ipcvecs
[minix.git] / commands / ash / bltin / expr.c
blob88d8c05ccdfba35a69fafefa2b8a41169df6c26a
1 /*
2 * The expr and test commands.
4 * Copyright (C) 1989 by Kenneth Almquist. All rights reserved.
5 * This file is part of ash, which is distributed under the terms specified
6 * by the Ash General Public License. See the file named LICENSE.
7 */
10 #include "bltin.h"
11 #include "operators.h"
12 #include <regex.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <stdlib.h>
16 #include <unistd.h>
19 #define STACKSIZE 12
20 #define NESTINCR 16
22 /* data types */
23 #define STRING 0
24 #define INTEGER 1
25 #define BOOLEAN 2
29 * This structure hold a value. The type keyword specifies the type of
30 * the value, and the union u holds the value. The value of a boolean
31 * is stored in u.num (1 = TRUE, 0 = FALSE).
34 struct value {
35 int type;
36 union {
37 char *string;
38 long num;
39 } u;
43 struct operator {
44 short op; /* which operator */
45 short pri; /* priority of operator */
49 struct filestat {
50 char *name; /* name of file */
51 int rcode; /* return code from stat */
52 struct stat stat; /* status info on file */
56 extern char *match_begin[10]; /* matched string */
57 extern short match_length[10]; /* defined in regexp.c */
58 extern short number_parens; /* number of \( \) pairs */
61 #ifdef __STDC__
62 static int expr_is_false(struct value *);
63 static void expr_operator(int, struct value *, struct filestat *);
64 static int lookup_op(char *, char *const*);
65 #else
66 static int expr_is_false();
67 static void expr_operator();
68 static int lookup_op();
69 #endif
73 exprcmd(argc, argv) char **argv; {
74 char **ap;
75 char *opname;
76 char c;
77 char *p;
78 int print;
79 int nest; /* parenthises nesting */
80 int op;
81 int pri;
82 int skipping;
83 int binary;
84 struct operator opstack[STACKSIZE];
85 struct operator *opsp;
86 struct value valstack[STACKSIZE + 1];
87 struct value *valsp;
88 struct filestat fs;
90 INITARGS(argv);
91 c = **argv;
92 print = 1;
93 if (c == 't')
94 print = 0;
95 else if (c == '[') {
96 if (! equal(argv[argc - 1], "]"))
97 error("missing ]");
98 argv[argc - 1] = NULL;
99 print = 0;
101 ap = argv + 1;
102 fs.name = NULL;
105 * We use operator precedence parsing, evaluating the expression
106 * as we parse it. Parentheses are handled by bumping up the
107 * priority of operators using the variable "nest." We use the
108 * variable "skipping" to turn off evaluation temporarily for the
109 * short circuit boolean operators. (It is important do the short
110 * circuit evaluation because under NFS a stat operation can take
111 * infinitely long.)
114 nest = 0;
115 skipping = 0;
116 opsp = opstack + STACKSIZE;
117 valsp = valstack;
118 if (*ap == NULL) {
119 valstack[0].type = BOOLEAN;
120 valstack[0].u.num = 0;
121 goto done;
123 for (;;) {
124 opname = *ap++;
125 if (opname == NULL)
126 syntax: error("syntax error");
127 if (opname[0] == '(' && opname[1] == '\0') {
128 nest += NESTINCR;
129 continue;
130 } else if (*ap && (op = lookup_op(opname, unary_op)) >= 0) {
131 if (opsp == &opstack[0])
132 overflow: error("Expression too complex");
133 --opsp;
134 opsp->op = op;
135 opsp->pri = op_priority[op] + nest;
136 continue;
138 } else {
139 if (opname[0] == '\'') {
140 for (p = opname ; *++p != '\0' ; );
141 if (--p > opname && *p == '\'') {
142 *p = '\0';
143 opname++;
146 valsp->type = STRING;
147 valsp->u.string = opname;
148 valsp++;
150 for (;;) {
151 opname = *ap++;
152 if (opname == NULL) {
153 if (nest != 0)
154 goto syntax;
155 pri = 0;
156 break;
158 if (opname[0] != ')' || opname[1] != '\0') {
159 if ((op = lookup_op(opname, binary_op)) < 0)
160 goto syntax;
161 op += FIRST_BINARY_OP;
162 pri = op_priority[op] + nest;
163 break;
165 if ((nest -= NESTINCR) < 0)
166 goto syntax;
168 while (opsp < &opstack[STACKSIZE] && opsp->pri >= pri) {
169 binary = opsp->op;
170 for (;;) {
171 valsp--;
172 c = op_argflag[opsp->op];
173 if (c == OP_INT) {
174 if (valsp->type == STRING)
175 valsp->u.num = atol(valsp->u.string);
176 valsp->type = INTEGER;
177 } else if (c >= OP_STRING) { /* OP_STRING or OP_FILE */
178 if (valsp->type == INTEGER) {
179 p = stalloc(32);
180 #ifdef SHELL
181 fmtstr(p, 32, "%d", valsp->u.num);
182 #else
183 sprintf(p, "%d", valsp->u.num);
184 #endif
185 valsp->u.string = p;
186 } else if (valsp->type == BOOLEAN) {
187 if (valsp->u.num)
188 valsp->u.string = "true";
189 else
190 valsp->u.string = "";
192 valsp->type = STRING;
193 if (c == OP_FILE
194 && (fs.name == NULL
195 || ! equal(fs.name, valsp->u.string))) {
196 fs.name = valsp->u.string;
197 fs.rcode = stat(valsp->u.string, &fs.stat);
200 if (binary < FIRST_BINARY_OP)
201 break;
202 binary = 0;
204 if (! skipping)
205 expr_operator(opsp->op, valsp, &fs);
206 else if (opsp->op == AND1 || opsp->op == OR1)
207 skipping--;
208 valsp++; /* push value */
209 opsp++; /* pop operator */
211 if (opname == NULL)
212 break;
213 if (opsp == &opstack[0])
214 goto overflow;
215 if (op == AND1 || op == AND2) {
216 op = AND1;
217 if (skipping || expr_is_false(valsp - 1))
218 skipping++;
220 if (op == OR1 || op == OR2) {
221 op = OR1;
222 if (skipping || ! expr_is_false(valsp - 1))
223 skipping++;
225 opsp--;
226 opsp->op = op;
227 opsp->pri = pri;
229 done:
230 if (print) {
231 if (valstack[0].type == STRING)
232 printf("%s\n", valstack[0].u.string);
233 else if (valstack[0].type == INTEGER)
234 printf("%ld\n", valstack[0].u.num);
235 else if (valstack[0].u.num != 0)
236 printf("true\n");
238 return expr_is_false(&valstack[0]);
242 static int
243 expr_is_false(val)
244 struct value *val;
246 if (val->type == STRING) {
247 if (val->u.string[0] == '\0')
248 return 1;
249 } else { /* INTEGER or BOOLEAN */
250 if (val->u.num == 0)
251 return 1;
253 return 0;
258 * Execute an operator. Op is the operator. Sp is the stack pointer;
259 * sp[0] refers to the first operand, sp[1] refers to the second operand
260 * (if any), and the result is placed in sp[0]. The operands are converted
261 * to the type expected by the operator before expr_operator is called.
262 * Fs is a pointer to a structure which holds the value of the last call
263 * to stat, to avoid repeated stat calls on the same file.
266 static void
267 expr_operator(op, sp, fs)
268 int op;
269 struct value *sp;
270 struct filestat *fs;
272 int i, r;
273 struct stat st1, st2;
274 regex_t pat;
275 regmatch_t rm[2];
277 switch (op) {
278 case NOT:
279 sp->u.num = expr_is_false(sp);
280 sp->type = BOOLEAN;
281 break;
282 case EXISTS:
283 if (fs->rcode >= 0) goto true;
284 goto false;
285 case ISREAD:
286 i = 04;
287 goto permission;
288 case ISWRITE:
289 i = 02;
290 goto permission;
291 case ISEXEC:
292 i = 01;
293 permission:
294 if (fs->stat.st_uid == geteuid())
295 i <<= 6;
296 else if (fs->stat.st_gid == getegid())
297 i <<= 3;
298 goto filebit; /* true if (stat.st_mode & i) != 0 */
299 case ISFILE:
300 i = S_IFREG;
301 goto filetype;
302 case ISDIR:
303 i = S_IFDIR;
304 goto filetype;
305 case ISCHAR:
306 i = S_IFCHR;
307 goto filetype;
308 case ISBLOCK:
309 i = S_IFBLK;
310 goto filetype;
311 case ISFIFO:
312 #ifdef S_IFIFO
313 i = S_IFIFO;
314 goto filetype;
315 #else
316 goto false;
317 #endif
318 filetype:
319 if ((fs->stat.st_mode & S_IFMT) == i && fs->rcode >= 0) {
320 true:
321 sp->u.num = 1;
322 } else {
323 false:
324 sp->u.num = 0;
326 sp->type = BOOLEAN;
327 break;
328 case ISSETUID:
329 i = S_ISUID;
330 goto filebit;
331 case ISSETGID:
332 i = S_ISGID;
333 goto filebit;
334 case ISSTICKY:
335 i = S_ISVTX;
336 filebit:
337 if (fs->stat.st_mode & i && fs->rcode >= 0)
338 goto true;
339 goto false;
340 case ISSLINK:
341 if (lstat(fs->name, &st1) == -1)
342 goto false;
343 if (S_ISLNK(st1.st_mode))
344 goto true;
345 goto false;
346 case ISSIZE:
347 sp->u.num = fs->rcode >= 0? fs->stat.st_size : 0L;
348 sp->type = INTEGER;
349 break;
350 case OLDER:
351 case NEWER:
352 if (stat(sp->u.string, &st1) != 0) {
353 sp->u.num = 0;
354 } else if (stat((sp + 1)->u.string, &st2) != 0) {
355 sp->u.num = 1;
356 } else {
357 int isnewer = st1.st_mtime >= st2.st_mtime;
358 if(op == NEWER)
359 sp->u.num = isnewer;
360 else
361 sp->u.num = !isnewer;
363 sp->type = INTEGER;
364 break;
365 case ISTTY:
366 sp->u.num = isatty(sp->u.num);
367 sp->type = BOOLEAN;
368 break;
369 case NULSTR:
370 if (sp->u.string[0] == '\0')
371 goto true;
372 goto false;
373 case STRLEN:
374 sp->u.num = strlen(sp->u.string);
375 sp->type = INTEGER;
376 break;
377 case OR1:
378 case AND1:
380 * These operators are mostly handled by the parser. If we
381 * get here it means that both operands were evaluated, so
382 * the value is the value of the second operand.
384 *sp = *(sp + 1);
385 break;
386 case STREQ:
387 case STRNE:
388 i = 0;
389 if (equal(sp->u.string, (sp + 1)->u.string))
390 i++;
391 if (op == STRNE)
392 i = 1 - i;
393 sp->u.num = i;
394 sp->type = BOOLEAN;
395 break;
396 case EQ:
397 if (sp->u.num == (sp + 1)->u.num)
398 goto true;
399 goto false;
400 case NE:
401 if (sp->u.num != (sp + 1)->u.num)
402 goto true;
403 goto false;
404 case GT:
405 if (sp->u.num > (sp + 1)->u.num)
406 goto true;
407 goto false;
408 case LT:
409 if (sp->u.num < (sp + 1)->u.num)
410 goto true;
411 goto false;
412 case LE:
413 if (sp->u.num <= (sp + 1)->u.num)
414 goto true;
415 goto false;
416 case GE:
417 if (sp->u.num >= (sp + 1)->u.num)
418 goto true;
419 goto false;
420 case PLUS:
421 sp->u.num += (sp + 1)->u.num;
422 break;
423 case MINUS:
424 sp->u.num -= (sp + 1)->u.num;
425 break;
426 case TIMES:
427 sp->u.num *= (sp + 1)->u.num;
428 break;
429 case DIVIDE:
430 if ((sp + 1)->u.num == 0)
431 error("Division by zero");
432 sp->u.num /= (sp + 1)->u.num;
433 break;
434 case REM:
435 if ((sp + 1)->u.num == 0)
436 error("Division by zero");
437 sp->u.num %= (sp + 1)->u.num;
438 break;
439 case MATCHPAT:
441 r = regcomp(&pat, (sp + 1)->u.string, 0);
442 if (r)
443 error("Bad regular expression");
444 if (regexec(&pat, sp->u.string, 2, rm, 0) == 0 &&
445 rm[0].rm_so == 0)
447 if (pat.re_nsub > 0) {
448 sp->u.string[rm[1].rm_eo] = '\0';
449 sp->u.string = sp->u.string+rm[1].rm_so;
450 } else {
451 sp->u.num = rm[0].rm_eo;
452 sp->type = INTEGER;
454 } else {
455 if (pat.re_nsub > 0) {
456 sp->u.string[0] = '\0';
457 } else {
458 sp->u.num = 0;
459 sp->type = INTEGER;
463 break;
468 static int
469 lookup_op(name, table)
470 char *name;
471 char *const*table;
473 register char *const*tp;
474 register char const *p;
475 char c = name[1];
477 for (tp = table ; (p = *tp) != NULL ; tp++) {
478 if (p[1] == c && equal(p, name))
479 return tp - table;
481 return -1;