xref: /onnv-gate/usr/src/lib/libshell/common/data/testops.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1982-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                  David Korn <dgk@research.att.com>                   *
184887Schin *                                                                      *
194887Schin ***********************************************************************/
204887Schin #pragma prototyped
214887Schin 
224887Schin /*
234887Schin  * tables for the test builin [[...]] and [...]
244887Schin  */
254887Schin 
264887Schin #include	<ast.h>
274887Schin 
284887Schin #include	"shtable.h"
294887Schin #include	"test.h"
304887Schin 
314887Schin /*
324887Schin  * This is the list of binary test and [[...]] operators
334887Schin  */
344887Schin 
354887Schin const Shtable_t shtab_testops[] =
364887Schin {
374887Schin 		"!=",		TEST_SNE,
384887Schin 		"-a",		TEST_AND,
394887Schin 		"-ef",		TEST_EF,
404887Schin 		"-eq",		TEST_EQ,
414887Schin 		"-ge",		TEST_GE,
424887Schin 		"-gt",		TEST_GT,
434887Schin 		"-le",		TEST_LE,
444887Schin 		"-lt",		TEST_LT,
454887Schin 		"-ne",		TEST_NE,
464887Schin 		"-nt",		TEST_NT,
474887Schin 		"-o",		TEST_OR,
484887Schin 		"-ot",		TEST_OT,
494887Schin 		"=",		TEST_SEQ,
504887Schin 		"==",		TEST_SEQ,
514887Schin 		"=~",           TEST_REP,
524887Schin 		"<",		TEST_SLT,
534887Schin 		">",		TEST_SGT,
544887Schin 		"]]",		TEST_END,
554887Schin 		"",		0
564887Schin };
574887Schin 
584887Schin const char sh_opttest[] =
594887Schin "[-1c?\n@(#)$Id: test (AT&T Research) 2003-03-18 $\n]"
604887Schin USAGE_LICENSE
614887Schin "[+NAME?test - evaluate expression]"
624887Schin "[+DESCRIPTION?\btest\b evaluates expressions and indicates its "
634887Schin 	"results based on the exit status.  Option parsing is not "
644887Schin 	"performed so that all arguments, including \b--\b are processed "
654887Schin 	" as operands.  The evaluation of the "
664887Schin 	"expression depends on the number of operands as follows:]{"
674887Schin 	"[+0?Evaluates to false.]"
684887Schin 	"[+1?True if argument is not an empty string.]"
694887Schin 	"[+2?If first operand is \b!\b, the result is True if the second "
704887Schin 		"operand an empty string.  Otherwise, it is evaluated "
714887Schin 		"as one of the unary expressions defined below.  If the "
724887Schin 		"unary operator is invalid and the second argument is \b--\b,"
734887Schin 		"then the first argument is processed as an option argument.]"
744887Schin 	"[+3?If first operand is \b!\b, the result is True if the second "
754887Schin 		"and third operand evaluated as a unary expression is False.  "
764887Schin 		"Otherwise, the three operands are evaluaged as one of the  "
774887Schin 		"binary expressions listed below.]"
784887Schin 	"[+4?If first operand is \b!\b, the result is True if the next "
794887Schin 		"three operands are a valid binary expression that is False.]"
804887Schin "}"
814887Schin "[If any \afile\a is of the form \b/dev/fd/\b\an\a, then file descriptor "
824887Schin 	"\an\a is checked.]"
834887Schin "[+?Unary expressions can be one of the following:]{"
844887Schin 	"[+-a \afile\a?True if \afile\a exists, obsolete.]"
854887Schin 	"[+-b \afile\a?True if \afile\a exists and is a block special file.]"
864887Schin 	"[+-c \afile\a?True if \afile\a exists and is a character special "
874887Schin 		"file.]"
884887Schin 	"[+-d \afile\a?True if \afile\a exists and is a directory.]"
894887Schin 	"[+-e \afile\a?True if \afile\a exists.]"
904887Schin 	"[+-f \afile\a?True if \afile\a exists and is a regular file.]"
914887Schin 	"[+-g \afile\a?True if \afile\a exists and has its set-group-id bit "
924887Schin 		"set.]"
934887Schin 	"[+-h \afile\a?True if \afile\a exists and is a symbolic link.]"
944887Schin 	"[+-k \afile\a?True if \afile\a exists and has its sticky bit on.]"
954887Schin #if SHOPT_TEST_L
964887Schin 	"[+-l \afile\a?True if \afile\a exists and is a symbolic link.]"
974887Schin #endif
984887Schin 	"[+-n \astring\a?True if length of \astring\a is non-zero.]"
994887Schin 	"[+-o \aoption\a?True if the shell option \aoption\a is enabled.]"
1004887Schin 	"[+-p \afile\a?True if \afile\a exists and is a pipe or fifo.]"
1014887Schin 	"[+-r \afile\a?True if \afile\a exists and is readable.]"
1024887Schin 	"[+-s \afile\a?True if \afile\a exists and has size > 0.]"
1034887Schin 	"[+-t \afildes\a?True if file descriptor number \afildes\a is "
1044887Schin 		"open and is associated with a terminal device.]"
1054887Schin 	"[+-u \afile\a?True if \afile\a exists and has its set-user-id bit "
1064887Schin 		"set.]"
10710898Sroland.mainz@nrubsig.org 	"[+-v \avarname\a?True if \avarname\a is a valid variable name that is set.]"
1084887Schin 	"[+-w \afile\a?True if \afile\a exists and is writable.]"
1094887Schin 	"[+-x \afile\a?True if \afile\a exists and is executable.  For a "
1104887Schin 		"directory it means that it can be searched.]"
1114887Schin 	"[+-z \astring\a?True if \astring\a is a zero length string.]"
1124887Schin 	"[+-G \afile\a?True if \afile\a exists and group is the effective "
1134887Schin 		"group id of the current process.]"
1144887Schin 	"[+-L \afile\a?True if \afile\a exists and is a symbolic link.]"
1154887Schin 	"[+-N \afile\a?True if \afile\a exists and has been modified since "
1164887Schin 		"it was last read.]"
1174887Schin 	"[+-O \afile\a?True if \afile\a exists and owner is the effective "
1184887Schin 		"user id of the current process.]"
11910898Sroland.mainz@nrubsig.org 	"[+-R \avarname\a?True if \avarname\a is a name reference.]"
1204887Schin 	"[+-S \afile\a?True if \afile\a exists and is a socket.]"
12110898Sroland.mainz@nrubsig.org #if SHOPT_FS_3D
12210898Sroland.mainz@nrubsig.org 	"[+-V \afile\a?True if \afile\a exists and is a version "
12310898Sroland.mainz@nrubsig.org 		"directory.]"
12410898Sroland.mainz@nrubsig.org #endif /* SHOPT_FS_3D */
1254887Schin "}"
1264887Schin "[+?Binary expressions can be one of the following:]{"
1274887Schin 	"[+\astring1\a = \astring2\a?True if \astring1\a is equal to "
1284887Schin 		"\astring2\a.]"
1294887Schin 	"[+\astring1\a == \astring2\a?True if \astring1\a is equal to "
1304887Schin 		"\astring2\a.]"
1314887Schin 	"[+\astring1\a != \astring2\a?True if \astring1\a is not equal to "
1324887Schin 		"\astring2\a.]"
1334887Schin 	"[+\anum1\a -eq \anum2\a?True if numerical value of \anum1\a is "
1344887Schin 		"equal to \anum2\a.]"
1354887Schin 	"[+\anum1\a -ne \anum2\a?True if numerical value of \anum1\a is not "
1364887Schin 		"equal to \anum2\a.]"
1374887Schin 	"[+\anum1\a -lt \anum2\a?True if numerical value of \anum1\a is less "
1384887Schin 		"than \anum2\a.]"
1394887Schin 	"[+\anum1\a -le \anum2\a?True if numerical value of \anum1\a is less "
1404887Schin 		"than or equal to \anum2\a.]"
1414887Schin 	"[+\anum1\a -gt \anum2\a?True if numerical value of \anum1\a is "
1424887Schin 		"greater than \anum2\a.]"
1434887Schin 	"[+\anum1\a -ge \anum2\a?True if numerical value of \anum1\a is "
1444887Schin 		"greater than or equal to \anum2\a.]"
1454887Schin 	"[+\afile1\a -nt \afile2\a?True if \afile1\a is newer than \afile2\a "
1464887Schin 		"or \afile2\a does not exist.]"
1474887Schin 	"[+\afile1\a -ot \afile2\a?True if \afile1\a is older than \afile2\a "
1484887Schin 		"or \afile2\a does not exist.]"
1494887Schin 	"[+\afile1\a -ef \afile2\a?True if \afile1\a is another name for "
1504887Schin 		"\afile2\a.  This will be true if \afile1\a is a hard link "
1514887Schin 		"or a symbolic link to \afile2\a.]"
1524887Schin "}"
1534887Schin "\n"
1544887Schin "\n[expression]\n"
1554887Schin "\n"
1564887Schin "[+EXIT STATUS?]{"
1574887Schin 	"[+0?Indicates that the specified expression is True.]"
1584887Schin 	"[+1?Indicates that the specified expression is False.]"
1594887Schin 	"[+>1?An error occurred.]"
1604887Schin "}"
1614887Schin 
1624887Schin "[+SEE ALSO?\blet\b(1), \bexpr\b(1)]"
1634887Schin ;
1644887Schin 
16510898Sroland.mainz@nrubsig.org const char test_opchars[]	= "HLNRSVOGCaeohrwxdcbfugkv"
1664887Schin #if SHOPT_TEST_L
1674887Schin 	"l"
1684887Schin #endif
1694887Schin 				"psnzt";
1704887Schin const char e_argument[]		= "argument expected";
1714887Schin const char e_missing[]		= "%s missing";
1724887Schin const char e_badop[]		= "%s: unknown operator";
1734887Schin const char e_tstbegin[]		= "[[ ! ";
1744887Schin const char e_tstend[]		= " ]]\n";
175