logo

utils-std

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

expr.y (12201B)


  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 <errno.h>
  20. #include <inttypes.h>
  21. #include <limits.h>
  22. #include <locale.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <regex.h>
  27. #include <unistd.h>
  28. /*
  29. * POSIX specifies a specific error code for syntax errors. We exit
  30. * with this code for all errors.
  31. */
  32. #define ERR_EXIT 2
  33. enum valtype {
  34. integer, numeric_string, string
  35. } ;
  36. struct val {
  37. enum valtype type;
  38. union {
  39. char *s;
  40. intmax_t i;
  41. } u;
  42. } ;
  43. char **av;
  44. int nonposix;
  45. struct val *result;
  46. static void assert_to_integer(struct val *);
  47. static void assert_div(intmax_t, intmax_t);
  48. static void assert_minus(intmax_t, intmax_t, intmax_t);
  49. static void assert_plus(intmax_t, intmax_t, intmax_t);
  50. static void assert_times(intmax_t, intmax_t, intmax_t);
  51. static int compare_vals(struct val *, struct val *);
  52. static void free_value(struct val *);
  53. static int is_integer(const char *);
  54. static int is_string(struct val *);
  55. static int is_zero_or_null(struct val *);
  56. static struct val *make_integer(intmax_t);
  57. static struct val *make_str(const char *);
  58. static struct val *op_and(struct val *, struct val *);
  59. static struct val *op_colon(struct val *, struct val *);
  60. static struct val *op_div(struct val *, struct val *);
  61. static struct val *op_eq(struct val *, struct val *);
  62. static struct val *op_ge(struct val *, struct val *);
  63. static struct val *op_gt(struct val *, struct val *);
  64. static struct val *op_le(struct val *, struct val *);
  65. static struct val *op_lt(struct val *, struct val *);
  66. static struct val *op_minus(struct val *, struct val *);
  67. static struct val *op_ne(struct val *, struct val *);
  68. static struct val *op_or(struct val *, struct val *);
  69. static struct val *op_plus(struct val *, struct val *);
  70. static struct val *op_rem(struct val *, struct val *);
  71. static struct val *op_times(struct val *, struct val *);
  72. static int to_integer(struct val *);
  73. static void to_string(struct val *);
  74. #define YYERROR_IS_DECLARED
  75. static int yyerror(const char *);
  76. static int yylex(void);
  77. const char *argv0 = "expr";
  78. %}
  79. %union
  80. {
  81. struct val *val;
  82. }
  83. %left <val> '|'
  84. %left <val> '&'
  85. %left <val> '=' '>' '<' GE LE NE
  86. %left <val> '+' '-'
  87. %left <val> '*' '/' '%'
  88. %left <val> ':'
  89. %token <val> TOKEN
  90. %type <val> start expr
  91. %%
  92. start: expr { result = $$; }
  93. expr: TOKEN
  94. | '(' expr ')' { $$ = $2; }
  95. | expr '|' expr { $$ = op_or($1, $3); }
  96. | expr '&' expr { $$ = op_and($1, $3); }
  97. | expr '=' expr { $$ = op_eq($1, $3); }
  98. | expr '>' expr { $$ = op_gt($1, $3); }
  99. | expr '<' expr { $$ = op_lt($1, $3); }
  100. | expr GE expr { $$ = op_ge($1, $3); }
  101. | expr LE expr { $$ = op_le($1, $3); }
  102. | expr NE expr { $$ = op_ne($1, $3); }
  103. | expr '+' expr { $$ = op_plus($1, $3); }
  104. | expr '-' expr { $$ = op_minus($1, $3); }
  105. | expr '*' expr { $$ = op_times($1, $3); }
  106. | expr '/' expr { $$ = op_div($1, $3); }
  107. | expr '%' expr { $$ = op_rem($1, $3); }
  108. | expr ':' expr { $$ = op_colon($1, $3); }
  109. ;
  110. %%
  111. static struct val *
  112. make_integer(intmax_t i)
  113. {
  114. struct val *vp;
  115. vp = (struct val *)malloc(sizeof(*vp));
  116. if (vp == NULL)
  117. utils_errx(ERR_EXIT, "malloc(vp) failed");
  118. vp->type = integer;
  119. vp->u.i = i;
  120. return (vp);
  121. }
  122. static struct val *
  123. make_str(const char *s)
  124. {
  125. struct val *vp;
  126. vp = (struct val *)malloc(sizeof(*vp));
  127. if (vp == NULL)
  128. utils_errx(ERR_EXIT, "malloc(vp) failed");
  129. if ((vp->u.s = strdup(s)) == NULL)
  130. utils_errx(ERR_EXIT, "strdup(s) failed");
  131. if (is_integer(s))
  132. vp->type = numeric_string;
  133. else
  134. vp->type = string;
  135. return (vp);
  136. }
  137. static void
  138. free_value(struct val *vp)
  139. {
  140. if (vp->type == string || vp->type == numeric_string)
  141. free(vp->u.s);
  142. if (vp->type == string || vp->type == numeric_string || vp->type == integer)
  143. free(vp);
  144. }
  145. static int
  146. to_integer(struct val *vp)
  147. {
  148. intmax_t i;
  149. /* we can only convert numeric_string to integer, here */
  150. if (vp->type == numeric_string) {
  151. errno = 0;
  152. i = strtoimax(vp->u.s, (char **)NULL, 10);
  153. /* just keep as numeric_string, if the conversion fails */
  154. if (errno != ERANGE) {
  155. free(vp->u.s);
  156. vp->u.i = i;
  157. vp->type = integer;
  158. }
  159. }
  160. return (vp->type == integer);
  161. }
  162. static void
  163. assert_to_integer(struct val *vp)
  164. {
  165. if (vp->type == string)
  166. utils_errx(ERR_EXIT, "not a decimal number: '%s'", vp->u.s);
  167. if (!to_integer(vp))
  168. utils_errx(ERR_EXIT, "operand too large: '%s'", vp->u.s);
  169. }
  170. static void
  171. to_string(struct val *vp)
  172. {
  173. char *tmp;
  174. if (vp->type == string || vp->type == numeric_string)
  175. return;
  176. /*
  177. * log_10(x) ~= 0.3 * log_2(x). Rounding up gives the number
  178. * of digits; add one each for the sign and terminating null
  179. * character, respectively.
  180. */
  181. #define NDIGITS(x) (3 * (sizeof(x) * CHAR_BIT) / 10 + 1 + 1 + 1)
  182. tmp = malloc(NDIGITS(vp->u.i));
  183. if (tmp == NULL)
  184. utils_errx(ERR_EXIT, "malloc() failed");
  185. sprintf(tmp, "%jd", 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. setlocale(LC_ALL, "");
  244. if (getenv("EXPR_COMPAT") != NULL
  245. || getenv("POSIXLY_CORRECT") != NULL) {
  246. av = argv + 1;
  247. nonposix = 1;
  248. } else {
  249. while ((c = getopt(argc, argv, ":e")) != -1) {
  250. switch (c) {
  251. case 'e':
  252. nonposix = 1;
  253. break;
  254. case ':':
  255. utils_errx(1, "Missing operand for option: '-%c'\n", optopt);
  256. break;
  257. case '?':
  258. utils_errx(1, "Unrecognised option: '-%c'\n", optopt);
  259. break;
  260. default:
  261. abort();
  262. }
  263. }
  264. av = argv + optind;
  265. }
  266. yyparse();
  267. assert(result);
  268. if (result->type == integer)
  269. printf("%jd\n", result->u.i);
  270. else
  271. printf("%s\n", result->u.s);
  272. int err = is_zero_or_null(result);
  273. free_value(result);
  274. return err;
  275. }
  276. static int
  277. yyerror(const char *s)
  278. {
  279. utils_errx(ERR_EXIT, "syntax error: %s", s);
  280. }
  281. static struct val *
  282. op_or(struct val *a, struct val *b)
  283. {
  284. if (!is_zero_or_null(a)) {
  285. free_value(b);
  286. return (a);
  287. }
  288. free_value(a);
  289. if (!is_zero_or_null(b))
  290. return (b);
  291. free_value(b);
  292. return (make_integer((intmax_t)0));
  293. }
  294. static struct val *
  295. op_and(struct val *a, struct val *b)
  296. {
  297. if (is_zero_or_null(a) || is_zero_or_null(b)) {
  298. free_value(a);
  299. free_value(b);
  300. return (make_integer((intmax_t)0));
  301. } else {
  302. free_value(b);
  303. return (a);
  304. }
  305. }
  306. static int
  307. compare_vals(struct val *a, struct val *b)
  308. {
  309. int r;
  310. if (is_string(a) || is_string(b)) {
  311. to_string(a);
  312. to_string(b);
  313. r = strcoll(a->u.s, b->u.s);
  314. } else {
  315. assert_to_integer(a);
  316. assert_to_integer(b);
  317. if (a->u.i > b->u.i)
  318. r = 1;
  319. else if (a->u.i < b->u.i)
  320. r = -1;
  321. else
  322. r = 0;
  323. }
  324. free_value(a);
  325. free_value(b);
  326. return (r);
  327. }
  328. static struct val *
  329. op_eq(struct val *a, struct val *b)
  330. {
  331. return (make_integer((intmax_t)(compare_vals(a, b) == 0)));
  332. }
  333. static struct val *
  334. op_gt(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_lt(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_ge(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_le(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_ne(struct val *a, struct val *b)
  355. {
  356. return (make_integer((intmax_t)(compare_vals(a, b) != 0)));
  357. }
  358. static void
  359. assert_plus(intmax_t a, intmax_t b, intmax_t r)
  360. {
  361. /*
  362. * sum of two positive numbers must be positive,
  363. * sum of two negative numbers must be negative
  364. */
  365. if ((a > 0 && b > 0 && r <= 0) ||
  366. (a < 0 && b < 0 && r >= 0))
  367. utils_errx(ERR_EXIT, "overflow");
  368. }
  369. static struct val *
  370. op_plus(struct val *a, struct val *b)
  371. {
  372. struct val *r;
  373. assert_to_integer(a);
  374. assert_to_integer(b);
  375. r = make_integer(a->u.i + b->u.i);
  376. assert_plus(a->u.i, b->u.i, r->u.i);
  377. free_value(a);
  378. free_value(b);
  379. return (r);
  380. }
  381. static void
  382. assert_minus(intmax_t a, intmax_t b, intmax_t r)
  383. {
  384. if ((a >= 0 && b < 0 && r <= 0) ||
  385. (a < 0 && b > 0 && r >= 0))
  386. utils_errx(ERR_EXIT, "overflow");
  387. }
  388. static struct val *
  389. op_minus(struct val *a, struct val *b)
  390. {
  391. struct val *r;
  392. assert_to_integer(a);
  393. assert_to_integer(b);
  394. r = make_integer(a->u.i - b->u.i);
  395. assert_minus(a->u.i, b->u.i, r->u.i);
  396. free_value(a);
  397. free_value(b);
  398. return (r);
  399. }
  400. /*
  401. * We depend on undefined behaviour giving a result (in r).
  402. * To test this result, pass it as volatile. This prevents
  403. * optimizing away of the test based on the undefined behaviour.
  404. */
  405. static void
  406. assert_times(intmax_t a, intmax_t b, volatile intmax_t r)
  407. {
  408. /*
  409. * If the first operand is 0, no overflow is possible,
  410. * else the result of the division test must match the
  411. * second operand.
  412. *
  413. * Be careful to avoid overflow in the overflow test, as
  414. * in assert_div(). Overflow in division would kill us
  415. * with a SIGFPE before getting the test wrong. In old
  416. * buggy versions, optimization used to give a null test
  417. * instead of a SIGFPE.
  418. */
  419. if ((a == -1 && b == INTMAX_MIN) || (a != 0 && r / a != b))
  420. utils_errx(ERR_EXIT, "overflow");
  421. }
  422. static struct val *
  423. op_times(struct val *a, struct val *b)
  424. {
  425. struct val *r;
  426. assert_to_integer(a);
  427. assert_to_integer(b);
  428. r = make_integer(a->u.i * b->u.i);
  429. assert_times(a->u.i, b->u.i, r->u.i);
  430. free_value(a);
  431. free_value(b);
  432. return (r);
  433. }
  434. static void
  435. assert_div(intmax_t a, intmax_t b)
  436. {
  437. if (b == 0)
  438. utils_errx(ERR_EXIT, "division by zero");
  439. /* only INTMAX_MIN / -1 causes overflow */
  440. if (a == INTMAX_MIN && b == -1)
  441. utils_errx(ERR_EXIT, "overflow");
  442. }
  443. static struct val *
  444. op_div(struct val *a, struct val *b)
  445. {
  446. struct val *r;
  447. assert_to_integer(a);
  448. assert_to_integer(b);
  449. /* assert based on operands only, not on result */
  450. assert_div(a->u.i, b->u.i);
  451. r = make_integer(a->u.i / b->u.i);
  452. free_value(a);
  453. free_value(b);
  454. return (r);
  455. }
  456. static struct val *
  457. op_rem(struct val *a, struct val *b)
  458. {
  459. struct val *r;
  460. assert_to_integer(a);
  461. assert_to_integer(b);
  462. /* pass a=1 to only check for div by zero */
  463. assert_div(1, b->u.i);
  464. r = make_integer(a->u.i % b->u.i);
  465. free_value(a);
  466. free_value(b);
  467. return (r);
  468. }
  469. static struct val *
  470. op_colon(struct val *a, struct val *b)
  471. {
  472. regex_t rp;
  473. regmatch_t rm[2];
  474. char errbuf[256];
  475. int eval;
  476. struct val *v;
  477. /* coerce both arguments to strings */
  478. to_string(a);
  479. to_string(b);
  480. /* compile regular expression */
  481. if ((eval = regcomp(&rp, b->u.s, 0)) != 0) {
  482. regerror(eval, &rp, errbuf, sizeof(errbuf));
  483. utils_errx(ERR_EXIT, "%s", errbuf);
  484. }
  485. /* compare string against pattern */
  486. /* remember that patterns are anchored to the beginning of the line */
  487. if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0)
  488. if (rm[1].rm_so >= 0) {
  489. *(a->u.s + rm[1].rm_eo) = '\0';
  490. v = make_str(a->u.s + rm[1].rm_so);
  491. } else
  492. v = make_integer((intmax_t)(rm[0].rm_eo));
  493. else
  494. if (rp.re_nsub == 0)
  495. v = make_integer((intmax_t)0);
  496. else
  497. v = make_str("");
  498. /* free arguments and pattern buffer */
  499. free_value(a);
  500. free_value(b);
  501. regfree(&rp);
  502. return (v);
  503. }