aruu

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

arith_yylex.c (6654B)


      1 /* -
      2  * spdx-license-identifier: bsd-3-clause
      3  *
      4  * copyright (c) 2002
      5  * herbert xu
      6  * copyright (c) 1993
      7  * the regents of the university of california. 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_yacc.h"
     43 #include "error.h"
     44 #include "expand.h"
     45 #include "memalloc.h"
     46 #include "parser.h"
     47 #include "shell.h"
     48 #include "syntax.h"
     49 #include <ctype.h>
     50 #include <errno.h>
     51 #include <inttypes.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 
     55 #if ARITH_BOR + 11 != ARITH_BORASS || ARITH_ASS + 11 != ARITH_EQ
     56 #error Arithmetic tokens are out of order.
     57 #endif
     58 
     59 arith_t
     60 strtoarith_t(const char *restrict nptr, char **restrict endptr)
     61 {
     62   arith_t val;
     63 
     64   while (isspace((unsigned char)*nptr))
     65     nptr++;
     66   switch (*nptr) {
     67     case '-':
     68       return strtoimax(nptr, endptr, 0);
     69     case '0':
     70       return (arith_t)strtoumax(nptr, endptr, 0);
     71     default:
     72       val = (arith_t)strtoumax(nptr, endptr, 0);
     73       if (val >= 0)
     74         return val;
     75       else if (val == ARITH_MIN) {
     76         errno = ERANGE;
     77         return ARITH_MIN;
     78       } else {
     79         errno = ERANGE;
     80         return ARITH_MAX;
     81       }
     82   }
     83 }
     84 
     85 int
     86 yylex(void)
     87 {
     88   int         value;
     89   const char *buf = arith_buf;
     90   char       *end;
     91   const char *p;
     92 
     93   for (;;) {
     94     value = *buf;
     95     switch (value) {
     96       case ' ':
     97       case '\t':
     98       case '\n':
     99         buf++;
    100         continue;
    101       default:
    102         return ARITH_BAD;
    103       case '0':
    104       case '1':
    105       case '2':
    106       case '3':
    107       case '4':
    108       case '5':
    109       case '6':
    110       case '7':
    111       case '8':
    112       case '9':
    113         yylval.val = strtoarith_t(buf, &end);
    114         arith_buf  = end;
    115         return ARITH_NUM;
    116       case 'A':
    117       case 'B':
    118       case 'C':
    119       case 'D':
    120       case 'E':
    121       case 'F':
    122       case 'G':
    123       case 'H':
    124       case 'I':
    125       case 'J':
    126       case 'K':
    127       case 'L':
    128       case 'M':
    129       case 'N':
    130       case 'O':
    131       case 'P':
    132       case 'Q':
    133       case 'R':
    134       case 'S':
    135       case 'T':
    136       case 'U':
    137       case 'V':
    138       case 'W':
    139       case 'X':
    140       case 'Y':
    141       case 'Z':
    142       case '_':
    143       case 'a':
    144       case 'b':
    145       case 'c':
    146       case 'd':
    147       case 'e':
    148       case 'f':
    149       case 'g':
    150       case 'h':
    151       case 'i':
    152       case 'j':
    153       case 'k':
    154       case 'l':
    155       case 'm':
    156       case 'n':
    157       case 'o':
    158       case 'p':
    159       case 'q':
    160       case 'r':
    161       case 's':
    162       case 't':
    163       case 'u':
    164       case 'v':
    165       case 'w':
    166       case 'x':
    167       case 'y':
    168       case 'z':
    169         p = buf;
    170         while (buf++, is_in_name(*buf))
    171           ;
    172         yylval.name = stalloc(buf - p + 1);
    173         memcpy(yylval.name, p, buf - p);
    174         yylval.name[buf - p] = '\0';
    175         value                = ARITH_VAR;
    176         goto out;
    177       case '=':
    178         value += ARITH_ASS - '=';
    179       checkeq:
    180         buf++;
    181       checkeqcur:
    182         if (*buf != '=')
    183           goto out;
    184         value += 11;
    185         break;
    186       case '>':
    187         switch (*++buf) {
    188           case '=':
    189             value += ARITH_GE - '>';
    190             break;
    191           case '>':
    192             value += ARITH_RSHIFT - '>';
    193             goto checkeq;
    194           default:
    195             value += ARITH_GT - '>';
    196             goto out;
    197         }
    198         break;
    199       case '<':
    200         switch (*++buf) {
    201           case '=':
    202             value += ARITH_LE - '<';
    203             break;
    204           case '<':
    205             value += ARITH_LSHIFT - '<';
    206             goto checkeq;
    207           default:
    208             value += ARITH_LT - '<';
    209             goto out;
    210         }
    211         break;
    212       case '|':
    213         if (*++buf != '|') {
    214           value += ARITH_BOR - '|';
    215           goto checkeqcur;
    216         }
    217         value += ARITH_OR - '|';
    218         break;
    219       case '&':
    220         if (*++buf != '&') {
    221           value += ARITH_BAND - '&';
    222           goto checkeqcur;
    223         }
    224         value += ARITH_AND - '&';
    225         break;
    226       case '!':
    227         if (*++buf != '=') {
    228           value += ARITH_NOT - '!';
    229           goto out;
    230         }
    231         value += ARITH_NE - '!';
    232         break;
    233       case 0:
    234         goto out;
    235       case '(':
    236         value += ARITH_LPAREN - '(';
    237         break;
    238       case ')':
    239         value += ARITH_RPAREN - ')';
    240         break;
    241       case '*':
    242         value += ARITH_MUL - '*';
    243         goto checkeq;
    244       case '/':
    245         value += ARITH_DIV - '/';
    246         goto checkeq;
    247       case '%':
    248         value += ARITH_REM - '%';
    249         goto checkeq;
    250       case '+':
    251         if (buf[1] == '+')
    252           return ARITH_BAD;
    253         value += ARITH_ADD - '+';
    254         goto checkeq;
    255       case '-':
    256         if (buf[1] == '-')
    257           return ARITH_BAD;
    258         value += ARITH_SUB - '-';
    259         goto checkeq;
    260       case '~':
    261         value += ARITH_BNOT - '~';
    262         break;
    263       case '^':
    264         value += ARITH_BXOR - '^';
    265         goto checkeq;
    266       case '?':
    267         value += ARITH_QMARK - '?';
    268         break;
    269       case ':':
    270         value += ARITH_COLON - ':';
    271         break;
    272     }
    273     break;
    274   }
    275 
    276   buf++;
    277 out:
    278   arith_buf = buf;
    279   return value;
    280 }