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