OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [sh/] [sh2.c] - Blame information for rev 199

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
#define Extern extern
2
#include <sys/types.h>
3
#include <signal.h>
4
#include <stdio.h>
5
 
6
#include <errno.h>
7
#include <setjmp.h>
8
#include "sh.h"
9
 
10
/* -------- csyn.c -------- */
11
/*
12
 * shell: syntax (C version)
13
 */
14
 
15
typedef union {
16
        char    *cp;
17
        char    **wp;
18
        int     i;
19
        struct  op *o;
20
} YYSTYPE;
21
#define WORD    256
22
#define LOGAND  257
23
#define LOGOR   258
24
#define BREAK   259
25
#define IF      260
26
#define THEN    261
27
#define ELSE    262
28
#define ELIF    263
29
#define FI      264
30
#define CASE    265
31
#define ESAC    266
32
#define FOR     267
33
#define WHILE   268
34
#define UNTIL   269
35
#define DO      270
36
#define DONE    271
37
#define IN      272
38
#define YYERRCODE 300
39
 
40
/* flags to yylex */
41
#define CONTIN  01      /* skip new lines to complete command */
42
 
43
/* #include "sh.h" */
44
#define SYNTAXERR       zzerr()
45
static  int     startl;
46
static  int     peeksym;
47
static  int     nlseen;
48
static  int     iounit = IODEFAULT;
49
 
50
static  YYSTYPE yylval;
51
 
52
static struct op *pipeline(int cf );
53
static struct op *andor(void);
54
static struct op *c_list(void);
55
static int synio(int cf );
56
static void musthave (int c, int cf );
57
static struct op *simple(void);
58
static struct op *nested(int type, int mark );
59
static struct op *command(int cf );
60
static struct op *dogroup(int onlydone );
61
static struct op *thenpart(void);
62
static struct op *elsepart(void);
63
static struct op *caselist(void);
64
static struct op *casepart(void);
65
static char **pattern(void);
66
static char **wordlist(void);
67
static struct op *list(struct op *t1, struct op *t2 );
68
static struct op *block(int type, struct op *t1, struct op *t2, char **wp );
69
static struct op *newtp(void);
70
static struct op *namelist(struct op *t );
71
static char **copyw(void);
72
static void word(char *cp );
73
static struct ioword **copyio(void);
74
static struct ioword *io (int u, int f, char *cp );
75
static void zzerr(void);
76
void yyerror(char *s );
77
static int yylex(int cf );
78
int collect(int c, int c1 );
79
int dual(int c );
80
static void diag(int ec );
81
static char *tree(unsigned size );
82
 
83
int
84
yyparse()
85
{
86
        startl  = 1;
87
        peeksym = 0;
88
        yynerrs = 0;
89
        outtree = c_list();
90
        musthave('\n', 0);
91
        return(yynerrs!=0);
92
}
93
 
94
static struct op *
95
pipeline(cf)
96
int cf;
97
{
98
        register struct op *t, *p;
99
        register int c;
100
 
101
        t = command(cf);
102
        if (t != NULL) {
103
                while ((c = yylex(0)) == '|') {
104
                        if ((p = command(CONTIN)) == NULL)
105
                                SYNTAXERR;
106
                        if (t->type != TPAREN && t->type != TCOM) {
107
                                /* shell statement */
108
                                t = block(TPAREN, t, NOBLOCK, NOWORDS);
109
                        }
110
                        t = block(TPIPE, t, p, NOWORDS);
111
                }
112
                peeksym = c;
113
        }
114
        return(t);
115
}
116
 
117
static struct op *
118
andor()
119
{
120
        register struct op *t, *p;
121
        register int c;
122
 
123
        t = pipeline(0);
124
        if (t != NULL) {
125
                while ((c = yylex(0)) == LOGAND || c == LOGOR) {
126
                        if ((p = pipeline(CONTIN)) == NULL)
127
                                SYNTAXERR;
128
                        t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS);
129
                }
130
                peeksym = c;
131
        }
132
        return(t);
133
}
134
 
