mirrored from git://git.code.sf.net/p/zsh/code
/
params.c
5957 lines (5372 loc) · 142 KB
/
params.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
/*
* params.c - parameters
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1992-1997 Paul Falstad
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and to distribute modified versions of this software for any
* purpose, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* In no event shall Paul Falstad or the Zsh Development Group be liable
* to any party for direct, indirect, special, incidental, or consequential
* damages arising out of the use of this software and its documentation,
* even if Paul Falstad and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Paul Falstad and the Zsh Development Group specifically disclaim any
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose. The software
* provided hereunder is on an "as is" basis, and Paul Falstad and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*
*/
#include "zsh.mdh"
#include "params.pro"
#include "version.h"
#ifdef CUSTOM_PATCHLEVEL
#define ZSH_PATCHLEVEL CUSTOM_PATCHLEVEL
#else
#include "patchlevel.h"
#include <math.h>
/* If removed from the ChangeLog for some reason */
#ifndef ZSH_PATCHLEVEL
#define ZSH_PATCHLEVEL "unknown"
#endif
#endif
/* What level of localness we are at.
*
* Hand-wavingly, this is incremented at every function call and decremented
* at every function return. See startparamscope().
*/
/**/
mod_export int locallevel;
/* Variables holding values of special parameters */
/**/
mod_export
char **pparams, /* $argv */
**cdpath, /* $cdpath */
**fpath, /* $fpath */
**mailpath, /* $mailpath */
**manpath, /* $manpath */
**psvar, /* $psvar */
**watch, /* $watch */
**zsh_eval_context; /* $zsh_eval_context */
/**/
mod_export
char **path, /* $path */
**fignore; /* $fignore */
/**/
mod_export
char *argzero, /* $0 */
*posixzero, /* $0 */
*home, /* $HOME */
*nullcmd, /* $NULLCMD */
*oldpwd, /* $OLDPWD */
*zoptarg, /* $OPTARG */
*prompt, /* $PROMPT */
*prompt2, /* $PROMPT2 */
*prompt3, /* $PROMPT3 */
*prompt4, /* $PROMPT4 */
*readnullcmd, /* $READNULLCMD */
*rprompt, /* $RPROMPT */
*rprompt2, /* $RPROMPT2 */
*sprompt, /* $SPROMPT */
*wordchars; /* $WORDCHARS */
/**/
mod_export
char *ifs, /* $IFS */
*postedit, /* $POSTEDIT */
*term, /* $TERM */
*zsh_terminfo, /* $TERMINFO */
*zsh_terminfodirs, /* $TERMINFO_DIRS */
*ttystrname, /* $TTY */
*pwd; /* $PWD */
/**/
mod_export
zlong lastval, /* $? */
mypid, /* $$ */
lastpid, /* $! */
zterm_columns, /* $COLUMNS */
zterm_lines, /* $LINES */
rprompt_indent, /* $ZLE_RPROMPT_INDENT */
ppid, /* $PPID */
zsh_subshell; /* $ZSH_SUBSHELL */
/* $FUNCNEST */
/**/
mod_export
zlong zsh_funcnest =
#ifdef MAX_FUNCTION_DEPTH
MAX_FUNCTION_DEPTH
#else
/* Disabled by default but can be enabled at run time */
-1
#endif
;
/**/
zlong lineno, /* $LINENO */
zoptind, /* $OPTIND */
shlvl; /* $SHLVL */
/* $histchars */
/**/
mod_export unsigned char bangchar;
/**/
unsigned char hatchar, hashchar;
/**/
unsigned char keyboardhackchar = '\0';
/* $SECONDS = now.tv_sec - shtimer.tv_sec
* + (now.tv_usec - shtimer.tv_usec) / 1000000.0
* (rounded to an integer if the parameter is not set to float) */
/**/
struct timeval shtimer;
/* 0 if this $TERM setup is usable, otherwise it contains TERM_* flags */
/**/
mod_export int termflags;
/* Forward declaration */
static void
rprompt_indent_unsetfn(Param pm, int exp);
/* Standard methods for get/set/unset pointers in parameters */
/**/
mod_export const struct gsu_scalar stdscalar_gsu =
{ strgetfn, strsetfn, stdunsetfn };
/**/
mod_export const struct gsu_scalar varscalar_gsu =
{ strvargetfn, strvarsetfn, stdunsetfn };
/**/
mod_export const struct gsu_scalar nullsetscalar_gsu =
{ strgetfn, nullstrsetfn, NULL };
/**/
mod_export const struct gsu_integer stdinteger_gsu =
{ intgetfn, intsetfn, stdunsetfn };
/**/
mod_export const struct gsu_integer varinteger_gsu =
{ intvargetfn, intvarsetfn, stdunsetfn };
/**/
mod_export const struct gsu_integer nullsetinteger_gsu =
{ intgetfn, NULL, NULL };
/**/
mod_export const struct gsu_float stdfloat_gsu =
{ floatgetfn, floatsetfn, stdunsetfn };
/**/
mod_export const struct gsu_array stdarray_gsu =
{ arrgetfn, arrsetfn, stdunsetfn };
/**/
mod_export const struct gsu_array vararray_gsu =
{ arrvargetfn, arrvarsetfn, stdunsetfn };
/**/
mod_export const struct gsu_hash stdhash_gsu =
{ hashgetfn, hashsetfn, stdunsetfn };
/**/
mod_export const struct gsu_hash nullsethash_gsu =
{ hashgetfn, nullsethashfn, nullunsetfn };
/* Non standard methods (not exported) */
static const struct gsu_integer pound_gsu =
{ poundgetfn, nullintsetfn, stdunsetfn };
static const struct gsu_integer errno_gsu =
{ errnogetfn, errnosetfn, stdunsetfn };
static const struct gsu_integer gid_gsu =
{ gidgetfn, gidsetfn, stdunsetfn };
static const struct gsu_integer egid_gsu =
{ egidgetfn, egidsetfn, stdunsetfn };
static const struct gsu_integer histsize_gsu =
{ histsizegetfn, histsizesetfn, stdunsetfn };
static const struct gsu_integer random_gsu =
{ randomgetfn, randomsetfn, stdunsetfn };
static const struct gsu_integer savehist_gsu =
{ savehistsizegetfn, savehistsizesetfn, stdunsetfn };
static const struct gsu_integer intseconds_gsu =
{ intsecondsgetfn, intsecondssetfn, stdunsetfn };
static const struct gsu_float floatseconds_gsu =
{ floatsecondsgetfn, floatsecondssetfn, stdunsetfn };
static const struct gsu_integer uid_gsu =
{ uidgetfn, uidsetfn, stdunsetfn };
static const struct gsu_integer euid_gsu =
{ euidgetfn, euidsetfn, stdunsetfn };
static const struct gsu_integer ttyidle_gsu =
{ ttyidlegetfn, nullintsetfn, stdunsetfn };
static const struct gsu_scalar argzero_gsu =
{ argzerogetfn, argzerosetfn, nullunsetfn };
static const struct gsu_scalar username_gsu =
{ usernamegetfn, usernamesetfn, stdunsetfn };
static const struct gsu_scalar dash_gsu =
{ dashgetfn, nullstrsetfn, stdunsetfn };
static const struct gsu_scalar histchars_gsu =
{ histcharsgetfn, histcharssetfn, stdunsetfn };
static const struct gsu_scalar home_gsu =
{ homegetfn, homesetfn, stdunsetfn };
static const struct gsu_scalar term_gsu =
{ termgetfn, termsetfn, stdunsetfn };
static const struct gsu_scalar terminfo_gsu =
{ terminfogetfn, terminfosetfn, stdunsetfn };
static const struct gsu_scalar terminfodirs_gsu =
{ terminfodirsgetfn, terminfodirssetfn, stdunsetfn };
static const struct gsu_scalar wordchars_gsu =
{ wordcharsgetfn, wordcharssetfn, stdunsetfn };
static const struct gsu_scalar ifs_gsu =
{ ifsgetfn, ifssetfn, stdunsetfn };
static const struct gsu_scalar underscore_gsu =
{ underscoregetfn, nullstrsetfn, stdunsetfn };
static const struct gsu_scalar keyboard_hack_gsu =
{ keyboardhackgetfn, keyboardhacksetfn, stdunsetfn };
#ifdef USE_LOCALE
static const struct gsu_scalar lc_blah_gsu =
{ strgetfn, lcsetfn, stdunsetfn };
static const struct gsu_scalar lang_gsu =
{ strgetfn, langsetfn, stdunsetfn };
static const struct gsu_scalar lc_all_gsu =
{ strgetfn, lc_allsetfn, stdunsetfn };
#endif
static const struct gsu_integer varint_readonly_gsu =
{ intvargetfn, nullintsetfn, stdunsetfn };
static const struct gsu_integer zlevar_gsu =
{ intvargetfn, zlevarsetfn, stdunsetfn };
static const struct gsu_scalar colonarr_gsu =
{ colonarrgetfn, colonarrsetfn, stdunsetfn };
static const struct gsu_integer argc_gsu =
{ poundgetfn, nullintsetfn, stdunsetfn };
static const struct gsu_array pipestatus_gsu =
{ pipestatgetfn, pipestatsetfn, stdunsetfn };
static const struct gsu_integer rprompt_indent_gsu =
{ intvargetfn, zlevarsetfn, rprompt_indent_unsetfn };
/* Nodes for special parameters for parameter hash table */
#ifdef HAVE_UNION_INIT
# define BR(X) {X}
typedef struct param initparam;
#else
# define BR(X) X
typedef struct iparam {
struct hashnode *next;
char *nam; /* hash data */
int flags; /* PM_* flags (defined in zsh.h) */
void *value;
void *gsu; /* get/set/unset methods */
int base; /* output base */
int width; /* output field width */
char *env; /* location in environment, if exported */
char *ename; /* name of corresponding environment var */
Param old; /* old struct for use with local */
int level; /* if (old != NULL), level of localness */
} initparam;
#endif
static initparam special_params[] ={
#define GSU(X) BR((GsuScalar)(void *)(&(X)))
#define NULL_GSU BR((GsuScalar)(void *)NULL)
#define IPDEF1(A,B,C) {{NULL,A,PM_INTEGER|PM_SPECIAL|C},BR(NULL),GSU(B),10,0,NULL,NULL,NULL,0}
IPDEF1("#", pound_gsu, PM_READONLY_SPECIAL),
IPDEF1("ERRNO", errno_gsu, PM_UNSET),
IPDEF1("GID", gid_gsu, PM_DONTIMPORT | PM_RESTRICTED),
IPDEF1("EGID", egid_gsu, PM_DONTIMPORT | PM_RESTRICTED),
IPDEF1("HISTSIZE", histsize_gsu, PM_RESTRICTED),
IPDEF1("RANDOM", random_gsu, 0),
IPDEF1("SAVEHIST", savehist_gsu, PM_RESTRICTED),
IPDEF1("SECONDS", intseconds_gsu, 0),
IPDEF1("UID", uid_gsu, PM_DONTIMPORT | PM_RESTRICTED),
IPDEF1("EUID", euid_gsu, PM_DONTIMPORT | PM_RESTRICTED),
IPDEF1("TTYIDLE", ttyidle_gsu, PM_READONLY_SPECIAL),
#define IPDEF2(A,B,C) {{NULL,A,PM_SCALAR|PM_SPECIAL|C},BR(NULL),GSU(B),0,0,NULL,NULL,NULL,0}
IPDEF2("USERNAME", username_gsu, PM_DONTIMPORT|PM_RESTRICTED),
IPDEF2("-", dash_gsu, PM_READONLY_SPECIAL),
IPDEF2("histchars", histchars_gsu, PM_DONTIMPORT),
IPDEF2("HOME", home_gsu, PM_UNSET),
IPDEF2("TERM", term_gsu, PM_UNSET),
IPDEF2("TERMINFO", terminfo_gsu, PM_UNSET),
IPDEF2("TERMINFO_DIRS", terminfodirs_gsu, PM_UNSET),
IPDEF2("WORDCHARS", wordchars_gsu, 0),
IPDEF2("IFS", ifs_gsu, PM_DONTIMPORT | PM_RESTRICTED),
IPDEF2("_", underscore_gsu, PM_DONTIMPORT),
IPDEF2("KEYBOARD_HACK", keyboard_hack_gsu, PM_DONTIMPORT),
IPDEF2("0", argzero_gsu, 0),
#ifdef USE_LOCALE
# define LCIPDEF(name) IPDEF2(name, lc_blah_gsu, PM_UNSET)
IPDEF2("LANG", lang_gsu, PM_UNSET),
IPDEF2("LC_ALL", lc_all_gsu, PM_UNSET),
# ifdef LC_COLLATE
LCIPDEF("LC_COLLATE"),
# endif
# ifdef LC_CTYPE
LCIPDEF("LC_CTYPE"),
# endif
# ifdef LC_MESSAGES
LCIPDEF("LC_MESSAGES"),
# endif
# ifdef LC_NUMERIC
LCIPDEF("LC_NUMERIC"),
# endif
# ifdef LC_TIME
LCIPDEF("LC_TIME"),
# endif
#endif /* USE_LOCALE */
#define IPDEF4(A,B) {{NULL,A,PM_INTEGER|PM_READONLY_SPECIAL},BR((void *)B),GSU(varint_readonly_gsu),10,0,NULL,NULL,NULL,0}
IPDEF4("!", &lastpid),
IPDEF4("$", &mypid),
IPDEF4("?", &lastval),
IPDEF4("HISTCMD", &curhist),
IPDEF4("LINENO", &lineno),
IPDEF4("PPID", &ppid),
IPDEF4("ZSH_SUBSHELL", &zsh_subshell),
#define IPDEF5(A,B,F) {{NULL,A,PM_INTEGER|PM_SPECIAL},BR((void *)B),GSU(F),10,0,NULL,NULL,NULL,0}
#define IPDEF5U(A,B,F) {{NULL,A,PM_INTEGER|PM_SPECIAL|PM_UNSET},BR((void *)B),GSU(F),10,0,NULL,NULL,NULL,0}
IPDEF5("COLUMNS", &zterm_columns, zlevar_gsu),
IPDEF5("LINES", &zterm_lines, zlevar_gsu),
IPDEF5U("ZLE_RPROMPT_INDENT", &rprompt_indent, rprompt_indent_gsu),
IPDEF5("SHLVL", &shlvl, varinteger_gsu),
IPDEF5("FUNCNEST", &zsh_funcnest, varinteger_gsu),
/* Don't import internal integer status variables. */
#define IPDEF6(A,B,F) {{NULL,A,PM_INTEGER|PM_SPECIAL|PM_DONTIMPORT},BR((void *)B),GSU(F),10,0,NULL,NULL,NULL,0}
IPDEF6("OPTIND", &zoptind, varinteger_gsu),
IPDEF6("TRY_BLOCK_ERROR", &try_errflag, varinteger_gsu),
IPDEF6("TRY_BLOCK_INTERRUPT", &try_interrupt, varinteger_gsu),
#define IPDEF7(A,B) {{NULL,A,PM_SCALAR|PM_SPECIAL},BR((void *)B),GSU(varscalar_gsu),0,0,NULL,NULL,NULL,0}
#define IPDEF7R(A,B) {{NULL,A,PM_SCALAR|PM_SPECIAL|PM_DONTIMPORT_SUID},BR((void *)B),GSU(varscalar_gsu),0,0,NULL,NULL,NULL,0}
#define IPDEF7U(A,B) {{NULL,A,PM_SCALAR|PM_SPECIAL|PM_UNSET},BR((void *)B),GSU(varscalar_gsu),0,0,NULL,NULL,NULL,0}
IPDEF7("OPTARG", &zoptarg),
IPDEF7("NULLCMD", &nullcmd),
IPDEF7U("POSTEDIT", &postedit),
IPDEF7("READNULLCMD", &readnullcmd),
IPDEF7("PS1", &prompt),
IPDEF7U("RPS1", &rprompt),
IPDEF7U("RPROMPT", &rprompt),
IPDEF7("PS2", &prompt2),
IPDEF7U("RPS2", &rprompt2),
IPDEF7U("RPROMPT2", &rprompt2),
IPDEF7("PS3", &prompt3),
IPDEF7R("PS4", &prompt4),
IPDEF7("SPROMPT", &sprompt),
#define IPDEF9(A,B,C,D) {{NULL,A,D|PM_ARRAY|PM_SPECIAL|PM_DONTIMPORT},BR((void *)B),GSU(vararray_gsu),0,0,NULL,C,NULL,0}
IPDEF9("*", &pparams, NULL, PM_ARRAY|PM_READONLY_SPECIAL|PM_DONTIMPORT),
IPDEF9("@", &pparams, NULL, PM_ARRAY|PM_READONLY_SPECIAL|PM_DONTIMPORT),
/*
* This empty row indicates the end of parameters available in
* all emulations.
*/
{{NULL,NULL,0},BR(NULL),NULL_GSU,0,0,NULL,NULL,NULL,0},
#define IPDEF8(A,B,C,D) {{NULL,A,D|PM_SCALAR|PM_SPECIAL},BR((void *)B),GSU(colonarr_gsu),0,0,NULL,C,NULL,0}
IPDEF8("CDPATH", &cdpath, "cdpath", PM_TIED),
IPDEF8("FIGNORE", &fignore, "fignore", PM_TIED),
IPDEF8("FPATH", &fpath, "fpath", PM_TIED),
IPDEF8("MAILPATH", &mailpath, "mailpath", PM_TIED),
IPDEF8("WATCH", &watch, "watch", PM_TIED),
IPDEF8("PATH", &path, "path", PM_RESTRICTED|PM_TIED),
IPDEF8("PSVAR", &psvar, "psvar", PM_TIED),
IPDEF8("ZSH_EVAL_CONTEXT", &zsh_eval_context, "zsh_eval_context", PM_READONLY_SPECIAL|PM_TIED),
/* MODULE_PATH is not imported for security reasons */
IPDEF8("MODULE_PATH", &module_path, "module_path", PM_DONTIMPORT|PM_RESTRICTED|PM_TIED),
#define IPDEF10(A,B) {{NULL,A,PM_ARRAY|PM_SPECIAL},BR(NULL),GSU(B),10,0,NULL,NULL,NULL,0}
/*
* The following parameters are not available in sh/ksh compatibility *
* mode.
*/
/* All of these have sh compatible equivalents. */
IPDEF1("ARGC", argc_gsu, PM_READONLY_SPECIAL),
IPDEF2("HISTCHARS", histchars_gsu, PM_DONTIMPORT),
IPDEF4("status", &lastval),
IPDEF7("prompt", &prompt),
IPDEF7("PROMPT", &prompt),
IPDEF7("PROMPT2", &prompt2),
IPDEF7("PROMPT3", &prompt3),
IPDEF7("PROMPT4", &prompt4),
IPDEF8("MANPATH", &manpath, "manpath", PM_TIED),
IPDEF9("argv", &pparams, NULL, 0),
IPDEF9("fignore", &fignore, "FIGNORE", PM_TIED),
IPDEF9("cdpath", &cdpath, "CDPATH", PM_TIED),
IPDEF9("fpath", &fpath, "FPATH", PM_TIED),
IPDEF9("mailpath", &mailpath, "MAILPATH", PM_TIED),
IPDEF9("manpath", &manpath, "MANPATH", PM_TIED),
IPDEF9("psvar", &psvar, "PSVAR", PM_TIED),
IPDEF9("watch", &watch, "WATCH", PM_TIED),
IPDEF9("zsh_eval_context", &zsh_eval_context, "ZSH_EVAL_CONTEXT", PM_TIED|PM_READONLY_SPECIAL),
IPDEF9("module_path", &module_path, "MODULE_PATH", PM_TIED|PM_RESTRICTED),
IPDEF9("path", &path, "PATH", PM_TIED|PM_RESTRICTED),
/* These are known to zsh alone. */
IPDEF10("pipestatus", pipestatus_gsu),
{{NULL,NULL,0},BR(NULL),NULL_GSU,0,0,NULL,NULL,NULL,0},
};
/*
* Alternative versions of colon-separated path parameters for
* sh emulation. These don't link to the array versions.
*/
static initparam special_params_sh[] = {
IPDEF8("CDPATH", &cdpath, NULL, 0),
IPDEF8("FIGNORE", &fignore, NULL, 0),
IPDEF8("FPATH", &fpath, NULL, 0),
IPDEF8("MAILPATH", &mailpath, NULL, 0),
IPDEF8("WATCH", &watch, NULL, 0),
IPDEF8("PATH", &path, NULL, PM_RESTRICTED),
IPDEF8("PSVAR", &psvar, NULL, 0),
IPDEF8("ZSH_EVAL_CONTEXT", &zsh_eval_context, NULL, PM_READONLY_SPECIAL),
/* MODULE_PATH is not imported for security reasons */
IPDEF8("MODULE_PATH", &module_path, NULL, PM_DONTIMPORT|PM_RESTRICTED),
{{NULL,NULL,0},BR(NULL),NULL_GSU,0,0,NULL,NULL,NULL,0},
};
/*
* Special way of referring to the positional parameters. Unlike $*
* and $@, this is not readonly. This parameter is not directly
* visible in user space.
*/
static initparam argvparam_pm = IPDEF9("", &pparams, NULL, \
PM_ARRAY|PM_SPECIAL|PM_DONTIMPORT);
#undef BR
#define IS_UNSET_VALUE(V) \
((V) && (!(V)->pm || ((V)->pm->node.flags & PM_UNSET) || \
!(V)->pm->node.nam || !*(V)->pm->node.nam))
static Param argvparam;
/* "parameter table" - hash table containing the parameters
*
* realparamtab always points to the shell's global table. paramtab is sometimes
* temporarily changed to point at another table, while dealing with the keys
* of an associative array (for example, see makecompparams() which initializes
* the associative array ${compstate}).
*/
/**/
mod_export HashTable paramtab, realparamtab;
/**/
mod_export HashTable
newparamtable(int size, char const *name)
{
HashTable ht;
if (!size)
size = 17;
ht = newhashtable(size, name, NULL);
ht->hash = hasher;
ht->emptytable = emptyhashtable;
ht->filltable = NULL;
ht->cmpnodes = strcmp;
ht->addnode = addhashnode;
ht->getnode = getparamnode;
ht->getnode2 = gethashnode2;
ht->removenode = removehashnode;
ht->disablenode = NULL;
ht->enablenode = NULL;
ht->freenode = freeparamnode;
ht->printnode = printparamnode;
return ht;
}
/**/
static HashNode
getparamnode(HashTable ht, const char *nam)
{
HashNode hn = gethashnode2(ht, nam);
Param pm = (Param) hn;
if (pm && pm->u.str && (pm->node.flags & PM_AUTOLOAD)) {
char *mn = dupstring(pm->u.str);
(void)ensurefeature(mn, "p:", (pm->node.flags & PM_AUTOALL) ? NULL :
nam);
hn = gethashnode2(ht, nam);
if (!hn) {
/*
* This used to be a warning, but surely if we allow
* stuff to go ahead with the autoload stub with
* no error status we're in for all sorts of mayhem?
*/
zerr("autoloading module %s failed to define parameter: %s", mn,
nam);
}
}
return hn;
}
/* Copy a parameter hash table */
static HashTable outtable;
/**/
static void
scancopyparams(HashNode hn, UNUSED(int flags))
{
/* Going into a real parameter, so always use permanent storage */
Param pm = (Param)hn;
Param tpm = (Param) zshcalloc(sizeof *tpm);
tpm->node.nam = ztrdup(pm->node.nam);
copyparam(tpm, pm, 0);
addhashnode(outtable, tpm->node.nam, tpm);
}
/**/
HashTable
copyparamtable(HashTable ht, char *name)
{
HashTable nht = 0;
if (ht) {
nht = newparamtable(ht->hsize, name);
outtable = nht;
scanhashtable(ht, 0, 0, 0, scancopyparams, 0);
outtable = NULL;
}
return nht;
}
/* Flag to freeparamnode to unset the struct */
static int delunset;
/* Function to delete a parameter table. */
/**/
mod_export void
deleteparamtable(HashTable t)
{
/* The parameters in the hash table need to be unset *
* before being deleted. */
int odelunset = delunset;
delunset = 1;
deletehashtable(t);
delunset = odelunset;
}
static unsigned numparamvals;
/**/
mod_export void
scancountparams(UNUSED(HashNode hn), int flags)
{
++numparamvals;
if ((flags & SCANPM_WANTKEYS) && (flags & SCANPM_WANTVALS))
++numparamvals;
}
static Patprog scanprog;
static char *scanstr;
static char **paramvals;
static Param foundparam;
/**/
static void
scanparamvals(HashNode hn, int flags)
{
struct value v;
Patprog prog;
if (numparamvals && !(flags & SCANPM_MATCHMANY) &&
(flags & (SCANPM_MATCHVAL|SCANPM_MATCHKEY|SCANPM_KEYMATCH)))
return;
v.pm = (Param)hn;
if ((flags & SCANPM_KEYMATCH)) {
char *tmp = dupstring(v.pm->node.nam);
tokenize(tmp);
remnulargs(tmp);
if (!(prog = patcompile(tmp, 0, NULL)) || !pattry(prog, scanstr))
return;
} else if ((flags & SCANPM_MATCHKEY) && !pattry(scanprog, v.pm->node.nam)) {
return;
}
foundparam = v.pm;
if (flags & SCANPM_WANTKEYS) {
paramvals[numparamvals++] = v.pm->node.nam;
if (!(flags & (SCANPM_WANTVALS|SCANPM_MATCHVAL)))
return;
}
v.isarr = (PM_TYPE(v.pm->node.flags) & (PM_ARRAY|PM_HASHED));
v.flags = 0;
v.start = 0;
v.end = -1;
paramvals[numparamvals] = getstrvalue(&v);
if (flags & SCANPM_MATCHVAL) {
if (pattry(scanprog, paramvals[numparamvals])) {
numparamvals += ((flags & SCANPM_WANTVALS) ? 1 :
!(flags & SCANPM_WANTKEYS));
} else if (flags & SCANPM_WANTKEYS)
--numparamvals; /* Value didn't match, discard key */
} else
++numparamvals;
foundparam = NULL;
}
/**/
char **
paramvalarr(HashTable ht, int flags)
{
DPUTS((flags & (SCANPM_MATCHKEY|SCANPM_MATCHVAL)) && !scanprog,
"BUG: scanning hash without scanprog set");
numparamvals = 0;
if (ht)
scanhashtable(ht, 0, 0, PM_UNSET, scancountparams, flags);
paramvals = (char **) zhalloc((numparamvals + 1) * sizeof(char *));
if (ht) {
numparamvals = 0;
scanhashtable(ht, 0, 0, PM_UNSET, scanparamvals, flags);
}
paramvals[numparamvals] = 0;
return paramvals;
}
/* Return the full array (no indexing) referred to by a Value. *
* The array value is cached for the lifetime of the Value. */
/**/
static char **
getvaluearr(Value v)
{
if (v->arr)
return v->arr;
else if (PM_TYPE(v->pm->node.flags) == PM_ARRAY)
return v->arr = v->pm->gsu.a->getfn(v->pm);
else if (PM_TYPE(v->pm->node.flags) == PM_HASHED) {
v->arr = paramvalarr(v->pm->gsu.h->getfn(v->pm), v->isarr);
/* Can't take numeric slices of associative arrays */
v->start = 0;
v->end = numparamvals + 1;
return v->arr;
} else
return NULL;
}
/* Return whether the variable is set *
* checks that array slices are within range *
* used for [[ -v ... ]] condition test */
/**/
int
issetvar(char *name)
{
struct value vbuf;
Value v;
int slice;
char **arr;
if (!(v = getvalue(&vbuf, &name, 1)) || *name)
return 0; /* no value or more chars after the variable name */
if (v->isarr & ~SCANPM_ARRONLY)
return v->end > 1; /* for extracted elements, end gives us a count */
slice = v->start != 0 || v->end != -1;
if (PM_TYPE(v->pm->node.flags) != PM_ARRAY || !slice)
return !slice && !(v->pm->node.flags & PM_UNSET);
if (!v->end) /* empty array slice */
return 0;
/* get the array and check end is within range */
if (!(arr = getvaluearr(v)))
return 0;
return arrlen_ge(arr, v->end < 0 ? - v->end : v->end);
}
/*
* Split environment string into (name, value) pair.
* this is used to avoid in-place editing of environment table
* that results in core dump on some systems
*/
static int
split_env_string(char *env, char **name, char **value)
{
char *str, *tenv;
if (!env || !name || !value)
return 0;
tenv = strcpy(zhalloc(strlen(env) + 1), env);
for (str = tenv; *str && *str != '='; str++) {
if (STOUC(*str) >= 128) {
/*
* We'll ignore environment variables with names not
* from the portable character set since we don't
* know of a good reason to accept them.
*/
return 0;
}
}
if (str != tenv && *str == '=') {
*str = '\0';
*name = tenv;
*value = str + 1;
return 1;
} else
return 0;
}
/**
* Check parameter flags to see if parameter shouldn't be imported
* from environment at start.
*
* return 1: don't import: 0: ok to import.
*/
static int dontimport(int flags)
{
/* If explicitly marked as don't export */
if (flags & PM_DONTIMPORT)
return 1;
/* If value already exported */
if (flags & PM_EXPORTED)
return 1;
/* If security issue when importing and running with some privilege */
if ((flags & PM_DONTIMPORT_SUID) && isset(PRIVILEGED))
return 1;
/* OK to import */
return 0;
}
/* Set up parameter hash table. This will add predefined *
* parameter entries as well as setting up parameter table *
* entries for environment variables we inherit. */
/**/
void
createparamtable(void)
{
Param ip, pm;
#if !defined(HAVE_PUTENV) && !defined(USE_SET_UNSET_ENV)
char **new_environ;
int envsize;
#endif
#ifndef USE_SET_UNSET_ENV
char **envp;
#endif
char **envp2, **sigptr, **t;
char buf[50], *str, *iname, *ivalue, *hostnam;
int oae = opts[ALLEXPORT];
#ifdef HAVE_UNAME
struct utsname unamebuf;
char *machinebuf;
#endif
paramtab = realparamtab = newparamtable(151, "paramtab");
/* Add the special parameters to the hash table */
for (ip = special_params; ip->node.nam; ip++)
paramtab->addnode(paramtab, ztrdup(ip->node.nam), ip);
if (EMULATION(EMULATE_SH|EMULATE_KSH)) {
for (ip = special_params_sh; ip->node.nam; ip++)
paramtab->addnode(paramtab, ztrdup(ip->node.nam), ip);
} else {
while ((++ip)->node.nam)
paramtab->addnode(paramtab, ztrdup(ip->node.nam), ip);
}
argvparam = (Param) &argvparam_pm;
noerrs = 2;
/* Add the standard non-special parameters which have to *
* be initialized before we copy the environment variables. *
* We don't want to override whatever values the user has *
* given them in the environment. */
opts[ALLEXPORT] = 0;
setiparam("MAILCHECK", 60);
setiparam("LOGCHECK", 60);
setiparam("KEYTIMEOUT", 40);
setiparam("LISTMAX", 100);
/*
* We used to get the output baud rate here. However, that's
* pretty irrelevant to a terminal on an X display and can lead
* to unnecessary delays if it's wrong (which it probably is).
* Furthermore, even if the output is slow it's very likely
* to be because of WAN delays, not covered by the output
* baud rate.
* So allow the user to set it in the special cases where it's
* useful.
*/
setsparam("TMPPREFIX", ztrdup_metafy(DEFAULT_TMPPREFIX));
setsparam("TIMEFMT", ztrdup_metafy(DEFAULT_TIMEFMT));
setsparam("WATCHFMT", ztrdup_metafy(default_watchfmt));
hostnam = (char *)zalloc(256);
gethostname(hostnam, 256);
setsparam("HOST", ztrdup_metafy(hostnam));
zfree(hostnam, 256);
setsparam("LOGNAME",
ztrdup_metafy((str = getlogin()) && *str ?
str : cached_username));
#if !defined(HAVE_PUTENV) && !defined(USE_SET_UNSET_ENV)
/* Copy the environment variables we are inheriting to dynamic *
* memory, so we can do mallocs and frees on it. */
envsize = sizeof(char *)*(1 + arrlen(environ));
new_environ = (char **) zalloc(envsize);
memcpy(new_environ, environ, envsize);
environ = new_environ;
#endif
/* Use heap allocation to avoid many small alloc/free calls */
pushheap();
/* Now incorporate environment variables we are inheriting *
* into the parameter hash table. Copy them into dynamic *
* memory so that we can free them if needed */
for (
#ifndef USE_SET_UNSET_ENV
envp =
#endif
envp2 = environ; *envp2; envp2++) {
if (split_env_string(*envp2, &iname, &ivalue)) {
if (!idigit(*iname) && isident(iname) && !strchr(iname, '[')) {
/*
* Parameters that aren't already in the parameter table
* aren't special to the shell, so it's always OK to
* import. Otherwise, check parameter flags.
*/
if ((!(pm = (Param) paramtab->getnode(paramtab, iname)) ||
!dontimport(pm->node.flags)) &&
(pm = assignsparam(iname, metafy(ivalue, -1, META_DUP),
ASSPM_ENV_IMPORT))) {
pm->node.flags |= PM_EXPORTED;
if (pm->node.flags & PM_SPECIAL)
pm->env = mkenvstr (pm->node.nam,
getsparam(pm->node.nam), pm->node.flags);
else
pm->env = ztrdup(*envp2);
#ifndef USE_SET_UNSET_ENV
*envp++ = pm->env;
#endif
}
}
}
}
popheap();
#ifndef USE_SET_UNSET_ENV
*envp = NULL;
#endif
opts[ALLEXPORT] = oae;
/*
* For native emulation we always set the variable home
* (see setupvals()).
*/
pm = (Param) paramtab->getnode(paramtab, "HOME");
if (EMULATION(EMULATE_ZSH))
{
pm->node.flags &= ~PM_UNSET;
if (!(pm->node.flags & PM_EXPORTED))
addenv(pm, home);
} else if (!home)
pm->node.flags |= PM_UNSET;
pm = (Param) paramtab->getnode(paramtab, "LOGNAME");
if (!(pm->node.flags & PM_EXPORTED))
addenv(pm, pm->u.str);
pm = (Param) paramtab->getnode(paramtab, "SHLVL");
sprintf(buf, "%d", (int)++shlvl);
/* shlvl value in environment needs updating unconditionally */
addenv(pm, buf);
/* Add the standard non-special parameters */
set_pwd_env();
#ifdef HAVE_UNAME
if(uname(&unamebuf)) setsparam("CPUTYPE", ztrdup("unknown"));
else
{
machinebuf = ztrdup_metafy(unamebuf.machine);
setsparam("CPUTYPE", machinebuf);
}
#else
setsparam("CPUTYPE", ztrdup_metafy("unknown"));
#endif
setsparam("MACHTYPE", ztrdup_metafy(MACHTYPE));
setsparam("OSTYPE", ztrdup_metafy(OSTYPE));
setsparam("TTY", ztrdup_metafy(ttystrname));
setsparam("VENDOR", ztrdup_metafy(VENDOR));
setsparam("ZSH_ARGZERO", ztrdup(posixzero));
setsparam("ZSH_VERSION", ztrdup_metafy(ZSH_VERSION));
setsparam("ZSH_PATCHLEVEL", ztrdup_metafy(ZSH_PATCHLEVEL));
setaparam("signals", sigptr = zalloc((SIGCOUNT+4) * sizeof(char *)));
for (t = sigs; (*sigptr++ = ztrdup_metafy(*t++)); );
noerrs = 0;
}
/* assign various functions used for non-special parameters */
/**/
mod_export void
assigngetset(Param pm)
{
switch (PM_TYPE(pm->node.flags)) {
case PM_SCALAR:
pm->gsu.s = &stdscalar_gsu;
break;
case PM_INTEGER:
pm->gsu.i = &stdinteger_gsu;
break;
case PM_EFLOAT:
case PM_FFLOAT:
pm->gsu.f = &stdfloat_gsu;
break;
case PM_ARRAY:
pm->gsu.a = &stdarray_gsu;
break;
case PM_HASHED:
pm->gsu.h = &stdhash_gsu;
break;
default:
DPUTS(1, "BUG: tried to create param node without valid flag");
break;
}
}
/* Create a parameter, so that it can be assigned to. Returns NULL if the *
* parameter already exists or can't be created, otherwise returns the *
* parameter node. If a parameter of the same name exists in an outer *
* scope, it is hidden by a newly created parameter. An already existing *
* parameter node at the current level may be `created' and returned *
* provided it is unset and not special. If the parameter can't be *
* created because it already exists, the PM_UNSET flag is cleared. */
/**/
mod_export Param
createparam(char *name, int flags)
{
Param pm, oldpm;
if (paramtab != realparamtab)
flags = (flags & ~PM_EXPORTED) | PM_HASHELEM;
if (name != nulstring) {
oldpm = (Param) (paramtab == realparamtab ?
/* gethashnode2() for direct table read */
gethashnode2(paramtab, name) :
paramtab->getnode(paramtab, name));
DPUTS(oldpm && oldpm->level > locallevel,
"BUG: old local parameter not deleted");
if (oldpm && (oldpm->level == locallevel || !(flags & PM_LOCAL))) {
if (isset(POSIXBUILTINS) && (oldpm->node.flags & PM_READONLY)) {
zerr("read-only variable: %s", name);