PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/freebsd/bin/expr/expr.y

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
Happy | 556 lines | 474 code | 82 blank | 0 comment | 0 complexity | f7ac39f9f096174818872429ffe0575f MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. %{
  2. /*-
  3. * Written by Pace Willisson (pace@blitz.com)
  4. * and placed in the public domain.
  5. *
  6. * Largely rewritten by J.T. Conklin (jtc@wimsey.com)
  7. *
  8. * $FreeBSD$
  9. */
  10. #include <sys/types.h>
  11. #include <ctype.h>
  12. #include <err.h>
  13. #include <errno.h>
  14. #include <inttypes.h>
  15. #include <limits.h>
  16. #include <locale.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <regex.h>
  21. #include <unistd.h>
  22. /*
  23. * POSIX specifies a specific error code for syntax errors. We exit
  24. * with this code for all errors.
  25. */
  26. #define ERR_EXIT 2
  27. enum valtype {
  28. integer, numeric_string, string
  29. } ;
  30. struct val {
  31. enum valtype type;
  32. union {
  33. char *s;
  34. intmax_t i;
  35. } u;
  36. } ;
  37. char **av;
  38. int nonposix;
  39. struct val *result;
  40. void assert_to_integer(struct val *);
  41. void assert_div(intmax_t, intmax_t);
  42. void assert_minus(intmax_t, intmax_t, intmax_t);
  43. void assert_plus(intmax_t, intmax_t, intmax_t);
  44. void assert_times(intmax_t, intmax_t, intmax_t);
  45. int compare_vals(struct val *, struct val *);
  46. void free_value(struct val *);
  47. int is_integer(const char *);
  48. int is_string(struct val *);
  49. int is_zero_or_null(struct val *);
  50. struct val *make_integer(intmax_t);
  51. struct val *make_str(const char *);
  52. struct val *op_and(struct val *, struct val *);
  53. struct val *op_colon(struct val *, struct val *);
  54. struct val *op_div(struct val *, struct val *);
  55. struct val *op_eq(struct val *, struct val *);
  56. struct val *op_ge(struct val *, struct val *);
  57. struct val *op_gt(struct val *, struct val *);
  58. struct val *op_le(struct val *, struct val *);
  59. struct val *op_lt(struct val *, struct val *);
  60. struct val *op_minus(struct val *, struct val *);
  61. struct val *op_ne(struct val *, struct val *);
  62. struct val *op_or(struct val *, struct val *);
  63. struct val *op_plus(struct val *, struct val *);
  64. struct val *op_rem(struct val *, struct val *);
  65. struct val *op_times(struct val *, struct val *);
  66. int to_integer(struct val *);
  67. void to_string(struct val *);
  68. int yyerror(const char *);
  69. int yylex(void);
  70. int yyparse(void);
  71. %}
  72. %union
  73. {
  74. struct val *val;
  75. }
  76. %left <val> '|'
  77. %left <val> '&'
  78. %left <val> '=' '>' '<' GE LE NE
  79. %left <val> '+' '-'
  80. %left <val> '*' '/' '%'
  81. %left <val> ':'
  82. %token <val> TOKEN
  83. %type <val> start expr
  84. %%
  85. start: expr { result = $$; }
  86. expr: TOKEN
  87. | '(' expr ')' { $$ = $2; }
  88. | expr '|' expr { $$ = op_or($1, $3); }
  89. | expr '&' expr { $$ = op_and($1, $3); }
  90. | expr '=' expr { $$ = op_eq($1, $3); }
  91. | expr '>' expr { $$ = op_gt($1, $3); }
  92. | expr '<' expr { $$ = op_lt($1, $3); }
  93. | expr GE expr { $$ = op_ge($1, $3); }
  94. | expr LE expr { $$ = op_le($1, $3); }
  95. | expr NE expr { $$ = op_ne($1, $3); }
  96. | expr '+' expr { $$ = op_plus($1, $3); }
  97. | expr '-' expr { $$ = op_minus($1, $3); }
  98. | expr '*' expr { $$ = op_times($1, $3); }
  99. | expr '/' expr { $$ = op_div($1, $3); }
  100. | expr '%' expr { $$ = op_rem($1, $3); }
  101. | expr ':' expr { $$ = op_colon($1, $3); }
  102. ;
  103. %%
  104. struct val *
  105. make_integer(intmax_t i)
  106. {
  107. struct val *vp;
  108. vp = (struct val *)malloc(sizeof(*vp));
  109. if (vp == NULL)
  110. errx(ERR_EXIT, "malloc() failed");
  111. vp->type = integer;
  112. vp->u.i = i;
  113. return (vp);
  114. }
  115. struct val *
  116. make_str(const char *s)
  117. {
  118. struct val *vp;
  119. vp = (struct val *)malloc(sizeof(*vp));
  120. if (vp == NULL || ((vp->u.s = strdup(s)) == NULL))
  121. errx(ERR_EXIT, "malloc() failed");
  122. if (is_integer(s))
  123. vp->type = numeric_string;
  124. else
  125. vp->type = string;
  126. return (vp);
  127. }
  128. void
  129. free_value(struct val *vp)
  130. {
  131. if (vp->type == string || vp->type == numeric_string)
  132. free(vp->u.s);
  133. }
  134. int
  135. to_integer(struct val *vp)
  136. {
  137. intmax_t i;
  138. /* we can only convert numeric_string to integer, here */
  139. if (vp->type == numeric_string) {
  140. errno = 0;
  141. i = strtoimax(vp->u.s, (char **)NULL, 10);
  142. /* just keep as numeric_string, if the conversion fails */
  143. if (errno != ERANGE) {
  144. free(vp->u.s);
  145. vp->u.i = i;
  146. vp->type = integer;
  147. }
  148. }
  149. return (vp->type == integer);
  150. }
  151. void
  152. assert_to_integer(struct val *vp)
  153. {
  154. if (vp->type == string)
  155. errx(ERR_EXIT, "not a decimal number: '%s'", vp->u.s);
  156. if (!to_integer(vp))
  157. errx(ERR_EXIT, "operand too large: '%s'", vp->u.s);
  158. }
  159. void
  160. to_string(struct val *vp)
  161. {
  162. char *tmp;
  163. if (vp->type == string || vp->type == numeric_string)
  164. return;
  165. /*
  166. * log_10(x) ~= 0.3 * log_2(x). Rounding up gives the number
  167. * of digits; add one each for the sign and terminating null
  168. * character, respectively.
  169. */
  170. #define NDIGITS(x) (3 * (sizeof(x) * CHAR_BIT) / 10 + 1 + 1 + 1)
  171. tmp = malloc(NDIGITS(vp->u.i));
  172. if (tmp == NULL)
  173. errx(ERR_EXIT, "malloc() failed");
  174. sprintf(tmp, "%jd", vp->u.i);
  175. vp->type = string;
  176. vp->u.s = tmp;
  177. }
  178. int
  179. is_integer(const char *s)
  180. {
  181. if (nonposix) {
  182. if (*s == '\0')
  183. return (1);
  184. while (isspace((unsigned char)*s))
  185. s++;
  186. }
  187. if (*s == '-' || (nonposix && *s == '+'))
  188. s++;
  189. if (*s == '\0')
  190. return (0);
  191. while (isdigit((unsigned char)*s))
  192. s++;
  193. return (*s == '\0');
  194. }
  195. int
  196. is_string(struct val *vp)
  197. {
  198. /* only TRUE if this string is not a valid integer */
  199. return (vp->type == string);
  200. }
  201. int
  202. yylex(void)
  203. {
  204. char *p;
  205. if (*av == NULL)
  206. return (0);
  207. p = *av++;
  208. if (strlen(p) == 1) {
  209. if (strchr("|&=<>+-*/%:()", *p))
  210. return (*p);
  211. } else if (strlen(p) == 2 && p[1] == '=') {
  212. switch (*p) {
  213. case '>': return (GE);
  214. case '<': return (LE);
  215. case '!': return (NE);
  216. }
  217. }
  218. yylval.val = make_str(p);
  219. return (TOKEN);
  220. }
  221. int
  222. is_zero_or_null(struct val *vp)
  223. {
  224. if (vp->type == integer)
  225. return (vp->u.i == 0);
  226. return (*vp->u.s == 0 || (to_integer(vp) && vp->u.i == 0));
  227. }
  228. int
  229. main(int argc, char *argv[])
  230. {
  231. int c;
  232. setlocale(LC_ALL, "");
  233. if (getenv("EXPR_COMPAT") != NULL
  234. || check_utility_compat("expr")) {
  235. av = argv + 1;
  236. nonposix = 1;
  237. } else {
  238. while ((c = getopt(argc, argv, "e")) != -1) {
  239. switch (c) {
  240. case 'e':
  241. nonposix = 1;
  242. break;
  243. default:
  244. errx(ERR_EXIT,
  245. "usage: expr [-e] expression\n");
  246. }
  247. }
  248. av = argv + optind;
  249. }
  250. yyparse();
  251. if (result->type == integer)
  252. printf("%jd\n", result->u.i);
  253. else
  254. printf("%s\n", result->u.s);
  255. return (is_zero_or_null(result));
  256. }
  257. int
  258. yyerror(const char *s __unused)
  259. {
  260. errx(ERR_EXIT, "syntax error");
  261. }
  262. struct val *
  263. op_or(struct val *a, struct val *b)
  264. {
  265. if (!is_zero_or_null(a)) {
  266. free_value(b);
  267. return (a);
  268. }
  269. free_value(a);
  270. if (!is_zero_or_null(b))
  271. return (b);
  272. free_value(b);
  273. return (make_integer((intmax_t)0));
  274. }
  275. struct val *
  276. op_and(struct val *a, struct val *b)
  277. {
  278. if (is_zero_or_null(a) || is_zero_or_null(b)) {
  279. free_value(a);
  280. free_value(b);
  281. return (make_integer((intmax_t)0));
  282. } else {
  283. free_value(b);
  284. return (a);
  285. }
  286. }
  287. int
  288. compare_vals(struct val *a, struct val *b)
  289. {
  290. int r;
  291. if (is_string(a) || is_string(b)) {
  292. to_string(a);
  293. to_string(b);
  294. r = strcoll(a->u.s, b->u.s);
  295. } else {
  296. assert_to_integer(a);
  297. assert_to_integer(b);
  298. if (a->u.i > b->u.i)
  299. r = 1;
  300. else if (a->u.i < b->u.i)
  301. r = -1;
  302. else
  303. r = 0;
  304. }
  305. free_value(a);
  306. free_value(b);
  307. return (r);
  308. }
  309. struct val *
  310. op_eq(struct val *a, struct val *b)
  311. {
  312. return (make_integer((intmax_t)(compare_vals(a, b) == 0)));
  313. }
  314. struct val *
  315. op_gt(struct val *a, struct val *b)
  316. {
  317. return (make_integer((intmax_t)(compare_vals(a, b) > 0)));
  318. }
  319. struct val *
  320. op_lt(struct val *a, struct val *b)
  321. {
  322. return (make_integer((intmax_t)(compare_vals(a, b) < 0)));
  323. }
  324. struct val *
  325. op_ge(struct val *a, struct val *b)
  326. {
  327. return (make_integer((intmax_t)(compare_vals(a, b) >= 0)));
  328. }
  329. struct val *
  330. op_le(struct val *a, struct val *b)
  331. {
  332. return (make_integer((intmax_t)(compare_vals(a, b) <= 0)));
  333. }
  334. struct val *
  335. op_ne(struct val *a, struct val *b)
  336. {
  337. return (make_integer((intmax_t)(compare_vals(a, b) != 0)));
  338. }
  339. void
  340. assert_plus(intmax_t a, intmax_t b, intmax_t r)
  341. {
  342. /*
  343. * sum of two positive numbers must be positive,
  344. * sum of two negative numbers must be negative
  345. */
  346. if ((a > 0 && b > 0 && r <= 0) ||
  347. (a < 0 && b < 0 && r >= 0))
  348. errx(ERR_EXIT, "overflow");
  349. }
  350. struct val *
  351. op_plus(struct val *a, struct val *b)
  352. {
  353. struct val *r;
  354. assert_to_integer(a);
  355. assert_to_integer(b);
  356. r = make_integer(a->u.i + b->u.i);
  357. assert_plus(a->u.i, b->u.i, r->u.i);
  358. free_value(a);
  359. free_value(b);
  360. return (r);
  361. }
  362. void
  363. assert_minus(intmax_t a, intmax_t b, intmax_t r)
  364. {
  365. /* special case subtraction of INTMAX_MIN */
  366. if (b == INTMAX_MIN && a < 0)
  367. errx(ERR_EXIT, "overflow");
  368. /* check addition of negative subtrahend */
  369. assert_plus(a, -b, r);
  370. }
  371. struct val *
  372. op_minus(struct val *a, struct val *b)
  373. {
  374. struct val *r;
  375. assert_to_integer(a);
  376. assert_to_integer(b);
  377. r = make_integer(a->u.i - b->u.i);
  378. assert_minus(a->u.i, b->u.i, r->u.i);
  379. free_value(a);
  380. free_value(b);
  381. return (r);
  382. }
  383. void
  384. assert_times(intmax_t a, intmax_t b, intmax_t r)
  385. {
  386. /*
  387. * if first operand is 0, no overflow is possible,
  388. * else result of division test must match second operand
  389. */
  390. if (a != 0 && r / a != b)
  391. errx(ERR_EXIT, "overflow");
  392. }
  393. struct val *
  394. op_times(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_times(a->u.i, b->u.i, r->u.i);
  401. free_value(a);
  402. free_value(b);
  403. return (r);
  404. }
  405. void
  406. assert_div(intmax_t a, intmax_t b)
  407. {
  408. if (b == 0)
  409. errx(ERR_EXIT, "division by zero");
  410. /* only INTMAX_MIN / -1 causes overflow */
  411. if (a == INTMAX_MIN && b == -1)
  412. errx(ERR_EXIT, "overflow");
  413. }
  414. struct val *
  415. op_div(struct val *a, struct val *b)
  416. {
  417. struct val *r;
  418. assert_to_integer(a);
  419. assert_to_integer(b);
  420. /* assert based on operands only, not on result */
  421. assert_div(a->u.i, b->u.i);
  422. r = make_integer(a->u.i / b->u.i);
  423. free_value(a);
  424. free_value(b);
  425. return (r);
  426. }
  427. struct val *
  428. op_rem(struct val *a, struct val *b)
  429. {
  430. struct val *r;
  431. assert_to_integer(a);
  432. assert_to_integer(b);
  433. /* pass a=1 to only check for div by zero */
  434. assert_div(1, b->u.i);
  435. r = make_integer(a->u.i % b->u.i);
  436. free_value(a);
  437. free_value(b);
  438. return (r);
  439. }
  440. struct val *
  441. op_colon(struct val *a, struct val *b)
  442. {
  443. regex_t rp;
  444. regmatch_t rm[2];
  445. char errbuf[256];
  446. int eval;
  447. struct val *v;
  448. /* coerce both arguments to strings */
  449. to_string(a);
  450. to_string(b);
  451. /* compile regular expression */
  452. if ((eval = regcomp(&rp, b->u.s, 0)) != 0) {
  453. regerror(eval, &rp, errbuf, sizeof(errbuf));
  454. errx(ERR_EXIT, "%s", errbuf);
  455. }
  456. /* compare string against pattern */
  457. /* remember that patterns are anchored to the beginning of the line */
  458. if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0)
  459. if (rm[1].rm_so >= 0) {
  460. *(a->u.s + rm[1].rm_eo) = '\0';
  461. v = make_str(a->u.s + rm[1].rm_so);
  462. } else
  463. v = make_integer((intmax_t)(rm[0].rm_eo - rm[0].rm_so));
  464. else
  465. if (rp.re_nsub == 0)
  466. v = make_integer((intmax_t)0);
  467. else
  468. v = make_str("");
  469. /* free arguments and pattern buffer */
  470. free_value(a);
  471. free_value(b);
  472. regfree(&rp);
  473. return (v);
  474. }