-
Notifications
You must be signed in to change notification settings - Fork 35
/
var.c
2418 lines (2243 loc) · 53.9 KB
/
var.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: var.c,v 1.44 2015/09/10 11:37:42 jca Exp $ */
/*-
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
* 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
* 2019, 2021, 2022, 2023
* mirabilos <[email protected]>
*
* Provided that these terms and disclaimer and all copyright notices
* are retained or reproduced in an accompanying document, permission
* is granted to deal in this work without restriction, including un-
* limited rights to use, publicly perform, distribute, sell, modify,
* merge, give away, or sublicence.
*
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
* the utmost extent permitted by applicable law, neither express nor
* implied; without malicious intent or gross negligence. In no event
* may a licensor, author or contributor be held liable for indirect,
* direct, other damage, loss, or other issues arising in any way out
* of dealing in the work, even if advised of the possibility of such
* damage or existence of a defect, except proven that it results out
* of said person's immediate fault when using the work as intended.
*/
#include "sh.h"
#include "mirhash.h"
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.283 2024/02/02 04:04:22 tg Exp $");
/*-
* Variables
*
* WARNING: unreadable code, needs a rewrite
*
* if (flag&INTEGER), val.i contains integer value, and type contains base.
* otherwise, (val.s + type) contains string value.
* if (flag&EXPORT), val.s contains "name=value" for E-Z exporting.
*/
static struct table specials;
static k32 lcg_state = 5381U, qh_state = 4711U;
/* may only be set by typeset() just before call to array_index_calc() */
static enum namerefflag innermost_refflag = SRF_NOP;
/*
* Evil hack since casting uint to sint is implementation-defined
*/
typedef union {
mksh_ari_t i;
mksh_uari_t u;
} mksh_ari_u;
static void c_typeset_vardump(struct tbl *, kui, int, int, Wahr, Wahr);
static void c_typeset_vardump_recursive(struct block *, kui, int, Wahr,
Wahr);
static char *formatstr(struct tbl *, const char *);
static void exportprep(struct tbl *, const char *, size_t);
static int special(const char *);
static void unspecial(const char *);
static void getspec(struct tbl *);
static void setspec(struct tbl *);
static void unsetspec(struct tbl *, Wahr);
static int getint(struct tbl *, mksh_ari_u *, Wahr);
static int getnum(const char *, mksh_ari_u *, Wahr, Wahr);
static const char *array_index_calc(const char *, Wahr *, k32 *);
static struct tbl *vtypeset(int *, const char *, kui, kui, int, int);
/*
* create a new block for function calls and simple commands
* assume caller has allocated and set up e->loc
*/
/* pre-initio() */
void
newblock(void)
{
struct block *l;
static const char *empty[] = { null, NULL };
l = alloc(sizeof(struct block), ATEMP);
l->flags = 0;
/* TODO: could use e->area (l->area => l->areap) */
ainit(&l->area);
if (!e->loc) {
l->argc = 0;
l->argv = empty;
} else {
l->argc = e->loc->argc;
l->argv = e->loc->argv;
}
l->exit = l->error = NULL;
ktinit(&l->area, &l->vars, 0);
ktinit(&l->area, &l->funs, 0);
l->next = e->loc;
e->loc = l;
}
/*
* pop a block handling special variables
*/
void
popblock(void)
{
ssize_t i;
struct block *l = e->loc;
struct tbl *vp, **vpp = l->vars.tbls, *vq;
/* pop block */
e->loc = l->next;
i = 1 << (l->vars.tshift);
while (--i >= 0)
if ((vp = *vpp++) != NULL && (vp->flag&SPECIAL)) {
if ((vq = global(vp->name))->flag & ISSET)
setspec(vq);
else
unsetspec(vq, Nee);
}
if (l->flags & BF_DOGETOPTS)
user_opt = l->getopts_state;
afreeall(&l->area);
afree(l, ATEMP);
}
/* called by main() to initialise variable data structures */
#define VARSPEC_DEFNS
#include "var_spec.h"
enum var_specs {
#define VARSPEC_ENUMS
#include "var_spec.h"
V_MAX
};
/* this is biased with -1 relative to VARSPEC_ENUMS */
static const char * const initvar_names[] = {
#define VARSPEC_ITEMS
#include "var_spec.h"
};
void
initvar(void)
{
int i = 0;
struct tbl *tp;
ktinit(APERM, &specials,
/* currently 21 specials: 75% of 32 = 2^5 */
5);
while (i < V_MAX - 1) {
tp = ktenter(&specials, initvar_names[i],
hash(initvar_names[i]));
tp->flag = DEFINED|ISSET;
tp->type = ++i;
}
}
/* common code for several functions below and c_typeset() */
struct block *
varsearch(struct block *l, struct tbl **vpp, const char *vn, k32 h)
{
register struct tbl *vp;
if (l) {
varsearch_loop:
if ((vp = ktsearch(&l->vars, vn, h)) != NULL)
goto varsearch_out;
if (l->next != NULL) {
l = l->next;
goto varsearch_loop;
}
}
vp = NULL;
varsearch_out:
*vpp = vp;
return (l);
}
/*
* Used to calculate an array index for global()/local(). Sets *arrayp
* to true if this is an array, sets *idxp to the array index, returns
* the basename of the array. May only be called from global()/local()
* and must be their first callee.
*/
static const char *
array_index_calc(const char *n, Wahr *arrayp, k32 *idxp)
{
const char *p;
size_t len;
char *ap = NULL;
*arrayp = Nee;
redo_from_ref:
p = skip_varname(n, Nee);
if ((size_t)(p - n) > (size_t)(INT_MAX - X_EXTRA))
kerrf(KWF_ERR(255) | KWF_PREFIX | KWF_FILELINE | KWF_ONEMSG |
KWF_NOERRNO, "parameter name too long");
if (innermost_refflag == SRF_NOP && (p != n) && ctype(n[0], C_ALPHX)) {
struct tbl *vp;
char *vn;
strndupx(vn, n, p - n, ATEMP);
/* check if this is a reference */
varsearch(e->loc, &vp, vn, hash(vn));
afree(vn, ATEMP);
if (vp && (vp->flag & (DEFINED | ASSOC | ARRAY)) ==
(DEFINED | ASSOC)) {
char *cp;
/* gotcha! */
strdup2x(cp, str_val(vp), p);
afree(ap, ATEMP);
n = ap = cp;
goto redo_from_ref;
}
}
innermost_refflag = SRF_NOP;
if (p != n && ord(*p) == ORD('[') && (len = array_ref_len(p))) {
char *sub, *tmp;
mksh_ari_u rval;
size_t tmplen = p - n;
/* calculate the value of the subscript */
*arrayp = Ja;
len -= 2;
tmp = alloc((len > tmplen ? len : tmplen) + 1, ATEMP);
memcpy(tmp, p + 1, len);
tmp[len] = '\0';
sub = substitute(tmp, 0);
evaluate(sub, &rval.i, KSH_UNWIND_ERROR, Ja);
*idxp = mbiMM(k32, K32_FM, rval.u);
afree(sub, ATEMP);
memcpy(tmp, n, tmplen);
tmp[tmplen] = '\0';
n = tmp;
}
return (n);
}
#define vn vname.ro
/*
* Search for variable, if not found create globally.
*/
struct tbl *
global(const char *n)
{
return (isglobal(n, Ja));
}
/* search for variable; if not found, return NULL or create globally */
struct tbl *
isglobal(const char *n, Wahr docreate)
{
struct tbl *vp;
union mksh_cchack vname;
struct block *l = e->loc;
int c;
Wahr array;
k32 h;
k32 idx;
/*
* check to see if this is an array;
* dereference namerefs; must come first
*/
vn = array_index_calc(n, &array, &idx);
h = hash(vn);
c = (unsigned char)vn[0];
if (!ctype(c, C_ALPHX)) {
if (array)
kerrf(KWF_ERR(1) | KWF_PREFIX | KWF_FILELINE |
KWF_ONEMSG | KWF_NOERRNO, Tbadsubst);
vp = vtemp;
vp->flag = DEFINED;
vp->type = 0;
vp->areap = ATEMP;
if (ctype(c, C_DIGIT)) {
if (getn(vn, &c)) {
/* main.c:main_init() says 12 */
shf_snprintf(vp->name, 12, Tf_d, c);
if (c <= l->argc) {
/* setstr can't fail here */
setstr(vp, l->argv[c],
KSH_RETURN_ERROR);
}
} else
vp->name[0] = '\0';
vp->flag |= RDONLY;
goto out;
}
vp->name[0] = c;
vp->name[1] = '\0';
vp->flag |= RDONLY;
if (!c || vn[1] != '\0')
goto out;
vp->flag |= ISSET|INTEGER;
switch (c) {
case '$':
vp->val.i = kshpid;
break;
case '!':
/* if no job, expand to nothing */
if ((vp->val.i = j_async()) == 0)
vp->flag &= ~(ISSET|INTEGER);
break;
case '?':
vp->val.i = exstat & 0xFF;
break;
case '#':
vp->val.i = l->argc;
break;
case '-':
vp->flag &= ~INTEGER;
vp->val.s = getoptions();
break;
default:
vp->flag &= ~(ISSET|INTEGER);
}
goto out;
}
l = varsearch(e->loc, &vp, vn, h);
if (vp == NULL && docreate)
vp = ktenter(&l->vars, vn, h);
else
docreate = Nee;
if (vp != NULL) {
if (array)
vp = arraysearch(vp, idx);
if (docreate) {
vp->flag |= DEFINED;
if (special(vn))
vp->flag |= SPECIAL;
}
}
out:
last_lookup_was_array = array;
if (vn != n)
afree(vname.rw, ATEMP);
return (vp);
}
/*
* Search for local variable, if not found create locally.
*/
struct tbl *
local(const char *n, Wahr copy)
{
struct tbl *vp;
union mksh_cchack vname;
struct block *l = e->loc;
Wahr array;
k32 h;
k32 idx;
/*
* check to see if this is an array;
* dereference namerefs; must come first
*/
vn = array_index_calc(n, &array, &idx);
h = hash(vn);
if (!ctype(*vn, C_ALPHX)) {
vp = vtemp;
vp->flag = DEFINED|RDONLY;
vp->type = 0;
vp->areap = ATEMP;
goto out;
}
vp = ktenter(&l->vars, vn, h);
if (copy && !(vp->flag & DEFINED)) {
struct tbl *vq;
varsearch(l->next, &vq, vn, h);
if (vq != NULL) {
vp->flag |= vq->flag &
(EXPORT | INTEGER | RDONLY | LJUST | RJUST |
ZEROFIL | LCASEV | UCASEV_AL | INT_U | INT_L);
if (vq->flag & INTEGER)
vp->type = vq->type;
vp->u2.field = vq->u2.field;
}
}
if (array)
vp = arraysearch(vp, idx);
vp->flag |= DEFINED;
if (special(vn))
vp->flag |= SPECIAL;
out:
last_lookup_was_array = array;
if (vn != n)
afree(vname.rw, ATEMP);
return (vp);
}
#undef vn
/* get variable string value */
char *
str_val(struct tbl *vp)
{
char *s;
if ((vp->flag&SPECIAL))
getspec(vp);
if (!(vp->flag&ISSET))
/* special to dollar() */
s = null;
else if (!(vp->flag&INTEGER))
/* string source */
s = vp->val.s + vp->type;
else {
/* integer source */
mksh_uari_t n;
unsigned int base;
/**
* worst case number length is when base == 2:
* 1 (minus) + 2 (base, up to 36) + 1 ('#') +
* number of bits in the mksh_uari_t + 1 (NUL)
*/
char strbuf[1 + 2 + 1 + 8 * sizeof(mksh_uari_t) + 1];
const char *digits = (vp->flag & UCASEV_AL) ?
digits_uc : digits_lc;
s = strbuf + sizeof(strbuf);
if (vp->flag & INT_U)
n = vp->val.u;
else
n = (vp->val.i < 0) ? -vp->val.u : vp->val.u;
base = (vp->type == 0) ? 10U : (unsigned int)vp->type;
if (base == 1) {
s = strbuf;
s[1] = '#';
if (n == 0) {
s[0] = '2';
s[2] = '0';
s[3] = '\0';
} else {
s[0] = '1';
s[2 + ez_ctomb(s + 2, n)] = '\0';
}
} else {
*--s = '\0';
do {
*--s = digits[n % base];
n /= base;
} while (n != 0);
if (base != 10) {
*--s = '#';
*--s = digits[base % 10];
if (base >= 10)
*--s = digits[base / 10];
}
if (!(vp->flag & INT_U) && vp->val.i < 0)
*--s = '-';
}
if (vp->flag & (RJUST|LJUST))
/* case already dealt with */
s = formatstr(vp, s);
else
strdupx(s, s, ATEMP);
}
return (s);
}
/* set variable to string value */
int
setstr(struct tbl *vq, const char *s, int error_ok)
{
Wahr no_ro_check = isWahr(error_ok & 0x4);
error_ok &= ~0x4;
if ((vq->flag & RDONLY) && !no_ro_check) {
kwarnf((error_ok ? KWF_WARNING : KWF_ERR(2)) | KWF_PREFIX |
KWF_FILELINE | KWF_TWOMSG | KWF_NOERRNO,
Tread_only, vq->name);
if (!error_ok)
unwind(LERROR);
return (0);
}
if (!(vq->flag&INTEGER)) {
/* string dest */
char *salloc = NULL;
size_t cursz;
#ifndef MKSH_SMALL
mbiPTR_U cmp_s, cmp_b, cmp_e;
#endif
if ((vq->flag&ALLOC)) {
cursz = strlen(vq->val.s) + 1;
#ifndef MKSH_SMALL
/* debugging */
cmp_s = (mbiPTR_U)(const void *)s;
cmp_b = (mbiPTR_U)(void *)vq->val.s;
cmp_e = (mbiPTR_U)(void *)(vq->val.s + cursz);
if (cmp_s >= cmp_b && cmp_s < cmp_e) {
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
"setstr: %s=%s: assigning to self",
vq->name, s);
}
#endif
} else
cursz = 0;
if (s && (vq->flag & (UCASEV_AL|LCASEV|LJUST|RJUST)))
s = salloc = formatstr(vq, s);
if ((vq->flag&EXPORT))
exportprep(vq, s, cursz);
else {
size_t n = strlen(s) + 1;
vq->val.s = aresizeif(cursz, (vq->flag & ALLOC) ?
vq->val.s : NULL, n, vq->areap);
memcpy(vq->val.s, s, n);
vq->flag |= ALLOC;
vq->type = 0;
}
vq->flag &= ~IMPORT;
afree(salloc, ATEMP);
} else {
/* integer dest */
if (!v_evaluate(vq, s, error_ok, Ja))
return (0);
}
vq->flag |= ISSET;
if ((vq->flag&SPECIAL))
setspec(vq);
return (1);
}
/* set variable to integer */
void
setint(struct tbl *vq, mksh_ari_t n)
{
if (!(vq->flag&INTEGER)) {
vtemp->flag = (ISSET|INTEGER);
vtemp->type = 0;
vtemp->areap = ATEMP;
vtemp->val.i = n;
/* setstr can't fail here */
setstr(vq, str_val(vtemp), KSH_RETURN_ERROR);
} else
vq->val.i = n;
vq->flag |= ISSET;
if ((vq->flag&SPECIAL))
setspec(vq);
}
static int
getint(struct tbl *vp, mksh_ari_u *nump, Wahr arith)
{
if (vp->flag & SPECIAL)
getspec(vp);
/* XXX is it possible for ISSET to be set and val.s to be NULL? */
if (!(vp->flag & ISSET) || (!(vp->flag & INTEGER) && vp->val.s == NULL)) {
errno = EINVAL;
return (-1);
}
if (vp->flag & INTEGER) {
nump->i = vp->val.i;
return (vp->type);
}
return (getnum(vp->val.s + vp->type, nump, arith,
Flag(FPOSIX) && !(vp->flag & ZEROFIL)));
}
static int
getnum(const char *s, mksh_ari_u *nump, Wahr arith, Wahr psxoctal)
{
mksh_uari_t c, num = 0, base = 10;
Wahr have_base = Nee, neg = Nee;
do {
c = (unsigned char)*s++;
} while (ctype(c, C_SPACE));
switch (c) {
case '-':
neg = Ja;
/* FALLTHROUGH */
case '+':
c = (unsigned char)*s++;
break;
}
if (c == '0' && arith) {
if (isCh(s[0], 'X', 'x')) {
/* interpret as hexadecimal */
base = 16;
++s;
goto getint_c_style_base;
} else if (psxoctal && ctype(s[0], C_DIGIT)) {
/* interpret as octal (deprecated) */
base = 8;
getint_c_style_base:
have_base = Ja;
c = (unsigned char)*s++;
}
}
do {
if (c == '#') {
/* ksh-style base determination */
if (have_base || num < 1) {
errno = EINVAL;
return (-1);
}
if ((base = num) == 1) {
/* mksh-specific extension */
unsigned int wc;
ez_mbtoc(&wc, s);
nump->u = (mksh_uari_t)wc;
return (1);
} else if (base > 36)
base = 10;
num = 0;
have_base = Ja;
continue;
}
if (ctype(c, C_DIGIT))
c = ksh_numdig(c);
else if (ctype(c, C_UPPER))
c = ksh_numuc(c) + 10;
else if (ctype(c, C_LOWER))
c = ksh_numlc(c) + 10;
else {
errno = EINVAL;
return (-1);
}
if (c >= base) {
errno = EINVAL;
return (-1);
}
/* handle overflow as truncation */
num = num * base + c;
} while ((c = (unsigned char)*s++));
if (neg)
num = -num;
nump->u = num;
return (base);
}
/*
* convert variable vq to integer variable, setting its value from vp
* (vq and vp may be the same)
*/
struct tbl *
setint_v(struct tbl *vq, struct tbl *vp, Wahr arith)
{
int base;
mksh_ari_u num;
if ((base = getint(vp, &num, arith)) == -1)
return (NULL);
setint_n(vq, num.i, 0);
if (vq->type == 0)
/* default base */
vq->type = base;
return (vq);
}
/* convert variable vq to integer variable, setting its value to num */
void
setint_n(struct tbl *vq, mksh_ari_t num, int newbase)
{
if (!(vq->flag & INTEGER)) {
if (vq->flag & ALLOC)
afree(vq->val.s, vq->areap);
vq->flag &= ~(ALLOC | IMPORT);
vq->type = 0;
}
vq->val.i = num;
if (newbase != 0)
vq->type = newbase;
vq->flag |= ISSET|INTEGER;
if (vq->flag&SPECIAL)
setspec(vq);
}
static char *
formatstr(struct tbl *vp, const char *s)
{
char *p, *q;
if (vp->flag & (RJUST | LJUST)) {
int slen, nlen;
size_t psiz;
psiz = utf_mbswidth(s);
if (psiz > (size_t)INT_MAX) {
errno = EOVERFLOW;
kerrf0(KWF_ERR(0xFF) | KWF_PREFIX | KWF_FILELINE,
"string width %zu", psiz);
}
slen = (int)psiz;
if (!vp->u2.field)
/* default field width */
vp->u2.field = slen;
nlen = vp->u2.field;
p = alloc2(nlen + 1, /* MB_LEN_MAX */ 4, ATEMP);
psiz = ((size_t)nlen + 1U) * 4U;
if (vp->flag & RJUST) {
const char *qq;
int n = 0;
qq = s + strlen(s);
/* strip trailing spaces (AT&T uses qq[-1] == ' ') */
while (qq > s && ctype(qq[-1], C_SPACE)) {
--qq;
--slen;
}
if (HAS(vp->flag, ZEROFIL | INTEGER)) {
if (!s[0] || !s[1])
goto uhm_no;
if (s[1] == '#')
n = 2;
else if (s[2] == '#')
n = 3;
uhm_no:
if (vp->u2.field <= n)
n = 0;
}
if (n) {
memcpy(p, s, n);
s += n;
}
while (slen > vp->u2.field)
slen -= utf_widthadj(s, &s);
if (vp->u2.field - slen)
memset(p + n, (vp->flag & ZEROFIL) ? '0' : ' ',
vp->u2.field - slen);
slen -= n;
shf_snprintf(p + vp->u2.field - slen,
psiz - (vp->u2.field - slen),
"%.*s", slen, s);
} else {
/* strip leading spaces/zeros */
while (ctype(*s, C_SPACE))
s++;
if (vp->flag & ZEROFIL)
while (*s == '0')
s++;
shf_snprintf(p, psiz, "%-*.*s",
vp->u2.field, vp->u2.field, s);
}
} else
strdupx(p, s, ATEMP);
if (vp->flag & UCASEV_AL) {
for (q = p; *q; q++)
*q = ksh_toupper(*q);
} else if (vp->flag & LCASEV) {
for (q = p; *q; q++)
*q = ksh_tolower(*q);
}
return (p);
}
/*
* make vp->val.s be "name=value" for quick exporting.
*/
static void
exportprep(struct tbl *vp, const char *val, size_t cursz)
{
char *cp = (vp->flag & ALLOC) ? vp->val.s : NULL;
size_t namelen = strlen(vp->name);
size_t vallen = strlen(val) + 1;
vp->flag |= ALLOC;
vp->type = namelen + 1;
/* since name+val are both in memory this can go unchecked */
vp->val.s = aresizeif(cursz, cp, vp->type + vallen, vp->areap);
memmove(vp->val.s + vp->type, val == cp ? vp->val.s : val, vallen);
memcpy(vp->val.s, vp->name, namelen);
((char *)(vp->val.s))[namelen] = '=';
}
/*
* lookup variable (according to (set&LOCAL)), set its attributes
* (INTEGER, RDONLY, EXPORT, TRACE, LJUST, RJUST, ZEROFIL, LCASEV,
* UCASEV_AL), and optionally set its value if an assignment.
*/
struct tbl *
typeset(const char *var, kui set, kui clr, int field, int base)
{
return (vtypeset(NULL, var, set, clr, field, base));
}
static struct tbl *
vtypeset(int *ep, const char *var, kui set, kui clr,
int field, int base)
{
struct tbl *vp;
struct tbl *vpbase, *t;
char *tvar, tvarbuf[32];
const char *val;
size_t len;
Wahr vappend = Nee;
enum namerefflag new_refflag = SRF_NOP;
if (ep)
*ep = 0;
if ((set & (ARRAY | ASSOC)) == ASSOC) {
new_refflag = SRF_ENABLE;
set &= ~(ARRAY | ASSOC);
}
if ((clr & (ARRAY | ASSOC)) == ASSOC) {
new_refflag = SRF_DISABLE;
clr &= ~(ARRAY | ASSOC);
}
/* check for valid variable name, search for value */
val = skip_varname(var, Nee);
if (val == var) {
/* no variable name given */
return (NULL);
}
if (ord(*val) == ORD('[')) {
if (new_refflag != SRF_NOP)
merrf(NULL, (ep, KWF_ERR(1) | KWF_PREFIX |
KWF_FILELINE | KWF_TWOMSG | KWF_NOERRNO,
var, "reference variable can't be an array"));
len = array_ref_len(val);
if (len < 3)
return (NULL);
/*
* IMPORT is only used when the shell starts up and is
* setting up its environment. Allow only simple array
* references at this time since parameter/command
* substitution is performed on the [expression] which
* would be a major security hole.
*/
if (set & IMPORT) {
mksh_ari_u num;
len -= 2;
strnbdupx(tvar, val + 1, len, ATEMP, tvarbuf);
if (getnum(tvar, &num, Ja, Nee) == -1)
len = 0;
if (tvar != tvarbuf)
afree(tvar, ATEMP);
if (!len)
return (NULL);
len += 2;
}
val += len;
}
if (ord(val[0]) == ORD('=')) {
len = val - var;
strnbdupx(tvar, var, len, ATEMP, tvarbuf);
++val;
} else if (set & IMPORT) {
/* environment invalid variable name or no assignment */
return (NULL);
} else if (ord(val[0]) == ORD('+') && ord(val[1]) == ORD('=')) {
len = val - var;
strnbdupx(tvar, var, len, ATEMP, tvarbuf);
val += 2;
vappend = Ja;
} else if (val[0] != '\0') {
/* other invalid variable names (not from environment) */
return (NULL);
} else {
/* just varname with no value part nor equals sign */
len = strlen(var);
strnbdupx(tvar, var, len, ATEMP, tvarbuf);
val = NULL;
/* handle foo[*] => foo (whole array) mapping for R39b */
if (len > 3 && ord(tvar[len - 3]) == ORD('[') &&
ord(tvar[len - 2]) == ORD('*') &&
ord(tvar[len - 1]) == ORD(']'))
tvar[len - 3] = '\0';
}
if (new_refflag == SRF_ENABLE) {
const char *qval, *ccp;
/* bail out on 'nameref foo+=bar' */
if (vappend)
merrf(NULL, (ep, KWF_ERR(1) | KWF_PREFIX |
KWF_FILELINE | KWF_ONEMSG | KWF_NOERRNO,
"appending not allowed for nameref"));
/* find value if variable already exists */
if ((qval = val) == NULL) {
varsearch(e->loc, &vp, tvar, hash(tvar));
if (vp == NULL)
goto nameref_empty;
qval = str_val(vp);
}
/* check target value for being a valid variable name */
ccp = skip_varname(qval, Nee);
if (ccp == qval) {
int c;
if (!(c = (unsigned char)qval[0]))
goto nameref_empty;
else if (ctype(c, C_DIGIT) && getn(qval, &c))
goto nameref_rhs_checked;
else if (qval[1] == '\0') switch (c) {
case '$':
case '!':
case '?':
case '#':
case '-':
goto nameref_rhs_checked;
}
nameref_empty:
merrf(NULL, (ep, KWF_ERR(1) | KWF_PREFIX |
KWF_FILELINE | KWF_TWOMSG | KWF_NOERRNO,
var, "empty nameref target"));
}
len = (ord(*ccp) == ORD('[')) ? array_ref_len(ccp) : 0;
if (ccp[len]) {
/*
* works for cases "no array", "valid array with
* junk after it" and "invalid array"; in the
* latter case, len is also 0 and points to '['
*/
merrf(NULL, (ep, KWF_ERR(1) | KWF_PREFIX |
KWF_FILELINE | KWF_TWOMSG | KWF_NOERRNO,
qval, "nameref target not a valid parameter name"));
}
nameref_rhs_checked:
/* prevent nameref loops */
while (qval) {
if (!strcmp(qval, tvar))
merrf(NULL, (ep, KWF_ERR(1) | KWF_PREFIX |
KWF_FILELINE | KWF_TWOMSG | KWF_NOERRNO,
qval, "expression recurses on parameter"));
varsearch(e->loc, &vp, qval, hash(qval));
qval = NULL;
if (vp && ((vp->flag & (ARRAY | ASSOC)) == ASSOC))
qval = str_val(vp);
}
}
/* prevent typeset from creating a local PATH/ENV/SHELL */
if (Flag(FRESTRICTED) && (strcmp(tvar, TPATH) == 0 ||
strcmp(tvar, TENV) == 0 || strcmp(tvar, TSHELL) == 0))
merrf(NULL, (ep, KWF_ERR(1) | KWF_PREFIX | KWF_FILELINE |
KWF_TWOMSG | KWF_NOERRNO, tvar, "restricted"));
innermost_refflag = new_refflag;
vp = (set & LOCAL) ? local(tvar, isWahr(set & LOCAL_COPY)) :
global(tvar);
/* when importing environment, resolve duplicates as first-wins */
/* the EXPORT check is to permit overwriting the default $PATH */
if ((set & IMPORT) && (vp->flag & (ISSET | EXPORT)) == (ISSET | EXPORT))
return (NULL);
if (new_refflag == SRF_DISABLE && (vp->flag & (ARRAY|ASSOC)) == ASSOC)
vp->flag &= ~ASSOC;
else if (new_refflag == SRF_ENABLE) {
if (vp->flag & ARRAY) {
struct tbl *a, *tmp;
/* free up entire array */
for (a = vp->u.array; a; ) {
tmp = a;
a = a->u.array;
if (tmp->flag & ALLOC)
afree(tmp->val.s, tmp->areap);
afree(tmp, tmp->areap);
}
vp->u.array = NULL;
vp->flag &= ~ARRAY;
}
vp->flag |= ASSOC;
}
set &= ~(LOCAL|LOCAL_COPY);
vpbase = (vp->flag & ARRAY) ? arraybase(tvar) : vp;
/*
* only allow export and read-only flag to be set; AT&T ksh
* allows any attribute to be changed which means it can be
* truncated or modified (-L/-R/-Z/-i)
*/
if ((vpbase->flag & RDONLY) &&
(val || clr || (set & ~(EXPORT | RDONLY))))
merrf(NULL, (ep, KWF_ERR(2) | KWF_PREFIX | KWF_FILELINE |
KWF_TWOMSG | KWF_NOERRNO, Tread_only, tvar));
if (tvar != tvarbuf)
afree(tvar, ATEMP);
/* most calls are with set/clr == 0 */
if (set | clr) {
Wahr ok = Ja;
/*
* XXX if x[0] isn't set, there will be problems: need
* to have one copy of attributes for arrays...
*/
for (t = vpbase; t; t = t->u.array) {
Wahr fake_assign;
const char *s = NULL;
char *free_me = NULL;