2 * Copyright (c) 1980, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)exp.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
39 "$Id: exp.c,v 1.6 1998/05/06 06:50:44 charnier Exp $";
43 #include <sys/types.h>
55 #define IGNORE 1 /* in ignore, it means to ignore value, just parse */
56 #define NOGLOB 2 /* in ignore, it means not to globone */
72 static int exp1 __P((Char ***, bool));
73 static int exp2 __P((Char ***, bool));
74 static int exp2a __P((Char ***, bool));
75 static int exp2b __P((Char ***, bool));
76 static int exp2c __P((Char ***, bool));
77 static Char * exp3 __P((Char ***, bool));
78 static Char * exp3a __P((Char ***, bool));
79 static Char * exp4 __P((Char ***, bool));
80 static Char * exp5 __P((Char ***, bool));
81 static Char * exp6 __P((Char ***, bool));
82 static void evalav __P((Char **));
83 static int isa __P((Char *, int));
84 static int egetn __P((Char *));
87 static void etracc __P((char *, Char *, Char ***));
88 static void etraci __P((char *, int, Char ***));
103 int p1 = exp1(vp, ignore);
106 etraci("exp0 p1", p1, vp);
108 if (**vp && eq(**vp, STRor2)) {
112 p2 = exp0(vp, (ignore & IGNORE) || p1);
114 etraci("exp0 p2", p2, vp);
126 int p1 = exp2(vp, ignore);
129 etraci("exp1 p1", p1, vp);
131 if (**vp && eq(**vp, STRand2)) {
135 p2 = exp1(vp, (ignore & IGNORE) || !p1);
137 etraci("exp1 p2", p2, vp);
149 int p1 = exp2a(vp, ignore);
152 etraci("exp3 p1", p1, vp);
154 if (**vp && eq(**vp, STRor)) {
158 p2 = exp2(vp, ignore);
160 etraci("exp3 p2", p2, vp);
172 int p1 = exp2b(vp, ignore);
175 etraci("exp2a p1", p1, vp);
177 if (**vp && eq(**vp, STRcaret)) {
181 p2 = exp2a(vp, ignore);
183 etraci("exp2a p2", p2, vp);
195 int p1 = exp2c(vp, ignore);
198 etraci("exp2b p1", p1, vp);
200 if (**vp && eq(**vp, STRand)) {
204 p2 = exp2b(vp, ignore);
206 etraci("exp2b p2", p2, vp);
218 Char *p1 = exp3(vp, ignore);
223 etracc("exp2c p1", p1, vp);
225 if ((i = isa(**vp, EQOP)) != 0) {
227 if (i == EQMATCH || i == NOTEQMATCH)
229 p2 = exp3(vp, ignore);
231 etracc("exp2c p2", p2, vp);
233 if (!(ignore & IGNORE))
269 p1 = exp3a(vp, ignore);
271 etracc("exp3 p1", p1, vp);
273 if ((i = isa(**vp, RELOP)) != 0) {
275 if (**vp && eq(**vp, STRequal))
277 p2 = exp3(vp, ignore);
279 etracc("exp3 p2", p2, vp);
281 if (!(ignore & IGNORE))
285 i = egetn(p1) > egetn(p2);
289 i = egetn(p1) >= egetn(p2);
293 i = egetn(p1) < egetn(p2);
297 i = egetn(p1) <= egetn(p2);
315 p1 = exp4(vp, ignore);
317 etracc("exp3a p1", p1, vp);
320 if (op && any("<>", op[0]) && op[0] == op[1]) {
322 p2 = exp3a(vp, ignore);
324 etracc("exp3a p2", p2, vp);
327 i = egetn(p1) << egetn(p2);
329 i = egetn(p1) >> egetn(p2);
345 p1 = exp5(vp, ignore);
347 etracc("exp4 p1", p1, vp);
349 if (isa(**vp, ADDOP)) {
352 p2 = exp4(vp, ignore);
354 etracc("exp4 p2", p2, vp);
356 if (!(ignore & IGNORE))
360 i = egetn(p1) + egetn(p2);
364 i = egetn(p1) - egetn(p2);
382 p1 = exp6(vp, ignore);
384 etracc("exp5 p1", p1, vp);
386 if (isa(**vp, MULOP)) {
389 p2 = exp5(vp, ignore);
391 etracc("exp5 p2", p2, vp);
393 if (!(ignore & IGNORE))
397 i = egetn(p1) * egetn(p2);
430 stderror(ERR_NAME | ERR_EXPRESSION);
431 if (eq(**vp, STRbang)) {
433 cp = exp6(vp, ignore);
435 etracc("exp6 ! cp", cp, vp);
441 if (eq(**vp, STRtilde)) {
443 cp = exp6(vp, ignore);
445 etracc("exp6 ~ cp", cp, vp);
451 if (eq(**vp, STRLparen)) {
453 ccode = exp0(vp, ignore);
455 etraci("exp6 () ccode", ccode, vp);
457 if (*vp == 0 || **vp == 0 || ***vp != ')')
458 stderror(ERR_NAME | ERR_EXPRESSION);
460 return (putn(ccode));
462 if (eq(**vp, STRLbrace)) {
464 struct command faket;
467 faket.t_dtyp = NODE_COMMAND;
469 faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
470 faket.t_dcom = fakecom;
471 fakecom[0] = STRfakecom;
477 stderror(ERR_NAME | ERR_MISSING, '}');
478 if (eq(*(*vp)++, STRRbrace))
482 return (Strsave(STRNULL));
484 if (pfork(&faket, -1) == 0) {
492 etraci("exp6 {} status", egetn(value(STRstatus)), vp);
494 return (putn(egetn(value(STRstatus)) == 0));
496 if (isa(**vp, ANYOP))
497 return (Strsave(STRNULL));
499 if (*cp == '-' && any("erwxfdzopls", cp[1])) {
503 stderror(ERR_NAME | ERR_FILEINQ);
505 * Detect missing file names by checking for operator in the file name
506 * position. However, if an operator name appears there, we must make
507 * sure that there's no file by that name (e.g., "/") before announcing
508 * an error. Even this check isn't quite right, since it doesn't take
509 * globbing into account.
511 if (isa(**vp, ANYOP) && stat(short2str(**vp), &stb))
512 stderror(ERR_NAME | ERR_FILENAME);
516 return (Strsave(STRNULL));
517 ep = globone(dp, G_ERROR);
521 i = !access(short2str(ep), R_OK);
525 i = !access(short2str(ep), W_OK);
529 i = !access(short2str(ep), X_OK);
535 cp[1] == 'l' ? lstat(short2str(ep), &stb) :
537 stat(short2str(ep), &stb)) {
539 return (Strsave(STR0));
544 i = S_ISREG(stb.st_mode);
548 i = S_ISDIR(stb.st_mode);
553 i = S_ISFIFO(stb.st_mode);
561 i = S_ISLNK(stb.st_mode);
569 i = S_ISSOCK(stb.st_mode);
576 i = stb.st_size == 0;
584 i = stb.st_uid == uid;
589 etraci("exp6 -? i", i, vp);
595 etracc("exp6 default", cp, vp);
597 return (ignore & NOGLOB ? Strsave(cp) : globone(cp, G_ERROR));
604 struct wordent paraml1;
605 struct wordent *hp = ¶ml1;
607 struct wordent *wdp = hp;
609 set(STRstatus, Strsave(STR0));
610 hp->prev = hp->next = hp;
613 struct wordent *new =
614 (struct wordent *) xcalloc(1, sizeof *wdp);
620 wdp->word = Strsave(*v++);
624 t = syntax(paraml1.next, ¶ml1, 0);
627 execute(t, -1, NULL, NULL);
628 freelex(¶ml1), freesyn(t);
637 return ((what & RESTOP) != 0);
639 if (what & ADDOP && (*cp == '+' || *cp == '-'))
641 if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
643 if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
644 *cp == '~' || *cp == '^' || *cp == '"'))
647 else if (cp[2] == 0) {
649 if (cp[0] == '|' && cp[1] == '&')
651 if (cp[0] == '<' && cp[1] == '<')
653 if (cp[0] == '>' && cp[1] == '>')
663 else if (cp[0] == '!') {
684 if (*cp && *cp != '-' && !Isdigit(*cp))
685 stderror(ERR_NAME | ERR_EXPRESSION);
698 (void) fprintf(csherr, "%s=%d\t", str, i);
700 (void) fprintf(csherr, "\n");
708 (void) fprintf(csherr, "%s=%s\t", str, vis_str(cp));
710 (void) fprintf(csherr, "\n");