1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.exp.c,v 3.51 2006/05/13 21:25:20 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.51 2006/05/13 21:25:20 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 int exp1 (Char ***, int);
62 static int exp2x (Char ***, int);
63 static int exp2a (Char ***, int);
64 static int exp2b (Char ***, int);
65 static int 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 int egetn (Char *);
77 static void etracc (char *, Char *, Char ***);
78 static void etraci (char *, int, Char ***);
80 #define etracc(A, B, C) ((void)0)
81 #define etraci(A, B, C) ((void)0)
86 * shell access function according to POSIX and non POSIX
87 * From Beto Appleton (beto@aixwiz.aix.ibm.com)
90 sh_access(const Char *fname, int mode)
92 #if defined(POSIX) && !defined(USE_ACCESS)
95 char *name = short2str(fname);
100 #if !defined(POSIX) || defined(USE_ACCESS)
101 return access(name, mode);
106 * -r file True if file exists and is readable.
107 * -w file True if file exists and is writable.
108 * True shall indicate only that the write flag is on.
109 * The file shall not be writable on a read-only file
110 * system even if this test indicates true.
111 * -x file True if file exists and is executable.
112 * True shall indicate only that the execute flag is on.
113 * If file is a directory, true indicates that the file
116 if (mode != W_OK && mode != X_OK)
117 return access(name, mode);
119 if (stat(name, &statb) == -1)
122 if (access(name, mode) == 0) {
124 if (S_ISDIR(statb.st_mode) && mode == X_OK)
128 /* root needs permission for someone */
131 mode = S_IWUSR | S_IWGRP | S_IWOTH;
134 mode = S_IXUSR | S_IXGRP | S_IXOTH;
143 else if (euid == statb.st_uid)
146 else if (egid == statb.st_gid)
151 /* you can be in several groups */
156 * Try these things to find a positive maximum groups value:
157 * 1) sysconf(_SC_NGROUPS_MAX)
159 * 3) getgroups(0, unused)
160 * Then allocate and scan the groups array if one of these worked.
162 # if defined (HAVE_SYSCONF) && defined (_SC_NGROUPS_MAX)
163 if ((n = sysconf(_SC_NGROUPS_MAX)) == -1)
164 # endif /* _SC_NGROUPS_MAX */
167 n = getgroups(0, (GETGROUPS_T *) NULL);
170 groups = xmalloc(n * sizeof(*groups));
171 n = getgroups((int) n, groups);
173 if (groups[n] == statb.st_gid) {
179 # endif /* NGROUPS_MAX */
181 if (statb.st_mode & mode)
191 return (exp0(vp, 0));
195 exp0(Char ***vp, int ignore)
197 int p1 = exp1(vp, ignore);
199 etraci("exp0 p1", p1, vp);
200 if (**vp && eq(**vp, STRor2)) {
204 p2 = exp0(vp, (ignore & TEXP_IGNORE) || p1);
205 etraci("exp0 p2", p2, vp);
212 exp1(Char ***vp, int ignore)
214 int p1 = exp2x(vp, ignore);
216 etraci("exp1 p1", p1, vp);
217 if (**vp && eq(**vp, STRand2)) {
221 p2 = exp1(vp, (ignore & TEXP_IGNORE) || !p1);
222 etraci("exp1 p2", p2, vp);
229 exp2x(Char ***vp, int ignore)
231 int p1 = exp2a(vp, ignore);
233 etraci("exp3 p1", p1, vp);
234 if (**vp && eq(**vp, STRor)) {
238 p2 = exp2x(vp, ignore);
239 etraci("exp3 p2", p2, vp);
246 exp2a(Char ***vp, int ignore)
248 int p1 = exp2b(vp, ignore);
250 etraci("exp2a p1", p1, vp);
251 if (**vp && eq(**vp, STRcaret)) {
255 p2 = exp2a(vp, ignore);
256 etraci("exp2a p2", p2, vp);
263 exp2b(Char ***vp, int ignore)
265 int p1 = exp2c(vp, ignore);
267 etraci("exp2b p1", p1, vp);
268 if (**vp && eq(**vp, STRand)) {
272 p2 = exp2b(vp, ignore);
273 etraci("exp2b p2", p2, vp);
280 exp2c(Char ***vp, int ignore)
282 Char *p1 = exp3(vp, ignore);
286 cleanup_push(p1, xfree);
287 etracc("exp2c p1", p1, vp);
288 if ((i = isa(**vp, EQOP)) != 0) {
290 if (i == EQMATCH || i == NOTEQMATCH)
291 ignore |= TEXP_NOGLOB;
292 p2 = exp3(vp, ignore);
293 cleanup_push(p2, xfree);
294 etracc("exp2c p2", p2, vp);
295 if (!(ignore & TEXP_IGNORE))
323 exp3(Char ***vp, int ignore)
328 p1 = exp3a(vp, ignore);
329 etracc("exp3 p1", p1, vp);
330 if ((i = isa(**vp, RELOP)) != 0) {
332 if (**vp && eq(**vp, STRequal))
334 cleanup_push(p1, xfree);
335 p2 = exp3(vp, ignore);
336 cleanup_push(p2, xfree);
337 etracc("exp3 p2", p2, vp);
338 if (!(ignore & TEXP_IGNORE))
342 i = egetn(p1) > egetn(p2);
346 i = egetn(p1) >= egetn(p2);
350 i = egetn(p1) < egetn(p2);
354 i = egetn(p1) <= egetn(p2);
364 exp3a(Char ***vp, int ignore)
370 p1 = exp4(vp, ignore);
371 etracc("exp3a p1", p1, vp);
373 if (op && any("<>", op[0]) && op[0] == op[1]) {
375 cleanup_push(p1, xfree);
376 p2 = exp3a(vp, ignore);
377 cleanup_push(p2, xfree);
378 etracc("exp3a p2", p2, vp);
380 i = egetn(p1) << egetn(p2);
382 i = egetn(p1) >> egetn(p2);
390 exp4(Char ***vp, int ignore)
395 p1 = exp5(vp, ignore);
396 etracc("exp4 p1", p1, vp);
397 if (isa(**vp, ADDOP)) {
398 const Char *op = *(*vp)++;
400 cleanup_push(p1, xfree);
401 p2 = exp4(vp, ignore);
402 cleanup_push(p2, xfree);
403 etracc("exp4 p2", p2, vp);
404 if (!(ignore & TEXP_IGNORE))
408 i = egetn(p1) + egetn(p2);
412 i = egetn(p1) - egetn(p2);
422 exp5(Char ***vp, int ignore)
427 p1 = exp6(vp, ignore);
428 etracc("exp5 p1", p1, vp);
430 if (isa(**vp, MULOP)) {
431 const Char *op = *(*vp)++;
432 if ((ignore & TEXP_NOGLOB) != 0) {
434 * We are just trying to get the right side of
435 * a =~ or !~ operator
441 cleanup_push(p1, xfree);
442 p2 = exp5(vp, ignore);
443 cleanup_push(p2, xfree);
444 etracc("exp5 p2", p2, vp);
445 if (!(ignore & TEXP_IGNORE))
449 i = egetn(p1) * egetn(p2);
473 exp6(Char ***vp, int ignore)
479 stderror(ERR_NAME | ERR_EXPRESSION);
480 if (eq(**vp, STRbang)) {
482 cp = exp6(vp, ignore);
483 cleanup_push(cp, xfree);
484 etracc("exp6 ! cp", cp, vp);
489 if (eq(**vp, STRtilde)) {
491 cp = exp6(vp, ignore);
492 cleanup_push(cp, xfree);
493 etracc("exp6 ~ cp", cp, vp);
498 if (eq(**vp, STRLparen)) {
500 ccode = exp0(vp, ignore);
501 etraci("exp6 () ccode", ccode, vp);
502 if (**vp == 0 || ***vp != ')')
503 stderror(ERR_NAME | ERR_EXPRESSION);
505 return (putn(ccode));
507 if (eq(**vp, STRLbrace)) {
509 struct command faket;
512 faket.t_dtyp = NODE_COMMAND;
513 faket.t_dflg = F_BACKQ;
514 faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
515 faket.t_dcom = fakecom;
516 fakecom[0] = STRfakecom;
522 stderror(ERR_NAME | ERR_MISSING, '}');
523 if (eq(*(*vp)++, STRRbrace))
526 if (ignore & TEXP_IGNORE)
527 return (Strsave(STRNULL));
529 cleanup_push(&faket, psavejob_cleanup); /* faket is only a marker */
530 if (pfork(&faket, -1) == 0) {
536 cleanup_until(&faket);
537 etraci("exp6 {} status", egetn(varval(STRstatus)), vp);
538 return (putn(egetn(varval(STRstatus)) == 0));
540 if (isa(**vp, ANYOP))
541 return (Strsave(STRNULL));
544 # define FILETESTS "erwxfdzoplstSXLbcugkmKR"
546 # define FILETESTS "erwxfdzoplstSXLbcugkmK"
548 #define FILEVALS "ZAMCDIUGNFPL"
549 if (*cp == '-' && (any(FILETESTS, cp[1]) || any(FILEVALS, cp[1])))
550 return(filetest(cp, vp, ignore));
551 etracc("exp6 default", cp, vp);
552 return (ignore & TEXP_NOGLOB ? Strsave(cp) : globone(cp, G_APPEND));
557 * Extended file tests
558 * From: John Rowe <rowe@excc.exeter.ac.uk>
561 filetest(Char *cp, Char ***vp, int ignore)
564 struct cvxstat stb, *st = NULL;
565 # define TCSH_STAT stat64
567 # define TCSH_STAT stat
568 struct stat stb, *st = NULL;
573 struct cvxstat lstb, *lst = NULL;
574 # define TCSH_LSTAT lstat64
576 # define TCSH_LSTAT lstat
577 struct stat lstb, *lst = NULL;
583 unsigned pmask = 0xffff;
585 Char *ft = cp, *dp, *ep, *strdev, *strino, *strF, *str, valtest = '\0',
587 char *string, string0[8];
592 while(any(FILETESTS, *++ft))
595 if (!*ft && *(ft - 1) == 'L')
598 if (any(FILEVALS, *ft)) {
601 * Value tests return '-1' on failure as 0 is
602 * a legitimate value for many of them.
603 * 'F' returns ':' for compatibility.
605 errval = valtest == 'F' ? STRcolon : STRminus1;
607 if (valtest == 'P' && *ft >= '0' && *ft <= '7') {
608 pmask = (char) *ft - '0';
609 while ( *++ft >= '0' && *ft <= '7' )
610 pmask = 8 * pmask + ((char) *ft - '0');
612 if (Strcmp(ft, STRcolon) == 0 && any("AMCUGP", valtest)) {
618 if (*ft || ft == cp + 1)
619 stderror(ERR_NAME | ERR_FILEINQ);
622 * Detect missing file names by checking for operator in the file name
623 * position. However, if an operator name appears there, we must make
624 * sure that there's no file by that name (e.g., "/") before announcing
625 * an error. Even this check isn't quite right, since it doesn't take
626 * globbing into account.
629 if (isa(**vp, ANYOP) && TCSH_STAT(short2str(**vp), &stb))
630 stderror(ERR_NAME | ERR_FILENAME);
633 if (ignore & TEXP_IGNORE)
634 return (Strsave(STRNULL));
635 ep = globone(dp, G_APPEND);
636 cleanup_push(ep, xfree);
642 i = !sh_access(ep, R_OK);
646 i = !sh_access(ep, W_OK);
650 i = !sh_access(ep, X_OK);
653 case 'X': /* tcsh extension, name is an executable in the path
654 * or a tcsh builtin command
659 case 't': /* SGI extension, true when file is a tty */
660 i = isatty(atoi(short2str(ep)));
666 if (tolower(*ft) == 'l') {
668 * avoid convex compiler bug.
672 if (TCSH_LSTAT(short2str(ep), lst) == -1) {
674 return (Strsave(errval));
683 * avoid convex compiler bug.
687 if (TCSH_STAT(short2str(ep), st) == -1) {
689 return (Strsave(errval));
697 i = S_ISREG(st->st_mode);
705 i = S_ISDIR(st->st_mode);
713 i = S_ISFIFO(st->st_mode);
714 #else /* !S_ISFIFO */
716 #endif /* S_ISFIFO */
721 i = S_ISOFL(st->st_dm_mode);
738 i = S_ISLNK(lst->st_mode);
746 i = S_ISSOCK(st->st_mode);
747 # else /* !S_ISSOCK */
749 # endif /* S_ISSOCK */
754 i = S_ISBLK(st->st_mode);
762 i = S_ISCHR(st->st_mode);
769 i = (S_ISUID & st->st_mode) != 0;
773 i = (S_ISGID & st->st_mode) != 0;
777 i = (S_ISVTX & st->st_mode) != 0;
781 i = st->st_size == 0;
786 i = (stb.st_dmonflags & IMIGRATED) == IMIGRATED;
791 i = stb.st_size != 0;
799 i = st->st_uid == uid;
803 * Value operators are a tcsh extension.
807 i = (int) st->st_dev;
811 i = (int) st->st_ino;
815 strdev = putn( (int) st->st_dev);
816 strino = putn( (int) st->st_ino);
817 strF = xmalloc((2 + Strlen(strdev) + Strlen(strino))
819 (void) Strcat(Strcat(Strcpy(strF, strdev), STRcolon), strino);
831 filnam = short2str(ep);
832 string = areadlink(filnam);
833 strF = string == NULL ? errval : str2short(string);
836 return(Strsave(strF));
845 i = (int) st->st_nlink;
849 string = string0 + 1;
850 (void) xsnprintf(string, sizeof(string0) - 1, "%o",
851 pmask & (unsigned int)
852 ((S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID) & st->st_mode));
853 if (altout && *string != '0')
856 return(Strsave(str2short(string)));
859 if (altout && (pw = xgetpwuid(st->st_uid))) {
861 return(Strsave(str2short(pw->pw_name)));
863 i = (int) st->st_uid;
867 if (altout && (gr = xgetgrgid(st->st_gid))) {
869 return(Strsave(str2short(gr->gr_name)));
871 i = (int) st->st_gid;
875 i = (int) st->st_size;
878 case 'A': case 'M': case 'C':
879 footime = *ft == 'A' ? st->st_atime :
880 *ft == 'M' ? st->st_mtime : st->st_ctime;
882 strF = str2short(ctime(&footime));
883 if ((str = Strchr(strF, '\n')) != NULL)
886 return(Strsave(strF));
894 etraci("exp6 -? i", i, vp);
903 struct wordent paraml1;
904 struct wordent *hp = ¶ml1;
906 struct wordent *wdp = hp;
908 setcopy(STRstatus, STR0, VAR_READWRITE);
909 hp->prev = hp->next = hp;
912 struct wordent *new = xcalloc(1, sizeof *wdp);
918 wdp->word = Strsave(*v++);
921 cleanup_push(¶ml1, lex_cleanup);
923 t = syntax(paraml1.next, ¶ml1, 0);
924 cleanup_push(t, syntax_cleanup);
927 execute(t, -1, NULL, NULL, TRUE);
928 cleanup_until(¶ml1);
932 isa(Char *cp, int what)
935 return ((what & RESTOP) != 0);
939 if (what & ADDOP && (*cp == '+' || *cp == '-'))
941 if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
943 if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
944 *cp == '~' || *cp == '^' || *cp == '"'))
947 else if (cp[2] == 0) {
949 if (cp[0] == '|' && cp[1] == '&')
951 if (cp[0] == '<' && cp[1] == '<')
953 if (cp[0] == '>' && cp[1] == '>')
963 else if (cp[0] == '!') {
983 if (*cp && *cp != '-' && !Isdigit(*cp))
984 stderror(ERR_NAME | ERR_EXPRESSION);
992 etraci(char *str, int i, Char ***vp)
994 xprintf("%s=%d\t", str, i);
999 etracc(char *str, Char *cp, Char ***vp)
1001 xprintf("%s=%s\t", str, cp);