This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.shrc
164 lines (145 loc) · 4.14 KB
/
.shrc
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# shellcheck shell=sh
# Shell configuration
# Utilities -------------------------------------------------------------------
SHELL_CMD="$(ps -o comm -p $$ | tail -n 2)"
export SHELL_CMD
SHELL_CMD_NAME="$(basename "$(printf %s "$SHELL_CMD" | sed 's|^-||')")"
export SHELL_CMD_NAME
strlist() {
[ $# -eq 0 ] && return
while [ $# -gt 1 ]; do
#printf %s\\0 "$1" | xxd -p | tr -d '\n'
printf %s: "$1"
shift
done
#printf %s "$1" | xxd -p | tr -d '\n'
printf %s "$1"
}
strlist_len() {
#printf %s $(($(printf %s "$1" | xxd -r -p | tr -dC '\000' | wc -m) 1))
printf %s $(($(printf %s "$1" | tr -dC : | wc -m) 1))
}
#strlist_at() {
# # FIXME
#
# local list0="$1"
# local entry_i=0
# while [ "$entry_i" != "$2" ] && [ -n "$list0" ]; do
# [ "$(expr "X$list0" : 'X00.*')" ] && entry_i=$((entry_i 1))
# list0="$(printf %s "$list0" | tail -c 3)"
# done
#
# [ -z "$list0" ] && return 1
#
# local list1="$list0"
# local end_char_i=0
# while [ "$end_char_i" -lt "${#list1}" ]; do
# expr "X$list1" : 'X00.*' >/dev/null && break
# list1="$(printf %s "$list1" | tail -c 3)"
# end_char_i=$((end_char_i 2))
# done
#
# [ "$end_char_i" -eq 0 ] && printf ''
#
# printf %s "$list0" | head -c "$end_char_i" | xxd -r -p
#}
#
#strlist_each() {
# # FIXME
#
# local list0="$1"
# local list1="$1"
# shift
# local end_char_i=0
# while [ "$end_char_i" -lt "${#list}" ]; do
# if expr "X$list" : 'X00.*' >/dev/null; then
# entry="$(printf %s "$list" | head -c "$end_char_i" | xxd -r -p)"
# echo entry: "$entry"
# eval "$@" "$entry" || return
# fi
# list="$(printf %s "$list" | tail -c 3)"
# end_char_i=$((end_char_i 2))
# done
#}
# Local config ----------------------------------------------------------------
PRE_SHRCS=''
SHRC_DIRS="$HOME"/.sh
case "$SHELL_CMD_NAME" in
bash)
PRE_SHRCS="$(strlist /etc/bashrc /etc/bash.bashrc /etc/bash/bashrc)"
SHRC_DIRS="$(strlist "$SHRC_DIRS" "$HOME"/.bash)"
export SHELL_NAME='Bash'
;;
csh)
SHRC_DIRS="$(strlist "$SHRC_DIRS" "$HOME"/.csh)"
export SHELL_NAME='csh'
;;
fish)
SHRC_DIRS="$(strlist "$SHRC_DIRS" "$HOME"/.fish)"
export SHELL_NAME='fish'
;;
tcsh)
SHRC_DIRS="$(strlist "$SHRC_DIRS" "$HOME"/.csh "$HOME"/.tcsh)"
export SHELL_NAME='tcsh'
;;
zsh)
SHRC_DIRS="$(strlist "$SHRC_DIRS" "$HOME"/.zsh)"
export SHELL_NAME='Zsh'
;;
esac
if command -v brew > /dev/null 2>&1; then
GNU_CMD_DIRS="$(strlist "$PRE_SHRCS" "$(brew --prefix)"/opt/coreutils/libexec/gnubin)"
fi
GNU_CMD_DIRS="$(printf '%s:%s' "$PRE_SHRCS" /opt/local/libexec/gnubin)"
# External settings -----------------------------------------------------------
_debug=
# use GNU commands on non-GNU systems
while [ -n "$GNU_CMD_DIRS" ]; do
dir="${GNU_CMD_DIRS%%:*}"
if [ -d "$dir" ]; then
PATH="$dir:$PATH"
break
fi
if [ "$GNU_CMD_DIRS" = "$dir" ]; then
GNU_CMD_DIRS=''
else
GNU_CMD_DIRS="${GNU_CMD_DIRS#*:}"
fi
done
unset GNU_CMD_DIRS dir
#strlist_each "$PRE_SHRCS" .
while [ -n "$PRE_SHRCS" ] && [ ! "$abort" ]; do
rc="${PRE_SHRCS%%:*}"
if [ -e "$rc" ]; then
[ "$_debug" ] && echo sourcing \'"$rc"\' >&2
# NOTE:
# do not abort on nonzero exit, because these system-wide configs sometimes end in a
# test that returns nonzero
# shellcheck disable=SC1090
. "$rc"
fi
if [ "$PRE_SHRCS" = "$rc" ]; then
PRE_SHRCS=''
else
PRE_SHRCS="${PRE_SHRCS#*:}"
fi
done
unset PRE_SHRCS rc
while [ -n "$SHRC_DIRS" ] && [ ! "$abort" ]; do
dir="${SHRC_DIRS%%:*}"
if [ -d "$dir" ]; then
for rc in "$dir"/*.*sh; do
[ "$_debug" ] && echo sourcing \'"$rc"\' >&2
# shellcheck disable=SC1090
. "$rc" || abort=true
[ "$abort" ] && break
done
fi
if [ "$SHRC_DIRS" = "$dir" ]; then
SHRC_DIRS=''
else
SHRC_DIRS="${SHRC_DIRS#*:}"
fi
done
unset SHRC_DIRS dir rc
unset abort