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