135
static struct op *
136
c_list()
137
{
138
        register struct op *t, *p;
139
        register int c;
140
 
141
        t = andor();
142
        if (t != NULL) {
143
                if((peeksym = yylex(0)) == '&')
144
                        t = block(TASYNC, t, NOBLOCK, NOWORDS);
145
                while ((c = yylex(0)) == ';' || c == '&' || (multiline && c == '\n')) {
146
                        if ((p = andor()) == NULL)
147
                                return(t);
148
                        if((peeksym = yylex(0)) == '&')
149
                                p = block(TASYNC, p, NOBLOCK, NOWORDS);
150
                        t = list(t, p);
151
                }
152
                peeksym = c;
153
        }
154
        return(t);
155
}
156
 
157
 
158
static int
159
synio(cf)
160
int cf;
161
{
162
        register struct ioword *iop;
163
        register int i;
164
        register int c;
165
 
166
        if ((c = yylex(cf)) != '<' && c != '>') {
167
                peeksym = c;
168
                return(0);
169
        }
170
        i = yylval.i;
171
        musthave(WORD, 0);
172
        iop = io(iounit, i, yylval.cp);
173
        iounit = IODEFAULT;
174
        if (i & IOHERE)
175
                markhere(yylval.cp, iop);
176
        return(1);
177
}
178
 
179
static void
180
musthave(c, cf)
181
int c, cf;
182
{
183
        if ((peeksym = yylex(cf)) != c)
184
                SYNTAXERR;
185
        peeksym = 0;
186
}
187
 
188
static struct op *
189
simple()
190
{
191
        register struct op *t;
192
 
193
        t = NULL;
194
        for (;;) {
195
                switch (peeksym = yylex(0)) {
196
                case '<':
197
                case '>':
198
                        (void) synio(0);
199
                        break;
200
 
201
                case WORD:
202
                        if (t == NULL) {
203
                                t = newtp();
204
                                t->type = TCOM;
205
                        }
206
                        peeksym = 0;
207
                        word(yylval.cp);
208
                        break;
209
 
210
                default:
211
                        return(t);
212
                }
213
        }
214
}
215
 
216
static struct op *
217
nested(type, mark)
218
int type, mark;
219
{
220
        register struct op *t;
221
 
222
        multiline++;
223
        t = c_list();
224
        musthave(mark, 0);
225
        multiline--;
226
        return(block(type, t, NOBLOCK, NOWORDS));
227
}
228
 
229
static struct op *
230
command(cf)
231
int cf;
232
{
233
        register struct op *t;
234
        struct wdblock *iosave;
235
        register int c;
236
 
237
        iosave = iolist;
238
        iolist = NULL;
239
        if (multiline)
240
                cf |= CONTIN;
241
        while (synio(cf))
242
                cf = 0;
243
        switch (c = yylex(cf)) {
244
        default:
245
                peeksym = c;
246
                if ((t = simple()) == NULL) {
247
                        if (iolist == NULL)
248
                                return((struct op *)NULL);
249
                        t = newtp();
250
                        t->type = TCOM;
251
                }
252
                break;
253
 
254
        case '(':
255
                t = nested(TPAREN, ')');
256
                break;
257
 
258
        case '{':
259
                t = nested(TBRACE, '}');
260
                break;
261
 
262
        case FOR:
263
                t = newtp();
264
                t->type = TFOR;
265
                musthave(WORD, 0);
266
                startl = 1;
267
                t->str = yylval.cp;
268
                multiline++;
269
                t->words = wordlist();
270
                if ((c = yylex(0)) != '\n' && c != ';')
271
                        peeksym = c;
272
                t->left = dogroup(0);
273
                multiline--;
274
                break;
275
 
276
        case WHILE:
277
        case UNTIL:
278
                multiline++;
279
                t = newtp();
280
                t->type = c == WHILE? TWHILE: TUNTIL;
281
                t->left = c_list();
282
                t->right = dogroup(1);
283
                t->words = NULL;
284
                multiline--;
285
                break;
286
 
287
        case CASE:
288
                t = newtp();
289
                t->type = TCASE;
290
                musthave(WORD, 0);
291
                t->str = yylval.cp;
292
                startl++;
293
                multiline++;
294
                musthave(IN, CONTIN);
295
                startl++;
296
                t->left = caselist();
297
                musthave(ESAC, 0);
298
                multiline--;
299
                break;
300
 
301
        case IF:
302
                multiline++;
303
                t = newtp();
304
                t->type = TIF;
305
                t->left = c_list();
306
                t->right = thenpart();
307
                musthave(FI, 0);
308
                multiline--;
309
                break;
310
        }
311
        while (synio(0))
312
                ;
313
        t = namelist(t);
314
        iolist = iosave;
315
        return(t);
316
}
317
 
