1 /* $Header: /src/pub/tcsh/sh.exp.c,v 3.45 2005/01/18 20:24:50 christos Exp $ */
3 * sh.exp.c: Expression evaluations
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. 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
35 RCSID("$Id: sh.exp.c,v 3.45 2005/01/18 20:24:50 christos Exp $")
43 #define TEXP_IGNORE 1 /* in ignore, it means to ignore value, just parse */
44 #define TEXP_NOGLOB 2 /* in ignore, it means not to globone */
60 static int sh_access __P((Char *, int));
61 static int exp1 __P((Char ***, int));
62 static int exp2x __P((Char ***, int));
63 static int exp2a __P((Char ***, int));
64 static int exp2b __P((Char ***, int));
65 static int exp2c __P((Char ***, int));
66 static Char *exp3 __P((Char ***, int));
67 static Char *exp3a __P((Char ***, int));
68 static Char *exp4 __P((Char ***, int));
69 static Char *exp5 __P((Char ***, int));
70 static Char *exp6 __P((Char ***, int));
71 static void evalav __P((Char **));
72 static int isa __P((Char *, int));
73 static int egetn __P((Char *));
77 static void etracc __P((char *, Char *, Char ***));
78 static void etraci __P((char *, int, Char ***));
83 * shell access function according to POSIX and non POSIX
84 * From Beto Appleton (beto@aixwiz.aix.ibm.com)
87 sh_access(fname, mode)
91 #if defined(POSIX) && !defined(USE_ACCESS)
94 char *name = short2str(fname);
99 #if !defined(POSIX) || defined(USE_ACCESS)
100 return access(name, mode);
105 * -r file True if file exists and is readable.
106 * -w file True if file exists and is writable.
107 * True shall indicate only that the write flag is on.
108 * The file shall not be writable on a read-only file
109 * system even if this test indicates true.
110 * -x file True if file exists and is executable.
111 * True shall indicate only that the execute flag is on.
112 * If file is a directory, true indicates that the file
115 if (mode != W_OK && mode != X_OK)
116 return access(name, mode);
118 if (stat(name, &statb) == -1)
121 if (access(name, mode) == 0) {
123 if (S_ISDIR(statb.st_mode) && mode == X_OK)
127 /* root needs permission for someone */
130 mode = S_IWUSR | S_IWGRP | S_IWOTH;
133 mode = S_IXUSR | S_IXGRP | S_IXOTH;
142 else if (euid == statb.st_uid)
145 else if (egid == statb.st_gid)
150 /* you can be in several groups */
155 * Try these things to find a positive maximum groups value:
156 * 1) sysconf(_SC_NGROUPS_MAX)
158 * 3) getgroups(0, unused)
159 * Then allocate and scan the groups array if one of these worked.
161 # if defined (HAVE_SYSCONF) && defined (_SC_NGROUPS_MAX)
162 if ((n = sysconf(_SC_NGROUPS_MAX)) == -1)
163 # endif /* _SC_NGROUPS_MAX */
166 n = getgroups(0, (GETGROUPS_T *) NULL);
169 groups = xmalloc((size_t) (n * sizeof(*groups)));
170 n = getgroups((int) n, groups);
172 if (groups[n] == statb.st_gid) {
178 # endif /* NGROUPS_MAX */
180 if (statb.st_mode & mode)
191 return (exp0(vp, 0));
199 int p1 = exp1(vp, ignore);
202 etraci("exp0 p1", p1, vp);
204 if (**vp && eq(**vp, STRor2)) {
208 p2 = exp0(vp, (ignore & TEXP_IGNORE) || p1);
210 etraci("exp0 p2", p2, vp);
222 int p1 = exp2x(vp, ignore);
225 etraci("exp1 p1", p1, vp);
227 if (**vp && eq(**vp, STRand2)) {
231 p2 = exp1(vp, (ignore & TEXP_IGNORE) || !p1);
233 etraci("exp1 p2", p2, vp);
245 int p1 = exp2a(vp, ignore);
248 etraci("exp3 p1", p1, vp);
250 if (**vp && eq(**vp, STRor)) {
254 p2 = exp2x(vp, ignore);
256 etraci("exp3 p2", p2, vp);
268 int p1 = exp2b(vp, ignore);
271 etraci("exp2a p1", p1, vp);
273 if (**vp && eq(**vp, STRcaret)) {
277 p2 = exp2a(vp, ignore);
279 etraci("exp2a p2", p2, vp);
291 int p1 = exp2c(vp, ignore);
294 etraci("exp2b p1", p1, vp);
296 if (**vp && eq(**vp, STRand)) {
300 p2 = exp2b(vp, ignore);
302 etraci("exp2b p2", p2, vp);
314 Char *p1 = exp3(vp, ignore);
319 etracc("exp2c p1", p1, vp);
321 if ((i = isa(**vp, EQOP)) != 0) {
323 if (i == EQMATCH || i == NOTEQMATCH)
324 ignore |= TEXP_NOGLOB;
325 p2 = exp3(vp, ignore);
327 etracc("exp2c p2", p2, vp);
329 if (!(ignore & TEXP_IGNORE))
365 p1 = exp3a(vp, ignore);
367 etracc("exp3 p1", p1, vp);
369 if ((i = isa(**vp, RELOP)) != 0) {
371 if (**vp && eq(**vp, STRequal))
373 p2 = exp3(vp, ignore);
375 etracc("exp3 p2", p2, vp);
377 if (!(ignore & TEXP_IGNORE))
381 i = egetn(p1) > egetn(p2);
385 i = egetn(p1) >= egetn(p2);
389 i = egetn(p1) < egetn(p2);
393 i = egetn(p1) <= egetn(p2);
411 p1 = exp4(vp, ignore);
413 etracc("exp3a p1", p1, vp);
416 if (op && any("<>", op[0]) && op[0] == op[1]) {
418 p2 = exp3a(vp, ignore);
420 etracc("exp3a p2", p2, vp);
423 i = egetn(p1) << egetn(p2);
425 i = egetn(p1) >> egetn(p2);
441 p1 = exp5(vp, ignore);
443 etracc("exp4 p1", p1, vp);
445 if (isa(**vp, ADDOP)) {
448 p2 = exp4(vp, ignore);
450 etracc("exp4 p2", p2, vp);
452 if (!(ignore & TEXP_IGNORE))
456 i = egetn(p1) + egetn(p2);
460 i = egetn(p1) - egetn(p2);
478 p1 = exp6(vp, ignore);
480 etracc("exp5 p1", p1, vp);
483 if (isa(**vp, MULOP)) {
485 if ((ignore & TEXP_NOGLOB) != 0)
487 * We are just trying to get the right side of
488 * a =~ or !~ operator
492 p2 = exp5(vp, ignore);
494 etracc("exp5 p2", p2, vp);
496 if (!(ignore & TEXP_IGNORE))
500 i = egetn(p1) * egetn(p2);
533 stderror(ERR_NAME | ERR_EXPRESSION);
534 if (eq(**vp, STRbang)) {
536 cp = exp6(vp, ignore);
538 etracc("exp6 ! cp", cp, vp);
544 if (eq(**vp, STRtilde)) {
546 cp = exp6(vp, ignore);
548 etracc("exp6 ~ cp", cp, vp);
554 if (eq(**vp, STRLparen)) {
556 ccode = exp0(vp, ignore);
558 etraci("exp6 () ccode", ccode, vp);
560 if (*vp == 0 || **vp == 0 || ***vp != ')')
561 stderror(ERR_NAME | ERR_EXPRESSION);
563 return (putn(ccode));
565 if (eq(**vp, STRLbrace)) {
567 struct command faket;
570 faket.t_dtyp = NODE_COMMAND;
571 faket.t_dflg = F_BACKQ;
572 faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
573 faket.t_dcom = fakecom;
574 fakecom[0] = STRfakecom;
580 stderror(ERR_NAME | ERR_MISSING, '}');
581 if (eq(*(*vp)++, STRRbrace))
584 if (ignore & TEXP_IGNORE)
585 return (Strsave(STRNULL));
587 if (pfork(&faket, -1) == 0) {
595 etraci("exp6 {} status", egetn(varval(STRstatus)), vp);
597 return (putn(egetn(varval(STRstatus)) == 0));
599 if (isa(**vp, ANYOP))
600 return (Strsave(STRNULL));
603 # define FILETESTS "erwxfdzoplstSXLbcugkmKR"
605 # define FILETESTS "erwxfdzoplstSXLbcugkmK"
607 #define FILEVALS "ZAMCDIUGNFPL"
608 if (*cp == '-' && (any(FILETESTS, cp[1]) || any(FILEVALS, cp[1])))
609 return(filetest(cp, vp, ignore));
611 etracc("exp6 default", cp, vp);
613 return (ignore & TEXP_NOGLOB ? Strsave(cp) : globone(cp, G_APPEND));
618 * Extended file tests
619 * From: John Rowe <rowe@excc.exeter.ac.uk>
622 filetest(cp, vp, ignore)
627 struct cvxstat stb, *st = NULL;
628 # define TCSH_STAT stat64
630 # define TCSH_STAT stat
631 struct stat stb, *st = NULL;
636 struct cvxstat lstb, *lst = NULL;
637 # define TCSH_LSTAT lstat64
639 # define TCSH_LSTAT lstat
640 struct stat lstb, *lst = NULL;
646 unsigned pmask = 0xffff;
648 Char *ft = cp, *dp, *ep, *strdev, *strino, *strF, *str, valtest = '\0',
650 char *string, string0[8];
655 while(any(FILETESTS, *++ft))
658 if (!*ft && *(ft - 1) == 'L')
661 if (any(FILEVALS, *ft)) {
664 * Value tests return '-1' on failure as 0 is
665 * a legitimate value for many of them.
666 * 'F' returns ':' for compatibility.
668 errval = valtest == 'F' ? STRcolon : STRminus1;
670 if (valtest == 'P' && *ft >= '0' && *ft <= '7') {
671 pmask = (char) *ft - '0';
672 while ( *++ft >= '0' && *ft <= '7' )
673 pmask = 8 * pmask + ((char) *ft - '0');
675 if (Strcmp(ft, STRcolon) == 0 && any("AMCUGP", valtest)) {
681 if (*ft || ft == cp + 1)
682 stderror(ERR_NAME | ERR_FILEINQ);
685 * Detect missing file names by checking for operator in the file name
686 * position. However, if an operator name appears there, we must make
687 * sure that there's no file by that name (e.g., "/") before announcing
688 * an error. Even this check isn't quite right, since it doesn't take
689 * globbing into account.
692 if (isa(**vp, ANYOP) && TCSH_STAT(short2str(**vp), &stb))
693 stderror(ERR_NAME | ERR_FILENAME);
696 if (ignore & TEXP_IGNORE)
697 return (Strsave(STRNULL));
698 ep = globone(dp, G_APPEND);
704 i = !sh_access(ep, R_OK);
708 i = !sh_access(ep, W_OK);
712 i = !sh_access(ep, X_OK);
715 case 'X': /* tcsh extension, name is an executable in the path
716 * or a tcsh builtin command
721 case 't': /* SGI extension, true when file is a tty */
722 i = isatty(atoi(short2str(ep)));
728 if (tolower(*ft) == 'l') {
730 * avoid convex compiler bug.
734 if (TCSH_LSTAT(short2str(ep), lst) == -1) {
736 return (Strsave(errval));
745 * avoid convex compiler bug.
749 if (TCSH_STAT(short2str(ep), st) == -1) {
751 return (Strsave(errval));
759 i = S_ISREG(st->st_mode);
767 i = S_ISDIR(st->st_mode);
775 i = S_ISFIFO(st->st_mode);
776 #else /* !S_ISFIFO */
778 #endif /* S_ISFIFO */
783 i = S_ISOFL(st->st_dm_mode);
800 i = S_ISLNK(lst->st_mode);
808 i = S_ISSOCK(st->st_mode);
809 # else /* !S_ISSOCK */
811 # endif /* S_ISSOCK */
816 i = S_ISBLK(st->st_mode);
824 i = S_ISCHR(st->st_mode);
831 i = (S_ISUID & st->st_mode) != 0;
835 i = (S_ISGID & st->st_mode) != 0;
839 i = (S_ISVTX & st->st_mode) != 0;
843 i = st->st_size == 0;
848 i = (stb.st_dmonflags & IMIGRATED) == IMIGRATED;
853 i = stb.st_size != 0;
861 i = st->st_uid == uid;
865 * Value operators are a tcsh extension.
869 i = (int) st->st_dev;
873 i = (int) st->st_ino;
877 strdev = putn( (int) st->st_dev);
878 strino = putn( (int) st->st_ino);
879 strF = (Char *) xmalloc((size_t) (2 + Strlen(strdev) +
880 Strlen(strino)) * sizeof(Char));
881 (void) Strcat(Strcat(Strcpy(strF, strdev), STRcolon), strino);
882 xfree((ptr_t) strdev);
883 xfree((ptr_t) strino);
893 filnam = short2str(ep);
895 # define MY_PATH_MAX PATH_MAX
896 #else /* !PATH_MAX */
898 * I can't think of any more sensible alterative; readlink doesn't give
899 * us an errno if the buffer isn't large enough :-(
901 # define MY_PATH_MAX 2048
902 #endif /* PATH_MAX */
903 i = readlink(filnam, string = (char *)
904 xmalloc((size_t) (1 + MY_PATH_MAX) * sizeof(char)),
906 if (i >= 0 && i <= MY_PATH_MAX)
907 string[i] = '\0'; /* readlink does not null terminate */
908 strF = (i < 0) ? errval : str2short(string);
909 xfree((ptr_t) string);
911 return(Strsave(strF));
920 i = (int) st->st_nlink;
924 string = string0 + 1;
925 (void) xsnprintf(string, sizeof(string0) - 1, "%o",
926 pmask & (unsigned int)
927 ((S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID) & st->st_mode));
928 if (altout && *string != '0')
931 return(Strsave(str2short(string)));
934 if (altout && (pw = getpwuid(st->st_uid))) {
936 return(Strsave(str2short(pw->pw_name)));
938 i = (int) st->st_uid;
942 if ( altout && (gr = getgrgid(st->st_gid))) {
944 return(Strsave(str2short(gr->gr_name)));
946 i = (int) st->st_gid;
950 i = (int) st->st_size;
953 case 'A': case 'M': case 'C':
954 footime = *ft == 'A' ? st->st_atime :
955 *ft == 'M' ? st->st_mtime : st->st_ctime;
957 strF = str2short(ctime(&footime));
958 if ((str = Strchr(strF, '\n')) != NULL)
961 return(Strsave(strF));
970 etraci("exp6 -? i", i, vp);
981 struct wordent paraml1;
982 struct wordent *hp = ¶ml1;
984 struct wordent *wdp = hp;
986 set(STRstatus, Strsave(STR0), VAR_READWRITE);
987 hp->prev = hp->next = hp;
990 struct wordent *new =
991 (struct wordent *) xcalloc(1, sizeof *wdp);
997 wdp->word = Strsave(*v++);
1001 t = syntax(paraml1.next, ¶ml1, 0);
1004 execute(t, -1, NULL, NULL, TRUE);
1005 freelex(¶ml1), freesyn(t);
1014 return ((what & RESTOP) != 0);
1018 if (what & ADDOP && (*cp == '+' || *cp == '-'))
1020 if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
1022 if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
1023 *cp == '~' || *cp == '^' || *cp == '"'))
1026 else if (cp[2] == 0) {
1027 if (what & RESTOP) {
1028 if (cp[0] == '|' && cp[1] == '&')
1030 if (cp[0] == '<' && cp[1] == '<')
1032 if (cp[0] == '>' && cp[1] == '>')
1042 else if (cp[0] == '!') {
1046 return (NOTEQMATCH);
1063 if (*cp && *cp != '-' && !Isdigit(*cp))
1064 stderror(ERR_NAME | ERR_EXPRESSION);
1077 xprintf("%s=%d\t", str, i);
1087 xprintf("%s=%s\t", str, cp);