Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Created November 27, 2022 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ormaaj/577393805b7e6c5fb868d2c97277dea0 to your computer and use it in GitHub Desktop.
Save ormaaj/577393805b7e6c5fb868d2c97277dea0 to your computer and use it in GitHub Desktop.
init
#!/bin/bash
# ormaaj's init
\unalias -a
shopt -s lastpipe extglob expand_aliases
shopt -u assoc_expand_once
typeset -ga bool=([1]=)
function initializeBuiltins {
typeset -a a
typeset x y z libdir
set +m
IFS= read -rd '' -u 3 libdir 3</proc/self/cmdline
libdir=$(readlink -qne -- "${libdir%%+([^/])}/../usr/libx32")
#{
# libdir=$(readlink -qne -- "$(dirname "$(readlink -qnf -- "$(grep -zm 1 -E \$ /dev/fd/3)")")/../usr/lib64")
#} 3</proc/self/cmdline
[[ -d ${libdir} ]] || return 0
# Load the good builtins, omit the useless ones, and remap those that are incomplete or broken.
for x in "${libdir}"/{ctypes.so,bash/!(hello|truefalse|mypid|necho|*.*)}; do
if [[ $(file -b --mime-type -- "$x") == 'application/x-sharedlib' && !$? -ne 0 ]]; then
nm -CDj --without-symbol-versions -- "$x" |
while IFS= read -r y; do
[[ $y =~ ^(.+)_struct$ ]] || continue
a+=("${BASH_REMATCH[1]}")
done
(( PIPESTATUS[0] )) || enable -f "$x" -- "${a[@]}" 2>&3
unset -v 'a[@]'
fi
done 3>/dev/null
for x in ln head rm stat finfo; do
printf -v y %q%.s "$(type -P "$x")" "$((z=$?))"
let z && continue
eval -- \
function "${x}" { command -- "$y" \"\$@\" \; } \; \
function "${x}2" { builtin -- "$x" \"\$@\" \; }
done
return 0
}
function checkValidNames {
${1:+\:} return 1
typeset LC_{CTYPE,COLLATE}=C
typeset _name
for _name; do
[[ ${_name} == [[:alpha:]_]*([[:alnum:]_]) ]] || return 1
done
}
function arrayToSet {
checkValidNames "$1" "$2" || return
typeset -n _arr=$1 _set=$2
typeset _elems
printf -v _elems -- '[%q]= ' "${_arr[@]}"
eval ${BASH_VERSION+'--'} "_set+=(${_elems%% })"
}
function closefds {
typeset -n arr=$1
[[ ${arr@a} == *A* ]] || return
set -- "${!arr[@]}"
set -- "${@/*/${!arr}[&]}"
typeset -n ref
for ref; do
exec {ref}<&-
done
}
# run in the new interactive shell before showing the prompt
function shellinit {
typeset -n _fd=$1
shift
shopt -q login_shell || BASH_XTRACEFD=${_fd[devnull]} command . /etc/profile
(( BASHPID - 1 )) || printf "We're #%d!\n" "$BASHPID"
#mount -nva
#mountpoint -q /mnt/ormaaj3 || mount -nv /mnt/
set -m
typeset -ga PROMPT_COMMAND=('history -a')
typeset -i COLS LINES
tput -T "$TERM" -S <<<$'cols\nlines' | { IFS= read -r COLS; IFS= read -r LINES; }
#reptyr -VL tmux new -t 0 -s init \; resizew -x "$COLS" -y "$LINES" \; detach
#( exec -a init -- reptyr -VL /usr/bin/unshare --pid /sbin/init )
}
function init {
set -x
typeset -ix BASH_XTRACEFD
exec {BASH_XTRACEFD}>&2
if [[ ${FUNCNAME[0]} != "${FUNCNAME[1]}" ]]; then
unset -v HOME
typeset -gx \
HOME=~ \
HISTFILESIZE=-1 \
LANG=en_US.UTF-8 \
LC_CTYPE=C.UTF-8 \
LC_COLLATE=C.UTF-8 \
BASH_COMPAT=51 \
COLORS=$(tput -T linux-16color colors) \
MANPAGER=less
{
"${FUNCNAME[0]}" fd "$@";
} {fd[stdin]}<&0 {fd[stdout]}>&1 {fd[devnull]}>/dev/null
closefds fd
return
fi
typeset -n _fd=$1
shift
typeset -a bashargs=(
-o vi
+o history
+o histexpand
+o monitor
-O lithist
-O histappend
-O lastpipe
-O extglob
-O expand_aliases
+O assoc_expand_once
)
tput -T linux-16color longname
typeset -gx TERM=linux${bool[$?]+-16color}
# setterm --term "$TERM" --regtabs 4
# setfont ter-v12n
# pgrep -c gpm >&"${_fd[devnull]}" || gpm -m /dev/input/mice -t ps2
#mount -vna
#env-update
BASH_XTRACEFD=${_fd[devnull]} command . /etc/profile
BASH_XTRACEFD=${_fd[devnull]} command . ~/.bashrc
shopt -s nullglob
IFS=\| command eval eval '"namespaces=( /proc/self/ns/@(${namespaces[*]}) )"'
typeset nsdir=~/ns
[[ -d $nsdir ]] || mkdir -pm 700 -- "$nsdir"
typeset -A ns
arrayToSet namespaces ns
typeset -a params=("$@")
set -- "${!ns[@]}"
typeset -n ref
set +m
{ typeset -p -- "${!_fd}" | tee "/dev/fd/${BASH_XTRACEFD}"; typeset -fp shellinit closefds; } | {
typeset -p "${!_fd}" >&"${BASH_XTRACEFD}";
[[ -o xtrace ]]
PS1= PS2= command exec -a init -- /bin/bash -psi "${bashargs[@]}" "$(( $? + ${#@} ))" "$PS1" "$PS2" "$@"
} {_fd[pipe]}<&0- >&"${_fd[devnull]}" 2>&1 {_fd[heredoc]}<<-\EOF1 <<-EOF2
{
command . "/dev/fd/${fd[heredoc]}" -- "$@"
typeset +x PS1=$1 PS2=$2; command shift 2
exec <&"${fd[stdin]}"- >&"${fd[stdout]}"- 2>&1 4<&-
{ typeset -p fd
lsof -ap "$BASHPID" -d 0-9999 +f g; } >&"$BASH_XTRACEFD"
set -mH +x
}
trap 'echo done >&"${fd[stdout]}"' exit
[[ $# == "${1-1}" ]] || set -x; command shift
command . "/dev/fd/${fd[pipe]}"
shellinit fd "$@"
EOF1
fd+=([pipe]=${_fd[pipe]}); exec <&"\${fd[heredoc]}"
EOF2
}
init "$@"
# vim: set ft=bash fenc=utf-8 ff=unix ts=4 noet :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment