xref: /titanic_51/usr/src/lib/libshell/common/data/testops.c (revision c3a558e7c77127215b010652905be7916ec5a080)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1982-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                  David Korn <dgk@research.att.com>                   *
18 *                                                                      *
19 ***********************************************************************/
20 #pragma prototyped
21 
22 /*
23  * tables for the test builin [[...]] and [...]
24  */
25 
26 #include	<ast.h>
27 
28 #include	"shtable.h"
29 #include	"test.h"
30 
31 /*
32  * This is the list of binary test and [[...]] operators
33  */
34 
35 const Shtable_t shtab_testops[] =
36 {
37 		"!=",		TEST_SNE,
38 		"-a",		TEST_AND,
39 		"-ef",		TEST_EF,
40 		"-eq",		TEST_EQ,
41 		"-ge",		TEST_GE,
42 		"-gt",		TEST_GT,
43 		"-le",		TEST_LE,
44 		"-lt",		TEST_LT,
45 		"-ne",		TEST_NE,
46 		"-nt",		TEST_NT,
47 		"-o",		TEST_OR,
48 		"-ot",		TEST_OT,
49 		"=",		TEST_SEQ,
50 		"==",		TEST_SEQ,
51 		"=~",           TEST_REP,
52 		"<",		TEST_SLT,
53 		">",		TEST_SGT,
54 		"]]",		TEST_END,
55 		"",		0
56 };
57 
58 const char sh_opttest[] =
59 "[-1c?\n@(#)$Id: test (AT&T Research) 2003-03-18 $\n]"
60 USAGE_LICENSE
61 "[+NAME?test - evaluate expression]"
62 "[+DESCRIPTION?\btest\b evaluates expressions and indicates its "
63 	"results based on the exit status.  Option parsing is not "
64 	"performed so that all arguments, including \b--\b are processed "
65 	" as operands.  The evaluation of the "
66 	"expression depends on the number of operands as follows:]{"
67 	"[+0?Evaluates to false.]"
68 	"[+1?True if argument is not an empty string.]"
69 	"[+2?If first operand is \b!\b, the result is True if the second "
70 		"operand an empty string.  Otherwise, it is evaluated "
71 		"as one of the unary expressions defined below.  If the "
72 		"unary operator is invalid and the second argument is \b--\b,"
73 		"then the first argument is processed as an option argument.]"
74 	"[+3?If first operand is \b!\b, the result is True if the second "
75 		"and third operand evaluated as a unary expression is False.  "
76 		"Otherwise, the three operands are evaluaged as one of the  "
77 		"binary expressions listed below.]"
78 	"[+4?If first operand is \b!\b, the result is True if the next "
79 		"three operands are a valid binary expression that is False.]"
80 "}"
81 "[If any \afile\a is of the form \b/dev/fd/\b\an\a, then file descriptor "
82 	"\an\a is checked.]"
83 "[+?Unary expressions can be one of the following:]{"
84 	"[+-a \afile\a?True if \afile\a exists, obsolete.]"
85 	"[+-b \afile\a?True if \afile\a exists and is a block special file.]"
86 	"[+-c \afile\a?True if \afile\a exists and is a character special "
87 		"file.]"
88 	"[+-d \afile\a?True if \afile\a exists and is a directory.]"
89 	"[+-e \afile\a?True if \afile\a exists.]"
90 	"[+-f \afile\a?True if \afile\a exists and is a regular file.]"
91 	"[+-g \afile\a?True if \afile\a exists and has its set-group-id bit "
92 		"set.]"
93 	"[+-h \afile\a?True if \afile\a exists and is a symbolic link.]"
94 	"[+-k \afile\a?True if \afile\a exists and has its sticky bit on.]"
95 #if SHOPT_TEST_L
96 	"[+-l \afile\a?True if \afile\a exists and is a symbolic link.]"
97 #endif
98 	"[+-n \astring\a?True if length of \astring\a is non-zero.]"
99 	"[+-o \aoption\a?True if the shell option \aoption\a is enabled.]"
100 	"[+-p \afile\a?True if \afile\a exists and is a pipe or fifo.]"
101 	"[+-r \afile\a?True if \afile\a exists and is readable.]"
102 	"[+-s \afile\a?True if \afile\a exists and has size > 0.]"
103 	"[+-t \afildes\a?True if file descriptor number \afildes\a is "
104 		"open and is associated with a terminal device.]"
105 	"[+-u \afile\a?True if \afile\a exists and has its set-user-id bit "
106 		"set.]"
107 	"[+-v \avarname\a?True if \avarname\a is a valid variable name that is set.]"
108 	"[+-w \afile\a?True if \afile\a exists and is writable.]"
109 	"[+-x \afile\a?True if \afile\a exists and is executable.  For a "
110 		"directory it means that it can be searched.]"
111 	"[+-z \astring\a?True if \astring\a is a zero length string.]"
112 	"[+-G \afile\a?True if \afile\a exists and group is the effective "
113 		"group id of the current process.]"
114 	"[+-L \afile\a?True if \afile\a exists and is a symbolic link.]"
115 	"[+-N \afile\a?True if \afile\a exists and has been modified since "
116 		"it was last read.]"
117 	"[+-O \afile\a?True if \afile\a exists and owner is the effective "
118 		"user id of the current process.]"
119 	"[+-R \avarname\a?True if \avarname\a is a name reference.]"
120 	"[+-S \afile\a?True if \afile\a exists and is a socket.]"
121 #if SHOPT_FS_3D
122 	"[+-V \afile\a?True if \afile\a exists and is a version "
123 		"directory.]"
124 #endif /* SHOPT_FS_3D */
125 "}"
126 "[+?Binary expressions can be one of the following:]{"
127 	"[+\astring1\a = \astring2\a?True if \astring1\a is equal to "
128 		"\astring2\a.]"
129 	"[+\astring1\a == \astring2\a?True if \astring1\a is equal to "
130 		"\astring2\a.]"
131 	"[+\astring1\a != \astring2\a?True if \astring1\a is not equal to "
132 		"\astring2\a.]"
133 	"[+\anum1\a -eq \anum2\a?True if numerical value of \anum1\a is "
134 		"equal to \anum2\a.]"
135 	"[+\anum1\a -ne \anum2\a?True if numerical value of \anum1\a is not "
136 		"equal to \anum2\a.]"
137 	"[+\anum1\a -lt \anum2\a?True if numerical value of \anum1\a is less "
138 		"than \anum2\a.]"
139 	"[+\anum1\a -le \anum2\a?True if numerical value of \anum1\a is less "
140 		"than or equal to \anum2\a.]"
141 	"[+\anum1\a -gt \anum2\a?True if numerical value of \anum1\a is "
142 		"greater than \anum2\a.]"
143 	"[+\anum1\a -ge \anum2\a?True if numerical value of \anum1\a is "
144 		"greater than or equal to \anum2\a.]"
145 	"[+\afile1\a -nt \afile2\a?True if \afile1\a is newer than \afile2\a "
146 		"or \afile2\a does not exist.]"
147 	"[+\afile1\a -ot \afile2\a?True if \afile1\a is older than \afile2\a "
148 		"or \afile2\a does not exist.]"
149 	"[+\afile1\a -ef \afile2\a?True if \afile1\a is another name for "
150 		"\afile2\a.  This will be true if \afile1\a is a hard link "
151 		"or a symbolic link to \afile2\a.]"
152 "}"
153 "\n"
154 "\n[expression]\n"
155 "\n"
156 "[+EXIT STATUS?]{"
157 	"[+0?Indicates that the specified expression is True.]"
158 	"[+1?Indicates that the specified expression is False.]"
159 	"[+>1?An error occurred.]"
160 "}"
161 
162 "[+SEE ALSO?\blet\b(1), \bexpr\b(1)]"
163 ;
164 
165 const char test_opchars[]	= "HLNRSVOGCaeohrwxdcbfugkv"
166 #if SHOPT_TEST_L
167 	"l"
168 #endif
169 				"psnzt";
170 const char e_argument[]		= "argument expected";
171 const char e_missing[]		= "%s missing";
172 const char e_badop[]		= "%s: unknown operator";
173 const char e_tstbegin[]		= "[[ ! ";
174 const char e_tstend[]		= " ]]\n";
175