logo

utils-std

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

expr.y (12256B)


  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 <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. %}
  78. %union
  79. {
  80. struct val *val;
  81. }
  82. %left <val> '|'
  83. %left <val> '&'
  84. %left <val> '=' '>' '<' GE LE NE
  85. %left <val> '+' '-'
  86. %left <val> '*' '/' '%'
  87. %left <val> ':'
  88. %token <val> TOKEN
  89. %type <val> start expr
  90. %%
  91. start: expr { result = $$; }
  92. expr: TOKEN
  93. | '(' expr ')' { $$ = $2; }
  94. | expr '|' expr { $$ = op_or($1, $3); }
  95. | expr '&' expr { $$ = op_and($1, $3); }
  96. | expr '=' expr { $$ = op_eq($1, $3); }
  97. | expr '>' expr { $$ = op_gt($1, $3); }
  98. | expr '<' expr { $$ = op_lt($1, $3); }
  99. | expr GE expr { $$ = op_ge($1, $3); }
  100. | expr LE expr { $$ = op_le($1, $3); }
  101. | expr NE expr { $$ = op_ne($1, $3); }
  102. | expr '+' expr { $$ = op_plus($1, $3); }
  103. | expr '-' expr { $$ = op_minus($1, $3); }
  104. | expr '*' expr { $$ = op_times($1, $3); }
  105. | expr '/' expr { $$ = op_div($1, $3); }
  106. | expr '%' expr { $$ = op_rem($1, $3); }
  107. | expr ':' expr { $$ = op_colon($1, $3); }
  108. ;
  109. %%
  110. static struct val *
  111. make_integer(intmax_t i)
  112. {
  113. struct val *vp;
  114. vp = (struct val *)malloc(sizeof(*vp));
  115. if (vp == NULL)
  116. errx(ERR_EXIT, "error: malloc(vp) failed");
  117. vp->type = integer;
  118. vp->u.i = i;
  119. return (vp);
  120. }
  121. static struct val *
  122. make_str(const char *s)
  123. {
  124. struct val *vp;
  125. vp = (struct val *)malloc(sizeof(*vp));
  126. if (vp == NULL)
  127. errx(ERR_EXIT, "error: malloc(vp) failed");
  128. if ((vp->u.s = strdup(s)) == NULL)
  129. errx(ERR_EXIT, "error: strdup(s) failed");
  130. if (is_integer(s))
  131. vp->type = numeric_string;
  132. else
  133. vp->type = string;
  134. return (vp);
  135. }
  136. static void
  137. free_value(struct val *vp)
  138. {
  139. if (vp->type == string || vp->type == numeric_string)
  140. free(vp->u.s);
  141. if (vp->type == string || vp->type == numeric_string || vp->type == integer)
  142. free(vp);
  143. }
  144. static int
  145. to_integer(struct val *vp)
  146. {
  147. intmax_t i;
  148. /* we can only convert numeric_string to integer, here */
  149. if (vp->type == numeric_string) {
  150. errno = 0;
  151. i = strtoimax(vp->u.s, (char **)NULL, 10);
  152. /* just keep as numeric_string, if the conversion fails */
  153. if (errno != ERANGE) {
  154. free(vp->u.s);
  155. vp->u.i = i;
  156. vp->type = integer;
  157. }
  158. }
  159. return (vp->type == integer);
  160. }
  161. static void
  162. assert_to_integer(struct val *vp)
  163. {
  164. if (vp->type == string)
  165. errx(ERR_EXIT, "error: not a decimal number: '%s'", vp->u.s);
  166. if (!to_integer(vp))
  167. errx(ERR_EXIT, "error: operand too large: '%s'", vp->u.s);
  168. }
  169. static void
  170. to_string(struct val *vp)
  171. {
  172. char *tmp;
  173. if (vp->type == string || vp->type == numeric_string)
  174. return;
  175. /*
  176. * log_10(x) ~= 0.3 * log_2(x). Rounding up gives the number
  177. * of digits; add one each for the sign and terminating null
  178. * character, respectively.
  179. */
  180. #define NDIGITS(x) (3 * (sizeof(x) * CHAR_BIT) / 10 + 1 + 1 + 1)
  181. tmp = malloc(NDIGITS(vp->u.i));
  182. if (tmp == NULL)
  183. errx(ERR_EXIT, "error: malloc() failed");
  184. sprintf(tmp, "%jd", vp->u.i);
  185. vp->type = string;
  186. vp->u.s = tmp;
  187. }
  188. static int
  189. is_integer(const char *s)
  190. {
  191. if (nonposix) {
  192. if (*s == '\0')
  193. return (1);
  194. while (isspace((unsigned char)*s))
  195. s++;
  196. }
  197. if (*s == '-' || (nonposix && *s == '+'))
  198. s++;
  199. if (*s == '\0')
  200. return (0);
  201. while (isdigit((unsigned char)*s))
  202. s++;
  203. return (*s == '\0');
  204. }
  205. static int
  206. is_string(struct val *vp)
  207. {
  208. /* only TRUE if this string is not a valid integer */
  209. return (vp->type == string);
  210. }
  211. static int
  212. yylex(void)
  213. {
  214. char *p;
  215. if (*av == NULL)
  216. return (0);
  217. p = *av++;
  218. if (strlen(p) == 1) {
  219. if (strchr("|&=<>+-*/%:()", *p))
  220. return (*p);
  221. } else if (strlen(p) == 2 && p[1] == '=') {
  222. switch (*p) {
  223. case '>': return (GE);
  224. case '<': return (LE);
  225. case '!': return (NE);
  226. }
  227. }
  228. yylval.val = make_str(p);
  229. return (TOKEN);
  230. }
  231. static int
  232. is_zero_or_null(struct val *vp)
  233. {
  234. if (vp->type == integer)
  235. return (vp->u.i == 0);
  236. return (*vp->u.s == 0 || (to_integer(vp) && vp->u.i == 0));
  237. }
  238. int
  239. main(int argc, char *argv[])
  240. {
  241. int c;
  242. setlocale(LC_ALL, "");
  243. if (getenv("EXPR_COMPAT") != NULL
  244. || getenv("POSIXLY_CORRECT") != NULL) {
  245. av = argv + 1;
  246. nonposix = 1;
  247. } else {
  248. while ((c = getopt(argc, argv, ":e")) != -1) {
  249. switch (c) {
  250. case 'e':
  251. nonposix = 1;
  252. break;
  253. case ':':
  254. fprintf(stderr, "expr: error: Missing operand for option: '-%c'\n", optopt);
  255. break;
  256. case '?':
  257. fprintf(stderr, "expr: error: Unrecognised option: '-%c'\n", optopt);
  258. break;
  259. default:
  260. errx(ERR_EXIT,
  261. "usage: expr [-e] expression\n");
  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. errx(ERR_EXIT, "error: 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. errx(ERR_EXIT, "error: 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. errx(ERR_EXIT, "error: 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. errx(ERR_EXIT, "error: 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. errx(ERR_EXIT, "error: division by zero");
  439. /* only INTMAX_MIN / -1 causes overflow */
  440. if (a == INTMAX_MIN && b == -1)
  441. errx(ERR_EXIT, "error: 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. errx(ERR_EXIT, "error: %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. }