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 ###############################################################################
20 function in_git_repo {
23 while [ "$pwd" != "/" ]; do
24 if [ -d "$pwd/.git" ]; then
35 # Function to set the title bar. Works on the terminal emulators listed.
39 if [ "${PWD#$HOME*}" != "$PWD" ]; then
40 current_dir="~${PWD#$HOME*}"
41 elif [ "$PWD" = "$HOME" ]; then
48 current_dir="${current_dir#/view/$view_name*}"
49 current_dir="${current_dir#/sview/$view_name*}"
51 # Add CVS/Root if there is one
52 if [ -f "CVS/Root" ]; then
53 current_dir="$current_dir ($(cat CVS/Root | tr -d $CR))"
56 # Add GIT info if available
58 current_dir="git: $(git branch | sed -n -e 's/^\* \(.*\)/\1/p') $current_dir"
61 if [ "$TERM" = "hpterm" -o \
63 "$TERM" = "2392A" ]; then
64 string=$(echo "${SYSNAME##*:}:$@")
65 echo -n "${ESC}&f0k${#string}D$string"
66 elif [ "$TERM" = "dtterm" -o \
67 "$TERM" = "vt221" ]; then
68 string=$(echo "${SYSNAME##*:}:$@")
69 echo -n "${ESC}]2;$string\007"
70 elif [ "$TERM" = "cygwin" -o "$TERM" = "vt100" -o "$TERM" = "xterm" ]; then
71 PS1="\[\e]0;$prefix$current_dir\a\e[01;33m\]$SYSNAME:\[\e[0m\]"
75 # Function to set the icon name. Works on the terminal emulators listed.
77 if [ "$TERM" = "hpterm" -o \
79 "$TERM" = "2392A" ]; then
81 echo -n "${ESC}&f-1k${#string}D$string"
82 elif [ "$TERM" = "dtterm" -o \
83 "$TERM" = "vt100" -a "$DTTERM" = "True" ]; then
84 # Note setting icon_name on vt100 overwrites the title bar so skip it
85 echo -n "${ESC}]1;$@\007"
89 # Sets both the title bar and the icon name.
92 icon_name "${SYSNAME##*:}"
95 # Sets title bar to machine name and path. Will include a view name if in a
96 # view and a string to indicate that you are root.
98 if [ $($id -u) -eq 0 ]; then
104 view_name=$(scm pwv -short 2> /dev/null);
106 if [ $? -ne 0 -o -z "$view_name" ]; then
110 if [[ $view_name = *NONE* ]]; then
114 title_bar "${ROOT}View: $view_name: "
117 icon_name "${SYSNAME##*:}"
120 # Sets prompt on terminals listed.
121 function set_prompt {
122 if [ $($id -u) -eq 0 ]; then
123 if [ "$TERM" = "hpterm" -o \
125 "$TERM" = "2392A" -o \
126 "$TERM" = "dtterm" -o \
127 ! -z "$DTTERM" ]; then
128 ROOT="${RED}Wizard$NORMAL "
129 elif [ "$TERM" = "vt100" -o \
130 "$TERM" = "xterm" -o \
131 "$TERM" = "vt220" ]; then
132 ROOT="${BOLD}${BLINK}Wizard$NORMAL "
138 if [ "$TERM" = "vt100" -o \
139 "$TERM" = "xterm" -o \
140 "$TERM" = "vt220" ]; then
141 PS1="$ROOT$BOLD$SYSNAME:$NORMAL"
149 # Function to override the normal cd command, setting title and prompt.
161 # Functions to override the normal push/popd commands, setting title and prompt.
166 \pushd "$1" > /dev/null
176 \popd "$1" > /dev/null
182 # Function to override rlogin. Note that it fixes up the title and prompt
190 # Function to override rsh. Note that it fixes up the title and prompt
198 # Function to override ssh. Note that it fixes up the title and prompt
207 if [ $ARCHITECTURE = "FreeBSD" ]; then
217 ps $psopts | grep "$str" | grep -v "grep $str" | grep -v "grep -d skip"
222 function start_imap {
223 # Starts an ssh tunnel for IMAP
224 ssh -C -L 143:defaria.com:143 andrew@defaria.com
228 # Displays the command line from the /proc filesystem (if present)
232 if [ $# -ne 1 ]; then
233 error "Usage: cmdline <pid>"
239 if [ ! -d "/proc" ]; then
240 error "This OS has no /proc filesystem"
244 if [ ! -d "/proc/$pid" ]; then
245 error "PID $pid does not exist"
249 if [ ! -f "/proc/$pid/cmdline" ]; then
250 error "PID $pid has no cmdline!"
254 cat /proc/$pid/cmdline | tr -c [:print:] " "
259 if [ $# -gt 0 ]; then
260 ypcat passwd | grep -i $@
262 ypcat passwd | $PAGER
267 if [ $# -gt 0 ]; then
268 ypcat group | grep -i $@