zpmod  b19981f
High-performance Zsh module for script optimization and filesystem helpers
zpmod.pro File Reference

Prototype stub for zpmod when building out-of-tree. More...

#include "zpmod_utils.h"
#include "zpmod_source.h"
#include "zpmod_fpath.h"
#include "zpmod_source_hot.h"
#include "zpmod_fs.h"
Include dependency graph for zpmod.pro:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ZPMOD_PRO
 

Functions

int bin_custom_dot (char *name, char **argv, Options ops, int func)
 
int bin_readarray (char *nam, char **argv, Options ops, int func)
 readarray builtin entrypoint More...
 
void zpmod_usage (void)
 Print usage for the zpmod builtin. More...
 
int setup_ (Module m)
 
int features_ (Module m, char ***features)
 Provide feature list (builtins) to zsh. More...
 
int enables_ (Module m, int **enables)
 Enable/disable builtins as requested by the shell. More...
 
int boot_ (Module m)
 
int cleanup_ (Module m)
 Cleanup features when unloading. More...
 
int finish_ (Module m)
 
char * zp_build_source_report (int no_paths, int *rep_size)
 
char * zp_unmetafy_zalloc (const char *to_copy, int *new_len)
 Duplicate and unmetafy a zsh string with zalloc; see header for details. More...
 
char * my_ztrdup_glen (const char *s, unsigned *len_ret)
 zalloc-backed strdup with length out parameter. More...
 
void zp_freeparamnode (HashNode hn)
 
int cmd_dirlist (char *nam, char **argv)
 
int cmd_pathstat (char *nam, char **argv)
 
int cmd_readfile (char *nam, char **argv)
 
int zp_path_warmup_core (const char *nam, int quiet, int prune_missing, int dry_run)
 Implements path-warmup functionality for executable discovery and path pruning. More...
 
int cmd_source_study (char *nam, char **argv)
 
int cmd_fpath_index (char *nam, char **argv)
 Implements zpmod fpath-index with intelligent skip detection. More...
 
int cmd_source_hot (char *nam, char **argv)
 Implements zpmod source-hot. More...
 

Detailed Description

Prototype stub for zpmod when building out-of-tree.

Definition in file zpmod.pro.

Macro Definition Documentation

◆ ZPMOD_PRO

#define ZPMOD_PRO

Definition at line 6 of file zpmod.pro.

Function Documentation

◆ bin_custom_dot()

int bin_custom_dot ( char *  name,
char **  argv,
Options  ops,
int  func 
)

◆ bin_readarray()

int bin_readarray ( char *  nam,
char **  argv,
Options  ops,
int  func 
)

readarray builtin entrypoint

Definition at line 26 of file readarray.c.

References setaparam(), setsparam(), unsetparam(), zalloc(), zfree(), zrealloc(), zsfree(), ztrdup(), and zwarnnam().

Here is the call graph for this function:

◆ boot_()

int boot_ ( Module  m)

◆ cleanup_()

int cleanup_ ( Module  m)

Cleanup features when unloading.

Definition at line 56 of file module.c.

◆ cmd_dirlist()

int cmd_dirlist ( char *  nam,
char **  argv 
)

Definition at line 348 of file zpmod_builtin.c.

References zp_dirlist_core(), and zwarnnam().

Referenced by bin_zpmod().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cmd_fpath_index()

int cmd_fpath_index ( char *  nam,
char **  argv 
)

Implements zpmod fpath-index with intelligent skip detection.

Generates function-to-path mappings from FPATH directories with performance optimization through skip detection that avoids rebuilding unchanged indexes.

Index Format (v1)

The generated index uses a structured format for reliable skip detection:

# zpmod fpath-index v1
# dir 0 /first/fpath/dir 1234567890
# dir 1 /second/fpath/dir 1234567891
# dir 2 /third/fpath/dir -1
function_name /first/fpath/dir/function_name
other_func /second/fpath/dir/other_func

Skip Detection Algorithm

The skip detection was redesigned to handle missing directories consistently, fixing a critical issue where mtime mismatches caused unnecessary rebuilds.

Original Problem: When a directory didn't exist during index generation, we recorded -1 as the mtime. However, if the directory appeared later or stat() behaved differently during verification, we'd get a mismatch and force an unnecessary rebuild.

Solution: Unified mtime handling ensures consistent comparison:

  • During generation: stat() failure → record -1
  • During verification: stat() failure → use -1 for comparison
  • Both paths handle missing directories identically
Parameters
namBuiltin name for error reporting
argvCommand arguments (–out, –rebuild, –preload)
Returns
0 on success, 1 on failure
Note
The preload functionality populates shfunctab with function stubs for faster autoload resolution without writing index files.

Definition at line 57 of file fpath.c.

References addhashnode(), dupstring(), getaparam(), gethashnode2(), shfunctab, zalloc(), ztrdup(), and zwarnnam().

