-
Notifications
You must be signed in to change notification settings - Fork 6
/
c_ksh.c
1428 lines (1325 loc) · 30.5 KB
/
c_ksh.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
/* $OpenBSD: c_ksh.c,v 1.63 2024/04/23 13:34:50 jsg Exp $ */
/*
* built-in Korn commands: c_*
*/
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "sh.h"
int
c_cd(char **wp)
{
int optc;
int physical = Flag(FPHYSICAL);
int cdnode; /* was a node from cdpath added in? */
int printpath = 0; /* print where we cd'd? */
int rval;
struct tbl *pwd_s, *oldpwd_s;
XString xs;
char *xp;
char *dir, *try, *pwd;
int phys_path;
char *cdpath;
char *fdir = NULL;
while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1)
switch (optc) {
case 'L':
physical = 0;
break;
case 'P':
physical = 1;
break;
case '?':
return 1;
}
wp += builtin_opt.optind;
if (Flag(FRESTRICTED)) {
bi_errorf("restricted shell - can't cd");
return 1;
}
pwd_s = global("PWD");
oldpwd_s = global("OLDPWD");
if (!wp[0]) {
/* No arguments - go home */
if ((dir = str_val(global("HOME"))) == null) {
bi_errorf("no home directory (HOME not set)");
return 1;
}
} else if (!wp[1]) {
/* One argument: - or dir */
dir = wp[0];
if (strcmp(dir, "-") == 0) {
dir = str_val(oldpwd_s);
if (dir == null) {
bi_errorf("no OLDPWD");
return 1;
}
printpath++;
}
} else if (!wp[2]) {
/* Two arguments - substitute arg1 in PWD for arg2 */
int ilen, olen, nlen, elen;
char *cp;
if (!current_wd[0]) {
bi_errorf("don't know current directory");
return 1;
}
/* substitute arg1 for arg2 in current path.
* if the first substitution fails because the cd fails
* we could try to find another substitution. For now
* we don't
*/
if ((cp = strstr(current_wd, wp[0])) == NULL) {
bi_errorf("bad substitution");
return 1;
}
ilen = cp - current_wd;
olen = strlen(wp[0]);
nlen = strlen(wp[1]);
elen = strlen(current_wd + ilen + olen) + 1;
fdir = dir = alloc(ilen + nlen + elen, ATEMP);
memcpy(dir, current_wd, ilen);
memcpy(dir + ilen, wp[1], nlen);
memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen);
printpath++;
} else {
bi_errorf("too many arguments");
return 1;
}
Xinit(xs, xp, PATH_MAX, ATEMP);
/* xp will have a bogus value after make_path() - set it to 0
* so that if it's used, it will cause a dump
*/
xp = NULL;
cdpath = str_val(global("CDPATH"));
do {
cdnode = make_path(current_wd, dir, &cdpath, &xs, &phys_path);
if (physical)
rval = chdir(try = Xstring(xs, xp) + phys_path);
else {
simplify_path(Xstring(xs, xp));
rval = chdir(try = Xstring(xs, xp));
}
} while (rval == -1 && cdpath != NULL);
if (rval == -1) {
if (cdnode)
bi_errorf("%s: bad directory", dir);
else
bi_errorf("%s - %s", try, strerror(errno));
afree(fdir, ATEMP);
return 1;
}
/* Clear out tracked aliases with relative paths */
flushcom(0);
/* Set OLDPWD (note: unsetting OLDPWD does not disable this
* setting in at&t ksh)
*/
if (current_wd[0])
/* Ignore failure (happens if readonly or integer) */
setstr(oldpwd_s, current_wd, KSH_RETURN_ERROR);
if (Xstring(xs, xp)[0] != '/') {
pwd = NULL;
} else
if (!physical || !(pwd = get_phys_path(Xstring(xs, xp))))
pwd = Xstring(xs, xp);
/* Set PWD */
if (pwd) {
char *ptmp = pwd;
set_current_wd(ptmp);
/* Ignore failure (happens if readonly or integer) */
setstr(pwd_s, ptmp, KSH_RETURN_ERROR);
} else {
set_current_wd(null);
pwd = Xstring(xs, xp);
/* XXX unset $PWD? */
}
if (printpath || cdnode)
shprintf("%s\n", pwd);
afree(fdir, ATEMP);
return 0;
}
int
c_pwd(char **wp)
{
int optc;
int physical = Flag(FPHYSICAL);
char *p, *freep = NULL;
while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1)
switch (optc) {
case 'L':
physical = 0;
break;
case 'P':
physical = 1;
break;
case '?':
return 1;
}
wp += builtin_opt.optind;
if (wp[0]) {
bi_errorf("too many arguments");
return 1;
}
p = current_wd[0] ? (physical ? get_phys_path(current_wd) : current_wd) :
NULL;
if (p && access(p, R_OK) == -1)
p = NULL;
if (!p) {
freep = p = ksh_get_wd(NULL, 0);
if (!p) {
bi_errorf("can't get current directory - %s",
strerror(errno));
return 1;
}
}
shprintf("%s\n", p);
afree(freep, ATEMP);
return 0;
}
int
c_print(char **wp)
{
#define PO_NL BIT(0) /* print newline */
#define PO_EXPAND BIT(1) /* expand backslash sequences */
#define PO_PMINUSMINUS BIT(2) /* print a -- argument */
#define PO_HIST BIT(3) /* print to history instead of stdout */
#define PO_COPROC BIT(4) /* printing to coprocess: block SIGPIPE */
int fd = 1;
int flags = PO_EXPAND|PO_NL;
char *s;
const char *emsg;
XString xs;
char *xp;
if (wp[0][0] == 'e') { /* echo command */
int nflags = flags;
/* A compromise between sysV and BSD echo commands:
* escape sequences are enabled by default, and
* -n, -e and -E are recognized if they appear
* in arguments with no illegal options (ie, echo -nq
* will print -nq).
* Different from sysV echo since options are recognized,
* different from BSD echo since escape sequences are enabled
* by default.
*/
wp += 1;
if (Flag(FPOSIX)) {
if (*wp && strcmp(*wp, "-n") == 0) {
flags &= ~PO_NL;
wp++;
}
} else {
while ((s = *wp) && *s == '-' && s[1]) {
while (*++s)
if (*s == 'n')
nflags &= ~PO_NL;
else if (*s == 'e')
nflags |= PO_EXPAND;
else if (*s == 'E')
nflags &= ~PO_EXPAND;
else
/* bad option: don't use
* nflags, print argument
*/
break;
if (*s)
break;
wp++;
flags = nflags;
}
}
} else {
int optc;
const char *options = "Rnprsu,";
while ((optc = ksh_getopt(wp, &builtin_opt, options)) != -1)
switch (optc) {
case 'R': /* fake BSD echo command */
flags |= PO_PMINUSMINUS;
flags &= ~PO_EXPAND;
options = "ne";
break;
case 'e':
flags |= PO_EXPAND;
break;
case 'n':
flags &= ~PO_NL;
break;
case 'p':
if ((fd = coproc_getfd(W_OK, &emsg)) < 0) {
bi_errorf("-p: %s", emsg);
return 1;
}
break;
case 'r':
flags &= ~PO_EXPAND;
break;
case 's':
flags |= PO_HIST;
break;
case 'u':
if (!*(s = builtin_opt.optarg))
fd = 0;
else if ((fd = check_fd(s, W_OK, &emsg)) < 0) {
bi_errorf("-u: %s: %s", s, emsg);
return 1;
}
break;
case '?':
return 1;
}
if (!(builtin_opt.info & GI_MINUSMINUS)) {
/* treat a lone - like -- */
if (wp[builtin_opt.optind] &&
strcmp(wp[builtin_opt.optind], "-") == 0)
builtin_opt.optind++;
} else if (flags & PO_PMINUSMINUS)
builtin_opt.optind--;
wp += builtin_opt.optind;
}
Xinit(xs, xp, 128, ATEMP);
while (*wp != NULL) {
int c;
s = *wp;
while ((c = *s++) != '\0') {
Xcheck(xs, xp);
if ((flags & PO_EXPAND) && c == '\\') {
int i;
switch ((c = *s++)) {
/* Oddly enough, \007 seems more portable than
* \a (due to HP-UX cc, Ultrix cc, old pcc's,
* etc.).
*/
case 'a': c = '\007'; break;
case 'b': c = '\b'; break;
case 'c': flags &= ~PO_NL;
continue; /* AT&T brain damage */
case 'f': c = '\f'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'v': c = 0x0B; break;
case '0':
/* Look for an octal number: can have
* three digits (not counting the
* leading 0). Truly burnt.
*/
c = 0;
for (i = 0; i < 3; i++) {
if (*s >= '0' && *s <= '7')
c = c*8 + *s++ - '0';
else
break;
}
break;
case '\0': s--; c = '\\'; break;
case '\\': break;
default:
Xput(xs, xp, '\\');
}
}
Xput(xs, xp, c);
}
if (*++wp != NULL)
Xput(xs, xp, ' ');
}
if (flags & PO_NL)
Xput(xs, xp, '\n');
if (flags & PO_HIST) {
Xput(xs, xp, '\0');
source->line++;
histsave(source->line, Xstring(xs, xp), 1);
Xfree(xs, xp);
} else {
int n, len = Xlength(xs, xp);
int opipe = 0;
/* Ensure we aren't killed by a SIGPIPE while writing to
* a coprocess. at&t ksh doesn't seem to do this (seems
* to just check that the co-process is alive, which is
* not enough).
*/
if (coproc.write >= 0 && coproc.write == fd) {
flags |= PO_COPROC;
opipe = block_pipe();
}
for (s = Xstring(xs, xp); len > 0; ) {
n = write(fd, s, len);
if (n == -1) {
if (flags & PO_COPROC)
restore_pipe(opipe);
if (errno == EINTR) {
/* allow user to ^C out */
intrcheck();
if (flags & PO_COPROC)
opipe = block_pipe();
continue;
}
/* This doesn't really make sense - could
* break scripts (print -p generates
* error message).
*if (errno == EPIPE)
* coproc_write_close(fd);
*/
return 1;
}
s += n;
len -= n;
}
if (flags & PO_COPROC)
restore_pipe(opipe);
}
return 0;
}
int
c_whence(char **wp)
{
struct tbl *tp;
char *id;
int pflag = 0, vflag = 0, Vflag = 0;
int ret = 0;
int optc;
int iam_whence;
int fcflags;
const char *options;
switch (wp[0][0]) {
case 'c': /* command */
iam_whence = 0;
options = "pvV";
break;
case 't': /* type */
vflag = 1;
/* FALLTHROUGH */
case 'w': /* whence */
iam_whence = 1;
options = "pv";
break;
default:
bi_errorf("builtin not handled by %s", __func__);
return 1;
}
while ((optc = ksh_getopt(wp, &builtin_opt, options)) != -1)
switch (optc) {
case 'p':
pflag = 1;
break;
case 'v':
vflag = 1;
break;
case 'V':
Vflag = 1;
break;
case '?':
return 1;
}
wp += builtin_opt.optind;
fcflags = FC_BI | FC_PATH | FC_FUNC;
if (!iam_whence) {
/* Note that -p on its own is dealt with in comexec() */
if (pflag)
fcflags |= FC_DEFPATH;
/* Convert command options to whence options. Note that
* command -pV and command -pv use a different path search
* than whence -v or whence -pv. This should be considered
* a feature.
*/
vflag = Vflag;
} else if (pflag)
fcflags &= ~(FC_BI | FC_FUNC);
while ((vflag || ret == 0) && (id = *wp++) != NULL) {
tp = NULL;
if (!iam_whence || !pflag)
tp = ktsearch(&keywords, id, hash(id));
if (!tp && (!iam_whence || !pflag)) {
tp = ktsearch(&aliases, id, hash(id));
if (tp && !(tp->flag & ISSET))
tp = NULL;
}
if (!tp)
tp = findcom(id, fcflags);
if (vflag || (tp->type != CALIAS && tp->type != CEXEC &&
tp->type != CTALIAS))
shprintf("%s", id);
switch (tp->type) {
case CKEYWD:
if (vflag)
shprintf(" is a reserved word");
break;
case CALIAS:
if (vflag)
shprintf(" is an %salias for ",
(tp->flag & EXPORT) ? "exported " : "");
if (!iam_whence && !vflag)
shprintf("alias %s=", id);
print_value_quoted(tp->val.s);
break;
case CFUNC:
if (vflag) {
shprintf(" is a");
if (tp->flag & EXPORT)
shprintf("n exported");
if (tp->flag & TRACE)
shprintf(" traced");
if (!(tp->flag & ISSET)) {
shprintf(" undefined");
if (tp->u.fpath)
shprintf(" (autoload from %s)",
tp->u.fpath);
}
shprintf(" function");
}
break;
case CSHELL:
if (vflag)
shprintf(" is a%s shell builtin",
(tp->flag & SPEC_BI) ? " special" : "");
break;
case CTALIAS:
case CEXEC:
if (tp->flag & ISSET) {
if (vflag) {
shprintf(" is ");
if (tp->type == CTALIAS)
shprintf("a tracked %salias for ",
(tp->flag & EXPORT) ?
"exported " : "");
}
shprintf("%s", tp->val.s);
} else {
if (vflag)
shprintf(" not found");
ret = 1;
}
break;
default:
shprintf("%s is *GOK*", id);
break;
}
if (vflag || !ret)
shprintf("\n");
}
return ret;
}
/* Deal with command -vV - command -p dealt with in comexec() */
int
c_command(char **wp)
{
/* Let c_whence do the work. Note that c_command() must be
* a distinct function from c_whence() (tested in comexec()).
*/
return c_whence(wp);
}
int
c_type(char **wp)
{
/* Let c_whence do the work. type = command -V = whence -v */
return c_whence(wp);
}
/* typeset, export, and readonly */
int
c_typeset(char **wp)
{
struct block *l;
struct tbl *vp, **p;
int fset = 0, fclr = 0, thing = 0, func = 0, local = 0, pflag = 0;
const char *options = "L#R#UZ#fi#lprtux"; /* see comment below */
char *fieldstr, *basestr;
int field, base, optc, flag;
switch (**wp) {
case 'e': /* export */
fset |= EXPORT;
options = "p";
break;
case 'r': /* readonly */
fset |= RDONLY;
options = "p";
break;
case 's': /* set */
/* called with 'typeset -' */
break;
case 't': /* typeset */
local = 1;
break;
}
fieldstr = basestr = NULL;
builtin_opt.flags |= GF_PLUSOPT;
/* at&t ksh seems to have 0-9 as options, which are multiplied
* to get a number that is used with -L, -R, -Z or -i (eg, -1R2
* sets right justify in a field of 12). This allows options
* to be grouped in an order (eg, -Lu12), but disallows -i8 -L3 and
* does not allow the number to be specified as a separate argument
* Here, the number must follow the RLZi option, but is optional
* (see the # kludge in ksh_getopt()).
*/
while ((optc = ksh_getopt(wp, &builtin_opt, options)) != -1) {
flag = 0;
switch (optc) {
case 'L':
flag = LJUST;
fieldstr = builtin_opt.optarg;
break;
case 'R':
flag = RJUST;
fieldstr = builtin_opt.optarg;
break;
case 'U':
/* at&t ksh uses u, but this conflicts with
* upper/lower case. If this option is changed,
* need to change the -U below as well
*/
flag = INT_U;
break;
case 'Z':
flag = ZEROFIL;
fieldstr = builtin_opt.optarg;
break;
case 'f':
func = 1;
break;
case 'i':
flag = INTEGER;
basestr = builtin_opt.optarg;
break;
case 'l':
flag = LCASEV;
break;
case 'p':
/* posix export/readonly -p flag.
* typeset -p is the same as typeset (in pdksh);
* here for compatibility with ksh93.
*/
pflag = 1;
break;
case 'r':
flag = RDONLY;
break;
case 't':
flag = TRACE;
break;
case 'u':
flag = UCASEV_AL; /* upper case / autoload */
break;
case 'x':
flag = EXPORT;
break;
case '?':
return 1;
}
if (builtin_opt.info & GI_PLUS) {
fclr |= flag;
fset &= ~flag;
thing = '+';
} else {
fset |= flag;
fclr &= ~flag;
thing = '-';
}
}
field = 0;
if (fieldstr && !bi_getn(fieldstr, &field))
return 1;
base = 0;
if (basestr && !bi_getn(basestr, &base))
return 1;
if (!(builtin_opt.info & GI_MINUSMINUS) && wp[builtin_opt.optind] &&
(wp[builtin_opt.optind][0] == '-' ||
wp[builtin_opt.optind][0] == '+') &&
wp[builtin_opt.optind][1] == '\0') {
thing = wp[builtin_opt.optind][0];
builtin_opt.optind++;
}
if (func && ((fset|fclr) & ~(TRACE|UCASEV_AL|EXPORT))) {
bi_errorf("only -t, -u and -x options may be used with -f");
return 1;
}
if (wp[builtin_opt.optind]) {
/* Take care of exclusions.
* At this point, flags in fset are cleared in fclr and vise
* versa. This property should be preserved.
*/
if (fset & LCASEV) /* LCASEV has priority over UCASEV_AL */
fset &= ~UCASEV_AL;
if (fset & LJUST) /* LJUST has priority over RJUST */
fset &= ~RJUST;
if ((fset & (ZEROFIL|LJUST)) == ZEROFIL) { /* -Z implies -ZR */
fset |= RJUST;
fclr &= ~RJUST;
}
/* Setting these attributes clears the others, unless they
* are also set in this command
*/
if (fset & (LJUST | RJUST | ZEROFIL | UCASEV_AL | LCASEV |
INTEGER | INT_U | INT_L))
fclr |= ~fset & (LJUST | RJUST | ZEROFIL | UCASEV_AL |
LCASEV | INTEGER | INT_U | INT_L);
}
/* set variables and attributes */
if (wp[builtin_opt.optind]) {
int i;
int rval = 0;
struct tbl *f;
if (local && !func)
fset |= LOCAL;
for (i = builtin_opt.optind; wp[i]; i++) {
if (func) {
f = findfunc(wp[i], hash(wp[i]),
(fset&UCASEV_AL) ? true : false);
if (!f) {
/* at&t ksh does ++rval: bogus */
rval = 1;
continue;
}
if (fset | fclr) {
f->flag |= fset;
f->flag &= ~fclr;
} else
fptreef(shl_stdout, 0,
f->flag & FKSH ?
"function %s %T\n" :
"%s() %T\n", wp[i], f->val.t);
} else if (!typeset(wp[i], fset, fclr, field, base)) {
bi_errorf("%s: not identifier", wp[i]);
return 1;
}
}
return rval;
}
/* list variables and attributes */
flag = fset | fclr; /* no difference at this point.. */
if (func) {
for (l = genv->loc; l; l = l->next) {
for (p = ktsort(&l->funs); (vp = *p++); ) {
if (flag && (vp->flag & flag) == 0)
continue;
if (thing == '-')
fptreef(shl_stdout, 0, vp->flag & FKSH ?
"function %s %T\n" : "%s() %T\n",
vp->name, vp->val.t);
else
shprintf("%s\n", vp->name);
}
}
} else {
for (l = genv->loc; l; l = l->next) {
for (p = ktsort(&l->vars); (vp = *p++); ) {
struct tbl *tvp;
int any_set = 0;
/*
* See if the parameter is set (for arrays, if any
* element is set).
*/
for (tvp = vp; tvp; tvp = tvp->u.array)
if (tvp->flag & ISSET) {
any_set = 1;
break;
}
/*
* Check attributes - note that all array elements
* have (should have?) the same attributes, so checking
* the first is sufficient.
*
* Report an unset param only if the user has
* explicitly given it some attribute (like export);
* otherwise, after "echo $FOO", we would report FOO...
*/
if (!any_set && !(vp->flag & USERATTRIB))
continue;
if (flag && (vp->flag & flag) == 0)
continue;
for (; vp; vp = vp->u.array) {
/* Ignore array elements that aren't
* set unless there are no set elements,
* in which case the first is reported on */
if ((vp->flag&ARRAY) && any_set &&
!(vp->flag & ISSET))
continue;
/* no arguments */
if (thing == 0 && flag == 0) {
/* at&t ksh prints things
* like export, integer,
* leftadj, zerofill, etc.,
* but POSIX says must
* be suitable for re-entry...
*/
shprintf("typeset ");
if ((vp->flag&INTEGER))
shprintf("-i ");
if ((vp->flag&EXPORT))
shprintf("-x ");
if ((vp->flag&RDONLY))
shprintf("-r ");
if ((vp->flag&TRACE))
shprintf("-t ");
if ((vp->flag&LJUST))
shprintf("-L%d ", vp->u2.field);
if ((vp->flag&RJUST))
shprintf("-R%d ", vp->u2.field);
if ((vp->flag&ZEROFIL))
shprintf("-Z ");
if ((vp->flag&LCASEV))
shprintf("-l ");
if ((vp->flag&UCASEV_AL))
shprintf("-u ");
if ((vp->flag&INT_U))
shprintf("-U ");
shprintf("%s\n", vp->name);
if (vp->flag&ARRAY)
break;
} else {
if (pflag)
shprintf("%s ",
(flag & EXPORT) ?
"export" : "readonly");
if ((vp->flag&ARRAY) && any_set)
shprintf("%s[%d]",
vp->name, vp->index);
else
shprintf("%s", vp->name);
if (thing == '-' && (vp->flag&ISSET)) {
char *s = str_val(vp);
shprintf("=");
/* at&t ksh can't have
* justified integers.. */
if ((vp->flag &
(INTEGER|LJUST|RJUST)) ==
INTEGER)
shprintf("%s", s);
else
print_value_quoted(s);
}
shprintf("\n");
}
/* Only report first `element' of an array with
* no set elements.
*/
if (!any_set)
break;
}
}
}
}
return 0;
}
int
c_alias(char **wp)
{
struct table *t = &aliases;
int rv = 0, rflag = 0, tflag, Uflag = 0, pflag = 0, prefix = 0;
int xflag = 0;
int optc;
builtin_opt.flags |= GF_PLUSOPT;
while ((optc = ksh_getopt(wp, &builtin_opt, "dprtUx")) != -1) {
prefix = builtin_opt.info & GI_PLUS ? '+' : '-';
switch (optc) {
case 'd':
t = &homedirs;
break;
case 'p':
pflag = 1;
break;
case 'r':
rflag = 1;
break;
case 't':
t = &taliases;
break;
case 'U':
/*
* kludge for tracked alias initialization
* (don't do a path search, just make an entry)
*/
Uflag = 1;
break;
case 'x':
xflag = EXPORT;
break;
case '?':
return 1;
}
}
wp += builtin_opt.optind;
if (!(builtin_opt.info & GI_MINUSMINUS) && *wp &&
(wp[0][0] == '-' || wp[0][0] == '+') && wp[0][1] == '\0') {
prefix = wp[0][0];
wp++;
}
tflag = t == &taliases;
/* "hash -r" means reset all the tracked aliases.. */
if (rflag) {
static const char *const args[] = {
"unalias", "-ta", NULL
};
if (!tflag || *wp) {
shprintf("alias: -r flag can only be used with -t"
" and without arguments\n");
return 1;
}
ksh_getopt_reset(&builtin_opt, GF_ERROR);
return c_unalias((char **) args);
}
if (*wp == NULL) {
struct tbl *ap, **p;
for (p = ktsort(t); (ap = *p++) != NULL; )
if ((ap->flag & (ISSET|xflag)) == (ISSET|xflag)) {
if (pflag)
shf_puts("alias ", shl_stdout);
shf_puts(ap->name, shl_stdout);
if (prefix != '+') {
shf_putc('=', shl_stdout);
print_value_quoted(ap->val.s);
}
shprintf("\n");
}
}
for (; *wp != NULL; wp++) {
char *alias = *wp;
char *val = strchr(alias, '=');
char *newval;
struct tbl *ap;
int h;
if (val)
alias = str_nsave(alias, val++ - alias, ATEMP);
h = hash(alias);
if (val == NULL && !tflag && !xflag) {
ap = ktsearch(t, alias, h);
if (ap != NULL && (ap->flag&ISSET)) {
if (pflag)
shf_puts("alias ", shl_stdout);
shf_puts(ap->name, shl_stdout);
if (prefix != '+') {
shf_putc('=', shl_stdout);
print_value_quoted(ap->val.s);
}
shprintf("\n");
} else {
shprintf("%s alias not found\n", alias);
rv = 1;
}
continue;
}
ap = ktenter(t, alias, h);
ap->type = tflag ? CTALIAS : CALIAS;
/* Are we setting the value or just some flags? */
if ((val && !tflag) || (!val && tflag && !Uflag)) {
if (ap->flag&ALLOC) {
ap->flag &= ~(ALLOC|ISSET);
afree(ap->val.s, APERM);
}
/* ignore values for -t (at&t ksh does this) */
newval = tflag ? search(alias, search_path, X_OK, NULL)
: val;
if (newval) {
ap->val.s = str_save(newval, APERM);
ap->flag |= ALLOC|ISSET;
} else
ap->flag &= ~ISSET;
}
ap->flag |= DEFINED;
if (prefix == '+')
ap->flag &= ~xflag;
else
ap->flag |= xflag;
if (val)
afree(alias, ATEMP);
}
return rv;
}
int
c_unalias(char **wp)
{
struct table *t = &aliases;
struct tbl *ap;
int rv = 0, all = 0;
int optc;
while ((optc = ksh_getopt(wp, &builtin_opt, "adt")) != -1)
switch (optc) {
case 'a':
all = 1;
break;
case 'd':