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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* UNIX shell
*/
#include "defs.h"
#include "sym.h"
#include <wait.h>
static unsigned char quote; /* used locally */
static unsigned char quoted; /* used locally */
static int getch();
static void comsubst(int);
static void flush(int);
static void
copyto(unsigned char endch, int trimflag)
/* trimflag - flag to check if argument will be trimmed */
{
unsigned int c;
unsigned int d;
unsigned char *pc;
while ((c = getch(endch, trimflag)) != endch && c)
if (quote) {
if(c == '\\') { /* don't interpret next character */
if (staktop >= brkend)
growstak(staktop);
pushstak(c);
d = readwc();
if(!escchar(d)) { /* both \ and following
character are quoted if next
character is not $, `, ", or \*/
if (staktop >= brkend)
growstak(staktop);
pushstak('\\');
if (staktop >= brkend)
growstak(staktop);
pushstak('\\');
pc = readw(d);
/* push entire multibyte char */
while(*pc) {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc++);
}
} else {
pc = readw(d);
/* d might be NULL */
/* Evenif d is NULL, we have to save it */
if (*pc) {
while (*pc) {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc++);
}
} else {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc);
}
}
} else { /* push escapes onto stack to quote characters */
pc = readw(c);
if (staktop >= brkend)
growstak(staktop);
pushstak('\\');
while(*pc) {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc++);
}
}
} else if(c == '\\') {
c = readwc(); /* get character to be escaped */
if (staktop >= brkend)
growstak(staktop);
pushstak('\\');
pc = readw(c);
/* c might be NULL */
/* Evenif c is NULL, we have to save it */
if (*pc) {
while (*pc) {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc++);
}
} else {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc);
}
} else {
pc = readw(c);
while (*pc) {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc++);
}
}
if (staktop >= brkend)
growstak(staktop);
zerostak();
if (c != endch)
error(badsub);
}
static void
skipto(unsigned char endch)
{
/*
* skip chars up to }
*/
unsigned int c;
while ((c = readwc()) && c != endch)
{
switch (c)
{
case SQUOTE:
skipto(SQUOTE);
break;
case DQUOTE:
skipto(DQUOTE);
break;
case DOLLAR:
if (readwc() == BRACE)
skipto('}');
}
}
if (c != endch)
error(badsub);
}
static
int getch(endch, trimflag)
unsigned char endch;
int trimflag; /* flag to check if an argument is going to be trimmed, here document
output is never trimmed
*/
{
unsigned int d;
int atflag; /* flag to check if $@ has already been seen within double
quotes */
retry:
d = readwc();
if (!subchar(d))
return(d);
if (d == DOLLAR)
{
unsigned int c;
if ((c = readwc(), dolchar(c)))
{
struct namnod *n = (struct namnod *)NIL;
int dolg = 0;
BOOL bra;
BOOL nulflg;
unsigned char *argp, *v;
unsigned char idb[2];
unsigned char *id = idb;
if (bra = (c == BRACE))
c = readwc();
if (letter(c))
{
argp = (unsigned char *)relstak();
while (alphanum(c))
{
if (staktop >= brkend)
growstak(staktop);
pushstak(c);
c = readwc();
}
if (staktop >= brkend)
growstak(staktop);
zerostak();
n = lookup(absstak(argp));
setstak(argp);
if (n->namflg & N_FUNCTN)
error(badsub);
v = n->namval;
id = (unsigned char *)n->namid;
peekc = c | MARK;
}
else if (digchar(c))
{
*id = c;
idb[1] = 0;
if (astchar(c))
{
if(c == '@' && !atflag && quote) {
quoted--;
atflag = 1;
}
dolg = 1;
c = '1';
}
c -= '0';
v = ((c == 0) ? cmdadr : ((int)c <= dolc) ? dolv[c] : (unsigned char *)(dolg = 0));
}
else if (c == '$')
v = pidadr;
else if (c == '!')
v = pcsadr;
else if (c == '#')
{
itos(dolc);
v = numbuf;
}
else if (c == '?')
{
itos(retval);
v = numbuf;
}
else if (c == '-')
v = flagadr;
else if (bra)
error(badsub);
else
goto retry;
c = readwc();
if (c == ':' && bra) /* null and unset fix */
{
nulflg = 1;
c = readwc();
}
else
nulflg = 0;
if (!defchar(c) && bra)
error(badsub);
argp = 0;
if (bra)
{
if (c != '}')
{
argp = (unsigned char *)relstak();
if ((v == 0 || (nulflg && *v == 0)) ^ (setchar(c)))
copyto('}', trimflag);
else
skipto('}');
argp = absstak(argp);
}
}
else
{
peekc = c | MARK;
c = 0;
}
if (v && (!nulflg || *v))
{
if (c != '+')
{
for (;;)
{
if (*v == 0 && quote) {
if (staktop >= brkend)
growstak(staktop);
pushstak('\\');
if (staktop >= brkend)
growstak(staktop);
pushstak('\0');
} else {
while (c = *v) {
wchar_t wc;
int length;
if ((length = mbtowc(&wc, (char *)v, MB_LEN_MAX)) <= 0)
length = 1;
if(quote || (c == '\\' && trimflag)) {
if (staktop >= brkend)
growstak(staktop);
pushstak('\\');
}
while(length-- > 0) {
if (staktop >= brkend)
growstak(staktop);
pushstak(*v++);
}
}
}
if (dolg == 0 || (++dolg > dolc))
break;
else /* $* and $@ expansion */
{
v = dolv[dolg];
if(*id == '*' && quote) {
/* push quoted space so that " $* " will not be broken into separate arguments */
if (staktop >= brkend)
growstak(staktop);
pushstak('\\');
}
if (staktop >= brkend)
growstak(staktop);
pushstak(' ');
}
}
}
}
else if (argp)
{
if (c == '?') {
if(trimflag)
trim(argp);
failed(id, *argp ? (const char *)argp :
badparam);
}
else if (c == '=')
{
if (n)
{
int strlngth = staktop - stakbot;
unsigned char *savptr = fixstak();
unsigned char *newargp;
/*
* copy word onto stack, trim it, and then
* do assignment
*/
usestak();
while(c = *argp) {
wchar_t wc;
int len;
if ((len = mbtowc(&wc, (char *)argp, MB_LEN_MAX)) <= 0)
len = 1;
if(c == '\\' && trimflag) {
argp++;
if (*argp == 0) {
argp++;
continue;
}
if ((len = mbtowc(&wc, (char *)argp, MB_LEN_MAX)) <= 0)
len = 1;
}
while(len-- > 0) {
if (staktop >= brkend)
growstak(staktop);
pushstak(*argp++);
}
}
newargp = fixstak();
assign(n, newargp);
tdystak(savptr);
(void) memcpystak(stakbot, savptr, strlngth);
staktop = stakbot + strlngth;
}
else
error(badsub);
}
}
else if (flags & setflg)
failed(id, unset);
goto retry;
}
else
peekc = c | MARK;
}
else if (d == endch)
return(d);
else if (d == SQUOTE)
{
comsubst(trimflag);
goto retry;
}
else if (d == DQUOTE && trimflag)
{
if(!quote) {
atflag = 0;
quoted++;
}
quote ^= QUOTE;
goto retry;
}
return(d);
}
unsigned char *
macro(as)
unsigned char *as;
{
/*
* Strip "" and do $ substitution
* Leaves result on top of stack
*/
BOOL savqu = quoted;
unsigned char savq = quote;
struct filehdr fb;
push(&fb);
estabf(as);
usestak();
quote = 0;
quoted = 0;
copyto(0, 1);
pop();
if (quoted && (stakbot == staktop)) {
if (staktop >= brkend)
growstak(staktop);
pushstak('\\');
if (staktop >= brkend)
growstak(staktop);
pushstak('\0');
/*
* above is the fix for *'.c' bug
*/
}
quote = savq;
quoted = savqu;
return(fixstak());
}
/* Save file descriptor for command substitution */
int savpipe = -1;
static void
comsubst(int trimflag)
/* trimflag - used to determine if argument will later be trimmed */
{
/*
* command substn
*/
struct fileblk cb;
unsigned int d;
int strlngth = staktop - stakbot;
unsigned char *oldstaktop;
unsigned char *savptr = fixstak();
unsigned char *pc;
usestak();
while ((d = readwc()) != SQUOTE && d) {
if(d == '\\') {
d = readwc();
if(!escchar(d) || (d == '"' && !quote)) {
/* trim quotes for `, \, or " if command substitution is within
double quotes */
if (staktop >= brkend)
growstak(staktop);
pushstak('\\');
}
}
pc = readw(d);
/* d might be NULL */
if (*pc) {
while (*pc) {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc++);
}
} else {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc);
}
}
{
unsigned char *argc;
argc = fixstak();
push(&cb);
estabf(argc); /* read from string */
}
{
struct trenod *t;
int pv[2];
/*
* this is done like this so that the pipe
* is open only when needed
*/
t = makefork(FPOU, cmd(EOFSYM, MTFLG | NLFLG ));
chkpipe(pv);
savpipe = pv[OTPIPE];
initf(pv[INPIPE]); /* read from pipe */
execute(t, XEC_NOSTOP, (int)(flags & errflg), 0, pv);
close(pv[OTPIPE]);
savpipe = -1;
}
tdystak(savptr);
(void) memcpystak(stakbot, savptr, strlngth);
oldstaktop = staktop = stakbot + strlngth;
while (d = readwc()) {
if(quote || (d == '\\' && trimflag)) {
unsigned char *rest;
/* quote output from command subst. if within double
quotes or backslash part of output */
rest = readw(d);
if (staktop >= brkend)
growstak(staktop);
pushstak('\\');
while(d = *rest++) {
/* Pick up all of multibyte character */
if (staktop >= brkend)
growstak(staktop);
pushstak(d);
}
}
else {
pc = readw(d);
while (*pc) {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc++);
}
}
}
{
extern pid_t parent;
int stat;
int rc;
int ret = 0;
while ((ret = waitpid(parent,&stat,0)) != parent) {
/* break out if waitpid(2) has failed */
if (ret == -1)
break;
}
if (WIFEXITED(stat))
rc = WEXITSTATUS(stat);
else
rc = (WTERMSIG(stat) | SIGFLG);
if (rc && (flags & errflg))
exitsh(rc);
exitval = rc;
flags |= eflag;
exitset();
}
while (oldstaktop != staktop)
{ /* strip off trailing newlines from command substitution only */
if ((*--staktop) != NL)
{
++staktop;
break;
} else if(quote)
staktop--; /* skip past backslashes if quoting */
}
pop();
}
#define CPYSIZ 512
void
subst(int in, int ot)
{
unsigned int c;
struct fileblk fb;
int count = CPYSIZ;
unsigned char *pc;
push(&fb);
initf(in);
/*
* DQUOTE used to stop it from quoting
*/
while (c = (getch(DQUOTE, 0))) /* read characters from here document
and interpret them */
{
if(c == '\\') {
c = readwc(); /* check if character in here document is
escaped */
if(!escchar(c) || c == '"') {
if (staktop >= brkend)
growstak(staktop);
pushstak('\\');
}
}
pc = readw(c);
/* c might be NULL */
if (*pc) {
while (*pc) {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc++);
}
} else {
if (staktop >= brkend)
growstak(staktop);
pushstak(*pc);
}
if (--count == 0)
{
flush(ot);
count = CPYSIZ;
}
}
flush(ot);
pop();
}
static void
flush(int ot)
{
write(ot, stakbot, staktop - stakbot);
if (flags & execpr)
write(output, stakbot, staktop - stakbot);
staktop = stakbot;
}
|