forked from freebsd/freebsd-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsh.lex.c
1849 lines (1706 loc) · 37.3 KB
/
sh.lex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Header: /p/tcsh/cvsroot/tcsh/sh.lex.c,v 3.91 2016/08/01 16:21:09 christos Exp $ */
/*
* sh.lex.c: Lexical analysis into tokens
*/
/*-
* Copyright (c) 1980, 1991 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "sh.h"
RCSID("$tcsh: sh.lex.c,v 3.91 2016/08/01 16:21:09 christos Exp $")
#include "ed.h"
#include <assert.h>
/* #define DEBUG_INP */
/* #define DEBUG_SEEK */
/*
* C shell
*/
#define FLAG_G 1
#define FLAG_A 2
/*
* These lexical routines read input and form lists of words.
* There is some involved processing here, because of the complications
* of input buffering, and especially because of history substitution.
*/
static Char *word (int);
static eChar getC1 (int);
static void getdol (void);
static void getexcl (Char);
static struct Hist *findev (Char *, int);
static void setexclp (Char *);
static eChar bgetc (void);
static void balloc (int);
static void bfree (void);
static struct wordent *gethent (Char);
static int matchs (const Char *, const Char *);
static int getsel (int *, int *, int);
static struct wordent *getsub (struct wordent *);
static Char *subword (Char *, Char, int *, size_t *);
static struct wordent *dosub (Char, struct wordent *, int);
/*
* Peekc is a peek character for getC, peekread for readc.
* There is a subtlety here in many places... history routines
* will read ahead and then insert stuff into the input stream.
* If they push back a character then they must push it behind
* the text substituted by the history substitution. On the other
* hand in several places we need 2 peek characters. To make this
* all work, the history routines read with getC, and make use both
* of ungetC and unreadc. The key observation is that the state
* of getC at the call of a history reference is such that calls
* to getC from the history routines will always yield calls of
* readc, unless this peeking is involved. That is to say that during
* getexcl the variables lap, exclp, and exclnxt are all zero.
*
* Getdol invokes history substitution, hence the extra peek, peekd,
* which it can ungetD to be before history substitutions.
*/
static Char peekc = 0, peekd = 0;
static Char peekread = 0;
/* (Tail of) current word from ! subst */
static Char *exclp = NULL;
/* The rest of the ! subst words */
static struct wordent *exclnxt = NULL;
/* Count of remaining words in ! subst */
static int exclc = 0;
/* "Globp" for alias resubstitution */
int aret = TCSH_F_SEEK;
/*
* Labuf implements a general buffer for lookahead during lexical operations.
* Text which is to be placed in the input stream can be stuck here.
* We stick parsed ahead $ constructs during initial input,
* process id's from `$$', and modified variable values (from qualifiers
* during expansion in sh.dol.c) here.
*/
struct Strbuf labuf; /* = Strbuf_INIT; */
/*
* Lex returns to its caller not only a wordlist (as a "var" parameter)
* but also whether a history substitution occurred. This is used in
* the main (process) routine to determine whether to echo, and also
* when called by the alias routine to determine whether to keep the
* argument list.
*/
static int hadhist = 0;
/*
* Avoid alias expansion recursion via \!#
*/
int hleft;
struct Strbuf histline; /* = Strbuf_INIT; last line input */
int histvalid = 0; /* is histline valid */
static Char getCtmp;
#define getC(f) (((getCtmp = peekc) != '\0') ? (peekc = 0, (eChar)getCtmp) : getC1(f))
#define ungetC(c) peekc = (Char) c
#define ungetD(c) peekd = (Char) c
/* Use Htime to store timestamps picked up from history file for enthist()
* if reading saved history (sg)
*/
time_t Htime = (time_t)0;
static time_t a2time_t (Char *);
/*
* special parsing rules apply for source -h
*/
extern int enterhist;
int
lex(struct wordent *hp)
{
struct wordent *wdp;
eChar c;
int parsehtime = enterhist;
histvalid = 0;
histline.len = 0;
btell(&lineloc);
hp->next = hp->prev = hp;
hp->word = STRNULL;
hadhist = 0;
do
c = readc(0);
while (c == ' ' || c == '\t');
if (c == (eChar)HISTSUB && intty)
/* ^lef^rit from tty is short !:s^lef^rit */
getexcl(c);
else
unreadc(c);
cleanup_push(hp, lex_cleanup);
wdp = hp;
/*
* The following loop is written so that the links needed by freelex will
* be ready and rarin to go even if it is interrupted.
*/
do {
struct wordent *new;
new = xmalloc(sizeof(*new));
new->word = NULL;
new->prev = wdp;
new->next = hp;
wdp->next = new;
hp->prev = new;
wdp = new;
wdp->word = word(parsehtime);
parsehtime = 0;
} while (wdp->word[0] != '\n');
cleanup_ignore(hp);
cleanup_until(hp);
Strbuf_terminate(&histline);
if (histline.len != 0 && histline.s[histline.len - 1] == '\n')
histline.s[histline.len - 1] = '\0';
histvalid = 1;
return (hadhist);
}
static time_t
a2time_t(Char *wordx)
{
/* Attempt to distinguish timestamps from other possible entries.
* Format: "+NNNNNNNNNN" (10 digits, left padded with ascii '0') */
time_t ret;
Char *s;
int ct;
if (!wordx || *(s = wordx) != '+')
return (time_t)0;
for (++s, ret = 0, ct = 0; *s; ++s, ++ct) {
if (!isdigit((unsigned char)*s))
return (time_t)0;
ret = ret * 10 + (time_t)((unsigned char)*s - '0');
}
if (ct != 10)
return (time_t)0;
return ret;
}
void
prlex(struct wordent *sp0)
{
struct wordent *sp = sp0->next;
for (;;) {
xprintf("%S", sp->word);
sp = sp->next;
if (sp == sp0)
break;
if (sp->word[0] != '\n')
xputchar(' ');
}
}
void
copylex(struct wordent *hp, struct wordent *fp)
{
struct wordent *wdp;
wdp = hp;
fp = fp->next;
do {
struct wordent *new;
new = xmalloc(sizeof(*new));
new->word = NULL;
new->prev = wdp;
new->next = hp;
wdp->next = new;
hp->prev = new;
wdp = new;
wdp->word = Strsave(fp->word);
fp = fp->next;
} while (wdp->word[0] != '\n');
}
void
initlex(struct wordent *vp)
{
vp->word = STRNULL;
vp->prev = vp;
vp->next = vp;
}
void
freelex(struct wordent *vp)
{
struct wordent *fp;
while (vp->next != vp) {
fp = vp->next;
vp->next = fp->next;
xfree(fp->word);
xfree(fp);
}
vp->prev = vp;
}
void
lex_cleanup(void *xvp)
{
struct wordent *vp;
vp = xvp;
freelex(vp);
}
static Char *
word(int parsehtime)
{
eChar c, c1;
struct Strbuf wbuf = Strbuf_INIT;
Char hbuf[12];
int h;
int dolflg;
cleanup_push(&wbuf, Strbuf_cleanup);
loop:
while ((c = getC(DOALL)) == ' ' || c == '\t')
continue;
if (cmap(c, _META | _ESC))
switch (c) {
case '&':
case '|':
case '<':
case '>':
Strbuf_append1(&wbuf, c);
c1 = getC(DOALL);
if (c1 == c)
Strbuf_append1(&wbuf, c1);
else
ungetC(c1);
goto ret;
case '#':
if (intty || (enterhist && !parsehtime))
break;
c = 0;
h = 0;
do {
c1 = c;
c = getC(0);
if (h < 11 && parsehtime)
hbuf[h++] = c;
} while (c != '\n');
if (parsehtime) {
hbuf[11] = '\0';
Htime = a2time_t(hbuf);
}
if (c1 == '\\')
goto loop;
/*FALLTHROUGH*/
case ';':
case '(':
case ')':
case '\n':
Strbuf_append1(&wbuf, c);
goto ret;
case '\\':
c = getC(0);
if (c == '\n') {
if (onelflg == 1)
onelflg = 2;
goto loop;
}
if (c != (eChar)HIST)
Strbuf_append1(&wbuf, '\\');
c |= QUOTE;
default:
break;
}
c1 = 0;
dolflg = DOALL;
for (;;) {
if (c1) {
if (c == c1) {
c1 = 0;
dolflg = DOALL;
}
else if (c == '\\') {
c = getC(0);
/*
* PWP: this is dumb, but how all of the other shells work. If \ quotes
* a character OUTSIDE of a set of ''s, why shouldn't it quote EVERY
* following character INSIDE a set of ''s.
*
* Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar
*/
if (c == (eChar)HIST)
c |= QUOTE;
else {
if (bslash_quote &&
((c == '\'') || (c == '"') ||
(c == '\\') || (c == '$'))) {
c |= QUOTE;
}
else {
if (c == '\n')
/*
* if (c1 == '`') c = ' '; else
*/
c |= QUOTE;
ungetC(c);
c = '\\' | QUOTE;
}
}
}
else if (c == '\n') {
seterror(ERR_UNMATCHED, c1);
ungetC(c);
break;
}
}
else if (cmap(c, _META | _QF | _QB | _ESC)) {
if (c == '\\') {
c = getC(0);
if (c == '\n') {
if (onelflg == 1)
onelflg = 2;
break;
}
if (c != (eChar)HIST)
Strbuf_append1(&wbuf, '\\');
c |= QUOTE;
}
else if (cmap(c, _QF | _QB)) { /* '"` */
c1 = c;
dolflg = c == '"' ? DOALL : DOEXCL;
}
else if (c != '#' || (!intty && !enterhist)) {
ungetC(c);
break;
}
}
Strbuf_append1(&wbuf, c);
c = getC(dolflg);
}
ret:
cleanup_ignore(&wbuf);
cleanup_until(&wbuf);
return Strbuf_finish(&wbuf);
}
static eChar
getC1(int flag)
{
eChar c;
for (;;) {
if ((c = peekc) != 0) {
peekc = 0;
return (c);
}
if (lap < labuf.len) {
c = labuf.s[lap++];
if (cmap(c, _META | _QF | _QB))
c |= QUOTE;
return (c);
}
if ((c = peekd) != 0) {
peekd = 0;
return (c);
}
if (exclp) {
if ((c = *exclp++) != 0)
return (c);
if (exclnxt && --exclc >= 0) {
exclnxt = exclnxt->next;
setexclp(exclnxt->word);
return (' ');
}
exclp = 0;
exclnxt = 0;
/* this will throw away the dummy history entries */
savehist(NULL, 0);
}
if (exclnxt) {
exclnxt = exclnxt->next;
if (--exclc < 0)
exclnxt = 0;
else
setexclp(exclnxt->word);
continue;
}
c = readc(1);
/* Catch EOF in the middle of a line. (An EOF at the beginning of
* a line would have been processed by the readc(0) in lex().) */
if (c == CHAR_ERR)
c = '\n';
if (c == '$' && (flag & DODOL)) {
getdol();
continue;
}
if (c == (eChar)HIST && (flag & DOEXCL)) {
getexcl(0);
continue;
}
break;
}
return (c);
}
static void
getdol(void)
{
struct Strbuf name = Strbuf_INIT;
eChar c;
eChar sc;
int special = 0;
c = sc = getC(DOEXCL);
if (any("\t \n", c)) {
ungetD(c);
ungetC('$' | QUOTE);
return;
}
cleanup_push(&name, Strbuf_cleanup);
Strbuf_append1(&name, '$');
if (c == '{')
Strbuf_append1(&name, c), c = getC(DOEXCL);
if (c == '#' || c == '?' || c == '%')
special++, Strbuf_append1(&name, c), c = getC(DOEXCL);
Strbuf_append1(&name, c);
switch (c) {
case '<':
case '$':
case '!':
if (special)
seterror(ERR_SPDOLLT);
goto end;
case '\n':
ungetD(c);
name.len--;
if (!special)
seterror(ERR_NEWLINE);
goto end;
case '*':
if (special)
seterror(ERR_SPSTAR);
goto end;
default:
if (Isdigit(c)) {
#ifdef notdef
/* let $?0 pass for now */
if (special) {
seterror(ERR_DIGIT);
goto end;
}
#endif
while ((c = getC(DOEXCL)) != 0) {
if (!Isdigit(c))
break;
Strbuf_append1(&name, c);
}
}
else if (letter(c)) {
while ((c = getC(DOEXCL)) != 0) {
/* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
if (!letter(c) && !Isdigit(c))
break;
Strbuf_append1(&name, c);
}
}
else {
if (!special)
seterror(ERR_VARILL);
else {
ungetD(c);
name.len--;
}
goto end;
}
break;
}
if (c == '[') {
Strbuf_append1(&name, c);
do {
/*
* Michael Greim: Allow $ expansion to take place in selector
* expressions. (limits the number of characters returned)
*/
c = getC(DOEXCL | DODOL);
if (c == '\n') {
ungetD(c);
name.len--;
seterror(ERR_NLINDEX);
goto end;
}
Strbuf_append1(&name, c);
} while (c != ']');
c = getC(DOEXCL);
}
if (c == ':') {
/*
* if the :g modifier is followed by a newline, then error right away!
* -strike
*/
int gmodflag = 0, amodflag = 0;
do {
Strbuf_append1(&name, c), c = getC(DOEXCL);
if (c == 'g' || c == 'a') {
if (c == 'g')
gmodflag++;
else
amodflag++;
Strbuf_append1(&name, c); c = getC(DOEXCL);
}
if ((c == 'g' && !gmodflag) || (c == 'a' && !amodflag)) {
if (c == 'g')
gmodflag++;
else
amodflag++;
Strbuf_append1(&name, c); c = getC(DOEXCL);
}
Strbuf_append1(&name, c);
/* scan s// [eichin:19910926.0512EST] */
if (c == 's') {
int delimcnt = 2;
eChar delim = getC(0);
Strbuf_append1(&name, delim);
if (!delim || letter(delim)
|| Isdigit(delim) || any(" \t\n", delim)) {
seterror(ERR_BADSUBST);
break;
}
while ((c = getC(0)) != CHAR_ERR) {
Strbuf_append1(&name, c);
if(c == delim) delimcnt--;
if(!delimcnt) break;
}
if(delimcnt) {
seterror(ERR_BADSUBST);
break;
}
c = 's';
}
if (!any("htrqxesul", c)) {
if ((amodflag || gmodflag) && c == '\n')
stderror(ERR_VARSYN); /* strike */
seterror(ERR_BADMOD, c);
goto end;
}
}
while ((c = getC(DOEXCL)) == ':');
ungetD(c);
}
else
ungetD(c);
if (sc == '{') {
c = getC(DOEXCL);
if (c != '}') {
ungetD(c);
seterror(ERR_MISSING, '}');
goto end;
}
Strbuf_append1(&name, c);
}
end:
cleanup_ignore(&name);
cleanup_until(&name);
addla(Strbuf_finish(&name));
}
/* xfree()'s its argument */
void
addla(Char *cp)
{
static struct Strbuf buf; /* = Strbuf_INIT; */
buf.len = 0;
Strbuf_appendn(&buf, labuf.s + lap, labuf.len - lap);
labuf.len = 0;
Strbuf_append(&labuf, cp);
Strbuf_terminate(&labuf);
Strbuf_appendn(&labuf, buf.s, buf.len);
xfree(cp);
lap = 0;
}
/* left-hand side of last :s or search string of last ?event? */
static struct Strbuf lhsb; /* = Strbuf_INIT; */
static struct Strbuf slhs; /* = Strbuf_INIT; left-hand side of last :s */
static struct Strbuf rhsb; /* = Strbuf_INIT; right-hand side of last :s */
static int quesarg;
static void
getexcl(Char sc)
{
struct wordent *hp, *ip;
int left, right, dol;
eChar c;
if (sc == 0) {
c = getC(0);
if (c == '{')
sc = (Char) c;
else
ungetC(c);
}
quesarg = -1;
lastev = eventno;
hp = gethent(sc);
if (hp == 0)
return;
hadhist = 1;
dol = 0;
if (hp == alhistp)
for (ip = hp->next->next; ip != alhistt; ip = ip->next)
dol++;
else
for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
dol++;
left = 0, right = dol;
if (sc == HISTSUB && HISTSUB != '\0') {
ungetC('s'), unreadc(HISTSUB), c = ':';
goto subst;
}
c = getC(0);
if (!any(":^$*-%", c))
goto subst;
left = right = -1;
if (c == ':') {
c = getC(0);
unreadc(c);
if (letter(c) || c == '&') {
c = ':';
left = 0, right = dol;
goto subst;
}
}
else
ungetC(c);
if (!getsel(&left, &right, dol))
return;
c = getC(0);
if (c == '*')
ungetC(c), c = '-';
if (c == '-') {
if (!getsel(&left, &right, dol))
return;
c = getC(0);
}
subst:
exclc = right - left + 1;
while (--left >= 0)
hp = hp->next;
if ((sc == HISTSUB && HISTSUB != '\0') || c == ':') {
do {
hp = getsub(hp);
c = getC(0);
} while (c == ':');
}
unreadc(c);
if (sc == '{') {
c = getC(0);
if (c != '}')
seterror(ERR_BADBANG);
}
exclnxt = hp;
}
static struct wordent *
getsub(struct wordent *en)
{
eChar delim;
eChar c;
eChar sc;
int global;
do {
exclnxt = 0;
global = 0;
sc = c = getC(0);
while (c == 'g' || c == 'a') {
global |= (c == 'g') ? FLAG_G : FLAG_A;
sc = c = getC(0);
}
switch (c) {
case 'p':
justpr++;
return (en);
case 'x':
case 'q':
global |= FLAG_G;
/*FALLTHROUGH*/
case 'h':
case 'r':
case 't':
case 'e':
case 'u':
case 'l':
break;
case '&':
if (slhs.len == 0) {
seterror(ERR_NOSUBST);
return (en);
}
lhsb.len = 0;
Strbuf_append(&lhsb, slhs.s);
Strbuf_terminate(&lhsb);
break;
#ifdef notdef
case '~':
if (lhsb.len == 0)
goto badlhs;
break;
#endif
case 's':
delim = getC(0);
if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
unreadc(delim);
lhsb.len = 0;
seterror(ERR_BADSUBST);
return (en);
}
Strbuf_terminate(&lhsb);
lhsb.len = 0;
for (;;) {
c = getC(0);
if (c == '\n') {
unreadc(c);
break;
}
if (c == delim)
break;
if (c == '\\') {
c = getC(0);
if (c != delim && c != '\\')
Strbuf_append1(&lhsb, '\\');
}
Strbuf_append1(&lhsb, c);
}
if (lhsb.len != 0)
Strbuf_terminate(&lhsb);
else if (lhsb.s[0] == 0) {
seterror(ERR_LHS);
return (en);
} else
lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
rhsb.len = 0;
for (;;) {
c = getC(0);
if (c == '\n') {
unreadc(c);
break;
}
if (c == delim)
break;
if (c == '\\') {
c = getC(0);
if (c != delim /* && c != '~' */ )
Strbuf_append1(&rhsb, '\\');
}
Strbuf_append1(&rhsb, c);
}
Strbuf_terminate(&rhsb);
break;
default:
if (c == '\n')
unreadc(c);
seterror(ERR_BADBANGMOD, (int)c);
return (en);
}
slhs.len = 0;
if (lhsb.s != NULL && lhsb.len != 0)
Strbuf_append(&slhs, lhsb.s);
Strbuf_terminate(&slhs);
if (exclc)
en = dosub(sc, en, global);
}
while ((c = getC(0)) == ':');
unreadc(c);
return (en);
}
/*
*
* From Beto Appleton ([email protected])
*
* when using history substitution, and the variable
* 'history' is set to a value higher than 1000,
* the shell might either freeze (hang) or core-dump.
* We raise the limit to 50000000
*/
#define HIST_PURGE -50000000
static struct wordent *
dosub(Char sc, struct wordent *en, int global)
{
struct wordent lexi;
int didsub = 0, didone = 0;
struct wordent *hp = &lexi;
struct wordent *wdp;
int i = exclc;
struct Hist *hst;
wdp = hp;
while (--i >= 0) {
struct wordent *new = xcalloc(1, sizeof *wdp);
new->word = 0;
new->prev = wdp;
new->next = hp;
wdp->next = new;
wdp = new;
en = en->next;
if (en->word) {
Char *tword, *otword;
if ((global & FLAG_G) || didsub == 0) {
size_t pos;
pos = 0;
tword = subword(en->word, sc, &didone, &pos);
if (didone)
didsub = 1;
if (global & FLAG_A) {
while (didone && tword != STRNULL) {
otword = tword;
tword = subword(otword, sc, &didone, &pos);
if (Strcmp(tword, otword) == 0) {
xfree(otword);
break;
}
else
xfree(otword);
}
}
}
else
tword = Strsave(en->word);
wdp->word = tword;
}
}
if (didsub == 0)
seterror(ERR_MODFAIL);
hp->prev = wdp;
/*
* ANSI mode HP/UX compiler chokes on
* return &enthist(HIST_PURGE, &lexi, 0)->Hlex;
*/
hst = enthist(HIST_PURGE, &lexi, 0, 0, -1);
return &(hst->Hlex);
}
/* Return a newly allocated result of one modification of CP using the
operation TYPE. Set ADID to 1 if a modification was performed.
If TYPE == 's', perform substitutions only from *START_POS on and set
*START_POS to the position of next substitution attempt. */
static Char *
subword(Char *cp, Char type, int *adid, size_t *start_pos)
{
Char *wp;
const Char *mp, *np;
switch (type) {
case 'r':
case 'e':
case 'h':
case 't':
case 'q':
case 'x':
case 'u':
case 'l':
wp = domod(cp, type);
if (wp == 0) {
*adid = 0;
return (Strsave(cp));
}
*adid = 1;
return (wp);
default:
for (mp = cp + *start_pos; *mp; mp++) {
if (matchs(mp, lhsb.s)) {
struct Strbuf wbuf = Strbuf_INIT;
Strbuf_appendn(&wbuf, cp, mp - cp);
for (np = rhsb.s; *np; np++)
switch (*np) {
case '\\':
if (np[1] == '&')
np++;
/* fall into ... */
default:
Strbuf_append1(&wbuf, *np);
continue;
case '&':
Strbuf_append(&wbuf, lhsb.s);
continue;
}
*start_pos = wbuf.len;
Strbuf_append(&wbuf, mp + lhsb.len);
*adid = 1;