-
Notifications
You must be signed in to change notification settings - Fork 154
/
macro.c
2961 lines (2923 loc) · 61.9 KB
/
macro.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
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2014 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgkorn@gmail.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* Shell macro expander
* expands ~
* expands ${...}
* expands $(...)
* expands $((...))
* expands `...`
*
* David Korn
* AT&T Labs
*
*/
#include "defs.h"
#include <fcin.h>
#include <pwd.h>
#include <ctype.h>
#include <regex.h>
#include "name.h"
#include "variables.h"
#include "shlex.h"
#include "io.h"
#include "jobs.h"
#include "shnodes.h"
#include "path.h"
#include "national.h"
#include "streval.h"
#if SHOPT_MULTIBYTE
# undef isascii
# define isacii(c) ((c)<=UCHAR_MAX)
# include <lc.h>
#else
# define mbchar(p) (*(unsigned char*)p++)
#endif /* SHOPT_MULTIBYTE */
#if _WINIX
static int Skip;
#endif /*_WINIX */
static int _c_;
typedef struct _mac_
{
Shell_t *shp; /* pointer to shell interpreter */
Sfio_t *sp; /* stream pointer for here-document */
struct argnod **arghead; /* address of head of argument list */
char *ifsp; /* pointer to IFS value */
int fields; /* number of fields */
short quoted; /* set when word has quotes */
unsigned char ifs; /* first char of IFS */
char atmode; /* when processing $@ */
char quote; /* set within double quoted contexts */
char lit; /* set within single quotes */
char split; /* set when word splittin is possible */
char pattern; /* set when file expansion follows */
char patfound; /* set if pattern character found */
char assign; /* set for assignments */
char arith; /* set for ((...)) */
char let; /* set when expanding let arguments */
char zeros; /* strip leading zeros when set */
char arrayok; /* $x[] ok for arrays */
char subcopy; /* set when copying subscript */
char macsub; /* set to 1 when running mac_substitute */
char maccase; /* set to 1 when expanding case pattern */
int dotdot; /* set for .. in subscript */
void *nvwalk; /* for name space walking*/
} Mac_t;
#undef ESCAPE
#define ESCAPE '\\'
#define isescchar(s) ((s)>S_QUOTE)
#define isqescchar(s) ((s)>=S_QUOTE)
#define isbracechar(c) ((c)==RBRACE || (_c_=sh_lexstates[ST_BRACE][c])==S_MOD1 ||_c_==S_MOD2)
#define ltos(x) fmtbase((long)(x),0,0)
/* type of macro expansions */
#define M_BRACE 1 /* ${var} */
#define M_TREE 2 /* ${var.} */
#define M_SIZE 3 /* ${#var} */
#define M_VNAME 4 /* ${!var} */
#define M_SUBNAME 5 /* ${!var[sub]} */
#define M_NAMESCAN 6 /* ${!var*} */
#define M_NAMECOUNT 7 /* ${#var*} */
#define M_TYPE 8 /* ${@var} */
#define M_EVAL 9 /* ${$var} */
static int substring(const char*, size_t, const char*, int[], int);
static void copyto(Mac_t*, int, int);
static void comsubst(Mac_t*, Shnode_t*, int);
static bool varsub(Mac_t*);
static void mac_copy(Mac_t*,const char*, size_t);
static void tilde_expand2(Shell_t*,int);
static char *sh_tilde(Shell_t*,const char*);
static char *special(Shell_t *,int);
static void endfield(Mac_t*,int);
static void mac_error(Namval_t*);
static char *mac_getstring(char*);
static int charlen(const char*,int);
#if SHOPT_MULTIBYTE
static char *lastchar(const char*,const char*);
#endif /* SHOPT_MULTIBYTE */
void *sh_macopen(Shell_t *shp)
{
void *addr = newof(0,Mac_t,1,0);
Mac_t *mp = (Mac_t*)addr;
mp->shp = shp;
return(addr);
}
/*
* perform only parameter substitution and catch failures
*/
char *sh_mactry(Shell_t *shp,register char *string)
{
if(string)
{
int jmp_val;
int savexit = shp->savexit;
struct checkpt buff;
sh_pushcontext(shp,&buff,SH_JMPSUB);
jmp_val = sigsetjmp(buff.buff,0);
if(jmp_val == 0)
string = sh_mactrim(shp,string,0);
sh_popcontext(shp,&buff);
shp->savexit = savexit;
return(string);
}
return("");
}
/*
* Perform parameter expansion, command substitution, and arithmetic
* expansion on <str>.
* If <mode> greater than 1 file expansion is performed if the result
* yields a single pathname.
* If <mode> negative, than expansion rules for assignment are applied.
*/
char *sh_mactrim(Shell_t *shp, char *str, register int mode)
{
register Mac_t *mp = (Mac_t*)shp->mac_context;
Stk_t *stkp = shp->stk;
Mac_t savemac;
savemac = *mp;
stkseek(stkp,0);
mp->arith = (mode==3);
mp->let = 0;
shp->argaddr = 0;
mp->pattern = (mode==1||mode==2);
mp->patfound = 0;
mp->assign = 0;
if(mode<0)
mp->assign = -mode;
mp->quoted = mp->lit = mp->split = mp->quote = 0;
mp->sp = 0;
if(mp->ifsp=nv_getval(sh_scoped(shp,IFSNOD)))
mp->ifs = *mp->ifsp;
else
mp->ifs = ' ';
stkseek(stkp,0);
fcsopen(str);
copyto(mp,0,mp->arith);
str = stkfreeze(stkp,1);
if(mode==2)
{
/* expand only if unique */
struct argnod *arglist=0;
if((mode=path_expand(shp,str,&arglist))==1)
str = arglist->argval;
else if(mode>1)
errormsg(SH_DICT,ERROR_exit(1),e_ambiguous,str);
sh_trim(str);
}
*mp = savemac;
return(str);
}
/*
* Perform all the expansions on the argument <argp>
*/
int sh_macexpand(Shell_t* shp, register struct argnod *argp, struct argnod **arghead,int flag)
{
register int flags = argp->argflag;
register char *str = argp->argval;
register Mac_t *mp = (Mac_t*)shp->mac_context;
char **saveargaddr = shp->argaddr;
Mac_t savemac;
Stk_t *stkp = shp->stk;
savemac = *mp;
mp->sp = 0;
if(mp->ifsp=nv_getval(sh_scoped(shp,IFSNOD)))
mp->ifs = *mp->ifsp;
else
mp->ifs = ' ';
if((flag&ARG_OPTIMIZE) && !shp->indebug && !(flags&ARG_MESSAGE))
shp->argaddr = (char**)&argp->argchn.ap;
else
shp->argaddr = 0;
mp->arghead = arghead;
mp->quoted = mp->lit = mp->quote = 0;
mp->arith = ((flag&ARG_ARITH)!=0);
mp->let = ((flag&ARG_LET)!=0);
mp->split = !(flag&ARG_ASSIGN);
mp->assign = !mp->split;
mp->pattern = mp->split && !(flag&ARG_NOGLOB) && !sh_isoption(mp->shp,SH_NOGLOB);
mp->arrayok = mp->arith || (flag&ARG_ARRAYOK);
str = argp->argval;
fcsopen(str);
mp->fields = 0;
mp->atmode = 0;
if(!arghead)
{
mp->split = 0;
mp->pattern = ((flag&ARG_EXP)!=0);
mp->maccase = ((flag&ARG_CASE)!=0);
stkseek(stkp,0);
}
else
{
stkseek(stkp,ARGVAL);
*stkptr(stkp,ARGVAL-1) = 0;
}
mp->patfound = 0;
if(mp->pattern)
mp->arrayok = 0;
copyto(mp,0,mp->arith);
if(!arghead)
{
argp->argchn.cp = stkfreeze(stkp,1);
if(shp->argaddr)
argp->argflag |= ARG_MAKE;
}
else
{
endfield(mp,mp->quoted|mp->atmode);
flags = mp->fields;
if(flags==1 && shp->argaddr)
argp->argchn.ap = *arghead;
}
shp->argaddr = saveargaddr;
*mp = savemac;
return(flags);
}
/*
* Expand here document which is stored in <infile> or <string>
* The result is written to <outfile>
*/
void sh_machere(Shell_t *shp,Sfio_t *infile, Sfio_t *outfile, char *string)
{
register int c,n;
register const char *state = sh_lexstates[ST_QUOTE];
register char *cp;
register Mac_t *mp = (Mac_t*)shp->mac_context;
Lex_t *lp = (Lex_t*)mp->shp->lex_context;
Fcin_t save;
Mac_t savemac;
Stk_t *stkp = shp->stk;
savemac = *mp;
stkseek(stkp,0);
shp->argaddr = 0;
mp->sp = outfile;
mp->split = mp->assign = mp->pattern = mp->patfound = mp->lit = mp->arith = mp->let = 0;
mp->quote = 1;
mp->ifsp = nv_getval(sh_scoped(shp,IFSNOD));
mp->ifs = ' ';
fcsave(&save);
if(infile)
fcfopen(infile);
else
fcsopen(string);
fcnotify(0,lp);
cp = fcseek(0);
while(1)
{
#if SHOPT_MULTIBYTE
if(mbwide())
{
do
{
ssize_t len;
switch(len = mbsize(cp))
{
case -1: /* illegal multi-byte char */
case 0:
case 1:
n=state[*(unsigned char*)cp++];
break;
default:
/* use state of alpha character */
n=state['a'];
cp += len;
}
}
while(n == 0);
}
else
#endif /* SHOPT_MULTIBYTE */
while((n=state[*(unsigned char*)cp++])==0);
if(n==S_NL || n==S_QUOTE || n==S_RBRA)
continue;
if(c=(cp-1)-fcseek(0))
sfwrite(outfile,fcseek(0),c);
cp = fcseek(c+1);
switch(n)
{
case S_EOF:
if((n=fcfill()) <=0)
{
/* ignore 0 byte when reading from file */
if(n==0 && fcfile())
continue;
fcrestore(&save);
*mp = savemac;
return;
}
cp = fcseek(-1);
continue;
case S_ESC:
fcgetc(c);
cp=fcseek(-1);
if(c>0)
cp++;
if(!isescchar(state[c]))
sfputc(outfile,ESCAPE);
continue;
case S_GRAVE:
comsubst(mp,(Shnode_t*)0,0);
break;
case S_DOL:
c = fcget();
if(c=='.')
goto regular;
again:
switch(n=sh_lexstates[ST_DOL][c])
{
case S_ALP: case S_SPC1: case S_SPC2:
case S_DIG: case S_LBRA:
{
Fcin_t save2;
int offset = stktell(stkp);
int offset2;
fcnotify(0,lp);
sfputc(stkp,c);
if(n==S_LBRA)
{
c = fcget();
fcseek(-1);
if(sh_lexstates[ST_NORM][c]==S_BREAK)
{
comsubst(mp,(Shnode_t*)0,2);
break;
}
sh_lexskip(lp,RBRACE,1,ST_BRACE);
}
else if(n==S_ALP)
{
while(fcgetc(c),isaname(c))
sfputc(stkp,c);
fcseek(-1);
}
sfputc(stkp,0);
offset2 = stktell(stkp);
fcsave(&save2);
fcsopen(stkptr(stkp,offset));
varsub(mp);
if(c=stktell(stkp)-offset2)
sfwrite(outfile,(char*)stkptr(stkp,offset2),c);
fcrestore(&save2);
stkseek(stkp,offset);
break;
}
case S_PAR:
comsubst(mp,(Shnode_t*)0,3);
break;
case S_EOF:
if((c=fcfill()) > 0)
goto again;
/* FALL THRU */
default:
regular:
sfputc(outfile,'$');
fcseek(-1);
break;
}
}
cp = fcseek(0);
}
}
/*
* expand argument but do not trim pattern characters
*/
char *sh_macpat(Shell_t *shp,register struct argnod *arg, int flags)
{
register char *sp = arg->argval;
if((arg->argflag&ARG_RAW))
return(sp);
sh_stats(STAT_ARGEXPAND);
if(flags&ARG_OPTIMIZE)
arg->argchn.ap=0;
if(!(sp=arg->argchn.cp))
{
sh_macexpand(shp,arg,NIL(struct argnod**),flags|ARG_ARRAYOK);
sp = arg->argchn.cp;
if(!(flags&ARG_OPTIMIZE) || !(arg->argflag&ARG_MAKE))
arg->argchn.cp = 0;
arg->argflag &= ~ARG_MAKE;
}
else
sh_stats(STAT_ARGHITS);
return(sp);
}
/*
* Process the characters up to <endch> or end of input string
*/
static void copyto(register Mac_t *mp,int endch, int newquote)
{
register int c,n;
register const char *state = sh_lexstates[ST_MACRO];
register char *cp,*first;
Shell_t *shp = mp->shp;
Lex_t *lp = (Lex_t*)shp->lex_context;
int tilde = -1;
int oldquote = mp->quote;
int ansi_c = 0;
int paren = 0;
int ere = 0;
int dotdot = 0;
int brace = 0;
Sfio_t *sp = mp->sp;
Stk_t *stkp = shp->stk;
char *resume = 0;
mp->sp = NIL(Sfio_t*);
mp->quote = newquote;
first = cp = fcseek(0);
if(!mp->quote && *cp=='~' && cp[1]!=LPAREN)
tilde = stktell(stkp);
/* handle // operator specially */
if(mp->pattern==2 && *cp=='/')
cp++;
while(1)
{
#if SHOPT_MULTIBYTE
if(mbwide())
{
ssize_t len;
do
{
switch(len = mbsize(cp))
{
case -1: /* illegal multi-byte char */
case 0:
len = 1;
case 1:
n = state[*(unsigned char*)cp++];
break;
default:
/* treat as if alpha */
cp += len;
n=state['a'];
}
}
while(n == 0);
c = (cp-len) - first;
}
else
#endif /* SHOPT_MULTIBYTE */
{
while((n=state[*(unsigned char*)cp++])==0);
c = (cp-1) - first;
}
switch(n)
{
case S_ESC:
if(ansi_c)
{
/* process ANSI-C escape character */
char *addr= --cp;
if(c)
sfwrite(stkp,first,c);
c = chresc(cp,&addr);
cp = addr;
first = fcseek(cp-first);
#if SHOPT_MULTIBYTE
if(c > UCHAR_MAX && mbwide())
{
int i;
unsigned char mb[8];
n = mbconv((char*)mb, c);
for(i=0;i<n;i++)
sfputc(stkp,mb[i]);
}
else
#endif /* SHOPT_MULTIBYTE */
sfputc(stkp,c);
if(c==ESCAPE && mp->pattern)
sfputc(stkp,ESCAPE);
break;
}
else if(sh_isoption(shp,SH_BRACEEXPAND) && mp->pattern==4 && (*cp==',' || *cp==LBRACE || *cp==RBRACE || *cp=='.'))
break;
else if(mp->split && endch && !mp->quote && !mp->lit)
{
if(c)
mac_copy(mp,first,c);
cp = fcseek(c+2);
if(c= cp[-1])
{
sfputc(stkp,c);
if(c==ESCAPE)
sfputc(stkp,ESCAPE);
}
else
cp--;
first = cp;
break;
}
n = state[*(unsigned char*)cp];
if(n==S_ENDCH && *cp!=endch)
n = S_PAT;
if(mp->pattern)
{
/* preserve \digit for pattern matching */
/* also \alpha for extended patterns */
if(!mp->lit && !mp->quote)
{
int nc = *(unsigned char*)cp;
if((n==S_DIG || ((paren+ere) && (sh_lexstates[ST_DOL][nc]==S_ALP) || nc=='<' || nc=='>')))
break;
if(ere && mp->pattern==1 && strchr(".[()*+?{|^$&!",*cp))
break;
}
/* followed by file expansion */
if(!mp->lit && (n==S_ESC || (!mp->quote &&
(n==S_PAT||n==S_ENDCH||n==S_SLASH||n==S_BRACT||*cp=='-'))))
{
cp += (n!=S_EOF);
if(ere && n==S_ESC && *cp =='\\' && cp[1]=='$')
{
/* convert \\\$ into \$' */
sfwrite(stkp,first,c+1);
cp = first = fcseek(c+3);
}
break;
}
if(!(ere && *cp=='$') && (mp->lit || (mp->quote && !isqescchar(n) && n!=S_ENDCH)))
{
/* add \ for file expansion */
sfwrite(stkp,first,c+1);
first = fcseek(c);
break;
}
}
if(mp->lit)
break;
if(!mp->quote || isqescchar(n) || n==S_ENDCH)
{
/* eliminate \ */
if(c)
sfwrite(stkp,first,c);
/* check new-line joining */
first = fcseek(c+1);
}
cp += (n!=S_EOF);
break;
case S_GRAVE: case S_DOL:
if(mp->lit)
break;
if(c)
{
if(mp->split && !mp->quote && endch)
mac_copy(mp,first,c);
else
sfwrite(stkp,first,c);
}
first = fcseek(c+1);
c = mp->pattern;
if(n==S_GRAVE)
comsubst(mp,(Shnode_t*)0,0);
else if((n= *cp) == '"' && !mp->quote)
{
int off = stktell(stkp);
char *dp;
cp = first = fcseek(1);
mp->quote = 1;
if(!ERROR_translating())
break;
while(n=c, c= *++cp)
{
if(c=='\\' && n==c)
c = 0;
else if(c=='"' && n!='\\')
break;
}
n = cp-first;
sfwrite(stkp,first,n);
sfputc(stkp,0);
cp = stkptr(stkp,off);
dp = (char*)sh_translate(cp);
stkseek(stkp,off);
if(dp==cp)
{
cp = first;
break;
}
resume = fcseek(n);
fcclose();
fcsopen(dp);
cp = first = fcseek(0);
break;
}
else if(n==0 || !varsub(mp))
{
if(n=='\'' && !mp->quote)
ansi_c = 1;
else if(mp->quote || n!='"')
sfputc(stkp,'$');
}
cp = first = fcseek(0);
if(mp->quote && cp)
mp->pattern = c;
break;
case S_ENDCH:
if((mp->lit || cp[-1]!=endch || mp->quote!=newquote))
goto pattern;
if(endch==RBRACE && mp->pattern && brace)
{
brace--;
if(*cp==LPAREN && mp->pattern!=2)
goto pattern;
continue;
}
case S_EOF:
if(c)
{
if(mp->split && !mp->quote && !mp->lit && endch)
mac_copy(mp,first,c);
else
sfwrite(stkp,first,c);
}
if(n==S_EOF && resume)
{
fcclose();
fcsopen(resume);
resume = 0;
cp = first = fcseek(0);
continue;
}
c += (n!=S_EOF);
first = fcseek(c);
if(tilde>=0)
tilde_expand2(shp,tilde);
goto done;
case S_QUOTE:
if(mp->lit || mp->arith)
break;
case S_LIT:
if(mp->arith)
{
if((*cp=='`' || *cp=='[') && cp[1]=='\'')
cp +=2;
break;
}
if(n==S_LIT && mp->quote)
break;
if(c)
{
if(mp->split && endch && !mp->quote && !mp->lit)
mac_copy(mp,first,c);
else
sfwrite(stkp,first,c);
}
first = fcseek(c+1);
if(n==S_LIT)
{
if(mp->quote)
continue;
if(mp->lit)
mp->lit = ansi_c = 0;
else
mp->lit = 1;
}
else
mp->quote = !mp->quote;
mp->quoted++;
break;
case S_BRACT:
if(mp->arith || (((mp->assign&1) || endch==RBRACT) &&
!(mp->quote || mp->lit)))
{
int offset=0,oldpat = mp->pattern;
int oldarith = mp->arith, oldsub=mp->subcopy;
sfwrite(stkp,first,++c);
if(mp->assign&1)
{
if(first[c-2]=='.')
offset = stktell(stkp);
if(isastchar(*cp) && cp[1]==']')
errormsg(SH_DICT,ERROR_exit(1),
e_badsubscript,*cp);
}
first = fcseek(c);
mp->pattern = 4;
mp->arith = 0;
mp->subcopy = 0;
copyto(mp,RBRACT,0);
mp->subcopy = oldsub;
mp->arith = oldarith;
mp->pattern = oldpat;
sfputc(stkp,RBRACT);
if(offset)
{
cp = stkptr(stkp,stktell(stkp));
if(sh_checkid(stkptr(stkp,offset),cp)!=cp)
stkseek(stkp,stktell(stkp)-2);
}
cp = first = fcseek(0);
break;
}
case S_PAT:
if(mp->pattern && !(mp->quote || mp->lit))
{
mp->patfound = mp->pattern;
if((n=cp[-1])==LPAREN)
{
paren++;
if((cp-first)>1 && cp[-2]=='~')
{
char *p = cp;
while((c=mbchar(p)) && c!=RPAREN)
if(c=='A'||c=='E'||c=='K'||c=='P'||c=='X')
{
ere = 1;
break;
}
}
}
else if(n==RPAREN)
--paren;
}
goto pattern;
case S_COM:
if(mp->pattern==4 && (mp->quote || mp->lit))
{
if(c)
{
sfwrite(stkp,first,c);
first = fcseek(c);
}
sfputc(stkp,ESCAPE);
}
break;
case S_BRACE:
if(!(mp->quote || mp->lit))
{
mp->patfound = mp->split && sh_isoption(shp,SH_BRACEEXPAND);
brace++;
}
pattern:
if(!mp->pattern || !(mp->quote || mp->lit))
{
/* mark beginning of {a,b} */
if(n==S_BRACE && endch==0 && mp->pattern)
mp->pattern=4;
if(n==S_SLASH && mp->pattern==2)
mp->pattern=3;
break;
}
if(mp->pattern==3)
break;
if(c)
sfwrite(stkp,first,c);
first = fcseek(c);
sfputc(stkp,ESCAPE);
break;
case S_EQ:
if(mp->assign==1)
{
if(*cp=='~' && !endch && !mp->quote && !mp->lit)
tilde = stktell(stkp)+(c+1);
mp->assign = 2;
}
break;
case S_SLASH:
case S_COLON:
if(tilde >=0)
{
if(c)
sfwrite(stkp,first,c);
first = fcseek(c);
tilde_expand2(shp,tilde);
#if _WINIX
if(Skip)
{
first = cp = fcseek(Skip);
Skip = 0;
}
#endif /*_WINIX */
tilde = -1;
c=0;
}
if(n==S_COLON && mp->assign==2 && *cp=='~' && endch==0 && !mp->quote &&!mp->lit)
tilde = stktell(stkp)+(c+1);
else if(n==S_SLASH && mp->pattern==2)
#if 0
goto pattern;
#else
{
if(mp->quote || mp->lit)
goto pattern;
sfwrite(stkp,first,c+1);
first = fcseek(c+1);
c = stktell(stkp);
sh_lexskip(lp,RBRACE,0,ST_NESTED);
stkseek(stkp,c);
cp = fcseek(-1);
sfwrite(stkp,first,cp-first);
first=cp;
}
#endif
break;
case S_DOT:
if(*cp=='.' && mp->subcopy==1)
{
sfwrite(stkp,first,c);
sfputc(stkp,0);
dotdot = stktell(stkp);
cp = first = fcseek(c+2);
}
break;
}
}
done:
mp->sp = sp;
mp->dotdot = dotdot;
mp->quote = oldquote;
}
/*
* copy <str> to stack performing sub-expression substitutions
*/
static void mac_substitute(Mac_t *mp, register char *cp,char *str,register int subexp[],int subsize)
{
register int c,n;
register char *first=fcseek(0);
char *ptr;
Mac_t savemac;
Stk_t *stkp = mp->shp->stk;
n = stktell(stkp);
savemac = *mp;
mp->pattern = 3;
mp->split = 0;
mp->macsub++;
fcsopen(cp);
copyto(mp,0,0);
sfputc(stkp,0);
ptr = cp = strdup(stkptr(stkp,n));
stkseek(stkp,n);
*mp = savemac;
fcsopen(first);
first = cp;
while(1)
{
while((c= *cp++) && c!=ESCAPE);
if(c==0)
break;
if((n= *cp++)=='\\' || n==RBRACE || (n>='0' && n<='9' && (n-='0')<subsize))
{
c = cp-first-2;
if(c)
mac_copy(mp,first,c);
first=cp;
if(n=='\\' || n==RBRACE)
{
first--;
continue;
}
if((c=subexp[2*n])>=0)
{
if((n=subexp[2*n+1]-c)>0)
mac_copy(mp,str+c,n);
}
}
else if(n==0)
break;
}
if(n=cp-first-1)
mac_copy(mp,first,n);
free(ptr);
}
#if SHOPT_FILESCAN
#define MAX_OFFSETS (sizeof(shp->offsets)/sizeof(shp->offsets[0]))
#define MAX_ARGN (32*1024)
/*
* compute the arguments $1 ... $n and $# from the current line as needed
* save line offsets in the offsets array.
*/
static char *getdolarg(Shell_t *shp, int n, int *size)
{
register int c=S_DELIM, d=shp->ifstable['\\'];
register unsigned char *first,*last,*cp = (unsigned char*)shp->cur_line;
register int m=shp->offsets[0],delim=0;
if(m==0)
return(0);
if(m<0)
m = 0;
else if(n<=m)
m = n-1;
else
m--;
if(m >= MAX_OFFSETS-1)
m = MAX_OFFSETS-2;
cp += shp->offsets[m+1];
n -= m;
shp->ifstable['\\'] = 0;
shp->ifstable[0] = S_EOF;
while(1)
{
if(c==S_DELIM)
while(shp->ifstable[*cp++]==S_SPACE);
first = --cp;
if(++m < MAX_OFFSETS)
shp->offsets[m] = (first-(unsigned char*)shp->cur_line);
while((c=shp->ifstable[*cp++])==0);
last = cp-1;
if(c==S_SPACE)
while((c=shp->ifstable[*cp++])==S_SPACE);
if(--n==0 || c==S_EOF)
{
if(last==first && c==S_EOF && (!delim || (m>1)))
{
n++;
m--;
}
break;
}
delim = (c==S_DELIM);
}
shp->ifstable['\\'] = d;
if(m > shp->offsets[0])
shp->offsets[0] = m;
if(n)
first = last = 0;
if(size)
*size = last-first;
return((char*)first);
}
#endif /* SHOPT_FILESCAN */
/*
* get the prefix after name reference resolution
*/
static char *prefix(Shell_t *shp, char *id)
{
Namval_t *np;
register char *sub=0, *cp = strchr(id,'.');
if(cp)
{
*cp = 0;
np = nv_search(id, shp->var_tree,0);
*cp = '.';
if(isastchar(cp[1]))
cp[1] = 0;
if(np && nv_isref(np))
{
size_t n;
char *sp;
shp->argaddr = 0;
while(nv_isref(np) && np->nvalue.cp)
{
sub = nv_refsub(np);
np = nv_refnode(np);
if(sub)
nv_putsub(np,sub,0,0L);
}