Repository navigation
Expand file tree
/
Copy pathmodule.c
More file actions
3639 lines (3221 loc) · 81.5 KB
/
Copy pathmodule.c
File metadata and controls
3639 lines (3221 loc) · 81.5 KB
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
/*
* module.c - deal with dynamic modules
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1996-1997 Zoltán Hidvégi
* 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 Zoltán Hidvégi 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 Zoltán Hidvégi and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Zoltán Hidvégi 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 Zoltán Hidvégi and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*/
#include "zsh.mdh"
#include "module.pro"
/*
* List of linked-in modules.
* This is set up at boot and remains for the life of the shell;
* entries do not appear in "zmodload" listings.
*/
/**/
LinkList linkedmodules;
/* $module_path ($MODULE_PATH) */
/**/
char **module_path;
/* Hash of modules */
/**/
mod_export HashTable modulestab;
/*
* Bit flags passed as the "flags" argument of a autofeaturefn_t.
* Used in other places, such as the final argument to
* do_module_features().
*/
enum {
/*
* `-i' option: ignore errors pertaining to redefinitions,
* or indicate to do_module_features() that it should be
* silent.
*/
FEAT_IGNORE = 0x0001,
/* If a condition, condition is infix rather than prefix */
FEAT_INFIX = 0x0002,
/*
* Enable all features in the module when autoloading.
* This is the traditional zmodload -a behaviour;
* zmodload -Fa only enables features explicitly marked for
* autoloading.
*/
FEAT_AUTOALL = 0x0004,
/*
* Remove feature: alternative to "-X:NAME" used if
* X is passed separately from NAME.
*/
FEAT_REMOVE = 0x0008,
/*
* For do_module_features(). Check that any autoloads
* for the module are actually provided.
*/
FEAT_CHECKAUTO = 0x0010
};
/*
* All functions to add or remove autoloadable features fit
* the following prototype.
*
* "module" is the name of the module.
*
* "feature" is the name of the feature, minus any type prefix.
*
* "flags" is a set of the bits above.
*
* The return value is 0 for success, -1 for failure with no
* message needed, and one of the following to indicate the calling
* function should print a message:
*
* 1: failed to add [type] `[feature]'
* 2: [feature]: no such [type]
* 3: [feature]: [type] is already defined
*/
typedef int (*autofeaturefn_t)(const char *module, const char *feature,
int flags);
/* Bits in the second argument to find_module. */
enum {
/*
* Resolve any aliases to the underlying module.
*/
FINDMOD_ALIASP = 0x0001,
/*
* Create an element for the module in the list if
* it is not found.
*/
FINDMOD_CREATE = 0x0002,
};
static void
freemodulenode(HashNode hn)
{
Module m = (Module) hn;
if (m->node.flags & MOD_ALIAS)
zsfree(m->u.alias);
zsfree(m->node.nam);
if (m->autoloads)
freelinklist(m->autoloads, freestr);
if (m->deps)
freelinklist(m->deps, freestr);
zfree(m, sizeof(*m));
}
/* flags argument to printmodulenode */
enum {
/* -L flag, output zmodload commands */
PRINTMOD_LIST = 0x0001,
/* -e flag */
PRINTMOD_EXIST = 0x0002,
/* -A flag */
PRINTMOD_ALIAS = 0x0004,
/* -d flag */
PRINTMOD_DEPS = 0x0008,
/* -F flag */
PRINTMOD_FEATURES = 0x0010,
/* -l flag in combination with -L flag */
PRINTMOD_LISTALL = 0x0020,
/* -a flag */
PRINTMOD_AUTO = 0x0040
};
/* Scan function for printing module details */
static void
printmodulenode(HashNode hn, int flags)
{
Module m = (Module)hn;
/*
* If we check for a module loaded under an alias, we
* need the name of the alias. We can use it in other
* cases, too.
*/
const char *modname = m->node.nam;
if (flags & PRINTMOD_DEPS) {
/*
* Print the module's dependencies.
*/
LinkNode n;
if (!m->deps)
return;
if (flags & PRINTMOD_LIST) {
printf("zmodload -d ");
if (modname[0] == '-')
fputs("-- ", stdout);
quotedzputs(modname, stdout);
} else {
nicezputs(modname, stdout);
putchar(':');
}
for (n = firstnode(m->deps); n; incnode(n)) {
putchar(' ');
if (flags & PRINTMOD_LIST)
quotedzputs((char *) getdata(n), stdout);
else
nicezputs((char *) getdata(n), stdout);
}
} else if (flags & PRINTMOD_EXIST) {
/*
* Just print the module name, provided the module is
* present under an alias or otherwise.
*/
if (m->node.flags & MOD_ALIAS) {
if (!(flags & PRINTMOD_ALIAS) ||
!(m = find_module(m->u.alias, FINDMOD_ALIASP, NULL)))
return;
}
if (!m->u.handle || (m->node.flags & MOD_UNLOAD))
return;
nicezputs(modname, stdout);
} else if (m->node.flags & MOD_ALIAS) {
/*
* Normal listing, but for aliases.
*/
if (flags & PRINTMOD_LIST) {
printf("zmodload -A ");
if (modname[0] == '-')
fputs("-- ", stdout);
quotedzputs(modname, stdout);
putchar('=');
quotedzputs(m->u.alias, stdout);
} else {
nicezputs(modname, stdout);
fputs(" -> ", stdout);
nicezputs(m->u.alias, stdout);
}
} else if (m->u.handle || (flags & PRINTMOD_AUTO)) {
/*
* Loaded module.
*/
if (flags & PRINTMOD_LIST) {
/*
* List with -L format. Possibly we are printing
* features, either enables or autoloads.
*/
char **features = NULL;
int *enables = NULL;
if (flags & PRINTMOD_AUTO) {
if (!m->autoloads || !firstnode(m->autoloads))
return;
} else if (flags & PRINTMOD_FEATURES) {
if (features_module(m, &features) ||
enables_module(m, &enables) ||
!*features)
return;
}
printf("zmodload ");
if (flags & PRINTMOD_AUTO) {
fputs("-Fa ", stdout);
} else if (features)
fputs("-F ", stdout);
if(modname[0] == '-')
fputs("-- ", stdout);
quotedzputs(modname, stdout);
if (flags & PRINTMOD_AUTO) {
LinkNode an;
for (an = firstnode(m->autoloads); an; incnode(an)) {
putchar(' ');
quotedzputs((char *)getdata(an), stdout);
}
} else if (features) {
const char *f;
while ((f = *features++)) {
int on = *enables++;
if (flags & PRINTMOD_LISTALL)
printf(" %s", on ? "+" : "-");
else if (!on)
continue;
else
putchar(' ');
quotedzputs(f, stdout);
}
}
} else /* -l */
nicezputs(modname, stdout);
} else
return;
putchar('\n');
}
/**/
HashTable
newmoduletable(int size, char const *name)
{
HashTable ht;
ht = newhashtable(size, name, NULL);
ht->hash = hasher;
ht->emptytable = emptyhashtable;
ht->filltable = NULL;
ht->cmpnodes = strcmp;
ht->addnode = addhashnode;
/* DISABLED is not supported */
ht->getnode = gethashnode2;
ht->getnode2 = gethashnode2;
ht->removenode = removehashnode;
ht->disablenode = NULL;
ht->enablenode = NULL;
ht->freenode = freemodulenode;
ht->printnode = printmodulenode;
return ht;
}
/************************************************************************
* zsh/main standard module functions
************************************************************************/
/* The `zsh/main' module contains all the base code that can't actually be *
* built as a separate module. It is initialised by main(), so there's *
* nothing for the boot function to do. */
/**/
int
setup_(UNUSED(Module m))
{
return 0;
}
/**/
int
features_(UNUSED(Module m), UNUSED(char ***features))
{
/*
* There are lots and lots of features, but they're not
* handled here.
*/
return 1;
}
/**/
int
enables_(UNUSED(Module m), UNUSED(int **enables))
{
return 1;
}
/**/
int
boot_(UNUSED(Module m))
{
return 0;
}
/**/
int
cleanup_(UNUSED(Module m))
{
return 0;
}
/**/
int
finish_(UNUSED(Module m))
{
return 0;
}
/************************************************************************
* Module utility functions
************************************************************************/
/* This registers a builtin module. */
/**/
void
register_module(char *n, Module_void_func setup,
Module_features_func features,
Module_enables_func enables,
Module_void_func boot,
Module_void_func cleanup,
Module_void_func finish)
{
Linkedmod m;
m = (Linkedmod) zalloc(sizeof(*m));
m->name = ztrdup(n);
m->setup = setup;
m->features = features;
m->enables = enables;
m->boot = boot;
m->cleanup = cleanup;
m->finish = finish;
zaddlinknode(linkedmodules, m);
}
/* Check if a module is linked in. */
/**/
Linkedmod
module_linked(char const *name)
{
LinkNode node;
for (node = firstnode(linkedmodules); node; incnode(node))
if (!strcmp(((Linkedmod) getdata(node))->name, name))
return (Linkedmod) getdata(node);
return NULL;
}
/************************************************************************
* Support for the various feature types.
* First, builtins.
************************************************************************/
/* addbuiltin() can be used to add a new builtin. It returns zero on *
* success, 1 on failure. The only possible type of failure is that *
* a builtin with the specified name already exists. An autoloaded *
* builtin can be replaced using this function. */
/**/
static int
addbuiltin(Builtin b)
{
Builtin bn = (Builtin) builtintab->getnode2(builtintab, b->node.nam);
if (bn && (bn->node.flags & BINF_ADDED))
return 1;
if (bn)
builtintab->freenode(builtintab->removenode(builtintab, b->node.nam));
builtintab->addnode(builtintab, b->node.nam, b);
return 0;
}
/* Define an autoloadable builtin. It returns 0 on success, or 1 on *
* failure. The only possible cause of failure is that a builtin *
* with the specified name already exists. */
/**/
static int
add_autobin(const char *module, const char *bnam, int flags)
{
Builtin bn;
int ret;
bn = zshcalloc(sizeof(*bn));
bn->node.nam = ztrdup(bnam);
bn->optstr = ztrdup(module);
if (flags & FEAT_AUTOALL)
bn->node.flags |= BINF_AUTOALL;
if ((ret = addbuiltin(bn))) {
builtintab->freenode(&bn->node);
if (!(flags & FEAT_IGNORE))
return 1;
}
return 0;
}
/* Remove the builtin added previously by addbuiltin(). Returns *
* zero on success and -1 if there is no builtin with that name. */
/**/
int
deletebuiltin(const char *nam)
{
Builtin bn;
bn = (Builtin) builtintab->removenode(builtintab, nam);
if (!bn)
return -1;
builtintab->freenode(&bn->node);
return 0;
}
/* Remove an autoloaded added by add_autobin */
/**/
static int
del_autobin(UNUSED(const char *module), const char *bnam, int flags)
{
Builtin bn = (Builtin) builtintab->getnode2(builtintab, bnam);
if (!bn) {
if(!(flags & FEAT_IGNORE))
return 2;
} else if (bn->node.flags & BINF_ADDED) {
if (!(flags & FEAT_IGNORE))
return 3;
} else
deletebuiltin(bnam);
return 0;
}
/*
* Manipulate a set of builtins. This should be called
* via setfeatureenables() (or, usually, via the next level up,
* handlefeatures()).
*
* "nam" is the name of the calling code builtin, probably "zmodload".
*
* "binl" is the builtin table containing an array of "size" builtins.
*
* "e" is either NULL, in which case all builtins in the
* table are removed, or else an array corresponding to "binl"
* with a 1 for builtins that are to be added and a 0 for builtins
* that are to be removed. Any builtin already in the appropriate
* state is left alone.
*
* Returns 1 on any error, 0 for success. The recommended way
* of handling errors is to compare the enables passed down
* with the set retrieved after the error to find what failed.
*/
/**/
static int
setbuiltins(char const *nam, Builtin binl, int size, int *e)
{
int ret = 0, n;
for(n = 0; n < size; n++) {
Builtin b = &binl[n];
if (e && *e++) {
if (b->node.flags & BINF_ADDED)
continue;
if (addbuiltin(b)) {
zwarnnam(nam,
"name clash when adding builtin `%s'", b->node.nam);
ret = 1;
} else {
b->node.flags |= BINF_ADDED;
}
} else {
if (!(b->node.flags & BINF_ADDED))
continue;
if (deletebuiltin(b->node.nam)) {
zwarnnam(nam, "builtin `%s' already deleted", b->node.nam);
ret = 1;
} else {
b->node.flags &= ~BINF_ADDED;
}
}
}
return ret;
}
/*
* Add multiple builtins. binl points to a table of `size' builtin
* structures. Those for which (.flags & BINF_ADDED) is false are to be
* added; that flag is set if they succeed.
*
* If any fail, an error message is printed, using nam as the leading name.
* Returns 0 on success, 1 for any failure.
*
* This should not be used from a module; instead, use handlefeatures().
*/
/**/
mod_export int
addbuiltins(char const *nam, Builtin binl, int size)
{
int ret = 0, n;
for(n = 0; n < size; n++) {
Builtin b = &binl[n];
if(b->node.flags & BINF_ADDED)
continue;
if(addbuiltin(b)) {
zwarnnam(nam, "name clash when adding builtin `%s'", b->node.nam);
ret = 1;
} else {
b->node.flags |= BINF_ADDED;
}
}
return ret;
}
/************************************************************************
* Function wrappers.
************************************************************************/
/* The list of function wrappers defined. */
/**/
FuncWrap wrappers;
/* This adds a definition for a wrapper. Return value is one in case of *
* error and zero if all went fine. */
/**/
mod_export int
addwrapper(Module m, FuncWrap w)
{
FuncWrap p, q;
/*
* We can't add a wrapper to an alias, since it's supposed
* to behave identically to the resolved module. This shouldn't
* happen since we usually add wrappers when a real module is
* loaded.
*/
if (m->node.flags & MOD_ALIAS)
return 1;
if (w->flags & WRAPF_ADDED)
return 1;
for (p = wrappers, q = NULL; p; q = p, p = p->next);
if (q)
q->next = w;
else
wrappers = w;
w->next = NULL;
w->flags |= WRAPF_ADDED;
w->module = m;
return 0;
}
/* This removes the given wrapper definition from the list. Returned is *
* one in case of error and zero otherwise. */
/**/
mod_export int
deletewrapper(Module m, FuncWrap w)
{
FuncWrap p, q;
if (m->node.flags & MOD_ALIAS)
return 1;
if (w->flags & WRAPF_ADDED) {
for (p = wrappers, q = NULL; p && p != w; q = p, p = p->next);
if (p) {
if (q)
q->next = p->next;
else
wrappers = p->next;
p->flags &= ~WRAPF_ADDED;
return 0;
}
}
return 1;
}
/************************************************************************
* Conditions.
************************************************************************/
/* The list of module-defined conditions. */
/**/
mod_export Conddef condtab;
/* This gets a condition definition with the given name. The first *
* argument says if we have to look for an infix condition. The last *
* argument is non-zero if we should autoload modules if needed. */
/**/
Conddef
getconddef(int inf, const char *name, int autol)
{
Conddef p;
int f = 1;
char *lookup, *s;
/* detokenize the Dash to the form encoded in lookup tables */
lookup = dupstring(name);
if (!lookup)
return NULL;
for (s = lookup; *s != '\0'; s++) {
if (*s == Dash)
*s = '-';
}
do {
for (p = condtab; p; p = p->next) {
if ((!!inf == !!(p->flags & CONDF_INFIX)) &&
!strcmp(lookup, p->name))
break;
}
if (autol && p && p->module) {
/*
* This is a definition for an autoloaded condition; load the
* module if we haven't tried that already.
*/
if (f) {
(void)ensurefeature(p->module,
(p->flags & CONDF_INFIX) ? "C:" : "c:",
(p->flags & CONDF_AUTOALL) ? NULL : lookup);
f = 0;
p = NULL;
} else {
deleteconddef(p);
return NULL;
}
} else
break;
} while (!p);
return p;
}
/*
* This adds the given condition definition. The return value is zero on *
* success and 1 on failure. If there is a matching definition for an *
* autoloaded condition, it is removed.
*
* This is used for adding both an autoload definition or
* a real condition. In the latter case the caller is responsible
* for setting the CONDF_ADDED flag.
*/
/**/
static int
addconddef(Conddef c)
{
Conddef p = getconddef((c->flags & CONDF_INFIX), c->name, 0);
if (p) {
if (!p->module || (p->flags & CONDF_ADDED))
return 1;
/* There is an autoload definition. */
deleteconddef(p);
}
c->next = condtab;
condtab = c;
return 0;
}
/* This removes the given condition definition from the list(s). If this *
* is a definition for a autoloaded condition, the memory is freed. */
/**/
int
deleteconddef(Conddef c)
{
Conddef p, q;
for (p = condtab, q = NULL; p && p != c; q = p, p = p->next);
if (p) {
if (q)
q->next = p->next;
else
condtab = p->next;
if (p->module) {
/* autoloaded, free it */
zsfree(p->name);
zsfree(p->module);
zfree(p, sizeof(*p));
}
return 0;
}
return -1;
}
/*
* Add or remove sets of conditions. The interface is
* identical to setbuiltins().
*/
/**/
static int
setconddefs(char const *nam, Conddef c, int size, int *e)
{
int ret = 0;
while (size--) {
if (e && *e++) {
if (c->flags & CONDF_ADDED) {
c++;
continue;
}
if (addconddef(c)) {
zwarnnam(nam, "name clash when adding condition `%s'",
c->name);
ret = 1;
} else {
c->flags |= CONDF_ADDED;
}
} else {
if (!(c->flags & CONDF_ADDED)) {
c++;
continue;
}
if (deleteconddef(c)) {
zwarnnam(nam, "condition `%s' already deleted", c->name);
ret = 1;
} else {
c->flags &= ~CONDF_ADDED;
}
}
c++;
}
return ret;
}
/* This adds a definition for autoloading a module for a condition. */
/**/
static int
add_autocond(const char *module, const char *cnam, int flags)
{
Conddef c;
c = (Conddef) zalloc(sizeof(*c));
c->name = ztrdup(cnam);
c->flags = ((flags & FEAT_INFIX) ? CONDF_INFIX : 0);
if (flags & FEAT_AUTOALL)
c->flags |= CONDF_AUTOALL;
c->module = ztrdup(module);
if (addconddef(c)) {
zsfree(c->name);
zsfree(c->module);
zfree(c, sizeof(*c));
if (!(flags & FEAT_IGNORE))
return 1;
}
return 0;
}
/* Remove a condition added with add_autocond */
/**/
static int
del_autocond(UNUSED(const char *modnam), const char *cnam, int flags)
{
Conddef cd = getconddef((flags & FEAT_INFIX) ? 1 : 0, cnam, 0);
if (!cd) {
if (!(flags & FEAT_IGNORE)) {
return 2;
}
} else if (cd->flags & CONDF_ADDED) {
if (!(flags & FEAT_IGNORE))
return 3;
} else
deleteconddef(cd);
return 0;
}
/************************************************************************
* Hook functions.
************************************************************************/
/* This list of hook functions defined. */
/**/
Hookdef hooktab;
/* Find a hook definition given the name. */
/**/
Hookdef
gethookdef(char *n)
{
Hookdef p;
for (p = hooktab; p; p = p->next)
if (!strcmp(n, p->name))
return p;
return NULL;
}
/* This adds the given hook definition. The return value is zero on *
* success and 1 on failure. */
/**/
int
addhookdef(Hookdef h)
{
if (gethookdef(h->name))
return 1;
h->next = hooktab;
hooktab = h;
h->funcs = znewlinklist();
return 0;
}
/*
* This adds multiple hook definitions. This is like addbuiltins().
* This allows a NULL module because we call it from init.c.
*/
/**/
mod_export int
addhookdefs(Module m, Hookdef h, int size)
{
int ret = 0;
while (size--) {
if (addhookdef(h)) {
zwarnnam(m ? m->node.nam : NULL,
"name clash when adding hook `%s'", h->name);
ret = 1;
}
h++;
}
return ret;
}
/* Delete hook definitions. */
/**/
int
deletehookdef(Hookdef h)
{
Hookdef p, q;
for (p = hooktab, q = NULL; p && p != h; q = p, p = p->next);
if (!p)
return 1;
if (q)
q->next = p->next;
else
hooktab = p->next;
freelinklist(p->funcs, NULL);
return 0;
}
/* Remove multiple hook definitions. */
/**/
mod_export int
deletehookdefs(UNUSED(Module m), Hookdef h, int size)
{
int ret = 0;
while (size--) {
if (deletehookdef(h))
ret = 1;
h++;
}
return ret;
}
/* Add a function to a hook. */
/**/
int
addhookdeffunc(Hookdef h, Hookfn f)
{
zaddlinknode(h->funcs, (void *) f);
return 0;
}
/**/
mod_export int
addhookfunc(char *n, Hookfn f)
{
Hookdef h = gethookdef(n);
if (h)
return addhookdeffunc(h, f);
return 1;
}
/* Delete a function from a hook. */
/**/
int
deletehookdeffunc(Hookdef h, Hookfn f)
{
LinkNode p;
for (p = firstnode(h->funcs); p; incnode(p))
if (f == (Hookfn) getdata(p)) {
remnode(h->funcs, p);
return 0;
}
return 1;
}
/* Delete a hook. */
/**/
mod_export int
deletehookfunc(char *n, Hookfn f)
{
Hookdef h = gethookdef(n);
if (h)
return deletehookdeffunc(h, f);
return 1;
}
/* Run the function(s) for a hook. */
/**/
mod_export int
runhookdef(Hookdef h, void *d)
{
if (empty(h->funcs)) {
if (h->def)
return h->def(h, d);
return 0;
} else if (h->flags & HOOKF_ALL) {
LinkNode p;
int r;
for (p = firstnode(h->funcs); p; incnode(p))