many bugfix, fixed getopt split

This commit is contained in:
2026-10-13 16:01:13 +02:00
parent b5be992d82
commit ba1e07744e
22 changed files with 478 additions and 125 deletions
+119
View File
@@ -0,0 +1,119 @@
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# Protected by the BSD3 license. Please read bellow for details.
#
# * Redistribution and use in source and binary forms,
# * with or without modification, are permitted provided
# * that the following conditions are met:
# *
# * Redistributions of source code must retain the above
# * copyright notice, this list of conditions and the
# * following disclaimer.
# *
# * Redistributions in binary form must reproduce the above
# * copyright notice, this list of conditions and the following
# * disclaimer in the documentation and/or other materials
# * provided with the distribution.
# *
# * Neither the name of the copyright holder nor the names
# * of any other contributors may be used to endorse or
# * promote products derived from this software without
# * specific prior written permission.
# *
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# * OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Helpers for the option parsing of the commands. The project rule is to never
# use eval: these functions rebuild argument lists in pure bash.
# Split the output of getopt into the array whose name is $1, without eval and
# without xargs. getopt prints the normalised arguments as one shell-quoted
# line (options bare, every argument in single quotes, a quote inside an
# argument written '\''), for instance:
# -s 'a b' --long 'it'\''s' -- 'file' ''
# Quoted text is taken verbatim (newlines included), an empty argument ('')
# yields an empty element, and nothing is ever evaluated. The usual call is
# local -a _pargs=()
# _getopt_split _pargs "$PARSED"
# set -- "${_pargs[@]}"
# Usage: _getopt_split <array_name> <getopt_output>
_getopt_split()
{
local -n _gs_out="$1"
local s="$2" tok="" in_tok=0 run
local run_re=$'^[^ \t\n\'\\\\]+'
_gs_out=()
while [[ -n "$s" ]]; do
case "${s:0:1}" in
"'")
s="${s:1}"
if [[ "$s" == *\'* ]]; then
tok+="${s%%\'*}"
s="${s#*\'}"
else
# Unterminated quote (never produced by getopt): keep the rest
tok+="$s"
s=""
fi
in_tok=1
;;
'\')
tok+="${s:1:1}"
s="${s:2}"
in_tok=1
;;
' '|$'\t'|$'\n')
(( in_tok )) && _gs_out+=("$tok")
tok=""
in_tok=0
s="${s:1}"
;;
*)
[[ "$s" =~ $run_re ]] || break
run="${BASH_REMATCH[0]}"
tok+="$run"
s="${s:${#run}}"
in_tok=1
;;
esac
done
(( in_tok )) && _gs_out+=("$tok")
return 0
}
export -f _getopt_split
# Put back traps saved with "trap -p" (one "trap -- 'command' SIGNAL" line per
# trap), without eval. Used to restore the caller's traps after temporarily
# replacing them. An empty argument restores nothing.
# Usage: _trap_restore "$(trap -p INT TERM)"
_trap_restore()
{
local line
local -a w=()
while IFS= read -r line; do
[[ "$line" == "trap -- "* ]] || continue
_getopt_split w "$line"
# w = (trap -- <command> <SIGNAL>)
[[ ${#w[@]} -eq 4 ]] && trap -- "${w[2]}" "${w[3]}"
done <<< "$1"
return 0
}
export -f _trap_restore
# ------------------------------------------------------------------------------
# EOF
+20 -8
View File
@@ -250,10 +250,13 @@ utaz()
return 1
fi
# getopt emits a shell-quoted, normalised argument list ("--" terminates the
# options); re-evaluating it restores the arguments with spaces and quotes
# intact. Declared local so flags never leak in from the caller's environment.
# options); _getopt_split rebuilds the arguments from it (spaces and quotes
# intact, no eval). The flags are declared local so that none leaks in from
# the caller's environment.
local willrm="" createdir="" nodir="" icase="" csens=""
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
@@ -359,7 +362,12 @@ utaz()
elif [[ -d "$zitem" ]]; then
# Only direct children are considered (no recursion); NUL-separated
# so names containing spaces or newlines survive.
mapfile -d '' -t targets < <(find "$zitem" -mindepth 1 -maxdepth 1 -print0 2>/dev/null)
# ("mapfile -d" needs bash 4.4, the profile supports 4.3)
local _zt
targets=()
while IFS= read -r -d '' _zt; do
targets+=("$_zt")
done < <(find "$zitem" -mindepth 1 -maxdepth 1 -print0 2>/dev/null)
if [[ ${#targets[@]} -eq 0 ]]; then
disp I "Directory ${zitem} is empty, skipping."
continue
@@ -517,11 +525,13 @@ utaz()
# top-level directory (it then acts as the host directory).
# nullglob makes the array empty when nothing matches; dotglob
# makes hidden entries count, so ".git" alone is not ignored.
local _old_glob
_old_glob=$(shopt -p nullglob dotglob)
local _had_ng=0 _had_dg=0
shopt -q nullglob && _had_ng=1
shopt -q dotglob && _had_dg=1
shopt -s nullglob dotglob
local contents=( "${dir}"/* )
eval "$_old_glob"
(( _had_ng )) || shopt -u nullglob
(( _had_dg )) || shopt -u dotglob
[[ ${#contents[@]} -eq 1 && -d "${contents[0]}" && ! -L "${contents[0]}" ]] && flatten=1
fi
@@ -719,7 +729,9 @@ taz()
# See utaz: restore the getopt-normalised argument list, and keep every
# flag local so nothing leaks in from the caller's environment.
local willrm="" keepall="" verbose="" quiet="" nproc="" complevel=""
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
local compforms=()
local _f _list
while true; do
+3 -1
View File
@@ -241,7 +241,9 @@ conf_dump()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list, "--" ends options
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
+3 -1
View File
@@ -89,7 +89,9 @@ settrace()
return 1
fi
# getopt emits a shell-quoted, normalised argument list ("--" ends options)
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
# First pass: gather the requested action and flags. --on, --off and
# --status are mutually exclusive; --force only modifies --on and --off.
+3 -1
View File
@@ -651,7 +651,9 @@ mdcat()
fi
# getopt emits a shell-quoted, normalised argument list ("--" ends options)
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
+27 -13
View File
@@ -52,7 +52,9 @@ expandlist()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -90,8 +92,8 @@ expandlist()
local item="" result="" matched=0
# Restore the caller's nullglob setting on exit instead of forcing it off.
local _saved_nullglob
_saved_nullglob=$(shopt -p nullglob)
local _had_nullglob=0
shopt -q nullglob && _had_nullglob=1
shopt -s nullglob
# An empty IFS keeps the unquoted expansion below from splitting on spaces
# (it would cut "my dir/*.txt" in two) while globbing still happens.
@@ -129,7 +131,7 @@ expandlist()
unset content
done
eval "$_saved_nullglob"
(( _had_nullglob )) || shopt -u nullglob
printf '%s\n' "$result"
}
export -f expandlist
@@ -158,7 +160,9 @@ clean()
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -289,7 +293,9 @@ rmspc()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -352,8 +358,8 @@ rmspc()
(( verb )) && mvopt=(-v)
# Restore the caller's nullglob setting on exit instead of forcing it off.
local _saved_nullglob
_saved_nullglob=$(shopt -p nullglob)
local _had_nullglob=0
shopt -q nullglob && _had_nullglob=1
shopt -s nullglob
local f
for f in *; do
@@ -410,7 +416,7 @@ rmspc()
fi
done
unset f
eval "$_saved_nullglob"
(( _had_nullglob )) || shopt -u nullglob
}
export -f rmspc
# ------------------------------------------------------------------------------
@@ -446,7 +452,9 @@ file_stats()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -701,7 +709,9 @@ findbig()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -812,7 +822,9 @@ findzero()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -933,7 +945,9 @@ finddead()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
+11 -8
View File
@@ -97,11 +97,8 @@ _fun_stop_on_key()
kill "$pid" 2>/dev/null
}
wait "$pid" 2>/dev/null
if [[ -n "$old_int" ]]; then
eval "$old_int"
else
trap - INT
fi
trap - INT
_trap_restore "$old_int"
return 0
}
export -f _fun_stop_on_key
@@ -129,7 +126,9 @@ busy()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -219,7 +218,9 @@ fake_compile()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -421,7 +422,9 @@ hack()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
+24 -8
View File
@@ -132,7 +132,9 @@ gst()
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
@@ -180,7 +182,9 @@ ggraph()
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
local limit=30
while true; do
case "$1" in
@@ -232,7 +236,9 @@ gsync()
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
@@ -299,7 +305,9 @@ gacp()
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
local msg="" auto_add=0
case "${GIT_GACP_AUTO_ADD,,}" in
1|true|yes|on) auto_add=1 ;;
@@ -423,7 +431,9 @@ greset()
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
local clean_ignored=0
while true; do
case "$1" in
@@ -560,7 +570,9 @@ gwip()
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
@@ -618,7 +630,9 @@ gprune()
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
@@ -731,7 +745,9 @@ groot()
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
local do_go=0
while true; do
case "$1" in
+12 -4
View File
@@ -51,7 +51,9 @@ ver()
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
@@ -92,7 +94,9 @@ meteo()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
@@ -149,7 +153,9 @@ pinfo()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
@@ -392,7 +398,9 @@ showinfo()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
+9 -3
View File
@@ -109,7 +109,9 @@ setlocale()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
@@ -232,8 +234,12 @@ build_locale_shortcuts()
continue
fi
# Optional collision warning
if declare -F "$fname" >/dev/null 2>&1; then
# Collision warning. A function that already calls setlocale is one of
# these shortcuts, typically inherited through the environment from a
# parent shell that loaded the profile: replacing it is not worth a
# warning at every nested shell.
if declare -F "$fname" >/dev/null 2>&1 && \
! declare -f "$fname" 2>/dev/null | grep -q 'setlocale'; then
disp W "Overriding existing function '$fname'."
fi
+8 -4
View File
@@ -134,7 +134,7 @@ pkgs()
return 1
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
@@ -213,7 +213,7 @@ pkgf()
return 1
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
@@ -342,7 +342,9 @@ pkgi()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -462,7 +464,9 @@ pkgr()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
+10 -6
View File
@@ -52,7 +52,7 @@ ppg()
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -124,7 +124,7 @@ ppu()
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -174,7 +174,7 @@ ppn()
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -235,7 +235,7 @@ gpid()
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -356,7 +356,9 @@ ku()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
@@ -506,7 +508,9 @@ kt()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
+13
View File
@@ -592,6 +592,13 @@ _prompt_ctx_add()
fi
}
# Store the exit status of the user's last command (first command of a
# PROMPT_COMMAND that also contains other commands, see profile.sh).
_profile_save_status()
{
_profile_status=$?
}
# ------------------------------------------------------------------------------
# Function triggered internally by bash : defining prompt
# Usage: set_prompt
@@ -601,6 +608,12 @@ _prompt_ctx_add()
set_prompt()
{
local Last_Command=$? # Must come first: nothing may run before this line
# When other commands precede set_prompt in PROMPT_COMMAND (profile.sh adds
# _profile_save_status first for that case), $? is theirs: use the saved one.
if [[ -n "${_profile_status+x}" ]]; then
Last_Command="$_profile_status"
unset _profile_status
fi
local FancyX='\342\234\227'
local Checkmark='\342\234\223'
+3 -1
View File
@@ -109,7 +109,9 @@ genpwd()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
+4 -2
View File
@@ -265,7 +265,7 @@ _rain_leave()
printf '\e[0m\e[?25h\e[2J\e[H'
[[ -n "$saved_stty" ]] && stty "$saved_stty"
trap - TERM INT WINCH
[[ -n "$saved_traps" ]] && eval "$saved_traps"
_trap_restore "$saved_traps"
return 0
}
@@ -677,7 +677,9 @@ rainbow()
frame_sleep=$(_rain_normalize_speed "$_raw_speed") || frame_sleep=0.040
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)
+3 -1
View File
@@ -81,7 +81,9 @@ rmhost()
return 1
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
+6 -2
View File
@@ -95,7 +95,9 @@ check_updates()
return 2
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
@@ -189,7 +191,9 @@ profile_upgrade()
return 2
fi
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
local -a _pargs=()
_getopt_split _pargs "$PARSED"
set -- "${_pargs[@]}"
while true; do
case "$1" in
-h|--help)