003 File Manager
Current Path:
/usr/src/bin/sh
usr
/
src
/
bin
/
sh
/
📁
..
📄
Makefile
(1.76 KB)
📄
Makefile.depend
(312 B)
📄
TOUR
(14.11 KB)
📄
alias.c
(5.35 KB)
📄
alias.h
(1.83 KB)
📄
arith.h
(1.72 KB)
📄
arith_yacc.c
(8.38 KB)
📄
arith_yacc.h
(2.86 KB)
📄
arith_yylex.c
(5.5 KB)
📁
bltin
📄
builtins.def
(3.09 KB)
📄
cd.c
(8.94 KB)
📄
cd.h
(1.59 KB)
📄
dot.profile
(507 B)
📄
dot.shrc
(865 B)
📄
error.c
(5.08 KB)
📄
error.h
(3.69 KB)
📄
eval.c
(30.24 KB)
📄
eval.h
(2.84 KB)
📄
exec.c
(17.56 KB)
📄
exec.h
(2.8 KB)
📄
expand.c
(33.14 KB)
📄
expand.h
(2.47 KB)
📁
funcs
📄
histedit.c
(10.93 KB)
📄
input.c
(11.52 KB)
📄
input.h
(2.59 KB)
📄
jobs.c
(32.46 KB)
📄
jobs.h
(2.55 KB)
📄
mail.c
(3.36 KB)
📄
mail.h
(1.76 KB)
📄
main.c
(7.29 KB)
📄
main.h
(1.95 KB)
📄
memalloc.c
(7.68 KB)
📄
memalloc.h
(3.33 KB)
📄
miscbltin.c
(12.62 KB)
📄
mkbuiltins
(3.38 KB)
📄
mknodes.c
(11.14 KB)
📄
mksyntax.c
(8.83 KB)
📄
mktokens
(2.79 KB)
📄
myhistedit.h
(1.82 KB)
📄
mystring.c
(2.74 KB)
📄
mystring.h
(1.86 KB)
📄
nodes.c.pat
(4.35 KB)
📄
nodetypes
(5.1 KB)
📄
options.c
(12.97 KB)
📄
options.h
(3.63 KB)
📄
output.c
(7.24 KB)
📄
output.h
(3.24 KB)
📄
parser.c
(46.37 KB)
📄
parser.h
(3.43 KB)
📄
profile
(427 B)
📄
redir.c
(8.71 KB)
📄
redir.h
(2.02 KB)
📄
sh.1
(72.73 KB)
📄
shell.h
(2.61 KB)
📄
show.c
(8.08 KB)
📄
show.h
(1.84 KB)
📁
tests
📄
trap.c
(11.32 KB)
📄
trap.h
(2.09 KB)
📄
var.c
(19.03 KB)
📄
var.h
(4.4 KB)
Editing: arith_yacc.c
/*- * Copyright (c) 1993 * The Regents of the University of California. All rights reserved. * Copyright (c) 2007 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved. * * This code is derived from software contributed to Berkeley by * Kenneth Almquist. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); #include <limits.h> #include <errno.h> #include <inttypes.h> #include <stdlib.h> #include <stdio.h> #include "arith.h" #include "arith_yacc.h" #include "expand.h" #include "shell.h" #include "error.h" #include "memalloc.h" #include "output.h" #include "options.h" #include "var.h" #if ARITH_BOR + 11 != ARITH_BORASS || ARITH_ASS + 11 != ARITH_EQ #error Arithmetic tokens are out of order. #endif static const char *arith_startbuf; const char *arith_buf; union yystype yylval; static int last_token; #define ARITH_PRECEDENCE(op, prec) [op - ARITH_BINOP_MIN] = prec static const char prec[ARITH_BINOP_MAX - ARITH_BINOP_MIN] = { ARITH_PRECEDENCE(ARITH_MUL, 0), ARITH_PRECEDENCE(ARITH_DIV, 0), ARITH_PRECEDENCE(ARITH_REM, 0), ARITH_PRECEDENCE(ARITH_ADD, 1), ARITH_PRECEDENCE(ARITH_SUB, 1), ARITH_PRECEDENCE(ARITH_LSHIFT, 2), ARITH_PRECEDENCE(ARITH_RSHIFT, 2), ARITH_PRECEDENCE(ARITH_LT, 3), ARITH_PRECEDENCE(ARITH_LE, 3), ARITH_PRECEDENCE(ARITH_GT, 3), ARITH_PRECEDENCE(ARITH_GE, 3), ARITH_PRECEDENCE(ARITH_EQ, 4), ARITH_PRECEDENCE(ARITH_NE, 4), ARITH_PRECEDENCE(ARITH_BAND, 5), ARITH_PRECEDENCE(ARITH_BXOR, 6), ARITH_PRECEDENCE(ARITH_BOR, 7), }; #define ARITH_MAX_PREC 8 int letcmd(int, char **); static __dead2 void yyerror(const char *s) { error("arithmetic expression: %s: \"%s\"", s, arith_startbuf); /* NOTREACHED */ } static arith_t arith_lookupvarint(char *varname) { const char *str; char *p; arith_t result; str = lookupvar(varname); if (uflag && str == NULL) yyerror("variable not set"); if (str == NULL || *str == '\0') str = "0"; errno = 0; result = strtoarith_t(str, &p); if (errno != 0 || *p != '\0') yyerror("variable conversion error"); return result; } static inline int arith_prec(int op) { return prec[op - ARITH_BINOP_MIN]; } static inline int higher_prec(int op1, int op2) { return arith_prec(op1) < arith_prec(op2); } static arith_t do_binop(int op, arith_t a, arith_t b) { switch (op) { default: case ARITH_REM: case ARITH_DIV: if (!b) yyerror("division by zero"); if (a == ARITH_MIN && b == -1) yyerror("divide error"); return op == ARITH_REM ? a % b : a / b; case ARITH_MUL: return (uintmax_t)a * (uintmax_t)b; case ARITH_ADD: return (uintmax_t)a + (uintmax_t)b; case ARITH_SUB: return (uintmax_t)a - (uintmax_t)b; case ARITH_LSHIFT: return (uintmax_t)a << (b & (sizeof(uintmax_t) * CHAR_BIT - 1)); case ARITH_RSHIFT: return a >> (b & (sizeof(uintmax_t) * CHAR_BIT - 1)); case ARITH_LT: return a < b; case ARITH_LE: return a <= b; case ARITH_GT: return a > b; case ARITH_GE: return a >= b; case ARITH_EQ: return a == b; case ARITH_NE: return a != b; case ARITH_BAND: return a & b; case ARITH_BXOR: return a ^ b; case ARITH_BOR: return a | b; } } static arith_t assignment(int var, int noeval); static arith_t primary(int token, union yystype *val, int op, int noeval) { arith_t result; again: switch (token) { case ARITH_LPAREN: result = assignment(op, noeval); if (last_token != ARITH_RPAREN) yyerror("expecting ')'"); last_token = yylex(); return result; case ARITH_NUM: last_token = op; return val->val; case ARITH_VAR: last_token = op; return noeval ? val->val : arith_lookupvarint(val->name); case ARITH_ADD: token = op; *val = yylval; op = yylex(); goto again; case ARITH_SUB: *val = yylval; return -primary(op, val, yylex(), noeval); case ARITH_NOT: *val = yylval; return !primary(op, val, yylex(), noeval); case ARITH_BNOT: *val = yylval; return ~primary(op, val, yylex(), noeval); default: yyerror("expecting primary"); } } static arith_t binop2(arith_t a, int op, int precedence, int noeval) { for (;;) { union yystype val; arith_t b; int op2; int token; token = yylex(); val = yylval; b = primary(token, &val, yylex(), noeval); op2 = last_token; if (op2 >= ARITH_BINOP_MIN && op2 < ARITH_BINOP_MAX && higher_prec(op2, op)) { b = binop2(b, op2, arith_prec(op), noeval); op2 = last_token; } a = noeval ? b : do_binop(op, a, b); if (op2 < ARITH_BINOP_MIN || op2 >= ARITH_BINOP_MAX || arith_prec(op2) >= precedence) return a; op = op2; } } static arith_t binop(int token, union yystype *val, int op, int noeval) { arith_t a = primary(token, val, op, noeval); op = last_token; if (op < ARITH_BINOP_MIN || op >= ARITH_BINOP_MAX) return a; return binop2(a, op, ARITH_MAX_PREC, noeval); } static arith_t and(int token, union yystype *val, int op, int noeval) { arith_t a = binop(token, val, op, noeval); arith_t b; op = last_token; if (op != ARITH_AND) return a; token = yylex(); *val = yylval; b = and(token, val, yylex(), noeval | !a); return a && b; } static arith_t or(int token, union yystype *val, int op, int noeval) { arith_t a = and(token, val, op, noeval); arith_t b; op = last_token; if (op != ARITH_OR) return a; token = yylex(); *val = yylval; b = or(token, val, yylex(), noeval | !!a); return a || b; } static arith_t cond(int token, union yystype *val, int op, int noeval) { arith_t a = or(token, val, op, noeval); arith_t b; arith_t c; if (last_token != ARITH_QMARK) return a; b = assignment(yylex(), noeval | !a); if (last_token != ARITH_COLON) yyerror("expecting ':'"); token = yylex(); *val = yylval; c = cond(token, val, yylex(), noeval | !!a); return a ? b : c; } static arith_t assignment(int var, int noeval) { union yystype val = yylval; int op = yylex(); arith_t result; char sresult[DIGITS(result) + 1]; if (var != ARITH_VAR) return cond(var, &val, op, noeval); if (op != ARITH_ASS && (op < ARITH_ASS_MIN || op >= ARITH_ASS_MAX)) return cond(var, &val, op, noeval); result = assignment(yylex(), noeval); if (noeval) return result; if (op != ARITH_ASS) result = do_binop(op - 11, arith_lookupvarint(val.name), result); snprintf(sresult, sizeof(sresult), ARITH_FORMAT_STR, result); setvar(val.name, sresult, 0); return result; } arith_t arith(const char *s) { struct stackmark smark; arith_t result; setstackmark(&smark); arith_buf = arith_startbuf = s; result = assignment(yylex(), 0); if (last_token) yyerror("expecting EOF"); popstackmark(&smark); return result; } /* * The exp(1) builtin. */ int letcmd(int argc, char **argv) { const char *p; char *concat; char **ap; arith_t i; if (argc > 1) { p = argv[1]; if (argc > 2) { /* * Concatenate arguments. */ STARTSTACKSTR(concat); ap = argv + 2; for (;;) { while (*p) STPUTC(*p++, concat); if ((p = *ap++) == NULL) break; STPUTC(' ', concat); } STPUTC('\0', concat); p = grabstackstr(concat); } } else p = ""; i = arith(p); out1fmt(ARITH_FORMAT_STR "\n", i); return !i; }
Upload File
Create Folder