#!/hint/bash ################################################################################ # Added shell features # ################################################################################ ## # Usage: dquote STRING # Safely double-quotes a string. # It escapes ways to execute code, but not variables. ## dquote() { local str=$1 str="${str//\\/\\\\}" # backslash str="${str//\"/\\\"}" # dquote str="${str//\$(/\\\$(}" # $(...) str="${str//\`/\\\`}" # backtick printf '"%s"\n' "$str" } ## # Usage: expand_variables # Expands variables read from /dev/stdin ## expand_variables() ( IFS='' while read -r line; do eval printf "'%s\n'" "$(dquote "$line")" done ) is_mounted() { local dir="$(readlink -m $1)" local mntpnt="$(cut -d' ' -f2 /proc/mounts|grep -Fx -- "$dir")" [[ $dir = "$mntpnt" ]] } ################################################################################ # PATH manipulation # ################################################################################ ## # Usage: path_ls PATH # List executables in PATH (PATH is delimited by `:') ## path_ls() { local dirs IFS=: dirs=($@) find -L "${dirs[@]}" -maxdepth 1 -type f -executable -printf '%f\n' 2>/dev/null | sort -u } ## # Usage: path_which PATH PROGRAM # Find the full path of PROGRAM by searching PATH ## path_which() { local mypath=$1 local prog=$2 local which=$(which which) PATH="$mypath" "$which" -- "$prog" 2>/dev/null } ################################################################################ # wmii convenience functions # ################################################################################ ## # Usage: lstags # Lists wmii tags ## lstags() { ls "$WMII_DIR/tag" | sed -e 's,/$,,' -e '/^sel$/d' } ################################################################################ # X11 functions # ################################################################################ ## # Usage: connected_to_x_server # Return status indicates whether there is an X server at $DISPLAY ## connected_to_x_server() { xdpyinfo &>/dev/null } ################################################################################ # My wmii configuration # ################################################################################ ## # Usage: scansection SECTION # Reads the doc comments from a section of wmiirc. ## scansection() { local sec=$1 local file=$(conffile config.sh) # Isolate the section we want. < "$file" sed -n "/^\s*$sec\s*()/,/^}/{ /##/p }" | # Parse each line. while read -r line; do symbol="$(sed -nr 's/^\s*(.*)\)\s*##.*/\1/p' <<<"$line")" comment="$(sed -r 's/.*## ?//' <<<"$line")" if [ -z "$symbol" ]; then printf '%s\n' "$comment" else printf '\t%s\t%s\n' "$(expand_variables <<<"$symbol")" "$comment" fi done } ## # Usage: conffile FILE ## conffile() { echo "$HOME/.wmii-hg/$1" } ################################################################################ # 9P filesystem # ################################################################################ # Prefer 9pfuse since: # 1. Linux 9p directory listings are broken on i686 # 2. 9umount doesn't work with recent versions of Linux 9p if (. /etc/profile.d/plan9.sh || true; type 9pfuse) &>/dev/null; then . /etc/profile.d/plan9.sh || true mount.9p() { 9pfuse "$1" "$2" while ! [[ -f "$2/event" ]]; do :; done } umount.9p() { fusermount -u -z "$1" } elif type 9mount &>/dev/null; then mount.9p() { 9mount -i "$1" "$2" } umount.9p() { 9umount "$1" } else >&2 echo "ERROR: I don't know how to mount 9P filesystems" exit 5 # EXIT_NOTINSTALLED (LSB) fi