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