Removed /usr/local from CDPATH
[clearscm.git] / rc / functions
1 #!/bin/bash
2 ###############################################################################
3 #
4 # File:         $RCSfile: functions,v $
5 # Revision:     $Revision: 1.20 $
6 # Description:  Common bash functions
7 # Author:       Andrew@DeFaria.com
8 # Created:      Thu Jun  6 08:31:57 PDT 1996
9 # Modified:     $Date: 2013/03/26 20:38:23 $
10 # Language:     bash
11 #
12 # (c) Copyright 2000-2005, Andrew@DeFaria.com, all rights reserved.
13 #
14 ############################################################################### 
15 ESC=$(echo "\033")
16 CR=$(echo "\015")
17
18 export WIZARD="Wizard"
19
20 view_name=
21
22 # Function to set the title bar. Works on the terminal emulators listed.
23 function title_bar {
24   if [ $# -gt 1 ]; then
25     ROOT=shift
26     ROOT="${NORMAL}\($ROOT\)"
27   fi
28   
29   prefix="$@"
30   # Change $HOME -> ~
31   if [ "${PWD#$HOME*}" != "$PWD" ]; then
32     current_dir="~${PWD#$HOME*}"
33   elif [ "$PWD" = "$HOME" ]; then
34     current_dir=~
35   else
36     current_dir="$PWD"
37   fi
38
39   # Remove view name
40   current_dir="${current_dir#/view/$view_name*}"
41   current_dir="${current_dir#/sview/$view_name*}"
42
43   # Add CVS/Root if there is one
44   if [ -f "CVS/Root" ]; then
45     current_dir="$current_dir ($(cat CVS/Root | tr -d $CR))"
46   fi
47
48   # Add GIT info if available
49   if in_git_repo; then
50     current_dir="$current_dir [git: $(git branch | sed -n -e 's/^\* \(.*\)/\1/p')]"
51   fi
52
53   if [ "$TERM" = "hpterm" -o \
54        "$TERM" = "hp"     -o \
55        "$TERM" = "2392A" ]; then
56     string=$(echo "${SYSNAME##*:}:$@")
57     echo -n "${ESC}&f0k${#string}D$string"
58   elif [ "$TERM" = "dtterm" -o \
59          "$TERM" = "vt221" ]; then
60     string=$(echo "${SYSNAME##*:}:$@")
61     echo -n "${ESC}]2;$string\007"
62   elif [ "$TERM" = "cygwin" -o \
63          "$TERM" = "vt100"  -o \
64          "$TERM" = "xterm"  -o \
65          "$TERM" = "xtermc" -o \
66          "$TERM" = "xterm-256color" ]; then
67     PS1="\[\e]0;$prefix$current_dir\007\]$ROOT\[$B_YELLOW\]$SYSNAME:\[$B_WHITE\]"
68   fi
69 } # title_bar
70
71 # Function to set the icon name. Works on the terminal emulators listed.
72 function icon_name {
73   if [ "$TERM" = "hpterm" -o \
74        "$TERM" = "hp"     -o \
75        "$TERM" = "2392A" ]; then
76     string=$(echo "$1")
77     echo -n "${ESC}&f-1k${#string}D$string"
78   elif [ "$TERM" = "dtterm" -o \
79          "$TERM" = "vt100"  -a "$DTTERM" = "True" ]; then
80     # Note setting icon_name on vt100 overwrites the title bar so skip it
81     echo -n "${ESC}]1;$@\007"
82   fi
83 } # icon_name
84
85 # Sets both the title bar and the icon name. 
86 function title {
87   title_bar "$@"
88   icon_name "${SYSNAME##*:}"
89 } # title
90
91 # Sets title bar to machine name and path. Will include a view name if in a 
92 # view and a string to indicate that you are root.
93 function set_title {
94   if [ $($id -u) -eq 0 ]; then
95     root="$WIZARD "
96   else
97     root=
98   fi
99
100   view_name=$(scm pwv -short 2> /dev/null);
101
102   if [ $? -ne 0 -o -z "$view_name" ]; then
103     view_name='*NONE*'
104   fi
105
106   if [[ $view_name = *NONE* ]]; then
107     view_name=""
108     title_bar "$root"
109   else
110     title_bar "${root}View: $view_name: "
111   fi
112
113   icon_name "${SYSNAME##*:}"
114 } # set_title
115
116 # Sets prompt on terminals listed.
117 function set_prompt {
118   if [ $($id -u) -eq 0 ]; then
119     if [ "$TERM" = "hpterm"         -o \
120          "$TERM" = "hp"             -o \
121          "$TERM" = "2392A"          -o \
122          "$TERM" = "dtterm"         -o \
123          "$TERM" = "vt100"          -o \
124          "$TERM" = "cygwin"         -o \
125          "$TERM" = "xterm"          -o \
126          "$TERM" = "xtermc"         -o \
127          "$TERM" = "xterm-256color" -o \
128          "$TERM" = "vt220" ]; then
129       ROOT="\[${ROOT_COLOR}\]$WIZARD\[$NORMAL\] "
130     else
131       ROOT="$WIZARD "
132     fi
133   else
134     ROOT=""
135   fi
136
137   if [ "$TERM" = "vt100"          -o \
138        "$TERM" = "cygwin"         -o \
139        "$TERM" = "xterm"          -o \
140        "$TERM" = "xtermc"         -o \
141        "$TERM" = "xterm-256color" -o \
142        "$TERM" = "vt220" ]; then
143     PS1="$ROOT\[$B_YELLOW\]$SYSNAME:\[$B_WHITE\]"
144   else
145     PS1="$ROOT$SYSNAME:"
146   fi
147   
148   set_title
149 } # set_prompt
150
151 # Function to override the normal cd command, setting title and prompt.
152 function mycd {
153   if [ -z "$1" ]; then
154     \cd ~
155   else
156     \cd "$1"
157   fi
158   set_title
159   set_prompt
160 } # mycd
161 export mycd
162
163 # Functions to override the normal push/popd commands, setting title and prompt.
164 function mypushd {
165   if [ -z "$1" ]; then
166     \pushd > /dev/null
167   else
168     \pushd "$1" > /dev/null
169   fi
170   set_title
171   set_prompt
172 } # mypushd
173
174 function mypopd {
175   if [ -z "$1" ]; then
176     cd - > /dev/null
177   else
178     \popd "$1" > /dev/null
179   fi
180   set_title
181   set_prompt
182 } # mypopd
183
184 # Function to override rlogin. Note that it fixes up the title and prompt 
185 # upon return.
186 function rlogin {
187   /usr/bin/rlogin "$@"
188   set_title
189   set_prompt
190 } # rlogin
191
192 # Function to override rsh. Note that it fixes up the title and prompt 
193 # upon return.
194 function rsh {
195   /usr/bin/rsh "$@"
196   set_title
197   set_prompt
198 } # rsh
199
200 # Function to override ssh. Note that it fixes up the title and prompt 
201 # upon return.
202 function ssh {
203   /usr/bin/ssh "$@"
204   set_title
205   set_prompt
206 } # ssh
207
208 function processRunning {
209   if [ $ARCHITECTURE = "FreeBSD" ]; then
210     psopts="-aux"
211   else
212     psopts="-ef"
213   fi
214
215   if [ $1 != '' ]; then
216     return $(ps $psopts | grep "$1" | grep -v "grep $1" | grep -v "grep -d skip" | wc -l)
217   fi
218 } # processRunning
219
220 function sj {
221   if [ $ARCHITECTURE = "FreeBSD" ]; then
222     psopts="-aux"
223   else
224     psopts="-ef"
225   fi
226
227   if [ $# = 0 ]; then
228     ps $psopts | $PAGER
229   else
230     for str; do
231       ps $psopts | grep "$str" | grep -ve "grep $str" -e "grep -d skip" -e "grep --color=auto"
232     done
233   fi
234 } # sj
235
236 function start_imap {
237   # Starts an ssh tunnel for IMAP
238   ssh -C -L 143:defaria.com:143 andrew@defaria.com
239 } # start_imap
240
241 function cmdline {
242   # Displays the command line from the /proc filesystem (if present)
243
244   me=$0;
245
246   if [ $# -ne 1 ]; then
247     error "Usage: cmdline <pid>"
248     return 1
249   fi
250
251   pid=$1;
252
253   if [ ! -d "/proc" ]; then
254     error "This OS has no /proc filesystem"
255     return 1
256   fi
257
258   if [ ! -d "/proc/$pid" ]; then
259     error "PID $pid does not exist"
260     return 1
261   fi
262
263   if [ ! -f "/proc/$pid/cmdline" ]; then
264     error "PID $pid has no cmdline!"
265     return 1
266   fi
267
268   cat /proc/$pid/cmdline | tr -c [:print:] " "
269   display
270 } # cmdline
271
272 function user {
273   processRunning ypbind;  ypbind=$?
274   processRunning winbind; winbind=$?
275
276   if [ $# -gt 0 ]; then
277     if [ $ypbind -gt 0 ]; then
278       ypcat passwd | grep -i $@
279     elif [ $winbind -gt 0 ]; then
280       for user in $(wbinfo -u | grep -i $@); do
281         wbinfo --user-info $user
282       done
283     fi
284   else
285     if [ $ypbind -gt 0 ]; then
286       ypcat passwd | $PAGER
287     elif [ $wbind -gt 0 ]; then
288       for user in $(wbinfo -u); do
289         wbinfo --user-info $user
290       done | $PAGER
291     fi
292   fi
293 } # user
294
295 function group {
296   processRunning ypbind;  ypbind=$?
297   processRunning winbind; winbind=$?
298
299   if [ $# -gt 0 ]; then
300     if [ $ypbind -gt 0 ]; then
301       ypcat group | grep -i $@
302     elif [ $winbind -gt 0 ]; then
303       for group in $(wbinfo -g | grep -i $@); do
304         wbinfo --group-info $group
305       done
306     fi
307   else
308     if [ $ypbind -gt 0 ]; then
309       ypcat group | $PAGER
310     elif [ $winbind -gt 0 ]; then
311       for group in $(wbinfo -g); do
312         wbinfo --group-info $group
313       done | $PAGER
314     fi
315   fi
316 } # group