Referenced by bin_zpmod().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cmd_pathstat()

int cmd_pathstat ( char *  nam,
char **  argv 
)

Definition at line 381 of file zpmod_builtin.c.

References zp_icon(), zp_pathstat_core(), zp_take_opt_with_arg(), and zwarnnam().

Referenced by bin_zpmod().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cmd_readfile()

int cmd_readfile ( char *  nam,
char **  argv 
)

Definition at line 418 of file zpmod_builtin.c.

References zp_readfile_core(), and zwarnnam().

Referenced by bin_zpmod().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cmd_source_hot()

int cmd_source_hot ( char *  nam,
char **  argv 
)

Implements zpmod source-hot.

Parameters
namThe builtin name.
argvSubcommand arguments.
Returns
int 0 on success, 1 on failure.

Definition at line 37 of file source_hot.c.

References arrlen(), compare_hot_scripts(), hot_script_t::count, getaparam(), hot_script_t::path, path, hot_script_t::total_ms, zalloc(), zfree(), zsfree(), ztrdup(), and zwarnnam().

Referenced by bin_zpmod().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cmd_source_study()

int cmd_source_study ( char *  nam,
char **  argv 
)

Definition at line 313 of file zpmod_builtin.c.

References zp_source_study_core(), and zwarnnam().

Referenced by bin_zpmod().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ enables_()

int enables_ ( Module  m,
int **  enables 
)

Enable/disable builtins as requested by the shell.

Definition at line 50 of file module.c.

◆ features_()

int features_ ( Module  m,
char ***  features 
)

Provide feature list (builtins) to zsh.

Definition at line 45 of file module.c.

◆ finish_()

int finish_ ( Module  m)

◆ my_ztrdup_glen()

char* my_ztrdup_glen ( const char *  s,
unsigned *  len_ret 
)

zalloc-backed strdup with length out parameter.

Duplicate a C string using zalloc and report length.

Definition at line 63 of file utils.c.

References zalloc().

Referenced by zp_unmetafy_zalloc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setup_()

int setup_ ( Module  m)

◆ zp_build_source_report()

char* zp_build_source_report ( int  no_paths,
int *  rep_size 
)

Definition at line 670 of file source.c.

References source_event::duration, zp_sevent_node::event, source_event::file_name, source_event::full_path, gethashnode2(), zalloc(), zfree(), zp_icon(), zp_sevent_count, zp_source_events, zrealloc(), and ztrdup().

Here is the call graph for this function:

◆ zp_freeparamnode()

void zp_freeparamnode ( HashNode  hn)

◆ zp_path_warmup_core()

int zp_path_warmup_core ( const char *  nam,
int  quiet,
int  prune_missing,
int  dry_run 
)

Implements path-warmup functionality for executable discovery and path pruning.

Scan $PATH directories to warm filesystem caches and prepare command lookup.

This function provides two main features:

  1. Executable Discovery: Scans all directories in $PATH and touches executable files to warm filesystem caches and improve command lookup performance.
  2. Path Pruning: Safely removes non-existent directories from $PATH to reduce lookup overhead and eliminate stale entries.

Memory Safety Design

The prune functionality was redesigned to avoid memory corruption issues that occurred when mixing getaparam("path") results with setaparam("path") calls. The original implementation caused double-free errors due to zsh's parameter management internals.

Solution: Use a two-pass approach with separate array construction:

  • Pass 1: Count valid directories without modifying the original array
  • Pass 2: Build a new array using ztrdup() for proper string ownership
  • Safe update: Use setaparam("path", new_array) without dependency on getaparam() result
Parameters
namBuiltin name for error reporting
quietSuppress progress messages if true
prune_missingRemove non-existent directories from path if true
dry_runReport what would be pruned without making changes if true
Returns
Number of executable files discovered, or negative on error
Note
This function follows zpmod's memory management patterns using zsh allocators (zalloc, ztrdup, zfree) to ensure compatibility with zsh's garbage collection.

Definition at line 254 of file fs.c.

References arrlen(), getaparam(), PATH_MAX, setaparam(), zalloc(), and ztrdup().

Referenced by bin_zpmod().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ zp_unmetafy_zalloc()

char* zp_unmetafy_zalloc ( const char *  to_copy,
int *  new_len 
)

Duplicate and unmetafy a zsh string with zalloc; see header for details.

Duplicate and unmetafy a zsh string using zsh allocators.

Definition at line 76 of file utils.c.

References my_ztrdup_glen(), unmetafy(), zalloc(), and zfree().

Referenced by cmd_report_append(), zp_dirlist_core(), zp_pathstat_core(), and zp_readfile_core().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ zpmod_usage()

void zpmod_usage ( void  )

Print usage for the zpmod builtin.

Definition at line 96 of file zpmod_builtin.c.

References zp_icon().

Referenced by bin_zpmod().

Here is the call graph for this function:
Here is the caller graph for this function: