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