Changeset View
Changeset View
Standalone View
Standalone View
bin/expr/expr.y
Show All 35 Lines | struct val { | ||||
enum valtype type; | enum valtype type; | ||||
union { | union { | ||||
char *s; | char *s; | ||||
intmax_t i; | intmax_t i; | ||||
} u; | } u; | ||||
} ; | } ; | ||||
char **av; | char **av; | ||||
int cflags; | |||||
cem: No need for `= 0` for globals — they are zero initialized by definition. (See `nonposix` right… | |||||
int nonposix; | int nonposix; | ||||
struct val *result; | struct val *result; | ||||
void assert_to_integer(struct val *); | void assert_to_integer(struct val *); | ||||
void assert_div(intmax_t, intmax_t); | void assert_div(intmax_t, intmax_t); | ||||
void assert_minus(intmax_t, intmax_t, intmax_t); | void assert_minus(intmax_t, intmax_t, intmax_t); | ||||
void assert_plus(intmax_t, intmax_t, intmax_t); | void assert_plus(intmax_t, intmax_t, intmax_t); | ||||
void assert_times(intmax_t, intmax_t, intmax_t); | void assert_times(intmax_t, intmax_t, intmax_t); | ||||
▲ Show 20 Lines • Show All 218 Lines • ▼ Show 20 Lines | main(int argc, char *argv[]) | ||||
int c; | int c; | ||||
setlocale(LC_ALL, ""); | setlocale(LC_ALL, ""); | ||||
if (getenv("EXPR_COMPAT") != NULL | if (getenv("EXPR_COMPAT") != NULL | ||||
|| check_utility_compat("expr")) { | || check_utility_compat("expr")) { | ||||
av = argv + 1; | av = argv + 1; | ||||
nonposix = 1; | nonposix = 1; | ||||
} else { | } else { | ||||
while ((c = getopt(argc, argv, "e")) != -1) { | while ((c = getopt(argc, argv, "Ee")) != -1) { | ||||
switch (c) { | switch (c) { | ||||
case 'E': | |||||
cflags |= REG_EXTENDED; | |||||
break; | |||||
case 'e': | case 'e': | ||||
nonposix = 1; | nonposix = 1; | ||||
break; | break; | ||||
default: | default: | ||||
errx(ERR_EXIT, | errx(ERR_EXIT, | ||||
"usage: expr [-e] expression\n"); | "usage: expr [-Ee] expression\n"); | ||||
} | } | ||||
} | } | ||||
av = argv + optind; | av = argv + optind; | ||||
} | } | ||||
yyparse(); | yyparse(); | ||||
if (result->type == integer) | if (result->type == integer) | ||||
▲ Show 20 Lines • Show All 239 Lines • ▼ Show 20 Lines | op_colon(struct val *a, struct val *b) | ||||
int eval; | int eval; | ||||
struct val *v; | struct val *v; | ||||
/* coerce both arguments to strings */ | /* coerce both arguments to strings */ | ||||
to_string(a); | to_string(a); | ||||
to_string(b); | to_string(b); | ||||
/* compile regular expression */ | /* compile regular expression */ | ||||
if ((eval = regcomp(&rp, b->u.s, 0)) != 0) { | if ((eval = regcomp(&rp, b->u.s, cflags)) != 0) { | ||||
regerror(eval, &rp, errbuf, sizeof(errbuf)); | regerror(eval, &rp, errbuf, sizeof(errbuf)); | ||||
errx(ERR_EXIT, "%s", errbuf); | errx(ERR_EXIT, "%s", errbuf); | ||||
} | } | ||||
/* compare string against pattern */ | /* compare string against pattern */ | ||||
/* remember that patterns are anchored to the beginning of the line */ | /* remember that patterns are anchored to the beginning of the line */ | ||||
if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0) | if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0) | ||||
if (rm[1].rm_so >= 0) { | if (rm[1].rm_so >= 0) { | ||||
Show All 18 Lines |
No need for = 0 for globals — they are zero initialized by definition. (See nonposix right below.)