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