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_
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).
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). - 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). - 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(const char *module, const char *fnam, int flags)fromSrc/module.c:1410. - add_
autoparam - Port of
static int add_autoparam(const char *module, const char *pnam, int flags)fromSrc/module.c:1197. - add_dep
- Port of
add_dep(const char *name, char *from)fromSrc/module.c:2369. - addbuiltin
- Port of
addbuiltins(char const *nam, Builtin binl, int size)fromSrc/module.c:544. - addbuiltins
- Port of
addbuiltins(char const *nam, Builtin binl, int size)fromSrc/module.c:544. 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
int addconddef(Conddef c)fromSrc/module.c:703. 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
int addhookdef(Hookdef h)fromSrc/module.c:864. - addhookdeffunc
- Port of
int addhookdeffunc(Hookdef h, Hookfn f)fromSrc/module.c:939. - addhookdefs
- Port of
int addhookdefs(Module m, Hookdef h, int size)fromSrc/module.c:883. - addhookfunc
- Port of
int addhookfunc(char *n, Hookfn f)fromSrc/module.c:948. - addmathfunc
- Port of
addmathfunc(MathFunc f)fromSrc/module.c:1313. Returns 0 on add, 1 on clash (existing entry not autoloadable). Replaces autoloadable entries viaremovemathfunc. - addparamdef
- Port of
int addparamdef(Paramdef d)fromSrc/module.c:1061. 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(Module m, FuncWrap w)fromSrc/module.c:577. Returns 0 on add, 1 on clash. Walks WRAPPERS for an existing entry with the same handler; appends if absent and sets WRAPF_ADDED on the input record. - autofeatures
- Port of
addmathfunc(MathFunc f)fromSrc/module.c:1313. - autoloadscan
- Port of
autoloadscan(HashNode hn, int printflags)fromSrc/module.c:2403. - bin_
zmodload - Direct port of
bin_zmodload(char *nam, char **args, Options ops, UNUSED(int func))fromSrc/module.c:2440. 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(char *nam, char **args, Options ops)fromSrc/module.c:2515. - bin_
zmodload_ auto - Port of
bin_zmodload_auto(char *nam, char **args, Options ops)fromSrc/module.c:2726. - bin_
zmodload_ dep - Port of
bin_zmodload_dep(UNUSED(char *nam), char **args, Options ops)fromSrc/module.c:2649. - bin_
zmodload_ exist - Port of
bin_zmodload_exist(UNUSED(char *nam), char **args, Options ops)fromSrc/module.c:2623. - bin_
zmodload_ features - Port of
bin_zmodload_features(const char *nam, char **args, Options ops)fromSrc/module.c:3003. - bin_
zmodload_ load - Port of
bin_zmodload_load(char *nam, char **args, Options ops)fromSrc/module.c:2971. - boot_
- Port of
boot_(UNUSED(Module m))fromSrc/module.c:331. - boot_
module - Port of
boot_module(Module m)fromSrc/module.c:1910. - checkaddparam
- Port of
checkaddparam(const char *nam, int opt_i)fromSrc/module.c:1026. - cleanup_
- Port of
cleanup_(UNUSED(Module m))fromSrc/module.c:338. - cleanup_
module - Port of
cleanup_module(Module m)fromSrc/module.c:1918. - del_
automathfunc - Port of
del_automathfunc(UNUSED(const char *modnam), const char *fnam, int flags)fromSrc/module.c:1436. - del_
autoparam - Port of
static int del_autoparam(const char *modnam, const char *pnam, int flags)fromSrc/module.c:1234. - delete_
module - Port of
delete_module(Module m)fromSrc/module.c:1687. - deletebuiltin
- Port of
int deletebuiltin(const char *nam)fromSrc/module.c:449. - deleteconddef
- Port of
int deleteconddef(Conddef c)fromSrc/module.c:724. 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
int deletehookdef(Hookdef h)fromSrc/module.c:902. - deletehookdeffunc
- Port of
int deletehookdeffunc(Hookdef h, Hookfn f)fromSrc/module.c:961. - deletehookdefs
- Port of
int deletehookdefs(Module m, Hookdef h, int size)fromSrc/module.c:923.mis unused perUNUSED(Module m)in C. - deletehookfunc
- Port of
int deletehookfunc(const char *n, Hookfn f)fromSrc/module.c:977. - deletemathfunc
- Port of
deletemathfunc(MathFunc f)fromSrc/module.c:1342. 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
int deleteparamdef(Paramdef d)fromSrc/module.c:1128. 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(Module m, FuncWrap w)fromSrc/module.c:609. Removes entry with the same handler from WRAPPERS. Returns 0 on success, 1 on miss / alias / never-added. - do_
boot_ module - Port of
deletemathfunc(MathFunc f)fromSrc/module.c:1342. - do_
cleanup_ module - Port of
do_cleanup_module(Module m)fromSrc/module.c:2159. - do_
load_ module - Port of
do_load_module(char const *name, int silent)fromSrc/module.c:1610. - do_
module_ features - Port of
do_module_features(Module m, Feature_enables enablesarr, int flags)fromSrc/module.c:1998. - dyn_
boot_ module - Port of
dyn_boot_module(Module m)fromSrc/module.c:1747. - dyn_
cleanup_ module - Port of
dyn_cleanup_module(Module m)fromSrc/module.c:1754. - dyn_
enables_ module - Port of
dyn_enables_module(Module m, int **enables)fromSrc/module.c:1740. - dyn_
features_ module - Port of
dyn_features_module(Module m, char ***features)fromSrc/module.c:1733. - dyn_
finish_ module - Port of
static int dyn_finish_module(Module m)fromSrc/module.c:1766. 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(Module m)fromSrc/module.c:1726. - enables_
- Port of
enables_(UNUSED(Module m), UNUSED(int **enables))fromSrc/module.c:324. - enables_
module - Port of
enables_module(Module m, int **enables)fromSrc/module.c:1901. - ensurefeature
- Port of
ensurefeature(const char *modname, const char *prefix, const char *feature)fromSrc/module.c:3415. - features_
- Port of
features_(UNUSED(Module m), UNUSED(char ***features))fromSrc/module.c:313. - features_
module - Port of
features_module(Module m, char ***features)fromSrc/module.c:1892. - featuresarray
- Port of
mod_export char **featuresarray(UNUSED(Module m), Features f)fromSrc/module.c:3284. 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(const char *name, int flags, const char **namep)fromSrc/module.c:1659. - finish_
- Port of
finish_(UNUSED(Module m))fromSrc/module.c:345. - finish_
module - Port of
finish_module(Module m)fromSrc/module.c:1926. - freemodulenode
- Free module node (from module.c freemodulenode)
Free a module table entry.
Port of
freemodulenode(HashNode hn)from Src/module.c:119 — Rust’sDrophandles the per-field free; this exists for API parity with C callers. - getconddef
- Port of
Conddef getconddef(int inf, const char *name, int autol)fromSrc/module.c:648. - getfeatureenables
- Port of
mod_export int *getfeatureenables(UNUSED(Module m), Features f)fromSrc/module.c:3319. 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
Hookdef gethookdef(const char *n)fromSrc/module.c:849. - getmathfunc
- Port of
getmathfunc(const char *name, int autol)fromSrc/module.c:1283. - hpux_
dlsym - Port of
hpux_dlsym(void *handle, char *name)fromSrc/module.c:1530. - load_
and_ bind - Port of
load_and_bind(const char *fn)fromSrc/module.c:1468. - modname_
ok - Port of
modname_ok(char const *p)fromSrc/module.c:2173. - module_
func - Port of
module_func(Module m, const char *name)fromSrc/module.c:1770. - module_
loaded - Port of
module_loaded(const char *name)fromSrc/module.c:1703. - newmoduletable
- Create new module table (from module.c newmoduletable)
Create an empty module table.
Port of
newmoduletable(int size, char const *name)from Src/module.c:274 — the C source allocates themodulestabhash withcreatehashtable. WARNING: param names don’t match C — Rust=() vs C=(size, name) - printautoparams
- Port of
printautoparams(HashNode hn, int lon)fromSrc/module.c:2710. - printmodulenode
- Direct port of
void printmodulenode(HashNode hn, int flags)fromSrc/module.c:154. - register_
module - Register module (from module.c register_module)
Register a module by name.
Port of
register_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)from Src/module.c:359 — 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(MathFunc previous, MathFunc current)fromSrc/module.c:1267. 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
removemathfunc(MathFunc previous, MathFunc current)fromSrc/module.c:1267. - resolvebuiltin
- Port of
static HashNode resolvebuiltin(const char *cmdarg, HashNode hn)fromSrc/exec.c:2700-2724— the autoloaded-builtin stub firing. - runhookdef
- Port of
int runhookdef(Hookdef h, void *d)fromSrc/module.c:990. - setconddefs
- Port of
int setconddefs(char const *nam, Conddef c, int size, int *e)fromSrc/module.c:754. 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. - setmathfuncs
- Port of
int setmathfuncs(char const *nam, MathFunc f, int size, int *e)fromSrc/module.c:1374. Bulk add/delete of math-function definitions via the parallele[]selector array (same shape as setconddefs). - setup_
- Port of
setup_(UNUSED(Module m))fromSrc/module.c:306. - setup_
module - Port of
setup_module(Module m)fromSrc/module.c:1884. - try_
load_ module - Port of
try_load_module(char const *name)fromSrc/module.c:1583. - unload_
named_ module - Port of
unload_named_module(char *modname, char *nam, int silent)fromSrc/module.c:2923-2965.