Commit | Line | Data |
---|---|---|
74a53e28 MW |
1 | ### -*-sh-*- |
2 | ### | |
3 | ### Common per-shell configuration. | |
4 | ||
5 | ###-------------------------------------------------------------------------- | |
6 | ### Utilities. | |
7 | ||
8 | __mdw_programp () { type >/dev/null 2>&1 "$1"; } | |
9 | ||
10 | __mdw_source_if_exists () { | |
11 | local i | |
12 | for i in "$@"; do | |
13 | if [ -r "$i" ]; then . "$i"; fi | |
14 | done | |
15 | } | |
16 | ||
17 | ###-------------------------------------------------------------------------- | |
18 | ### Prompt machinery. | |
19 | ||
20 | __mdw_set_prompt_hacks () { host=$(hostname); dir=""; } | |
21 | ||
22 | __mdw_set_prompt_pieces () { | |
23 | local hqual | |
24 | hqual="" | |
25 | ||
26 | ## Fancy highlighting in some terminals. | |
27 | local bold unbold nl gitcolour rccolour uncolour | |
28 | local host dir | |
29 | bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour="" | |
30 | __mdw_set_prompt_hacks | |
31 | ||
32 | ## Choose the right delimiters. Highlight root prompts specially; | |
33 | ## highlight when I'm running as some other user. Highlight when this | |
34 | ## isn't the outermost shell on the terminal. | |
35 | local left right user u tty | |
2ab43296 | 36 | user=${USER-${LOGNAME-$(id -un)}} |
74a53e28 MW |
37 | case $(id -u) in |
38 | 0) | |
39 | left=$(echo « | iconv -f UTF-8 -t //translit) | |
40 | right=$(echo » | iconv -f UTF-8 -t //translit) | |
41 | ;; | |
42 | *) | |
43 | case $user in | |
44 | mdw | mwooding | nemo) u="" left="[" right="]" ;; | |
45 | *) u="$user@" left="{" right="}" ;; | |
46 | esac | |
47 | tty=$(tty) | |
48 | case "$__mdw_tty" in | |
49 | "$tty") left="<" right=">" ;; | |
50 | *) __mdw_tty=$tty; export __mdw_tty ;; | |
51 | esac | |
52 | ;; | |
53 | esac | |
54 | ||
55 | ## If this session is insecure then highlight that. | |
56 | local sec_l sec_r h | |
57 | h=$(hostname) | |
58 | case ${SSH_CLIENT-nil},${SCHROOT_CHROOT_NAME-nil},$__mdw_sechost in | |
59 | nil,nil,"$h") sec_l="" sec_r="" ;; | |
60 | nil,nil,*) sec_l="(" sec_r=")" ;; | |
61 | *) sec_l="" sec_r="" | |
62 | esac | |
63 | ||
64 | ## If this is an schroot environment then point this out. | |
65 | hqual="$hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}" | |
66 | ||
67 | ## Put together the main pieces. | |
68 | __mdw_prompt_left="$nl$bold$left$sec_l$u$host$hqual$sec_r$dir" | |
69 | __mdw_prompt_git_left="$unbold$gitcolour" | |
70 | __mdw_prompt_git_right="$uncolour$bold" | |
71 | __mdw_prompt_rc_left="$unbold$rccolour" | |
72 | __mdw_prompt_rc_right="$uncolour$bold" | |
73 | __mdw_prompt_right="$right$unbold" | |
74 | } | |
75 | ||
76 | __mdw_set_prompt () { | |
77 | __mdw_last_rc=$? | |
78 | local git rc | |
79 | if type __git_ps1 >/dev/null 2>&1; then | |
80 | git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right" | |
81 | else | |
82 | git="" | |
83 | fi | |
84 | case $__mdw_last_rc in | |
85 | 0) rc="" ;; | |
86 | *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;; | |
87 | esac | |
88 | PS1="$__mdw_prompt_left$git$rc$__mdw_prompt_right" | |
89 | PS2="$PS1 $bold>$unbold " | |
90 | unset __mdw_last_rc | |
91 | } | |
92 | ||
93 | __mdw_precmd () { | |
94 | __mdw_set_prompt | |
95 | case ${STY+t} in | |
96 | t) printf "\ek%s\e\\" "$__mdw_shell" ;; | |
97 | esac | |
98 | } | |
99 | ||
100 | __mdw_preexec () { | |
101 | case ${STY+t} in | |
102 | t) printf "\ek%s\e\\" "$1" ;; | |
103 | esac | |
104 | } | |
105 | ||
106 | ###-------------------------------------------------------------------------- | |
107 | ### Some handy aliases. | |
108 | ||
109 | alias cx='chmod +x' | |
110 | alias which="command -v" | |
111 | alias rc="rc -l" | |
112 | rootly () { | |
113 | case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac | |
114 | $__MDW_ROOTLY "$@" | |
115 | } | |
116 | alias r=rootly | |
117 | alias re="rootly $EDITOR" | |
118 | alias pstree="pstree -hl" | |
119 | alias cdtmp='cd ${TMPDIR-/tmp}' | |
120 | alias pushtmp='pushd ${TMPDIR-/tmp}' | |
121 | alias e="$EDITOR" | |
122 | alias svn="svnwrap svn" | |
123 | alias @="ssh" | |
124 | ||
125 | ###-------------------------------------------------------------------------- | |
126 | ### Colour output. | |
127 | ||
128 | ## Arrange for `ls' output to be in colour. | |
129 | if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors") | |
130 | else unset LS_COLORS; fi | |
131 | ||
132 | unalias ls 2>/dev/null || : | |
133 | ls () { | |
134 | if [ -t 1 ]; then command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@" | |
135 | else command ls "$@"; fi | |
136 | } | |
137 | ||
138 | ## Arrange for `grep' output to be in colour. | |
139 | export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34" | |
140 | ||
141 | greplike () { | |
142 | local grep=$1; shift | |
143 | if [ -t 1 ]; then | |
144 | command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager | |
145 | else | |
146 | command $grep "$@" | |
147 | fi | |
148 | } | |
149 | alias grep="greplike grep" | |
150 | alias egrep="greplike egrep" | |
151 | alias fgrep="greplike fgrep" | |
152 | alias zgrep="greplike zgrep" | |
153 | ||
154 | ###-------------------------------------------------------------------------- | |
155 | ### Other hacks. | |
156 | ||
157 | ## Turn off pagers inside Emacs shell buffers. | |
158 | case "$INSIDE_EMACS" in | |
159 | 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;; | |
160 | esac | |
161 | ||
162 | ###-------------------------------------------------------------------------- | |
163 | ### More complicated shell functions. | |
164 | ||
165 | ## xt [@HOST] XTERM-ARGS | |
166 | ## | |
167 | ## Open a terminal, maybe on a remote host. | |
168 | xt () { | |
169 | case "$1" in | |
170 | @*) | |
171 | local remote=${1#@} title | |
172 | shift | |
173 | if [ $# -gt 0 ]; then | |
174 | title="xterm [$remote] $1" | |
175 | else | |
176 | title="xterm [$remote]" | |
177 | fi | |
178 | (xterm -title "$title" -e ssh $remote "$@" &) | |
179 | ;; | |
180 | *) | |
181 | (xterm "$@" &) | |
182 | ;; | |
183 | esac | |
184 | } | |
185 | ||
186 | ## core [y|n] | |
187 | ## | |
188 | ## Tweak core dumps on and off, or show the current status. | |
189 | core () { | |
190 | case "x$1" in | |
191 | xon|xy|xyes) ulimit -Sc $(ulimit -Hc) ;; | |
192 | xoff|xn|xno) ulimit -Sc 0 ;; | |
193 | x) | |
194 | local l=$(ulimit -Sc) | |
195 | case $l in | |
196 | 0) echo "Core dumps disabled" ;; | |
197 | unlimited) echo "Core dumps enabled" ;; | |
198 | *) echo "Core dump limit is $l blocks" ;; | |
199 | esac | |
200 | ;; | |
201 | *) | |
202 | echo >&2 "usage: core [y|n]" | |
203 | return 1 | |
204 | ;; | |
205 | esac | |
206 | } | |
207 | ||
208 | ## world [NAME] | |
209 | ## | |
210 | ## Set current security world to NAME. With no NAME, print the currently | |
211 | ## selected world. | |
212 | world () { | |
213 | local nfast=${NFAST_HOME-/opt/nfast} | |
214 | local kmdata | |
215 | case "$#" in | |
216 | 0) | |
217 | echo "${NFAST_KMDATA#$nfast/kmdata-}" | |
218 | ;; | |
219 | *) | |
220 | if [ -d "$1" ]; then | |
221 | kmdata=$1 | |
222 | elif [ -d "$nfast/kmdata-$1" ]; then | |
223 | kmdata=$nfast/kmdata-$1 | |
224 | else | |
225 | echo >&2 "world: can't find world $1" | |
226 | return 1 | |
227 | fi | |
228 | shift | |
229 | case "$#" in | |
230 | 0) export NFAST_KMDATA=$kmdata ;; | |
231 | *) "$@" ;; | |
232 | esac | |
233 | ;; | |
234 | esac | |
235 | } | |
236 | ||
237 | ## path-add [VAR] DIR | |
238 | ## | |
239 | ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if | |
240 | ## it's not there already. | |
241 | path_add () { | |
242 | local pathvar export dir val | |
243 | case $# in | |
244 | 1) pathvar=PATH dir=$1 export="export PATH" ;; | |
245 | 2) pathvar=$1 dir=$2 export=: ;; | |
246 | *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;; | |
247 | esac | |
248 | eval val=\$$pathvar | |
249 | case ":$val:" in | |
250 | *:"$dir":*) ;; | |
251 | *) val=$dir:$val ;; | |
252 | esac | |
253 | eval $pathvar=\$val | |
254 | eval $export | |
255 | } | |
256 | ||
257 | ## path-remove [VAR] DIR | |
258 | ## | |
259 | ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an | |
260 | ## error if DIR isn't in VAR. | |
261 | path_remove () { | |
262 | local pathvar export dir val | |
263 | case $# in | |
264 | 1) pathvar=PATH dir=$1 export="export PATH" ;; | |
265 | 2) pathvar=$1 dir=$2 export=: ;; | |
266 | *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;; | |
267 | esac | |
268 | eval val=\$$pathvar | |
269 | case ":$val:" in | |
270 | :"$dir":) val= ;; | |
271 | :"$dir":*) val=${val#$dir:} ;; | |
272 | *:"$dir":) val=${val%:$dir} ;; | |
273 | *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;; | |
274 | esac | |
275 | eval $pathvar=\$val | |
276 | eval $export | |
277 | } | |
278 | ||
279 | ## pathhack [-f] +HACK|-HACK... | |
280 | ## | |
281 | ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded | |
282 | ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack | |
283 | ## that's already on the PATH doesn't do anything unless `-f' is set, in | |
284 | ## which case it gets moved to the beginning. With no arguments, print the | |
285 | ## currently installed hacks. | |
286 | pathhack () { | |
287 | local p e force arg hack dir | |
288 | p=$PATH | |
289 | if [ $# -eq 0 ]; then | |
290 | while :; do | |
291 | e=${p%%:*} | |
292 | case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac | |
293 | case "$p" in *:*) p=${p#*:} ;; *) break ;; esac | |
294 | done | |
295 | return | |
296 | fi | |
297 | force=nil | |
298 | while [ $# -gt 0 ]; do | |
299 | arg=$1 | |
300 | case "$arg" in | |
301 | -f | --force) force=t; shift; continue ;; | |
302 | --) shift; break ;; | |
303 | [-+]*) ;; | |
304 | *) break; ;; | |
305 | esac | |
306 | hack=${arg#[+-]} | |
307 | dir=$HOME/bin/hacks/$hack | |
308 | if ! [ -d "$dir" ]; then | |
309 | echo "$0: path hack $hack not found" | |
310 | return 1 | |
311 | fi | |
312 | case "$arg,$force,:$PATH:" in | |
313 | -*,*,*:"$dir":*) path_remove p "$dir" ;; | |
314 | +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;; | |
315 | +*,nil,*:"$dir":*) ;; | |
316 | +*,*) path_add p "$dir" ;; | |
317 | esac | |
318 | shift | |
319 | done | |
320 | if [ $# -eq 0 ]; then PATH=$p; export PATH | |
321 | else PATH=$p "$@"; fi | |
322 | } | |
323 | ||
324 | ###-------------------------------------------------------------------------- | |
325 | ### Finishing touches. | |
326 | ||
4cb8b8da MW |
327 | ## Make sure `$HOME/bin' is on the path. |
328 | path_add "$HOME/bin" | |
329 | ||
6054bdb6 MW |
330 | ## Set the temporary directory again. (A setuid or setgid program may have |
331 | ## unhelpfully forgotten this for us.) | |
332 | case ${TMPDIR+t} in t) ;; *) eval $(tmpdir -b); esac | |
333 | ||
74a53e28 MW |
334 | ## For `root' use -- some simple molly-guards. |
335 | case $(id -u) in | |
336 | 0) | |
337 | alias rm="rm -i" cp="cp -i" mv="mv -i" | |
338 | set -o noclobber | |
339 | ;; | |
340 | esac | |
341 | ||
342 | ## Run any local hooks. | |
343 | __mdw_source_if_exists "$HOME/.shell-local" | |
344 | ||
345 | ###----- That's all, folks -------------------------------------------------- |