testops.c revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968
29949e866e40b95795203f3ee46f44a197c946e4stevel/***********************************************************************
29949e866e40b95795203f3ee46f44a197c946e4stevel* *
29949e866e40b95795203f3ee46f44a197c946e4stevel* This software is part of the ast package *
29949e866e40b95795203f3ee46f44a197c946e4stevel* Copyright (c) 1982-2007 AT&T Knowledge Ventures *
29949e866e40b95795203f3ee46f44a197c946e4stevel* and is licensed under the *
29949e866e40b95795203f3ee46f44a197c946e4stevel* Common Public License, Version 1.0 *
29949e866e40b95795203f3ee46f44a197c946e4stevel* by AT&T Knowledge Ventures *
29949e866e40b95795203f3ee46f44a197c946e4stevel* *
29949e866e40b95795203f3ee46f44a197c946e4stevel* A copy of the License is available at *
29949e866e40b95795203f3ee46f44a197c946e4stevel* http://www.opensource.org/licenses/cpl1.0.txt *
29949e866e40b95795203f3ee46f44a197c946e4stevel* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
29949e866e40b95795203f3ee46f44a197c946e4stevel* *
29949e866e40b95795203f3ee46f44a197c946e4stevel* Information and Software Systems Research *
29949e866e40b95795203f3ee46f44a197c946e4stevel* AT&T Research *
29949e866e40b95795203f3ee46f44a197c946e4stevel* Florham Park NJ *
29949e866e40b95795203f3ee46f44a197c946e4stevel* *
29949e866e40b95795203f3ee46f44a197c946e4stevel* David Korn <dgk@research.att.com> *
29949e866e40b95795203f3ee46f44a197c946e4stevel* *
29949e866e40b95795203f3ee46f44a197c946e4stevel***********************************************************************/
29949e866e40b95795203f3ee46f44a197c946e4stevel#pragma prototyped
29949e866e40b95795203f3ee46f44a197c946e4stevel
29949e866e40b95795203f3ee46f44a197c946e4stevel/*
29949e866e40b95795203f3ee46f44a197c946e4stevel * tables for the test builin [[...]] and [...]
29949e866e40b95795203f3ee46f44a197c946e4stevel */
29949e866e40b95795203f3ee46f44a197c946e4stevel
29949e866e40b95795203f3ee46f44a197c946e4stevel#include <ast.h>
29949e866e40b95795203f3ee46f44a197c946e4stevel
29949e866e40b95795203f3ee46f44a197c946e4stevel#include "shtable.h"
29949e866e40b95795203f3ee46f44a197c946e4stevel#include "test.h"
29949e866e40b95795203f3ee46f44a197c946e4stevel
29949e866e40b95795203f3ee46f44a197c946e4stevel/*
29949e866e40b95795203f3ee46f44a197c946e4stevel * This is the list of binary test and [[...]] operators
29949e866e40b95795203f3ee46f44a197c946e4stevel */
29949e866e40b95795203f3ee46f44a197c946e4stevel
29949e866e40b95795203f3ee46f44a197c946e4stevelconst Shtable_t shtab_testops[] =
29949e866e40b95795203f3ee46f44a197c946e4stevel{
29949e866e40b95795203f3ee46f44a197c946e4stevel "!=", TEST_SNE,
29949e866e40b95795203f3ee46f44a197c946e4stevel "-a", TEST_AND,
29949e866e40b95795203f3ee46f44a197c946e4stevel "-ef", TEST_EF,
29949e866e40b95795203f3ee46f44a197c946e4stevel "-eq", TEST_EQ,
29949e866e40b95795203f3ee46f44a197c946e4stevel "-ge", TEST_GE,
29949e866e40b95795203f3ee46f44a197c946e4stevel "-gt", TEST_GT,
29949e866e40b95795203f3ee46f44a197c946e4stevel "-le", TEST_LE,
29949e866e40b95795203f3ee46f44a197c946e4stevel "-lt", TEST_LT,
29949e866e40b95795203f3ee46f44a197c946e4stevel "-ne", TEST_NE,
29949e866e40b95795203f3ee46f44a197c946e4stevel "-nt", TEST_NT,
29949e866e40b95795203f3ee46f44a197c946e4stevel "-o", TEST_OR,
29949e866e40b95795203f3ee46f44a197c946e4stevel "-ot", TEST_OT,
29949e866e40b95795203f3ee46f44a197c946e4stevel "=", TEST_SEQ,
29949e866e40b95795203f3ee46f44a197c946e4stevel "==", TEST_SEQ,
29949e866e40b95795203f3ee46f44a197c946e4stevel "=~", TEST_REP,
29949e866e40b95795203f3ee46f44a197c946e4stevel "<", TEST_SLT,
29949e866e40b95795203f3ee46f44a197c946e4stevel ">", TEST_SGT,
29949e866e40b95795203f3ee46f44a197c946e4stevel "]]", TEST_END,
29949e866e40b95795203f3ee46f44a197c946e4stevel "", 0
29949e866e40b95795203f3ee46f44a197c946e4stevel};
29949e866e40b95795203f3ee46f44a197c946e4stevel
29949e866e40b95795203f3ee46f44a197c946e4stevelconst char sh_opttest[] =
29949e866e40b95795203f3ee46f44a197c946e4stevel"[-1c?\n@(#)$Id: test (AT&T Research) 2003-03-18 $\n]"
29949e866e40b95795203f3ee46f44a197c946e4stevelUSAGE_LICENSE
29949e866e40b95795203f3ee46f44a197c946e4stevel"[+NAME?test - evaluate expression]"
29949e866e40b95795203f3ee46f44a197c946e4stevel"[+DESCRIPTION?\btest\b evaluates expressions and indicates its "
29949e866e40b95795203f3ee46f44a197c946e4stevel "results based on the exit status. Option parsing is not "
29949e866e40b95795203f3ee46f44a197c946e4stevel "performed so that all arguments, including \b--\b are processed "
29949e866e40b95795203f3ee46f44a197c946e4stevel " as operands. The evaluation of the "
29949e866e40b95795203f3ee46f44a197c946e4stevel "expression depends on the number of operands as follows:]{"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+0?Evaluates to false.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+1?True if argument is not an empty string.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+2?If first operand is \b!\b, the result is True if the second "
29949e866e40b95795203f3ee46f44a197c946e4stevel "operand an empty string. Otherwise, it is evaluated "
29949e866e40b95795203f3ee46f44a197c946e4stevel "as one of the unary expressions defined below. If the "
29949e866e40b95795203f3ee46f44a197c946e4stevel "unary operator is invalid and the second argument is \b--\b,"
29949e866e40b95795203f3ee46f44a197c946e4stevel "then the first argument is processed as an option argument.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+3?If first operand is \b!\b, the result is True if the second "
29949e866e40b95795203f3ee46f44a197c946e4stevel "and third operand evaluated as a unary expression is False. "
29949e866e40b95795203f3ee46f44a197c946e4stevel "Otherwise, the three operands are evaluaged as one of the "
29949e866e40b95795203f3ee46f44a197c946e4stevel "binary expressions listed below.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+4?If first operand is \b!\b, the result is True if the next "
29949e866e40b95795203f3ee46f44a197c946e4stevel "three operands are a valid binary expression that is False.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel"}"
29949e866e40b95795203f3ee46f44a197c946e4stevel"[If any \afile\a is of the form \b/dev/fd/\b\an\a, then file descriptor "
29949e866e40b95795203f3ee46f44a197c946e4stevel "\an\a is checked.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel"[+?Unary expressions can be one of the following:]{"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-a \afile\a?True if \afile\a exists, obsolete.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-b \afile\a?True if \afile\a exists and is a block special file.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-c \afile\a?True if \afile\a exists and is a character special "
29949e866e40b95795203f3ee46f44a197c946e4stevel "file.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-d \afile\a?True if \afile\a exists and is a directory.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-e \afile\a?True if \afile\a exists.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-f \afile\a?True if \afile\a exists and is a regular file.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-g \afile\a?True if \afile\a exists and has its set-group-id bit "
29949e866e40b95795203f3ee46f44a197c946e4stevel "set.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-h \afile\a?True if \afile\a exists and is a symbolic link.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-k \afile\a?True if \afile\a exists and has its sticky bit on.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel#if SHOPT_TEST_L
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-l \afile\a?True if \afile\a exists and is a symbolic link.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel#endif
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-n \astring\a?True if length of \astring\a is non-zero.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-o \aoption\a?True if the shell option \aoption\a is enabled.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-p \afile\a?True if \afile\a exists and is a pipe or fifo.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-r \afile\a?True if \afile\a exists and is readable.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-s \afile\a?True if \afile\a exists and has size > 0.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-t \afildes\a?True if file descriptor number \afildes\a is "
29949e866e40b95795203f3ee46f44a197c946e4stevel "open and is associated with a terminal device.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-u \afile\a?True if \afile\a exists and has its set-user-id bit "
29949e866e40b95795203f3ee46f44a197c946e4stevel "set.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-w \afile\a?True if \afile\a exists and is writable.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-x \afile\a?True if \afile\a exists and is executable. For a "
29949e866e40b95795203f3ee46f44a197c946e4stevel "directory it means that it can be searched.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-z \astring\a?True if \astring\a is a zero length string.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-G \afile\a?True if \afile\a exists and group is the effective "
29949e866e40b95795203f3ee46f44a197c946e4stevel "group id of the current process.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-L \afile\a?True if \afile\a exists and is a symbolic link.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-N \afile\a?True if \afile\a exists and has been modified since "
29949e866e40b95795203f3ee46f44a197c946e4stevel "it was last read.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-O \afile\a?True if \afile\a exists and owner is the effective "
29949e866e40b95795203f3ee46f44a197c946e4stevel "user id of the current process.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+-S \afile\a?True if \afile\a exists and is a socket.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel"}"
29949e866e40b95795203f3ee46f44a197c946e4stevel"[+?Binary expressions can be one of the following:]{"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\astring1\a = \astring2\a?True if \astring1\a is equal to "
29949e866e40b95795203f3ee46f44a197c946e4stevel "\astring2\a.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\astring1\a == \astring2\a?True if \astring1\a is equal to "
29949e866e40b95795203f3ee46f44a197c946e4stevel "\astring2\a.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\astring1\a != \astring2\a?True if \astring1\a is not equal to "
29949e866e40b95795203f3ee46f44a197c946e4stevel "\astring2\a.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\anum1\a -eq \anum2\a?True if numerical value of \anum1\a is "
29949e866e40b95795203f3ee46f44a197c946e4stevel "equal to \anum2\a.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\anum1\a -ne \anum2\a?True if numerical value of \anum1\a is not "
29949e866e40b95795203f3ee46f44a197c946e4stevel "equal to \anum2\a.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\anum1\a -lt \anum2\a?True if numerical value of \anum1\a is less "
29949e866e40b95795203f3ee46f44a197c946e4stevel "than \anum2\a.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\anum1\a -le \anum2\a?True if numerical value of \anum1\a is less "
29949e866e40b95795203f3ee46f44a197c946e4stevel "than or equal to \anum2\a.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\anum1\a -gt \anum2\a?True if numerical value of \anum1\a is "
29949e866e40b95795203f3ee46f44a197c946e4stevel "greater than \anum2\a.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\anum1\a -ge \anum2\a?True if numerical value of \anum1\a is "
29949e866e40b95795203f3ee46f44a197c946e4stevel "greater than or equal to \anum2\a.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\afile1\a -nt \afile2\a?True if \afile1\a is newer than \afile2\a "
29949e866e40b95795203f3ee46f44a197c946e4stevel "or \afile2\a does not exist.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\afile1\a -ot \afile2\a?True if \afile1\a is older than \afile2\a "
29949e866e40b95795203f3ee46f44a197c946e4stevel "or \afile2\a does not exist.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+\afile1\a -ef \afile2\a?True if \afile1\a is another name for "
29949e866e40b95795203f3ee46f44a197c946e4stevel "\afile2\a. This will be true if \afile1\a is a hard link "
29949e866e40b95795203f3ee46f44a197c946e4stevel "or a symbolic link to \afile2\a.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel"}"
29949e866e40b95795203f3ee46f44a197c946e4stevel"\n"
29949e866e40b95795203f3ee46f44a197c946e4stevel"\n[expression]\n"
29949e866e40b95795203f3ee46f44a197c946e4stevel"\n"
29949e866e40b95795203f3ee46f44a197c946e4stevel"[+EXIT STATUS?]{"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+0?Indicates that the specified expression is True.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+1?Indicates that the specified expression is False.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel "[+>1?An error occurred.]"
29949e866e40b95795203f3ee46f44a197c946e4stevel"}"
29949e866e40b95795203f3ee46f44a197c946e4stevel
29949e866e40b95795203f3ee46f44a197c946e4stevel"[+SEE ALSO?\blet\b(1), \bexpr\b(1)]"
29949e866e40b95795203f3ee46f44a197c946e4stevel;
29949e866e40b95795203f3ee46f44a197c946e4stevel
29949e866e40b95795203f3ee46f44a197c946e4stevelconst char test_opchars[] = "HLNSVOGCaeohrwxdcbfugk"
29949e866e40b95795203f3ee46f44a197c946e4stevel#if SHOPT_TEST_L
29949e866e40b95795203f3ee46f44a197c946e4stevel "l"
29949e866e40b95795203f3ee46f44a197c946e4stevel#endif
29949e866e40b95795203f3ee46f44a197c946e4stevel "psnzt";
29949e866e40b95795203f3ee46f44a197c946e4stevelconst char e_argument[] = "argument expected";
29949e866e40b95795203f3ee46f44a197c946e4stevelconst char e_missing[] = "%s missing";
29949e866e40b95795203f3ee46f44a197c946e4stevelconst char e_badop[] = "%s: unknown operator";
29949e866e40b95795203f3ee46f44a197c946e4stevelconst char e_tstbegin[] = "[[ ! ";
29949e866e40b95795203f3ee46f44a197c946e4stevelconst char e_tstend[] = " ]]\n";
29949e866e40b95795203f3ee46f44a197c946e4stevel