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

Overrides for '. More...

#include "zpmod.mdh"
#include "zpmod.pro"
#include "zpmod_vendor_shims.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "zpmod_compat.h"
#include "zpmod_emoji.h"
#include "zpmod_source.h"
#include "zpmod_utils.h"
Include dependency graph for source.c:

Go to the source code of this file.

Data Structures

struct  fdhead
 

Macros

#define FD_EXT   ".zwc"
 
#define FD_PRELEN   12
 
#define FD_MAGIC   0x04050607
 
#define FD_OMAGIC   0x07060504
 
#define FDF_MAP   1
 
#define FDF_OTHER   2
 
#define FDHEADERLEN(f)   (((Wordcode)(f))[FD_PRELEN])
 
#define FDMAGIC(f)   (((Wordcode)(f))[0])
 
#define FDSETBYTE(f, i, v)    ((((unsigned char *)(((Wordcode)(f)) + 1))[i]) = ((unsigned char)(v)))
 
#define fdbyte(f, i)   ((wordcode)(((unsigned char *)(((Wordcode)(f)) + 1))[i]))
 
#define FDFLAGS(f)   fdbyte(f, 0)
 
#define FDOTHER(f)   (fdbyte(f, 1) + (fdbyte(f, 2) << 8) + (fdbyte(f, 3) << 16))
 
#define FDVERSION(f)   ((char *)((f) + 2))
 
#define FIRSTFDHEAD(f)   ((FDHead)(((Wordcode)(f)) + FD_PRELEN))
 
#define NEXTFDHEAD(f)   ((FDHead)(((Wordcode)(f)) + (f)->hlen))
 
#define FDHFLAGS(f)   (((FDHead)(f))->flags)
 
#define FDHTAIL(f)   (((FDHead)(f))->flags >> 2)
 
#define FDHF_KSHLOAD   1
 
#define FDHF_ZSHLOAD   2
 
#define FDNAME(f)   ((char *)(((FDHead)(f)) + 1))
 
#define custom_zwcstat(f, b)   (!!stat(f, b))
 

Typedefs

typedef struct fdheadFDHead
 

Functions

int bin_custom_dot (char *name, char **argv, UNUSED(Options ops), UNUSED(int func))
 Replacement handler for '. More...
 
static FDHead custom_dump_find_func (Wordcode h, char *name)
 
static Wordcode custom_load_dump_header (char *nam, char *name, int err)
 
static Eprog custom_check_dump_file (char *file, struct stat *sbuf, char *name, int *ksh, int test_only)
 
Eprog custom_try_source_file (char *file)
 Try to locate or build a ZWC dump for the script and return Eprog. More...
 
mod_export enum source_return custom_source (char *s)
 Execute a sourced script and record a timing event for reporting. More...
 
char * zp_build_source_report (int no_paths, int *rep_size)
 
void zp_free_sevent_node (HashNode hn)
 Hash lifecycle. More...
 
void zp_source_setup_overrides (void)
 
void zp_source_restore_overrides (void)
 

Variables

static HandlerFunc original_dot = NULL
 
static HandlerFunc original_source = NULL
 
static HashTable zp_source_events = NULL
 
static int zp_sevent_count = 0
 

Detailed Description

Overrides for '.

'/source and source-study event recording/reporting.

This module hooks into zsh’s sourcing mechanism to optionally compile ZWC caches, measure durations, and produce a report via the zpmod source-study subcommand. It also respects user options like FUNCTION_ARGZERO, PATH_DIRS, POSIX_BUILTINS, SHIN_STDIN, and SOURCE_TRACE through the stable option mapping helpers.

Definition in file source.c.

Macro Definition Documentation

◆ custom_zwcstat

#define custom_zwcstat (   f,
 
)    (!!stat(f, b))

Definition at line 188 of file source.c.

◆ FD_EXT

#define FD_EXT   ".zwc"

Definition at line 137 of file source.c.

◆ FD_MAGIC

#define FD_MAGIC   0x04050607

Definition at line 139 of file source.c.

◆ FD_OMAGIC

#define FD_OMAGIC   0x07060504

