typo fix in a comment in a testcase. oh well...
[oweals/busybox.git] / coreutils / test.c
index 73048d311de8d23297b1b9aa569c14c29629af8f..5864c7f3253ccf0d9a52ad012e79d354c28e4c8d 100644 (file)
@@ -222,7 +222,7 @@ static const char *const TOKSTR[] = {
 #define unnest_msg_and_return(expr, ...) return expr
 #endif
 
-enum token_types {
+enum {
        UNOP,
        BINOP,
        BUNOP,
@@ -231,53 +231,96 @@ enum token_types {
 };
 
 struct operator_t {
-       char op_text[4];
        unsigned char op_num, op_type;
 };
 
-static const struct operator_t ops[] = {
-       { "-r", FILRD   , UNOP   },
-       { "-w", FILWR   , UNOP   },
-       { "-x", FILEX   , UNOP   },
-       { "-e", FILEXIST, UNOP   },
-       { "-f", FILREG  , UNOP   },
-       { "-d", FILDIR  , UNOP   },
-       { "-c", FILCDEV , UNOP   },
-       { "-b", FILBDEV , UNOP   },
-       { "-p", FILFIFO , UNOP   },
-       { "-u", FILSUID , UNOP   },
-       { "-g", FILSGID , UNOP   },
-       { "-k", FILSTCK , UNOP   },
-       { "-s", FILGZ   , UNOP   },
-       { "-t", FILTT   , UNOP   },
-       { "-z", STREZ   , UNOP   },
-       { "-n", STRNZ   , UNOP   },
-       { "-h", FILSYM  , UNOP   },    /* for backwards compat */
-
-       { "-O" , FILUID , UNOP   },
-       { "-G" , FILGID , UNOP   },
-       { "-L" , FILSYM , UNOP   },
-       { "-S" , FILSOCK, UNOP   },
-       { "="  , STREQ  , BINOP  },
-       { "==" , STREQ  , BINOP  },
-       { "!=" , STRNE  , BINOP  },
-       { "<"  , STRLT  , BINOP  },
-       { ">"  , STRGT  , BINOP  },
-       { "-eq", INTEQ  , BINOP  },
-       { "-ne", INTNE  , BINOP  },
-       { "-ge", INTGE  , BINOP  },
-       { "-gt", INTGT  , BINOP  },
-       { "-le", INTLE  , BINOP  },
-       { "-lt", INTLT  , BINOP  },
-       { "-nt", FILNT  , BINOP  },
-       { "-ot", FILOT  , BINOP  },
-       { "-ef", FILEQ  , BINOP  },
-       { "!"  , UNOT   , BUNOP  },
-       { "-a" , BAND   , BBINOP },
-       { "-o" , BOR    , BBINOP },
-       { "("  , LPAREN , PAREN  },
-       { ")"  , RPAREN , PAREN  },
+static const struct operator_t ops_table[] = {
+       { /* "-r" */ FILRD   , UNOP   },
+       { /* "-w" */ FILWR   , UNOP   },
+       { /* "-x" */ FILEX   , UNOP   },
+       { /* "-e" */ FILEXIST, UNOP   },
+       { /* "-f" */ FILREG  , UNOP   },
+       { /* "-d" */ FILDIR  , UNOP   },
+       { /* "-c" */ FILCDEV , UNOP   },
+       { /* "-b" */ FILBDEV , UNOP   },
+       { /* "-p" */ FILFIFO , UNOP   },
+       { /* "-u" */ FILSUID , UNOP   },
+       { /* "-g" */ FILSGID , UNOP   },
+       { /* "-k" */ FILSTCK , UNOP   },
+       { /* "-s" */ FILGZ   , UNOP   },
+       { /* "-t" */ FILTT   , UNOP   },
+       { /* "-z" */ STREZ   , UNOP   },
+       { /* "-n" */ STRNZ   , UNOP   },
+       { /* "-h" */ FILSYM  , UNOP   },    /* for backwards compat */
+
+       { /* "-O" */ FILUID  , UNOP   },
+       { /* "-G" */ FILGID  , UNOP   },
+       { /* "-L" */ FILSYM  , UNOP   },
+       { /* "-S" */ FILSOCK , UNOP   },
+       { /* "="  */ STREQ   , BINOP  },
+       { /* "==" */ STREQ   , BINOP  },
+       { /* "!=" */ STRNE   , BINOP  },
+       { /* "<"  */ STRLT   , BINOP  },
+       { /* ">"  */ STRGT   , BINOP  },
+       { /* "-eq"*/ INTEQ   , BINOP  },
+       { /* "-ne"*/ INTNE   , BINOP  },
+       { /* "-ge"*/ INTGE   , BINOP  },
+       { /* "-gt"*/ INTGT   , BINOP  },
+       { /* "-le"*/ INTLE   , BINOP  },
+       { /* "-lt"*/ INTLT   , BINOP  },
+       { /* "-nt"*/ FILNT   , BINOP  },
+       { /* "-ot"*/ FILOT   , BINOP  },
+       { /* "-ef"*/ FILEQ   , BINOP  },
+       { /* "!"  */ UNOT    , BUNOP  },
+       { /* "-a" */ BAND    , BBINOP },
+       { /* "-o" */ BOR     , BBINOP },
+       { /* "("  */ LPAREN  , PAREN  },
+       { /* ")"  */ RPAREN  , PAREN  },
 };
+/* Please keep these two tables in sync */
+static const char ops_texts[] ALIGN1 =
+       "-r"  "\0"
+       "-w"  "\0"
+       "-x"  "\0"
+       "-e"  "\0"
+       "-f"  "\0"
+       "-d"  "\0"
+       "-c"  "\0"
+       "-b"  "\0"
+       "-p"  "\0"
+       "-u"  "\0"
+       "-g"  "\0"
+       "-k"  "\0"
+       "-s"  "\0"
+       "-t"  "\0"
+       "-z"  "\0"
+       "-n"  "\0"
+       "-h"  "\0"
+
+       "-O"  "\0"
+       "-G"  "\0"
+       "-L"  "\0"
+       "-S"  "\0"
+       "="   "\0"
+       "=="  "\0"
+       "!="  "\0"
+       "<"   "\0"
+       ">"   "\0"
+       "-eq" "\0"
+       "-ne" "\0"
+       "-ge" "\0"
+       "-gt" "\0"
+       "-le" "\0"
+       "-lt" "\0"
+       "-nt" "\0"
+       "-ot" "\0"
+       "-ef" "\0"
+       "!"   "\0"
+       "-a"  "\0"
+       "-o"  "\0"
+       "("   "\0"
+       ")"   "\0"
+;
 
 
 #if ENABLE_FEATURE_TEST_64
@@ -384,29 +427,22 @@ static int equalf(const char *f1, const char *f2)
 */
 
 
-static enum token check_operator(char *s)
+static enum token check_operator(const char *s)
 {
        static const struct operator_t no_op = {
                .op_num = -1,
                .op_type = -1
        };
-       const struct operator_t *op;
+       int n;
 
        last_operator = &no_op;
-       if (s == NULL) {
+       if (s == NULL)
                return EOI;
-       }
-
-       op = ops;
-       do {
-               if (strcmp(s, op->op_text) == 0) {
-                       last_operator = op;
-                       return op->op_num;
-               }
-               op++;
-       } while (op < ops + ARRAY_SIZE(ops));
-
-       return OPERAND;
+       n = index_in_strings(ops_texts, s);
+       if (n < 0)
+               return OPERAND;
+       last_operator = &ops_table[n];
+       return ops_table[n].op_num;
 }
 
 
@@ -422,7 +458,7 @@ static int binop(void)
 
        opnd2 = *++args;
        if (opnd2 == NULL)
-               syntax(op->op_text, "argument expected");
+               syntax(args[-1], "argument expected");
 
        if (is_int_op(op->op_num)) {
                val1 = getn(opnd1);
@@ -627,7 +663,14 @@ static number_t nexpr(enum token n)
 
        nest_msg(">nexpr(%s)\n", TOKSTR[n]);
        if (n == UNOT) {
-               res = !nexpr(check_operator(*++args));
+               n = check_operator(*++args);
+               if (n == EOI) {
+                       /* special case: [ ! ], [ a -a ! ] are valid */
+                       /* IOW, "! ARG" may miss ARG */
+                       unnest_msg("<nexpr:1 (!EOI)\n");
+                       return 1;
+               }
+               res = !nexpr(n);
                unnest_msg("<nexpr:%lld\n", res);
                return res;
        }
@@ -798,7 +841,7 @@ int test_main(int argc, char **argv)
                check_operator(argv[1]);
                if (last_operator->op_type == BINOP) {
                        /* "test [!] arg1 <binary_op> arg2" */
-                       args = &argv[0];
+                       args = argv;
                        res = (binop() == 0);
                        goto ret;
                }
@@ -811,7 +854,7 @@ int test_main(int argc, char **argv)
                argv--;
        }
 #endif
-       args = &argv[0];
+       args = argv;
        res = !oexpr(check_operator(*args));
 
        if (*args != NULL && *++args != NULL) {