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
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* UNIX shell
*/
#include "defs.h"
#include <stropts.h>
extern BOOL chkid();
extern unsigned char *simple();
extern int mailchk;
static void setname(unsigned char *, int);
static void set_builtins_path();
static int patheq();
static void namwalk(struct namnod *);
static void dolocale();
struct namnod ps2nod =
{
(struct namnod *)NIL,
&acctnod,
(unsigned char *)ps2name
};
struct namnod cdpnod =
{
(struct namnod *)NIL,
(struct namnod *)NIL,
(unsigned char *)cdpname
};
struct namnod pathnod =
{
&mailpnod,
(struct namnod *)NIL,
(unsigned char *)pathname
};
struct namnod ifsnod =
{
&homenod,
&mailnod,
(unsigned char *)ifsname
};
struct namnod ps1nod =
{
&pathnod,
&ps2nod,
(unsigned char *)ps1name
};
struct namnod homenod =
{
&cdpnod,
(struct namnod *)NIL,
(unsigned char *)homename
};
struct namnod mailnod =
{
(struct namnod *)NIL,
(struct namnod *)NIL,
(unsigned char *)mailname
};
struct namnod mchknod =
{
&ifsnod,
&ps1nod,
(unsigned char *)mchkname
};
struct namnod acctnod =
{
(struct namnod *)NIL,
(struct namnod *)NIL,
(unsigned char *)acctname
};
struct namnod mailpnod =
{
(struct namnod *)NIL,
(struct namnod *)NIL,
(unsigned char *)mailpname
};
struct namnod *namep = &mchknod;
/* ======== variable and string handling ======== */
int
syslook(unsigned char *w, struct sysnod syswds[], int n)
{
int low;
int high;
int mid;
int cond;
if (w == 0 || *w == 0)
return(0);
low = 0;
high = n - 1;
while (low <= high)
{
mid = (low + high) / 2;
if ((cond = cf(w, syswds[mid].sysnam)) < 0)
high = mid - 1;
else if (cond > 0)
low = mid + 1;
else
return(syswds[mid].sysval);
}
return(0);
}
void
setlist(struct argnod *arg, int xp)
{
if (flags & exportflg)
xp |= N_EXPORT;
while (arg)
{
unsigned char *s = mactrim(arg->argval);
setname(s, xp);
arg = arg->argnxt;
if (flags & execpr)
{
prs(s);
if (arg)
blank();
else
newline();
}
}
}
static void
setname(unsigned char *argi, int xp) /* does parameter assignments */
{
unsigned char *argscan = argi;
struct namnod *n;
if (letter(*argscan))
{
while (alphanum(*argscan))
argscan++;
if (*argscan == '=')
{
*argscan = 0; /* make name a cohesive string */
n = lookup(argi);
*argscan++ = '=';
attrib(n, xp);
if (xp & N_ENVNAM)
{
n->namenv = n->namval = argscan;
if (n == &pathnod)
set_builtins_path();
}
else
assign(n, argscan);
dolocale(n->namid);
return;
}
}
}
void
replace(unsigned char **a, unsigned char *v)
{
free(*a);
*a = make(v);
}
void
dfault(struct namnod *n, unsigned char *v)
{
if (n->namval == 0)
assign(n, v);
}
void
assign(struct namnod *n, unsigned char *v)
{
if (n->namflg & N_RDONLY)
failed(n->namid, wtfailed);
#ifndef RES
else if (flags & rshflg)
{
if (n == &pathnod || eq(n->namid,"SHELL"))
failed(n->namid, restricted);
}
#endif
else if (n->namflg & N_FUNCTN)
{
func_unhash(n->namid);
freefunc(n);
n->namenv = 0;
n->namflg = N_DEFAULT;
}
if (n == &mchknod)
{
mailchk = stoi(v);
}
replace(&n->namval, v);
attrib(n, N_ENVCHG);
if (n == &pathnod)
{
zaphash();
set_dotpath();
set_builtins_path();
return;
}
if (flags & prompt)
{
if ((n == &mailpnod) || (n == &mailnod && mailpnod.namflg == N_DEFAULT))
setmail(n->namval);
}
}
static void
set_builtins_path()
{
unsigned char *path;
ucb_builtins = 0;
path = getpath("");
while (path && *path)
{
if (patheq(path, "/usr/ucb"))
{
ucb_builtins++;
break;
}
else if (patheq(path, "/usr/bin"))
break;
else if (patheq(path, "/bin"))
break;
else if (patheq(path, "/usr/5bin"))
break;
path = nextpath(path);
}
}
static int
patheq(unsigned char *component, char *dir)
{
unsigned char c;
for (;;)
{
c = *component++;
if (c == COLON)
c = '\0'; /* end of component of path */
if (c != *dir++)
return (0);
if (c == '\0')
return(1);
}
}
int
readvar(unsigned char **names)
{
struct fileblk fb;
struct fileblk *f = &fb;
unsigned char c[MULTI_BYTE_MAX+1];
int rc = 0;
struct namnod *n = lookup(*names++); /* done now to avoid storage mess */
unsigned char *rel = (unsigned char *)relstak();
unsigned char *oldstak;
unsigned char *pc, *rest;
int d;
push(f);
initf(dup(0));
/*
* If stdin is a pipe then this lseek(2) will fail with ESPIPE, so
* the read buffer size is set to 1 because we will not be able
* lseek(2) back towards the beginning of the file, so we have
* to read a byte at a time instead
*
*/
if (lseek(0, (off_t)0, SEEK_CUR) == -1)
f->fsiz = 1;
/*
* If stdin is a socket then this isastream(3C) will return 1, so
* the read buffer size is set to 1 because we will not be able
* lseek(2) back towards the beginning of the file, so we have
* to read a byte at a time instead
*
*/
if (isastream(0) == 1)
f->fsiz = 1;
/*
* strip leading IFS characters
*/
for (;;)
{
d = nextwc();
if(eolchar(d))
break;
rest = readw(d);
pc = c;
while(*pc++ = *rest++);
if(!anys(c, ifsnod.namval))
break;
}
oldstak = curstak();
for (;;)
{
if ((*names && anys(c, ifsnod.namval)) || eolchar(d))
{
if (staktop >= brkend)
growstak(staktop);
zerostak();
assign(n, absstak(rel));
setstak(rel);
if (*names)
n = lookup(*names++);
else
n = 0;
if (eolchar(d))
{
break;
}
else /* strip imbedded IFS characters */
while(1) {
d = nextwc();
if(eolchar(d))
break;
rest = readw(d);
pc = c;
while(*pc++ = *rest++);
if(!anys(c, ifsnod.namval))
break;
}
}
else
{
if(d == '\\') {
d = readwc();
rest = readw(d);
while(d = *rest++) {
if (staktop >= brkend)
growstak(staktop);
pushstak(d);
}
oldstak = staktop;
}
else
{
pc = c;
while(d = *pc++) {
if (staktop >= brkend)
growstak(staktop);
pushstak(d);
}
if(!anys(c, ifsnod.namval))
oldstak = staktop;
}
d = nextwc();
if (eolchar(d))
staktop = oldstak;
else
{
rest = readw(d);
pc = c;
while(*pc++ = *rest++);
}
}
}
while (n)
{
assign(n, (unsigned char *)nullstr);
if (*names)
n = lookup(*names++);
else
n = 0;
}
if (eof)
rc = 1;
if (isastream(0) != 1)
/*
* If we are reading on a stream do not attempt to
* lseek(2) back towards the start because this is
* logically meaningless, but there is nothing in
* the standards to pervent the stream implementation
* from attempting it and breaking our code here
*
*/
lseek(0, (off_t)(f->nxtoff - f->endoff), SEEK_CUR);
pop();
return(rc);
}
void
assnum(unsigned char **p, long i)
{
int j = ltos(i);
replace(p, &numbuf[j]);
}
unsigned char *
make(v)
unsigned char *v;
{
unsigned char *p;
if (v)
{
movstr(v, p = (unsigned char *)alloc(length(v)));
return(p);
}
else
return(0);
}
struct namnod *
lookup(unsigned char *nam)
{
struct namnod *nscan = namep;
struct namnod **prev;
int LR;
if (!chkid(nam))
failed(nam, notid);
while (nscan)
{
if ((LR = cf(nam, nscan->namid)) == 0)
return(nscan);
else if (LR < 0)
prev = &(nscan->namlft);
else
prev = &(nscan->namrgt);
nscan = *prev;
}
/*
* add name node
*/
nscan = (struct namnod *)alloc(sizeof *nscan);
nscan->namlft = nscan->namrgt = (struct namnod *)NIL;
nscan->namid = make(nam);
nscan->namval = 0;
nscan->namflg = N_DEFAULT;
nscan->namenv = 0;
return(*prev = nscan);
}
BOOL
chkid(nam)
unsigned char *nam;
{
unsigned char *cp = nam;
if (!letter(*cp))
return(FALSE);
else
{
while (*++cp)
{
if (!alphanum(*cp))
return(FALSE);
}
}
return(TRUE);
}
static void (*namfn)();
void
namscan(void (*fn)())
{
namfn = fn;
namwalk(namep);
}
static void
namwalk(struct namnod *np)
{
if (np)
{
namwalk(np->namlft);
(*namfn)(np);
namwalk(np->namrgt);
}
}
void
printnam(struct namnod *n)
{
unsigned char *s;
sigchk();
if (n->namflg & N_FUNCTN)
{
struct fndnod *f = fndptr(n->namenv);
prs_buff(n->namid);
prs_buff("(){\n");
if (f != NULL)
prf(f->fndval);
prs_buff("\n}\n");
}
else if (s = n->namval)
{
prs_buff(n->namid);
prc_buff('=');
prs_buff(s);
prc_buff(NL);
}
}
static int namec;
void
printro(struct namnod *n)
{
if (n->namflg & N_RDONLY)
{
prs_buff(_gettext(readonly));
prc_buff(SPACE);
prs_buff(n->namid);
prc_buff(NL);
}
}
void
printexp(struct namnod *n)
{
if (n->namflg & N_EXPORT)
{
prs_buff(_gettext(export));
prc_buff(SPACE);
prs_buff(n->namid);
prc_buff(NL);
}
}
void
setup_env(void)
{
unsigned char **e = environ;
while (*e)
setname(*e++, N_ENVNAM);
}
static unsigned char **argnam;
static void
countnam(struct namnod *n)
{
if (n->namval)
namec++;
}
static void
pushnam(struct namnod *n)
{
int flg = n->namflg;
unsigned char *p;
unsigned char *namval;
if (((flg & N_ENVCHG) && (flg & N_EXPORT)) || (flg & N_FUNCTN))
namval = n->namval;
else {
/* Discard Local variable in child process */
if (!(flg & ~N_ENVCHG)) {
n->namflg = 0;
n->namenv = 0;
if (n->namval) {
/* Release for re-use */
free(n->namval);
n->namval = (unsigned char *)NIL;
}
}
namval = n->namenv;
}
if (namval)
{
p = movstrstak(n->namid, staktop);
p = movstrstak("=", p);
p = movstrstak(namval, p);
*argnam++ = getstak(p + 1 - (unsigned char *)(stakbot));
}
}
unsigned char **
local_setenv()
{
unsigned char **er;
namec = 0;
namscan(countnam);
argnam = er = (unsigned char **)getstak(namec * BYTESPERWORD + BYTESPERWORD);
namscan(pushnam);
*argnam++ = 0;
return(er);
}
struct namnod *
findnam(nam)
unsigned char *nam;
{
struct namnod *nscan = namep;
int LR;
if (!chkid(nam))
return(0);
while (nscan)
{
if ((LR = cf(nam, nscan->namid)) == 0)
return(nscan);
else if (LR < 0)
nscan = nscan->namlft;
else
nscan = nscan->namrgt;
}
return(0);
}
void
unset_name(unsigned char *name)
{
struct namnod *n;
unsigned char call_dolocale = 0;
if (n = findnam(name))
{
if (n->namflg & N_RDONLY)
failed(name, wtfailed);
if (n == &pathnod ||
n == &ifsnod ||
n == &ps1nod ||
n == &ps2nod ||
n == &mchknod)
{
failed(name, badunset);
}
#ifndef RES
if ((flags & rshflg) && eq(name, "SHELL"))
failed(name, restricted);
#endif
if (n->namflg & N_FUNCTN)
{
func_unhash(name);
freefunc(n);
}
else
{
call_dolocale++;
free(n->namval);
free(n->namenv);
}
n->namval = n->namenv = 0;
n->namflg = N_DEFAULT;
if (call_dolocale)
dolocale(name);
if (flags & prompt)
{
if (n == &mailpnod)
setmail(mailnod.namval);
else if (n == &mailnod && mailpnod.namflg == N_DEFAULT)
setmail(0);
}
}
}
/*
* The environment variables which affect locale.
* Note: if all names in this list do not begin with 'L',
* you MUST modify dolocale(). Also, be sure that the
* fake_env has the same number of elements as localevar.
*/
static char *localevar[] = {
"LC_ALL",
"LC_CTYPE",
"LC_MESSAGES",
"LANG",
0
};
static char *fake_env[] = {
0,
0,
0,
0,
0
};
/*
* If name is one of several special variables which affect the locale,
* do a setlocale().
*/
static void
dolocale(nm)
char *nm;
{
char **real_env;
struct namnod *n;
int lv, fe;
int i;
/*
* Take advantage of fact that names of these vars all start
* with 'L' to avoid unnecessary work.
* Do locale processing only if /usr is mounted.
*/
if ((*nm != 'L') || !localedir_exists ||
(!(eq(nm, "LC_ALL") || eq(nm, "LC_CTYPE") ||
eq(nm, "LANG") || eq(nm, "LC_MESSAGES"))))
return;
/*
* setlocale() has all the smarts built into it, but
* it works by examining the environment. Unfortunately,
* when you set an environment variable, the shell does
* not modify its own environment; it just remembers that the
* variable needs to be exported to any children. We hack around
* this by consing up a fake environment for the use of setlocale()
* and substituting it for the real env before calling setlocale().
*/
/*
* Build the fake environment.
* Look up the value of each of the special environment
* variables, and put their value into the fake environment,
* if they are exported.
*/
for (lv = 0, fe = 0; localevar[lv]; lv++) {
if ((n = findnam(localevar[lv]))) {
char *p, *q;
if (!n->namval)
continue;
fake_env[fe++] = p = alloc(length(localevar[lv])
+ length(n->namval) + 2);
/* copy name */
q = localevar[lv];
while (*q)
*p++ = *q++;
*p++ = '=';
/* copy value */
q = (char*)(n->namval);
while (*q)
*p++ = *q++;
*p++ = '\0';
}
}
fake_env[fe] = (char *)0;
/*
* Switch fake env for real and call setlocale().
*/
real_env = (char **)environ;
environ = (unsigned char **)fake_env;
if (setlocale(LC_ALL, "") == NULL)
prs(_gettext(badlocale));
/*
* Switch back and tear down the fake env.
*/
environ = (unsigned char **)real_env;
for (i = 0; i < fe; i++) {
free(fake_env[i]);
fake_env[i] = (char *)0;
}
}
|