318
static struct op *
319
dogroup(onlydone)
320
int onlydone;
321
{
322
        register int c;
323
        register struct op *list;
324
 
325
        c = yylex(CONTIN);
326
        if (c == DONE && onlydone)
327
                return((struct op *)NULL);
328
        if (c != DO)
329
                SYNTAXERR;
330
        list = c_list();
331
        musthave(DONE, 0);
332
        return(list);
333
}
334
 
335
static struct op *
336
thenpart()
337
{
338
        register int c;
339
        register struct op *t;
340
 
341
        if ((c = yylex(0)) != THEN) {
342
                peeksym = c;
343
                return((struct op *)NULL);
344
        }
345
        t = newtp();
346
        t->type = 0;
347
        t->left = c_list();
348
        if (t->left == NULL)
349
                SYNTAXERR;
350
        t->right = elsepart();
351
        return(t);
352
}
353
 
354
static struct op *
355
elsepart()
356
{
357
        register int c;
358
        register struct op *t;
359
 
360
        switch (c = yylex(0)) {
361
        case ELSE:
362
                if ((t = c_list()) == NULL)
363
                        SYNTAXERR;
364
                return(t);
365
 
366
        case ELIF:
367
                t = newtp();
368
                t->type = TELIF;
369
                t->left = c_list();
370
                t->right = thenpart();
371
                return(t);
372
 
373
        default:
374
                peeksym = c;
375
                return((struct op *)NULL);
376
        }
377
}
378
 
379
static struct op *
380
caselist()
381
{
382
        register struct op *t;
383
 
384
        t = NULL;
385
        while ((peeksym = yylex(CONTIN)) != ESAC)
386
                t = list(t, casepart());
387
        return(t);
388
}
389
 
390
static struct op *
391
casepart()
392
{
393
        register struct op *t;
394
 
395
        t = newtp();
396
        t->type = TPAT;
397
        t->words = pattern();
398
        musthave(')', 0);
399
        t->left = c_list();
400
        if ((peeksym = yylex(CONTIN)) != ESAC)
401
                musthave(BREAK, CONTIN);
402
        return(t);
403
}
404
 
405
static char **
406
pattern()
407
{
408
        register int c, cf;
409
 
410
        cf = CONTIN;
411
        do {
412
                musthave(WORD, cf);
413
                word(yylval.cp);
414
                cf = 0;
415
        } while ((c = yylex(0)) == '|');
416
        peeksym = c;
417
        word(NOWORD);
418
        return(copyw());
419
}
420
 
421
static char **
422
wordlist()
423
{
424
        register int c;
425
 
426
        if ((c = yylex(0)) != IN) {
427
                peeksym = c;
428
                return((char **)NULL);
429
        }
430
        startl = 0;
431
        while ((c = yylex(0)) == WORD)
432
                word(yylval.cp);
433
        word(NOWORD);
434
        peeksym = c;
435
        return(copyw());
436
}
437
 
438
/*
439
 * supporting functions
440
 */
441
static struct op *
442
list(t1, t2)
443
register struct op *t1, *t2;
444
{
445
        if (t1 == NULL)
446
                return(t2);
447
        if (t2 == NULL)
448
                return(t1);
449
        return(block(TLIST, t1, t2, NOWORDS));
450
}
451
 
452
static struct op *
453
block(type, t1, t2, wp)
454
int type;
455
struct op *t1, *t2;
456
char **wp;
457
{
458
        register struct op *t;
459
 
460
        t = newtp();
461
        t->type = type;
462
        t->left = t1;
463
        t->right = t2;
464
        t->words = wp;
465
        return(t);
466
}
467
 
