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.4 1997/02/22 14:01:52 peter Exp $";
43 #include <sys/types.h>
49 #endif /* SHORT_STRINGS */
59 #define IGNORE 1 /* in ignore, it means to ignore value, just parse */
60 #define NOGLOB 2 /* in ignore, it means not to globone */
76 static int exp1 __P((Char ***, bool));
77 static int exp2 __P((Char ***, bool));
78 static int exp2a __P((Char ***, bool));
79 static int exp2b __P((Char ***, bool));
80 static int exp2c __P((Char ***, bool));
81 static Char * exp3 __P((Char ***, bool));
82 static Char * exp3a __P((Char ***, bool));
83 static Char * exp4 __P((Char ***, bool));
84 static Char * exp5 __P((Char ***, bool));
85 static Char * exp6 __P((Char ***, bool));
86 static void evalav __P((Char **));
87 static int isa __P((Char *, int));
88 static int egetn __P((Char *));
91 static void etracc __P((char *, Char *, Char ***));
92 static void etraci __P((char *, int, Char ***));
107 int p1 = exp1(vp, ignore);
110 etraci("exp0 p1", p1, vp);
112 if (**vp && eq(**vp, STRor2)) {
116 p2 = exp0(vp, (ignore & IGNORE) || p1);
118 etraci("exp0 p2", p2, vp);
130 int p1 = exp2(vp, ignore);
133 etraci("exp1 p1", p1, vp);
135 if (**vp && eq(**vp, STRand2)) {
139 p2 = exp1(vp, (ignore & IGNORE) || !p1);
141 etraci("exp1 p2", p2, vp);
153 int p1 = exp2a(vp, ignore);
156 etraci("exp3 p1", p1, vp);
158 if (**vp && eq(**vp, STRor)) {
162 p2 = exp2(vp, ignore);
164 etraci("exp3 p2", p2, vp);
176 int p1 = exp2b(vp, ignore);
179 etraci("exp2a p1", p1, vp);
181 if (**vp && eq(**vp, STRcaret)) {
185 p2 = exp2a(vp, ignore);
187 etraci("exp2a p2", p2, vp);
199 int p1 = exp2c(vp, ignore);
202 etraci("exp2b p1", p1, vp);
204 if (**vp && eq(**vp, STRand)) {
208 p2 = exp2b(vp, ignore);
210 etraci("exp2b p2", p2, vp);
222 Char *p1 = exp3(vp, ignore);
227 etracc("exp2c p1", p1, vp);
229 if ((i = isa(**vp, EQOP)) != 0) {
231 if (i == EQMATCH || i == NOTEQMATCH)
233 p2 = exp3(vp, ignore);
235 etracc("exp2c p2", p2, vp);
237 if (!(ignore & IGNORE))
273 p1 = exp3a(vp, ignore);
275 etracc("exp3 p1", p1, vp);
277 if ((i = isa(**vp, RELOP)) != 0) {
279 if (**vp && eq(**vp, STRequal))
281 p2 = exp3(vp, ignore);
283 etracc("exp3 p2", p2, vp);
285 if (!(ignore & IGNORE))
289 i = egetn(p1) > egetn(p2);
293 i = egetn(p1) >= egetn(p2);
297 i = egetn(p1) < egetn(p2);
301 i = egetn(p1) <= egetn(p2);
319 p1 = exp4(vp, ignore);
321 etracc("exp3a p1", p1, vp);
324 if (op && any("<>", op[0]) && op[0] == op[1]) {
326 p2 = exp3a(vp, ignore);
328 etracc("exp3a p2", p2, vp);
331 i = egetn(p1) << egetn(p2);
333 i = egetn(p1) >> egetn(p2);
349 p1 = exp5(vp, ignore);
351 etracc("exp4 p1", p1, vp);
353 if (isa(**vp, ADDOP)) {
356 p2 = exp4(vp, ignore);
358 etracc("exp4 p2", p2, vp);
360 if (!(ignore & IGNORE))
364 i = egetn(p1) + egetn(p2);
368 i = egetn(p1) - egetn(p2);
386 p1 = exp6(vp, ignore);
388 etracc("exp5 p1", p1, vp);
390 if (isa(**vp, MULOP)) {
393 p2 = exp5(vp, ignore);
395 etracc("exp5 p2", p2, vp);
397 if (!(ignore & IGNORE))
401 i = egetn(p1) * egetn(p2);
434 stderror(ERR_NAME | ERR_EXPRESSION);
435 if (eq(**vp, STRbang)) {
437 cp = exp6(vp, ignore);
439 etracc("exp6 ! cp", cp, vp);
445 if (eq(**vp, STRtilde)) {
447 cp = exp6(vp, ignore);
449 etracc("exp6 ~ cp", cp, vp);
455 if (eq(**vp, STRLparen)) {
457 ccode = exp0(vp, ignore);
459 etraci("exp6 () ccode", ccode, vp);
461 if (*vp == 0 || **vp == 0 || ***vp != ')')
462 stderror(ERR_NAME | ERR_EXPRESSION);
464 return (putn(ccode));
466 if (eq(**vp, STRLbrace)) {
468 struct command faket;
471 faket.t_dtyp = NODE_COMMAND;
473 faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
474 faket.t_dcom = fakecom;
475 fakecom[0] = STRfakecom;
481 stderror(ERR_NAME | ERR_MISSING, '}');
482 if (eq(*(*vp)++, STRRbrace))
486 return (Strsave(STRNULL));
488 if (pfork(&faket, -1) == 0) {
496 etraci("exp6 {} status", egetn(value(STRstatus)), vp);
498 return (putn(egetn(value(STRstatus)) == 0));
500 if (isa(**vp, ANYOP))
501 return (Strsave(STRNULL));
503 if (*cp == '-' && any("erwxfdzopls", cp[1])) {
507 stderror(ERR_NAME | ERR_FILEINQ);
509 * Detect missing file names by checking for operator in the file name
510 * position. However, if an operator name appears there, we must make
511 * sure that there's no file by that name (e.g., "/") before announcing
512 * an error. Even this check isn't quite right, since it doesn't take
513 * globbing into account.
515 if (isa(**vp, ANYOP) && stat(short2str(**vp), &stb))
516 stderror(ERR_NAME | ERR_FILENAME);
520 return (Strsave(STRNULL));
521 ep = globone(dp, G_ERROR);
525 i = !access(short2str(ep), R_OK);
529 i = !access(short2str(ep), W_OK);
533 i = !access(short2str(ep), X_OK);
539 cp[1] == 'l' ? lstat(short2str(ep), &stb) :
541 stat(short2str(ep), &stb)) {
543 return (Strsave(STR0));
548 i = S_ISREG(stb.st_mode);
552 i = S_ISDIR(stb.st_mode);
557 i = S_ISFIFO(stb.st_mode);
565 i = S_ISLNK(stb.st_mode);
573 i = S_ISSOCK(stb.st_mode);
580 i = stb.st_size == 0;
588 i = stb.st_uid == uid;
593 etraci("exp6 -? i", i, vp);
599 etracc("exp6 default", cp, vp);
601 return (ignore & NOGLOB ? Strsave(cp) : globone(cp, G_ERROR));
608 struct wordent paraml1;
609 struct wordent *hp = ¶ml1;
611 struct wordent *wdp = hp;
613 set(STRstatus, Strsave(STR0));
614 hp->prev = hp->next = hp;
617 struct wordent *new =
618 (struct wordent *) xcalloc(1, sizeof *wdp);
624 wdp->word = Strsave(*v++);
628 t = syntax(paraml1.next, ¶ml1, 0);
631 execute(t, -1, NULL, NULL);
632 freelex(¶ml1), freesyn(t);
641 return ((what & RESTOP) != 0);
643 if (what & ADDOP && (*cp == '+' || *cp == '-'))
645 if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
647 if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
648 *cp == '~' || *cp == '^' || *cp == '"'))
651 else if (cp[2] == 0) {
653 if (cp[0] == '|' && cp[1] == '&')
655 if (cp[0] == '<' && cp[1] == '<')
657 if (cp[0] == '>' && cp[1] == '>')
667 else if (cp[0] == '!') {
688 if (*cp && *cp != '-' && !Isdigit(*cp))
689 stderror(ERR_NAME | ERR_EXPRESSION);
702 (void) fprintf(csherr, "%s=%d\t", str, i);
704 (void) fprintf(csherr, "\n");
712 (void) fprintf(csherr, "%s=%s\t", str, vis_str(cp));
714 (void) fprintf(csherr, "\n");