103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# $Id$
2b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes# $MirOS: src/bin/mksh/dot.mkshrc,v 1.89 2014/07/28 21:45:44 tg Exp $
303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#-
4c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser# Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010,
5427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes#		2011, 2012, 2013, 2014
603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#	Thorsten Glaser <tg@mirbsd.org>
703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#
803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# Provided that these terms and disclaimer and all copyright notices
903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# are retained or reproduced in an accompanying document, permission
1003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# is granted to deal in this work without restriction, including un-
1103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# limited rights to use, publicly perform, distribute, sell, modify,
1203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# merge, give away, or sublicence.
1303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#
1403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
1503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# the utmost extent permitted by applicable law, neither express nor
1603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# implied; without malicious intent or gross negligence. In no event
1703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# may a licensor, author or contributor be held liable for indirect,
1803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# direct, other damage, loss, or other issues arising in any way out
1903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# of dealing in the work, even if advised of the possibility of such
2003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# damage or existence of a defect, except proven that it results out
2103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# of said person's immediate fault when using the work as intended.
2203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#-
2303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# ${ENV:-~/.mkshrc}: mksh initialisation file for interactive shells
2403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
25811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser# catch non-mksh (including lksh) trying to shell this file
26811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glasercase $KSH_VERSION in
27811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser*MIRBSD\ KSH*) ;;
28811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser*) return 0 ;;
29811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaseresac
30811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser
31c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten GlaserPS1='#'; (( USER_ID )) && PS1='$'; [[ ${HOSTNAME:=$(ulimit -c 0; hostname -s \
32c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser    2>/dev/null)} = *([	 ]|localhost) ]] && HOSTNAME=$(ulimit -c 0; hostname \
33c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser    2>/dev/null); : ${EDITOR:=/bin/ed} ${HOSTNAME:=nil} ${TERM:=vt100}
34811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser: ${MKSH:=$(whence -p mksh)}; PS4='[$EPOCHREALTIME] '; PS1=$'\001\r''${|
3503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	local e=$?
3603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
37811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	(( e )) && REPLY+="$e|"
38811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	REPLY+=${USER:=$(ulimit -c 0; id -un 2>/dev/null || echo \?)}
39811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	REPLY+=@${HOSTNAME%%.*}:
40811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser
41811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	local d=${PWD:-?} p=~; [[ $p = ?(*/) ]] || d=${d/#$p/~}
42811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	local m=${%d} n p=...; (( m > 0 )) || m=${#d}
43811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	(( m > (n = (COLUMNS/3 < 7 ? 7 : COLUMNS/3)) )) && d=${d:(-n)} || p=
44811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	REPLY+=$p$d
45811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser
46c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	return $e
47811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser} '"$PS1 "; export EDITOR HOSTNAME MKSH TERM USER
4803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraalias ls=ls
4903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraunalias ls
5003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraalias l='ls -F'
5103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraalias la='l -a'
5203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraalias ll='l -l'
5303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraalias lo='l -alo'
54811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaseralias doch='sudo mksh -c "$(fc -ln -1)"'
55c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaserwhence -p rot13 >/dev/null || alias rot13='tr \
5603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \
5703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra    nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
58811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaserif whence -p hd >/dev/null; then :; elif whence -p hexdump >/dev/null; then
59811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	function hd {
60811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser		hexdump -e '"%08.8_ax  " 8/1 "%02X " " - " 8/1 "%02X "' \
61811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser		    -e '"  |" "%_p"' -e '"|\n"' "$@"
62811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	}
63811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaserelse
64811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	function hd {
65811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser		local -Uui16 -Z11 pos=0
66811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser		local -Uui16 -Z5 hv=2147483647
67811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser		local dasc line i
68811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser
69811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser		cat "$@" | { set +U; if read -arN -1 line; then
70811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser			typeset -i1 line
71811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser			i=0
72811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser			while (( i < ${#line[*]} )); do
73811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser				hv=${line[i++]}
74811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser				if (( (pos & 15) == 0 )); then
75811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser					(( pos )) && print -r -- "$dasc|"
76811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser					print -n "${pos#16#}  "
77811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser					dasc=' |'
78811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser				fi
79811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser				print -n "${hv#16#} "
80811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser				if (( (hv < 32) || (hv > 126) )); then
81811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser					dasc+=.
82811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser				else
83811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser					dasc+=${line[i-1]#1#}
84811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser				fi
85811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser				(( (pos++ & 15) == 7 )) && print -n -- '- '
86811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser			done
87811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser			while (( pos & 15 )); do
88811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser				print -n '   '
89811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser				(( (pos++ & 15) == 7 )) && print -n -- '- '
90811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser			done
91811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser			(( hv == 2147483647 )) || print -r -- "$dasc|"
92811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser		fi; }
93811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	}
94811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaserfi
9503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
9603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# Berkeley C shell compatible dirs, popd, and pushd functions
9703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# Z shell compatible chpwd() hook, used to update DIRSTACK[0]
98c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten GlaserDIRSTACKBASE=$(realpath ~/. 2>/dev/null || print -nr -- "${HOME:-/}")
9903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraset -A DIRSTACK
10003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafunction chpwd {
101c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	DIRSTACK[0]=$(realpath . 2>/dev/null || print -r -- "$PWD")
10203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	[[ $DIRSTACKBASE = ?(*/) ]] || \
10303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	    DIRSTACK[0]=${DIRSTACK[0]/#$DIRSTACKBASE/~}
10403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	:
10503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
10603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrachpwd .
10703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafunction cd {
108427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes	builtin cd "$@" || return $?
10903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	chpwd "$@"
11003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
11103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafunction cd_csh {
11203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	local d t=${1/#~/$DIRSTACKBASE}
11303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
11403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if ! d=$(builtin cd "$t" 2>&1); then
115427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes		print -u2 "${1}: ${d##*cd: $t: }."
11603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		return 1
11703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	fi
11803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	cd "$t"
11903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
12003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafunction dirs {
12103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	local d dwidth
122c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	local -i fl=0 fv=0 fn=0 cpos=0
12303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
12403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	while getopts ":lvn" d; do
12503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		case $d {
12603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(l)	fl=1 ;;
12703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(v)	fv=1 ;;
12803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(n)	fn=1 ;;
12903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(*)	print -u2 'Usage: dirs [-lvn].'
13003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			return 1 ;;
13103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		}
13203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	done
13303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	shift $((OPTIND - 1))
13403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (( $# > 0 )); then
13503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		print -u2 'Usage: dirs [-lvn].'
13603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		return 1
13703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	fi
13803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (( fv )); then
13903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		fv=0
14003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		while (( fv < ${#DIRSTACK[*]} )); do
14103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			d=${DIRSTACK[fv]}
14203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			(( fl )) && d=${d/#~/$DIRSTACKBASE}
14303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			print -r -- "$fv	$d"
14403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			let fv++
14503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		done
14603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	else
14703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		fv=0
14803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		while (( fv < ${#DIRSTACK[*]} )); do
14903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			d=${DIRSTACK[fv]}
15003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			(( fl )) && d=${d/#~/$DIRSTACKBASE}
15103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			(( dwidth = (${%d} > 0 ? ${%d} : ${#d}) ))
15203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			if (( fn && (cpos += dwidth + 1) >= 79 && \
15303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			    dwidth < 80 )); then
15403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				print
15503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				(( cpos = dwidth + 1 ))
15603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			fi
15703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			print -nr -- "$d "
15803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			let fv++
15903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		done
16003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		print
16103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	fi
16203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	return 0
16303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
16403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafunction popd {
16503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	local d fa
166c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	local -i n=1
16703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
16803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	while getopts ":0123456789lvn" d; do
16903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		case $d {
170c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		(l|v|n)	fa+=" -$d" ;;
17103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(+*)	n=2
17203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			break ;;
17303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(*)	print -u2 'Usage: popd [-lvn] [+<n>].'
17403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			return 1 ;;
17503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		}
17603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	done
17703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	shift $((OPTIND - n))
17803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	n=0
17903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (( $# > 1 )); then
18003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		print -u2 popd: Too many arguments.
18103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		return 1
18203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
18303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
18403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			print -u2 popd: Directory stack not that deep.
18503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			return 1
18603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		fi
18703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	elif [[ -n $1 ]]; then
18803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		print -u2 popd: Bad directory.
18903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		return 1
19003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	fi
19103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (( ${#DIRSTACK[*]} < 2 )); then
19203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		print -u2 popd: Directory stack empty.
19303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		return 1
19403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	fi
19503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	unset DIRSTACK[n]
19603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	set -A DIRSTACK -- "${DIRSTACK[@]}"
19703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	cd_csh "${DIRSTACK[0]}" || return 1
19803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	dirs $fa
19903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
20003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafunction pushd {
20103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	local d fa
202c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	local -i n=1
20303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
20403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	while getopts ":0123456789lvn" d; do
20503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		case $d {
206c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		(l|v|n)	fa+=" -$d" ;;
20703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(+*)	n=2
20803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			break ;;
20903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(*)	print -u2 'Usage: pushd [-lvn] [<dir>|+<n>].'
21003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			return 1 ;;
21103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		}
21203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	done
21303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	shift $((OPTIND - n))
21403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (( $# == 0 )); then
21503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		if (( ${#DIRSTACK[*]} < 2 )); then
21603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			print -u2 pushd: No other directory.
21703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			return 1
21803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		fi
21903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		d=${DIRSTACK[1]}
22003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		DIRSTACK[1]=${DIRSTACK[0]}
22103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		cd_csh "$d" || return 1
22203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	elif (( $# > 1 )); then
22303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		print -u2 pushd: Too many arguments.
22403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		return 1
22503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
22603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
22703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			print -u2 pushd: Directory stack not that deep.
22803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			return 1
22903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		fi
23003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		while (( n-- )); do
23103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			d=${DIRSTACK[0]}
23203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			unset DIRSTACK[0]
23303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			set -A DIRSTACK -- "${DIRSTACK[@]}" "$d"
23403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		done
23503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		cd_csh "${DIRSTACK[0]}" || return 1
23603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	else
23703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		set -A DIRSTACK -- placeholder "${DIRSTACK[@]}"
23803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		cd_csh "$1" || return 1
23903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	fi
24003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	dirs $fa
24103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
24203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
24303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# pager (not control character safe)
24403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafunction smores {
245b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes	(
246b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes		set +m
247b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes		cat "$@" |&
248b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes		trap "rv=\$?; kill $! >/dev/null 2>&1; exit \$rv" EXIT
249b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes		while IFS= read -pr line; do
250b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes			llen=${%line}
251b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes			(( llen == -1 )) && llen=${#line}
252b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes			(( llen = llen ? (llen + COLUMNS - 1) / COLUMNS : 1 ))
253b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes			if (( (curlin += llen) >= LINES )); then
254b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes				print -n -- '\033[7m--more--\033[0m'
255b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes				read -u1 || exit $?
256b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes				[[ $REPLY = [Qq]* ]] && exit 0
257b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes				curlin=$llen
258b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes			fi
259b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes			print -r -- "$line"
260b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes		done
261b4542e99ca8562aa584e15df8ef35356ff8c10feElliott Hughes	)
26203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
26303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
26403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# base64 encoder and decoder, RFC compliant, NUL safe
26503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafunction Lb64decode {
26603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	[[ -o utf8-mode ]]; local u=$?
26703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	set +U
26803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	local c s="$*" t=
269811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	[[ -n $s ]] || { s=$(cat; print x); s=${s%x}; }
270c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	local -i i=0 j=0 n=${#s} p=0 v x
27103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	local -i16 o
27203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
27303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	while (( i < n )); do
27403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		c=${s:(i++):1}
27503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		case $c {
27603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(=)	break ;;
27703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		([A-Z])	(( v = 1#$c - 65 )) ;;
27803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		([a-z])	(( v = 1#$c - 71 )) ;;
27903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		([0-9])	(( v = 1#$c + 4 )) ;;
28003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(+)	v=62 ;;
28103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(/)	v=63 ;;
28203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(*)	continue ;;
28303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		}
28403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(( x = (x << 6) | v ))
28503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		case $((p++)) {
28603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(0)	continue ;;
28703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(1)	(( o = (x >> 4) & 255 )) ;;
28803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(2)	(( o = (x >> 2) & 255 )) ;;
28903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(3)	(( o = x & 255 ))
29003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			p=0
29103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			;;
29203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		}
293c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		t+=\\x${o#16#}
294c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		(( ++j & 4095 )) && continue
295c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		print -n $t
296c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		t=
29703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	done
29803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	print -n $t
29903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	(( u )) || set -U
30003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
30103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
30203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraset -A Lb64encode_code -- A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
30303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra    a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 + /
30403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafunction Lb64encode {
30503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	[[ -o utf8-mode ]]; local u=$?
30603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	set +U
30703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	local c s t
30803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (( $# )); then
30903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		read -raN-1 s <<<"$*"
31003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		unset s[${#s[*]}-1]
31103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	else
31203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		read -raN-1 s
31303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	fi
31403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	local -i i=0 n=${#s[*]} j v
31503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
31603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	while (( i < n )); do
31703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(( v = s[i++] << 16 ))
31803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(( j = i < n ? s[i++] : 0 ))
31903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(( v |= j << 8 ))
32003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(( j = i < n ? s[i++] : 0 ))
32103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		(( v |= j ))
322c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		t+=${Lb64encode_code[v >> 18]}${Lb64encode_code[v >> 12 & 63]}
32303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		c=${Lb64encode_code[v >> 6 & 63]}
32403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		if (( i <= n )); then
325c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser			t+=$c${Lb64encode_code[v & 63]}
32603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		elif (( i == n + 1 )); then
327c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser			t+=$c=
32803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		else
329c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser			t+===
33003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		fi
33103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		if (( ${#t} == 76 || i >= n )); then
33203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			print $t
33303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			t=
33403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		fi
33503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	done
33603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	(( u )) || set -U
33703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
33803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
339427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes# Better Avalanche for the Jenkins Hash
340427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughestypeset -Z11 -Uui16 Lbafh_v
341427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughesfunction Lbafh_init {
342427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes	Lbafh_v=0
343427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes}
344427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughesfunction Lbafh_add {
34503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	[[ -o utf8-mode ]]; local u=$?
34603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	set +U
34703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	local s
34803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (( $# )); then
34903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		read -raN-1 s <<<"$*"
35003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		unset s[${#s[*]}-1]
35103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	else
35203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		read -raN-1 s
35303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	fi
35403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	local -i i=0 n=${#s[*]}
35503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
35603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	while (( i < n )); do
357427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes		((# Lbafh_v = (Lbafh_v + s[i++] + 1) * 1025 ))
358427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes		((# Lbafh_v ^= Lbafh_v >> 6 ))
35903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	done
36003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
36103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	(( u )) || set -U
36203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
363427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughesfunction Lbafh_finish {
364427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes	local -Ui t
365427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes
366427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes	((# t = (((Lbafh_v >> 7) & 0x01010101) * 0x1B) ^ \
367427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes	    ((Lbafh_v << 1) & 0xFEFEFEFE) ))
368427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes	((# Lbafh_v = t ^ (t >>> 8) ^ (Lbafh_v >>> 8) ^ \
369427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes	    (Lbafh_v >>> 16) ^ (Lbafh_v >>> 24) ))
370427d76ccc44aea287b51b233f0254a6107b2b3d1Elliott Hughes	:
37103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
37203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
37303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# strip comments (and leading/trailing whitespace if IFS is set) from
37403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# any file(s) given as argument, or stdin if none, and spew to stdout
37503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafunction Lstripcom {
37603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	cat "$@" | { set -o noglob; while read _line; do
37703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		_line=${_line%%#*}
37803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		[[ -n $_line ]] && print -r -- $_line
37903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	done; }
38003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
38103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
38203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra# give MidnightBSD's laffer1 a bit of csh feeling
38303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafunction setenv {
384c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	eval export "\"$1\""'="$2"'
38503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
38603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
38703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra: place customisations below this line
38803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
38903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrafor p in ~/.etc/bin ~/bin; do
39003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	[[ -d $p/. ]] || continue
39103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	[[ :$PATH: = *:$p:* ]] || PATH=$p:$PATH
39203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condradone
39303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
39403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraexport SHELL=$MKSH MANWIDTH=80 LESSHISTFILE=-
39503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraalias cls='print -n \\033c'
39603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
39703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#unset LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_IDENTIFICATION LC_MONETARY \
39803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#    LC_NAME LC_NUMERIC LC_TELEPHONE LC_TIME
39903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#p=en_GB.UTF-8
40003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#set -U
40103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#export LANG=C LC_CTYPE=$p LC_MEASUREMENT=$p LC_MESSAGES=$p LC_PAPER=$p
40203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
40303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraunset p
40403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
40503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra: place customisations above this line
406