Deleted Added
full compact
test.c (139969) test.c (192862)
1/* $NetBSD: test.c,v 1.21 1999/04/05 09:48:38 kleink Exp $ */
2
3/*-
4 * test(1); version 7-like -- author Erik Baalbergen
5 * modified by Eric Gisin to be used as built-in.
6 * modified by Arnold Robbins to add SVR3 compatibility
7 * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
8 * modified by J.T. Conklin for NetBSD.
9 *
10 * This program is in the Public Domain.
11 */
12
13#include <sys/cdefs.h>
1/* $NetBSD: test.c,v 1.21 1999/04/05 09:48:38 kleink Exp $ */
2
3/*-
4 * test(1); version 7-like -- author Erik Baalbergen
5 * modified by Eric Gisin to be used as built-in.
6 * modified by Arnold Robbins to add SVR3 compatibility
7 * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
8 * modified by J.T. Conklin for NetBSD.
9 *
10 * This program is in the Public Domain.
11 */
12
13#include <sys/cdefs.h>
14__FBSDID("$FreeBSD: head/bin/test/test.c 139969 2005-01-10 08:39:26Z imp $");
14__FBSDID("$FreeBSD: head/bin/test/test.c 192862 2009-05-26 22:33:10Z jilles $");
15
16#include <sys/types.h>
17#include <sys/stat.h>
18
19#include <ctype.h>
20#include <err.h>
21#include <errno.h>
22#include <inttypes.h>

--- 135 unchanged lines hidden (view full) ---

158 {"(", LPAREN, PAREN},
159 {")", RPAREN, PAREN},
160 {0, 0, 0}
161};
162
163struct t_op const *t_wp_op;
164int nargc;
165char **t_wp;
15
16#include <sys/types.h>
17#include <sys/stat.h>
18
19#include <ctype.h>
20#include <err.h>
21#include <errno.h>
22#include <inttypes.h>

--- 135 unchanged lines hidden (view full) ---

158 {"(", LPAREN, PAREN},
159 {")", RPAREN, PAREN},
160 {0, 0, 0}
161};
162
163struct t_op const *t_wp_op;
164int nargc;
165char **t_wp;
166int parenlevel;
166
167static int aexpr(enum token);
168static int binop(void);
169static int equalf(const char *, const char *);
170static int filstat(char *, enum token);
171static int getn(const char *);
172static intmax_t getq(const char *);
173static int intcmp(const char *, const char *);
167
168static int aexpr(enum token);
169static int binop(void);
170static int equalf(const char *, const char *);
171static int filstat(char *, enum token);
172static int getn(const char *);
173static intmax_t getq(const char *);
174static int intcmp(const char *, const char *);
174static int isoperand(void);
175static int isunopoperand(void);
176static int islparenoperand(void);
177static int isrparenoperand(void);
175static int newerf(const char *, const char *);
176static int nexpr(enum token);
177static int oexpr(enum token);
178static int olderf(const char *, const char *);
179static int primary(enum token);
180static void syntax(const char *, const char *);
181static enum token t_lex(char *);
182

--- 17 unchanged lines hidden (view full) ---

200 if (--argc <= 0)
201 return 1;
202
203#ifndef SHELL
204 (void)setlocale(LC_CTYPE, "");
205#endif
206 nargc = argc;
207 t_wp = &argv[1];
178static int newerf(const char *, const char *);
179static int nexpr(enum token);
180static int oexpr(enum token);
181static int olderf(const char *, const char *);
182static int primary(enum token);
183static void syntax(const char *, const char *);
184static enum token t_lex(char *);
185

--- 17 unchanged lines hidden (view full) ---

203 if (--argc <= 0)
204 return 1;
205
206#ifndef SHELL
207 (void)setlocale(LC_CTYPE, "");
208#endif
209 nargc = argc;
210 t_wp = &argv[1];
208 res = !oexpr(t_lex(*t_wp));
211 parenlevel = 0;
212 if (nargc == 4 && strcmp(*t_wp, "!") == 0) {
213 /* Things like ! "" -o x do not fit in the normal grammar. */
214 --nargc;
215 ++t_wp;
216 res = oexpr(t_lex(*t_wp));
217 } else
218 res = !oexpr(t_lex(*t_wp));
209
210 if (--nargc > 0)
211 syntax(*t_wp, "unexpected operator");
212
213 return res;
214}
215
216static void

--- 46 unchanged lines hidden (view full) ---

263primary(enum token n)
264{
265 enum token nn;
266 int res;
267
268 if (n == EOI)
269 return 0; /* missing expression */
270 if (n == LPAREN) {
219
220 if (--nargc > 0)
221 syntax(*t_wp, "unexpected operator");
222
223 return res;
224}
225
226static void

--- 46 unchanged lines hidden (view full) ---

273primary(enum token n)
274{
275 enum token nn;
276 int res;
277
278 if (n == EOI)
279 return 0; /* missing expression */
280 if (n == LPAREN) {
281 parenlevel++;
271 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
282 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
272 RPAREN)
283 RPAREN) {
284 parenlevel--;
273 return 0; /* missing expression */
285 return 0; /* missing expression */
286 }
274 res = oexpr(nn);
275 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
276 syntax(NULL, "closing paren expected");
287 res = oexpr(nn);
288 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
289 syntax(NULL, "closing paren expected");
290 parenlevel--;
277 return res;
278 }
279 if (t_wp_op && t_wp_op->op_type == UNOP) {
280 /* unary expression */
281 if (--nargc == 0)
282 syntax(t_wp_op->op_text, "argument expected");
283 switch (n) {
284 case STREZ:

--- 120 unchanged lines hidden (view full) ---

405 struct t_op const *op = ops;
406
407 if (s == 0) {
408 t_wp_op = NULL;
409 return EOI;
410 }
411 while (op->op_text) {
412 if (strcmp(s, op->op_text) == 0) {
291 return res;
292 }
293 if (t_wp_op && t_wp_op->op_type == UNOP) {
294 /* unary expression */
295 if (--nargc == 0)
296 syntax(t_wp_op->op_text, "argument expected");
297 switch (n) {
298 case STREZ:

--- 120 unchanged lines hidden (view full) ---

419 struct t_op const *op = ops;
420
421 if (s == 0) {
422 t_wp_op = NULL;
423 return EOI;
424 }
425 while (op->op_text) {
426 if (strcmp(s, op->op_text) == 0) {
413 if ((op->op_type == UNOP && isoperand()) ||
414 (op->op_num == LPAREN && nargc == 1))
427 if (((op->op_type == UNOP || op->op_type == BUNOP)
428 && isunopoperand()) ||
429 (op->op_num == LPAREN && islparenoperand()) ||
430 (op->op_num == RPAREN && isrparenoperand()))
415 break;
416 t_wp_op = op;
417 return op->op_num;
418 }
419 op++;
420 }
421 t_wp_op = NULL;
422 return OPERAND;
423}
424
425static int
431 break;
432 t_wp_op = op;
433 return op->op_num;
434 }
435 op++;
436 }
437 t_wp_op = NULL;
438 return OPERAND;
439}
440
441static int
426isoperand(void)
442isunopoperand(void)
427{
428 struct t_op const *op = ops;
429 char *s;
430 char *t;
431
432 if (nargc == 1)
433 return 1;
443{
444 struct t_op const *op = ops;
445 char *s;
446 char *t;
447
448 if (nargc == 1)
449 return 1;
434 if (nargc == 2)
435 return 0;
436 s = *(t_wp + 1);
450 s = *(t_wp + 1);
451 if (nargc == 2)
452 return parenlevel == 1 && strcmp(s, ")") == 0;
437 t = *(t_wp + 2);
438 while (op->op_text) {
439 if (strcmp(s, op->op_text) == 0)
440 return op->op_type == BINOP &&
453 t = *(t_wp + 2);
454 while (op->op_text) {
455 if (strcmp(s, op->op_text) == 0)
456 return op->op_type == BINOP &&
441 (t[0] != ')' || t[1] != '\0');
457 (parenlevel == 0 || t[0] != ')' || t[1] != '\0');
442 op++;
443 }
444 return 0;
445}
446
458 op++;
459 }
460 return 0;
461}
462
463static int
464islparenoperand(void)
465{
466 struct t_op const *op = ops;
467 char *s;
468
469 if (nargc == 1)
470 return 1;
471 s = *(t_wp + 1);
472 if (nargc == 2)
473 return parenlevel == 1 && strcmp(s, ")") == 0;
474 if (nargc != 3)
475 return 0;
476 while (op->op_text) {
477 if (strcmp(s, op->op_text) == 0)
478 return op->op_type == BINOP;
479 op++;
480 }
481 return 0;
482}
483
484static int
485isrparenoperand(void)
486{
487 char *s;
488
489 if (nargc == 1)
490 return 0;
491 s = *(t_wp + 1);
492 if (nargc == 2)
493 return parenlevel == 1 && strcmp(s, ")") == 0;
494 return 0;
495}
496
447/* atoi with error detection */
448static int
449getn(const char *s)
450{
451 char *p;
452 long r;
453
454 errno = 0;

--- 94 unchanged lines hidden ---
497/* atoi with error detection */
498static int
499getn(const char *s)
500{
501 char *p;
502 long r;
503
504 errno = 0;

--- 94 unchanged lines hidden ---