2
* sh.dol.c: Variable substitutions
5
* Copyright (c) 1980, 1991 The Regents of the University of California.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
16
* 3. All advertising materials mentioning features or use of this software
17
* must display the following acknowledgement:
18
* This product includes software developed by the University of
19
* California, Berkeley and its contributors.
20
* 4. Neither the name of the University nor the names of its contributors
21
* may be used to endorse or promote products derived from this software
22
* without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45
* These routines perform variable substitution and quoting via ' and ".
46
* To this point these constructs have been preserved in the divided
47
* input words. Here we expand variables and turn quoting via ' and " into
48
* QUOTE bits on characters (which prevent further interpretation).
49
* If the `:q' modifier was applied during history expansion, then
50
* some QUOTEing may have occurred already, so we dont "trim()" here.
53
static int Dpeekc, Dpeekrd; /* Peeks for DgetC and Dreadc */
54
static Char *Dcp, **Dvp; /* Input vector for Dreadc */
58
#define unDgetC(c) Dpeekc = c
60
#define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */
63
* The following variables give the information about the current
64
* $ expansion, recording the current word position, the remaining
65
* words within this expansion, the count of remaining words, and the
66
* information about any : modifier which is being applied.
68
#define MAXWLEN (BUFSIZE - 4)
70
#define MAXMOD MAXWLEN /* This cannot overflow */
72
static Char *dolp; /* Remaining chars from this word */
73
static Char **dolnxt; /* Further words */
74
static int dolcnt; /* Count of further words */
76
static Char dolmod; /* : modifier character */
78
static Char dolmod[MAXMOD]; /* : modifier character */
79
static int dolnmod; /* Number of modifiers */
81
static int dolmcnt; /* :gx -> 10000, else 1 */
82
static int dolwcnt; /* :ax -> 10000, else 1 */
84
static void Dfix2 __P((Char **));
85
static Char *Dpack __P((Char *, Char *));
86
static int Dword __P((void));
87
static void dolerror __P((Char *));
88
static int DgetC __P((int));
89
static void Dgetdol __P((void));
90
static void fixDolMod __P((void));
91
static void setDolp __P((Char *));
92
static void unDredc __P((int));
93
static int Dredc __P((void));
94
static void Dtestq __P((int));
97
* Fix up the $ expansions and quotations in the
98
* argument list to command t.
102
register struct command *t;
109
/* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
110
for (pp = t->t_dcom; (p = *pp++) != NULL;) {
112
if (cmap(*p, _DOL | QUOTES)) { /* $, \, ', ", ` */
113
Dfix2(t->t_dcom); /* found one */
124
* $ substitute one word, for i/o redirection
138
setname(short2str(cp));
139
stderror(ERR_NAME | ERR_AMBIG);
141
cp = Strsave(gargv[0]);
142
blkfree(gargv), gargv = 0;
147
* Subroutine to do actual fixing after state initialization.
153
ginit(); /* Initialize glob's area pointers */
155
Dcp = STRNULL; /* Setup input vector for Dreadc */
157
unDredc(0); /* Clear out any old peeks (at error) */
159
dolcnt = 0; /* Clear out residual $ expands (...) */
165
* Pack up more characters in this word
172
register int i = MAXWLEN - (int) (wp - wbuf);
195
if (cmap(c, _SP | _NL | _QF | _QB)) { /* sp \t\n'"` */
204
stderror(ERR_WTOOLONG);
210
* Get a word. This routine is analogous to the routine
211
* word() in sh.lex.c for the main lexical input. One difference
212
* here is that we don't get a newline to terminate our expansion.
213
* Rather, DgetC will return a DEOF when we hit the end-of-input.
220
register Char *wp = wbuf;
221
register int i = MAXWLEN;
222
register bool dolflg;
223
bool sofar = 0, done = 0;
233
/* finish this word and catch the code above the next time */
248
/* We preserve ` quotations which are done yet later */
249
*wp++ = (Char) c, --i;
254
* Note that DgetC never returns a QUOTES character from an
255
* expansion, so only true input quotes will get us here or out.
258
dolflg = c1 == '"' ? DODOL : 0;
263
if (c == '\n' || c == DEOF)
264
stderror(ERR_UNMATCHED, c1);
265
if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE)) {
266
if ((wp[-1] & TRIM) == '\\')
271
stderror(ERR_WTOOLONG);
276
* Leave any `s alone for later. Other chars are all
277
* quoted, thus `...` can tell it was within "...".
279
*wp++ = c == '`' ? '`' : c | QUOTE;
283
/* Prevent all further interpretation */
288
/* Leave all text alone for later */
297
*wp++ = '`' /* i--; eliminated */;
299
if ((wp = Dpack(wbuf, wp)) == NULL)
304
* Avoid a nasty message from the RTU 4.1A & RTU 5.0 compiler concerning
305
* the "overuse of registers". According to the compiler release notes,
306
* incorrect code may be produced unless the offending expression is
307
* rewritten. Therefore, we can't just ignore it, DAS DEC-90.
310
i -= (int) (wp - wbuf);
311
#else /* !masscomp */
312
i = MAXWLEN - (int) (wp - wbuf);
313
#endif /* masscomp */
319
c = DgetC(0); /* No $ subst! */
320
if (c == '\n' || c == DEOF) {
333
if ((wp = Dpack(wbuf, wp)) == NULL)
338
* Avoid a nasty message from the RTU 4.1A & RTU 5.0 compiler concerning
339
* the "overuse of registers". According to the compiler release notes,
340
* incorrect code may be produced unless the offending expression is
341
* rewritten. Therefore, we can't just ignore it, DAS DEC-90.
344
i -= (int) (wp - wbuf);
345
#else /* !masscomp */
346
i = MAXWLEN - (int) (wp - wbuf);
347
#endif /* masscomp */
352
/* Really NOTREACHED */
358
* Get a character, performing $ substitution unless flag is 0.
359
* Any QUOTES character which is returned from a $ expansion is
360
* QUOTEd so that it will not be recognized above.
369
if ((c = Dpeekc) != 0) {
374
c = *lap++ & (QUOTE | TRIM);
385
if ((c = *dolp++ & (QUOTE | TRIM)) != 0)
400
if (c == '$' && flag) {
407
static Char *nulvec[] = { NULL };
408
static struct varent nulargv = {nulvec, STRargv, VAR_READWRITE,
409
{ NULL, NULL, NULL }, 0 };
415
setname(short2str(s));
416
stderror(ERR_NAME | ERR_RANGE);
420
* Handle the multitudinous $ expansion forms.
427
register struct varent *vp = NULL;
428
Char name[4 * MAXVARLEN + 1];
430
int subscr = 0, lwb = 1, upb = 0;
431
bool dimen = 0, bitset = 0, length = 0;
434
static Char *dolbang = NULL;
437
dolmod = dolmcnt = dolwcnt = 0;
439
dolnmod = dolmcnt = dolwcnt = 0;
443
c = DgetC(0); /* sc is { to take } later */
444
if ((c & TRIM) == '#')
445
dimen++, c = DgetC(0); /* $# takes dimension */
447
bitset++, c = DgetC(0); /* $? tests existence */
449
length++, c = DgetC(0); /* $% returns length in chars */
453
if (dimen || bitset || length)
454
stderror(ERR_SYNTAX);
457
xfree((ptr_t) dolbang);
458
setDolp(dolbang = putn(backpid));
463
if (dimen || bitset || length)
464
stderror(ERR_SYNTAX);
469
/* Coherent compiler doesn't allow case-labels that are not
470
constant-expressions */
473
#else /* !SHORT_STRINGS */
476
#else /* !COHERENT */
480
stderror(ERR_NOTALLOWED, "$?<");
482
stderror(ERR_NOTALLOWED, "$#<");
484
stderror(ERR_NOTALLOWED, "$%<");
487
sigmask_t omask = sigsetmask(sigblock(0) & ~sigmask(SIGINT));
489
(void) sigrelse(SIGINT);
491
for (np = wbuf; force_read(OLDSTD, &tnp, 1) == 1; np++) {
492
*np = (unsigned char) tnp;
493
if (np >= &wbuf[BUFSIZE - 1])
494
stderror(ERR_LTOOLONG);
500
(void) sigsetmask(omask);
502
(void) sighold(SIGINT);
508
* KLUDGE: dolmod is set here because it will cause setDolp to call
509
* domod and thus to copy wbuf. Otherwise setDolp would use it
510
* directly. If we saved it ourselves, no one would know when to free
511
* it. The actual function of the 'q' causes filename expansion not to
512
* be done on the interpolated value.
515
* If we do that, then other modifiers don't work.
516
* in addition, let the user specify :q if wanted
519
/*old*/ dolmod = 'q';
520
/*new*/ dolmod[dolnmod++] = 'q';
529
(void) Strcpy(name, STRargv);
531
subscr = -1; /* Prevent eating [...] */
536
np = dimen ? STRargv : (bitset ? STRstatus : NULL);
539
(void) Strcpy(name, np);
541
subscr = -1; /* Prevent eating [...] */
546
stderror(ERR_SYNTAX);
553
stderror(ERR_NOTALLOWED, "$#<num>");
556
subscr = subscr * 10 + c - '0';
558
} while (Isdigit(c));
561
dolerror(vp->v_name);
566
dolp = dolzero ? STR1 : STR0;
570
stderror(ERR_DOLZERO);
573
length = Strlen(ffile);
586
stderror(ERR_NOTALLOWED, "$?<num>");
588
stderror(ERR_NOTALLOWED, "$%<num>");
598
np = dimen ? STRargv : (bitset ? STRstatus : NULL);
601
(void) Strcpy(name, np);
603
subscr = -1; /* Prevent eating [...] */
608
stderror(ERR_VARALNUM);
615
if (np >= &name[MAXVARLEN])
616
stderror(ERR_VARTOOLONG);
623
dolp = (vp || getenv(short2str(name))) ? STR1 : STR0;
627
np = str2short(getenv(short2str(name)));
637
upb = blklen(vp->vec);
638
if (dimen == 0 && subscr == 0 && c == '[') {
641
c = DgetC(DODOL); /* Allow $ expand within [ ] */
644
if (c == '\n' || c == DEOF)
646
if (np >= &name[sizeof(name) / sizeof(Char) - 2])
647
stderror(ERR_VARTOOLONG);
651
if (dolp || dolcnt) /* $ exp must end before ] */
652
stderror(ERR_EXPORD);
654
stderror(ERR_SYNTAX);
658
for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0')
660
if ((i < 0 || i > upb) && !any("-*", *np)) {
661
dolerror(vp->v_name);
666
upb = lwb, np = STRstar;
671
stderror(ERR_MISSING, '-');
673
register int i = upb;
679
i = i * 10 + *np++ - '0';
680
if (i < 0 || i > upb) {
681
dolerror(vp->v_name);
692
dolerror(vp->v_name);
698
stderror(ERR_SYNTAX);
710
Char *cp = putn(upb - lwb + 1);
712
/* this is a kludge. It prevents Dgetdol() from */
713
/* pushing erroneous ${#<error> values into the labuf. */
719
stderror(ERR_MISSING, '}');
730
for (i = lwb - 1, length = 0; i < upb; i++)
731
length += Strlen(vp->vec[i]);
733
/* We don't want that, since we can always compute it by adding $#xxx */
734
length += i - 1; /* Add the number of spaces in */
743
dolnxt = &vp->vec[lwb - 1];
744
dolcnt = upb - lwb + 1;
750
stderror(ERR_MISSING, '}');
764
c = DgetC(0), dolmcnt = 1, dolwcnt = 1;
765
if (c == 'g' || c == 'a') {
772
if ((c == 'g' && dolmcnt != 10000) ||
773
(c == 'a' && dolwcnt != 10000)) {
781
if (c == 's') { /* [eichin:19910926.0755EST] */
783
int delim = DgetC(0);
784
dolmod[dolnmod++] = (Char) c;
785
dolmod[dolnmod++] = (Char) delim;
787
if (!delim || letter(delim)
788
|| Isdigit(delim) || any(" \t\n", delim)) {
789
seterror(ERR_BADSUBST);
792
while ((c = DgetC(0)) != (-1)) {
793
dolmod[dolnmod++] = (Char) c;
794
if(c == delim) delimcnt--;
798
seterror(ERR_BADSUBST);
803
if (!any("luhtrqxes", c))
804
stderror(ERR_BADMOD, c);
806
dolmod[dolnmod++] = (Char) c;
814
while ((c = DgetC(0)) == ':');
832
if (dolmod == 0 || dolmcnt == 0) {
834
if (dolnmod == 0 || dolmcnt == 0) {
840
dp = domod(cp, dolmod);
842
dp = cp = Strsave(cp);
843
for (i = 0; i < dolnmod; i++) {
844
/* handle s// [eichin:19910926.0510EST] */
845
if(dolmod[i] == 's') {
847
Char *lhsub, *rhsub, *np;
848
size_t lhlen = 0, rhlen = 0;
852
if (!delim || letter(delim)
853
|| Isdigit(delim) || any(" \t\n", delim)) {
854
seterror(ERR_BADSUBST);
857
lhsub = &dolmod[++i];
858
while(dolmod[i] != delim && dolmod[++i]) {
862
rhsub = &dolmod[++i];
863
while(dolmod[i] != delim && dolmod[++i]) {
871
dp = Strstr(cp, lhsub);
873
np = (Char *) xmalloc((size_t)
874
((Strlen(cp) + 1 - lhlen + rhlen) *
876
(void) Strncpy(np, cp, (size_t) (dp - cp));
877
(void) Strcpy(np + (dp - cp), rhsub);
878
(void) Strcpy(np + (dp - cp) + rhlen, dp + lhlen);
884
/* should this do a seterror? */
888
while (dolwcnt == 10000);
890
* restore dolmod for additional words
892
dolmod[i] = rhsub[-1] = (Char) delim;
903
if ((dp = domod(cp, dolmod[i])) != NULL) {
905
if (Strcmp(cp, dp) == 0) {
918
while (dolwcnt == 10000);
960
if ((c = Dpeekrd) != 0) {
964
if (Dcp && (c = *Dcp++))
965
return (c & (QUOTE | TRIM));
984
* Form a shell temporary file (in unit 0) from the words
985
* of the shell input up to EOF or a line the same as "term".
986
* Unit 0 should have been closed before this call.
994
Char obuf[BUFSIZE], lbuf[BUFSIZE], mbuf[BUFSIZE];
995
int ocnt, lcnt, mcnt;
996
register Char *lbp, *obp, *mbp;
1001
tmp = short2str(shtemp);
1004
if (creat(tmp, 0600) < 0)
1005
stderror(ERR_SYSTEM, tmp, strerror(errno));
1009
# define O_TEMPORARY 0
1011
if (open(tmp, O_RDWR|O_CREAT|O_TEMPORARY, 0600) < 0) {
1016
stderror(ERR_SYSTEM, tmp, strerror(errno));
1018
(void) unlink(tmp); /* 0 0 inode! */
1038
c = readc(1); /* 1 -> Want EOF returns */
1039
if (c < 0 || c == '\n')
1041
if ((c &= TRIM) != 0) {
1045
stderror(ERR_NAME | ERR_OVERFLOW);
1052
* Check for EOF or compare to terminator -- before expansion
1054
if (c < 0 || eq(lbuf, term)) {
1055
(void) write(0, short2str(obuf), (size_t) (BUFSIZE - ocnt));
1056
(void) lseek(0, (off_t) 0, L_SET);
1062
* If term was quoted or -n just pass it on
1064
if (quoted || noexec) {
1067
for (lbp = lbuf; (c = *lbp++) != 0;) {
1070
(void) write(0, short2str(obuf), BUFSIZE);
1079
* Term wasn't quoted so variable and then command expand the input
1090
if ((c &= TRIM) == 0)
1092
/* \ quotes \ $ ` here */
1095
if (!any("$\\`", c))
1096
unDgetC(c | QUOTE), c = '\\';
1103
stderror(ERR_NAME | ERR_OVERFLOW);
1109
* If any ` in line do command substitution
1112
if (Strchr(mbp, '`') != NULL) {
1114
* 1 arg to dobackp causes substitution to be literal. Words are
1115
* broken only at newlines so that all blanks and tabs are
1116
* preserved. Blank lines (null words) are not discarded.
1118
vp = dobackp(mbuf, 1);
1121
/* Setup trivial vector similar to return of dobackp */
1122
Dv[0] = mbp, Dv[1] = NULL, vp = Dv;
1125
* Resurrect the words from the command substitution each separated by
1126
* a newline. Note that the last newline of a command substitution
1127
* will have been discarded, but we put a newline after the last word
1128
* because this represents the newline after the last input line!
1131
for (mbp = *vp; *mbp; mbp++) {
1132
*obp++ = *mbp & TRIM;
1134
(void) write(0, short2str(obuf), BUFSIZE);
1141
(void) write(0, short2str(obuf), BUFSIZE);
1147
blkfree(pargv), pargv = 0;