c_test.c revision 1.2 1 1.2 tls /* $NetBSD: c_test.c,v 1.2 1997/01/12 19:11:40 tls Exp $ */
2 1.2 tls
3 1.1 jtc /*
4 1.1 jtc * test(1); version 7-like -- author Erik Baalbergen
5 1.1 jtc * modified by Eric Gisin to be used as built-in.
6 1.1 jtc * modified by Arnold Robbins to add SVR3 compatibility
7 1.1 jtc * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
8 1.1 jtc * modified by Michael Rendell to add Korn's [[ .. ]] expressions.
9 1.1 jtc * modified by J.T. Conklin to add POSIX compatibility.
10 1.1 jtc */
11 1.1 jtc
12 1.1 jtc #include "sh.h"
13 1.1 jtc #include "ksh_stat.h"
14 1.1 jtc #include "c_test.h"
15 1.1 jtc
16 1.1 jtc /* test(1) accepts the following grammar:
17 1.1 jtc oexpr ::= aexpr | aexpr "-o" oexpr ;
18 1.1 jtc aexpr ::= nexpr | nexpr "-a" aexpr ;
19 1.1 jtc nexpr ::= primary | "!" nexpr ;
20 1.1 jtc primary ::= unary-operator operand
21 1.1 jtc | operand binary-operator operand
22 1.1 jtc | operand
23 1.1 jtc | "(" oexpr ")"
24 1.1 jtc ;
25 1.1 jtc
26 1.1 jtc unary-operator ::= "-a"|"-r"|"-w"|"-x"|"-e"|"-f"|"-d"|"-c"|"-b"|"-p"|
27 1.1 jtc "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|
28 1.1 jtc "-L"|"-h"|"-S"|"-H";
29 1.1 jtc
30 1.1 jtc binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
31 1.1 jtc "-nt"|"-ot"|"-ef"|
32 1.1 jtc "<"|">" # rules used for [[ .. ]] expressions
33 1.1 jtc ;
34 1.1 jtc operand ::= <any thing>
35 1.1 jtc */
36 1.1 jtc
37 1.1 jtc #define T_ERR_EXIT 2 /* POSIX says > 1 for errors */
38 1.1 jtc
39 1.1 jtc struct t_op {
40 1.1 jtc char op_text[4];
41 1.1 jtc Test_op op_num;
42 1.1 jtc };
43 1.1 jtc static const struct t_op u_ops [] = {
44 1.1 jtc {"-a", TO_FILAXST },
45 1.1 jtc {"-b", TO_FILBDEV },
46 1.1 jtc {"-c", TO_FILCDEV },
47 1.1 jtc {"-d", TO_FILID },
48 1.1 jtc {"-e", TO_FILEXST },
49 1.1 jtc {"-f", TO_FILREG },
50 1.1 jtc {"-G", TO_FILGID },
51 1.1 jtc {"-g", TO_FILSETG },
52 1.1 jtc {"-h", TO_FILSYM },
53 1.1 jtc {"-H", TO_FILCDF },
54 1.1 jtc {"-k", TO_FILSTCK },
55 1.1 jtc {"-L", TO_FILSYM },
56 1.1 jtc {"-n", TO_STNZE },
57 1.1 jtc {"-O", TO_FILUID },
58 1.1 jtc {"-o", TO_OPTION },
59 1.1 jtc {"-p", TO_FILFIFO },
60 1.1 jtc {"-r", TO_FILRD },
61 1.1 jtc {"-s", TO_FILGZ },
62 1.1 jtc {"-S", TO_FILSOCK },
63 1.1 jtc {"-t", TO_FILTT },
64 1.1 jtc {"-u", TO_FILSETU },
65 1.1 jtc {"-w", TO_FILWR },
66 1.1 jtc {"-x", TO_FILEX },
67 1.1 jtc {"-z", TO_STZER },
68 1.1 jtc {"", TO_NONOP }
69 1.1 jtc };
70 1.1 jtc static const struct t_op b_ops [] = {
71 1.1 jtc {"=", TO_STEQL },
72 1.1 jtc #ifdef KSH
73 1.1 jtc {"==", TO_STEQL },
74 1.1 jtc #endif /* KSH */
75 1.1 jtc {"!=", TO_STNEQ },
76 1.1 jtc {"<", TO_STLT },
77 1.1 jtc {">", TO_STGT },
78 1.1 jtc {"-eq", TO_INTEQ },
79 1.1 jtc {"-ne", TO_INTNE },
80 1.1 jtc {"-gt", TO_INTGT },
81 1.1 jtc {"-ge", TO_INTGE },
82 1.1 jtc {"-lt", TO_INTLT },
83 1.1 jtc {"-le", TO_INTLE },
84 1.1 jtc {"-ef", TO_FILEQ },
85 1.1 jtc {"-nt", TO_FILNT },
86 1.1 jtc {"-ot", TO_FILOT },
87 1.1 jtc {"", TO_NONOP }
88 1.1 jtc };
89 1.1 jtc
90 1.1 jtc static int test_stat ARGS((const char *path, struct stat *statb));
91 1.1 jtc static int test_eaccess ARGS((const char *path, int mode));
92 1.1 jtc static int test_oexpr ARGS((Test_env *te, int do_eval));
93 1.1 jtc static int test_aexpr ARGS((Test_env *te, int do_eval));
94 1.1 jtc static int test_nexpr ARGS((Test_env *te, int do_eval));
95 1.1 jtc static int test_primary ARGS((Test_env *te, int do_eval));
96 1.1 jtc static int ptest_isa ARGS((Test_env *te, Test_meta meta));
97 1.1 jtc static const char *ptest_getopnd ARGS((Test_env *te, Test_op op, int do_eval));
98 1.1 jtc static int ptest_eval ARGS((Test_env *te, Test_op op, const char *opnd1,
99 1.1 jtc const char *opnd2, int do_eval));
100 1.1 jtc static void ptest_error ARGS((Test_env *te, int offset, const char *msg));
101 1.1 jtc
102 1.1 jtc int
103 1.1 jtc c_test(wp)
104 1.1 jtc char **wp;
105 1.1 jtc {
106 1.1 jtc int argc;
107 1.1 jtc int res;
108 1.1 jtc Test_env te;
109 1.1 jtc
110 1.1 jtc te.flags = 0;
111 1.1 jtc te.isa = ptest_isa;
112 1.1 jtc te.getopnd = ptest_getopnd;
113 1.1 jtc te.eval = ptest_eval;
114 1.1 jtc te.error = ptest_error;
115 1.1 jtc
116 1.1 jtc for (argc = 0; wp[argc]; argc++)
117 1.1 jtc ;
118 1.1 jtc
119 1.1 jtc if (strcmp(wp[0], "[") == 0) {
120 1.1 jtc if (strcmp(wp[--argc], "]") != 0) {
121 1.1 jtc bi_errorf("missing ]");
122 1.1 jtc return T_ERR_EXIT;
123 1.1 jtc }
124 1.1 jtc }
125 1.1 jtc
126 1.1 jtc te.pos.wp = wp + 1;
127 1.1 jtc te.wp_end = wp + argc;
128 1.1 jtc
129 1.1 jtc /*
130 1.1 jtc * Handle the special cases from POSIX.2, section 4.62.4.
131 1.1 jtc * Implementation of all the rules isn't necessary since
132 1.1 jtc * our parser does the right thing for the ommited steps.
133 1.1 jtc */
134 1.1 jtc if (argc <= 5) {
135 1.1 jtc char **owp = wp;
136 1.1 jtc int invert = 0;
137 1.1 jtc Test_op op;
138 1.1 jtc const char *opnd1, *opnd2;
139 1.1 jtc
140 1.1 jtc while (--argc >= 0) {
141 1.1 jtc if ((*te.isa)(&te, TM_END))
142 1.1 jtc return !0;
143 1.1 jtc if (argc == 3) {
144 1.1 jtc opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
145 1.1 jtc if ((op = (Test_op) (*te.isa)(&te, TM_BINOP))) {
146 1.1 jtc opnd2 = (*te.getopnd)(&te, op, 1);
147 1.1 jtc res = (*te.eval)(&te, op, opnd1, opnd2,
148 1.1 jtc 1);
149 1.1 jtc if (te.flags & TEF_ERROR)
150 1.1 jtc return T_ERR_EXIT;
151 1.1 jtc if (invert & 1)
152 1.1 jtc res = !res;
153 1.1 jtc return !res;
154 1.1 jtc }
155 1.1 jtc /* back up to opnd1 */
156 1.1 jtc te.pos.wp--;
157 1.1 jtc }
158 1.1 jtc if (argc == 1) {
159 1.1 jtc opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
160 1.1 jtc res = (*te.eval)(&te, TO_STNZE, opnd1,
161 1.1 jtc (char *) 0, 1);
162 1.1 jtc if (invert & 1)
163 1.1 jtc res = !res;
164 1.1 jtc return !res;
165 1.1 jtc }
166 1.1 jtc if ((*te.isa)(&te, TM_NOT)) {
167 1.1 jtc invert++;
168 1.1 jtc } else
169 1.1 jtc break;
170 1.1 jtc }
171 1.1 jtc te.pos.wp = owp + 1;
172 1.1 jtc }
173 1.1 jtc
174 1.1 jtc return test_parse(&te);
175 1.1 jtc }
176 1.1 jtc
177 1.1 jtc /*
178 1.1 jtc * Generic test routines.
179 1.1 jtc */
180 1.1 jtc
181 1.1 jtc Test_op
182 1.1 jtc test_isop(te, meta, s)
183 1.1 jtc Test_env *te;
184 1.1 jtc Test_meta meta;
185 1.1 jtc const char *s;
186 1.1 jtc {
187 1.1 jtc char sc1;
188 1.1 jtc const struct t_op *otab;
189 1.1 jtc
190 1.1 jtc otab = meta == TM_UNOP ? u_ops : b_ops;
191 1.1 jtc if (*s) {
192 1.1 jtc sc1 = s[1];
193 1.1 jtc for (; otab->op_text[0]; otab++)
194 1.1 jtc if (sc1 == otab->op_text[1]
195 1.1 jtc && strcmp(s, otab->op_text) == 0
196 1.1 jtc && ((te->flags & TEF_DBRACKET)
197 1.1 jtc || (otab->op_num != TO_STLT
198 1.1 jtc && otab->op_num != TO_STGT)))
199 1.1 jtc return otab->op_num;
200 1.1 jtc }
201 1.1 jtc return TO_NONOP;
202 1.1 jtc }
203 1.1 jtc
204 1.1 jtc int
205 1.1 jtc test_eval(te, op, opnd1, opnd2, do_eval)
206 1.1 jtc Test_env *te;
207 1.1 jtc Test_op op;
208 1.1 jtc const char *opnd1;
209 1.1 jtc const char *opnd2;
210 1.1 jtc int do_eval;
211 1.1 jtc {
212 1.1 jtc int res;
213 1.1 jtc int not;
214 1.1 jtc struct stat b1, b2;
215 1.1 jtc
216 1.1 jtc if (!do_eval)
217 1.1 jtc return 0;
218 1.1 jtc
219 1.1 jtc switch ((int) op) {
220 1.1 jtc /*
221 1.1 jtc * Unary Operators
222 1.1 jtc */
223 1.1 jtc case TO_STNZE: /* -n */
224 1.1 jtc return *opnd1 != '\0';
225 1.1 jtc case TO_STZER: /* -z */
226 1.1 jtc return *opnd1 == '\0';
227 1.1 jtc case TO_OPTION: /* -o */
228 1.1 jtc if ((not = *opnd1 == '!'))
229 1.1 jtc opnd1++;
230 1.1 jtc if ((res = option(opnd1)) < 0)
231 1.1 jtc res = 0;
232 1.1 jtc else {
233 1.1 jtc res = Flag(res);
234 1.1 jtc if (not)
235 1.1 jtc res = !res;
236 1.1 jtc }
237 1.1 jtc return res;
238 1.1 jtc case TO_FILRD: /* -r */
239 1.1 jtc return test_eaccess(opnd1, R_OK) == 0;
240 1.1 jtc case TO_FILWR: /* -w */
241 1.1 jtc return test_eaccess(opnd1, W_OK) == 0;
242 1.1 jtc case TO_FILEX: /* -x */
243 1.1 jtc return test_eaccess(opnd1, X_OK) == 0;
244 1.1 jtc case TO_FILAXST: /* -a */
245 1.1 jtc return test_stat(opnd1, &b1) == 0;
246 1.1 jtc case TO_FILEXST: /* -e */
247 1.1 jtc /* at&t ksh does not appear to do the /dev/fd/ thing for
248 1.1 jtc * this (unless the os itself handles it)
249 1.1 jtc */
250 1.1 jtc return stat(opnd1, &b1) == 0;
251 1.1 jtc case TO_FILREG: /* -r */
252 1.1 jtc return test_stat(opnd1, &b1) == 0 && S_ISREG(b1.st_mode);
253 1.1 jtc case TO_FILID: /* -d */
254 1.1 jtc return test_stat(opnd1, &b1) == 0 && S_ISDIR(b1.st_mode);
255 1.1 jtc case TO_FILCDEV: /* -c */
256 1.1 jtc #ifdef S_ISCHR
257 1.1 jtc return test_stat(opnd1, &b1) == 0 && S_ISCHR(b1.st_mode);
258 1.1 jtc #else
259 1.1 jtc return 0;
260 1.1 jtc #endif
261 1.1 jtc case TO_FILBDEV: /* -b */
262 1.1 jtc #ifdef S_ISBLK
263 1.1 jtc return test_stat(opnd1, &b1) == 0 && S_ISBLK(b1.st_mode);
264 1.1 jtc #else
265 1.1 jtc return 0;
266 1.1 jtc #endif
267 1.1 jtc case TO_FILFIFO: /* -p */
268 1.1 jtc #ifdef S_ISFIFO
269 1.1 jtc return test_stat(opnd1, &b1) == 0 && S_ISFIFO(b1.st_mode);
270 1.1 jtc #else
271 1.1 jtc return 0;
272 1.1 jtc #endif
273 1.1 jtc case TO_FILSYM: /* -h -L */
274 1.1 jtc #ifdef S_ISLNK
275 1.1 jtc return lstat(opnd1, &b1) == 0 && S_ISLNK(b1.st_mode);
276 1.1 jtc #else
277 1.1 jtc return 0;
278 1.1 jtc #endif
279 1.1 jtc case TO_FILSOCK: /* -S */
280 1.1 jtc #ifdef S_ISSOCK
281 1.1 jtc return test_stat(opnd1, &b1) == 0 && S_ISSOCK(b1.st_mode);
282 1.1 jtc #else
283 1.1 jtc return 0;
284 1.1 jtc #endif
285 1.1 jtc case TO_FILCDF:/* -H HP context dependent files (directories) */
286 1.1 jtc #ifdef S_ISCDF
287 1.1 jtc {
288 1.1 jtc /* Append a + to filename and check to see if result is a
289 1.1 jtc * setuid directory. CDF stuff in general is hookey, since
290 1.1 jtc * it breaks for the following sequence: echo hi > foo+;
291 1.1 jtc * mkdir foo; echo bye > foo/default; chmod u+s foo
292 1.1 jtc * (foo+ refers to the file with hi in it, there is no way
293 1.1 jtc * to get at the file with bye in it - please correct me if
294 1.1 jtc * I'm wrong about this).
295 1.1 jtc */
296 1.1 jtc int len = strlen(opnd1);
297 1.1 jtc char *p = str_nsave(opnd1, len + 1, ATEMP);
298 1.1 jtc
299 1.1 jtc p[len++] = '+';
300 1.1 jtc p[len] = '\0';
301 1.1 jtc return stat(p, &b1) == 0 && S_ISCDF(b1.st_mode);
302 1.1 jtc }
303 1.1 jtc #else
304 1.1 jtc return 0;
305 1.1 jtc #endif
306 1.1 jtc case TO_FILSETU: /* -u */
307 1.1 jtc #ifdef S_ISUID
308 1.1 jtc return test_stat(opnd1, &b1) == 0
309 1.1 jtc && (b1.st_mode & S_ISUID) == S_ISUID;
310 1.1 jtc #else
311 1.1 jtc return 0;
312 1.1 jtc #endif
313 1.1 jtc case TO_FILSETG: /* -g */
314 1.1 jtc #ifdef S_ISGID
315 1.1 jtc return test_stat(opnd1, &b1) == 0
316 1.1 jtc && (b1.st_mode & S_ISGID) == S_ISGID;
317 1.1 jtc #else
318 1.1 jtc return 0;
319 1.1 jtc #endif
320 1.1 jtc case TO_FILSTCK: /* -k */
321 1.1 jtc return test_stat(opnd1, &b1) == 0
322 1.1 jtc && (b1.st_mode & S_ISVTX) == S_ISVTX;
323 1.1 jtc case TO_FILGZ: /* -s */
324 1.1 jtc return test_stat(opnd1, &b1) == 0 && b1.st_size > 0L;
325 1.1 jtc case TO_FILTT: /* -t */
326 1.1 jtc if (opnd1 && !bi_getn(opnd1, &res)) {
327 1.1 jtc te->flags |= TEF_ERROR;
328 1.1 jtc res = 0;
329 1.1 jtc } else
330 1.1 jtc res = isatty(opnd1 ? res : 0);
331 1.1 jtc return res;
332 1.1 jtc case TO_FILUID: /* -O */
333 1.1 jtc return test_stat(opnd1, &b1) == 0 && b1.st_uid == geteuid();
334 1.1 jtc case TO_FILGID: /* -G */
335 1.1 jtc return test_stat(opnd1, &b1) == 0 && b1.st_gid == getegid();
336 1.1 jtc /*
337 1.1 jtc * Binary Operators
338 1.1 jtc */
339 1.1 jtc case TO_STEQL: /* = */
340 1.1 jtc if (te->flags & TEF_DBRACKET)
341 1.1 jtc return gmatch(opnd1, opnd2, FALSE);
342 1.1 jtc return strcmp(opnd1, opnd2) == 0;
343 1.1 jtc case TO_STNEQ: /* != */
344 1.1 jtc if (te->flags & TEF_DBRACKET)
345 1.1 jtc return !gmatch(opnd1, opnd2, FALSE);
346 1.1 jtc return strcmp(opnd1, opnd2) != 0;
347 1.1 jtc case TO_STLT: /* < */
348 1.1 jtc return strcmp(opnd1, opnd2) < 0;
349 1.1 jtc case TO_STGT: /* > */
350 1.1 jtc return strcmp(opnd1, opnd2) > 0;
351 1.1 jtc case TO_INTEQ: /* -eq */
352 1.1 jtc case TO_INTNE: /* -ne */
353 1.1 jtc case TO_INTGE: /* -ge */
354 1.1 jtc case TO_INTGT: /* -gt */
355 1.1 jtc case TO_INTLE: /* -le */
356 1.1 jtc case TO_INTLT: /* -lt */
357 1.1 jtc {
358 1.1 jtc long v1, v2;
359 1.1 jtc
360 1.1 jtc if (!evaluate(opnd1, &v1, TRUE)
361 1.1 jtc || !evaluate(opnd2, &v2, TRUE))
362 1.1 jtc {
363 1.1 jtc /* error already printed.. */
364 1.1 jtc te->flags |= TEF_ERROR;
365 1.1 jtc return 1;
366 1.1 jtc }
367 1.1 jtc switch ((int) op) {
368 1.1 jtc case TO_INTEQ:
369 1.1 jtc return v1 == v2;
370 1.1 jtc case TO_INTNE:
371 1.1 jtc return v1 != v2;
372 1.1 jtc case TO_INTGE:
373 1.1 jtc return v1 >= v2;
374 1.1 jtc case TO_INTGT:
375 1.1 jtc return v1 > v2;
376 1.1 jtc case TO_INTLE:
377 1.1 jtc return v1 <= v2;
378 1.1 jtc case TO_INTLT:
379 1.1 jtc return v1 < v2;
380 1.1 jtc }
381 1.1 jtc }
382 1.1 jtc case TO_FILNT: /* -nt */
383 1.1 jtc return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0
384 1.1 jtc && b1.st_mtime > b2.st_mtime;
385 1.1 jtc case TO_FILOT: /* -ot */
386 1.1 jtc return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0
387 1.1 jtc && b1.st_mtime < b2.st_mtime;
388 1.1 jtc case TO_FILEQ: /* -ef */
389 1.1 jtc return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0
390 1.1 jtc && b1.st_dev == b2.st_dev
391 1.1 jtc && b1.st_ino == b2.st_ino;
392 1.1 jtc }
393 1.1 jtc (*te->error)(te, 0, "internal error: unknown op");
394 1.1 jtc return 1;
395 1.1 jtc }
396 1.1 jtc
397 1.1 jtc /* Nasty kludge to handle Korn's bizarre /dev/fd hack */
398 1.1 jtc static int
399 1.1 jtc test_stat(path, statb)
400 1.1 jtc const char *path;
401 1.1 jtc struct stat *statb;
402 1.1 jtc {
403 1.1 jtc #if !defined(HAVE_DEV_FD)
404 1.1 jtc int fd;
405 1.1 jtc
406 1.1 jtc if (strncmp(path, "/dev/fd/", 8) == 0 && getn(path + 8, &fd))
407 1.1 jtc return fstat(fd, statb);
408 1.1 jtc #endif /* !HAVE_DEV_FD */
409 1.1 jtc
410 1.1 jtc return stat(path, statb);
411 1.1 jtc }
412 1.1 jtc
413 1.1 jtc /* Another nasty kludge to handle Korn's bizarre /dev/fd hack */
414 1.1 jtc static int
415 1.1 jtc test_eaccess(path, mode)
416 1.1 jtc const char *path;
417 1.1 jtc int mode;
418 1.1 jtc {
419 1.1 jtc #if !defined(HAVE_DEV_FD)
420 1.1 jtc int fd;
421 1.1 jtc
422 1.1 jtc if (strncmp(path, "/dev/fd/", 8) == 0 && getn(path + 8, &fd)) {
423 1.1 jtc int flags;
424 1.1 jtc
425 1.1 jtc if ((flags = fcntl(fd, F_GETFL, 0)) < 0
426 1.1 jtc || (mode & X_OK)
427 1.1 jtc || ((mode & W_OK) && (flags & O_ACCMODE) == O_RDONLY)
428 1.1 jtc || ((mode & R_OK) && (flags & O_ACCMODE) == O_WRONLY))
429 1.1 jtc return -1;
430 1.1 jtc return 0;
431 1.1 jtc }
432 1.1 jtc #endif /* !HAVE_DEV_FD */
433 1.1 jtc
434 1.1 jtc return eaccess(path, mode);
435 1.1 jtc }
436 1.1 jtc
437 1.1 jtc int
438 1.1 jtc test_parse(te)
439 1.1 jtc Test_env *te;
440 1.1 jtc {
441 1.1 jtc int res;
442 1.1 jtc
443 1.1 jtc res = test_oexpr(te, 1);
444 1.1 jtc
445 1.1 jtc if (!(te->flags & TEF_ERROR) && !(*te->isa)(te, TM_END))
446 1.1 jtc (*te->error)(te, 0, "unexpected operator/operand");
447 1.1 jtc
448 1.1 jtc return (te->flags & TEF_ERROR) ? T_ERR_EXIT : !res;
449 1.1 jtc }
450 1.1 jtc
451 1.1 jtc static int
452 1.1 jtc test_oexpr(te, do_eval)
453 1.1 jtc Test_env *te;
454 1.1 jtc int do_eval;
455 1.1 jtc {
456 1.1 jtc int res;
457 1.1 jtc
458 1.1 jtc res = test_aexpr(te, do_eval);
459 1.1 jtc if (res)
460 1.1 jtc do_eval = 0;
461 1.1 jtc if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_OR))
462 1.1 jtc return test_oexpr(te, do_eval) || res;
463 1.1 jtc return res;
464 1.1 jtc }
465 1.1 jtc
466 1.1 jtc static int
467 1.1 jtc test_aexpr(te, do_eval)
468 1.1 jtc Test_env *te;
469 1.1 jtc int do_eval;
470 1.1 jtc {
471 1.1 jtc int res;
472 1.1 jtc
473 1.1 jtc res = test_nexpr(te, do_eval);
474 1.1 jtc if (!res)
475 1.1 jtc do_eval = 0;
476 1.1 jtc if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_AND))
477 1.1 jtc return test_aexpr(te, do_eval) && res;
478 1.1 jtc return res;
479 1.1 jtc }
480 1.1 jtc
481 1.1 jtc static int
482 1.1 jtc test_nexpr(te, do_eval)
483 1.1 jtc Test_env *te;
484 1.1 jtc int do_eval;
485 1.1 jtc {
486 1.1 jtc if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_NOT))
487 1.1 jtc return !test_nexpr(te, do_eval);
488 1.1 jtc return test_primary(te, do_eval);
489 1.1 jtc }
490 1.1 jtc
491 1.1 jtc static int
492 1.1 jtc test_primary(te, do_eval)
493 1.1 jtc Test_env *te;
494 1.1 jtc int do_eval;
495 1.1 jtc {
496 1.1 jtc const char *opnd1, *opnd2;
497 1.1 jtc int res;
498 1.1 jtc Test_op op;
499 1.1 jtc
500 1.1 jtc if (te->flags & TEF_ERROR)
501 1.1 jtc return 0;
502 1.1 jtc if ((*te->isa)(te, TM_OPAREN)) {
503 1.1 jtc res = test_oexpr(te, do_eval);
504 1.1 jtc if (te->flags & TEF_ERROR)
505 1.1 jtc return 0;
506 1.1 jtc if (!(*te->isa)(te, TM_CPAREN)) {
507 1.1 jtc (*te->error)(te, 0, "missing closing paren");
508 1.1 jtc return 0;
509 1.1 jtc }
510 1.1 jtc return res;
511 1.1 jtc }
512 1.1 jtc if ((op = (Test_op) (*te->isa)(te, TM_UNOP))) {
513 1.1 jtc /* unary expression */
514 1.1 jtc opnd1 = (*te->getopnd)(te, op, do_eval);
515 1.1 jtc if (!opnd1) {
516 1.1 jtc (*te->error)(te, -1, "missing argument");
517 1.1 jtc return 0;
518 1.1 jtc }
519 1.1 jtc
520 1.1 jtc return (*te->eval)(te, op, opnd1, (const char *) 0, do_eval);
521 1.1 jtc }
522 1.1 jtc opnd1 = (*te->getopnd)(te, TO_NONOP, do_eval);
523 1.1 jtc if (!opnd1) {
524 1.1 jtc (*te->error)(te, 0, "expression expected");
525 1.1 jtc return 0;
526 1.1 jtc }
527 1.1 jtc if ((op = (Test_op) (*te->isa)(te, TM_BINOP))) {
528 1.1 jtc /* binary expression */
529 1.1 jtc opnd2 = (*te->getopnd)(te, op, do_eval);
530 1.1 jtc if (!opnd2) {
531 1.1 jtc (*te->error)(te, -1, "missing second argument");
532 1.1 jtc return 0;
533 1.1 jtc }
534 1.1 jtc
535 1.1 jtc return (*te->eval)(te, op, opnd1, opnd2, do_eval);
536 1.1 jtc }
537 1.1 jtc if (te->flags & TEF_DBRACKET) {
538 1.1 jtc (*te->error)(te, -1, "missing expression operator");
539 1.1 jtc return 0;
540 1.1 jtc }
541 1.1 jtc return (*te->eval)(te, TO_STNZE, opnd1, (const char *) 0, do_eval);
542 1.1 jtc }
543 1.1 jtc
544 1.1 jtc /*
545 1.1 jtc * Plain test (test and [ .. ]) specific routines.
546 1.1 jtc */
547 1.1 jtc
548 1.1 jtc /* Test if the current token is a whatever. Accepts the current token if
549 1.1 jtc * it is. Returns 0 if it is not, non-zero if it is (in the case of
550 1.1 jtc * TM_UNOP and TM_BINOP, the returned value is a Test_op).
551 1.1 jtc */
552 1.1 jtc static int
553 1.1 jtc ptest_isa(te, meta)
554 1.1 jtc Test_env *te;
555 1.1 jtc Test_meta meta;
556 1.1 jtc {
557 1.1 jtc /* Order important - indexed by Test_meta values */
558 1.1 jtc static const char *const tokens[] = {
559 1.1 jtc "-o", "-a", "!", "(", ")"
560 1.1 jtc };
561 1.1 jtc int ret;
562 1.1 jtc
563 1.1 jtc if (te->pos.wp >= te->wp_end)
564 1.1 jtc return meta == TM_END;
565 1.1 jtc
566 1.1 jtc if (meta == TM_UNOP || meta == TM_BINOP)
567 1.1 jtc ret = (int) test_isop(te, meta, *te->pos.wp);
568 1.1 jtc else if (meta == TM_END)
569 1.1 jtc ret = 0;
570 1.1 jtc else
571 1.1 jtc ret = strcmp(*te->pos.wp, tokens[(int) meta]) == 0;
572 1.1 jtc
573 1.1 jtc /* Accept the token? */
574 1.1 jtc if (ret)
575 1.1 jtc te->pos.wp++;
576 1.1 jtc
577 1.1 jtc return ret;
578 1.1 jtc }
579 1.1 jtc
580 1.1 jtc static const char *
581 1.1 jtc ptest_getopnd(te, op, do_eval)
582 1.1 jtc Test_env *te;
583 1.1 jtc Test_op op;
584 1.1 jtc int do_eval;
585 1.1 jtc {
586 1.1 jtc if (te->pos.wp >= te->wp_end)
587 1.1 jtc return op == TO_FILTT ? "1" : (const char *) 0;
588 1.1 jtc return *te->pos.wp++;
589 1.1 jtc }
590 1.1 jtc
591 1.1 jtc static int
592 1.1 jtc ptest_eval(te, op, opnd1, opnd2, do_eval)
593 1.1 jtc Test_env *te;
594 1.1 jtc Test_op op;
595 1.1 jtc const char *opnd1;
596 1.1 jtc const char *opnd2;
597 1.1 jtc int do_eval;
598 1.1 jtc {
599 1.1 jtc return test_eval(te, op, opnd1, opnd2, do_eval);
600 1.1 jtc }
601 1.1 jtc
602 1.1 jtc static void
603 1.1 jtc ptest_error(te, offset, msg)
604 1.1 jtc Test_env *te;
605 1.1 jtc int offset;
606 1.1 jtc const char *msg;
607 1.1 jtc {
608 1.1 jtc const char *op = te->pos.wp + offset >= te->wp_end ?
609 1.1 jtc (const char *) 0 : te->pos.wp[offset];
610 1.1 jtc
611 1.1 jtc te->flags |= TEF_ERROR;
612 1.1 jtc if (op)
613 1.1 jtc bi_errorf("%s: %s", op, msg);
614 1.1 jtc else
615 1.1 jtc bi_errorf("%s", msg);
616 1.1 jtc }
617