From owner-svn-src-stable-10@freebsd.org Sat Apr 9 21:49:58 2016 Return-Path: Delivered-To: svn-src-stable-10@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 63A3EB0A797; Sat, 9 Apr 2016 21:49:58 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 214A01184; Sat, 9 Apr 2016 21:49:58 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u39LnvAW023755; Sat, 9 Apr 2016 21:49:57 GMT (envelope-from jilles@FreeBSD.org) Received: (from jilles@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u39LnvRZ023754; Sat, 9 Apr 2016 21:49:57 GMT (envelope-from jilles@FreeBSD.org) Message-Id: <201604092149.u39LnvRZ023754@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jilles set sender to jilles@FreeBSD.org using -f From: Jilles Tjoelker Date: Sat, 9 Apr 2016 21:49:57 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r297766 - stable/10/bin/test X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-10@freebsd.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: SVN commit messages for only the 10-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 09 Apr 2016 21:49:58 -0000 Author: jilles Date: Sat Apr 9 21:49:57 2016 New Revision: 297766 URL: https://svnweb.freebsd.org/changeset/base/297766 Log: MFC r258799: test: Simplify the code by unifying op_num and op_type. The global variable t_wp_op is no longer needed. Modified: stable/10/bin/test/test.c Directory Properties: stable/10/ (props changed) Modified: stable/10/bin/test/test.c ============================================================================== --- stable/10/bin/test/test.c Sat Apr 9 21:04:51 2016 (r297765) +++ stable/10/bin/test/test.c Sat Apr 9 21:49:57 2016 (r297766) @@ -67,9 +67,18 @@ error(const char *msg, ...) operand ::= */ +enum token_types { + UNOP = 0x100, + BINOP = 0x200, + BUNOP = 0x300, + BBINOP = 0x400, + PAREN = 0x500 +}; + enum token { EOI, - FILRD, + OPERAND, + FILRD = UNOP + 1, FILWR, FILEX, FILEXIST, @@ -85,13 +94,13 @@ enum token { FILSUID, FILSGID, FILSTCK, - FILNT, - FILOT, - FILEQ, - FILUID, - FILGID, STREZ, STRNZ, + FILUID, + FILGID, + FILNT = BINOP + 1, + FILOT, + FILEQ, STREQ, STRNE, STRLT, @@ -102,70 +111,62 @@ enum token { INTGT, INTLE, INTLT, - UNOT, - BAND, + UNOT = BUNOP + 1, + BAND = BBINOP + 1, BOR, - LPAREN, - RPAREN, - OPERAND + LPAREN = PAREN + 1, + RPAREN }; -enum token_types { - UNOP, - BINOP, - BUNOP, - BBINOP, - PAREN -}; +#define TOKEN_TYPE(token) ((token) & 0xff00) static struct t_op { char op_text[4]; - short op_num, op_type; + short op_num; } const 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}, - {"", 0, 0} + {"-r", FILRD}, + {"-w", FILWR}, + {"-x", FILEX}, + {"-e", FILEXIST}, + {"-f", FILREG}, + {"-d", FILDIR}, + {"-c", FILCDEV}, + {"-b", FILBDEV}, + {"-p", FILFIFO}, + {"-u", FILSUID}, + {"-g", FILSGID}, + {"-k", FILSTCK}, + {"-s", FILGZ}, + {"-t", FILTT}, + {"-z", STREZ}, + {"-n", STRNZ}, + {"-h", FILSYM}, /* for backwards compat */ + {"-O", FILUID}, + {"-G", FILGID}, + {"-L", FILSYM}, + {"-S", FILSOCK}, + {"=", STREQ}, + {"==", STREQ}, + {"!=", STRNE}, + {"<", STRLT}, + {">", STRGT}, + {"-eq", INTEQ}, + {"-ne", INTNE}, + {"-ge", INTGE}, + {"-gt", INTGT}, + {"-le", INTLE}, + {"-lt", INTLT}, + {"-nt", FILNT}, + {"-ot", FILOT}, + {"-ef", FILEQ}, + {"!", UNOT}, + {"-a", BAND}, + {"-o", BOR}, + {"(", LPAREN}, + {")", RPAREN}, + {"", 0} }; -static struct t_op const *t_wp_op; static int nargc; static char **t_wp; static int parenlevel; @@ -295,10 +296,10 @@ primary(enum token n) parenlevel--; return res; } - if (t_wp_op && t_wp_op->op_type == UNOP) { + if (TOKEN_TYPE(n) == UNOP) { /* unary expression */ if (--nargc == 0) - syntax(t_wp_op->op_text, "argument expected"); + syntax(NULL, "argument expected"); /* impossible */ switch (n) { case STREZ: return strlen(*++t_wp) == 0; @@ -311,10 +312,8 @@ primary(enum token n) } } - if (t_lex(nargc > 0 ? t_wp[1] : NULL), t_wp_op && t_wp_op->op_type == - BINOP) { + if (TOKEN_TYPE(t_lex(nargc > 0 ? t_wp[1] : NULL)) == BINOP) return binop(); - } return strlen(*t_wp) > 0; } @@ -322,17 +321,17 @@ primary(enum token n) static int binop(void) { - const char *opnd1, *opnd2; - struct t_op const *op; + const char *opnd1, *op, *opnd2; + enum token n; opnd1 = *t_wp; - (void) t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL); - op = t_wp_op; + op = nargc > 0 ? t_wp[1] : NULL; + n = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL); if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL) - syntax(op->op_text, "argument expected"); + syntax(op, "argument expected"); - switch (op->op_num) { + switch (n) { case STREQ: return strcmp(opnd1, opnd2) == 0; case STRNE: @@ -424,22 +423,20 @@ t_lex(char *s) struct t_op const *op = ops; if (s == 0) { - t_wp_op = NULL; return EOI; } while (*op->op_text) { if (strcmp(s, op->op_text) == 0) { - if (((op->op_type == UNOP || op->op_type == BUNOP) + if (((TOKEN_TYPE(op->op_num) == UNOP || + TOKEN_TYPE(op->op_num) == BUNOP) && isunopoperand()) || (op->op_num == LPAREN && islparenoperand()) || (op->op_num == RPAREN && isrparenoperand())) break; - t_wp_op = op; return op->op_num; } op++; } - t_wp_op = NULL; return OPERAND; } @@ -458,7 +455,7 @@ isunopoperand(void) t = *(t_wp + 2); while (*op->op_text) { if (strcmp(s, op->op_text) == 0) - return op->op_type == BINOP && + return TOKEN_TYPE(op->op_num) == BINOP && (parenlevel == 0 || t[0] != ')' || t[1] != '\0'); op++; } @@ -480,7 +477,7 @@ islparenoperand(void) return 0; while (*op->op_text) { if (strcmp(s, op->op_text) == 0) - return op->op_type == BINOP; + return TOKEN_TYPE(op->op_num) == BINOP; op++; } return 0;