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
|
/*
* parameter.c - parameter interface to zsh internals
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1999 Sven Wischnowsky
* 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 Sven Wischnowsky 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 Sven Wischnowsky and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Sven Wischnowsky 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 Sven Wischnowsky and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*
*/
#include "parameter.mdh"
#include "parameter.pro"
/* Empty dummy function for special hash parameters. */
/**/
static void
shempty(void)
{
}
/* Create a simple special hash parameter. */
/**/
static Param
createspecialhash(char *name, GetNodeFunc get, ScanTabFunc scan)
{
Param pm;
HashTable ht;
if (!(pm = createparam(name, PM_SPECIAL|PM_HIDE|PM_REMOVABLE|PM_HASHED)))
return NULL;
pm->level = pm->old ? locallevel : 0;
pm->gets.hfn = hashgetfn;
pm->sets.hfn = hashsetfn;
pm->unsetfn = stdunsetfn;
pm->u.hash = ht = newhashtable(7, name, NULL);
ht->hash = hasher;
ht->emptytable = (TableFunc) shempty;
ht->filltable = NULL;
ht->addnode = (AddNodeFunc) shempty;
ht->getnode = ht->getnode2 = get;
ht->removenode = (RemoveNodeFunc) shempty;
ht->disablenode = NULL;
ht->enablenode = NULL;
ht->freenode = (FreeNodeFunc) shempty;
ht->printnode = printparamnode;
ht->scantab = scan;
return pm;
}
/* Functions for the parameters special parameter. */
/* Return a string describing the type of a parameter. */
/**/
static char *
paramtypestr(Param pm)
{
char *val = NULL;
int f = pm->flags;
if (!(f & PM_UNSET)) {
if (pm->flags & PM_AUTOLOAD)
return dupstring("undefined");
switch (PM_TYPE(f)) {
case PM_SCALAR: val = "scalar"; break;
case PM_ARRAY: val = "array"; break;
case PM_INTEGER: val = "integer"; break;
case PM_EFLOAT:
case PM_FFLOAT: val = "float"; break;
case PM_HASHED: val = "association"; break;
}
DPUTS(!val, "BUG: type not handled in parameter");
val = dupstring(val);
if (pm->level)
val = dyncat(val, "-local");
if (f & PM_LEFT)
val = dyncat(val, "-left");
if (f & PM_RIGHT_B)
val = dyncat(val, "-right_blanks");
if (f & PM_RIGHT_Z)
val = dyncat(val, "-right_zeros");
if (f & PM_LOWER)
val = dyncat(val, "-lower");
if (f & PM_UPPER)
val = dyncat(val, "-upper");
if (f & PM_READONLY)
val = dyncat(val, "-readonly");
if (f & PM_TAGGED)
val = dyncat(val, "-tag");
if (f & PM_EXPORTED)
val = dyncat(val, "-export");
if (f & PM_UNIQUE)
val = dyncat(val, "-unique");
} else
val = dupstring("");
return val;
}
/**/
static HashNode
getpmparameter(HashTable ht, char *name)
{
Param rpm, pm = NULL;
HEAPALLOC {
pm = (Param) zhalloc(sizeof(struct param));
pm->nam = dupstring(name);
pm->flags = PM_SCALAR | PM_READONLY;
pm->sets.cfn = NULL;
pm->gets.cfn = strgetfn;
pm->unsetfn = NULL;
pm->ct = 0;
pm->env = NULL;
pm->ename = NULL;
pm->old = NULL;
pm->level = 0;
if ((rpm = (Param) realparamtab->getnode(realparamtab, name)) &&
!(rpm->flags & PM_UNSET))
pm->u.str = paramtypestr(rpm);
else {
pm->u.str = "";
pm->flags |= PM_UNSET;
}
} LASTALLOC;
return (HashNode) pm;
}
/**/
static void
scanpmparameters(HashTable ht, ScanFunc func, int flags)
{
struct param pm;
int i;
HashNode hn;
pm.flags = PM_SCALAR | PM_READONLY;
pm.sets.cfn = NULL;
pm.gets.cfn = strgetfn;
pm.unsetfn = NULL;
pm.ct = 0;
pm.env = NULL;
pm.ename = NULL;
pm.old = NULL;
pm.level = 0;
for (i = 0; i < realparamtab->hsize; i++)
for (hn = realparamtab->nodes[i]; hn; hn = hn->next) {
pm.nam = hn->nam;
if (func != scancountparams)
pm.u.str = paramtypestr((Param) hn);
func((HashNode) &pm, flags);
}
}
/* Functions for the commands special parameter. */
/**/
static void
setpmcommand(Param pm, char *value)
{
if (isset(RESTRICTED))
zwarnnam(NULL, "restricted: %s", value, 0);
else {
Cmdnam cn = zcalloc(sizeof(*cn));
cn->flags = HASHED;
cn->u.cmd = value;
cmdnamtab->addnode(cmdnamtab, ztrdup(pm->nam), (HashNode) cn);
}
}
/**/
static void
unsetpmcommand(Param pm, int exp)
{
HashNode hn = cmdnamtab->removenode(cmdnamtab, pm->nam);
if (hn)
cmdnamtab->freenode(hn);
}
/**/
static void
setpmcommands(Param pm, HashTable ht)
{
int i;
HashNode hn;
if (!ht)
return;
for (i = 0; i < ht->hsize; i++)
for (hn = ht->nodes[i]; hn; hn = hn->next) {
Cmdnam cn = zcalloc(sizeof(*cn));
struct value v;
v.isarr = v.inv = v.a = 0;
v.b = -1;
v.arr = NULL;
v.pm = (Param) hn;
cn->flags = HASHED;
cn->u.cmd = ztrdup(getstrvalue(&v));
cmdnamtab->addnode(cmdnamtab, ztrdup(hn->nam), (HashNode) cn);
}
deleteparamtable(ht);
}
/**/
static HashNode
getpmcommand(HashTable ht, char *name)
{
Cmdnam cmd;
Param pm = NULL;
if (!(cmd = (Cmdnam) cmdnamtab->getnode(cmdnamtab, name)) &&
isset(HASHLISTALL)) {
cmdnamtab->filltable(cmdnamtab);
cmd = (Cmdnam) cmdnamtab->getnode(cmdnamtab, name);
}
HEAPALLOC {
pm = (Param) zhalloc(sizeof(struct param));
pm->nam = dupstring(name);
pm->flags = PM_SCALAR;
pm->sets.cfn = setpmcommand;
pm->gets.cfn = strgetfn;
pm->unsetfn = unsetpmcommand;
pm->ct = 0;
pm->env = NULL;
pm->ename = NULL;
pm->old = NULL;
pm->level = 0;
if (cmd) {
if (cmd->flags & HASHED)
pm->u.str = cmd->u.cmd;
else {
pm->u.str = zhalloc(strlen(*(cmd->u.name)) +
strlen(name) + 2);
strcpy(pm->u.str, *(cmd->u.name));
strcat(pm->u.str, "/");
strcat(pm->u.str, name);
}
} else {
pm->u.str = "";
pm->flags |= PM_UNSET;
}
} LASTALLOC;
return (HashNode) pm;
}
/**/
static void
scanpmcommands(HashTable ht, ScanFunc func, int flags)
{
struct param pm;
int i;
HashNode hn;
Cmdnam cmd;
if (isset(HASHLISTALL))
cmdnamtab->filltable(cmdnamtab);
pm.flags = PM_SCALAR;
pm.sets.cfn = setpmcommand;
pm.gets.cfn = strgetfn;
pm.unsetfn = unsetpmcommand;
pm.ct = 0;
pm.env = NULL;
pm.ename = NULL;
pm.old = NULL;
pm.level = 0;
for (i = 0; i < cmdnamtab->hsize; i++)
for (hn = cmdnamtab->nodes[i]; hn; hn = hn->next) {
pm.nam = hn->nam;
cmd = (Cmdnam) hn;
if (func != scancountparams) {
if (cmd->flags & HASHED)
pm.u.str = cmd->u.cmd;
else {
pm.u.str = zhalloc(strlen(*(cmd->u.name)) +
strlen(cmd->nam) + 2);
strcpy(pm.u.str, *(cmd->u.name));
strcat(pm.u.str, "/");
strcat(pm.u.str, cmd->nam);
}
}
func((HashNode) &pm, flags);
}
}
/* Functions for the functions special parameter. */
/**/
static void
setfunction(char *name, char *val)
{
char *value = dupstring(val);
Shfunc shf;
List list;
int sn;
val = metafy(val, strlen(val), META_REALLOC);
HEAPALLOC {
list = parse_string(val, 1);
} LASTALLOC;
if (!list || list == &dummy_list) {
zwarnnam(NULL, "invalid function definition", value, 0);
zsfree(val);
return;
}
PERMALLOC {
shf = (Shfunc) zalloc(sizeof(*shf));
shf->funcdef = (List) dupstruct(list);
shf->flags = 0;
if (!strncmp(name, "TRAP", 4) &&
(sn = getsignum(name + 4)) != -1) {
if (settrap(sn, shf->funcdef)) {
freestruct(shf->funcdef);
zfree(shf, sizeof(*shf));
zsfree(val);
LASTALLOC_RETURN;
}
sigtrapped[sn] |= ZSIG_FUNC;
}
shfunctab->addnode(shfunctab, ztrdup(name), shf);
} LASTALLOC;
zsfree(val);
}
/**/
static void
setpmfunction(Param pm, char *value)
{
setfunction(pm->nam, value);
}
/**/
static void
unsetpmfunction(Param pm, int exp)
{
HashNode hn = shfunctab->removenode(shfunctab, pm->nam);
if (hn)
shfunctab->freenode(hn);
}
/**/
static void
setpmfunctions(Param pm, HashTable ht)
{
int i;
HashNode hn;
if (!ht)
return;
for (i = 0; i < ht->hsize; i++)
for (hn = ht->nodes[i]; hn; hn = hn->next) {
struct value v;
v.isarr = v.inv = v.a = 0;
v.b = -1;
v.arr = NULL;
v.pm = (Param) hn;
setfunction(hn->nam, ztrdup(getstrvalue(&v)));
}
deleteparamtable(ht);
}
/**/
static HashNode
getpmfunction(HashTable ht, char *name)
{
Shfunc shf;
Param pm = NULL;
HEAPALLOC {
pm = (Param) zhalloc(sizeof(struct param));
pm->nam = dupstring(name);
pm->flags = PM_SCALAR;
pm->sets.cfn = setpmfunction;
pm->gets.cfn = strgetfn;
pm->unsetfn = unsetpmfunction;
pm->ct = 0;
pm->env = NULL;
pm->ename = NULL;
pm->old = NULL;
pm->level = 0;
if ((shf = (Shfunc) shfunctab->getnode(shfunctab, name))) {
if (shf->flags & PM_UNDEFINED)
pm->u.str = tricat("builtin autoload -X",
((shf->flags & PM_UNALIASED)? "U" : ""),
((shf->flags & PM_TAGGED)? "t" : ""));
else {
char *t = getpermtext((void *) dupstruct((void *)
shf->funcdef)), *h;
h = dupstring(t);
zsfree(t);
unmetafy(h, NULL);
pm->u.str = h;
}
} else {
pm->u.str = "";
pm->flags |= PM_UNSET;
}
} LASTALLOC;
return (HashNode) pm;
}
/**/
static void
scanpmfunctions(HashTable ht, ScanFunc func, int flags)
{
struct param pm;
int i;
HashNode hn;
pm.flags = PM_SCALAR;
pm.sets.cfn = setpmcommand;
pm.gets.cfn = strgetfn;
pm.unsetfn = unsetpmcommand;
pm.ct = 0;
pm.env = NULL;
pm.ename = NULL;
pm.old = NULL;
pm.level = 0;
for (i = 0; i < shfunctab->hsize; i++)
for (hn = shfunctab->nodes[i]; hn; hn = hn->next) {
if (!(hn->flags & DISABLED)) {
pm.nam = hn->nam;
if (func != scancountparams) {
if (((Shfunc) hn)->flags & PM_UNDEFINED) {
Shfunc shf = (Shfunc) hn;
pm.u.str = tricat("builtin autoload -X",
((shf->flags & PM_UNALIASED)? "U" : ""),
((shf->flags & PM_TAGGED)? "t" : ""));
} else {
char *t = getpermtext((void *)
dupstruct((void *) ((Shfunc) hn)->funcdef));
unmetafy((pm.u.str = dupstring(t)), NULL);
zsfree(t);
}
}
func((HashNode) &pm, flags);
}
}
}
/* Functions for the options special parameter. */
/**/
static void
setpmoption(Param pm, char *value)
{
int n;
if (!value || (strcmp(value, "on") && strcmp(value, "off")))
zwarnnam(NULL, "invalid value: %s", value, 0);
else if (!(n = optlookup(pm->nam)))
zwarnnam(NULL, "no such option: %s", pm->nam, 0);
else if (dosetopt(n, (value && strcmp(value, "off")), 0))
zwarnnam(NULL, "can't change option: %s", pm->nam, 0);
zsfree(value);
}
/**/
static void
unsetpmoption(Param pm, int exp)
{
int n;
if (!(n = optlookup(pm->nam)))
zwarnnam(NULL, "no such option: %s", pm->nam, 0);
else if (dosetopt(n, 0, 0))
zwarnnam(NULL, "can't change option: %s", pm->nam, 0);
}
/**/
static void
setpmoptions(Param pm, HashTable ht)
{
int i;
HashNode hn;
if (!ht)
return;
for (i = 0; i < ht->hsize; i++)
for (hn = ht->nodes[i]; hn; hn = hn->next) {
struct value v;
char *val;
v.isarr = v.inv = v.a = 0;
v.b = -1;
v.arr = NULL;
v.pm = (Param) hn;
val = getstrvalue(&v);
if (!val || (strcmp(val, "on") && strcmp(val, "off")))
zwarnnam(NULL, "invalid value: %s", val, 0);
else if (dosetopt(optlookup(hn->nam),
(val && strcmp(val, "off")), 0))
zwarnnam(NULL, "can't change option: %s", hn->nam, 0);
}
deleteparamtable(ht);
}
/**/
static HashNode
getpmoption(HashTable ht, char *name)
{
Param pm = NULL;
int n;
HEAPALLOC {
pm = (Param) zhalloc(sizeof(struct param));
pm->nam = dupstring(name);
pm->flags = PM_SCALAR;
pm->sets.cfn = setpmoption;
pm->gets.cfn = strgetfn;
pm->unsetfn = unsetpmoption;
pm->ct = 0;
pm->env = NULL;
pm->ename = NULL;
pm->old = NULL;
pm->level = 0;
if ((n = optlookup(name)))
pm->u.str = dupstring(opts[n] ? "on" : "off");
else {
pm->u.str = "";
pm->flags |= PM_UNSET;
}
} LASTALLOC;
return (HashNode) pm;
}
/**/
static void
scanpmoptions(HashTable ht, ScanFunc func, int flags)
{
struct param pm;
int i;
HashNode hn;
pm.flags = PM_SCALAR;
pm.sets.cfn = setpmoption;
pm.gets.cfn = strgetfn;
pm.unsetfn = unsetpmoption;
pm.ct = 0;
pm.env = NULL;
pm.ename = NULL;
pm.old = NULL;
pm.level = 0;
for (i = 0; i < optiontab->hsize; i++)
for (hn = optiontab->nodes[i]; hn; hn = hn->next) {
pm.nam = hn->nam;
pm.u.str = opts[((Optname) hn)->optno] ? "on" : "off";
func((HashNode) &pm, flags);
}
}
/* Functions for the modules special parameter. */
static char *modpmname;
static int modpmfound;
/**/
static void
modpmbuiltinscan(HashNode hn, int dummy)
{
if (!(((Builtin) hn)->flags & BINF_ADDED) &&
!strcmp(((Builtin) hn)->optstr, modpmname))
modpmfound = 1;
}
/**/
static void
modpmparamscan(HashNode hn, int dummy)
{
if ((((Param) hn)->flags & PM_AUTOLOAD) &&
!strcmp(((Param) hn)->u.str, modpmname))
modpmfound = 1;
}
/**/
static int
findmodnode(LinkList l, char *nam)
{
LinkNode node;
for (node = firstnode(l); node; incnode(node))
if (!strcmp(nam, (char *) getdata(node)))
return 1;
return 0;
}
/**/
static HashNode
getpmmodule(HashTable ht, char *name)
{
Param pm = NULL;
char *type = NULL;
LinkNode node;
HEAPALLOC {
pm = (Param) zhalloc(sizeof(struct param));
pm->nam = dupstring(name);
pm->flags = PM_SCALAR | PM_READONLY;
pm->sets.cfn = NULL;
pm->gets.cfn = strgetfn;
pm->unsetfn = NULL;
pm->ct = 0;
pm->env = NULL;
pm->ename = NULL;
pm->old = NULL;
pm->level = 0;
for (node = firstnode(bltinmodules); node; incnode(node))
if (!strcmp(name, (char *) getdata(node))) {
type = "builtin";
break;
}
#ifdef DYNAMIC
if (!type) {
Module m;
for (node = firstnode(modules); node; incnode(node)) {
m = (Module) getdata(node);
if (m->handle && !(m->flags & MOD_UNLOAD) &&
!strcmp(name, m->nam)) {
type = "loaded";
break;
}
}
}
modpmname = name;
modpmfound = 0;
if (!type) {
scanhashtable(builtintab, 0, 0, 0, modpmbuiltinscan, 0);
if (!modpmfound) {
Conddef p;
for (p = condtab; p; p = p->next)
if (p->module && !strcmp(name, p->module)) {
modpmfound = 1;
break;
}
if (!modpmfound)
scanhashtable(realparamtab, 0, 0, 0, modpmparamscan, 0);
}
if (modpmfound)
type = "autoloaded";
}
#endif
if (type)
pm->u.str = type;
else {
pm->u.str = "";
pm->flags |= PM_UNSET;
}
} LASTALLOC;
return (HashNode) pm;
}
/**/
static void
scanpmmodules(HashTable ht, ScanFunc func, int flags)
{
struct param pm;
int i;
HashNode hn;
LinkList done = newlinklist();
LinkNode node;
Module m;
Conddef p;
pm.flags = PM_SCALAR | PM_READONLY;
pm.sets.cfn = NULL;
pm.gets.cfn = strgetfn;
pm.unsetfn = NULL;
pm.ct = 0;
pm.env = NULL;
pm.ename = NULL;
pm.old = NULL;
pm.level = 0;
for (node = firstnode(bltinmodules); node; incnode(node)) {
pm.nam = (char *) getdata(node);
addlinknode(done, pm.nam);
pm.u.str = "builtin";
func((HashNode) &pm, flags);
}
#ifdef DYNAMIC
for (node = firstnode(modules); node; incnode(node)) {
m = (Module) getdata(node);
if (m->handle && !(m->flags & MOD_UNLOAD)) {
pm.nam = m->nam;
addlinknode(done, pm.nam);
pm.u.str = "loaded";
func((HashNode) &pm, flags);
}
}
for (i = 0; i < builtintab->hsize; i++)
for (hn = builtintab->nodes[i]; hn; hn = hn->next) {
if (!(((Builtin) hn)->flags & BINF_ADDED) &&
!findmodnode(done, ((Builtin) hn)->optstr)) {
pm.nam = ((Builtin) hn)->optstr;
addlinknode(done, pm.nam);
pm.u.str = "autoloaded";
func((HashNode) &pm, flags);
}
}
for (p = condtab; p; p = p->next)
if (p->module && !findmodnode(done, p->module)) {
pm.nam = p->module;
addlinknode(done, pm.nam);
pm.u.str = "autoloaded";
func((HashNode) &pm, flags);
}
for (i = 0; i < realparamtab->hsize; i++)
for (hn = realparamtab->nodes[i]; hn; hn = hn->next) {
if ((((Param) hn)->flags & PM_AUTOLOAD) &&
!findmodnode(done, ((Param) hn)->u.str)) {
pm.nam = ((Param) hn)->u.str;
addlinknode(done, pm.nam);
pm.u.str = "autoloaded";
func((HashNode) &pm, flags);
}
}
#endif
}
/* Functions for the dirstack special parameter. */
static void
dirssetfn(Param pm, char **x)
{
PERMALLOC {
freelinklist(dirstack, freestr);
dirstack = newlinklist();
while (x && *x)
addlinknode(dirstack, ztrdup(*x++));
} LASTALLOC;
}
static char **
dirsgetfn(Param pm)
{
int l = countlinknodes(dirstack);
char **ret = (char **) zhalloc((l + 1) * sizeof(char *)), **p;
LinkNode n;
for (n = firstnode(dirstack), p = ret; n; incnode(n), p++)
*p = dupstring((char *) getdata(n));
*p = NULL;
return ret;
}
/* Names and Params for the special parameters. */
#define PAR_NAM "parameters"
#define CMD_NAM "commands"
#define FUN_NAM "functions"
#define OPT_NAM "options"
#define MOD_NAM "modules"
#define DIR_NAM "dirstack"
static Param parpm, cmdpm, funpm, optpm, modpm, dirpm;
/**/
int
setup_parameter(Module m)
{
return 0;
}
/**/
int
boot_parameter(Module m)
{
/* Create the special associative arrays.
* As an example for autoloaded parameters, this is probably a bad
* example, because the zsh core doesn't support creation of
* special hashes, yet. */
unsetparam(PAR_NAM);
if (!(parpm = createspecialhash(PAR_NAM, getpmparameter,
scanpmparameters)))
return 1;
parpm->flags |= PM_READONLY;
unsetparam(CMD_NAM);
if (!(cmdpm = createspecialhash(CMD_NAM, getpmcommand,
scanpmcommands)))
return 1;
cmdpm->sets.hfn = setpmcommands;
unsetparam(FUN_NAM);
if (!(funpm = createspecialhash(FUN_NAM, getpmfunction,
scanpmfunctions)))
return 1;
funpm->sets.hfn = setpmfunctions;
unsetparam(OPT_NAM);
if (!(optpm = createspecialhash(OPT_NAM, getpmoption,
scanpmoptions)))
return 1;
optpm->sets.hfn = setpmoptions;
unsetparam(MOD_NAM);
if (!(modpm = createspecialhash(MOD_NAM, getpmmodule,
scanpmmodules)))
return 1;
modpm->flags |= PM_READONLY;
unsetparam(DIR_NAM);
if (!(dirpm = createparam(DIR_NAM,
PM_ARRAY|PM_HIDE|PM_SPECIAL|PM_REMOVABLE)))
return 1;
dirpm->sets.afn = dirssetfn;
dirpm->gets.afn = dirsgetfn;
dirpm->unsetfn = stdunsetfn;
return 0;
}
#ifdef MODULE
/**/
int
cleanup_parameter(Module m)
{
Param pm;
/* Remove the special parameters if they are still the same. */
if ((pm = (Param) paramtab->getnode(paramtab, PAR_NAM)) && pm == parpm) {
pm->flags &= ~PM_READONLY;
unsetparam_pm(pm, 0, 1);
}
if ((pm = (Param) paramtab->getnode(paramtab, CMD_NAM)) && pm == cmdpm)
unsetparam_pm(pm, 0, 1);
if ((pm = (Param) paramtab->getnode(paramtab, FUN_NAM)) && pm == funpm)
unsetparam_pm(pm, 0, 1);
if ((pm = (Param) paramtab->getnode(paramtab, OPT_NAM)) && pm == optpm)
unsetparam_pm(pm, 0, 1);
if ((pm = (Param) paramtab->getnode(paramtab, MOD_NAM)) && pm == modpm) {
pm->flags &= ~PM_READONLY;
unsetparam_pm(pm, 0, 1);
}
if ((pm = (Param) paramtab->getnode(paramtab, DIR_NAM)) && pm == dirpm)
unsetparam_pm(pm, 0, 1);
return 0;
}
/**/
int
finish_parameter(Module m)
{
return 0;
}
#endif
|