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

Utility helpers shared across module components. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

char * zp_unmetafy_zalloc (const char *to_copy, int *new_len)
 Duplicate and unmetafy a zsh string using zsh allocators. More...
 
char * my_ztrdup_glen (const char *s, unsigned *len_ret)
 Duplicate a C string using zalloc and report length. More...
 
int zp_has_option (char **argv, char opt)
 Lightweight option scanner for argv-style subcommands. More...
 
int zp_take_opt_with_arg (char ***argvp, char opt, char **out_arg)
 Consume a short option that requires an argument from argv. More...
 

Detailed Description

Utility helpers shared across module components.

These helpers follow zpmod’s memory rules: allocate with zsh allocators, and free with exact lengths where applicable.

Definition in file zpmod_utils.h.

Function Documentation

◆ my_ztrdup_glen()

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

Duplicate a C string using zalloc and report length.

Parameters
sSource string (may be NULL).
len_retOut parameter receiving the allocated buffer length.
Returns
Newly allocated duplicate (must be freed with zfree using *len_ret), or NULL if s is NULL.

Duplicate a C string using zalloc and report length.

Definition at line 63 of file utils.c.

◆ zp_has_option()

int zp_has_option ( char **  argv,
char  opt 
)

Lightweight option scanner for argv-style subcommands.

Scans argv for a short option (e.g., 'l' for -l). Stops at "--" if encountered.

Parameters
argvArgument vector terminated by NULL.
optShort option character to detect.
Returns
1 if present, 0 otherwise.

Lightweight option scanner for argv-style subcommands.

Definition at line 13 of file utils.c.

◆ zp_take_opt_with_arg()

int zp_take_opt_with_arg ( char ***  argvp,
char  opt,
char **  out_arg 
)

Consume a short option that requires an argument from argv.

Recognizes patterns "-oARG" (attached) and "-o ARG" (separate). When matched, advances the argv pointer and writes the argument to out_arg.

Parameters
argvpIn/out pointer to argv cursor (modified on consumption).
optShort option character to match (e.g., 'd').
out_argOut parameter set to the option argument on success.
Returns
1 if option was present and consumed successfully, 0 if the current argv element does not match the option, -1 if the option matched but the argument was missing.

Consume a short option that requires an argument from argv.

Supports both attached (-oARG) and separate (-o ARG) forms.

Definition at line 35 of file utils.c.

Referenced by cmd_pathstat().

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 using zsh allocators.

Makes a new allocation with zalloc, containing the unmetafied copy of to_copy. When new_len is provided, it receives the exact buffer length to use with zfree.

Parameters
to_copyMetafied zsh string to duplicate and unmetafy.
new_lenOptional out parameter for the returned buffer length.
Returns
Newly allocated buffer (must be freed with zfree using length), or NULL on allocation failure.

Duplicate and unmetafy a zsh string using zsh allocators.

Definition at line 76 of file utils.c.