Skip to main content

Module utils

Module utils 

Source
Expand description

Utility functions for zshrs

Port from zsh/Src/utils.c

Provides miscellaneous utilities: error handling, file operations, string utilities, and character classification.

Structs§

DirSav
Initialize directory save struct (from utils.c init_dirsav)
HookManager
Hook function manager (from utils.c callhookfunc)
TimedFn
Timed function entry (from utils.c addtimedfn/deltimedfn)

Enums§

QuoteType
Quote type constants for quotestring() Port from zsh.h QT_* enum

Statics§

SCRIPT_FILENAME
Script filename
SCRIPT_NAME
Script name for error messages

Functions§

addlockfd
Add lock file descriptor (from utils.c addlockfd)
addmodulefd
Add module file descriptor (from utils.c addmodulefd)
addunprintable
Add unprintable character representation (from utils.c addunprintable)
adduserdir
Add user directory (from utils.c adduserdir)
adjustcolumns
Adjust terminal columns (from utils.c adjustcolumns)
adjustlines
Adjust terminal lines (from utils.c adjustlines)
adjustwinsize
Adjust terminal window size (from utils.c adjustwinsize)
append_file
Append to file
arrdup
Duplicate array (from utils.c arrdup)
arrdup_max
Duplicate array with max elements (from utils.c arrdup_max)
arrlen
Array length - port from arrlen()
arrlen_ge
Check if array length >= n (from utils.c arrlen_ge)
arrlen_gt
Check if array length > n (from utils.c arrlen_gt)
arrlen_le
Check if array length <= n (from utils.c arrlen_le)
arrlen_lt
Check if array length < n (from utils.c arrlen_lt)
attachtty
Attach to the controlling tty’s process group (from utils.c attachtty)
bicat
Buffer concat (from utils.c bicat)
charcount
Character count for strings
check_fd_table
Check fd table for valid file descriptors (from utils.c check_fd_table)
checkglobqual
Check glob qualifier syntax Port from zsh/Src/utils.c checkglobqual()
checkmailpath
Check mail paths (from utils.c checkmailpath)
checkrmall
Check before removing directory tree (from utils.c checkrmall)
chuck
Remove character from string (from utils.c chuck)
colonsplit
Split colon-separated list (from utils.c colonsplit)
convbase
Convert integer to string with specified base Port from zsh/Src/utils.c convbase()
convfloat
Convert float for output (from utils.c convfloat)
convfloat_underscore
Convert float with underscores (from utils.c convfloat_underscore)
copy_file
Copy file
current_time_fmt
Get current time formatted
dircmp
Compare directory paths (from utils.c dircmp)
dputs
Debug printf (from utils.c dputs) - only active in debug builds
dquotedzputs
Double-quote and print string (from utils.c dquotedzputs)
dquotedztrdup
Duplicate and double-quote a string (from utils.c dquotedztrdup)
dupstrpfx
Duplicate string prefix
dyncat
String concat (from utils.c dyncat)
eaccess
Check access with effective UID (from utils.c eaccess)
environ
Get all environment variables
equalsplit
Split on ‘=’ returning (name, value) (from utils.c equalsplit)
expand_tilde
Expand tilde in a path
file_exists
Check if file exists
file_mtime
Get file modification time as seconds since epoch
file_size
Get file size
find_in_path
Find an executable in PATH
finddir
Find named directory for path (from utils.c finddir)
finddir_scan
Scan for named directory matches (from utils.c finddir_scan)
findpwd
Get the current working directory (port of findpwd/set_pwd_env)
findsep
Find a separator in string (from utils.c findsep)
findword
Find word at position (from utils.c findword)
format_time
Format seconds as HH:MM:SS
fprintdir
Print directory name with ~ substitution (from utils.c fprintdir)
freearray
Free array (no-op in Rust, provided for API compat)
freestr
Free a string (from utils.c freestr) - no-op in Rust
get_groupname
Get group name from GID (from utils.c getgrgid handling)
get_term_height
Get terminal height
get_term_width
Get terminal width
get_user_home_by_name
Get home directory for user by name (from utils.c getpwnam handling)
get_username
Get username from UID (from utils.c getpwuid handling)
getegid
Get effective group ID
getenv
Get environment variable
geteuid
Get effective user ID
getgid
Get current group ID
gethostname
Get hostname
getkeystring
Parse getkeystring escape sequences (from utils.c getkeystring) Handles \n \t \r \e \a \b \f \v \ ' " \xNN \uNNNN \UNNNNNNNN \0NNN
getnameddir
Get named directory (from utils.c getnameddir)
getpid
Get process ID
getppid
Get parent process ID
getquery
Simple interactive query (from utils.c getquery)
getshfunc
Get shell function by name (from utils.c getshfunc)
gettempfile
Create a temporary file (from utils.c gettempfile)
gettempname
Get temporary file/directory name Port from zsh/Src/utils.c gettempname()
gettygrp
Get the terminal’s process group (from utils.c gettygrp)
gettyinfo
Get terminal info (from utils.c gettyinfo/fdgettyinfo)
getuid
Get current user ID
getumask
Get umask
getumask_value
Get or set the file creation mask (wrapper over umask)
has_token
Check if metafied - port from zsh/Src/utils.c has_token()
hash_string
Hash a string (simple djb2)
hasspecial
Check for special characters that need quoting (from utils.c hasspecial)
hmkarray
Make single-element array on heap (from utils.c hmkarray)
imeta
Check if character needs metafication
init_dirsav
inittyptab
Initialize character type table (from utils.c inittyptab) In Rust we use Unicode-aware char methods, so this is mostly a no-op
invnstrcmp
Inverted numeric comparison (from utils.c invnstrncmp)
is_dir
Check if path is a directory
is_directory
Check if a path is a directory
is_executable
Check if a file exists and is executable
is_file
Check if path is a file
is_float
Check if string is a valid floating point number
is_identifier
Check if string is a valid identifier
is_ifs_char
Check if character is an IFS character
is_link
Check if path is a symlink
is_nicechar
Check if a character needs nice formatting (from utils.c is_nicechar)
is_niceformat
Check if nice formatting is needed (from utils.c is_mb_niceformat)
is_number
Check if string looks like a number
is_root
Check if running as root
is_sb_niceformat
Check if single-byte needs nice format (from utils.c is_sb_niceformat)
is_tty
Check if a file descriptor is a tty
is_wcs_nicechar
Check if wide char needs nice formatting (from utils.c is_wcs_nicechar)
is_word_char
Check if character is a word character
is_word_char_simple
Check if character is a simple word character (from utils.c)
isabspath
Check if path is absolute
isprintable
Check if string contains only printable characters
ispwd
Check if path is the current directory (from utils.c ispwd)
itype_end
Check if a character type at end of string (from utils.c itype_end) Returns the position after the identifier characters
iwsep
Check if character is a whitespace separator
lchdir
Change directory with safeguards (from utils.c lchdir)
linecount
Line count for strings
list_dir
List directory contents
mailstat
Check mail file status (from utils.c mailstat)
makeabspath
Make path absolute
makebangspecial
Make bang character special/non-special (from utils.c makebangspecial)
makecommaspecial
Make comma character special (from utils.c makecommaspecial)
mb_charinit
Initialize multibyte state (from utils.c mb_charinit) - no-op in Rust
mb_charlenconv
Multibyte char length (from utils.c mb_charlenconv)
mb_charlenconv_r
Multibyte char length with conversion (from utils.c mb_charlenconv_r)
mb_metacharlenconv_r
Multibyte metachar length with conversion (from utils.c mb_metacharlenconv_r)
mb_metastrlenend
Multibyte metastring length to end (from utils.c mb_metastrlenend)
metafy
Metafy string (from utils.c metafy)
metalen
Count meta characters in string (from utils.c metalen)
mindist
Spelling correction with full path (from utils.c mindist)
mkarray
Make single-element array (from utils.c mkarray)
mkdir
Create directory
mkdir_p
Create directory recursively
mode_to_octal
Convert file mode to octal string (from utils.c mode_to_octal)
monotonic_time_ns
Get monotonic time in nanoseconds
move_fd
Move a file descriptor to a high number (>10)
movefd
Move file descriptor to a high number (from utils.c movefd)
next_word_boundary
Get next word boundary (from utils.c)
nicechar
Nicely format a string for display (escape unprintable chars)
nicechar_ctrl
Get nice representation of control character
nicechar_sel
Nice char with quoting selection (from utils.c nicechar_sel)
nicedup
Dup string nicely (from utils.c nicedup)
nicedupstring
Nice-format and duplicate string (from utils.c nicedupstring)
niceformat
Nice format for string display (from utils.c mb_niceformat)
nicezputs
Nicely format a string
niceztrlen
Count nice string length (from utils.c niceztrlen)
noquery
Suppress query (from utils.c noquery)
normalize_path
Path normalization (from utils.c xsymlink handling)
nstrcmp
Numeric string comparison (from utils.c nstrncmp)
parse_time
Parse HH:MM:SS to seconds
pathprog
Find program in PATH (from utils.c pathprog)
preprompt_actions
Pre-prompt processing (from utils.c preprompt)
prev_word_boundary
Get previous word boundary (from utils.c)
print_if_link
Print symlink target if it is one (from utils.c print_if_link)
printhhmmss
Print/format time in HH:MM:SS (from utils.c printhhmmss)
printprompt4
Print prompt4 (PS4 for trace output) (from utils.c printprompt4)
printsafe
Print-safe string representation
privasserted
Check if running with elevated privileges (from utils.c privasserted)
putraw
Put raw character to terminal (from utils.c putraw)
putshout
Put character to shell output (from utils.c putshout)
quote_string
Quote a string for safe shell use (convenience wrapper)
quotedzputs
Duplicate a string with quoting for display (from utils.c quotedzputs)
quotestring
Quote a string according to the specified type Port from zsh/Src/utils.c quotestring() (lines 6141-6452)
random_int
Generate random integer
random_range
Generate random integer in range [0, max)
read1char
Read a single character (from utils.c read1char)
read_file
Read file contents to string
read_lines
Read file lines
read_loop
Read/write loop wrappers (from utils.c read_loop/write_loop)
read_poll
Read poll - check for pending input Port from zsh/Src/utils.c read_poll() lines 2643-2730
readlink
Read symlink target
realpath
Get real (canonical) path
redup
Redup: duplicate fd x to y (from utils.c redup)
rename_file
Rename/move file
restoredir
Restore saved directory (from utils.c restoredir)
rm_dir
Remove directory
rm_dir_all
Remove directory recursively
rm_file
Remove file
sb_niceformat
Single-byte nice format (from utils.c sb_niceformat)
sepjoin
Join array with separator - port from zsh/Src/utils.c sepjoin() lines 3926-3958
sepsplit
Split string by separator - port from zsh/Src/utils.c sepsplit() lines 3961-3992
set_widearray
Set wide character array (from utils.c set_widearray) - no-op, Rust uses native UTF-8
setblock_fd
Set blocking/nonblocking on a file descriptor Port from zsh/Src/utils.c setblock_fd() lines 2578-2618
setblock_stdin
Set stdin to blocking mode (from utils.c setblock_stdin)
setcbreak
Set terminal to cbreak mode (from utils.c setcbreak)
setenv
Set environment variable
settyinfo
Set terminal info (from utils.c settyinfo/fdsettyinfo)
setumask
Set umask
shescape
Escape string for shell
skipparens
Skip balanced parentheses (from utils.c skipparens)
skipwsep
Skip whitespace separators (from utils.c skipwsep)
skipwsep_ifs
Skip whitespace separators from IFS (port helper, with custom IFS)
slashsplit
Split path into components (from utils.c slashsplit)
spacesplit
IFS-style word splitting - port from zsh/Src/utils.c spacesplit()
spckword
Spelling correction distance (from utils.c spdist, already exists but adding spckword) Check if word is close enough to correct (from utils.c spckword)
spdist
Compute edit distance between two strings (for spelling correction) Port from zsh/Src/utils.c spdist() lines 4675-4759
split_quoted
Split a string respecting quotes
spname
Spelling correction: find closest match (from utils.c spname)
spscan
Scan for spelling correction (from utils.c spscan)
str_ends_with
Check if string ends with suffix (from utils.c)
str_starts_with
Check if string starts with prefix
strpfx
Check if s is a prefix of t (from utils.c strpfx)
strsfx
Check if s is a suffix of t (from utils.c strsfx)
strucpy
Copy string with upper/lower case (from utils.c strucpy)
struncpy
Copy n chars with upper/lower case (from utils.c struncpy)
subst_string_by_func
Call shell function by name (from utils.c subst_string_by_func)
subst_string_by_hook
Call hook function by name (from utils.c subst_string_by_hook)
substnamedir
Substitute named directory in path (from utils.c substnamedir)
symlink
Create symlink
term_columns
Get terminal width (fallback to 80)
term_lines
Get terminal lines (fallback to 24)
time_now
Get current time as seconds since epoch
time_now_ns
Get current time with nanoseconds
timespec_diff_us
Compute time difference in microseconds (from utils.c timespec_diff_us)
tricat
Triple concat (from utils.c tricat)
tulower
Convert character to lowercase
tuupper
Convert character to uppercase
ucs4tomb
Convert UCS-4 to multibyte (from utils.c ucs4tomb)
ucs4toutf8
Convert UCS-4 to UTF-8 (from utils.c ucs4toutf8)
unescape
Unescape string
unmeta
Unmetafy string (from utils.c unmeta lines 4930-5051)
unmeta_one
Unmetafy a single character (from utils.c unmeta_one)
unmetafy
Unmetafy string (from utils.c unmetafy) - zsh meta encoding to plain
unsetenv
Unset environment variable
upchdir
Go up n directories (from utils.c upchdir)
wcs_nicechar
Wide char nice format (from utils.c wcs_nicechar)
wcs_nicechar_sel
Wide char nice format (from utils.c wcs_nicechar_sel)
wcs_zarrdup
Duplicate array of wide strings (from utils.c wcs_zarrdup) - same as zarrdup in Rust
wcsiblank
Check if wide character is blank (from utils.c wcsiblank)
wcsitype
Get wide character type (from utils.c wcsitype)
wordcount
Word count for strings
wordcount_sep
Count words in string (from utils.c wordcount - extended version)
write_file
Write string to file
write_loop
write_to_fd
Write a string to a file descriptor
xsymlink
Resolve symlinks in path (from utils.c xsymlinks/xsymlink)
zarrdup
Duplicate array with zsh allocation (from utils.c zarrdup)
zbasename
Get basename of path (from utils.c)
zbeep
Ring the terminal bell (from utils.c zbeep)
zchdir
Set current working directory
zclose
Close a file descriptor
zcloselockfd
Close lock file descriptor (from utils.c zcloselockfd)
zdeleteterm
Delete terminal setup (from utils.c zdeleteterm)
zdirname
Get dirname of path (from utils.c)
zerr
Print an error message
zerrmsg
Print formatted error with optional errno
zerrnam
Print an error message with command name
zexpandtabs
Expand tabs to spaces (from utils.c zexpandtabs)
zgetcwd
Get current working directory
zjoin
Join array with delimiter (from utils.c zjoin)
zmonotime
Get monotonic time (from utils.c zmonotime)
zreaddir
Check if directory is readable with entries (from utils.c)
zsetupterm
Initialize terminal (from utils.c zsetupterm)
zsleep
Sleep for a given number of seconds (fractional)
zsleep_ms
Sleep for milliseconds
zsleep_random
Sleep random amount up to max microseconds (from utils.c zsleep_random)
zstricmp
Compare strings case-insensitively (from utils.c zstricmp)
zstrstr
Find needle in haystack (from utils.c zstrstr)
zstrtol
Parse a string to a signed integer with base detection Port from zsh/Src/utils.c zstrtol() lines 2384-2516
zstrtol_underscore
Parse integer with underscore separators (from utils.c zstrtol_underscore)
zstrtoul_underscore
Parse unsigned integer with underscore support Port from zsh/Src/utils.c zstrtoul_underscore() lines 2528-2575
ztrcmp
Compare strings with meta handling (from utils.c ztrcmp lines 5106-5130)
ztrdup
String duplicate (from utils.c ztrdup)
ztrdup_metafy
Metafy and duplicate string (from utils.c ztrdup_metafy)
ztrftime
Format time struct (from utils.c ztrftime)
ztrftimebuf
Buffer size helper for time formatting (from utils.c ztrftimebuf)
ztrlen
Unmetafied string length (from utils.c ztrlen lines 5135-5152)
ztrlenend
Get string length counting to end pointer (from utils.c ztrlenend)
ztrncpy
Duplicate n characters (from utils.c ztrncpy)
ztrsub
String pointer subtraction with meta handling (from utils.c ztrsub)
zwarn
Print a warning message
zwarning
Warning with va_list formatting (from utils.c zwarning)
zwarnnam
Print a warning with command name
zwcwidth
Get wide character width (from utils.c zwcwidth)
zz_plural_z_alpha
Plural helper (from utils.c zz_plural_z_alpha) - returns ‘s’ for plural

Type Aliases§

PrepromptFn
Pre-prompt function list (from utils.c addprepromptfn/delprepromptfn)