logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

expr.y (12481B)


  1. %{
  2. // SPDX-License-Identifier: 0BSD
  3. // Copyright Pace Willisson (pace@blitz.com), J.T. Conklin (jtc@wimsey.com)
  4. /*-
  5. * Written by Pace Willisson (pace@blitz.com)
  6. * and placed in the public domain.
  7. *
  8. * Largely rewritten by J.T. Conklin (jtc@wimsey.com)
  9. */
  10. /*
  11. * Warning: OpenBSD yacc prints the following at the very beginning:
  12. * #include <string.h>
  13. * #include <stdlib.h>
  14. */
  15. #include <assert.h>
  16. #include <sys/types.h>
  17. #include <ctype.h>
  18. #include "../lib/err.h"
  19. #include "../lib/getopt_nolong.h"
  20. #include <errno.h>
  21. #include <inttypes.h>
  22. #include <limits.h>
  23. #include <locale.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <regex.h>
  28. #include <unistd.h>
  29. /*
  30. * POSIX specifies a specific error code for syntax errors. We exit
  31. * with this code for all errors.
  32. */
  33. #define ERR_EXIT 2
  34. enum valtype {
  35. integer, numeric_string, string
  36. } ;
  37. struct val {
  38. enum valtype type;
  39. union {
  40. char *s;
  41. intmax_t i;
  42. } u;
  43. } ;
  44. char **av;
  45. int nonposix;
  46. struct val *result;
  47. static void assert_to_integer(struct val *);
  48. static void assert_div(intmax_t, intmax_t);
  49. static void assert_minus(intmax_t, intmax_t, intmax_t);
  50. static void assert_plus(intmax_t, intmax_t, intmax_t);
  51. static void assert_times(intmax_t, intmax_t, intmax_t);
  52. static int compare_vals(struct val *, struct val *);
  53. static void free_value(struct val *);
  54. static int is_integer(const char *);
  55. static int is_string(struct val *);
  56. static int is_zero_or_null(struct val *);
  57. static struct val *make_integer(intmax_t);
  58. static struct val *make_str(const char *);
  59. static struct val *op_and(struct val *, struct val *);
  60. static struct val *op_colon(struct val *, struct val *);
  61. static struct val *op_div(struct val *, struct val *);
  62. static struct val *op_eq(struct val *, struct val *);
  63. static struct val *op_ge(struct val *, struct val *);
  64. static struct val *op_gt(struct val *, struct val *);
  65. static struct val *op_le(struct val *, struct val *);
  66. static struct val *op_lt(struct val *, struct val *);
  67. static struct val *op_minus(struct val *, struct val *);
  68. static struct val *op_ne(struct val *, struct val *);
  69. static struct val *op_or(struct val *, struct val *);
  70. static struct val *op_plus(struct val *, struct val *);
  71. static struct val *op_rem(struct val *, struct val *);
  72. static struct val *op_times(struct val *, struct val *);
  73. static int to_integer(struct val *);
  74. static void to_string(struct val *);
  75. #define YYERROR_IS_DECLARED
  76. static int yyerror(const char *);
  77. static int yylex(void);
  78. const char *argv0 = "expr";
  79. %}
  80. %union
  81. {
  82. struct val *val;
  83. }
  84. %left <val> '|'
  85. %left <val> '&'
  86. %left <val> '=' '>' '<' GE LE NE
  87. %left <val> '+' '-'
  88. %left <val> '*' '/' '%'
  89. %left <val> ':'
  90. %token <val> TOKEN
  91. %type <val> start expr
  92. %%
  93. start: expr { result = $$; }
  94. expr: TOKEN
  95. | '(' expr ')' { $$ = $2; }
  96. | expr '|' expr { $$ = op_or($1, $3); }
  97. | expr '&' expr { $$ = op_and($1, $3); }
  98. | expr '=' expr { $$ = op_eq($1, $3); }
  99. | expr '>' expr { $$ = op_gt($1, $3); }
  100. | expr '<' expr { $$ = op_lt($1, $3); }
  101. | expr GE expr { $$ = op_ge($1, $3); }
  102. | expr LE expr { $$ = op_le($1, $3); }
  103. | expr NE expr { $$ = op_ne($1, $3); }
  104. | expr '+' expr { $$ = op_plus($1, $3); }
  105. | expr '-' expr { $$ = op_minus($1, $3); }
  106. | expr '*' expr { $$ = op_times($1, $3); }
  107. | expr '/' expr { $$ = op_div($1, $3); }
  108. | expr '%' expr { $$ = op_rem($1, $3); }
  109. | expr ':' expr { $$ = op_colon($1, $3); }
  110. ;
  111. %%
  112. static struct val *
  113. make_integer(intmax_t i)
  114. {
  115. struct val *vp;
  116. vp = (struct val *)malloc(sizeof(*vp));
  117. if (vp == NULL)
  118. utils_errx(ERR_EXIT, "malloc(vp) failed");
  119. vp->type = integer;
  120. vp->u.i = i;
  121. return (vp);
  122. }
  123. static struct val *
  124. make_str(const char *s)
  125. {
  126. struct val *vp;
  127. vp = (struct val *)malloc(sizeof(*vp));
  128. if (vp == NULL)
  129. utils_errx(ERR_EXIT, "malloc(vp) failed");
  130. if ((vp->u.s = strdup(s)) == NULL)
  131. utils_errx(ERR_EXIT, "strdup(s) failed");
  132. if (is_integer(s))
  133. vp->type = numeric_string;
  134. else
  135. vp->type = string;
  136. return (vp);
  137. }
  138. static void
  139. free_value(struct val *vp)
  140. {
  141. if (vp->type == string || vp->type == numeric_string)
  142. free(vp->u.s);
  143. if (vp->type == string || vp->type == numeric_string || vp->type == integer)
  144. free(vp);
  145. }
  146. static int
  147. to_integer(struct val *vp)
  148. {
  149. intmax_t i;
  150. /* we can only convert numeric_string to integer, here */
  151. if (vp->type == numeric_string) {
  152. errno = 0;
  153. i = strtoimax(vp->u.s, (char **)NULL, 10);
  154. /* just keep as numeric_string, if the conversion fails */
  155. if (errno != ERANGE) {
  156. free(vp->u.s);
  157. vp->u.i = i;
  158. vp->type = integer;
  159. }
  160. }
  161. return (vp->type == integer);
  162. }
  163. static void
  164. assert_to_integer(struct val *vp)
  165. {
  166. if (vp->type == string)
  167. utils_errx(ERR_EXIT, "not a decimal number: '%s'", vp->u.s);
  168. if (!to_integer(vp))
  169. utils_errx(ERR_EXIT, "operand too large: '%s'", vp->u.s);
  170. }
  171. static void
  172. to_string(struct val *vp)
  173. {
  174. if (vp->type == string || vp->type == numeric_string)
  175. return;
  176. int ndigits = snprintf(NULL, 0, "%jd", vp->u.i);
  177. if(ndigits < 0)
  178. utils_errx(ERR_EXIT, "failed to pre-format integer %jd as a string", vp->u.i);
  179. ndigits++; // NULL terminator
  180. char *tmp = malloc(ndigits);
  181. if (tmp == NULL)
  182. utils_errx(ERR_EXIT, "malloc(%zd) for to_string() failed", ndigits);
  183. int ret = snprintf(tmp, ndigits, "%jd", vp->u.i);
  184. if(ret < 0)
  185. utils_errx(ERR_EXIT, "failed to format integer %jd as a string", vp->u.i);
  186. vp->type = string;
  187. vp->u.s = tmp;
  188. }
  189. static int
  190. is_integer(const char *s)
  191. {
  192. if (nonposix) {
  193. if (*s == '\0')
  194. return (1);
  195. while (isspace((unsigned char)*s))
  196. s++;
  197. }
  198. if (*s == '-' || (nonposix && *s == '+'))
  199. s++;
  200. if (*s == '\0')
  201. return (0);
  202. while (isdigit((unsigned char)*s))
  203. s++;
  204. return (*s == '\0');
  205. }
  206. static int
  207. is_string(struct val *vp)
  208. {
  209. /* only TRUE if this string is not a valid integer */
  210. return (vp->type == string);
  211. }
  212. static int
  213. yylex(void)
  214. {
  215. char *p;
  216. if (*av == NULL)
  217. return (0);
  218. p = *av++;
  219. if (strlen(p) == 1) {
  220. if (strchr("|&=<>+-*/%:()", *p))
  221. return (*p);
  222. } else if (strlen(p) == 2 && p[1] == '=') {
  223. switch (*p) {
  224. case '>': return (GE);
  225. case '<': return (LE);
  226. case '!': return (NE);
  227. }
  228. }
  229. yylval.val = make_str(p);
  230. return (TOKEN);
  231. }
  232. static int
  233. is_zero_or_null(struct val *vp)
  234. {
  235. if (vp->type == integer)
  236. return (vp->u.i == 0);
  237. return (*vp->u.s == 0 || (to_integer(vp) && vp->u.i == 0));
  238. }
  239. int
  240. main(int argc, char *argv[])
  241. {
  242. int c;
  243. char *lc_all = setlocale(LC_ALL, "");
  244. if(lc_all == NULL)
  245. {
  246. fprintf(stderr, "%s: warning: Failed loading locales. setlocale(LC_ALL, \"\"): %s\n", argv0, strerror(errno));
  247. }
  248. errno = 0;
  249. if (getenv("EXPR_COMPAT") != NULL
  250. || getenv("POSIXLY_CORRECT") != NULL) {
  251. av = argv + 1;
  252. nonposix = 1;
  253. } else {
  254. while ((c = getopt_nolong(argc, argv, ":e")) != -1) {
  255. switch (c) {
  256. case 'e':
  257. nonposix = 1;
  258. break;
  259. case ':':
  260. utils_errx(1, "Missing operand for option: '-%c'", optopt);
  261. break;
  262. case '?':
  263. utils_errx(1, "Unrecognised option: '-%c'", optopt);
  264. break;
  265. default:
  266. abort();
  267. }
  268. }
  269. av = argv + optind;
  270. }
  271. yyparse();
  272. assert(result);
  273. if (result->type == integer)
  274. printf("%jd\n", result->u.i);
  275. else
  276. printf("%s\n", result->u.s);
  277. int err = is_zero_or_null(result);
  278. free_value(result);
  279. return err;
  280. }
  281. static int
  282. yyerror(const char *s)
  283. {
  284. utils_errx(ERR_EXIT, "syntax error: %s", s);
  285. }
  286. static struct val *
  287. op_or(struct val *a, struct val *b)
  288. {
  289. if (!is_zero_or_null(a)) {
  290. free_value(b);
  291. return (a);
  292. }
  293. free_value(a);
  294. if (!is_zero_or_null(b))
  295. return (b);
  296. free_value(b);
  297. return (make_integer((intmax_t)0));
  298. }
  299. static struct val *
  300. op_and(struct val *a, struct val *b)
  301. {
  302. if (is_zero_or_null(a) || is_zero_or_null(b)) {
  303. free_value(a);
  304. free_value(b);
  305. return (make_integer((intmax_t)0));
  306. } else {
  307. free_value(b);
  308. return (a);
  309. }
  310. }
  311. static int
  312. compare_vals(struct val *a, struct val *b)
  313. {
  314. int r;
  315. if (is_string(a) || is_string(b)) {
  316. to_string(a);
  317. to_string(b);
  318. r = strcoll(a->u.s, b->u.s);
  319. } else {
  320. assert_to_integer(a);
  321. assert_to_integer(b);
  322. if (a->u.i > b->u.i)
  323. r = 1;
  324. else if (a->u.i < b->u.i)
  325. r = -1;
  326. else
  327. r = 0;
  328. }
  329. free_value(a);
  330. free_value(b);
  331. return (r);
  332. }
  333. static struct val *
  334. op_eq(struct val *a, struct val *b)
  335. {
  336. return (make_integer((intmax_t)(compare_vals(a, b) == 0)));
  337. }
  338. static struct val *
  339. op_gt(struct val *a, struct val *b)
  340. {
  341. return (make_integer((intmax_t)(compare_vals(a, b) > 0)));
  342. }
  343. static struct val *
  344. op_lt(struct val *a, struct val *b)
  345. {
  346. return (make_integer((intmax_t)(compare_vals(a, b) < 0)));
  347. }
  348. static struct val *
  349. op_ge(struct val *a, struct val *b)
  350. {
  351. return (make_integer((intmax_t)(compare_vals(a, b) >= 0)));
  352. }
  353. static struct val *
  354. op_le(struct val *a, struct val *b)
  355. {
  356. return (make_integer((intmax_t)(compare_vals(a, b) <= 0)));
  357. }
  358. static struct val *
  359. op_ne(struct val *a, struct val *b)
  360. {
  361. return (make_integer((intmax_t)(compare_vals(a, b) != 0)));
  362. }
  363. static void
  364. assert_plus(intmax_t a, intmax_t b, intmax_t r)
  365. {
  366. /*
  367. * sum of two positive numbers must be positive,
  368. * sum of two negative numbers must be negative
  369. */
  370. if ((a > 0 && b > 0 && r <= 0) ||
  371. (a < 0 && b < 0 && r >= 0))
  372. utils_errx(ERR_EXIT, "overflow");
  373. }
  374. static struct val *
  375. op_plus(struct val *a, struct val *b)
  376. {
  377. struct val *r;
  378. assert_to_integer(a);
  379. assert_to_integer(b);
  380. r = make_integer(a->u.i + b->u.i);
  381. assert_plus(a->u.i, b->u.i, r->u.i);
  382. free_value(a);
  383. free_value(b);
  384. return (r);
  385. }
  386. static void
  387. assert_minus(intmax_t a, intmax_t b, intmax_t r)
  388. {
  389. if ((a >= 0 && b < 0 && r <= 0) ||
  390. (a < 0 && b > 0 && r >= 0))
  391. utils_errx(ERR_EXIT, "overflow");
  392. }
  393. static struct val *
  394. op_minus(struct val *a, struct val *b)
  395. {
  396. struct val *r;
  397. assert_to_integer(a);
  398. assert_to_integer(b);
  399. r = make_integer(a->u.i - b->u.i);
  400. assert_minus(a->u.i, b->u.i, r->u.i);
  401. free_value(a);
  402. free_value(b);
  403. return (r);
  404. }
  405. /*
  406. * We depend on undefined behaviour giving a result (in r).
  407. * To test this result, pass it as volatile. This prevents
  408. * optimizing away of the test based on the undefined behaviour.
  409. */
  410. static void
  411. assert_times(intmax_t a, intmax_t b, volatile intmax_t r)
  412. {
  413. /*
  414. * If the first operand is 0, no overflow is possible,
  415. * else the result of the division test must match the
  416. * second operand.
  417. *
  418. * Be careful to avoid overflow in the overflow test, as
  419. * in assert_div(). Overflow in division would kill us
  420. * with a SIGFPE before getting the test wrong. In old
  421. * buggy versions, optimization used to give a null test
  422. * instead of a SIGFPE.
  423. */
  424. if ((a == -1 && b == INTMAX_MIN) || (a != 0 && r / a != b))
  425. utils_errx(ERR_EXIT, "overflow");
  426. }
  427. static struct val *
  428. op_times(struct val *a, struct val *b)
  429. {
  430. struct val *r;
  431. assert_to_integer(a);
  432. assert_to_integer(b);
  433. r = make_integer(a->u.i * b->u.i);
  434. assert_times(a->u.i, b->u.i, r->u.i);
  435. free_value(a);
  436. free_value(b);
  437. return (r);
  438. }
  439. static void
  440. assert_div(intmax_t a, intmax_t b)
  441. {
  442. if (b == 0)
  443. utils_errx(ERR_EXIT, "division by zero");
  444. /* only INTMAX_MIN / -1 causes overflow */
  445. if (a == INTMAX_MIN && b == -1)
  446. utils_errx(ERR_EXIT, "overflow");
  447. }
  448. static struct val *
  449. op_div(struct val *a, struct val *b)
  450. {
  451. struct val *r;
  452. assert_to_integer(a);
  453. assert_to_integer(b);
  454. /* assert based on operands only, not on result */
  455. assert_div(a->u.i, b->u.i);
  456. r = make_integer(a->u.i / b->u.i);
  457. free_value(a);
  458. free_value(b);
  459. return (r);
  460. }
  461. static struct val *
  462. op_rem(struct val *a, struct val *b)
  463. {
  464. struct val *r;
  465. assert_to_integer(a);
  466. assert_to_integer(b);
  467. /* pass a=1 to only check for div by zero */
  468. assert_div(1, b->u.i);
  469. r = make_integer(a->u.i % b->u.i);
  470. free_value(a);
  471. free_value(b);
  472. return (r);
  473. }
  474. static struct val *
  475. op_colon(struct val *a, struct val *b)
  476. {
  477. regex_t rp;
  478. regmatch_t rm[2];
  479. char errbuf[256];
  480. int eval;
  481. struct val *v;
  482. /* coerce both arguments to strings */
  483. to_string(a);
  484. to_string(b);
  485. /* compile regular expression */
  486. if ((eval = regcomp(&rp, b->u.s, 0)) != 0) {
  487. regerror(eval, &rp, errbuf, sizeof(errbuf));
  488. utils_errx(ERR_EXIT, "%s", errbuf);
  489. }
  490. /* compare string against pattern */
  491. /* remember that patterns are anchored to the beginning of the line */
  492. if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0)
  493. if (rm[1].rm_so >= 0) {
  494. *(a->u.s + rm[1].rm_eo) = '\0';
  495. v = make_str(a->u.s + rm[1].rm_so);
  496. } else
  497. v = make_integer((intmax_t)(rm[0].rm_eo));
  498. else
  499. if (rp.re_nsub == 0)
  500. v = make_integer((intmax_t)0);
  501. else
  502. v = make_str("");
  503. /* free arguments and pattern buffer */
  504. free_value(a);
  505. free_value(b);
  506. regfree(&rp);
  507. return (v);
  508. }