2 ###############################################################################
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 $
12 # (c) Copyright 2000-2005, Andrew@DeFaria.com, all rights reserved.
14 ###############################################################################
18 export WIZARD="Wizard"
22 # Function to set the title bar. Works on the terminal emulators listed.
26 ROOT="${NORMAL}\($ROOT\)"
31 if [ "${PWD#$HOME*}" != "$PWD" ]; then
32 current_dir="~${PWD#$HOME*}"
33 elif [ "$PWD" = "$HOME" ]; then
40 current_dir="${current_dir#/view/$view_name*}"
41 current_dir="${current_dir#/sview/$view_name*}"
43 # Add CVS/Root if there is one
44 if [ -f "CVS/Root" ]; then
45 current_dir="$current_dir ($(cat CVS/Root | tr -d $CR))"
48 # Add GIT info if available
50 current_dir="$current_dir [git: $(git branch | sed -n -e 's/^\* \(.*\)/\1/p')]"
53 if [ "$TERM" = "hpterm" -o \
55 "$TERM" = "2392A" ]; then
56 string=$(echo "${SYSNAME##*:}:$@")
57 echo -n "${ESC}&f0k${#string}D$string"
58 elif [ "$TERM" = "dtterm" -o \
59 "$TERM" = "vt221" ]; then
60 string=$(echo "${SYSNAME##*:}:$@")
61 echo -n "${ESC}]2;$string\007"
62 elif [ "$TERM" = "cygwin" -o \
63 "$TERM" = "vt100" -o \
64 "$TERM" = "xterm" -o \
65 "$TERM" = "xtermc" -o \
66 "$TERM" = "xterm-256color" ]; then
67 PS1="\[\e]0;$prefix$current_dir\007\]$ROOT\[$B_YELLOW\]$SYSNAME:\[$B_WHITE\]"
71 # Function to set the icon name. Works on the terminal emulators listed.
73 if [ "$TERM" = "hpterm" -o \
75 "$TERM" = "2392A" ]; then
77 echo -n "${ESC}&f-1k${#string}D$string"
78 elif [ "$TERM" = "dtterm" -o \
79 "$TERM" = "vt100" -a "$DTTERM" = "True" ]; then
80 # Note setting icon_name on vt100 overwrites the title bar so skip it
81 echo -n "${ESC}]1;$@\007"
85 # Sets both the title bar and the icon name.
88 icon_name "${SYSNAME##*:}"
91 # Sets title bar to machine name and path. Will include a view name if in a
92 # view and a string to indicate that you are root.
94 if [ $($id -u) -eq 0 ]; then
100 view_name=$(scm pwv -short 2> /dev/null);
102 if [ $? -ne 0 -o -z "$view_name" ]; then
106 if [[ $view_name = *NONE* ]]; then
110 title_bar "${root}View: $view_name: "
113 icon_name "${SYSNAME##*:}"
116 # Sets prompt on terminals listed.
117 function set_prompt {
118 if [ $($id -u) -eq 0 ]; then
119 if [ "$TERM" = "hpterm" -o \
121 "$TERM" = "2392A" -o \
122 "$TERM" = "dtterm" -o \
123 "$TERM" = "vt100" -o \
124 "$TERM" = "cygwin" -o \
125 "$TERM" = "xterm" -o \
126 "$TERM" = "xtermc" -o \
127 "$TERM" = "xterm-256color" -o \
128 "$TERM" = "vt220" ]; then
129 ROOT="\[${ROOT_COLOR}\]$WIZARD\[$NORMAL\] "
137 if [ "$TERM" = "vt100" -o \
138 "$TERM" = "cygwin" -o \
139 "$TERM" = "xterm" -o \
140 "$TERM" = "xtermc" -o \
141 "$TERM" = "xterm-256color" -o \
142 "$TERM" = "vt220" ]; then
143 PS1="$ROOT\[$B_YELLOW\]$SYSNAME:\[$B_WHITE\]"
151 # Function to override the normal cd command, setting title and prompt.
163 # Functions to override the normal push/popd commands, setting title and prompt.
168 \pushd "$1" > /dev/null
178 \popd "$1" > /dev/null
184 # Function to override rlogin. Note that it fixes up the title and prompt
192 # Function to override rsh. Note that it fixes up the title and prompt
200 # Function to override ssh. Note that it fixes up the title and prompt
208 function processRunning {
209 if [ $ARCHITECTURE = "FreeBSD" ]; then
215 if [ $1 != '' ]; then
216 return $(ps $psopts | grep "$1" | grep -v "grep $1" | grep -v "grep -d skip" | wc -l)
221 if [ $ARCHITECTURE = "FreeBSD" ]; then
231 ps $psopts | grep "$str" | grep -ve "grep $str" -e "grep -d skip" -e "grep --color=auto"
236 function start_imap {
237 # Starts an ssh tunnel for IMAP
238 ssh -C -L 143:defaria.com:143 andrew@defaria.com
242 # Displays the command line from the /proc filesystem (if present)
246 if [ $# -ne 1 ]; then
247 error "Usage: cmdline <pid>"
253 if [ ! -d "/proc" ]; then
254 error "This OS has no /proc filesystem"
258 if [ ! -d "/proc/$pid" ]; then
259 error "PID $pid does not exist"
263 if [ ! -f "/proc/$pid/cmdline" ]; then
264 error "PID $pid has no cmdline!"
268 cat /proc/$pid/cmdline | tr -c [:print:] " "
273 processRunning ypbind; ypbind=$?
274 processRunning winbind; winbind=$?
276 if [ $# -gt 0 ]; then
277 if [ $ypbind -gt 0 ]; then
278 ypcat passwd | grep -i $@
279 elif [ $winbind -gt 0 ]; then
280 for user in $(wbinfo -u | grep -i $@); do
281 wbinfo --user-info $user
285 if [ $ypbind -gt 0 ]; then
286 ypcat passwd | $PAGER
287 elif [ $wbind -gt 0 ]; then
288 for user in $(wbinfo -u); do
289 wbinfo --user-info $user
296 processRunning ypbind; ypbind=$?
297 processRunning winbind; winbind=$?
299 if [ $# -gt 0 ]; then
300 if [ $ypbind -gt 0 ]; then
301 ypcat group | grep -i $@
302 elif [ $winbind -gt 0 ]; then
303 for group in $(wbinfo -g | grep -i $@); do
304 wbinfo --group-info $group
308 if [ $ypbind -gt 0 ]; then
310 elif [ $winbind -gt 0 ]; then
311 for group in $(wbinfo -g); do
312 wbinfo --group-info $group