1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.exp.c,v 3.58 2011/12/25 15:21: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("$tcsh: sh.exp.c,v 3.58 2011/12/25 15:21: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 (const Char *, int);
61 static tcsh_number_t exp1 (Char ***, int);
62 static tcsh_number_t exp2x (Char ***, int);
63 static tcsh_number_t exp2a (Char ***, int);
64 static tcsh_number_t exp2b (Char ***, int);
65 static tcsh_number_t exp2c (Char ***, int);
66 static Char *exp3 (Char ***, int);
67 static Char *exp3a (Char ***, int);
68 static Char *exp4 (Char ***, int);
69 static Char *exp5 (Char ***, int);
70 static Char *exp6 (Char ***, int);
71 static void evalav (Char **);
72 static int isa (Char *, int);
73 static tcsh_number_t egetn (const Char *);
76 static void etracc (const char *, const Char *, Char ***);
77 static void etraci (const char *, tcsh_number_t, Char ***);
79 #define etracc(A, B, C) ((void)0)
80 #define etraci(A, B, C) ((void)0)
84 * shell access function according to POSIX and non POSIX
85 * From Beto Appleton (beto@aixwiz.aix.ibm.com)
88 sh_access(const Char *fname, int mode)
90 #if defined(POSIX) && !defined(USE_ACCESS)
93 char *name = short2str(fname);
98 #if !defined(POSIX) || defined(USE_ACCESS)
99 return access(name, mode);
104 * -r file True if file exists and is readable.
105 * -w file True if file exists and is writable.
106 * True shall indicate only that the write flag is on.
107 * The file shall not be writable on a read-only file
108 * system even if this test indicates true.
109 * -x file True if file exists and is executable.
110 * True shall indicate only that the execute flag is on.
111 * If file is a directory, true indicates that the file
114 if (mode != W_OK && mode != X_OK)
115 return access(name, mode);
117 if (stat(name, &statb) == -1)
120 if (access(name, mode) == 0) {
122 if (S_ISDIR(statb.st_mode) && mode == X_OK)
126 /* root needs permission for someone */
129 mode = S_IWUSR | S_IWGRP | S_IWOTH;
132 mode = S_IXUSR | S_IXGRP | S_IXOTH;
141 else if (euid == statb.st_uid)
144 else if (egid == statb.st_gid)
149 /* you can be in several groups */
154 * Try these things to find a positive maximum groups value:
155 * 1) sysconf(_SC_NGROUPS_MAX)
157 * 3) getgroups(0, unused)
158 * Then allocate and scan the groups array if one of these worked.
160 # if defined (HAVE_SYSCONF) && defined (_SC_NGROUPS_MAX)
161 if ((n = sysconf(_SC_NGROUPS_MAX)) == -1)
162 # endif /* _SC_NGROUPS_MAX */
165 n = getgroups(0, (GETGROUPS_T *) NULL);
168 groups = xmalloc(n * sizeof(*groups));
169 n = getgroups((int) n, groups);
171 if (groups[n] == statb.st_gid) {
177 # endif /* NGROUPS_MAX */
179 if (statb.st_mode & mode)
189 return (exp0(vp, 0));
193 exp0(Char ***vp, int ignore)
195 tcsh_number_t p1 = exp1(vp, ignore);
197 etraci("exp0 p1", p1, vp);
198 while (**vp && eq(**vp, STRor2)) {
204 exp0(vp, (ignore & TEXP_IGNORE) || p1) :
205 exp1(vp, (ignore & TEXP_IGNORE) || p1);
206 if (compat_expr || !(ignore & TEXP_IGNORE))
208 etraci("exp0 p1", p1, vp);
216 exp1(Char ***vp, int ignore)
218 tcsh_number_t p1 = exp2x(vp, ignore);
220 etraci("exp1 p1", p1, vp);
221 while (**vp && eq(**vp, STRand2)) {
226 exp1(vp, (ignore & TEXP_IGNORE) || !p1) :
227 exp2x(vp, (ignore & TEXP_IGNORE) || !p1);
229 etraci("exp1 p2", p2, vp);
230 if (compat_expr || !(ignore & TEXP_IGNORE))
232 etraci("exp1 p1", p1, vp);
240 exp2x(Char ***vp, int ignore)
242 tcsh_number_t p1 = exp2a(vp, ignore);
244 etraci("exp2x p1", p1, vp);
245 while (**vp && eq(**vp, STRor)) {
252 etraci("exp2x p2", p2, vp);
253 if (compat_expr || !(ignore & TEXP_IGNORE))
255 etraci("exp2x p1", p1, vp);
263 exp2a(Char ***vp, int ignore)
265 tcsh_number_t p1 = exp2b(vp, ignore);
267 etraci("exp2a p1", p1, vp);
268 while (**vp && eq(**vp, STRcaret)) {
275 etraci("exp2a p2", p2, vp);
276 if (compat_expr || !(ignore & TEXP_IGNORE))
278 etraci("exp2a p1", p1, vp);
286 exp2b(Char ***vp, int ignore)
288 tcsh_number_t p1 = exp2c(vp, ignore);
290 etraci("exp2b p1", p1, vp);
291 while (**vp && eq(**vp, STRand)) {
298 etraci("exp2b p2", p2, vp);
299 if (compat_expr || !(ignore & TEXP_IGNORE))
301 etraci("exp2b p1", p1, vp);
309 exp2c(Char ***vp, int ignore)
311 Char *p1 = exp3(vp, ignore);
315 cleanup_push(p1, xfree);
316 etracc("exp2c p1", p1, vp);
317 if ((i = isa(**vp, EQOP)) != 0) {
319 if (i == EQMATCH || i == NOTEQMATCH)
320 ignore |= TEXP_NOGLOB;
321 p2 = exp3(vp, ignore);
322 cleanup_push(p2, xfree);
323 etracc("exp2c p2", p2, vp);
324 if (!(ignore & TEXP_IGNORE))
352 exp3(Char ***vp, int ignore)
357 p1 = exp3a(vp, ignore);
358 etracc("exp3 p1", p1, vp);
359 while ((i = isa(**vp, RELOP)) != 0) {
361 if (**vp && eq(**vp, STRequal))
363 cleanup_push(p1, xfree);
367 cleanup_push(p2, xfree);
368 etracc("exp3 p2", p2, vp);
369 if (!(ignore & TEXP_IGNORE))
373 i = egetn(p1) > egetn(p2);
377 i = egetn(p1) >= egetn(p2);
381 i = egetn(p1) < egetn(p2);
385 i = egetn(p1) <= egetn(p2);
390 etracc("exp3 p1", p1, vp);
398 exp3a(Char ***vp, int ignore)
404 p1 = exp4(vp, ignore);
405 etracc("exp3a p1", p1, vp);
407 if (op && any("<>", op[0]) && op[0] == op[1]) {
409 cleanup_push(p1, xfree);
413 cleanup_push(p2, xfree);
414 etracc("exp3a p2", p2, vp);
416 i = egetn(p1) << egetn(p2);
418 i = egetn(p1) >> egetn(p2);
421 etracc("exp3a p1", p1, vp);
427 exp4(Char ***vp, int ignore)
432 p1 = exp5(vp, ignore);
433 etracc("exp4 p1", p1, vp);
434 while (isa(**vp, ADDOP)) {
435 const Char *op = *(*vp)++;
437 cleanup_push(p1, xfree);
441 cleanup_push(p2, xfree);
442 etracc("exp4 p2", p2, vp);
443 if (!(ignore & TEXP_IGNORE))
447 i = egetn(p1) + egetn(p2);
451 i = egetn(p1) - egetn(p2);
456 etracc("exp4 p1", p1, vp);
464 exp5(Char ***vp, int ignore)
469 p1 = exp6(vp, ignore);
470 etracc("exp5 p1", p1, vp);
472 while (isa(**vp, MULOP)) {
473 const Char *op = *(*vp)++;
474 if ((ignore & TEXP_NOGLOB) != 0) {
476 * We are just trying to get the right side of
477 * a =~ or !~ operator
483 cleanup_push(p1, xfree);
487 cleanup_push(p2, xfree);
488 etracc("exp5 p2", p2, vp);
489 if (!(ignore & TEXP_IGNORE))
493 i = egetn(p1) * egetn(p2);
512 etracc("exp5 p1", p1, vp);
520 exp6(Char ***vp, int ignore)
527 stderror(ERR_NAME | ERR_EXPRESSION);
528 if (eq(**vp, STRbang)) {
530 cp = exp6(vp, ignore);
531 cleanup_push(cp, xfree);
532 etracc("exp6 ! cp", cp, vp);
537 if (eq(**vp, STRtilde)) {
539 cp = exp6(vp, ignore);
540 cleanup_push(cp, xfree);
541 etracc("exp6 ~ cp", cp, vp);
546 if (eq(**vp, STRLparen)) {
548 ccode = exp0(vp, ignore);
549 etraci("exp6 () ccode", ccode, vp);
550 if (**vp == 0 || ***vp != ')')
551 stderror(ERR_NAME | ERR_EXPRESSION);
553 return (putn(ccode));
555 if (eq(**vp, STRLbrace)) {
557 struct command faket;
560 faket.t_dtyp = NODE_COMMAND;
561 faket.t_dflg = F_BACKQ;
562 faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
563 faket.t_dcom = fakecom;
564 fakecom[0] = STRfakecom;
570 stderror(ERR_NAME | ERR_MISSING, '}');
571 if (eq(*(*vp)++, STRRbrace))
574 if (ignore & TEXP_IGNORE)
575 return (Strsave(STRNULL));
577 cleanup_push(&faket, psavejob_cleanup); /* faket is only a marker */
578 if (pfork(&faket, -1) == 0) {
584 cleanup_until(&faket);
585 etraci("exp6 {} status", egetn(varval(STRstatus)), vp);
586 return (putn(egetn(varval(STRstatus)) == 0));
588 if (isa(**vp, ANYOP))
589 return (Strsave(STRNULL));
592 # define FILETESTS "erwxfdzoplstSXLbcugkmKR"
594 # define FILETESTS "erwxfdzoplstSXLbcugkmK"
596 #define FILEVALS "ZAMCDIUGNFPL"
597 if (*cp == '-' && (any(FILETESTS, cp[1]) || any(FILEVALS, cp[1])))
598 return(filetest(cp, vp, ignore));
599 etracc("exp6 default", cp, vp);
600 return (ignore & TEXP_NOGLOB ? Strsave(cp) : globone(cp, G_APPEND));
605 * Extended file tests
606 * From: John Rowe <rowe@excc.exeter.ac.uk>
609 filetest(Char *cp, Char ***vp, int ignore)
612 struct cvxstat stb, *st = NULL;
613 # define TCSH_STAT stat64
615 # define TCSH_STAT stat
616 struct stat stb, *st = NULL;
621 struct cvxstat lstb, *lst = NULL;
622 # define TCSH_LSTAT lstat64
624 # define TCSH_LSTAT lstat
625 struct stat lstb, *lst = NULL;
631 unsigned pmask = 0xffff;
633 Char *ft = cp, *dp, *ep, *strdev, *strino, *strF, *str, valtest = '\0',
635 char *string, string0[22 + MB_LEN_MAX + 1]; // space for 64 bit octal
640 while(any(FILETESTS, *++ft))
643 if (!*ft && *(ft - 1) == 'L')
646 if (any(FILEVALS, *ft)) {
649 * Value tests return '-1' on failure as 0 is
650 * a legitimate value for many of them.
651 * 'F' returns ':' for compatibility.
653 errval = valtest == 'F' ? STRcolon : STRminus1;
655 if (valtest == 'P' && *ft >= '0' && *ft <= '7') {
656 pmask = (char) *ft - '0';
657 while ( *++ft >= '0' && *ft <= '7' )
658 pmask = 8 * pmask + ((char) *ft - '0');
660 if (Strcmp(ft, STRcolon) == 0 && any("AMCUGP", valtest)) {
666 if (*ft || ft == cp + 1)
667 stderror(ERR_NAME | ERR_FILEINQ);
670 * Detect missing file names by checking for operator in the file name
671 * position. However, if an operator name appears there, we must make
672 * sure that there's no file by that name (e.g., "/") before announcing
673 * an error. Even this check isn't quite right, since it doesn't take
674 * globbing into account.
677 if (isa(**vp, ANYOP) && TCSH_STAT(short2str(**vp), &stb))
678 stderror(ERR_NAME | ERR_FILENAME);
681 if (ignore & TEXP_IGNORE)
682 return (Strsave(STRNULL));
683 ep = globone(dp, G_APPEND);
684 cleanup_push(ep, xfree);
690 i = !sh_access(ep, R_OK);
694 i = !sh_access(ep, W_OK);
698 i = !sh_access(ep, X_OK);
701 case 'X': /* tcsh extension, name is an executable in the path
702 * or a tcsh builtin command
707 case 't': /* SGI extension, true when file is a tty */
708 i = isatty(atoi(short2str(ep)));
714 if (tolower(*ft) == 'l') {
716 * avoid convex compiler bug.
720 if (TCSH_LSTAT(short2str(ep), lst) == -1) {
722 return (Strsave(errval));
731 * avoid convex compiler bug.
735 if (TCSH_STAT(short2str(ep), st) == -1) {
737 return (Strsave(errval));
745 i = S_ISREG(st->st_mode);
753 i = S_ISDIR(st->st_mode);
761 i = S_ISFIFO(st->st_mode);
762 #else /* !S_ISFIFO */
764 #endif /* S_ISFIFO */
769 i = S_ISOFL(st->st_dm_mode);
786 i = S_ISLNK(lst->st_mode);
794 i = S_ISSOCK(st->st_mode);
795 # else /* !S_ISSOCK */
797 # endif /* S_ISSOCK */
802 i = S_ISBLK(st->st_mode);
810 i = S_ISCHR(st->st_mode);
817 i = (S_ISUID & st->st_mode) != 0;
821 i = (S_ISGID & st->st_mode) != 0;
825 i = (S_ISVTX & st->st_mode) != 0;
829 i = st->st_size == 0;
834 i = (stb.st_dmonflags & IMIGRATED) == IMIGRATED;
839 i = stb.st_size != 0;
847 i = st->st_uid == uid;
851 * Value operators are a tcsh extension.
855 i = (tcsh_number_t) st->st_dev;
859 i = (tcsh_number_t) st->st_ino;
863 strdev = putn( (int) st->st_dev);
864 strino = putn( (int) st->st_ino);
865 strF = xmalloc((2 + Strlen(strdev) + Strlen(strino))
867 (void) Strcat(Strcat(Strcpy(strF, strdev), STRcolon), strino);
879 filnam = short2str(ep);
880 string = areadlink(filnam);
881 strF = string == NULL ? errval : str2short(string);
884 return(Strsave(strF));
893 i = (tcsh_number_t) st->st_nlink;
897 string = string0 + 1;
898 (void) xsnprintf(string, sizeof(string0) - 1, "%o",
899 pmask & (unsigned int)
900 ((S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID) & st->st_mode));
901 if (altout && *string != '0')
904 return(Strsave(str2short(string)));
907 if (altout && (pw = xgetpwuid(st->st_uid))) {
909 return(Strsave(str2short(pw->pw_name)));
911 i = (tcsh_number_t) st->st_uid;
915 if (altout && (gr = xgetgrgid(st->st_gid))) {
917 return(Strsave(str2short(gr->gr_name)));
919 i = (tcsh_number_t) st->st_gid;
923 i = (tcsh_number_t) st->st_size;
926 case 'A': case 'M': case 'C':
927 footime = *ft == 'A' ? st->st_atime :
928 *ft == 'M' ? st->st_mtime : st->st_ctime;
930 strF = str2short(ctime(&footime));
931 if ((str = Strchr(strF, '\n')) != NULL)
934 return(Strsave(strF));
936 i = (tcsh_number_t) footime;
942 etraci("exp6 -? i", i, vp);
951 struct wordent paraml1;
952 struct wordent *hp = ¶ml1;
954 struct wordent *wdp = hp;
956 setcopy(STRstatus, STR0, VAR_READWRITE);
957 hp->prev = hp->next = hp;
960 struct wordent *new = xcalloc(1, sizeof *wdp);
966 wdp->word = Strsave(*v++);
969 cleanup_push(¶ml1, lex_cleanup);
971 t = syntax(paraml1.next, ¶ml1, 0);
972 cleanup_push(t, syntax_cleanup);
975 execute(t, -1, NULL, NULL, TRUE);
976 cleanup_until(¶ml1);
980 isa(Char *cp, int what)
983 return ((what & RESTOP) != 0);
987 if (what & ADDOP && (*cp == '+' || *cp == '-'))
989 if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
991 if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
992 *cp == '~' || *cp == '^' || *cp == '"'))
995 else if (cp[2] == 0) {
997 if (cp[0] == '|' && cp[1] == '&')
999 if (cp[0] == '<' && cp[1] == '<')
1001 if (cp[0] == '>' && cp[1] == '>')
1011 else if (cp[0] == '!') {
1015 return (NOTEQMATCH);
1028 static tcsh_number_t
1029 egetn(const Char *cp)
1031 if (*cp && *cp != '-' && !Isdigit(*cp))
1032 stderror(ERR_NAME | ERR_EXPRESSION);
1040 etraci(const char *str, tcsh_number_t i, Char ***vp)
1042 #ifdef HAVE_LONG_LONG
1043 xprintf("%s=%lld\t", str, i);
1045 xprintf("%s=%ld\t", str, i);
1051 etracc(const char *str, const Char *cp, Char ***vp)
1053 xprintf("%s=%S\t", str, cp);