PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/commands/ash/bltin/expr.c

https://bitbucket.org/gthummalapalle/minix
C | 482 lines | 415 code | 32 blank | 35 comment | 153 complexity | 1696c243ce303fe2d9443e45ec744f13 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, WTFPL, AGPL-1.0
  1. /*
  2. * The expr and test commands.
  3. *
  4. * Copyright (C) 1989 by Kenneth Almquist. All rights reserved.
  5. * This file is part of ash, which is distributed under the terms specified
  6. * by the Ash General Public License. See the file named LICENSE.
  7. */
  8. #include "bltin.h"
  9. #include "operators.h"
  10. #include <regex.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #define STACKSIZE 12
  16. #define NESTINCR 16
  17. /* data types */
  18. #define STRING 0
  19. #define INTEGER 1
  20. #define BOOLEAN 2
  21. /*
  22. * This structure hold a value. The type keyword specifies the type of
  23. * the value, and the union u holds the value. The value of a boolean
  24. * is stored in u.num (1 = TRUE, 0 = FALSE).
  25. */
  26. struct value {
  27. int type;
  28. union {
  29. char *string;
  30. long num;
  31. } u;
  32. };
  33. struct operator {
  34. short op; /* which operator */
  35. short pri; /* priority of operator */
  36. };
  37. struct filestat {
  38. char *name; /* name of file */
  39. int rcode; /* return code from stat */
  40. struct stat stat; /* status info on file */
  41. };
  42. extern char *match_begin[10]; /* matched string */
  43. extern short match_length[10]; /* defined in regexp.c */
  44. extern short number_parens; /* number of \( \) pairs */
  45. #ifdef __STDC__
  46. static int expr_is_false(struct value *);
  47. static void expr_operator(int, struct value *, struct filestat *);
  48. static int lookup_op(char *, char *const*);
  49. #else
  50. static int expr_is_false();
  51. static void expr_operator();
  52. static int lookup_op();
  53. #endif
  54. int exprcmd(argc, argv) int argc; char **argv; {
  55. char **ap;
  56. char *opname;
  57. char c;
  58. char *p;
  59. int print;
  60. int nest; /* parenthises nesting */
  61. int op;
  62. int pri;
  63. int skipping;
  64. int binary;
  65. struct operator opstack[STACKSIZE];
  66. struct operator *opsp;
  67. struct value valstack[STACKSIZE + 1];
  68. struct value *valsp;
  69. struct filestat fs;
  70. INITARGS(argv);
  71. c = **argv;
  72. print = 1;
  73. if (c == 't')
  74. print = 0;
  75. else if (c == '[') {
  76. if (! equal(argv[argc - 1], "]"))
  77. error("missing ]");
  78. argv[argc - 1] = NULL;
  79. print = 0;
  80. }
  81. ap = argv + 1;
  82. fs.name = NULL;
  83. /*
  84. * We use operator precedence parsing, evaluating the expression
  85. * as we parse it. Parentheses are handled by bumping up the
  86. * priority of operators using the variable "nest." We use the
  87. * variable "skipping" to turn off evaluation temporarily for the
  88. * short circuit boolean operators. (It is important do the short
  89. * circuit evaluation because under NFS a stat operation can take
  90. * infinitely long.)
  91. */
  92. nest = 0;
  93. skipping = 0;
  94. opsp = opstack + STACKSIZE;
  95. valsp = valstack;
  96. if (*ap == NULL) {
  97. valstack[0].type = BOOLEAN;
  98. valstack[0].u.num = 0;
  99. goto done;
  100. }
  101. for (;;) {
  102. opname = *ap++;
  103. if (opname == NULL)
  104. syntax: error("syntax error");
  105. if (opname[0] == '(' && opname[1] == '\0') {
  106. nest += NESTINCR;
  107. continue;
  108. } else if (*ap && (op = lookup_op(opname, unary_op)) >= 0) {
  109. if (opsp == &opstack[0])
  110. overflow: error("Expression too complex");
  111. --opsp;
  112. opsp->op = op;
  113. opsp->pri = op_priority[op] + nest;
  114. continue;
  115. } else {
  116. if (opname[0] == '\'') {
  117. for (p = opname ; *++p != '\0' ; );
  118. if (--p > opname && *p == '\'') {
  119. *p = '\0';
  120. opname++;
  121. }
  122. }
  123. valsp->type = STRING;
  124. valsp->u.string = opname;
  125. valsp++;
  126. }
  127. for (;;) {
  128. opname = *ap++;
  129. if (opname == NULL) {
  130. if (nest != 0)
  131. goto syntax;
  132. pri = 0;
  133. break;
  134. }
  135. if (opname[0] != ')' || opname[1] != '\0') {
  136. if ((op = lookup_op(opname, binary_op)) < 0)
  137. goto syntax;
  138. op += FIRST_BINARY_OP;
  139. pri = op_priority[op] + nest;
  140. break;
  141. }
  142. if ((nest -= NESTINCR) < 0)
  143. goto syntax;
  144. }
  145. while (opsp < &opstack[STACKSIZE] && opsp->pri >= pri) {
  146. binary = opsp->op;
  147. for (;;) {
  148. valsp--;
  149. c = op_argflag[opsp->op];
  150. if (c == OP_INT) {
  151. if (valsp->type == STRING)
  152. valsp->u.num = atol(valsp->u.string);
  153. valsp->type = INTEGER;
  154. } else if (c >= OP_STRING) { /* OP_STRING or OP_FILE */
  155. if (valsp->type == INTEGER) {
  156. p = stalloc(32);
  157. #ifdef SHELL
  158. fmtstr(p, 32, "%ld", valsp->u.num);
  159. #else
  160. sprintf(p, "%d", valsp->u.num);
  161. #endif
  162. valsp->u.string = p;
  163. } else if (valsp->type == BOOLEAN) {
  164. if (valsp->u.num)
  165. valsp->u.string = "true";
  166. else
  167. valsp->u.string = "";
  168. }
  169. valsp->type = STRING;
  170. if (c == OP_FILE
  171. && (fs.name == NULL
  172. || ! equal(fs.name, valsp->u.string))) {
  173. fs.name = valsp->u.string;
  174. fs.rcode = stat(valsp->u.string, &fs.stat);
  175. }
  176. }
  177. if (binary < FIRST_BINARY_OP)
  178. break;
  179. binary = 0;
  180. }
  181. if (! skipping)
  182. expr_operator(opsp->op, valsp, &fs);
  183. else if (opsp->op == AND1 || opsp->op == OR1)
  184. skipping--;
  185. valsp++; /* push value */
  186. opsp++; /* pop operator */
  187. }
  188. if (opname == NULL)
  189. break;
  190. if (opsp == &opstack[0])
  191. goto overflow;
  192. if (op == AND1 || op == AND2) {
  193. op = AND1;
  194. if (skipping || expr_is_false(valsp - 1))
  195. skipping++;
  196. }
  197. if (op == OR1 || op == OR2) {
  198. op = OR1;
  199. if (skipping || ! expr_is_false(valsp - 1))
  200. skipping++;
  201. }
  202. opsp--;
  203. opsp->op = op;
  204. opsp->pri = pri;
  205. }
  206. done:
  207. if (print) {
  208. if (valstack[0].type == STRING)
  209. printf("%s\n", valstack[0].u.string);
  210. else if (valstack[0].type == INTEGER)
  211. printf("%ld\n", valstack[0].u.num);
  212. else if (valstack[0].u.num != 0)
  213. printf("true\n");
  214. }
  215. return expr_is_false(&valstack[0]);
  216. }
  217. static int
  218. expr_is_false(val)
  219. struct value *val;
  220. {
  221. if (val->type == STRING) {
  222. if (val->u.string[0] == '\0')
  223. return 1;
  224. } else { /* INTEGER or BOOLEAN */
  225. if (val->u.num == 0)
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. /*
  231. * Execute an operator. Op is the operator. Sp is the stack pointer;
  232. * sp[0] refers to the first operand, sp[1] refers to the second operand
  233. * (if any), and the result is placed in sp[0]. The operands are converted
  234. * to the type expected by the operator before expr_operator is called.
  235. * Fs is a pointer to a structure which holds the value of the last call
  236. * to stat, to avoid repeated stat calls on the same file.
  237. */
  238. static void
  239. expr_operator(op, sp, fs)
  240. int op;
  241. struct value *sp;
  242. struct filestat *fs;
  243. {
  244. int i, r;
  245. struct stat st1, st2;
  246. regex_t pat;
  247. regmatch_t rm[2];
  248. switch (op) {
  249. case NOT:
  250. sp->u.num = expr_is_false(sp);
  251. sp->type = BOOLEAN;
  252. break;
  253. case EXISTS:
  254. if (fs->rcode >= 0) goto true;
  255. goto false;
  256. case ISREAD:
  257. i = 04;
  258. goto permission;
  259. case ISWRITE:
  260. i = 02;
  261. goto permission;
  262. case ISEXEC:
  263. i = 01;
  264. permission:
  265. if (fs->stat.st_uid == geteuid())
  266. i <<= 6;
  267. else if (fs->stat.st_gid == getegid())
  268. i <<= 3;
  269. goto filebit; /* true if (stat.st_mode & i) != 0 */
  270. case ISFILE:
  271. i = S_IFREG;
  272. goto filetype;
  273. case ISDIR:
  274. i = S_IFDIR;
  275. goto filetype;
  276. case ISCHAR:
  277. i = S_IFCHR;
  278. goto filetype;
  279. case ISBLOCK:
  280. i = S_IFBLK;
  281. goto filetype;
  282. case ISFIFO:
  283. #ifdef S_IFIFO
  284. i = S_IFIFO;
  285. goto filetype;
  286. #else
  287. goto false;
  288. #endif
  289. filetype:
  290. if ((fs->stat.st_mode & S_IFMT) == i && fs->rcode >= 0) {
  291. true:
  292. sp->u.num = 1;
  293. } else {
  294. false:
  295. sp->u.num = 0;
  296. }
  297. sp->type = BOOLEAN;
  298. break;
  299. case ISSETUID:
  300. i = S_ISUID;
  301. goto filebit;
  302. case ISSETGID:
  303. i = S_ISGID;
  304. goto filebit;
  305. case ISSTICKY:
  306. i = S_ISVTX;
  307. filebit:
  308. if (fs->stat.st_mode & i && fs->rcode >= 0)
  309. goto true;
  310. goto false;
  311. case ISSLINK:
  312. if (lstat(fs->name, &st1) == -1)
  313. goto false;
  314. if (S_ISLNK(st1.st_mode))
  315. goto true;
  316. goto false;
  317. case ISSIZE:
  318. sp->u.num = fs->rcode >= 0? fs->stat.st_size : 0L;
  319. sp->type = INTEGER;
  320. break;
  321. case OLDER:
  322. case NEWER:
  323. if (stat(sp->u.string, &st1) != 0) {
  324. sp->u.num = 0;
  325. } else if (stat((sp + 1)->u.string, &st2) != 0) {
  326. sp->u.num = 1;
  327. } else {
  328. int isnewer = st1.st_mtime >= st2.st_mtime;
  329. if(op == NEWER)
  330. sp->u.num = isnewer;
  331. else
  332. sp->u.num = !isnewer;
  333. }
  334. sp->type = INTEGER;
  335. break;
  336. case ISTTY:
  337. sp->u.num = isatty(sp->u.num);
  338. sp->type = BOOLEAN;
  339. break;
  340. case NULSTR:
  341. if (sp->u.string[0] == '\0')
  342. goto true;
  343. goto false;
  344. case STRLEN:
  345. sp->u.num = strlen(sp->u.string);
  346. sp->type = INTEGER;
  347. break;
  348. case OR1:
  349. case AND1:
  350. /*
  351. * These operators are mostly handled by the parser. If we
  352. * get here it means that both operands were evaluated, so
  353. * the value is the value of the second operand.
  354. */
  355. *sp = *(sp + 1);
  356. break;
  357. case STREQ:
  358. case STRNE:
  359. i = 0;
  360. if (equal(sp->u.string, (sp + 1)->u.string))
  361. i++;
  362. if (op == STRNE)
  363. i = 1 - i;
  364. sp->u.num = i;
  365. sp->type = BOOLEAN;
  366. break;
  367. case EQ:
  368. if (sp->u.num == (sp + 1)->u.num)
  369. goto true;
  370. goto false;
  371. case NE:
  372. if (sp->u.num != (sp + 1)->u.num)
  373. goto true;
  374. goto false;
  375. case GT:
  376. if (sp->u.num > (sp + 1)->u.num)
  377. goto true;
  378. goto false;
  379. case LT:
  380. if (sp->u.num < (sp + 1)->u.num)
  381. goto true;
  382. goto false;
  383. case LE:
  384. if (sp->u.num <= (sp + 1)->u.num)
  385. goto true;
  386. goto false;
  387. case GE:
  388. if (sp->u.num >= (sp + 1)->u.num)
  389. goto true;
  390. goto false;
  391. case PLUS:
  392. sp->u.num += (sp + 1)->u.num;
  393. break;
  394. case MINUS:
  395. sp->u.num -= (sp + 1)->u.num;
  396. break;
  397. case TIMES:
  398. sp->u.num *= (sp + 1)->u.num;
  399. break;
  400. case DIVIDE:
  401. if ((sp + 1)->u.num == 0)
  402. error("Division by zero");
  403. sp->u.num /= (sp + 1)->u.num;
  404. break;
  405. case REM:
  406. if ((sp + 1)->u.num == 0)
  407. error("Division by zero");
  408. sp->u.num %= (sp + 1)->u.num;
  409. break;
  410. case MATCHPAT:
  411. {
  412. r = regcomp(&pat, (sp + 1)->u.string, 0);
  413. if (r)
  414. error("Bad regular expression");
  415. if (regexec(&pat, sp->u.string, 2, rm, 0) == 0 &&
  416. rm[0].rm_so == 0)
  417. {
  418. if (pat.re_nsub > 0) {
  419. sp->u.string[rm[1].rm_eo] = '\0';
  420. sp->u.string = sp->u.string+rm[1].rm_so;
  421. } else {
  422. sp->u.num = rm[0].rm_eo;
  423. sp->type = INTEGER;
  424. }
  425. } else {
  426. if (pat.re_nsub > 0) {
  427. sp->u.string[0] = '\0';
  428. } else {
  429. sp->u.num = 0;
  430. sp->type = INTEGER;
  431. }
  432. }
  433. }
  434. break;
  435. }
  436. }
  437. static int
  438. lookup_op(name, table)
  439. char *name;
  440. char *const*table;
  441. {
  442. register char *const*tp;
  443. register char const *p;
  444. char c = name[1];
  445. for (tp = table ; (p = *tp) != NULL ; tp++) {
  446. if (p[1] == c && equal(p, name))
  447. return tp - table;
  448. }
  449. return -1;
  450. }