aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

arith_yacc.c (9223B)


      1 /* -
      2  * spdx-license-identifier: bsd-3-clause
      3  *
      4  * copyright (c) 1993
      5  * the regents of the university of california. all rights reserved
      6  * copyright (c) 2007
      7  * herbert xu <herbert@gondor.apana.org.au>. all rights reserved
      8  *
      9  * this code is derived from software contributed to berkeley by
     10  * kenneth almquist
     11  *
     12  * redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. redistributions of source code must retain the above copyright
     16  * notice, this list of conditions and the following disclaimer
     17  * 2. redistributions in binary form must reproduce the above copyright
     18  * notice, this list of conditions and the following disclaimer in the
     19  * documentation and/or other materials provided with the distribution
     20  * 3. neither the name of the university nor the names of its contributors
     21  * may be used to endorse or promote products derived from this software
     22  * without specific prior written permission
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR a PARTICULAR PURPOSE
     27  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE
     35  */
     36 
     37 #if defined(__has_include)
     38 #if __has_include(<sys/cdefs.h>)
     39 #include <sys/cdefs.h>
     40 #endif
     41 #endif
     42 #include "arith.h"
     43 #include "arith_yacc.h"
     44 #include "error.h"
     45 #include "expand.h"
     46 #include "memalloc.h"
     47 #include "options.h"
     48 #include "output.h"
     49 #include "shell.h"
     50 #include "var.h"
     51 #include <errno.h>
     52 #include <inttypes.h>
     53 #include <limits.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 
     57 #if ARITH_BOR + 11 != ARITH_BORASS || ARITH_ASS + 11 != ARITH_EQ
     58 #error Arithmetic tokens are out of order.
     59 #endif
     60 
     61 static const char *arith_startbuf;
     62 
     63 const char   *arith_buf;
     64 union yystype yylval;
     65 
     66 static int last_token;
     67 
     68 #define ARITH_PRECEDENCE(op, prec) [op - ARITH_BINOP_MIN] = prec
     69 
     70 static const char prec[ARITH_BINOP_MAX - ARITH_BINOP_MIN] = {
     71     ARITH_PRECEDENCE(ARITH_MUL, 0),
     72     ARITH_PRECEDENCE(ARITH_DIV, 0),
     73     ARITH_PRECEDENCE(ARITH_REM, 0),
     74     ARITH_PRECEDENCE(ARITH_ADD, 1),
     75     ARITH_PRECEDENCE(ARITH_SUB, 1),
     76     ARITH_PRECEDENCE(ARITH_LSHIFT, 2),
     77     ARITH_PRECEDENCE(ARITH_RSHIFT, 2),
     78     ARITH_PRECEDENCE(ARITH_LT, 3),
     79     ARITH_PRECEDENCE(ARITH_LE, 3),
     80     ARITH_PRECEDENCE(ARITH_GT, 3),
     81     ARITH_PRECEDENCE(ARITH_GE, 3),
     82     ARITH_PRECEDENCE(ARITH_EQ, 4),
     83     ARITH_PRECEDENCE(ARITH_NE, 4),
     84     ARITH_PRECEDENCE(ARITH_BAND, 5),
     85     ARITH_PRECEDENCE(ARITH_BXOR, 6),
     86     ARITH_PRECEDENCE(ARITH_BOR, 7),
     87 };
     88 
     89 #define ARITH_MAX_PREC 8
     90 
     91 int letcmd(int, char **);
     92 
     93 static __dead2 void
     94 yyerror(const char *s)
     95 {
     96   error("arithmetic expression: %s: \"%s\"", s, arith_startbuf);
     97   /* NOTREACHED */
     98 }
     99 
    100 static arith_t
    101 arith_lookupvarint(char *varname)
    102 {
    103   const char *str;
    104   char       *p;
    105   arith_t     result;
    106 
    107   str = lookupvar(varname);
    108   if (uflag && str == NULL)
    109     yyerror("variable not set");
    110   if (str == NULL || *str == '\0')
    111     str = "0";
    112   errno  = 0;
    113   result = strtoarith_t(str, &p);
    114   if (errno != 0 || *p != '\0')
    115     yyerror("variable conversion error");
    116   return result;
    117 }
    118 
    119 static inline int
    120 arith_prec(int op)
    121 {
    122   return prec[op - ARITH_BINOP_MIN];
    123 }
    124 
    125 static inline int
    126 higher_prec(int op1, int op2)
    127 {
    128   return arith_prec(op1) < arith_prec(op2);
    129 }
    130 
    131 static arith_t
    132 do_binop(int op, arith_t a, arith_t b)
    133 {
    134   switch (op) {
    135     default:
    136     case ARITH_REM:
    137     case ARITH_DIV:
    138       if (!b)
    139         yyerror("division by zero");
    140       if (a == ARITH_MIN && b == -1)
    141         yyerror("divide error");
    142       return op == ARITH_REM ? a % b : a / b;
    143     case ARITH_MUL:
    144       return (uintmax_t)a * (uintmax_t)b;
    145     case ARITH_ADD:
    146       return (uintmax_t)a + (uintmax_t)b;
    147     case ARITH_SUB:
    148       return (uintmax_t)a - (uintmax_t)b;
    149     case ARITH_LSHIFT:
    150       return (uintmax_t)a << (b & (sizeof(uintmax_t) * CHAR_BIT - 1));
    151     case ARITH_RSHIFT:
    152       return a >> (b & (sizeof(uintmax_t) * CHAR_BIT - 1));
    153     case ARITH_LT:
    154       return a < b;
    155     case ARITH_LE:
    156       return a <= b;
    157     case ARITH_GT:
    158       return a > b;
    159     case ARITH_GE:
    160       return a >= b;
    161     case ARITH_EQ:
    162       return a == b;
    163     case ARITH_NE:
    164       return a != b;
    165     case ARITH_BAND:
    166       return a & b;
    167     case ARITH_BXOR:
    168       return a ^ b;
    169     case ARITH_BOR:
    170       return a | b;
    171   }
    172 }
    173 
    174 static arith_t assignment(int var, int noeval);
    175 
    176 static arith_t
    177 primary(int token, union yystype *val, int op, int noeval)
    178 {
    179   arith_t result;
    180 
    181 again:
    182   switch (token) {
    183     case ARITH_LPAREN:
    184       result = assignment(op, noeval);
    185       if (last_token != ARITH_RPAREN)
    186         yyerror("expecting ')'");
    187       last_token = yylex();
    188       return result;
    189     case ARITH_NUM:
    190       last_token = op;
    191       return val->val;
    192     case ARITH_VAR:
    193       last_token = op;
    194       return noeval ? val->val : arith_lookupvarint(val->name);
    195     case ARITH_ADD:
    196       token = op;
    197       *val  = yylval;
    198       op    = yylex();
    199       goto again;
    200     case ARITH_SUB:
    201       *val = yylval;
    202       return -primary(op, val, yylex(), noeval);
    203     case ARITH_NOT:
    204       *val = yylval;
    205       return !primary(op, val, yylex(), noeval);
    206     case ARITH_BNOT:
    207       *val = yylval;
    208       return ~primary(op, val, yylex(), noeval);
    209     default:
    210       yyerror("expecting primary");
    211   }
    212 }
    213 
    214 static arith_t
    215 binop2(arith_t a, int op, int precedence, int noeval)
    216 {
    217   for (;;) {
    218     union yystype val;
    219     arith_t       b;
    220     int           op2;
    221     int           token;
    222 
    223     token = yylex();
    224     val   = yylval;
    225 
    226     b = primary(token, &val, yylex(), noeval);
    227 
    228     op2 = last_token;
    229     if (op2 >= ARITH_BINOP_MIN && op2 < ARITH_BINOP_MAX && higher_prec(op2, op)) {
    230       b   = binop2(b, op2, arith_prec(op), noeval);
    231       op2 = last_token;
    232     }
    233 
    234     a = noeval ? b : do_binop(op, a, b);
    235 
    236     if (op2 < ARITH_BINOP_MIN || op2 >= ARITH_BINOP_MAX || arith_prec(op2) >= precedence)
    237       return a;
    238 
    239     op = op2;
    240   }
    241 }
    242 
    243 static arith_t
    244 binop(int token, union yystype *val, int op, int noeval)
    245 {
    246   arith_t a = primary(token, val, op, noeval);
    247 
    248   op = last_token;
    249   if (op < ARITH_BINOP_MIN || op >= ARITH_BINOP_MAX)
    250     return a;
    251 
    252   return binop2(a, op, ARITH_MAX_PREC, noeval);
    253 }
    254 
    255 static arith_t and (int token, union yystype *val, int op, int noeval)
    256 {
    257   arith_t a = binop(token, val, op, noeval);
    258   arith_t b;
    259 
    260   op = last_token;
    261   if (op != ARITH_AND)
    262     return a;
    263 
    264   token = yylex();
    265   *val  = yylval;
    266 
    267   b = and(token, val, yylex(), noeval | !a);
    268 
    269   return a && b;
    270 }
    271 
    272 static arith_t or (int token, union yystype *val, int op, int noeval)
    273 {
    274   arith_t a = and(token, val, op, noeval);
    275   arith_t b;
    276 
    277   op = last_token;
    278   if (op != ARITH_OR)
    279     return a;
    280 
    281   token = yylex();
    282   *val  = yylval;
    283 
    284   b = or (token, val, yylex(), noeval | !!a);
    285 
    286   return a || b;
    287 }
    288 
    289 static arith_t
    290 cond(int token, union yystype *val, int op, int noeval)
    291 {
    292   arith_t a = or (token, val, op, noeval);
    293   arith_t b;
    294   arith_t c;
    295 
    296   if (last_token != ARITH_QMARK)
    297     return a;
    298 
    299   b = assignment(yylex(), noeval | !a);
    300 
    301   if (last_token != ARITH_COLON)
    302     yyerror("expecting ':'");
    303 
    304   token = yylex();
    305   *val  = yylval;
    306 
    307   c = cond(token, val, yylex(), noeval | !!a);
    308 
    309   return a ? b : c;
    310 }
    311 
    312 static arith_t
    313 assignment(int var, int noeval)
    314 {
    315   union yystype val = yylval;
    316   int           op  = yylex();
    317   arith_t       result;
    318   char          sresult[DIGITS(result) + 1];
    319 
    320   if (var != ARITH_VAR)
    321     return cond(var, &val, op, noeval);
    322 
    323   if (op != ARITH_ASS && (op < ARITH_ASS_MIN || op >= ARITH_ASS_MAX))
    324     return cond(var, &val, op, noeval);
    325 
    326   result = assignment(yylex(), noeval);
    327   if (noeval)
    328     return result;
    329 
    330   if (op != ARITH_ASS)
    331     result = do_binop(op - 11, arith_lookupvarint(val.name), result);
    332   snprintf(sresult, sizeof(sresult), ARITH_FORMAT_STR, result);
    333   setvar(val.name, sresult, 0);
    334   return result;
    335 }
    336 
    337 arith_t
    338 arith(const char *s)
    339 {
    340   struct stackmark smark;
    341   arith_t          result;
    342 
    343   setstackmark(&smark);
    344 
    345   arith_buf = arith_startbuf = s;
    346 
    347   result = assignment(yylex(), 0);
    348 
    349   if (last_token)
    350     yyerror("expecting EOF");
    351 
    352   popstackmark(&smark);
    353 
    354   return result;
    355 }
    356 
    357 /*
    358  * the exp(1) builtin
    359  */
    360 int
    361 letcmd(int argc, char **argv)
    362 {
    363   const char *p;
    364   char       *concat;
    365   char      **ap;
    366   arith_t     i;
    367 
    368   if (argc > 1) {
    369     p = argv[1];
    370     if (argc > 2) {
    371       /*
    372        * concatenate arguments
    373        */
    374       STARTSTACKSTR(concat);
    375       ap = argv + 2;
    376       for (;;) {
    377         while (*p)
    378           STPUTC(*p++, concat);
    379         if ((p = *ap++) == NULL)
    380           break;
    381         STPUTC(' ', concat);
    382       }
    383       STPUTC('\0', concat);
    384       p = grabstackstr(concat);
    385     }
    386   } else
    387     p = "";
    388 
    389   i = arith(p);
    390 
    391   out1fmt(ARITH_FORMAT_STR "\n", i);
    392   return !i;
    393 }