2 * sh.exp.c: Expression evaluations
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 static int sh_access (const Char
*, int);
54 static tcsh_number_t
exp1 (Char
***, int);
55 static tcsh_number_t
exp2x (Char
***, int);
56 static tcsh_number_t
exp2a (Char
***, int);
57 static tcsh_number_t
exp2b (Char
***, int);
58 static tcsh_number_t
exp2c (Char
***, int);
59 static Char
*exp3 (Char
***, int);
60 static Char
*exp3a (Char
***, int);
61 static Char
*exp4 (Char
***, int);
62 static Char
*exp5 (Char
***, int);
63 static Char
*exp6 (Char
***, int);
64 static void evalav (Char
**);
65 static int isa (Char
*, int);
66 static tcsh_number_t
egetn (const Char
*);
69 static void etracc (const char *, const Char
*, Char
***);
70 static void etraci (const char *, tcsh_number_t
, Char
***);
72 #define etracc(A, B, C) ((void)0)
73 #define etraci(A, B, C) ((void)0)
77 * shell access function according to POSIX and non POSIX
78 * From Beto Appleton (beto@aixwiz.aix.ibm.com)
81 sh_access(const Char
*fname
, int mode
)
83 #if defined(POSIX) && !defined(USE_ACCESS)
86 char *name
= short2str(fname
);
91 #if !defined(POSIX) || defined(USE_ACCESS)
92 return access(name
, mode
);
97 * -r file True if file exists and is readable.
98 * -w file True if file exists and is writable.
99 * True shall indicate only that the write flag is on.
100 * The file shall not be writable on a read-only file
101 * system even if this test indicates true.
102 * -x file True if file exists and is executable.
103 * True shall indicate only that the execute flag is on.
104 * If file is a directory, true indicates that the file
107 if (mode
!= W_OK
&& mode
!= X_OK
)
108 return access(name
, mode
);
110 if (stat(name
, &statb
) == -1)
113 if (access(name
, mode
) == 0) {
115 if (S_ISDIR(statb
.st_mode
) && mode
== X_OK
)
119 /* root needs permission for someone */
122 mode
= S_IWUSR
| S_IWGRP
| S_IWOTH
;
125 mode
= S_IXUSR
| S_IXGRP
| S_IXOTH
;
134 else if (euid
== statb
.st_uid
)
137 else if (egid
== statb
.st_gid
)
142 /* you can be in several groups */
147 * Try these things to find a positive maximum groups value:
148 * 1) sysconf(_SC_NGROUPS_MAX)
150 * 3) getgroups(0, unused)
151 * Then allocate and scan the groups array if one of these worked.
153 # if defined (HAVE_SYSCONF) && defined (_SC_NGROUPS_MAX)
154 if ((n
= sysconf(_SC_NGROUPS_MAX
)) == -1)
155 # endif /* _SC_NGROUPS_MAX */
158 n
= getgroups(0, (GETGROUPS_T
*) NULL
);
161 groups
= xmalloc(n
* sizeof(*groups
));
162 n
= getgroups((int) n
, groups
);
164 if (groups
[n
] == statb
.st_gid
) {
171 # endif /* NGROUPS_MAX */
173 if (statb
.st_mode
& mode
)
183 return (exp0(vp
, 0));
187 exp0(Char
***vp
, int ignore
)
189 tcsh_number_t p1
= exp1(vp
, ignore
);
191 etraci("exp0 p1", p1
, vp
);
192 while (**vp
&& eq(**vp
, STRor2
)) {
198 exp0(vp
, (ignore
& TEXP_IGNORE
) || p1
) :
199 exp1(vp
, (ignore
& TEXP_IGNORE
) || p1
);
200 if (compat_expr
|| !(ignore
& TEXP_IGNORE
))
202 etraci("exp0 p1", p1
, vp
);
210 exp1(Char
***vp
, int ignore
)
212 tcsh_number_t p1
= exp2x(vp
, ignore
);
214 etraci("exp1 p1", p1
, vp
);
215 while (**vp
&& eq(**vp
, STRand2
)) {
220 exp1(vp
, (ignore
& TEXP_IGNORE
) || !p1
) :
221 exp2x(vp
, (ignore
& TEXP_IGNORE
) || !p1
);
223 etraci("exp1 p2", p2
, vp
);
224 if (compat_expr
|| !(ignore
& TEXP_IGNORE
))
226 etraci("exp1 p1", p1
, vp
);
234 exp2x(Char
***vp
, int ignore
)
236 tcsh_number_t p1
= exp2a(vp
, ignore
);
238 etraci("exp2x p1", p1
, vp
);
239 while (**vp
&& eq(**vp
, STRor
)) {
246 etraci("exp2x p2", p2
, vp
);
247 if (compat_expr
|| !(ignore
& TEXP_IGNORE
))
249 etraci("exp2x p1", p1
, vp
);
257 exp2a(Char
***vp
, int ignore
)
259 tcsh_number_t p1
= exp2b(vp
, ignore
);
261 etraci("exp2a p1", p1
, vp
);
262 while (**vp
&& eq(**vp
, STRcaret
)) {
269 etraci("exp2a p2", p2
, vp
);
270 if (compat_expr
|| !(ignore
& TEXP_IGNORE
))
272 etraci("exp2a p1", p1
, vp
);
280 exp2b(Char
***vp
, int ignore
)
282 tcsh_number_t p1
= exp2c(vp
, ignore
);
284 etraci("exp2b p1", p1
, vp
);
285 while (**vp
&& eq(**vp
, STRand
)) {
292 etraci("exp2b p2", p2
, vp
);
293 if (compat_expr
|| !(ignore
& TEXP_IGNORE
))
295 etraci("exp2b p1", p1
, vp
);
303 exp2c(Char
***vp
, int ignore
)
305 Char
*p1
= exp3(vp
, ignore
);
309 cleanup_push(p1
, xfree
);
310 etracc("exp2c p1", p1
, vp
);
311 if ((i
= isa(**vp
, EQOP
)) != 0) {
313 if (i
== EQMATCH
|| i
== NOTEQMATCH
)
314 ignore
|= TEXP_NOGLOB
;
315 p2
= exp3(vp
, ignore
);
316 cleanup_push(p2
, xfree
);
317 etracc("exp2c p2", p2
, vp
);
318 if (!(ignore
& TEXP_IGNORE
))
346 exp3(Char
***vp
, int ignore
)
351 p1
= exp3a(vp
, ignore
);
352 etracc("exp3 p1", p1
, vp
);
353 while ((i
= isa(**vp
, RELOP
)) != 0) {
355 if (**vp
&& eq(**vp
, STRequal
))
357 cleanup_push(p1
, xfree
);
361 cleanup_push(p2
, xfree
);
362 etracc("exp3 p2", p2
, vp
);
363 if (!(ignore
& TEXP_IGNORE
))
367 i
= egetn(p1
) > egetn(p2
);
371 i
= egetn(p1
) >= egetn(p2
);
375 i
= egetn(p1
) < egetn(p2
);
379 i
= egetn(p1
) <= egetn(p2
);
384 etracc("exp3 p1", p1
, vp
);
392 exp3a(Char
***vp
, int ignore
)
398 p1
= exp4(vp
, ignore
);
399 etracc("exp3a p1", p1
, vp
);
401 if (op
&& any("<>", op
[0]) && op
[0] == op
[1]) {
403 cleanup_push(p1
, xfree
);
407 cleanup_push(p2
, xfree
);
408 etracc("exp3a p2", p2
, vp
);
410 i
= egetn(p1
) << egetn(p2
);
412 i
= egetn(p1
) >> egetn(p2
);
415 etracc("exp3a p1", p1
, vp
);
421 exp4(Char
***vp
, int ignore
)
426 p1
= exp5(vp
, ignore
);
427 etracc("exp4 p1", p1
, vp
);
428 while (isa(**vp
, ADDOP
)) {
429 const Char
*op
= *(*vp
)++;
431 cleanup_push(p1
, xfree
);
435 cleanup_push(p2
, xfree
);
436 etracc("exp4 p2", p2
, vp
);
437 if (!(ignore
& TEXP_IGNORE
))
441 i
= egetn(p1
) + egetn(p2
);
445 i
= egetn(p1
) - egetn(p2
);
450 etracc("exp4 p1", p1
, vp
);
458 exp5(Char
***vp
, int ignore
)
463 p1
= exp6(vp
, ignore
);
464 etracc("exp5 p1", p1
, vp
);
466 while (isa(**vp
, MULOP
)) {
467 const Char
*op
= *(*vp
)++;
468 if ((ignore
& TEXP_NOGLOB
) != 0) {
470 * We are just trying to get the right side of
471 * a =~ or !~ operator
477 cleanup_push(p1
, xfree
);
481 cleanup_push(p2
, xfree
);
482 etracc("exp5 p2", p2
, vp
);
483 if (!(ignore
& TEXP_IGNORE
))
487 i
= egetn(p1
) * egetn(p2
);
506 etracc("exp5 p1", p1
, vp
);
514 exp6(Char
***vp
, int ignore
)
521 stderror(ERR_NAME
| ERR_EXPRESSION
);
522 if (eq(**vp
, STRbang
)) {
524 cp
= exp6(vp
, ignore
);
525 cleanup_push(cp
, xfree
);
526 etracc("exp6 ! cp", cp
, vp
);
531 if (eq(**vp
, STRtilde
)) {
533 cp
= exp6(vp
, ignore
);
534 cleanup_push(cp
, xfree
);
535 etracc("exp6 ~ cp", cp
, vp
);
540 if (eq(**vp
, STRLparen
)) {
542 ccode
= exp0(vp
, ignore
);
543 etraci("exp6 () ccode", ccode
, vp
);
544 if (**vp
== 0 || ***vp
!= ')')
545 stderror(ERR_NAME
| ERR_EXPRESSION
);
547 return (putn(ccode
));
549 if (eq(**vp
, STRLbrace
)) {
551 struct command faket
;
554 faket
.t_dtyp
= NODE_COMMAND
;
555 faket
.t_dflg
= F_BACKQ
;
556 faket
.t_dcar
= faket
.t_dcdr
= faket
.t_dspr
= NULL
;
557 faket
.t_dcom
= fakecom
;
558 fakecom
[0] = STRfakecom
;
564 stderror(ERR_NAME
| ERR_MISSING
, '}');
565 if (eq(*(*vp
)++, STRRbrace
))
568 if (ignore
& TEXP_IGNORE
)
569 return (Strsave(STRNULL
));
571 cleanup_push(&faket
, psavejob_cleanup
); /* faket is only a marker */
572 if (pfork(&faket
, -1) == 0) {
578 cleanup_until(&faket
);
579 etraci("exp6 {} status", getstatus(), vp
);
580 return putn(getstatus() == 0);
582 if (isa(**vp
, ANYOP
))
583 return (Strsave(STRNULL
));
586 # define FILETESTS "erwxfdzoplstSXLbcugkmKR"
588 # define FILETESTS "erwxfdzoplstSXLbcugkmK"
590 #define FILEVALS "ZAMCDIUGNFPL"
591 if (*cp
== '-' && (any(FILETESTS
, cp
[1]) || any(FILEVALS
, cp
[1])))
592 return(filetest(cp
, vp
, ignore
));
593 etracc("exp6 default", cp
, vp
);
594 return (ignore
& TEXP_NOGLOB
? Strsave(cp
) : globone(cp
, G_APPEND
));
599 * Extended file tests
600 * From: John Rowe <rowe@excc.exeter.ac.uk>
603 filetest(Char
*cp
, Char
***vp
, int ignore
)
606 struct cvxstat stb
, *st
= NULL
;
607 # define TCSH_STAT stat64
609 # define TCSH_STAT stat
610 struct stat stb
, *st
= NULL
;
615 struct cvxstat lstb
, *lst
= NULL
;
616 # define TCSH_LSTAT lstat64
618 # define TCSH_LSTAT lstat
619 struct stat lstb
, *lst
= NULL
;
625 unsigned pmask
= 0xffff;
627 Char
*ft
= cp
, *dp
, *ep
, *strdev
, *strino
, *strF
, *str
, valtest
= '\0',
629 char *string
, string0
[22 + MB_LEN_MAX
+ 1]; /* space for 64 bit octal */
634 while (any(FILETESTS
, *++ft
))
637 if (!*ft
&& *(ft
- 1) == 'L')
640 if (any(FILEVALS
, *ft
)) {
643 * Value tests return '-1' on failure as 0 is
644 * a legitimate value for many of them.
645 * 'F' returns ':' for compatibility.
647 errval
= valtest
== 'F' ? STRcolon
: STRminus1
;
649 if (valtest
== 'P' && *ft
>= '0' && *ft
<= '7') {
650 pmask
= (char) *ft
- '0';
651 while ( *++ft
>= '0' && *ft
<= '7' )
652 pmask
= 8 * pmask
+ ((char) *ft
- '0');
654 if (Strcmp(ft
, STRcolon
) == 0 && any("AMCUGP", valtest
)) {
660 if (*ft
|| ft
== cp
+ 1)
661 stderror(ERR_NAME
| ERR_FILEINQ
);
664 * Detect missing file names by checking for operator in the file name
665 * position. However, if an operator name appears there, we must make
666 * sure that there's no file by that name (e.g., "/") before announcing
667 * an error. Even this check isn't quite right, since it doesn't take
668 * globbing into account.
671 if (isa(**vp
, ANYOP
) && TCSH_STAT(short2str(**vp
), &stb
))
672 stderror(ERR_NAME
| ERR_FILENAME
);
675 if (ignore
& TEXP_IGNORE
)
676 return (Strsave(STRNULL
));
677 if ((ignore
& TEXP_NOGLOB
) == 0) {
678 ep
= globone(dp
, G_APPEND
);
682 cleanup_push(ep
, xfree
);
688 i
= !sh_access(ep
, R_OK
);
692 i
= !sh_access(ep
, W_OK
);
696 i
= !sh_access(ep
, X_OK
);
699 case 'X': /* tcsh extension, name is an executable in the path
700 * or a tcsh builtin command
705 case 't': /* SGI extension, true when file is a tty */
706 i
= isatty(atoi(short2str(ep
)));
712 if (tolower(*ft
) == 'l') {
714 * avoid convex compiler bug.
718 if (TCSH_LSTAT(short2str(ep
), lst
) == -1) {
720 return (Strsave(errval
));
729 * avoid convex compiler bug.
733 if (TCSH_STAT(short2str(ep
), st
) == -1) {
735 return (Strsave(errval
));
743 i
= S_ISREG(st
->st_mode
);
751 i
= S_ISDIR(st
->st_mode
);
759 i
= S_ISFIFO(st
->st_mode
);
760 #else /* !S_ISFIFO */
762 #endif /* S_ISFIFO */
767 i
= S_ISOFL(st
->st_dm_mode
);
784 i
= S_ISLNK(lst
->st_mode
);
792 i
= S_ISSOCK(st
->st_mode
);
793 # else /* !S_ISSOCK */
795 # endif /* S_ISSOCK */
800 i
= S_ISBLK(st
->st_mode
);
808 i
= S_ISCHR(st
->st_mode
);
815 i
= (S_ISUID
& st
->st_mode
) != 0;
819 i
= (S_ISGID
& st
->st_mode
) != 0;
823 i
= (S_ISVTX
& st
->st_mode
) != 0;
827 i
= st
->st_size
== 0;
832 i
= (stb
.st_dmonflags
& IMIGRATED
) == IMIGRATED
;
837 i
= stb
.st_size
!= 0;
845 i
= st
->st_uid
== uid
;
849 * Value operators are a tcsh extension.
853 i
= (tcsh_number_t
) st
->st_dev
;
857 i
= (tcsh_number_t
) st
->st_ino
;
861 strdev
= putn( (int) st
->st_dev
);
862 strino
= putn( (int) st
->st_ino
);
863 strF
= xmalloc((2 + Strlen(strdev
) + Strlen(strino
))
865 (void) Strcat(Strcat(Strcpy(strF
, strdev
), STRcolon
), strino
);
877 filnam
= short2str(ep
);
878 string
= areadlink(filnam
);
879 strF
= string
== NULL
? errval
: str2short(string
);
882 return(Strsave(strF
));
891 i
= (tcsh_number_t
) st
->st_nlink
;
895 string
= string0
+ 1;
896 (void) xsnprintf(string
, sizeof(string0
) - 1, "%o",
897 pmask
& (unsigned int)
898 ((S_IRWXU
|S_IRWXG
|S_IRWXO
|S_ISUID
|S_ISGID
) & st
->st_mode
));
899 if (altout
&& *string
!= '0')
902 return(Strsave(str2short(string
)));
905 if (altout
&& (pw
= xgetpwuid(st
->st_uid
))) {
907 return(Strsave(str2short(pw
->pw_name
)));
909 i
= (tcsh_number_t
) st
->st_uid
;
913 if (altout
&& (gr
= xgetgrgid(st
->st_gid
))) {
915 return(Strsave(str2short(gr
->gr_name
)));
917 i
= (tcsh_number_t
) st
->st_gid
;
921 i
= (tcsh_number_t
) st
->st_size
;
924 case 'A': case 'M': case 'C':
925 footime
= *ft
== 'A' ? st
->st_atime
:
926 *ft
== 'M' ? st
->st_mtime
: st
->st_ctime
;
928 strF
= str2short(ctime(&footime
));
929 if ((str
= Strchr(strF
, '\n')) != NULL
)
932 return(Strsave(strF
));
934 i
= (tcsh_number_t
) footime
;
940 etraci("exp6 -? i", i
, vp
);
949 struct wordent paraml1
;
950 struct wordent
*hp
= ¶ml1
;
952 struct wordent
*wdp
= hp
;
957 struct wordent
*new = xcalloc(1, sizeof *wdp
);
963 wdp
->word
= Strsave(*v
++);
966 cleanup_push(¶ml1
, lex_cleanup
);
968 t
= syntax(paraml1
.next
, ¶ml1
, 0);
969 cleanup_push(t
, syntax_cleanup
);
972 execute(t
, -1, NULL
, NULL
, TRUE
);
973 cleanup_until(¶ml1
);
977 isa(Char
*cp
, int what
)
980 return ((what
& RESTOP
) != 0);
984 if (what
& ADDOP
&& (*cp
== '+' || *cp
== '-'))
986 if (what
& MULOP
&& (*cp
== '*' || *cp
== '/' || *cp
== '%'))
988 if (what
& RESTOP
&& (*cp
== '(' || *cp
== ')' || *cp
== '!' ||
989 *cp
== '~' || *cp
== '^' || *cp
== '"'))
992 else if (cp
[2] == 0) {
994 if (cp
[0] == '|' && cp
[1] == '&')
996 if (cp
[0] == '<' && cp
[1] == '<')
998 if (cp
[0] == '>' && cp
[1] == '>')
1008 else if (cp
[0] == '!') {
1012 return (NOTEQMATCH
);
1025 static tcsh_number_t
1026 egetn(const Char
*cp
)
1028 if (*cp
&& *cp
!= '-' && !Isdigit(*cp
))
1029 stderror(ERR_NAME
| ERR_EXPRESSION
);
1037 etraci(const char *str
, tcsh_number_t i
, Char
***vp
)
1039 #ifdef HAVE_LONG_LONG
1040 xprintf("%s=%lld\t", str
, i
);
1042 xprintf("%s=%ld\t", str
, i
);
1048 etracc(const char *str
, const Char
*cp
, Char
***vp
)
1050 xprintf("%s=%S\t", str
, cp
);