This commit is contained in:
Yuri Zamyatin 2025-10-04 14:54:06 +00:00
parent 0ed437b0ea
commit 06660b992f
No known key found for this signature in database
GPG Key ID: 4009A4CD0333B579
2 changed files with 72 additions and 19 deletions

View File

@ -7,6 +7,26 @@ parse() {
local args=("$@")
local i=0
local arg_count=${#args[@]}
expand_flags() {
local expanded=()
for arg in "$@"; do
if [[ $arg =~ ^-[a-zA-Z]{2,}$ ]]; then
# This is a combined flag like -iu
local chars="${arg:1}"
for ((j=0; j<${#chars}; j++)); do
expanded+=("-${chars:$j:1}")
done
else
expanded+=("$arg")
fi
done
args=("${expanded[@]}")
arg_count=${#args[@]}
}
expand_flags "$@"
while [[ $i -lt $arg_count ]]; do
case "${args[$i]}" in
-u|--user)
@ -29,46 +49,71 @@ parse() {
break
;;
-*)
# Unknown flag, skip it
((i++))
;;
su)
((i++))
local su_user=""
local su_args=()
# Collect all remaining args for su processing
while [[ $i -lt $arg_count ]]; do
case "${args[$i]}" in
su_args+=("${args[$i]}")
((i++))
done
# Expand combined flags in su arguments
local su_expanded=()
for arg in "${su_args[@]}"; do
if [[ $arg =~ ^-[a-zA-Z]{2,}$ ]]; then
local chars="${arg:1}"
for ((j=0; j<${#chars}; j++)); do
su_expanded+=("-${chars:$j:1}")
done
else
su_expanded+=("$arg")
fi
done
# Process expanded su arguments
local k=0
local su_count=${#su_expanded[@]}
while [[ $k -lt $su_count ]]; do
case "${su_expanded[$k]}" in
-|-l|--login)
sudo_login_shell=true
((i++))
((k++))
;;
-c|--command)
((i++))
if [[ $i -lt $arg_count ]]; then
sudo_command="${args[$i]}"
((i++))
((k++))
if [[ $k -lt $su_count ]]; then
sudo_command="${su_expanded[$k]}"
((k++))
fi
;;
--session-command=*)
sudo_command="${args[$i]#*=}"
((i++))
sudo_command="${su_expanded[$k]#*=}"
((k++))
;;
-m|-p|--preserve-environment)
((i++))
((k++))
;;
-s|--shell)
((i++))
if [[ $i -lt $arg_count && "${args[$i]}" != -* ]]; then
((i++))
((k++))
if [[ $k -lt $su_count && "${su_expanded[$k]}" != -* ]]; then
((k++))
fi
;;
-*)
((i++))
((k++))
;;
*)
if [[ -z "$su_user" ]]; then
su_user="${args[$i]}"
((i++))
su_user="${su_expanded[$k]}"
((k++))
else
((i++))
((k++))
fi
;;
esac
@ -110,8 +155,9 @@ if [ -z "$sudo_command" ]; then
sudo_command="exec /bin/bash --rcfile \"\$WSCOMPAT_DIR/bashrc\""
fi
if [ "$sudo_login_shell" ]; then
bash_flags="--login --noprofile --norc";
exec /bin/sudo -u "$sudo_user" \
/bin/bash --login --noprofile --norc -c "$init_cmd; cd; $sudo_command"
else
bash_flags="--noprofile --norc";
exec /bin/sudo -u "$sudo_user" \
/bin/bash --noprofile --norc -c "$init_cmd; $sudo_command"
fi
exec /bin/sudo -u "$sudo_user" /bin/bash $bash_flags -c "$init_cmd; $sudo_command"

View File

@ -17,6 +17,13 @@ cat << 'WSCOMPAT_EOF_04tcIQE7' > "$WSCOMPAT_DIR/bashrc"
stty intr ^K
alias ssh='wssh'
alias sudo='wsudo'
su() {
if [ "$1" = "-" ]; then
shift && wsudo - -u "$@"
else
wsudo -u "$@"
fi
}
WSCOMPAT_EOF_04tcIQE7
export VIMINIT="source $WSCOMPAT_DIR/vimrc"