Added code to allow one to use my startup scripts by doing source
[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 view_name=
19
20 # Function to set the title bar. Works on the terminal emulators listed.
21 function title_bar {
22   if [ $# -gt 1 ]; then
23     ROOT=shift
24     ROOT="${NORMAL}\($ROOT\)"
25   fi
26
27   prefix="$@"
28
29   # Change $HOME -> ~
30   if [ "${PWD#$HOME*}" != "$PWD" ]; then
31     current_dir="~${PWD#$HOME*}"
32   elif [ "$PWD" = "$HOME" ]; then
33     current_dir=~
34   else
35     current_dir="$PWD"
36   fi
37
38   # Remove view name
39   current_dir="${current_dir#/view/$view_name*}"
40   current_dir="${current_dir#/sview/$view_name*}"
41
42   # Add CVS/Root if there is one
43   if [ -f "CVS/Root" ]; then
44     current_dir="$current_dir ($(cat CVS/Root | tr -d $CR))"
45   fi
46
47   # Add GIT info if available
48   if in_git_repo; then
49     current_dir="$current_dir [git: $(git branch | sed -n -e 's/^\* \(.*\)/\1/p')]"
50   fi
51
52   if [ "$TERM" = "hpterm" -o \
53        "$TERM" = "hp"     -o \
54        "$TERM" = "2392A" ]; then
55     string=$(echo "${SYSNAME##*:}:$@")
56     echo -n "${ESC}&f0k${#string}D$string"
57   elif [ "$TERM" = "dtterm" -o \
58          "$TERM" = "vt221" ]; then
59     string=$(echo "${SYSNAME##*:}:$@")
60     echo -n "${ESC}]2;$string\007"
61   elif [ "$TERM" = "cygwin" -o "$TERM" = "vt100" -o "$TERM" = "xterm" ]; then
62     PS1="\[\e]0;$prefix$current_dir\a$ROOT\e[01;33m\]$SYSNAME:\[\e[0m\]"
63   fi
64 } # title_bar
65
66 # Function to set the icon name. Works on the terminal emulators listed.
67 function icon_name {
68   if [ "$TERM" = "hpterm" -o \
69        "$TERM" = "hp"     -o \
70        "$TERM" = "2392A" ]; then
71     string=$(echo "$1")
72     echo -n "${ESC}&f-1k${#string}D$string"
73   elif [ "$TERM" = "dtterm" -o \
74          "$TERM" = "vt100"  -a "$DTTERM" = "True" ]; then
75     # Note setting icon_name on vt100 overwrites the title bar so skip it
76     echo -n "${ESC}]1;$@\007"
77   fi
78 } # icon_name
79
80 # Sets both the title bar and the icon name. 
81 function title {
82   title_bar "$@"
83   icon_name "${SYSNAME##*:}"
84 } # title
85
86 # Sets title bar to machine name and path. Will include a view name if in a 
87 # view and a string to indicate that you are root.
88 function set_title {
89   if [ $($id -u) -eq 0 ]; then
90     ROOT="Wizard "
91   else
92     ROOT=
93   fi
94
95   view_name=$(scm pwv -short 2> /dev/null);
96
97   if [ $? -ne 0 -o -z "$view_name" ]; then
98     view_name='*NONE*'
99   fi
100
101   if [[ $view_name = *NONE* ]]; then
102     view_name=""
103     title_bar "$ROOT"
104   else
105     title_bar "$ROOT" "View: $view_name: "
106   fi
107
108   icon_name "${SYSNAME##*:}"
109 } # set_title
110
111 # Sets prompt on terminals listed.
112 function set_prompt {
113   if [ $($id -u) -eq 0 ]; then
114     if [ "$TERM"   = "hpterm" -o \
115          "$TERM"   = "hp"     -o \
116          "$TERM"   = "2392A"  -o \
117          "$TERM"   = "dtterm" -o \
118          ! -z "$DTTERM" ]; then
119       ROOT="${RED}Wizard$NORMAL "
120     elif [ "$TERM" = "vt100" -o \
121            "$TERM" = "xterm" -o \
122            "$TERM" = "vt220" ]; then
123       ROOT="${BOLD}${BLINK}Wizard$NORMAL "
124     fi
125   else
126     ROOT=""
127   fi
128
129   if [ "$TERM" = "vt100" -o \
130        "$TERM" = "xterm" -o \
131        "$TERM" = "vt220" ]; then
132     PS1="$ROOT$BOLD$SYSNAME:$NORMAL"
133   else
134     PS1="$ROOT$SYSNAME:"
135   fi
136   
137   set_title
138 } # set_prompt
139
140 # Function to override the normal cd command, setting title and prompt.
141 function mycd {
142   if [ -z "$1" ]; then
143     \cd ~
144   else
145     \cd "$1"
146   fi
147   set_title
148   set_prompt
149 } # mycd
150 export mycd
151
152 # Functions to override the normal push/popd commands, setting title and prompt.
153 function mypushd {
154   if [ -z "$1" ]; then
155     \pushd > /dev/null
156   else
157     \pushd "$1" > /dev/null
158   fi
159   set_title
160   set_prompt
161 } # mypushd
162
163 function mypopd {
164   if [ -z "$1" ]; then
165     cd - > /dev/null
166   else
167     \popd "$1" > /dev/null
168   fi
169   set_title
170   set_prompt
171 } # mypopd
172
173 # Function to override rlogin. Note that it fixes up the title and prompt 
174 # upon return.
175 function rlogin {
176   /usr/bin/rlogin "$@"
177   set_title
178   set_prompt
179 } # rlogin
180
181 # Function to override rsh. Note that it fixes up the title and prompt 
182 # upon return.
183 function rsh {
184   /usr/bin/rsh "$@"
185   set_title
186   set_prompt
187 } # rsh
188
189 # Function to override ssh. Note that it fixes up the title and prompt 
190 # upon return.
191 function ssh {
192   /usr/bin/ssh "$@"
193   set_title
194   set_prompt
195 } # ssh
196
197 function sj {
198   if [ $ARCHITECTURE = "FreeBSD" ]; then
199     psopts="-aux"
200   else
201     psopts="-ef"
202   fi
203
204   if [ $# = 0 ]; then
205     ps $psopts | $PAGER
206   else
207     for str; do
208       ps $psopts | grep "$str" | grep -v "grep $str" | grep -v "grep -d skip"
209     done
210   fi
211 } # sj
212
213 function start_imap {
214   # Starts an ssh tunnel for IMAP
215   ssh -C -L 143:defaria.com:143 andrew@defaria.com
216 } # start_imap
217
218 function cmdline {
219   # Displays the command line from the /proc filesystem (if present)
220
221   me=$0;
222
223   if [ $# -ne 1 ]; then
224     error "Usage: cmdline <pid>"
225     return 1
226   fi
227
228   pid=$1;
229
230   if [ ! -d "/proc" ]; then
231     error "This OS has no /proc filesystem"
232     return 1
233   fi
234
235   if [ ! -d "/proc/$pid" ]; then
236     error "PID $pid does not exist"
237     return 1
238   fi
239
240   if [ ! -f "/proc/$pid/cmdline" ]; then
241     error "PID $pid has no cmdline!"
242     return 1
243   fi
244
245   cat /proc/$pid/cmdline | tr -c [:print:] " "
246   display
247 } # cmdline
248
249 function user {
250   if [ $# -gt 0 ]; then
251     ypcat passwd | grep -i $@
252   else
253     ypcat passwd | $PAGER
254   fi
255 } # user
256
257 function group {
258   if [ $# -gt 0 ]; then
259     ypcat group | grep -i $@
260   else
261     ypcat group | $PAGER
262   fi
263 } # group