15b81b6b3SRodney W. Grimes %{ 25b81b6b3SRodney W. Grimes /* Written by Pace Willisson (pace@blitz.com) 355c497bfSJ.T. Conklin * and placed in the public domain. 45b81b6b3SRodney W. Grimes * 555c497bfSJ.T. Conklin * Largely rewritten by J.T. Conklin (jtc@wimsey.com) 655c497bfSJ.T. Conklin * 72a456239SPeter Wemm * $FreeBSD$ 85b81b6b3SRodney W. Grimes */ 94cf61abaSJ.T. Conklin 1064867286SStefan Eßer #include <sys/types.h> 11f07e4247SGarrett Wollman 125b81b6b3SRodney W. Grimes #include <ctype.h> 1355c497bfSJ.T. Conklin #include <err.h> 14915198b4SStefan Eßer #include <errno.h> 15f07e4247SGarrett Wollman #include <inttypes.h> 16915198b4SStefan Eßer #include <limits.h> 17f07e4247SGarrett Wollman #include <locale.h> 18f07e4247SGarrett Wollman #include <stdio.h> 19f07e4247SGarrett Wollman #include <stdlib.h> 20f07e4247SGarrett Wollman #include <string.h> 21f07e4247SGarrett Wollman #include <regex.h> 22f07e4247SGarrett Wollman #include <unistd.h> 23f07e4247SGarrett Wollman 24f07e4247SGarrett Wollman /* 25f07e4247SGarrett Wollman * POSIX specifies a specific error code for syntax errors. We exit 26f07e4247SGarrett Wollman * with this code for all errors. 27f07e4247SGarrett Wollman */ 28f07e4247SGarrett Wollman #define ERR_EXIT 2 295b81b6b3SRodney W. Grimes 305b81b6b3SRodney W. Grimes enum valtype { 31717252eaSJoerg Wunsch integer, numeric_string, string 325b81b6b3SRodney W. Grimes } ; 335b81b6b3SRodney W. Grimes 345b81b6b3SRodney W. Grimes struct val { 355b81b6b3SRodney W. Grimes enum valtype type; 365b81b6b3SRodney W. Grimes union { 375b81b6b3SRodney W. Grimes char *s; 38f07e4247SGarrett Wollman intmax_t i; 395b81b6b3SRodney W. Grimes } u; 405b81b6b3SRodney W. Grimes } ; 415b81b6b3SRodney W. Grimes 425b81b6b3SRodney W. Grimes struct val *result; 433d06e95dSKris Kennaway 44f07e4247SGarrett Wollman int chk_div(intmax_t, intmax_t); 45f07e4247SGarrett Wollman int chk_minus(intmax_t, intmax_t, intmax_t); 46f07e4247SGarrett Wollman int chk_plus(intmax_t, intmax_t, intmax_t); 47f07e4247SGarrett Wollman int chk_times(intmax_t, intmax_t, intmax_t); 487669d0fcSWarner Losh void free_value(struct val *); 497669d0fcSWarner Losh int is_zero_or_null(struct val *); 507669d0fcSWarner Losh int isstring(struct val *); 51f07e4247SGarrett Wollman struct val *make_integer(intmax_t); 527669d0fcSWarner Losh struct val *make_str(const char *); 537669d0fcSWarner Losh struct val *op_and(struct val *, struct val *); 547669d0fcSWarner Losh struct val *op_colon(struct val *, struct val *); 557669d0fcSWarner Losh struct val *op_div(struct val *, struct val *); 567669d0fcSWarner Losh struct val *op_eq(struct val *, struct val *); 577669d0fcSWarner Losh struct val *op_ge(struct val *, struct val *); 587669d0fcSWarner Losh struct val *op_gt(struct val *, struct val *); 597669d0fcSWarner Losh struct val *op_le(struct val *, struct val *); 607669d0fcSWarner Losh struct val *op_lt(struct val *, struct val *); 617669d0fcSWarner Losh struct val *op_minus(struct val *, struct val *); 627669d0fcSWarner Losh struct val *op_ne(struct val *, struct val *); 637669d0fcSWarner Losh struct val *op_or(struct val *, struct val *); 647669d0fcSWarner Losh struct val *op_plus(struct val *, struct val *); 657669d0fcSWarner Losh struct val *op_rem(struct val *, struct val *); 667669d0fcSWarner Losh struct val *op_times(struct val *, struct val *); 67f07e4247SGarrett Wollman intmax_t to_integer(struct val *); 687669d0fcSWarner Losh void to_string(struct val *); 697669d0fcSWarner Losh int yyerror(const char *); 707669d0fcSWarner Losh int yylex(void); 717669d0fcSWarner Losh int yyparse(void); 725b81b6b3SRodney W. Grimes 731393277eSGarrett Wollman static int eflag; 745b81b6b3SRodney W. Grimes char **av; 755b81b6b3SRodney W. Grimes %} 765b81b6b3SRodney W. Grimes 775b81b6b3SRodney W. Grimes %union 785b81b6b3SRodney W. Grimes { 795b81b6b3SRodney W. Grimes struct val *val; 805b81b6b3SRodney W. Grimes } 815b81b6b3SRodney W. Grimes 825b81b6b3SRodney W. Grimes %left <val> '|' 835b81b6b3SRodney W. Grimes %left <val> '&' 845b81b6b3SRodney W. Grimes %left <val> '=' '>' '<' GE LE NE 855b81b6b3SRodney W. Grimes %left <val> '+' '-' 865b81b6b3SRodney W. Grimes %left <val> '*' '/' '%' 875b81b6b3SRodney W. Grimes %left <val> ':' 885b81b6b3SRodney W. Grimes 895b81b6b3SRodney W. Grimes %token <val> TOKEN 905b81b6b3SRodney W. Grimes %type <val> start expr 915b81b6b3SRodney W. Grimes 925b81b6b3SRodney W. Grimes %% 935b81b6b3SRodney W. Grimes 945b81b6b3SRodney W. Grimes start: expr { result = $$; } 955b81b6b3SRodney W. Grimes 965b81b6b3SRodney W. Grimes expr: TOKEN 975b81b6b3SRodney W. Grimes | '(' expr ')' { $$ = $2; } 985b81b6b3SRodney W. Grimes | expr '|' expr { $$ = op_or ($1, $3); } 995b81b6b3SRodney W. Grimes | expr '&' expr { $$ = op_and ($1, $3); } 1005b81b6b3SRodney W. Grimes | expr '=' expr { $$ = op_eq ($1, $3); } 1015b81b6b3SRodney W. Grimes | expr '>' expr { $$ = op_gt ($1, $3); } 1025b81b6b3SRodney W. Grimes | expr '<' expr { $$ = op_lt ($1, $3); } 1035b81b6b3SRodney W. Grimes | expr GE expr { $$ = op_ge ($1, $3); } 1045b81b6b3SRodney W. Grimes | expr LE expr { $$ = op_le ($1, $3); } 1055b81b6b3SRodney W. Grimes | expr NE expr { $$ = op_ne ($1, $3); } 1065b81b6b3SRodney W. Grimes | expr '+' expr { $$ = op_plus ($1, $3); } 1075b81b6b3SRodney W. Grimes | expr '-' expr { $$ = op_minus ($1, $3); } 1085b81b6b3SRodney W. Grimes | expr '*' expr { $$ = op_times ($1, $3); } 1095b81b6b3SRodney W. Grimes | expr '/' expr { $$ = op_div ($1, $3); } 1105b81b6b3SRodney W. Grimes | expr '%' expr { $$ = op_rem ($1, $3); } 1115b81b6b3SRodney W. Grimes | expr ':' expr { $$ = op_colon ($1, $3); } 1125b81b6b3SRodney W. Grimes ; 1135b81b6b3SRodney W. Grimes 1145b81b6b3SRodney W. Grimes 1155b81b6b3SRodney W. Grimes %% 1165b81b6b3SRodney W. Grimes 1175b81b6b3SRodney W. Grimes struct val * 118f07e4247SGarrett Wollman make_integer(intmax_t i) 1195b81b6b3SRodney W. Grimes { 1205b81b6b3SRodney W. Grimes struct val *vp; 1215b81b6b3SRodney W. Grimes 1225b81b6b3SRodney W. Grimes vp = (struct val *) malloc (sizeof (*vp)); 1235b81b6b3SRodney W. Grimes if (vp == NULL) { 124f07e4247SGarrett Wollman errx(ERR_EXIT, "malloc() failed"); 1255b81b6b3SRodney W. Grimes } 1265b81b6b3SRodney W. Grimes 1275b81b6b3SRodney W. Grimes vp->type = integer; 1285b81b6b3SRodney W. Grimes vp->u.i = i; 1295b81b6b3SRodney W. Grimes return vp; 1305b81b6b3SRodney W. Grimes } 1315b81b6b3SRodney W. Grimes 1325b81b6b3SRodney W. Grimes struct val * 1337669d0fcSWarner Losh make_str(const char *s) 1345b81b6b3SRodney W. Grimes { 1355b81b6b3SRodney W. Grimes struct val *vp; 136f07e4247SGarrett Wollman char *ep; 1375b81b6b3SRodney W. Grimes 1385b81b6b3SRodney W. Grimes vp = (struct val *) malloc (sizeof (*vp)); 1395b81b6b3SRodney W. Grimes if (vp == NULL || ((vp->u.s = strdup (s)) == NULL)) { 140f07e4247SGarrett Wollman errx(ERR_EXIT, "malloc() failed"); 1415b81b6b3SRodney W. Grimes } 1425b81b6b3SRodney W. Grimes 143f07e4247SGarrett Wollman /* 144f07e4247SGarrett Wollman * Previously we tried to scan the string to see if it ``looked like'' 145f07e4247SGarrett Wollman * an integer (erroneously, as it happened). Let strtoimax() do the 146f07e4247SGarrett Wollman * dirty work. We could cache the value, except that we are using 147f07e4247SGarrett Wollman * a union and need to preserve the original string form until we 148f07e4247SGarrett Wollman * are certain that it is not needed. 149f07e4247SGarrett Wollman * 150f07e4247SGarrett Wollman * IEEE Std.1003.1-2001 says: 151f07e4247SGarrett Wollman * /integer/ An argument consisting only of an (optional) unary minus 152f07e4247SGarrett Wollman * followed by digits. 153f07e4247SGarrett Wollman * 154f07e4247SGarrett Wollman * This means that arguments which consist of digits followed by 155f07e4247SGarrett Wollman * non-digits MUST NOT be considered integers. strtoimax() will 156f07e4247SGarrett Wollman * figure this out for us. 157f07e4247SGarrett Wollman */ 1581393277eSGarrett Wollman if (eflag) 159f07e4247SGarrett Wollman (void)strtoimax(s, &ep, 10); 1601393277eSGarrett Wollman else 1611393277eSGarrett Wollman (void)strtol(s, &ep, 10); 1622a353a9fSJoerg Wunsch 163f07e4247SGarrett Wollman if (*ep != '\0') 164717252eaSJoerg Wunsch vp->type = string; 165f07e4247SGarrett Wollman else 166f07e4247SGarrett Wollman vp->type = numeric_string; 1672a353a9fSJoerg Wunsch 1685b81b6b3SRodney W. Grimes return vp; 1695b81b6b3SRodney W. Grimes } 1705b81b6b3SRodney W. Grimes 1715b81b6b3SRodney W. Grimes 1725b81b6b3SRodney W. Grimes void 1737669d0fcSWarner Losh free_value(struct val *vp) 1745b81b6b3SRodney W. Grimes { 175717252eaSJoerg Wunsch if (vp->type == string || vp->type == numeric_string) 1765b81b6b3SRodney W. Grimes free (vp->u.s); 1775b81b6b3SRodney W. Grimes } 1785b81b6b3SRodney W. Grimes 1795b81b6b3SRodney W. Grimes 180f07e4247SGarrett Wollman intmax_t 1817669d0fcSWarner Losh to_integer(struct val *vp) 1825b81b6b3SRodney W. Grimes { 183f07e4247SGarrett Wollman intmax_t i; 1845b81b6b3SRodney W. Grimes 1855b81b6b3SRodney W. Grimes if (vp->type == integer) 1865b81b6b3SRodney W. Grimes return 1; 1875b81b6b3SRodney W. Grimes 188717252eaSJoerg Wunsch if (vp->type == string) 1895b81b6b3SRodney W. Grimes return 0; 1905b81b6b3SRodney W. Grimes 191717252eaSJoerg Wunsch /* vp->type == numeric_string, make it numeric */ 192915198b4SStefan Eßer errno = 0; 1931393277eSGarrett Wollman if (eflag) { 194f07e4247SGarrett Wollman i = strtoimax(vp->u.s, (char **)NULL, 10); 195f07e4247SGarrett Wollman if (errno == ERANGE) 196f07e4247SGarrett Wollman err(ERR_EXIT, NULL); 1971393277eSGarrett Wollman } else { 1981393277eSGarrett Wollman i = strtol(vp->u.s, (char **)NULL, 10); 1991393277eSGarrett Wollman } 200f07e4247SGarrett Wollman 2015b81b6b3SRodney W. Grimes free (vp->u.s); 2025b81b6b3SRodney W. Grimes vp->u.i = i; 203717252eaSJoerg Wunsch vp->type = integer; 2045b81b6b3SRodney W. Grimes return 1; 2055b81b6b3SRodney W. Grimes } 2065b81b6b3SRodney W. Grimes 2075b81b6b3SRodney W. Grimes void 2087669d0fcSWarner Losh to_string(struct val *vp) 2095b81b6b3SRodney W. Grimes { 2105b81b6b3SRodney W. Grimes char *tmp; 2115b81b6b3SRodney W. Grimes 212717252eaSJoerg Wunsch if (vp->type == string || vp->type == numeric_string) 2135b81b6b3SRodney W. Grimes return; 2145b81b6b3SRodney W. Grimes 215f07e4247SGarrett Wollman /* 216f07e4247SGarrett Wollman * log_10(x) ~= 0.3 * log_2(x). Rounding up gives the number 217f07e4247SGarrett Wollman * of digits; add one each for the sign and terminating null 218f07e4247SGarrett Wollman * character, respectively. 219f07e4247SGarrett Wollman */ 220f07e4247SGarrett Wollman #define NDIGITS(x) (3 * (sizeof(x) * CHAR_BIT) / 10 + 1 + 1 + 1) 221f07e4247SGarrett Wollman tmp = malloc(NDIGITS(vp->u.i)); 222f07e4247SGarrett Wollman if (tmp == NULL) 223f07e4247SGarrett Wollman errx(ERR_EXIT, "malloc() failed"); 2245b81b6b3SRodney W. Grimes 225f07e4247SGarrett Wollman sprintf(tmp, "%jd", vp->u.i); 2265b81b6b3SRodney W. Grimes vp->type = string; 2275b81b6b3SRodney W. Grimes vp->u.s = tmp; 2285b81b6b3SRodney W. Grimes } 2295b81b6b3SRodney W. Grimes 2305b81b6b3SRodney W. Grimes 2315b81b6b3SRodney W. Grimes int 2327669d0fcSWarner Losh isstring(struct val *vp) 2335b81b6b3SRodney W. Grimes { 234717252eaSJoerg Wunsch /* only TRUE if this string is not a valid integer */ 2355b81b6b3SRodney W. Grimes return (vp->type == string); 2365b81b6b3SRodney W. Grimes } 2375b81b6b3SRodney W. Grimes 2385b81b6b3SRodney W. Grimes 2395b81b6b3SRodney W. Grimes int 2407669d0fcSWarner Losh yylex(void) 2415b81b6b3SRodney W. Grimes { 2425b81b6b3SRodney W. Grimes char *p; 2435b81b6b3SRodney W. Grimes 2445b81b6b3SRodney W. Grimes if (*av == NULL) 2455b81b6b3SRodney W. Grimes return (0); 2465b81b6b3SRodney W. Grimes 2475b81b6b3SRodney W. Grimes p = *av++; 2485b81b6b3SRodney W. Grimes 2495b81b6b3SRodney W. Grimes if (strlen (p) == 1) { 2505b81b6b3SRodney W. Grimes if (strchr ("|&=<>+-*/%:()", *p)) 2515b81b6b3SRodney W. Grimes return (*p); 2525b81b6b3SRodney W. Grimes } else if (strlen (p) == 2 && p[1] == '=') { 2535b81b6b3SRodney W. Grimes switch (*p) { 2545b81b6b3SRodney W. Grimes case '>': return (GE); 2555b81b6b3SRodney W. Grimes case '<': return (LE); 2565b81b6b3SRodney W. Grimes case '!': return (NE); 2575b81b6b3SRodney W. Grimes } 2585b81b6b3SRodney W. Grimes } 2595b81b6b3SRodney W. Grimes 2605b81b6b3SRodney W. Grimes yylval.val = make_str (p); 2615b81b6b3SRodney W. Grimes return (TOKEN); 2625b81b6b3SRodney W. Grimes } 2635b81b6b3SRodney W. Grimes 2645b81b6b3SRodney W. Grimes int 2657669d0fcSWarner Losh is_zero_or_null(struct val *vp) 2665b81b6b3SRodney W. Grimes { 2675b81b6b3SRodney W. Grimes if (vp->type == integer) { 2685b81b6b3SRodney W. Grimes return (vp->u.i == 0); 2695b81b6b3SRodney W. Grimes } else { 270c9fe00dcSJ.T. Conklin return (*vp->u.s == 0 || (to_integer (vp) && vp->u.i == 0)); 2715b81b6b3SRodney W. Grimes } 2725b81b6b3SRodney W. Grimes /* NOTREACHED */ 2735b81b6b3SRodney W. Grimes } 2745b81b6b3SRodney W. Grimes 275717252eaSJoerg Wunsch int 276f07e4247SGarrett Wollman main(int argc, char *argv[]) 2775b81b6b3SRodney W. Grimes { 278f07e4247SGarrett Wollman int c; 2794cf61abaSJ.T. Conklin 280f07e4247SGarrett Wollman setlocale (LC_ALL, ""); 28196ab7da3SGarrett Wollman if (getenv("EXPR_COMPAT") != NULL) { 28296ab7da3SGarrett Wollman av = argv + 1; 28396ab7da3SGarrett Wollman } else { 2841393277eSGarrett Wollman while ((c = getopt(argc, argv, "e")) != -1) 285f07e4247SGarrett Wollman switch (c) { 2861393277eSGarrett Wollman case 'e': 2871393277eSGarrett Wollman eflag = 1; 2881393277eSGarrett Wollman break; 2891393277eSGarrett Wollman 290f07e4247SGarrett Wollman default: 2911393277eSGarrett Wollman fprintf(stderr, 2921393277eSGarrett Wollman "usage: expr [-e] expression\n"); 293f07e4247SGarrett Wollman exit(ERR_EXIT); 294f07e4247SGarrett Wollman } 295f07e4247SGarrett Wollman av = argv + optind; 29696ab7da3SGarrett Wollman } 2975b81b6b3SRodney W. Grimes 2985b81b6b3SRodney W. Grimes yyparse(); 2995b81b6b3SRodney W. Grimes 3005b81b6b3SRodney W. Grimes if (result->type == integer) 301f07e4247SGarrett Wollman printf("%jd\n", result->u.i); 3025b81b6b3SRodney W. Grimes else 3035b81b6b3SRodney W. Grimes printf("%s\n", result->u.s); 3045b81b6b3SRodney W. Grimes 305717252eaSJoerg Wunsch return (is_zero_or_null(result)); 3065b81b6b3SRodney W. Grimes } 3075b81b6b3SRodney W. Grimes 3085b81b6b3SRodney W. Grimes int 3097669d0fcSWarner Losh yyerror(const char *s __unused) 3105b81b6b3SRodney W. Grimes { 311f07e4247SGarrett Wollman errx(ERR_EXIT, "syntax error"); 3125b81b6b3SRodney W. Grimes } 3135b81b6b3SRodney W. Grimes 3145b81b6b3SRodney W. Grimes 3155b81b6b3SRodney W. Grimes struct val * 3167669d0fcSWarner Losh op_or(struct val *a, struct val *b) 3175b81b6b3SRodney W. Grimes { 3185b81b6b3SRodney W. Grimes if (is_zero_or_null (a)) { 3195b81b6b3SRodney W. Grimes free_value (a); 3205b81b6b3SRodney W. Grimes return (b); 3215b81b6b3SRodney W. Grimes } else { 3225b81b6b3SRodney W. Grimes free_value (b); 3235b81b6b3SRodney W. Grimes return (a); 3245b81b6b3SRodney W. Grimes } 3255b81b6b3SRodney W. Grimes } 3265b81b6b3SRodney W. Grimes 3275b81b6b3SRodney W. Grimes struct val * 3287669d0fcSWarner Losh op_and(struct val *a, struct val *b) 3295b81b6b3SRodney W. Grimes { 3305b81b6b3SRodney W. Grimes if (is_zero_or_null (a) || is_zero_or_null (b)) { 3315b81b6b3SRodney W. Grimes free_value (a); 3325b81b6b3SRodney W. Grimes free_value (b); 333f07e4247SGarrett Wollman return (make_integer ((intmax_t)0)); 3345b81b6b3SRodney W. Grimes } else { 3355b81b6b3SRodney W. Grimes free_value (b); 3365b81b6b3SRodney W. Grimes return (a); 3375b81b6b3SRodney W. Grimes } 3385b81b6b3SRodney W. Grimes } 3395b81b6b3SRodney W. Grimes 3405b81b6b3SRodney W. Grimes struct val * 3417669d0fcSWarner Losh op_eq(struct val *a, struct val *b) 3425b81b6b3SRodney W. Grimes { 3435b81b6b3SRodney W. Grimes struct val *r; 3445b81b6b3SRodney W. Grimes 3455b81b6b3SRodney W. Grimes if (isstring (a) || isstring (b)) { 3465b81b6b3SRodney W. Grimes to_string (a); 3475b81b6b3SRodney W. Grimes to_string (b); 348f07e4247SGarrett Wollman r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) == 0)); 3495b81b6b3SRodney W. Grimes } else { 350717252eaSJoerg Wunsch (void)to_integer(a); 351717252eaSJoerg Wunsch (void)to_integer(b); 352f07e4247SGarrett Wollman r = make_integer ((intmax_t)(a->u.i == b->u.i)); 3535b81b6b3SRodney W. Grimes } 3545b81b6b3SRodney W. Grimes 3555b81b6b3SRodney W. Grimes free_value (a); 3565b81b6b3SRodney W. Grimes free_value (b); 3575b81b6b3SRodney W. Grimes return r; 3585b81b6b3SRodney W. Grimes } 3595b81b6b3SRodney W. Grimes 3605b81b6b3SRodney W. Grimes struct val * 3617669d0fcSWarner Losh op_gt(struct val *a, struct val *b) 3625b81b6b3SRodney W. Grimes { 3635b81b6b3SRodney W. Grimes struct val *r; 3645b81b6b3SRodney W. Grimes 3655b81b6b3SRodney W. Grimes if (isstring (a) || isstring (b)) { 3665b81b6b3SRodney W. Grimes to_string (a); 3675b81b6b3SRodney W. Grimes to_string (b); 368f07e4247SGarrett Wollman r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) > 0)); 3695b81b6b3SRodney W. Grimes } else { 370717252eaSJoerg Wunsch (void)to_integer(a); 371717252eaSJoerg Wunsch (void)to_integer(b); 372f07e4247SGarrett Wollman r = make_integer ((intmax_t)(a->u.i > b->u.i)); 3735b81b6b3SRodney W. Grimes } 3745b81b6b3SRodney W. Grimes 3755b81b6b3SRodney W. Grimes free_value (a); 3765b81b6b3SRodney W. Grimes free_value (b); 3775b81b6b3SRodney W. Grimes return r; 3785b81b6b3SRodney W. Grimes } 3795b81b6b3SRodney W. Grimes 3805b81b6b3SRodney W. Grimes struct val * 3817669d0fcSWarner Losh op_lt(struct val *a, struct val *b) 3825b81b6b3SRodney W. Grimes { 3835b81b6b3SRodney W. Grimes struct val *r; 3845b81b6b3SRodney W. Grimes 3855b81b6b3SRodney W. Grimes if (isstring (a) || isstring (b)) { 3865b81b6b3SRodney W. Grimes to_string (a); 3875b81b6b3SRodney W. Grimes to_string (b); 388f07e4247SGarrett Wollman r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) < 0)); 3895b81b6b3SRodney W. Grimes } else { 390717252eaSJoerg Wunsch (void)to_integer(a); 391717252eaSJoerg Wunsch (void)to_integer(b); 392f07e4247SGarrett Wollman r = make_integer ((intmax_t)(a->u.i < b->u.i)); 3935b81b6b3SRodney W. Grimes } 3945b81b6b3SRodney W. Grimes 3955b81b6b3SRodney W. Grimes free_value (a); 3965b81b6b3SRodney W. Grimes free_value (b); 3975b81b6b3SRodney W. Grimes return r; 3985b81b6b3SRodney W. Grimes } 3995b81b6b3SRodney W. Grimes 4005b81b6b3SRodney W. Grimes struct val * 4017669d0fcSWarner Losh op_ge(struct val *a, struct val *b) 4025b81b6b3SRodney W. Grimes { 4035b81b6b3SRodney W. Grimes struct val *r; 4045b81b6b3SRodney W. Grimes 4055b81b6b3SRodney W. Grimes if (isstring (a) || isstring (b)) { 4065b81b6b3SRodney W. Grimes to_string (a); 4075b81b6b3SRodney W. Grimes to_string (b); 408f07e4247SGarrett Wollman r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) >= 0)); 4095b81b6b3SRodney W. Grimes } else { 410717252eaSJoerg Wunsch (void)to_integer(a); 411717252eaSJoerg Wunsch (void)to_integer(b); 412f07e4247SGarrett Wollman r = make_integer ((intmax_t)(a->u.i >= b->u.i)); 4135b81b6b3SRodney W. Grimes } 4145b81b6b3SRodney W. Grimes 4155b81b6b3SRodney W. Grimes free_value (a); 4165b81b6b3SRodney W. Grimes free_value (b); 4175b81b6b3SRodney W. Grimes return r; 4185b81b6b3SRodney W. Grimes } 4195b81b6b3SRodney W. Grimes 4205b81b6b3SRodney W. Grimes struct val * 4217669d0fcSWarner Losh op_le(struct val *a, struct val *b) 4225b81b6b3SRodney W. Grimes { 4235b81b6b3SRodney W. Grimes struct val *r; 4245b81b6b3SRodney W. Grimes 4255b81b6b3SRodney W. Grimes if (isstring (a) || isstring (b)) { 4265b81b6b3SRodney W. Grimes to_string (a); 4275b81b6b3SRodney W. Grimes to_string (b); 428f07e4247SGarrett Wollman r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) <= 0)); 4295b81b6b3SRodney W. Grimes } else { 430717252eaSJoerg Wunsch (void)to_integer(a); 431717252eaSJoerg Wunsch (void)to_integer(b); 432f07e4247SGarrett Wollman r = make_integer ((intmax_t)(a->u.i <= b->u.i)); 4335b81b6b3SRodney W. Grimes } 4345b81b6b3SRodney W. Grimes 4355b81b6b3SRodney W. Grimes free_value (a); 4365b81b6b3SRodney W. Grimes free_value (b); 4375b81b6b3SRodney W. Grimes return r; 4385b81b6b3SRodney W. Grimes } 4395b81b6b3SRodney W. Grimes 4405b81b6b3SRodney W. Grimes struct val * 4417669d0fcSWarner Losh op_ne(struct val *a, struct val *b) 4425b81b6b3SRodney W. Grimes { 4435b81b6b3SRodney W. Grimes struct val *r; 4445b81b6b3SRodney W. Grimes 4455b81b6b3SRodney W. Grimes if (isstring (a) || isstring (b)) { 4465b81b6b3SRodney W. Grimes to_string (a); 4475b81b6b3SRodney W. Grimes to_string (b); 448f07e4247SGarrett Wollman r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) != 0)); 4495b81b6b3SRodney W. Grimes } else { 450717252eaSJoerg Wunsch (void)to_integer(a); 451717252eaSJoerg Wunsch (void)to_integer(b); 452f07e4247SGarrett Wollman r = make_integer ((intmax_t)(a->u.i != b->u.i)); 4535b81b6b3SRodney W. Grimes } 4545b81b6b3SRodney W. Grimes 4555b81b6b3SRodney W. Grimes free_value (a); 4565b81b6b3SRodney W. Grimes free_value (b); 4575b81b6b3SRodney W. Grimes return r; 4585b81b6b3SRodney W. Grimes } 4595b81b6b3SRodney W. Grimes 460915198b4SStefan Eßer int 461f07e4247SGarrett Wollman chk_plus(intmax_t a, intmax_t b, intmax_t r) 462915198b4SStefan Eßer { 4631393277eSGarrett Wollman 464915198b4SStefan Eßer /* sum of two positive numbers must be positive */ 465915198b4SStefan Eßer if (a > 0 && b > 0 && r <= 0) 466915198b4SStefan Eßer return 1; 467915198b4SStefan Eßer /* sum of two negative numbers must be negative */ 468915198b4SStefan Eßer if (a < 0 && b < 0 && r >= 0) 469915198b4SStefan Eßer return 1; 470915198b4SStefan Eßer /* all other cases are OK */ 471915198b4SStefan Eßer return 0; 472915198b4SStefan Eßer } 473915198b4SStefan Eßer 4745b81b6b3SRodney W. Grimes struct val * 4757669d0fcSWarner Losh op_plus(struct val *a, struct val *b) 4765b81b6b3SRodney W. Grimes { 4775b81b6b3SRodney W. Grimes struct val *r; 4785b81b6b3SRodney W. Grimes 4795b81b6b3SRodney W. Grimes if (!to_integer(a) || !to_integer(b)) { 480f07e4247SGarrett Wollman errx(ERR_EXIT, "non-numeric argument"); 4815b81b6b3SRodney W. Grimes } 4825b81b6b3SRodney W. Grimes 4831393277eSGarrett Wollman if (eflag) { 4841393277eSGarrett Wollman r = make_integer(a->u.i + b->u.i); 485915198b4SStefan Eßer if (chk_plus(a->u.i, b->u.i, r->u.i)) { 486f07e4247SGarrett Wollman errx(ERR_EXIT, "overflow"); 487915198b4SStefan Eßer } 4881393277eSGarrett Wollman } else 4891393277eSGarrett Wollman r = make_integer((long)a->u.i + (long)b->u.i); 4901393277eSGarrett Wollman 4915b81b6b3SRodney W. Grimes free_value (a); 4925b81b6b3SRodney W. Grimes free_value (b); 4935b81b6b3SRodney W. Grimes return r; 4945b81b6b3SRodney W. Grimes } 4955b81b6b3SRodney W. Grimes 496915198b4SStefan Eßer int 497f07e4247SGarrett Wollman chk_minus(intmax_t a, intmax_t b, intmax_t r) 498915198b4SStefan Eßer { 4991393277eSGarrett Wollman 500f07e4247SGarrett Wollman /* special case subtraction of INTMAX_MIN */ 501f07e4247SGarrett Wollman if (b == INTMAX_MIN) { 502915198b4SStefan Eßer if (a >= 0) 503915198b4SStefan Eßer return 1; 504915198b4SStefan Eßer else 505915198b4SStefan Eßer return 0; 506915198b4SStefan Eßer } 507f07e4247SGarrett Wollman /* this is allowed for b != INTMAX_MIN */ 508915198b4SStefan Eßer return chk_plus (a, -b, r); 509915198b4SStefan Eßer } 510915198b4SStefan Eßer 5115b81b6b3SRodney W. Grimes struct val * 5127669d0fcSWarner Losh op_minus(struct val *a, struct val *b) 5135b81b6b3SRodney W. Grimes { 5145b81b6b3SRodney W. Grimes struct val *r; 5155b81b6b3SRodney W. Grimes 5165b81b6b3SRodney W. Grimes if (!to_integer(a) || !to_integer(b)) { 517f07e4247SGarrett Wollman errx(ERR_EXIT, "non-numeric argument"); 5185b81b6b3SRodney W. Grimes } 5195b81b6b3SRodney W. Grimes 5201393277eSGarrett Wollman if (eflag) { 5211393277eSGarrett Wollman r = make_integer(a->u.i - b->u.i); 522915198b4SStefan Eßer if (chk_minus(a->u.i, b->u.i, r->u.i)) { 523f07e4247SGarrett Wollman errx(ERR_EXIT, "overflow"); 524915198b4SStefan Eßer } 5251393277eSGarrett Wollman } else 5261393277eSGarrett Wollman r = make_integer((long)a->u.i - (long)b->u.i); 5271393277eSGarrett Wollman 5285b81b6b3SRodney W. Grimes free_value (a); 5295b81b6b3SRodney W. Grimes free_value (b); 5305b81b6b3SRodney W. Grimes return r; 5315b81b6b3SRodney W. Grimes } 5325b81b6b3SRodney W. Grimes 533915198b4SStefan Eßer int 534f07e4247SGarrett Wollman chk_times(intmax_t a, intmax_t b, intmax_t r) 535915198b4SStefan Eßer { 536915198b4SStefan Eßer /* special case: first operand is 0, no overflow possible */ 537915198b4SStefan Eßer if (a == 0) 538915198b4SStefan Eßer return 0; 539915198b4SStefan Eßer /* cerify that result of division matches second operand */ 540915198b4SStefan Eßer if (r / a != b) 541915198b4SStefan Eßer return 1; 542915198b4SStefan Eßer return 0; 543915198b4SStefan Eßer } 544915198b4SStefan Eßer 5455b81b6b3SRodney W. Grimes struct val * 5467669d0fcSWarner Losh op_times(struct val *a, struct val *b) 5475b81b6b3SRodney W. Grimes { 5485b81b6b3SRodney W. Grimes struct val *r; 5495b81b6b3SRodney W. Grimes 5505b81b6b3SRodney W. Grimes if (!to_integer(a) || !to_integer(b)) { 551f07e4247SGarrett Wollman errx(ERR_EXIT, "non-numeric argument"); 5525b81b6b3SRodney W. Grimes } 5535b81b6b3SRodney W. Grimes 5541393277eSGarrett Wollman if (eflag) { 5551393277eSGarrett Wollman r = make_integer(a->u.i * b->u.i); 556915198b4SStefan Eßer if (chk_times(a->u.i, b->u.i, r->u.i)) { 557f07e4247SGarrett Wollman errx(ERR_EXIT, "overflow"); 558915198b4SStefan Eßer } 5591393277eSGarrett Wollman } else 5601393277eSGarrett Wollman r = make_integer((long)a->u.i * (long)b->u.i); 5611393277eSGarrett Wollman 5625b81b6b3SRodney W. Grimes free_value (a); 5635b81b6b3SRodney W. Grimes free_value (b); 5645b81b6b3SRodney W. Grimes return (r); 5655b81b6b3SRodney W. Grimes } 5665b81b6b3SRodney W. Grimes 567915198b4SStefan Eßer int 568f07e4247SGarrett Wollman chk_div(intmax_t a, intmax_t b) 569915198b4SStefan Eßer { 570915198b4SStefan Eßer /* div by zero has been taken care of before */ 571f07e4247SGarrett Wollman /* only INTMAX_MIN / -1 causes overflow */ 572f07e4247SGarrett Wollman if (a == INTMAX_MIN && b == -1) 573915198b4SStefan Eßer return 1; 574915198b4SStefan Eßer /* everything else is OK */ 575915198b4SStefan Eßer return 0; 576915198b4SStefan Eßer } 577915198b4SStefan Eßer 5785b81b6b3SRodney W. Grimes struct val * 5797669d0fcSWarner Losh op_div(struct val *a, struct val *b) 5805b81b6b3SRodney W. Grimes { 5815b81b6b3SRodney W. Grimes struct val *r; 5825b81b6b3SRodney W. Grimes 5835b81b6b3SRodney W. Grimes if (!to_integer(a) || !to_integer(b)) { 584f07e4247SGarrett Wollman errx(ERR_EXIT, "non-numeric argument"); 5855b81b6b3SRodney W. Grimes } 5865b81b6b3SRodney W. Grimes 5875b81b6b3SRodney W. Grimes if (b->u.i == 0) { 588f07e4247SGarrett Wollman errx(ERR_EXIT, "division by zero"); 5895b81b6b3SRodney W. Grimes } 5905b81b6b3SRodney W. Grimes 5911393277eSGarrett Wollman if (eflag) { 5921393277eSGarrett Wollman r = make_integer(a->u.i / b->u.i); 5933d06e95dSKris Kennaway if (chk_div(a->u.i, b->u.i)) { 594f07e4247SGarrett Wollman errx(ERR_EXIT, "overflow"); 595915198b4SStefan Eßer } 5961393277eSGarrett Wollman } else 5971393277eSGarrett Wollman r = make_integer((long)a->u.i / (long)b->u.i); 5981393277eSGarrett Wollman 5995b81b6b3SRodney W. Grimes free_value (a); 6005b81b6b3SRodney W. Grimes free_value (b); 6015b81b6b3SRodney W. Grimes return r; 6025b81b6b3SRodney W. Grimes } 6035b81b6b3SRodney W. Grimes 6045b81b6b3SRodney W. Grimes struct val * 6057669d0fcSWarner Losh op_rem(struct val *a, struct val *b) 6065b81b6b3SRodney W. Grimes { 6075b81b6b3SRodney W. Grimes struct val *r; 6085b81b6b3SRodney W. Grimes 6095b81b6b3SRodney W. Grimes if (!to_integer(a) || !to_integer(b)) { 610f07e4247SGarrett Wollman errx(ERR_EXIT, "non-numeric argument"); 6115b81b6b3SRodney W. Grimes } 6125b81b6b3SRodney W. Grimes 6135b81b6b3SRodney W. Grimes if (b->u.i == 0) { 614f07e4247SGarrett Wollman errx(ERR_EXIT, "division by zero"); 6155b81b6b3SRodney W. Grimes } 6165b81b6b3SRodney W. Grimes 6171393277eSGarrett Wollman if (eflag) 6181393277eSGarrett Wollman r = make_integer(a->u.i % b->u.i); 619915198b4SStefan Eßer /* chk_rem necessary ??? */ 6201393277eSGarrett Wollman else 6211393277eSGarrett Wollman r = make_integer((long)a->u.i % (long)b->u.i); 6221393277eSGarrett Wollman 6235b81b6b3SRodney W. Grimes free_value (a); 6245b81b6b3SRodney W. Grimes free_value (b); 6255b81b6b3SRodney W. Grimes return r; 6265b81b6b3SRodney W. Grimes } 6275b81b6b3SRodney W. Grimes 6285b81b6b3SRodney W. Grimes struct val * 6297669d0fcSWarner Losh op_colon(struct val *a, struct val *b) 6305b81b6b3SRodney W. Grimes { 6314ba5f298SAndrew Moore regex_t rp; 6324cf61abaSJ.T. Conklin regmatch_t rm[2]; 6334ba5f298SAndrew Moore char errbuf[256]; 6344ba5f298SAndrew Moore int eval; 6354ba5f298SAndrew Moore struct val *v; 636c9fe00dcSJ.T. Conklin 637c9fe00dcSJ.T. Conklin /* coerce to both arguments to strings */ 638c9fe00dcSJ.T. Conklin to_string(a); 639c9fe00dcSJ.T. Conklin to_string(b); 6405b81b6b3SRodney W. Grimes 6414ba5f298SAndrew Moore /* compile regular expression */ 6424a13ab7cSJ.T. Conklin if ((eval = regcomp (&rp, b->u.s, 0)) != 0) { 6434ba5f298SAndrew Moore regerror (eval, &rp, errbuf, sizeof(errbuf)); 644f07e4247SGarrett Wollman errx(ERR_EXIT, "%s", errbuf); 6455b81b6b3SRodney W. Grimes } 6464ba5f298SAndrew Moore 6474ba5f298SAndrew Moore /* compare string against pattern */ 6484a13ab7cSJ.T. Conklin /* remember that patterns are anchored to the beginning of the line */ 6493d06e95dSKris Kennaway if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0) { 6504ba5f298SAndrew Moore if (rm[1].rm_so >= 0) { 65155c497bfSJ.T. Conklin *(a->u.s + rm[1].rm_eo) = '\0'; 6524ba5f298SAndrew Moore v = make_str (a->u.s + rm[1].rm_so); 6534ba5f298SAndrew Moore 6544ba5f298SAndrew Moore } else { 655f07e4247SGarrett Wollman v = make_integer ((intmax_t)(rm[0].rm_eo - rm[0].rm_so)); 6564ba5f298SAndrew Moore } 6574ba5f298SAndrew Moore } else { 65855c497bfSJ.T. Conklin if (rp.re_nsub == 0) { 659f07e4247SGarrett Wollman v = make_integer ((intmax_t)0); 66055c497bfSJ.T. Conklin } else { 66155c497bfSJ.T. Conklin v = make_str (""); 66255c497bfSJ.T. Conklin } 6634ba5f298SAndrew Moore } 6644ba5f298SAndrew Moore 6654ba5f298SAndrew Moore /* free arguments and pattern buffer */ 6664ba5f298SAndrew Moore free_value (a); 6674ba5f298SAndrew Moore free_value (b); 6684ba5f298SAndrew Moore regfree (&rp); 6694ba5f298SAndrew Moore 6704ba5f298SAndrew Moore return v; 6714ba5f298SAndrew Moore } 672