-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
init.sh
123 lines (116 loc) Β· 5.09 KB
/
init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env sh
# WHY: test -z "${DOROTHY_LOADED-}"
#
# sometimes bash_profile and bashrc are loaded on new terminals
# sometimes bash_profile is loaded at login, and bashrc is loaded on new terminals
# sometimes bash_profile will remember and export variables, but not functions
# zsh loads zprofile, and zshrc, but forgets functions
#
# As such, we need a way that will prevent double loads, but prevent inheritance.
# As functions in bash are sometimes forgotten, and functions in zsh are forgotten,
# that rules out https://stackoverflow.com/a/14467452 for this purpose.
# And as exported variables will be inherited by subshells, that rules out exported variables.
# As such, checking for the existence of a non-global non-local variable will work for this case.
# Note that bash v3 does not support `test ! -v 'DOROTHY_LOADED'`, so have to use `test -z "${DOROTHY_LOADED-}"``
# WHY: "$0" != 'bash'
#
# Sometimes, non-login shell invocations will attempt to load this script:
# - `bash -c -- 'echo $BASH_VERSION'`
# - `zsh -c -- 'echo $BASH_VERSION'`
#
# Whereas only true login shells should, as well as manual login shell invocations:
# - `bash -lc -- 'echo $BASH_VERSION'`
# - `zsh -lc -- 'echo $BASH_VERSION'`
#
# As such, we need to detect this difference, here are the samples:
#
# A true bash login shell (v3):
# - `$0` is `-bash`
# - `$-` is `himBH` via this script and manully, or if v5 then `himBHs` manually
# - `shopt -qp login_shell` returns `0`
#
# Manual invocation `bash -il` (v5):
# - `$0` is `bash`
# - `$-` is `himBH` via this script, then `himBHs` manually
# - `shopt -qp login_shell` returns `0`
#
# Manual invocation `/bin/bash -il` (v3) and `/usr/local/bin/bash -il` (v5):
# - `$0` is bash location: `/bin/bash`
# - `$-` is "himBH" via this script and manually
# - `shopt -qp login_shell` returns `0`
#
# A true zsh login shell:
# - `$0` is `-zsh`
# - `$-` is `569XZilm` via this script, then `569XZilms` manually
# - `[[ -o login ]]` returns `0`
#
# Manual invocation of zsh login shell via `/bin/zsh -il`:
# - `$0` is current script then zsh location
# - `$-` is `569XZilm` via this script, then `569XZilms` manually
# - `[[ -o login ]]` returns `0`
#
# Manual invocation of zsh non-login shell via `/bin/zsh -i`:
# - `$0` is current script then zsh location
# - `$-` is `569XZim` via this script, then `569XZims` manually
# - `[[ -o login ]]` returns `0`
if test -n "${DOROTHY_FORCE_LOAD-}"; then
DOROTHY_LOAD="$DOROTHY_FORCE_LOAD"
else
DOROTHY_LOAD='no' # this must be outside the below if, to ensure DOROTHY_LOAD is reset, and DOROTHY_LOADED is respected, otherwise posix shells may double load due to cross-compat between dotfiles (.profile along with whatever they support)
if test -z "${DOROTHY_LOADED_SHARED_SCOPE-}"; then
# `-dash` is macos login shell, `dash` is manual `dash -l` invocation (as $- doesn't include l in dash)
if test "$0" = '-bash' -o "$0" = '-zsh' -o "$0" = '-dash' -o "$0" = 'dash'; then
DOROTHY_LOAD='yes'
elif test -n "${BASH_VERSION-}"; then
# trunk-ignore(shellcheck/SC3044)
if shopt -qp login_shell; then
DOROTHY_LOAD='yes'
elif test "$-" = 'himBH' -a "${DOROTHY_LOADED_EXPORT_SCOPE-}" != 'yes'; then
case "${GIO_LAUNCHED_DESKTOP_FILE-}" in *lxterminal*) DOROTHY_LOAD='yes' ;; esac
fi
elif test -n "${ZSH_VERSION-}"; then
# trunk-ignore(shellcheck/SC3010)
if [[ -o login ]]; then
DOROTHY_LOAD='yes'
fi
elif test -z "$-" -a -z "$*" -a "${CI-}" = 'true'; then
DOROTHY_LOAD='yes' # dash on github ci, in which [$-] and [$*] are empty, and $0 = /home/runner....
elif test -n "${NVIM-}"; then
DOROTHY_LOAD='yes' # neovim: $0 = init.sh, and BASH_SOURCE[*] is undefined
else
# bash v3 and dash do not set l in $-
# zsh does, however zsh we have a definite option earlier
# so this is for the alternative posix shells
case $- in *l*) DOROTHY_LOAD='yes' ;; esac
fi
fi
fi
# if your login shell is failing identification,
# then make sure your terminal preferences has login shell enabled
if test "${DOROTHY_LOAD-}" = 'yes'; then
# non-exported scope, used to prevent case where bash_profile loads at login then bashrc loads on new terminsl
DOROTHY_LOADED_SHARED_SCOPE='yes'
# exported scope, used to prevent our workaround for non-login-terminal-applications from loading dorothy in manual non-login bash invocations
export DOROTHY_LOADED_EXPORT_SCOPE
DOROTHY_LOADED_EXPORT_SCOPE='yes'
# this should be consistent with:
# $DOROTHY/init.fish
# $DOROTHY/init.sh
# $DOROTHY/commands/dorothy
if test -z "${DOROTHY-}"; then
# https://stackoverflow.com/a/246128
# https://stackoverflow.com/a/14728194
# if true login shell on macos, then $0 is [-bash], [-zsh], [-dash], etc.
export DOROTHY
if test -n "${XDG_DATA_HOME-}" -a -n "${XDG_DATA_HOME-}/dorothy"; then
DOROTHY="$XDG_DATA_HOME/dorothy"
else
DOROTHY="$HOME/.local/share/dorothy"
fi
fi
# init dorothy's environment for the login shell
. "$DOROTHY/sources/login.sh"
# if the login shell is also interactive, then init dorothy for the interactive login shell
# [test -t 0] and [test -s] are true despite [env -i bash -lc ...]
case $- in *i*) . "$DOROTHY/sources/interactive.sh" ;; esac
fi