Take -lgnuregex back out. Linking with it causes 'make install' to fail.
[unix-history] / bin / csh / exp.c
CommitLineData
15637ed4
RG
1/*-
2 * Copyright (c) 1980, 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)exp.c 5.11 (Berkeley) 6/8/91";
36#endif /* not lint */
37
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <stdlib.h>
41#include <unistd.h>
42#if __STDC__
43# include <stdarg.h>
44#else
45# include <varargs.h>
46#endif
47
48#include "csh.h"
49#include "extern.h"
50
51#define IGNORE 1 /* in ignore, it means to ignore value, just parse */
52#define NOGLOB 2 /* in ignore, it means not to globone */
53
54#define ADDOP 1
55#define MULOP 2
56#define EQOP 4
57#define RELOP 8
58#define RESTOP 16
59#define ANYOP 31
60
61#define EQEQ 1
62#define GTR 2
63#define LSS 4
64#define NOTEQ 6
65#define EQMATCH 7
66#define NOTEQMATCH 8
67
68static int exp1 __P((Char ***, bool));
69static int exp2 __P((Char ***, bool));
70static int exp2a __P((Char ***, bool));
71static int exp2b __P((Char ***, bool));
72static int exp2c __P((Char ***, bool));
73static Char * exp3 __P((Char ***, bool));
74static Char * exp3a __P((Char ***, bool));
75static Char * exp4 __P((Char ***, bool));
76static Char * exp5 __P((Char ***, bool));
77static Char * exp6 __P((Char ***, bool));
78static void evalav __P((Char **));
79static int isa __P((Char *, int));
80static int egetn __P((Char *));
81
82#ifdef EDEBUG
83static void etracc __P((char *, Char *, Char ***));
84static void etraci __P((char *, int, Char ***));
85#endif
86
87int
88exp(vp)
89 register Char ***vp;
90{
91 return (exp0(vp, 0));
92}
93
94int
95exp0(vp, ignore)
96 register Char ***vp;
97 bool ignore;
98{
99 register int p1 = exp1(vp, ignore);
100
101#ifdef EDEBUG
102 etraci("exp0 p1", p1, vp);
103#endif
104 if (**vp && eq(**vp, STRor2)) {
105 register int p2;
106
107 (*vp)++;
108 p2 = exp0(vp, (ignore & IGNORE) || p1);
109#ifdef EDEBUG
110 etraci("exp0 p2", p2, vp);
111#endif
112 return (p1 || p2);
113 }
114 return (p1);
115}
116
117static int
118exp1(vp, ignore)
119 register Char ***vp;
120 bool ignore;
121{
122 register int p1 = exp2(vp, ignore);
123
124#ifdef EDEBUG
125 etraci("exp1 p1", p1, vp);
126#endif
127 if (**vp && eq(**vp, STRand2)) {
128 register int p2;
129
130 (*vp)++;
131 p2 = exp1(vp, (ignore & IGNORE) || !p1);
132#ifdef EDEBUG
133 etraci("exp1 p2", p2, vp);
134#endif
135 return (p1 && p2);
136 }
137 return (p1);
138}
139
140static int
141exp2(vp, ignore)
142 register Char ***vp;
143 bool ignore;
144{
145 register int p1 = exp2a(vp, ignore);
146
147#ifdef EDEBUG
148 etraci("exp3 p1", p1, vp);
149#endif
150 if (**vp && eq(**vp, STRor)) {
151 register int p2;
152
153 (*vp)++;
154 p2 = exp2(vp, ignore);
155#ifdef EDEBUG
156 etraci("exp3 p2", p2, vp);
157#endif
158 return (p1 | p2);
159 }
160 return (p1);
161}
162
163static int
164exp2a(vp, ignore)
165 register Char ***vp;
166 bool ignore;
167{
168 register int p1 = exp2b(vp, ignore);
169
170#ifdef EDEBUG
171 etraci("exp2a p1", p1, vp);
172#endif
173 if (**vp && eq(**vp, STRcaret)) {
174 register int p2;
175
176 (*vp)++;
177 p2 = exp2a(vp, ignore);
178#ifdef EDEBUG
179 etraci("exp2a p2", p2, vp);
180#endif
181 return (p1 ^ p2);
182 }
183 return (p1);
184}
185
186static int
187exp2b(vp, ignore)
188 register Char ***vp;
189 bool ignore;
190{
191 register int p1 = exp2c(vp, ignore);
192
193#ifdef EDEBUG
194 etraci("exp2b p1", p1, vp);
195#endif
196 if (**vp && eq(**vp, STRand)) {
197 register int p2;
198
199 (*vp)++;
200 p2 = exp2b(vp, ignore);
201#ifdef EDEBUG
202 etraci("exp2b p2", p2, vp);
203#endif
204 return (p1 & p2);
205 }
206 return (p1);
207}
208
209static int
210exp2c(vp, ignore)
211 register Char ***vp;
212 bool ignore;
213{
214 register Char *p1 = exp3(vp, ignore);
215 register Char *p2;
216 register int i;
217
218#ifdef EDEBUG
219 etracc("exp2c p1", p1, vp);
220#endif
221 if (i = isa(**vp, EQOP)) {
222 (*vp)++;
223 if (i == EQMATCH || i == NOTEQMATCH)
224 ignore |= NOGLOB;
225 p2 = exp3(vp, ignore);
226#ifdef EDEBUG
227 etracc("exp2c p2", p2, vp);
228#endif
229 if (!(ignore & IGNORE))
230 switch (i) {
231
232 case EQEQ:
233 i = eq(p1, p2);
234 break;
235
236 case NOTEQ:
237 i = !eq(p1, p2);
238 break;
239
240 case EQMATCH:
241 i = Gmatch(p1, p2);
242 break;
243
244 case NOTEQMATCH:
245 i = !Gmatch(p1, p2);
246 break;
247 }
248 xfree((ptr_t) p1);
249 xfree((ptr_t) p2);
250 return (i);
251 }
252 i = egetn(p1);
253 xfree((ptr_t) p1);
254 return (i);
255}
256
257static Char *
258exp3(vp, ignore)
259 register Char ***vp;
260 bool ignore;
261{
262 register Char *p1, *p2;
263 register int i;
264
265 p1 = exp3a(vp, ignore);
266#ifdef EDEBUG
267 etracc("exp3 p1", p1, vp);
268#endif
269 if (i = isa(**vp, RELOP)) {
270 (*vp)++;
271 if (**vp && eq(**vp, STRequal))
272 i |= 1, (*vp)++;
273 p2 = exp3(vp, ignore);
274#ifdef EDEBUG
275 etracc("exp3 p2", p2, vp);
276#endif
277 if (!(ignore & IGNORE))
278 switch (i) {
279
280 case GTR:
281 i = egetn(p1) > egetn(p2);
282 break;
283
284 case GTR | 1:
285 i = egetn(p1) >= egetn(p2);
286 break;
287
288 case LSS:
289 i = egetn(p1) < egetn(p2);
290 break;
291
292 case LSS | 1:
293 i = egetn(p1) <= egetn(p2);
294 break;
295 }
296 xfree((ptr_t) p1);
297 xfree((ptr_t) p2);
298 return (putn(i));
299 }
300 return (p1);
301}
302
303static Char *
304exp3a(vp, ignore)
305 register Char ***vp;
306 bool ignore;
307{
308 register Char *p1, *p2, *op;
309 register int i;
310
311 p1 = exp4(vp, ignore);
312#ifdef EDEBUG
313 etracc("exp3a p1", p1, vp);
314#endif
315 op = **vp;
316 if (op && any("<>", op[0]) && op[0] == op[1]) {
317 (*vp)++;
318 p2 = exp3a(vp, ignore);
319#ifdef EDEBUG
320 etracc("exp3a p2", p2, vp);
321#endif
322 if (op[0] == '<')
323 i = egetn(p1) << egetn(p2);
324 else
325 i = egetn(p1) >> egetn(p2);
326 xfree((ptr_t) p1);
327 xfree((ptr_t) p2);
328 return (putn(i));
329 }
330 return (p1);
331}
332
333static Char *
334exp4(vp, ignore)
335 register Char ***vp;
336 bool ignore;
337{
338 register Char *p1, *p2;
339 register int i = 0;
340
341 p1 = exp5(vp, ignore);
342#ifdef EDEBUG
343 etracc("exp4 p1", p1, vp);
344#endif
345 if (isa(**vp, ADDOP)) {
346 register Char *op = *(*vp)++;
347
348 p2 = exp4(vp, ignore);
349#ifdef EDEBUG
350 etracc("exp4 p2", p2, vp);
351#endif
352 if (!(ignore & IGNORE))
353 switch (op[0]) {
354
355 case '+':
356 i = egetn(p1) + egetn(p2);
357 break;
358
359 case '-':
360 i = egetn(p1) - egetn(p2);
361 break;
362 }
363 xfree((ptr_t) p1);
364 xfree((ptr_t) p2);
365 return (putn(i));
366 }
367 return (p1);
368}
369
370static Char *
371exp5(vp, ignore)
372 register Char ***vp;
373 bool ignore;
374{
375 register Char *p1, *p2;
376 register int i = 0;
377
378 p1 = exp6(vp, ignore);
379#ifdef EDEBUG
380 etracc("exp5 p1", p1, vp);
381#endif
382 if (isa(**vp, MULOP)) {
383 register Char *op = *(*vp)++;
384
385 p2 = exp5(vp, ignore);
386#ifdef EDEBUG
387 etracc("exp5 p2", p2, vp);
388#endif
389 if (!(ignore & IGNORE))
390 switch (op[0]) {
391
392 case '*':
393 i = egetn(p1) * egetn(p2);
394 break;
395
396 case '/':
397 i = egetn(p2);
398 if (i == 0)
399 stderror(ERR_DIV0);
400 i = egetn(p1) / i;
401 break;
402
403 case '%':
404 i = egetn(p2);
405 if (i == 0)
406 stderror(ERR_MOD0);
407 i = egetn(p1) % i;
408 break;
409 }
410 xfree((ptr_t) p1);
411 xfree((ptr_t) p2);
412 return (putn(i));
413 }
414 return (p1);
415}
416
417static Char *
418exp6(vp, ignore)
419 register Char ***vp;
420 bool ignore;
421{
422 int ccode, i = 0;
423 register Char *cp, *dp, *ep;
424
425 if (**vp == 0)
426 stderror(ERR_NAME | ERR_EXPRESSION);
427 if (eq(**vp, STRbang)) {
428 (*vp)++;
429 cp = exp6(vp, ignore);
430#ifdef EDEBUG
431 etracc("exp6 ! cp", cp, vp);
432#endif
433 i = egetn(cp);
434 xfree((ptr_t) cp);
435 return (putn(!i));
436 }
437 if (eq(**vp, STRtilde)) {
438 (*vp)++;
439 cp = exp6(vp, ignore);
440#ifdef EDEBUG
441 etracc("exp6 ~ cp", cp, vp);
442#endif
443 i = egetn(cp);
444 xfree((ptr_t) cp);
445 return (putn(~i));
446 }
447 if (eq(**vp, STRLparen)) {
448 (*vp)++;
449 ccode = exp0(vp, ignore);
450#ifdef EDEBUG
451 etraci("exp6 () ccode", ccode, vp);
452#endif
453 if (*vp == 0 || **vp == 0 || ***vp != ')')
454 stderror(ERR_NAME | ERR_EXPRESSION);
455 (*vp)++;
456 return (putn(ccode));
457 }
458 if (eq(**vp, STRLbrace)) {
459 register Char **v;
460 struct command faket;
461 Char *fakecom[2];
462
463 faket.t_dtyp = NODE_COMMAND;
464 faket.t_dflg = 0;
465 faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
466 faket.t_dcom = fakecom;
467 fakecom[0] = STRfakecom;
468 fakecom[1] = NULL;
469 (*vp)++;
470 v = *vp;
471 for (;;) {
472 if (!**vp)
473 stderror(ERR_NAME | ERR_MISSING, '}');
474 if (eq(*(*vp)++, STRRbrace))
475 break;
476 }
477 if (ignore & IGNORE)
478 return (Strsave(STRNULL));
479 psavejob();
480 if (pfork(&faket, -1) == 0) {
481 *--(*vp) = 0;
482 evalav(v);
483 exitstat();
484 }
485 pwait();
486 prestjob();
487#ifdef EDEBUG
488 etraci("exp6 {} status", egetn(value(STRstatus)), vp);
489#endif
490 return (putn(egetn(value(STRstatus)) == 0));
491 }
492 if (isa(**vp, ANYOP))
493 return (Strsave(STRNULL));
494 cp = *(*vp)++;
495 if (*cp == '-' && any("erwxfdzopls", cp[1])) {
496 struct stat stb;
497
498 if (cp[2] != '\0')
499 stderror(ERR_NAME | ERR_FILEINQ);
500 /*
501 * Detect missing file names by checking for operator in the file name
502 * position. However, if an operator name appears there, we must make
503 * sure that there's no file by that name (e.g., "/") before announcing
504 * an error. Even this check isn't quite right, since it doesn't take
505 * globbing into account.
506 */
507 if (isa(**vp, ANYOP) && stat(short2str(**vp), &stb))
508 stderror(ERR_NAME | ERR_FILENAME);
509
510 dp = *(*vp)++;
511 if (ignore & IGNORE)
512 return (Strsave(STRNULL));
513 ep = globone(dp, G_ERROR);
514 switch (cp[1]) {
515
516 case 'r':
517 i = !access(short2str(ep), R_OK);
518 break;
519
520 case 'w':
521 i = !access(short2str(ep), W_OK);
522 break;
523
524 case 'x':
525 i = !access(short2str(ep), X_OK);
526 break;
527
528 default:
529 if (
530#ifdef S_IFLNK
531 cp[1] == 'l' ? lstat(short2str(ep), &stb) :
532#endif
533 stat(short2str(ep), &stb)) {
534 xfree((ptr_t) ep);
535 return (Strsave(STR0));
536 }
537 switch (cp[1]) {
538
539 case 'f':
540 i = S_ISREG(stb.st_mode);
541 break;
542
543 case 'd':
544 i = S_ISDIR(stb.st_mode);
545 break;
546
547 case 'p':
548#ifdef S_ISFIFO
549 i = S_ISFIFO(stb.st_mode);
550#else
551 i = 0;
552#endif
553 break;
554
555 case 'l':
556#ifdef S_ISLNK
557 i = S_ISLNK(stb.st_mode);
558#else
559 i = 0;
560#endif
561 break;
562
563 case 's':
564#ifdef S_ISSOCK
565 i = S_ISSOCK(stb.st_mode);
566#else
567 i = 0;
568#endif
569 break;
570
571 case 'z':
572 i = stb.st_size == 0;
573 break;
574
575 case 'e':
576 i = 1;
577 break;
578
579 case 'o':
580 i = stb.st_uid == uid;
581 break;
582 }
583 }
584#ifdef EDEBUG
585 etraci("exp6 -? i", i, vp);
586#endif
587 xfree((ptr_t) ep);
588 return (putn(i));
589 }
590#ifdef EDEBUG
591 etracc("exp6 default", cp, vp);
592#endif
593 return (ignore & NOGLOB ? Strsave(cp) : globone(cp, G_ERROR));
594}
595
596static void
597evalav(v)
598 register Char **v;
599{
600 struct wordent paraml1;
601 register struct wordent *hp = &paraml1;
602 struct command *t;
603 register struct wordent *wdp = hp;
604
605 set(STRstatus, Strsave(STR0));
606 hp->prev = hp->next = hp;
607 hp->word = STRNULL;
608 while (*v) {
609 register struct wordent *new =
610 (struct wordent *) xcalloc(1, sizeof *wdp);
611
612 new->prev = wdp;
613 new->next = hp;
614 wdp->next = new;
615 wdp = new;
616 wdp->word = Strsave(*v++);
617 }
618 hp->prev = wdp;
619 alias(&paraml1);
620 t = syntax(paraml1.next, &paraml1, 0);
621 if (seterr)
622 stderror(ERR_OLD);
623 execute(t, -1, NULL, NULL);
624 freelex(&paraml1), freesyn(t);
625}
626
627static int
628isa(cp, what)
629 register Char *cp;
630 register int what;
631{
632 if (cp == 0)
633 return ((what & RESTOP) != 0);
634 if (cp[1] == 0) {
635 if (what & ADDOP && (*cp == '+' || *cp == '-'))
636 return (1);
637 if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
638 return (1);
639 if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
640 *cp == '~' || *cp == '^' || *cp == '"'))
641 return (1);
642 }
643 else if (cp[2] == 0) {
644 if (what & RESTOP) {
645 if (cp[0] == '|' && cp[1] == '&')
646 return (1);
647 if (cp[0] == '<' && cp[1] == '<')
648 return (1);
649 if (cp[0] == '>' && cp[1] == '>')
650 return (1);
651 }
652 if (what & EQOP) {
653 if (cp[0] == '=') {
654 if (cp[1] == '=')
655 return (EQEQ);
656 if (cp[1] == '~')
657 return (EQMATCH);
658 }
659 else if (cp[0] == '!') {
660 if (cp[1] == '=')
661 return (NOTEQ);
662 if (cp[1] == '~')
663 return (NOTEQMATCH);
664 }
665 }
666 }
667 if (what & RELOP) {
668 if (*cp == '<')
669 return (LSS);
670 if (*cp == '>')
671 return (GTR);
672 }
673 return (0);
674}
675
676static int
677egetn(cp)
678 register Char *cp;
679{
680 if (*cp && *cp != '-' && !Isdigit(*cp))
681 stderror(ERR_NAME | ERR_EXPRESSION);
682 return (getn(cp));
683}
684
685/* Phew! */
686
687#ifdef EDEBUG
688static void
689etraci(str, i, vp)
690 char *str;
691 int i;
692 Char ***vp;
693{
694 xprintf("%s=%d\t", str, i);
695 blkpr(*vp);
696 xprintf("\n");
697}
698static void
699etracc(str, cp, vp)
700 char *str;
701 Char *cp;
702 Char ***vp;
703{
704 xprintf("%s=%s\t", str, cp);
705 blkpr(*vp);
706 xprintf("\n");
707}
708#endif