logo

utils-std

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

expr.y (12573B)


  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 "../libutils/err.h"
  19. #include "../libutils/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. if(ndigits <= 0)
  181. utils_errx(ERR_EXIT, "pre-format length overflow", ndigits);
  182. char *tmp = malloc(ndigits);
  183. if (tmp == NULL)
  184. utils_errx(ERR_EXIT, "malloc(%zd) for to_string() failed", ndigits);
  185. int ret = snprintf(tmp, ndigits, "%jd", vp->u.i);
  186. if(ret < 0)
  187. utils_errx(ERR_EXIT, "failed to format integer %jd as a string", vp->u.i);
  188. vp->type = string;
  189. vp->u.s = tmp;
  190. }
  191. static int
  192. is_integer(const char *s)
  193. {
  194. if (nonposix) {
  195. if (*s == '\0')
  196. return (1);
  197. while (isspace((unsigned char)*s))
  198. s++;
  199. }
  200. if (*s == '-' || (nonposix && *s == '+'))
  201. s++;
  202. if (*s == '\0')
  203. return (0);
  204. while (isdigit((unsigned char)*s))
  205. s++;
  206. return (*s == '\0');
  207. }
  208. static int
  209. is_string(struct val *vp)
  210. {
  211. /* only TRUE if this string is not a valid integer */
  212. return (vp->type == string);
  213. }
  214. static int
  215. yylex(void)
  216. {
  217. char *p;
  218. if (*av == NULL)
  219. return (0);
  220. p = *av++;
  221. if (strlen(p) == 1) {
  222. if (strchr("|&=<>+-*/%:()", *p))
  223. return (*p);
  224. } else if (strlen(p) == 2 && p[1] == '=') {
  225. switch (*p) {
  226. case '>': return (GE);
  227. case '<': return (LE);
  228. case '!': return (NE);
  229. }
  230. }
  231. yylval.val = make_str(p);
  232. return (TOKEN);
  233. }
  234. static int
  235. is_zero_or_null(struct val *vp)
  236. {
  237. if (vp->type == integer)
  238. return (vp->u.i == 0);
  239. return (*vp->u.s == 0 || (to_integer(vp) && vp->u.i == 0));
  240. }
  241. int
  242. main(int argc, char *argv[])
  243. {
  244. int c;
  245. char *lc_all = setlocale(LC_ALL, "");
  246. if(lc_all == NULL)
  247. {
  248. fprintf(stderr, "%s: warning: Failed loading locales. setlocale(LC_ALL, \"\"): %s\n", argv0, strerror(errno));
  249. }
  250. errno = 0;
  251. if (getenv("EXPR_COMPAT") != NULL
  252. || getenv("POSIXLY_CORRECT") != NULL) {
  253. av = argv + 1;
  254. nonposix = 1;
  255. } else {
  256. while ((c = getopt_nolong(argc, argv, ":e")) != -1) {
  257. switch (c) {
  258. case 'e':
  259. nonposix = 1;
  260. break;
  261. case ':':
  262. utils_errx(1, "Missing operand for option: '-%c'", optopt);
  263. break;
  264. case '?':
  265. utils_errx(1, "Unrecognised option: '-%c'", optopt);
  266. break;
  267. default:
  268. abort();
  269. }
  270. }
  271. av = argv + optind;
  272. }
  273. yyparse();
  274. assert(result);
  275. if (result->type == integer)
  276. printf("%jd\n", result->u.i);
  277. else
  278. printf("%s\n", result->u.s);
  279. int err = is_zero_or_null(result);
  280. free_value(result);
  281. return err;
  282. }
  283. static int
  284. yyerror(const char *s)
  285. {
  286. utils_errx(ERR_EXIT, "syntax error: %s", s);
  287. }
  288. static struct val *
  289. op_or(struct val *a, struct val *b)
  290. {
  291. if (!is_zero_or_null(a)) {
  292. free_value(b);
  293. return (a);
  294. }
  295. free_value(a);
  296. if (!is_zero_or_null(b))
  297. return (b);
  298. free_value(b);
  299. return (make_integer((intmax_t)0));
  300. }
  301. static struct val *
  302. op_and(struct val *a, struct val *b)
  303. {
  304. if (is_zero_or_null(a) || is_zero_or_null(b)) {
  305. free_value(a);
  306. free_value(b);
  307. return (make_integer((intmax_t)0));
  308. } else {
  309. free_value(b);
  310. return (a);
  311. }
  312. }
  313. static int
  314. compare_vals(struct val *a, struct val *b)
  315. {
  316. int r;
  317. if (is_string(a) || is_string(b)) {
  318. to_string(a);
  319. to_string(b);
  320. r = strcoll(a->u.s, b->u.s);
  321. } else {
  322. assert_to_integer(a);
  323. assert_to_integer(b);
  324. if (a->u.i > b->u.i)
  325. r = 1;
  326. else if (a->u.i < b->u.i)
  327. r = -1;
  328. else
  329. r = 0;
  330. }
  331. free_value(a);
  332. free_value(b);
  333. return (r);
  334. }
  335. static struct val *
  336. op_eq(struct val *a, struct val *b)
  337. {
  338. return (make_integer((intmax_t)(compare_vals(a, b) == 0)));
  339. }
  340. static struct val *
  341. op_gt(struct val *a, struct val *b)
  342. {
  343. return (make_integer((intmax_t)(compare_vals(a, b) > 0)));
  344. }
  345. static struct val *
  346. op_lt(struct val *a, struct val *b)
  347. {
  348. return (make_integer((intmax_t)(compare_vals(a, b) < 0)));
  349. }
  350. static struct val *
  351. op_ge(struct val *a, struct val *b)
  352. {
  353. return (make_integer((intmax_t)(compare_vals(a, b) >= 0)));
  354. }
  355. static struct val *
  356. op_le(struct val *a, struct val *b)
  357. {
  358. return (make_integer((intmax_t)(compare_vals(a, b) <= 0)));
  359. }
  360. static struct val *
  361. op_ne(struct val *a, struct val *b)
  362. {
  363. return (make_integer((intmax_t)(compare_vals(a, b) != 0)));
  364. }
  365. static void
  366. assert_plus(intmax_t a, intmax_t b, intmax_t r)
  367. {
  368. /*
  369. * sum of two positive numbers must be positive,
  370. * sum of two negative numbers must be negative
  371. */
  372. if ((a > 0 && b > 0 && r <= 0) ||
  373. (a < 0 && b < 0 && r >= 0))
  374. utils_errx(ERR_EXIT, "overflow");
  375. }
  376. static struct val *
  377. op_plus(struct val *a, struct val *b)
  378. {
  379. struct val *r;
  380. assert_to_integer(a);
  381. assert_to_integer(b);
  382. r = make_integer(a->u.i + b->u.i);
  383. assert_plus(a->u.i, b->u.i, r->u.i);
  384. free_value(a);
  385. free_value(b);
  386. return (r);
  387. }
  388. static void
  389. assert_minus(intmax_t a, intmax_t b, intmax_t r)
  390. {
  391. if ((a >= 0 && b < 0 && r <= 0) ||
  392. (a < 0 && b > 0 && r >= 0))
  393. utils_errx(ERR_EXIT, "overflow");
  394. }
  395. static struct val *
  396. op_minus(struct val *a, struct val *b)
  397. {
  398. struct val *r;
  399. assert_to_integer(a);
  400. assert_to_integer(b);
  401. r = make_integer(a->u.i - b->u.i);
  402. assert_minus(a->u.i, b->u.i, r->u.i);
  403. free_value(a);
  404. free_value(b);
  405. return (r);
  406. }
  407. /*
  408. * We depend on undefined behaviour giving a result (in r).
  409. * To test this result, pass it as volatile. This prevents
  410. * optimizing away of the test based on the undefined behaviour.
  411. */
  412. static void
  413. assert_times(intmax_t a, intmax_t b, volatile intmax_t r)
  414. {
  415. /*
  416. * If the first operand is 0, no overflow is possible,
  417. * else the result of the division test must match the
  418. * second operand.
  419. *
  420. * Be careful to avoid overflow in the overflow test, as
  421. * in assert_div(). Overflow in division would kill us
  422. * with a SIGFPE before getting the test wrong. In old
  423. * buggy versions, optimization used to give a null test
  424. * instead of a SIGFPE.
  425. */
  426. if ((a == -1 && b == INTMAX_MIN) || (a != 0 && r / a != b))
  427. utils_errx(ERR_EXIT, "overflow");
  428. }
  429. static struct val *
  430. op_times(struct val *a, struct val *b)
  431. {
  432. struct val *r;
  433. assert_to_integer(a);
  434. assert_to_integer(b);
  435. r = make_integer(a->u.i * b->u.i);
  436. assert_times(a->u.i, b->u.i, r->u.i);
  437. free_value(a);
  438. free_value(b);
  439. return (r);
  440. }
  441. static void
  442. assert_div(intmax_t a, intmax_t b)
  443. {
  444. if (b == 0)
  445. utils_errx(ERR_EXIT, "division by zero");
  446. /* only INTMAX_MIN / -1 causes overflow */
  447. if (a == INTMAX_MIN && b == -1)
  448. utils_errx(ERR_EXIT, "overflow");
  449. }
  450. static struct val *
  451. op_div(struct val *a, struct val *b)
  452. {
  453. struct val *r;
  454. assert_to_integer(a);
  455. assert_to_integer(b);
  456. /* assert based on operands only, not on result */
  457. assert_div(a->u.i, b->u.i);
  458. r = make_integer(a->u.i / b->u.i);
  459. free_value(a);
  460. free_value(b);
  461. return (r);
  462. }
  463. static struct val *
  464. op_rem(struct val *a, struct val *b)
  465. {
  466. struct val *r;
  467. assert_to_integer(a);
  468. assert_to_integer(b);
  469. /* pass a=1 to only check for div by zero */
  470. assert_div(1, b->u.i);
  471. r = make_integer(a->u.i % b->u.i);
  472. free_value(a);
  473. free_value(b);
  474. return (r);
  475. }
  476. static struct val *
  477. op_colon(struct val *a, struct val *b)
  478. {
  479. regex_t rp;
  480. regmatch_t rm[2];
  481. char errbuf[256];
  482. int eval;
  483. struct val *v;
  484. /* coerce both arguments to strings */
  485. to_string(a);
  486. to_string(b);
  487. /* compile regular expression */
  488. if ((eval = regcomp(&rp, b->u.s, 0)) != 0) {
  489. regerror(eval, &rp, errbuf, sizeof(errbuf));
  490. utils_errx(ERR_EXIT, "%s", errbuf);
  491. }
  492. /* compare string against pattern */
  493. /* remember that patterns are anchored to the beginning of the line */
  494. if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0)
  495. if (rm[1].rm_so >= 0) {
  496. *(a->u.s + rm[1].rm_eo) = '\0';
  497. v = make_str(a->u.s + rm[1].rm_so);
  498. } else
  499. v = make_integer((intmax_t)(rm[0].rm_eo));
  500. else
  501. if (rp.re_nsub == 0)
  502. v = make_integer((intmax_t)0);
  503. else
  504. v = make_str("");
  505. /* free arguments and pattern buffer */
  506. free_value(a);
  507. free_value(b);
  508. regfree(&rp);
  509. return (v);
  510. }