1 /* $Header: /src/pub/tcsh/sh.exp.c,v 3.38 1998/11/24 18:17:32 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 RCSID("$Id: sh.exp.c,v 3.38 1998/11/24 18:17:32 christos Exp $")
45 #define TEXP_IGNORE 1 /* in ignore, it means to ignore value, just parse */
46 #define TEXP_NOGLOB 2 /* in ignore, it means not to globone */
62 static int sh_access __P((Char *, int));
63 static int exp1 __P((Char ***, bool));
64 static int exp2 __P((Char ***, bool));
65 static int exp2a __P((Char ***, bool));
66 static int exp2b __P((Char ***, bool));
67 static int exp2c __P((Char ***, bool));
68 static Char *exp3 __P((Char ***, bool));
69 static Char *exp3a __P((Char ***, bool));
70 static Char *exp4 __P((Char ***, bool));
71 static Char *exp5 __P((Char ***, bool));
72 static Char *exp6 __P((Char ***, bool));
73 static void evalav __P((Char **));
74 static int isa __P((Char *, int));
75 static int egetn __P((Char *));
79 static void etracc __P((char *, Char *, Char ***));
80 static void etraci __P((char *, int, Char ***));
85 * shell access function according to POSIX and non POSIX
86 * From Beto Appleton (beto@aixwiz.aix.ibm.com)
89 sh_access(fname, mode)
93 #if defined(POSIX) && !defined(USE_ACCESS)
96 char *name = short2str(fname);
101 #if !defined(POSIX) || defined(USE_ACCESS)
102 return access(name, mode);
107 * -r file True if file exists and is readable.
108 * -w file True if file exists and is writable.
109 * True shall indicate only that the write flag is on.
110 * The file shall not be writable on a read-only file
111 * system even if this test indicates true.
112 * -x file True if file exists and is executable.
113 * True shall indicate only that the execute flag is on.
114 * If file is a directory, true indicates that the file
117 if (mode != W_OK && mode != X_OK)
118 return access(name, mode);
120 if (stat(name, &statb) == -1)
123 if (access(name, mode) == 0) {
125 if (S_ISDIR(statb.st_mode) && mode == X_OK)
129 /* root needs permission for someone */
132 mode = S_IWUSR | S_IWGRP | S_IWOTH;
135 mode = S_IXUSR | S_IXGRP | S_IXOTH;
144 else if (euid == statb.st_uid)
147 else if (egid == statb.st_gid)
152 # if defined(__386BSD__) || defined(BSD4_4)
154 * These two decided that setgroup() should take an array of int's
155 * and they define _SC_NGROUPS_MAX without having sysconf
157 # undef _SC_NGROUPS_MAX
158 # if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__bsdi__)
165 # endif /* __386BSD__ || BSD4_4 */
166 /* you can be in several groups */
171 * Try these things to find a positive maximum groups value:
172 * 1) sysconf(_SC_NGROUPS_MAX)
174 * 3) getgroups(0, unused)
175 * Then allocate and scan the groups array if one of these worked.
177 # ifdef _SC_NGROUPS_MAX
178 if ((n = sysconf(_SC_NGROUPS_MAX)) == -1)
179 # endif /* _SC_NGROUPS_MAX */
182 n = getgroups(0, (GID_T *) NULL);
185 groups = (GID_T *) xmalloc((size_t) (n * sizeof(GID_T)));
186 n = getgroups((int) n, groups);
188 if (groups[n] == statb.st_gid) {
194 # endif /* NGROUPS_MAX */
196 if (statb.st_mode & mode)
207 return (exp0(vp, 0));
215 register int p1 = exp1(vp, ignore);
218 etraci("exp0 p1", p1, vp);
220 if (**vp && eq(**vp, STRor2)) {
224 p2 = exp0(vp, (ignore & TEXP_IGNORE) || p1);
226 etraci("exp0 p2", p2, vp);
238 register int p1 = exp2(vp, ignore);
241 etraci("exp1 p1", p1, vp);
243 if (**vp && eq(**vp, STRand2)) {
247 p2 = exp1(vp, (ignore & TEXP_IGNORE) || !p1);
249 etraci("exp1 p2", p2, vp);
261 register int p1 = exp2a(vp, ignore);
264 etraci("exp3 p1", p1, vp);
266 if (**vp && eq(**vp, STRor)) {
270 p2 = exp2(vp, ignore);
272 etraci("exp3 p2", p2, vp);
284 register int p1 = exp2b(vp, ignore);
287 etraci("exp2a p1", p1, vp);
289 if (**vp && eq(**vp, STRcaret)) {
293 p2 = exp2a(vp, ignore);
295 etraci("exp2a p2", p2, vp);
307 register int p1 = exp2c(vp, ignore);
310 etraci("exp2b p1", p1, vp);
312 if (**vp && eq(**vp, STRand)) {
316 p2 = exp2b(vp, ignore);
318 etraci("exp2b p2", p2, vp);
330 register Char *p1 = exp3(vp, ignore);
335 etracc("exp2c p1", p1, vp);
337 if ((i = isa(**vp, EQOP)) != 0) {
339 if (i == EQMATCH || i == NOTEQMATCH)
340 ignore |= TEXP_NOGLOB;
341 p2 = exp3(vp, ignore);
343 etracc("exp2c p2", p2, vp);
345 if (!(ignore & TEXP_IGNORE))
378 register Char *p1, *p2;
381 p1 = exp3a(vp, ignore);
383 etracc("exp3 p1", p1, vp);
385 if ((i = isa(**vp, RELOP)) != 0) {
387 if (**vp && eq(**vp, STRequal))
389 p2 = exp3(vp, ignore);
391 etracc("exp3 p2", p2, vp);
393 if (!(ignore & TEXP_IGNORE))
397 i = egetn(p1) > egetn(p2);
401 i = egetn(p1) >= egetn(p2);
405 i = egetn(p1) < egetn(p2);
409 i = egetn(p1) <= egetn(p2);
424 register Char *p1, *p2, *op;
427 p1 = exp4(vp, ignore);
429 etracc("exp3a p1", p1, vp);
432 if (op && any("<>", op[0]) && op[0] == op[1]) {
434 p2 = exp3a(vp, ignore);
436 etracc("exp3a p2", p2, vp);
439 i = egetn(p1) << egetn(p2);
441 i = egetn(p1) >> egetn(p2);
454 register Char *p1, *p2;
457 p1 = exp5(vp, ignore);
459 etracc("exp4 p1", p1, vp);
461 if (isa(**vp, ADDOP)) {
462 register Char *op = *(*vp)++;
464 p2 = exp4(vp, ignore);
466 etracc("exp4 p2", p2, vp);
468 if (!(ignore & TEXP_IGNORE))
472 i = egetn(p1) + egetn(p2);
476 i = egetn(p1) - egetn(p2);
491 register Char *p1, *p2;
494 p1 = exp6(vp, ignore);
496 etracc("exp5 p1", p1, vp);
499 if (isa(**vp, MULOP)) {
500 register Char *op = *(*vp)++;
501 if ((ignore & TEXP_NOGLOB) != 0)
503 * We are just trying to get the right side of
504 * a =~ or !~ operator
508 p2 = exp5(vp, ignore);
510 etracc("exp5 p2", p2, vp);
512 if (!(ignore & TEXP_IGNORE))
516 i = egetn(p1) * egetn(p2);
549 stderror(ERR_NAME | ERR_EXPRESSION);
550 if (eq(**vp, STRbang)) {
552 cp = exp6(vp, ignore);
554 etracc("exp6 ! cp", cp, vp);
560 if (eq(**vp, STRtilde)) {
562 cp = exp6(vp, ignore);
564 etracc("exp6 ~ cp", cp, vp);
570 if (eq(**vp, STRLparen)) {
572 ccode = exp0(vp, ignore);
574 etraci("exp6 () ccode", ccode, vp);
576 if (*vp == 0 || **vp == 0 || ***vp != ')')
577 stderror(ERR_NAME | ERR_EXPRESSION);
579 return (putn(ccode));
581 if (eq(**vp, STRLbrace)) {
583 struct command faket;
586 faket.t_dtyp = NODE_COMMAND;
587 faket.t_dflg = F_BACKQ;
588 faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
589 faket.t_dcom = fakecom;
590 fakecom[0] = STRfakecom;
596 stderror(ERR_NAME | ERR_MISSING, '}');
597 if (eq(*(*vp)++, STRRbrace))
600 if (ignore & TEXP_IGNORE)
601 return (Strsave(STRNULL));
603 if (pfork(&faket, -1) == 0) {
611 etraci("exp6 {} status", egetn(varval(STRstatus)), vp);
613 return (putn(egetn(varval(STRstatus)) == 0));
615 if (isa(**vp, ANYOP))
616 return (Strsave(STRNULL));
619 # define FILETESTS "erwxfdzoplstSXLbcugkmKR"
621 # define FILETESTS "erwxfdzoplstSXLbcugkmK"
623 #define FILEVALS "ZAMCDIUGNFPL"
624 if (*cp == '-' && (any(FILETESTS, cp[1]) || any(FILEVALS, cp[1])))
625 return(filetest(cp, vp, ignore));
627 etracc("exp6 default", cp, vp);
629 return (ignore & TEXP_NOGLOB ? Strsave(cp) : globone(cp, G_APPEND));
634 * Extended file tests
635 * From: John Rowe <rowe@excc.exeter.ac.uk>
638 filetest(cp, vp, ignore)
643 struct cvxstat stb, *st = NULL;
644 # define TCSH_STAT stat64
646 # define TCSH_STAT stat
647 struct stat stb, *st = NULL;
652 struct cvxstat lstb, *lst = NULL;
653 # define TCSH_LSTAT lstat64
655 # define TCSH_LSTAT lstat
656 struct stat lstb, *lst = NULL;
662 unsigned pmask = 0xffff;
664 Char *ft = cp, *dp, *ep, *strdev, *strino, *strF, *str, valtest = '\0',
666 char *string, string0[8];
671 while(any(FILETESTS, *++ft))
674 if (!*ft && *(ft - 1) == 'L')
677 if (any(FILEVALS, *ft)) {
680 * Value tests return '-1' on failure as 0 is
681 * a legitimate value for many of them.
682 * 'F' returns ':' for compatibility.
684 errval = valtest == 'F' ? STRcolon : STRminus1;
686 if (valtest == 'P' && *ft >= '0' && *ft <= '7') {
687 pmask = (char) *ft - '0';
688 while ( *++ft >= '0' && *ft <= '7' )
689 pmask = 8 * pmask + ((char) *ft - '0');
691 if (Strcmp(ft, STRcolon) == 0 && any("AMCUGP", valtest)) {
697 if (*ft || ft == cp + 1)
698 stderror(ERR_NAME | ERR_FILEINQ);
701 * Detect missing file names by checking for operator in the file name
702 * position. However, if an operator name appears there, we must make
703 * sure that there's no file by that name (e.g., "/") before announcing
704 * an error. Even this check isn't quite right, since it doesn't take
705 * globbing into account.
708 if (isa(**vp, ANYOP) && TCSH_STAT(short2str(**vp), &stb))
709 stderror(ERR_NAME | ERR_FILENAME);
712 if (ignore & TEXP_IGNORE)
713 return (Strsave(STRNULL));
714 ep = globone(dp, G_APPEND);
720 i = !sh_access(ep, R_OK);
724 i = !sh_access(ep, W_OK);
728 i = !sh_access(ep, X_OK);
731 case 'X': /* tcsh extension, name is an executable in the path
732 * or a tcsh builtin command
737 case 't': /* SGI extension, true when file is a tty */
738 i = isatty(atoi(short2str(ep)));
744 if (tolower(*ft) == 'l') {
746 * avoid convex compiler bug.
750 if (TCSH_LSTAT(short2str(ep), lst) == -1) {
752 return (Strsave(errval));
761 * avoid convex compiler bug.
765 if (TCSH_STAT(short2str(ep), st) == -1) {
767 return (Strsave(errval));
775 i = S_ISREG(st->st_mode);
783 i = S_ISDIR(st->st_mode);
791 i = S_ISFIFO(st->st_mode);
792 #else /* !S_ISFIFO */
794 #endif /* S_ISFIFO */
799 i = S_ISOFL(st->st_dm_mode);
816 i = S_ISLNK(lst->st_mode);
824 i = S_ISSOCK(st->st_mode);
825 # else /* !S_ISSOCK */
827 # endif /* S_ISSOCK */
832 i = S_ISBLK(st->st_mode);
840 i = S_ISCHR(st->st_mode);
847 i = (S_ISUID & st->st_mode) != 0;
851 i = (S_ISGID & st->st_mode) != 0;
855 i = (S_ISVTX & st->st_mode) != 0;
859 i = st->st_size == 0;
864 i = (stb.st_dmonflags & IMIGRATED) == IMIGRATED;
869 i = stb.st_size != 0;
877 i = st->st_uid == uid;
881 * Value operators are a tcsh extension.
885 i = (int) st->st_dev;
889 i = (int) st->st_ino;
893 strdev = putn( (int) st->st_dev);
894 strino = putn( (int) st->st_ino);
895 strF = (Char *) xmalloc((size_t) (2 + Strlen(strdev) +
896 Strlen(strino)) * sizeof(Char));
897 (void) Strcat(Strcat(Strcpy(strF, strdev), STRcolon), strino);
898 xfree((ptr_t) strdev);
899 xfree((ptr_t) strino);
909 filnam = short2str(ep);
911 # define MY_PATH_MAX PATH_MAX
912 #else /* !PATH_MAX */
914 * I can't think of any more sensible alterative; readlink doesn't give
915 * us an errno if the buffer isn't large enough :-(
917 # define MY_PATH_MAX 2048
918 #endif /* PATH_MAX */
919 i = readlink(filnam, string = (char *)
920 xmalloc((size_t) (1 + MY_PATH_MAX) * sizeof(char)),
922 if (i >= 0 && i <= MY_PATH_MAX)
923 string[i] = '\0'; /* readlink does not null terminate */
924 strF = (i < 0) ? errval : str2short(string);
925 xfree((ptr_t) string);
927 return(Strsave(strF));
936 i = (int) st->st_nlink;
940 string = string0 + 1;
941 (void) xsnprintf(string, sizeof(string0) - 1, "%o",
942 pmask & (unsigned int)
943 ((S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID) & st->st_mode));
944 if (altout && *string != '0')
947 return(Strsave(str2short(string)));
950 if (altout && (pw = getpwuid(st->st_uid))) {
952 return(Strsave(str2short(pw->pw_name)));
954 i = (int) st->st_uid;
958 if ( altout && (gr = getgrgid(st->st_gid))) {
960 return(Strsave(str2short(gr->gr_name)));
962 i = (int) st->st_gid;
966 i = (int) st->st_size;
969 case 'A': case 'M': case 'C':
970 footime = *ft == 'A' ? st->st_atime :
971 *ft == 'M' ? st->st_mtime : st->st_ctime;
973 strF = str2short(ctime(&footime));
974 if ((str = Strchr(strF, '\n')) != NULL)
977 return(Strsave(strF));
986 etraci("exp6 -? i", i, vp);
997 struct wordent paraml1;
998 register struct wordent *hp = ¶ml1;
1000 register struct wordent *wdp = hp;
1002 set(STRstatus, Strsave(STR0), VAR_READWRITE);
1003 hp->prev = hp->next = hp;
1006 register struct wordent *new =
1007 (struct wordent *) xcalloc(1, sizeof *wdp);
1013 wdp->word = Strsave(*v++);
1017 t = syntax(paraml1.next, ¶ml1, 0);
1020 execute(t, -1, NULL, NULL);
1021 freelex(¶ml1), freesyn(t);
1030 return ((what & RESTOP) != 0);
1034 if (what & ADDOP && (*cp == '+' || *cp == '-'))
1036 if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
1038 if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
1039 *cp == '~' || *cp == '^' || *cp == '"'))
1042 else if (cp[2] == 0) {
1043 if (what & RESTOP) {
1044 if (cp[0] == '|' && cp[1] == '&')
1046 if (cp[0] == '<' && cp[1] == '<')
1048 if (cp[0] == '>' && cp[1] == '>')
1058 else if (cp[0] == '!') {
1062 return (NOTEQMATCH);
1079 if (*cp && *cp != '-' && !Isdigit(*cp))
1080 stderror(ERR_NAME | ERR_EXPRESSION);
1093 xprintf("%s=%d\t", str, i);
1103 xprintf("%s=%s\t", str, cp);