468
struct res {
469
        char    *r_name;
470
        int     r_val;
471
} restab[] = {
472
    {"for",             FOR},
473
    {"case",            CASE},
474
    {"esac",            ESAC},
475
    {"while",   WHILE},
476
    {"do",              DO},
477
    {"done",            DONE},
478
    {"if",              IF},
479
    {"in",              IN},
480
    {"then",            THEN},
481
    {"else",            ELSE},
482
    {"elif",            ELIF},
483
    {"until",   UNTIL},
484
    {"fi",              FI},
485
 
486
    {";;",              BREAK},
487
    {"||",              LOGOR},
488
    {"&&",              LOGAND},
489
    {"{",               '{'},
490
    {"}",               '}'},
491
    {0,          0},
492
};
493
 
494
int
495
rlookup(n)
496
register char *n;
497
{
498
        register struct res *rp;
499
 
500
        for (rp = restab; rp->r_name; rp++)
501
                if (strcmp(rp->r_name, n) == 0)
502
                        return(rp->r_val);
503
        return(0);
504
}
505
 
506
static struct op *
507
newtp()
508
{
509
        register struct op *t;
510
 
511
        t = (struct op *)tree(sizeof(*t));
512
        t->type = 0;
513
        t->words = NULL;
514
        t->ioact = NULL;
515
        t->left = NULL;
516
        t->right = NULL;
517
        t->str = NULL;
518
        return(t);
519
}
520
 
521
static struct op *
522
namelist(t)
523
register struct op *t;
524
{
525
        if (iolist) {
526
                iolist = addword((char *)NULL, iolist);
527
                t->ioact = copyio();
528
        } else
529
                t->ioact = NULL;
530
        if (t->type != TCOM) {
531
                if (t->type != TPAREN && t->ioact != NULL) {
532
                        t = block(TPAREN, t, NOBLOCK, NOWORDS);
533
                        t->ioact = t->left->ioact;
534
                        t->left->ioact = NULL;
535
                }
536
                return(t);
537
        }
538
        word(NOWORD);
539
        t->words = copyw();
540
        return(t);
541
}
542
 
543
static char **
544
copyw()
545
{
546
        register char **wd;
547
 
548
        wd = getwords(wdlist);
549
        wdlist = 0;
550
        return(wd);
551
}
552
 
553
static void
554
word(cp)
555
char *cp;
556
{
557
        wdlist = addword(cp, wdlist);
558
}
559
 
560
static struct ioword **
561
copyio()
562
{
563
        register struct ioword **iop;
564
 
565
        iop = (struct ioword **) getwords(iolist);
566
        iolist = 0;
567
        return(iop);
568
}
569
 
570
static struct ioword *
571
io(u, f, cp)
572
int u;
573
int f;
574
char *cp;
575
{
576
        register struct ioword *iop;
577
 
578
        iop = (struct ioword *) tree(sizeof(*iop));
579
        iop->io_unit = u;
580
        iop->io_flag = f;
581
        iop->io_name = cp;
582
        iolist = addword((char *)iop, iolist);
583
        return(iop);
584
}
585
 
586
static void
587
zzerr()
588
{
589
        yyerror("syntax error");
590
}
591
 
592
void
593
yyerror(s)
594
char *s;
595
{
596
        yynerrs++;
597
        if (talking && e.iop <= iostack) {
598
                multiline = 0;
599
                while (eofc() == 0 && yylex(0) != '\n')
600
                        ;
601
        }
602
        err(s);
603
        fail();
604
}
605
 
