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