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
39 #define TEXP_IGNORE 1 /* in ignore, it means to ignore value, just parse */
40 #define TEXP_NOGLOB 2 /* in ignore, it means not to globone */
56 static int sh_access (const Char
*, int);
57 static tcsh_number_t
exp1 (Char
***, int);
58 static tcsh_number_t
exp2x (Char
***, int);
59 static tcsh_number_t
exp2a (Char
***, int);
60 static tcsh_number_t
exp2b (Char
***, int);
61 static tcsh_number_t
exp2c (Char
***, int);
62 static Char
*exp3 (Char
***, int);
63 static Char
*exp3a (Char
***, int);
64 static Char
*exp4 (Char
***, int);
65 static Char
*exp5 (Char
***, int);
66 static Char
*exp6 (Char
***, int);
67 static void evalav (Char
**);
68 static int isa (Char
*, int);
69 static tcsh_number_t
egetn (const Char
*);
72 static void etracc (const char *, const Char
*, Char
***);
73 static void etraci (const char *, tcsh_number_t
, Char
***);
75 #define etracc(A, B, C) ((void)0)
76 #define etraci(A, B, C) ((void)0)
80 * shell access function according to POSIX and non POSIX
81 * From Beto Appleton (beto@aixwiz.aix.ibm.com)
84 sh_access(const Char
*fname
, int mode
)
86 #if defined(POSIX) && !defined(USE_ACCESS)
89 char *name
= short2str(fname
);
94 #if !defined(POSIX) || defined(USE_ACCESS)
95 return access(name
, mode
);
100 * -r file True if file exists and is readable.
101 * -w file True if file exists and is writable.
102 * True shall indicate only that the write flag is on.
103 * The file shall not be writable on a read-only file
104 * system even if this test indicates true.
105 * -x file True if file exists and is executable.
106 * True shall indicate only that the execute flag is on.
107 * If file is a directory, true indicates that the file
110 if (mode
!= W_OK
&& mode
!= X_OK
)
111 return access(name
, mode
);
113 if (stat(name
, &statb
) == -1)
116 if (access(name
, mode
) == 0) {
118 if (S_ISDIR(statb
.st_mode
) && mode
== X_OK
)
122 /* root needs permission for someone */
125 mode
= S_IWUSR
| S_IWGRP
| S_IWOTH
;
128 mode
= S_IXUSR
| S_IXGRP
| S_IXOTH
;
137 else if (euid
== statb
.st_uid
)
140 else if (egid
== statb
.st_gid
)
145 /* you can be in several groups */
150 * Try these things to find a positive maximum groups value:
151 * 1) sysconf(_SC_NGROUPS_MAX)
153 * 3) getgroups(0, unused)
154 * Then allocate and scan the groups array if one of these worked.
156 # if defined (HAVE_SYSCONF) && defined (_SC_NGROUPS_MAX)
157 if ((n
= sysconf(_SC_NGROUPS_MAX
)) == -1)
158 # endif /* _SC_NGROUPS_MAX */
161 n
= getgroups(0, (GETGROUPS_T
*) NULL
);
164 groups
= xmalloc(n
* sizeof(*groups
));
165 n
= getgroups((int) n
, groups
);
167 if (groups
[n
] == statb
.st_gid
) {
174 # endif /* NGROUPS_MAX */
176 if (statb
.st_mode
& mode
)
186 return (exp0(vp
, 0));
190 exp0(Char
***vp
, int ignore
)
192 tcsh_number_t p1
= exp1(vp
, ignore
);
194 etraci("exp0 p1", p1
, vp
);
195 while (**vp
&& eq(**vp
, STRor2
)) {
201 exp0(vp
, (ignore
& TEXP_IGNORE
) || p1
) :
202 exp1(vp
, (ignore
& TEXP_IGNORE
) || p1
);
203 if (compat_expr
|| !(ignore
& TEXP_IGNORE
))
205 etraci("exp0 p1", p1
, vp
);
213 exp1(Char
***vp
, int ignore
)
215 tcsh_number_t p1
= exp2x(vp
, ignore
);
217 etraci("exp1 p1", p1
, vp
);
218 while (**vp
&& eq(**vp
, STRand2
)) {
223 exp1(vp
, (ignore
& TEXP_IGNORE
) || !p1
) :
224 exp2x(vp
, (ignore
& TEXP_IGNORE
) || !p1
);
226 etraci("exp1 p2", p2
, vp
);
227 if (compat_expr
|| !(ignore
& TEXP_IGNORE
))
229 etraci("exp1 p1", p1
, vp
);
237 exp2x(Char
***vp
, int ignore
)
239 tcsh_number_t p1
= exp2a(vp
, ignore
);
241 etraci("exp2x p1", p1
, vp
);
242 while (**vp
&& eq(**vp
, STRor
)) {
249 etraci("exp2x p2", p2
, vp
);
250 if (compat_expr
|| !(ignore
& TEXP_IGNORE
))
252 etraci("exp2x p1", p1
, vp
);
260 exp2a(Char
***vp
, int ignore
)
262 tcsh_number_t p1
= exp2b(vp
, ignore
);
264 etraci("exp2a p1", p1
, vp
);
265 while (**vp
&& eq(**vp
, STRcaret
)) {
272 etraci("exp2a p2", p2
, vp
);
273 if (compat_expr
|| !(ignore
& TEXP_IGNORE
))
275 etraci("exp2a p1", p1
, vp
);
283 exp2b(Char
***vp
, int ignore
)
285 tcsh_number_t p1
= exp2c(vp
, ignore
);
287 etraci("exp2b p1", p1
, vp
);
288 while (**vp
&& eq(**vp
, STRand
)) {
295 etraci("exp2b p2", p2
, vp
);
296 if (compat_expr
|| !(ignore
& TEXP_IGNORE
))
298 etraci("exp2b p1", p1
, vp
);
306 exp2c(Char
***vp
, int ignore
)
308 Char
*p1
= exp3(vp
, ignore
);
312 cleanup_push(p1
, xfree
);
313 etracc("exp2c p1", p1
, vp
);
314 if ((i
= isa(**vp
, EQOP
)) != 0) {
316 if (i
== EQMATCH
|| i
== NOTEQMATCH
)
317 ignore
|= TEXP_NOGLOB
;
318 p2
= exp3(vp
, ignore
);
319 cleanup_push(p2
, xfree
);
320 etracc("exp2c p2", p2
, vp
);
321 if (!(ignore
& TEXP_IGNORE
))
349 exp3(Char
***vp
, int ignore
)
354 p1
= exp3a(vp
, ignore
);
355 etracc("exp3 p1", p1
, vp
);
356 while ((i
= isa(**vp
, RELOP
)) != 0) {
358 if (**vp
&& eq(**vp
, STRequal
))
360 cleanup_push(p1
, xfree
);
364 cleanup_push(p2
, xfree
);
365 etracc("exp3 p2", p2
, vp
);
366 if (!(ignore
& TEXP_IGNORE
))
370 i
= egetn(p1
) > egetn(p2
);
374 i
= egetn(p1
) >= egetn(p2
);
378 i
= egetn(p1
) < egetn(p2
);
382 i
= egetn(p1
) <= egetn(p2
);
387 etracc("exp3 p1", p1
, vp
);
395 exp3a(Char
***vp
, int ignore
)
401 p1
= exp4(vp
, ignore
);
402 etracc("exp3a p1", p1
, vp
);
404 if (op
&& any("<>", op
[0]) && op
[0] == op
[1]) {
406 cleanup_push(p1
, xfree
);
410 cleanup_push(p2
, xfree
);
411 etracc("exp3a p2", p2
, vp
);
413 i
= egetn(p1
) << egetn(p2
);
415 i
= egetn(p1
) >> egetn(p2
);
418 etracc("exp3a p1", p1
, vp
);
424 exp4(Char
***vp
, int ignore
)
429 p1
= exp5(vp
, ignore
);
430 etracc("exp4 p1", p1
, vp
);
431 while (isa(**vp
, ADDOP
)) {
432 const Char
*op
= *(*vp
)++;
434 cleanup_push(p1
, xfree
);
438 cleanup_push(p2
, xfree
);
439 etracc("exp4 p2", p2
, vp
);
440 if (!(ignore
& TEXP_IGNORE
))
444 i
= egetn(p1
) + egetn(p2
);
448 i
= egetn(p1
) - egetn(p2
);
453 etracc("exp4 p1", p1
, vp
);
461 exp5(Char
***vp
, int ignore
)
466 p1
= exp6(vp
, ignore
);
467 etracc("exp5 p1", p1
, vp
);
469 while (isa(**vp
, MULOP
)) {
470 const Char
*op
= *(*vp
)++;
471 if ((ignore
& TEXP_NOGLOB
) != 0) {
473 * We are just trying to get the right side of
474 * a =~ or !~ operator
480 cleanup_push(p1
, xfree
);
484 cleanup_push(p2
, xfree
);
485 etracc("exp5 p2", p2
, vp
);
486 if (!(ignore
& TEXP_IGNORE
))
490 i
= egetn(p1
) * egetn(p2
);
509 etracc("exp5 p1", p1
, vp
);
517 exp6(Char
***vp
, int ignore
)
524 stderror(ERR_NAME
| ERR_EXPRESSION
);
525 if (eq(**vp
, STRbang
)) {
527 cp
= exp6(vp
, ignore
);
528 cleanup_push(cp
, xfree
);
529 etracc("exp6 ! cp", cp
, vp
);
534 if (eq(**vp
, STRtilde
)) {
536 cp
= exp6(vp
, ignore
);
537 cleanup_push(cp
, xfree
);
538 etracc("exp6 ~ cp", cp
, vp
);
543 if (eq(**vp
, STRLparen
)) {
545 ccode
= exp0(vp
, ignore
);
546 etraci("exp6 () ccode", ccode
, vp
);
547 if (**vp
== 0 || ***vp
!= ')')
548 stderror(ERR_NAME
| ERR_EXPRESSION
);
550 return (putn(ccode
));
552 if (eq(**vp
, STRLbrace
)) {
554 struct command faket
;
557 faket
.t_dtyp
= NODE_COMMAND
;
558 faket
.t_dflg
= F_BACKQ
;
559 faket
.t_dcar
= faket
.t_dcdr
= faket
.t_dspr
= NULL
;
560 faket
.t_dcom
= fakecom
;
561 fakecom
[0] = STRfakecom
;
567 stderror(ERR_NAME
| ERR_MISSING
, '}');
568 if (eq(*(*vp
)++, STRRbrace
))
571 if (ignore
& TEXP_IGNORE
)
572 return (Strsave(STRNULL
));
574 cleanup_push(&faket
, psavejob_cleanup
); /* faket is only a marker */
575 if (pfork(&faket
, -1) == 0) {
581 cleanup_until(&faket
);
582 etraci("exp6 {} status", egetn(varval(STRstatus
)), vp
);
583 return (putn(egetn(varval(STRstatus
)) == 0));
585 if (isa(**vp
, ANYOP
))
586 return (Strsave(STRNULL
));
589 # define FILETESTS "erwxfdzoplstSXLbcugkmKR"
591 # define FILETESTS "erwxfdzoplstSXLbcugkmK"
593 #define FILEVALS "ZAMCDIUGNFPL"
594 if (*cp
== '-' && (any(FILETESTS
, cp
[1]) || any(FILEVALS
, cp
[1])))
595 return(filetest(cp
, vp
, ignore
));
596 etracc("exp6 default", cp
, vp
);
597 return (ignore
& TEXP_NOGLOB
? Strsave(cp
) : globone(cp
, G_APPEND
));
602 * Extended file tests
603 * From: John Rowe <rowe@excc.exeter.ac.uk>
606 filetest(Char
*cp
, Char
***vp
, int ignore
)
609 struct cvxstat stb
, *st
= NULL
;
610 # define TCSH_STAT stat64
612 # define TCSH_STAT stat
613 struct stat stb
, *st
= NULL
;
618 struct cvxstat lstb
, *lst
= NULL
;
619 # define TCSH_LSTAT lstat64
621 # define TCSH_LSTAT lstat
622 struct stat lstb
, *lst
= NULL
;
628 unsigned pmask
= 0xffff;
630 Char
*ft
= cp
, *dp
, *ep
, *strdev
, *strino
, *strF
, *str
, valtest
= '\0',
632 char *string
, string0
[22 + MB_LEN_MAX
+ 1]; /* space for 64 bit octal */
637 while(any(FILETESTS
, *++ft
))
640 if (!*ft
&& *(ft
- 1) == 'L')
643 if (any(FILEVALS
, *ft
)) {
646 * Value tests return '-1' on failure as 0 is
647 * a legitimate value for many of them.
648 * 'F' returns ':' for compatibility.
650 errval
= valtest
== 'F' ? STRcolon
: STRminus1
;
652 if (valtest
== 'P' && *ft
>= '0' && *ft
<= '7') {
653 pmask
= (char) *ft
- '0';
654 while ( *++ft
>= '0' && *ft
<= '7' )
655 pmask
= 8 * pmask
+ ((char) *ft
- '0');
657 if (Strcmp(ft
, STRcolon
) == 0 && any("AMCUGP", valtest
)) {
663 if (*ft
|| ft
== cp
+ 1)
664 stderror(ERR_NAME
| ERR_FILEINQ
);
667 * Detect missing file names by checking for operator in the file name
668 * position. However, if an operator name appears there, we must make
669 * sure that there's no file by that name (e.g., "/") before announcing
670 * an error. Even this check isn't quite right, since it doesn't take
671 * globbing into account.
674 if (isa(**vp
, ANYOP
) && TCSH_STAT(short2str(**vp
), &stb
))
675 stderror(ERR_NAME
| ERR_FILENAME
);
678 if (ignore
& TEXP_IGNORE
)
679 return (Strsave(STRNULL
));
680 ep
= globone(dp
, G_APPEND
);
681 cleanup_push(ep
, xfree
);
687 i
= !sh_access(ep
, R_OK
);
691 i
= !sh_access(ep
, W_OK
);
695 i
= !sh_access(ep
, X_OK
);
698 case 'X': /* tcsh extension, name is an executable in the path
699 * or a tcsh builtin command
704 case 't': /* SGI extension, true when file is a tty */
705 i
= isatty(atoi(short2str(ep
)));
711 if (tolower(*ft
) == 'l') {
713 * avoid convex compiler bug.
717 if (TCSH_LSTAT(short2str(ep
), lst
) == -1) {
719 return (Strsave(errval
));
728 * avoid convex compiler bug.
732 if (TCSH_STAT(short2str(ep
), st
) == -1) {
734 return (Strsave(errval
));
742 i
= S_ISREG(st
->st_mode
);
750 i
= S_ISDIR(st
->st_mode
);
758 i
= S_ISFIFO(st
->st_mode
);
759 #else /* !S_ISFIFO */
761 #endif /* S_ISFIFO */
766 i
= S_ISOFL(st
->st_dm_mode
);
783 i
= S_ISLNK(lst
->st_mode
);
791 i
= S_ISSOCK(st
->st_mode
);
792 # else /* !S_ISSOCK */
794 # endif /* S_ISSOCK */
799 i
= S_ISBLK(st
->st_mode
);
807 i
= S_ISCHR(st
->st_mode
);
814 i
= (S_ISUID
& st
->st_mode
) != 0;
818 i
= (S_ISGID
& st
->st_mode
) != 0;
822 i
= (S_ISVTX
& st
->st_mode
) != 0;
826 i
= st
->st_size
== 0;
831 i
= (stb
.st_dmonflags
& IMIGRATED
) == IMIGRATED
;
836 i
= stb
.st_size
!= 0;
844 i
= st
->st_uid
== uid
;
848 * Value operators are a tcsh extension.
852 i
= (tcsh_number_t
) st
->st_dev
;
856 i
= (tcsh_number_t
) st
->st_ino
;
860 strdev
= putn( (int) st
->st_dev
);
861 strino
= putn( (int) st
->st_ino
);
862 strF
= xmalloc((2 + Strlen(strdev
) + Strlen(strino
))
864 (void) Strcat(Strcat(Strcpy(strF
, strdev
), STRcolon
), strino
);
876 filnam
= short2str(ep
);
877 string
= areadlink(filnam
);
878 strF
= string
== NULL
? errval
: str2short(string
);
881 return(Strsave(strF
));
890 i
= (tcsh_number_t
) st
->st_nlink
;
894 string
= string0
+ 1;
895 (void) xsnprintf(string
, sizeof(string0
) - 1, "%o",
896 pmask
& (unsigned int)
897 ((S_IRWXU
|S_IRWXG
|S_IRWXO
|S_ISUID
|S_ISGID
) & st
->st_mode
));
898 if (altout
&& *string
!= '0')
901 return(Strsave(str2short(string
)));
904 if (altout
&& (pw
= xgetpwuid(st
->st_uid
))) {
906 return(Strsave(str2short(pw
->pw_name
)));
908 i
= (tcsh_number_t
) st
->st_uid
;
912 if (altout
&& (gr
= xgetgrgid(st
->st_gid
))) {
914 return(Strsave(str2short(gr
->gr_name
)));
916 i
= (tcsh_number_t
) st
->st_gid
;
920 i
= (tcsh_number_t
) st
->st_size
;
923 case 'A': case 'M': case 'C':
924 footime
= *ft
== 'A' ? st
->st_atime
:
925 *ft
== 'M' ? st
->st_mtime
: st
->st_ctime
;
927 strF
= str2short(ctime(&footime
));
928 if ((str
= Strchr(strF
, '\n')) != NULL
)
931 return(Strsave(strF
));
933 i
= (tcsh_number_t
) footime
;
939 etraci("exp6 -? i", i
, vp
);
948 struct wordent paraml1
;
949 struct wordent
*hp
= ¶ml1
;
951 struct wordent
*wdp
= hp
;
953 setcopy(STRstatus
, STR0
, VAR_READWRITE
);
956 struct wordent
*new = xcalloc(1, sizeof *wdp
);
962 wdp
->word
= Strsave(*v
++);
965 cleanup_push(¶ml1
, lex_cleanup
);
967 t
= syntax(paraml1
.next
, ¶ml1
, 0);
968 cleanup_push(t
, syntax_cleanup
);
971 execute(t
, -1, NULL
, NULL
, TRUE
);
972 cleanup_until(¶ml1
);
976 isa(Char
*cp
, int what
)
979 return ((what
& RESTOP
) != 0);
983 if (what
& ADDOP
&& (*cp
== '+' || *cp
== '-'))
985 if (what
& MULOP
&& (*cp
== '*' || *cp
== '/' || *cp
== '%'))
987 if (what
& RESTOP
&& (*cp
== '(' || *cp
== ')' || *cp
== '!' ||
988 *cp
== '~' || *cp
== '^' || *cp
== '"'))
991 else if (cp
[2] == 0) {
993 if (cp
[0] == '|' && cp
[1] == '&')
995 if (cp
[0] == '<' && cp
[1] == '<')
997 if (cp
[0] == '>' && cp
[1] == '>')
1007 else if (cp
[0] == '!') {
1011 return (NOTEQMATCH
);
1024 static tcsh_number_t
1025 egetn(const Char
*cp
)
1027 if (*cp
&& *cp
!= '-' && !Isdigit(*cp
))
1028 stderror(ERR_NAME
| ERR_EXPRESSION
);
1036 etraci(const char *str
, tcsh_number_t i
, Char
***vp
)
1038 #ifdef HAVE_LONG_LONG
1039 xprintf("%s=%lld\t", str
, i
);
1041 xprintf("%s=%ld\t", str
, i
);
1047 etracc(const char *str
, const Char
*cp
, Char
***vp
)
1049 xprintf("%s=%S\t", str
, cp
);