606
static int
607
yylex(cf)
608
int cf;
609
{
610
        register int c, c1;
611
        int atstart;
612
 
613
        if ((c = peeksym) > 0) {
614
                peeksym = 0;
615
                if (c == '\n')
616
                        startl = 1;
617
                return(c);
618
        }
619
        nlseen = 0;
620
        e.linep = line;
621
        atstart = startl;
622
        startl = 0;
623
        yylval.i = 0;
624
 
625
loop:
626
        while ((c = my_getc(0)) == ' ' || c == '\t')
627
                ;
628
        switch (c) {
629
        default:
630
                if (any(c, "0123456789")) {
631
                        unget(c1 = my_getc(0));
632
                        if (c1 == '<' || c1 == '>') {
633
                                iounit = c - '0';
634
                                goto loop;
635
                        }
636
                        *e.linep++ = c;
637
                        c = c1;
638
                }
639
                break;
640
 
641
        case '#':
642
                while ((c = my_getc(0)) != 0 && c != '\n')
643
                        ;
644
                unget(c);
645
                goto loop;
646
 
647
        case 0:
648
                return(c);
649
 
650
        case '$':
651
                *e.linep++ = c;
652
                if ((c = my_getc(0)) == '{') {
653
                        if ((c = collect(c, '}')) != '\0')
654
                                return(c);
655
                        goto pack;
656
                }
657
                break;
658
 
659
        case '`':
660
        case '\'':
661
        case '"':
662
                if ((c = collect(c, c)) != '\0')
663
                        return(c);
664
                goto pack;
665
 
666
        case '|':
667
        case '&':
668
        case ';':
669
                if ((c1 = dual(c)) != '\0') {
670
                        startl = 1;
671
                        return(c1);
672
                }
673
                startl = 1;
674
                return(c);
675
        case '^':
676
                startl = 1;
677
                return('|');
678
        case '>':
679
        case '<':
680
                diag(c);
681
                return(c);
682
 
683
        case '\n':
684
                nlseen++;
685
                gethere();
686
                startl = 1;
687
                if (multiline || cf & CONTIN) {
688
                        if (talking && e.iop <= iostack)
689
                                prs(cprompt->value);
690
                        if (cf & CONTIN)
691
                                goto loop;
692
                }
693
                return(c);
694
 
695
        case '(':
696
        case ')':
697
                startl = 1;
698
                return(c);
699
        }
700
 
701
        unget(c);
702
 
703
pack:
704
        while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n"))
705
                if (e.linep >= elinep)
706
                        err("word too long");
707
                else
708
                        *e.linep++ = c;
709
        unget(c);
710
        if(any(c, "\"'`$"))
711
                goto loop;
712
        *e.linep++ = '\0';
713
        if (atstart && (c = rlookup(line))!=0) {
714
                startl = 1;
715
                return(c);
716
        }
717
        yylval.cp = strsave(line, areanum);
718
        return(WORD);
719
}
720
 
721
int
722
collect(c, c1)
723
register int c, c1;
724
{
725
        char s[2];
726
 
727
        *e.linep++ = c;
728
        while ((c = my_getc(c1)) != c1) {
729
                if (c == 0) {
730
                        unget(c);
731
                        s[0] = c1;
732
                        s[1] = 0;
733
                        prs("no closing "); yyerror(s);
734
                        return(YYERRCODE);
735
                }
736
                if (talking && c == '\n' && e.iop <= iostack)
737
                        prs(cprompt->value);
738
                *e.linep++ = c;
739
        }
740
        *e.linep++ = c;
741
        return(0);
742
}
743
 
744
int
745
dual(c)
746
register int c;
747
{
748
        char s[3];
749
        register char *cp = s;
750
 
751
        *cp++ = c;
752
        *cp++ = my_getc(0);
753
        *cp = 0;
754
        if ((c = rlookup(s)) == 0)
755
                unget(*--cp);
756
        return(c);
757
}
758
 
759
static void
760
diag(ec)
761
register int ec;
762
{
763
        register int c;
764
 
765
        c = my_getc(0);
766
        if (c == '>' || c == '<') {
767
                if (c != ec)
768
                        zzerr();
769
                yylval.i = ec == '>'? IOWRITE|IOCAT: IOHERE;
770
                c = my_getc(0);
771
        } else
772
                yylval.i = ec == '>'? IOWRITE: IOREAD;
773
        if (c != '&' || yylval.i == IOHERE)
774
                unget(c);
775
        else
776
                yylval.i |= IODUP;
777
}
778
 
779
static char *
780
tree(size)
781
unsigned size;
782
{
783
        register char *t;
784
 
785
        if ((t = getcell(size)) == NULL) {
786
                prs("command line too complicated\n");
787
                fail();
788
                /* NOTREACHED */
789
        }
790
        return(t);
791
}
792
 
793
/* VARARGS1 */
794
/* ARGSUSED */
795
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.