Expand description
module submodule.
Module system for zshrs
Port from zsh/Src/module.c (3,646 lines)
Hash of modules // c:46 The list of hook functions defined. // c:840 List of math functions. // c:1255
In C, module.c provides dynamic loading of .so modules at runtime via dlopen/dlsym. In Rust, all modules are statically compiled into the binary — there’s no dynamic loading. This module provides the registration, lookup, and management API that the rest of the shell uses to interact with module features (builtins, conditions, parameters, hooks, and math functions).
Re-exports§
pub use crate::ported::zsh_h::BINF_ADDED;pub use crate::ported::zsh_h::CONDF_ADDED;pub use crate::ported::zsh_h::CONDF_INFIX;pub use crate::ported::zsh_h::MFF_ADDED;
Structs§
- modulestab
- Module table (from module.c module hash table)
Table of registered modules.
Port of the
modulestabHashTable Src/module.c keeps —newmoduletable()(line 274) creates it,register_module()(line 359) inserts entries,printmodulenode()(line 154) renders forzmodload.
Constants§
- FEATURE_
TYPE_ BUILTIN - Feature-type index passed to
features_()(Src/module.c:313+). C ships bare ints; Rust adds names for readability. - FEATURE_
TYPE_ CONDITION FEATURE_TYPE_CONDITIONconstant.- FEATURE_
TYPE_ HOOK FEATURE_TYPE_HOOKconstant.- FEATURE_
TYPE_ MATHFUNC FEATURE_TYPE_MATHFUNCconstant.- FEATURE_
TYPE_ PARAMETER FEATURE_TYPE_PARAMETERconstant.- FEAT_
AUTOALL FEAT_AUTOALL—zmodload -aenable-all-features. Port ofenum { FEAT_AUTOALL = 0x0004 }fromSrc/module.c:69.- FEAT_
CHECKAUTO FEAT_CHECKAUTO— verify autoloads are actually provided. Port ofenum { FEAT_CHECKAUTO = 0x0010 }fromSrc/module.c:81.- FEAT_
IGNORE FEAT_IGNORE— bit in theflagsarg to add_/del_-automathfunc and friends. Port ofenum { FEAT_IGNORE = 0x0001 }fromSrc/module.c:62. /*-ioption: ignore redefinition errors. */- FEAT_
INFIX FEAT_INFIX— bit indicating a condition is infix-style. Port ofenum { FEAT_INFIX = 0x0002 }fromSrc/module.c:64.- FEAT_
PATTERN_ ARGS - !!! WARNING: RUST-ONLY FLAG BIT !!!
C has no such bit. It carries the
zmodload -mdecision on thePatprog patfield of eachstruct feature_enablesentry (Src/module.c:3253-3262fillsfep->pat = patprogs ? *patprogs++ : NULL, anddo_module_featuresat c:2093 branchesfep->pat ? pattry(fep->pat, *fp) : !strcmp(*fp, esp)). zshrs’s feature list is a plain&[String]with no per-entry payload, so there is nowhere to hang the compiled pattern.-msets the pat for EVERY entry of one invocation uniformly, so the decision is carried as this one flag bit alongsideFEAT_IGNORE/FEAT_CHECKAUTOand the pattern is compiled at the match site. Deliberately outside C’s 0x0001-0x0010 range so it cannot collide with a future upstream bit. - FEAT_
REMOVE FEAT_REMOVE— bit indicating feature removal pass. Port ofenum { FEAT_REMOVE = 0x0008 }fromSrc/module.c:76.- FINDMOD_
ALIASP FINDMOD_ALIASP— bit infind_module()’sflagsarg. Port ofenum { FINDMOD_ALIASP = 0x0001 }fromSrc/module.c:110. /* Resolve any aliases to the underlying module. */- FINDMOD_
CREATE FINDMOD_CREATE— bit infind_module()’sflagsarg. Port ofenum { FINDMOD_CREATE = 0x0002 }fromSrc/module.c:115. /* Create an element for the module in the list if not found. */- PRINTMOD_
ALIAS PRINTMOD_ALIASfromSrc/module.c:140. Resolve aliases when emitting underPRINTMOD_EXIST.- PRINTMOD_
AUTO PRINTMOD_AUTOfromSrc/module.c:148. Emit autoloads (zmodload -a).- PRINTMOD_
DEPS PRINTMOD_DEPSfromSrc/module.c:142. Emit the dependency list (zmodload -d).- PRINTMOD_
EXIST PRINTMOD_EXISTfromSrc/module.c:138. Print only when the module exists.- PRINTMOD_
FEATURES PRINTMOD_FEATURESfromSrc/module.c:144. Emit feature flags (zmodload -F).- PRINTMOD_
LIST PRINTMOD_LISTfromSrc/module.c:136. Long-form (zmodload -L) output.- PRINTMOD_
LISTALL PRINTMOD_LISTALLfromSrc/module.c:146. Include disabled features (zmodload -lL).- WRAPPER_
BIT_ KSH93 WRAPPERS_ADDEDbit forzsh/ksh93’sWRAPDEF(ksh93_wrapper)(Src/Modules/ksh93.c:230-232).- WRAPPER_
BIT_ ZPROF WRAPPERS_ADDEDbit forzsh/zprof’sWRAPDEF(zprof_wrapper)(Src/Modules/zprof.c:318-320).
Statics§
- CONDTAB
- Port of file-static
Conddef condtab;fromSrc/cond.c:21— the global condition-definition linked-list head consulted by[[ ... ]]dispatch. Modules register custom conditions viaaddconddef; the runtime walkscondtablooking for the matching name+infix flag at each[[evaluation. Rust port stores entries in aVec(linear add/remove + walk; same observable behaviour as C linked list). - DISABLED_
MODULE_ BUILTINS - !!! RUST-ONLY REGISTRY — NO C COUNTERPART !!!
- MATHFUNCS
- Port of
MathFunc mathfuncs;fromSrc/module.c:1258— the global head of the linked list of math functions. Both autoloadable math ported (added by modules) and user math ported (added byfunctions -M) live here. - MODULESTAB
- Port of
mod_export ModuleTable modulestabfromSrc/Modules/zmodload.c:32. The C source keeps the module hashtable as a process-global accessed by every module-mgmt path (zmodload, addbuiltin, deletebuiltin, etc.). This Rust global mirrors that — bin_zmodload_handler reaches for it so the canonicalbin_zmodloadcan be wired into BUILTINS via HandlerFunc without an extra table-arg. - WRAPPERS
- Port of file-static
FuncWrap wrappers;fromSrc/module.c:567— the global wrapper-function linked-list head. Modules register wrapper callbacks viaaddwrapper(FuncWrap)and the runtime fires them aroundrunshfunc(). The Rust port stores entries in aVec(linear add/remove + iterate; same observable behaviour). - WRAPPERS_
ADDED - Relaxed-atomic membership mirror of
WRAPPERS, one bit per wrapper-installing module (seeWRAPPER_BIT_ZPROF). Written byaddwrapper/deletewrapper, read byexec::runshfunc. See the WARNING block above. - hooktab
- Port of
Hookdef hooktab;fromSrc/module.c:843— the file-static linked-list head pointer to the chain of registeredhookdefnodes. Walked bygethookdef; mutated byaddhookdef/deletehookdef. Each node is aBox::leak’d hookdef (so the raw pointer has program-lifetime, matching C’s static-storagezshhooks[]and module-side hookdef arrays).
Traits§
- Module
Lifecycle - Module lifecycle callbacks (from module.c setup_/getrandom_buffer/cleanup_/finish_)
Lifecycle hooks every module must implement.
Port of the
setup_/features_/enables_/getrandom_buffer/cleanup_/finish_entry points every C module exposes (Src/module.c lines 306-345 illustrate the canonical no-op set). Rust modules implement this trait directly.
Functions§
- add_
automathfunc - Port of
add_automathfunc()fromSrc/module.c:1410. — C decladd_automathfunc(const char *module, const char *fnam, int flags). - add_
autoparam - Port of
add_autoparam()fromSrc/module.c:1198. — C decladd_autoparam(const char *module, const char *pnam, int flags). - add_dep
- Port of
add_dep()fromSrc/module.c:2369. — C decladd_dep(const char *name, char *from). - addbuiltin
- Port of
addbuiltin()fromSrc/module.c:409. — C decladdbuiltin(Builtin b). C body: look upb->node.namin builtintab; if BINF_ADDED clash → return 1; otherwise replace any pre-existing entry and addb. Returns 0 on add, 1 on clash. - addbuiltins
- Port of
addbuiltins()fromSrc/module.c:544. — C decladdbuiltins(char const *nam, Builtin binl, int size). Walks the slice; for each entry not already flagged BINF_ADDED, callsaddbuiltin. Returns 0 if all succeeded, 1 if any clashed. zwarnnam emitted on each clash matches C. - addconddef
- Port of
addconddef()fromSrc/module.c:703. — C decladdconddef(Conddef c). Walks CONDTAB for a clash on (name, infix-flag); replaces autoloadable entries via deleteconddef; otherwise prepends. Returns 0 on add, 1 on clash (existing entry already added). - addhookdef
- Port of
addhookdef()fromSrc/module.c:864. — C decladdhookdef(Hookdef h). - addhookdeffunc
- Port of
addhookdeffunc()fromSrc/module.c:939. — C decladdhookdeffunc(Hookdef h, Hookfn f). - addhookdefs
- Port of
addhookdefs()fromSrc/module.c:883. — C decladdhookdefs(Module m, Hookdef h, int size). - addhookfunc
- Port of
addhookfunc()fromSrc/module.c:948. — C decladdhookfunc(char *n, Hookfn f). - addmathfunc
- Port of
addmathfunc()fromSrc/module.c:1313. — C decladdmathfunc(MathFunc f). Returns 0 on add, 1 on clash (existing entry not autoloadable). Replaces autoloadable entries viaremovemathfunc. - addparamdef
- Port of
addparamdef()fromSrc/module.c:1061. — C decladdparamdef(Paramdef d). Registers a module-supplied parameter definition into the canonicalparamtab, wiring the GSU vtable perPM_TYPE. Returns 0 on success, 1 on error. - addwrapper
- Port of
addwrapper()fromSrc/module.c:577. — C decladdwrapper(Module m, FuncWrap w). Tail-appends a module’s function wrapper onto the globalWRAPPERSlist. Returns 1 on error, 0 on success. - autofeatures
- Port of
autofeatures()fromSrc/module.c:3437. — C declautofeatures(const char *cmdnam, const char *module, char **features, int prefchar, int defflags). - autoloadscan
- Port of
autoloadscan()fromSrc/module.c:2403. — C declautoloadscan(HashNode hn, int printflags). - bin_
zmodload - Port of
bin_zmodload()fromSrc/module.c:2440. — C declbin_zmodload(char *nam, char **args, Options ops, UNUSED(int func)). Top-level dispatcher for thezmodloadbuiltin. Validates flag combinations then routes to one of the per-mode helpers: -F → bin_zmodload_features (c:3003) -e → bin_zmodload_exist (c:2623) -d → bin_zmodload_dep (c:2649) -a/-b/-c/-p/-f → bin_zmodload_auto (c:2726) default → bin_zmodload_load (c:2971) -A/-R → bin_zmodload_alias (c:2515) WARNING: param names don’t match C — Rust=(nam, args, _func) vs C=(nam, args, ops, func) - bin_
zmodload_ alias - Port of
bin_zmodload_alias()fromSrc/module.c:2515. — C declbin_zmodload_alias(char *nam, char **args, Options ops). - bin_
zmodload_ auto - Port of
bin_zmodload_auto()fromSrc/module.c:2726. — C declbin_zmodload_auto(char *nam, char **args, Options ops). - bin_
zmodload_ dep - Port of
bin_zmodload_dep()fromSrc/module.c:2649. — C declbin_zmodload_dep(UNUSED(char *nam), char **args, Options ops). - bin_
zmodload_ exist - Port of
bin_zmodload_exist()fromSrc/module.c:2623. — C declbin_zmodload_exist(UNUSED(char *nam), char **args, Options ops). - bin_
zmodload_ features - Port of
bin_zmodload_features()fromSrc/module.c:3003. — C declbin_zmodload_features(const char *nam, char **args, Options ops). - bin_
zmodload_ load - Port of
bin_zmodload_load()fromSrc/module.c:2971. — C declbin_zmodload_load(char *nam, char **args, Options ops). - boot_
- Port of
boot_()fromSrc/module.c:331. — C declboot_(UNUSED(Module m)). - boot_
module - Port of
boot_module()fromSrc/module.c:1910. — C declboot_module(Module m). - checkaddparam
- Port of
checkaddparam()fromSrc/module.c:1026. — C declcheckaddparam(const char *nam, int opt_i). - cleanup_
- Port of
cleanup_()fromSrc/module.c:338. — C declcleanup_(UNUSED(Module m)). - cleanup_
module - Port of
cleanup_module()fromSrc/module.c:1918. — C declcleanup_module(Module m). - del_
automathfunc - Port of
del_automathfunc()fromSrc/module.c:1436. — C decldel_automathfunc(UNUSED(const char *modnam), const char *fnam, int flags). - del_
autoparam - Port of
del_autoparam()fromSrc/module.c:1235. — C decldel_autoparam(UNUSED(const char *modnam), const char *pnam, int flags). - delete_
module - Port of
delete_module()fromSrc/module.c:1687. — C decldelete_module(Module m). - deletebuiltin
- Port of
deletebuiltin()fromSrc/module.c:449. — C decldeletebuiltin(const char *nam). - deleteconddef
- Port of
deleteconddef()fromSrc/module.c:724. — C decldeleteconddef(Conddef c). Removes condition definitioncfromcondtab. Returns 0 on success, -1 on miss. C also frees the autoloaded entry’s name + module; Rust drop subsumes that. - deletehookdef
- Port of
deletehookdef()fromSrc/module.c:902. — C decldeletehookdef(Hookdef h). - deletehookdeffunc
- Port of
deletehookdeffunc()fromSrc/module.c:961. — C decldeletehookdeffunc(Hookdef h, Hookfn f). - deletehookdefs
- Port of
deletehookdefs()fromSrc/module.c:923. — C decldeletehookdefs(UNUSED(Module m), Hookdef h, int size).mis unused perUNUSED(Module m)in C. - deletehookfunc
- Port of
deletehookfunc()fromSrc/module.c:977. — C decldeletehookfunc(const char *n, Hookfn f). - deletemathfunc
- Port of
deletemathfunc()fromSrc/module.c:1342. — C decldeletemathfunc(MathFunc f). Removes f from MATHFUNCS; for unloaded/user-defined entries clears the MFF_ADDED flag instead of dropping the node (C:f->flags &= ~MFF_ADDEDwhen f->module is null). - deleteparamdef
- Port of
deleteparamdef()fromSrc/module.c:1124. — C decldeleteparamdef(Paramdef d). Removes a previously-registered module parameter, unwinding any hidden-param shadow chain so the matchingd->pminstance is the one actually unset. - deletewrapper
- Port of
deletewrapper()fromSrc/module.c:609. — C decldeletewrapper(Module m, FuncWrap w). Unlinks a module’s wrapper fromWRAPPERS. Returns 0 when the node was found and removed, 1 otherwise. - do_
boot_ module - Port of
do_boot_module()fromSrc/module.c:2139. — C decldo_boot_module(Module m, Feature_enables enablesarr, int silent). - do_
cleanup_ module - Port of
do_cleanup_module()fromSrc/module.c:2159. — C decldo_cleanup_module(Module m). - do_
load_ module - Port of
do_load_module()fromSrc/module.c:1610. — C decldo_load_module(char const *name, int silent). - do_
module_ features - Port of
do_module_features()fromSrc/module.c:1998. — C decldo_module_features(Module m, Feature_enables enablesarr, int flags). - dyn_
boot_ module - Port of
dyn_boot_module()fromSrc/module.c:1747. — C decldyn_boot_module(Module m). - dyn_
cleanup_ module - Port of
dyn_cleanup_module()fromSrc/module.c:1754. — C decldyn_cleanup_module(Module m). - dyn_
enables_ module - Port of
dyn_enables_module()fromSrc/module.c:1740. — C decldyn_enables_module(Module m, int **enables). - dyn_
features_ module - Port of
dyn_features_module()fromSrc/module.c:1733. — C decldyn_features_module(Module m, char ***features). - dyn_
finish_ module - Port of
dyn_finish_module()fromSrc/module.c:1761. — C decldyn_finish_module(Module m). C body:return ((int (*)(int,Module,void*)) m->u.handle)(3, m, NULL);— invokes the DSO entry point with opcode 3 (finish). no-op in Rust: zshrs has no dlopen path, all modules are statically linked,m->u.handleis always null, and finish-time resource release happens at process exit. Static-link path defers all DSO entry calls; the no-op return preserves caller semantics (0 = success). - dyn_
setup_ module - Port of
dyn_setup_module()fromSrc/module.c:1726. — C decldyn_setup_module(Module m). - enables_
- Port of
enables_()fromSrc/module.c:324. — C declenables_(UNUSED(Module m), UNUSED(int **enables)). - enables_
module - Port of
enables_module()fromSrc/module.c:1901. — C declenables_module(Module m, int **enables). - ensurefeature
- Port of
ensurefeature()fromSrc/module.c:3415. — C declensurefeature(const char *modname, const char *prefix, const char *feature). - features_
- Port of
features_()fromSrc/module.c:313. — C declfeatures_(UNUSED(Module m), UNUSED(char ***features)). - features_
module - Port of
features_module()fromSrc/module.c:1892. — C declfeatures_module(Module m, char ***features). - featuresarray
- Port of
featuresarray()fromSrc/module.c:3279. — C declfeaturesarray(UNUSED(Module m), Features f). Construct the feature-name array for a module: builtins getb:NAME, conditionsc:NAMEorC:NAMEifCONDF_INFIX, math funcsf:NAME, paramsp:NAME. Trailing abstract slots (n_abstract) are pre-allocated but left empty so the module’s own setup can fill them in. C uses zhalloc heap allocation — Box goes out of scope here as Rust’sVec<String>owns the entries (Drop happens automatically). Per-module Rust files insrc/ported/modules/*.rsandsrc/ported/builtins/*.rseach carry afeaturesarrayshim that delegates to this canonical free fn once the modules table is wired through. WARNING: param names don’t match C — Rust=(_m, bn, cd, mf, pd, n_abstract) vs C=(m, f) - find_
module - Port of
find_module()fromSrc/module.c:1659. — C declfind_module(const char *name, int flags, const char **namep). - finish_
- Port of
finish_()fromSrc/module.c:345. — C declfinish_(UNUSED(Module m)). - finish_
module - Port of
finish_module()fromSrc/module.c:1926. — C declfinish_module(Module m). - freemodulenode
- Free module node (from module.c freemodulenode)
Free a module table entry.
Port of
freemodulenode()fromSrc/module.c:119. — C declfreemodulenode(HashNode hn). Rust’sDrophandles the per-field free; this exists for API parity with C callers. - getconddef
- Port of
getconddef()fromSrc/module.c:648. — C declgetconddef(int inf, const char *name, int autol). - getfeatureenables
- Port of
getfeatureenables()fromSrc/module.c:3314. — C declgetfeatureenables(UNUSED(Module m), Features f). Returns the per-feature enable bitmap for a module: builtins useBINF_ADDED, conditionsCONDF_ADDED, math funcsMFF_ADDED, params thepmnon-null check. Trailing abstract slots are left at 0 (filled by the module’s own enables_). C uses zhalloc heap allocation; Rust’sVec<i32>owns the entries (Drop happens automatically). Per- module shims insrc/ported/modules/*.rsdelegate to this canonical free fn once the modules table is wired through. WARNING: param names don’t match C — Rust=(_m, bn, cd, mf, pd, n_abstract) vs C=(m, f) - gethookdef
- Port of
gethookdef()fromSrc/module.c:849. — C declgethookdef(const char *n). - getmathfunc
- Port of
getmathfunc()fromSrc/module.c:1283. — C declgetmathfunc(const char *name, int autol). - handlefeatures
- Port of
handlefeatures()fromSrc/module.c:3392. — C declhandlefeatures(Module m, Features f, int **enables). - hpux_
dlsym - Port of
hpux_dlsym()fromSrc/module.c:1530. — C declhpux_dlsym(void *handle, char *name). - load_
and_ bind - Port of
load_and_bind()fromSrc/module.c:1468. — C declload_and_bind(const char *fn). - modname_
ok - Port of
modname_ok()fromSrc/module.c:2173. — C declmodname_ok(char const *p). - module_
func - Port of
module_func()fromSrc/module.c:1770. — C declmodule_func(Module m, const char *name). - module_
loaded - Port of
module_loaded()fromSrc/module.c:1703. — C declmodule_loaded(const char *name). - newmoduletable
- Create new module table (from module.c newmoduletable)
Create an empty module table.
Port of
newmoduletable()fromSrc/module.c:274. — C declnewmoduletable(int size, char const *name). the C source allocates themodulestabhash withcreatehashtable. WARNING: param names don’t match C — Rust=() vs C=(size, name) - printautoparams
- Port of
printautoparams()fromSrc/module.c:2710. — C declprintautoparams(HashNode hn, int lon). - printmodulenode
- Port of
printmodulenode()fromSrc/module.c:154. — C declprintmodulenode(HashNode hn, int flags). - register_
module - Register module (from module.c register_module)
Register a module by name.
Port of
register_module()fromSrc/module.c:359. — C declregister_module(const 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). wraps a slot in the globalmodulestaband seeds its lifecycle callbacks. WARNING: param names don’t match C — Rust=(table, name) vs C=(n, setup, features, enables, boot, cleanup, finish) - removemathfunc
- Port of
removemathfunc()fromSrc/module.c:1267. — C declremovemathfunc(MathFunc previous, MathFunc current). Removes the named entry from MATHFUNCS and drops it (Rust drop subsumes C’s zsfree/zfree ladder). WARNING: param names don’t match C — Rust=(name) vs C=(previous, current) - require_
module - Port of
require_module()fromSrc/module.c:2344. — C declrequire_module(const char *module, Feature_enables features, int silent). - runhookdef
- Port of
runhookdef()fromSrc/module.c:990. — C declrunhookdef(Hookdef h, void *d). - setconddefs
- Port of
setconddefs()fromSrc/module.c:754. — C declsetconddefs(char const *nam, Conddef c, int size, int *e). Bulk add/delete of condition definitions: the parallele[]array selects per-entry add (e[i] != 0) vs delete (e[i] == 0). Returns 1 if any individual op clashed, 0 if all clean. - setfeatureenables
- Port of
setfeatureenables()fromSrc/module.c:3354. — C declsetfeatureenables(Module m, Features f, int *e). - setmathfuncs
- Port of
setmathfuncs()fromSrc/module.c:1374. — C declsetmathfuncs(char const *nam, MathFunc f, int size, int *e). Bulk add/delete of math-function definitions via the parallele[]selector array (same shape as setconddefs). - setup_
- Port of
setup_()fromSrc/module.c:306. — C declsetup_(UNUSED(Module m)). - setup_
module - Port of
setup_module()fromSrc/module.c:1884. — C declsetup_module(Module m). - try_
load_ module - Port of
try_load_module()fromSrc/module.c:1583. — C decltry_load_module(char const *name). - unload_
named_ module - Port of
unload_named_module()fromSrc/module.c:2924. — C declunload_named_module(char *modname, char *nam, int silent).