Definition at line 140 of file source.c.

◆ FD_PRELEN

#define FD_PRELEN   12

Definition at line 138 of file source.c.

◆ fdbyte

#define fdbyte (   f,
 
)    ((wordcode)(((unsigned char *)(((Wordcode)(f)) + 1))[i]))

Definition at line 157 of file source.c.

◆ FDF_MAP

#define FDF_MAP   1

Definition at line 141 of file source.c.

◆ FDF_OTHER

#define FDF_OTHER   2

Definition at line 142 of file source.c.

◆ FDFLAGS

#define FDFLAGS (   f)    fdbyte(f, 0)

Definition at line 158 of file source.c.

◆ FDHEADERLEN

#define FDHEADERLEN (   f)    (((Wordcode)(f))[FD_PRELEN])

Definition at line 153 of file source.c.

◆ FDHF_KSHLOAD

#define FDHF_KSHLOAD   1

Definition at line 165 of file source.c.

◆ FDHF_ZSHLOAD

#define FDHF_ZSHLOAD   2

Definition at line 166 of file source.c.

◆ FDHFLAGS

#define FDHFLAGS (   f)    (((FDHead)(f))->flags)

Definition at line 163 of file source.c.

◆ FDHTAIL

#define FDHTAIL (   f)    (((FDHead)(f))->flags >> 2)

Definition at line 164 of file source.c.

◆ FDMAGIC

#define FDMAGIC (   f)    (((Wordcode)(f))[0])

Definition at line 154 of file source.c.

◆ FDNAME

#define FDNAME (   f)    ((char *)(((FDHead)(f)) + 1))

Definition at line 167 of file source.c.

◆ FDOTHER

#define FDOTHER (   f)    (fdbyte(f, 1) + (fdbyte(f, 2) << 8) + (fdbyte(f, 3) << 16))

Definition at line 159 of file source.c.

◆ FDSETBYTE

#define FDSETBYTE (   f,
  i,
 
)     ((((unsigned char *)(((Wordcode)(f)) + 1))[i]) = ((unsigned char)(v)))

Definition at line 155 of file source.c.

◆ FDVERSION

#define FDVERSION (   f)    ((char *)((f) + 2))

Definition at line 160 of file source.c.

◆ FIRSTFDHEAD

#define FIRSTFDHEAD (   f)    ((FDHead)(((Wordcode)(f)) + FD_PRELEN))

Definition at line 161 of file source.c.

◆ NEXTFDHEAD

#define NEXTFDHEAD (   f)    ((FDHead)(((Wordcode)(f)) + (f)->hlen))

Definition at line 162 of file source.c.

Typedef Documentation

◆ FDHead

typedef struct fdhead* FDHead

Definition at line 144 of file source.c.

Function Documentation

◆ bin_custom_dot()

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

Replacement handler for '.

' and 'source' builtins.

Mirrors zsh’s semantics while integrating custom search behavior and error reporting, and delegates to custom_source() to execute the script.

Definition at line 48 of file source.c.

References argzero, custom_source(), dupstring(), freearray(), FUNCTIONARGZERO__, lastval, path, PATHDIRS__, popheap(), POSIXBUILTINS__, pparams, pushheap(), unmeta(), zarrdup(), zerrnam(), zhtricat(), zp_conv_opt(), zsfree(), ztrdup(), and zwarnnam().

Referenced by zp_source_setup_overrides().

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

◆ custom_check_dump_file()

static Eprog custom_check_dump_file ( char *  file,
struct stat *  sbuf,
char *  name,
int *  ksh,
int  test_only 
)
static

Definition at line 387 of file source.c.

References custom_dump_find_func(), custom_load_dump_header(), custom_zwcstat, dummy_eprog, FDF_MAP, FDF_OTHER, FDFLAGS, FDHF_KSHLOAD, FDHF_ZSHLOAD, FDHFLAGS, FDOTHER, incrdumpcount(), fdhead::len, fdhead::npats, fdhead::start, fdhead::strs, zalloc(), and zfree().

Referenced by custom_try_source_file().

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

◆ custom_dump_find_func()

static FDHead custom_dump_find_func ( Wordcode  h,
char *  name 
)
static

Definition at line 191 of file source.c.

References FDHEADERLEN, FDHTAIL, FDNAME, FIRSTFDHEAD, and NEXTFDHEAD.

Referenced by custom_check_dump_file().

Here is the caller graph for this function:

◆ custom_load_dump_header()

static Wordcode custom_load_dump_header ( char *  nam,
char *  name,
int  err 
)
static

Definition at line 202 of file source.c.

References FD_MAGIC, FD_OMAGIC, FD_PRELEN, FDHEADERLEN, FDMAGIC, FDOTHER, FDVERSION, getsparam(), zhalloc(), and zwarnnam().

Referenced by custom_check_dump_file().

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

◆ custom_source()

mod_export enum source_return custom_source ( char *  s)

Execute a sourced script and record a timing event for reporting.

Overridden dot/source and helpers exported from core.

Integrates with zsh state (opts, cmdstack, funcstack) and restores on exit. Records duration and paths for later consumption by the report generator.

Definition at line 506 of file source.c.

References addhashnode(), cmdsp, cmdstack, custom_try_source_file(), source_event::dir_path, dosetopt(), dupstring(), source_event::duration, errflag, zp_sevent_node::event, execode(), exit_pending, fdtable, source_event::file_name, freeeprog(), source_event::full_path, funcstack, source_event::id, lastval, lineno, source_event::load_error, loop(), loops, movefd(), opts, popheap(), printprompt4(), pushheap(), pwd, retflag, scriptfilename, scriptname, SHIN, shinbufrestore(), shinbufsave(), SHINSTDIN__, sourcelevel, SOURCETRACE__, subsh, thisjob, trap_return, trap_state, source_event::ts, unmeta(), xtrerr, zalloc(), zfree(), zp_conv_opt(), zp_sevent_count, zp_source_events, zshcalloc(), and ztrdup().

Referenced by bin_custom_dot().

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

◆ custom_try_source_file()

Eprog custom_try_source_file ( char *  file)

Try to locate or build a ZWC dump for the script and return Eprog.

When possible, compiles to .zwc and loads that for performance. Returns NULL if no dump is available and the caller should use a file fd.

Definition at line 322 of file source.c.

References bin_zcompile(), custom_check_dump_file(), dyncat(), FD_EXT, getsparam(), queue_signals(), strsfx(), unqueue_signals(), zfree(), ztrdup(), and zwarnnam().

Referenced by custom_source().

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

◆ 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_free_sevent_node()

void zp_free_sevent_node ( HashNode  hn)

Hash lifecycle.

Definition at line 727 of file source.c.

References source_event::dir_path, zp_sevent_node::event, source_event::file_name, source_event::full_path, zfree(), and zsfree().

Referenced by zp_source_setup_overrides().

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

◆ zp_source_restore_overrides()

void zp_source_restore_overrides ( void  )

Definition at line 762 of file source.c.

References builtintab, deletehashtable(), original_dot, original_source, and zp_source_events.

Referenced by finish_().

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

◆ zp_source_setup_overrides()

void zp_source_setup_overrides ( void  )

Definition at line 737 of file source.c.

References addhashnode(), bin_custom_dot(), builtintab, emptyhashtable(), gethashnode2(), hasher(), newhashtable(), original_dot, original_source, removehashnode(), zp_free_sevent_node(), zp_source_events, and zwarn().

Referenced by setup_().

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

Variable Documentation

◆ original_dot

HandlerFunc original_dot = NULL
static

Definition at line 37 of file source.c.

Referenced by zp_source_restore_overrides(), and zp_source_setup_overrides().

◆ original_source

HandlerFunc original_source = NULL
static

Definition at line 37 of file source.c.

Referenced by zp_source_restore_overrides(), and zp_source_setup_overrides().

◆ zp_sevent_count

int zp_sevent_count = 0
static

Definition at line 39 of file source.c.

Referenced by custom_source(), and zp_build_source_report().

◆ zp_source_events

HashTable zp_source_events = NULL
static