• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# $Id$
2# $MirOS: src/bin/mksh/dot.mkshrc,v 1.77 2013/02/17 15:58:26 tg Exp $
3#-
4# Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010,
5#		2011, 2012, 2013
6#	Thorsten Glaser <tg@mirbsd.org>
7#
8# Provided that these terms and disclaimer and all copyright notices
9# are retained or reproduced in an accompanying document, permission
10# is granted to deal in this work without restriction, including un-
11# limited rights to use, publicly perform, distribute, sell, modify,
12# merge, give away, or sublicence.
13#
14# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15# the utmost extent permitted by applicable law, neither express nor
16# implied; without malicious intent or gross negligence. In no event
17# may a licensor, author or contributor be held liable for indirect,
18# direct, other damage, loss, or other issues arising in any way out
19# of dealing in the work, even if advised of the possibility of such
20# damage or existence of a defect, except proven that it results out
21# of said person's immediate fault when using the work as intended.
22#-
23# ${ENV:-~/.mkshrc}: mksh initialisation file for interactive shells
24
25PS1='#'; (( USER_ID )) && PS1='$'; [[ ${HOSTNAME:=$(ulimit -c 0; hostname -s \
26    2>/dev/null)} = *([	 ]|localhost) ]] && HOSTNAME=$(ulimit -c 0; hostname \
27    2>/dev/null); : ${EDITOR:=/bin/ed} ${HOSTNAME:=nil} ${TERM:=vt100}
28function precmd {
29	local e=$?
30
31	(( e )) && print -n "$e|"
32	# precmd is required to retain the errorlevel when ${ …;} is used
33	return $e
34}
35PS1=$'\001\r''${ precmd;}${USER:=$(ulimit -c 0; id -un 2>/dev/null || echo \?
36	)}@${HOSTNAME%%.*}:${ local e=$? d=${PWD:-?} p=~; [[ $p = ?(*/) ]] || \
37	d=${d/#$p/~}; local m=${%d} n p=...; (( m > 0 )) || m=${#d}
38	(( m > (n = (COLUMNS/3 < 7 ? 7 : COLUMNS/3)) )) && d=${d:(-n)} || \
39	p=; print -nr -- "$p$d"; return $e;} '"$PS1 "
40: ${MKSH:=$(whence -p mksh)}; export EDITOR HOSTNAME MKSH TERM USER
41alias ls=ls
42unalias ls
43alias l='ls -F'
44alias la='l -a'
45alias ll='l -l'
46alias lo='l -alo'
47whence -p rot13 >/dev/null || alias rot13='tr \
48    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \
49    nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
50whence -p hd >/dev/null || function hd {
51	hexdump -e '"%08.8_ax  " 8/1 "%02X " " - " 8/1 "%02X "' \
52	    -e '"  |" "%_p"' -e '"|\n"' "$@"
53}
54
55# Berkeley C shell compatible dirs, popd, and pushd functions
56# Z shell compatible chpwd() hook, used to update DIRSTACK[0]
57DIRSTACKBASE=$(realpath ~/. 2>/dev/null || print -nr -- "${HOME:-/}")
58set -A DIRSTACK
59function chpwd {
60	DIRSTACK[0]=$(realpath . 2>/dev/null || print -r -- "$PWD")
61	[[ $DIRSTACKBASE = ?(*/) ]] || \
62	    DIRSTACK[0]=${DIRSTACK[0]/#$DIRSTACKBASE/~}
63	:
64}
65chpwd .
66function cd {
67	builtin cd "$@"
68	chpwd "$@"
69}
70function cd_csh {
71	local d t=${1/#~/$DIRSTACKBASE}
72
73	if ! d=$(builtin cd "$t" 2>&1); then
74		print -u2 "${1}: ${d##*$t - }."
75		return 1
76	fi
77	cd "$t"
78}
79function dirs {
80	local d dwidth
81	local -i fl=0 fv=0 fn=0 cpos=0
82
83	while getopts ":lvn" d; do
84		case $d {
85		(l)	fl=1 ;;
86		(v)	fv=1 ;;
87		(n)	fn=1 ;;
88		(*)	print -u2 'Usage: dirs [-lvn].'
89			return 1 ;;
90		}
91	done
92	shift $((OPTIND - 1))
93	if (( $# > 0 )); then
94		print -u2 'Usage: dirs [-lvn].'
95		return 1
96	fi
97	if (( fv )); then
98		fv=0
99		while (( fv < ${#DIRSTACK[*]} )); do
100			d=${DIRSTACK[fv]}
101			(( fl )) && d=${d/#~/$DIRSTACKBASE}
102			print -r -- "$fv	$d"
103			let fv++
104		done
105	else
106		fv=0
107		while (( fv < ${#DIRSTACK[*]} )); do
108			d=${DIRSTACK[fv]}
109			(( fl )) && d=${d/#~/$DIRSTACKBASE}
110			(( dwidth = (${%d} > 0 ? ${%d} : ${#d}) ))
111			if (( fn && (cpos += dwidth + 1) >= 79 && \
112			    dwidth < 80 )); then
113				print
114				(( cpos = dwidth + 1 ))
115			fi
116			print -nr -- "$d "
117			let fv++
118		done
119		print
120	fi
121	return 0
122}
123function popd {
124	local d fa
125	local -i n=1
126
127	while getopts ":0123456789lvn" d; do
128		case $d {
129		(l|v|n)	fa+=" -$d" ;;
130		(+*)	n=2
131			break ;;
132		(*)	print -u2 'Usage: popd [-lvn] [+<n>].'
133			return 1 ;;
134		}
135	done
136	shift $((OPTIND - n))
137	n=0
138	if (( $# > 1 )); then
139		print -u2 popd: Too many arguments.
140		return 1
141	elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
142		if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
143			print -u2 popd: Directory stack not that deep.
144			return 1
145		fi
146	elif [[ -n $1 ]]; then
147		print -u2 popd: Bad directory.
148		return 1
149	fi
150	if (( ${#DIRSTACK[*]} < 2 )); then
151		print -u2 popd: Directory stack empty.
152		return 1
153	fi
154	unset DIRSTACK[n]
155	set -A DIRSTACK -- "${DIRSTACK[@]}"
156	cd_csh "${DIRSTACK[0]}" || return 1
157	dirs $fa
158}
159function pushd {
160	local d fa
161	local -i n=1
162
163	while getopts ":0123456789lvn" d; do
164		case $d {
165		(l|v|n)	fa+=" -$d" ;;
166		(+*)	n=2
167			break ;;
168		(*)	print -u2 'Usage: pushd [-lvn] [<dir>|+<n>].'
169			return 1 ;;
170		}
171	done
172	shift $((OPTIND - n))
173	if (( $# == 0 )); then
174		if (( ${#DIRSTACK[*]} < 2 )); then
175			print -u2 pushd: No other directory.
176			return 1
177		fi
178		d=${DIRSTACK[1]}
179		DIRSTACK[1]=${DIRSTACK[0]}
180		cd_csh "$d" || return 1
181	elif (( $# > 1 )); then
182		print -u2 pushd: Too many arguments.
183		return 1
184	elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
185		if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
186			print -u2 pushd: Directory stack not that deep.
187			return 1
188		fi
189		while (( n-- )); do
190			d=${DIRSTACK[0]}
191			unset DIRSTACK[0]
192			set -A DIRSTACK -- "${DIRSTACK[@]}" "$d"
193		done
194		cd_csh "${DIRSTACK[0]}" || return 1
195	else
196		set -A DIRSTACK -- placeholder "${DIRSTACK[@]}"
197		cd_csh "$1" || return 1
198	fi
199	dirs $fa
200}
201
202# pager (not control character safe)
203function smores {
204	local dummy line llen curlin=0
205
206	cat "$@" | while IFS= read -r line; do
207		llen=${%line}
208		(( llen == -1 )) && llen=${#line}
209		(( llen = llen ? (llen + COLUMNS - 1) / COLUMNS : 1 ))
210		if (( (curlin += llen) >= LINES )); then
211			print -n -- '\033[7m--more--\033[0m'
212			read -u1 dummy
213			[[ $dummy = [Qq]* ]] && return 0
214			curlin=$llen
215		fi
216		print -r -- "$line"
217	done
218}
219
220# base64 encoder and decoder, RFC compliant, NUL safe
221function Lb64decode {
222	[[ -o utf8-mode ]]; local u=$?
223	set +U
224	local c s="$*" t=
225	[[ -n $s ]] || { s=$(cat;print x); s=${s%x}; }
226	local -i i=0 j=0 n=${#s} p=0 v x
227	local -i16 o
228
229	while (( i < n )); do
230		c=${s:(i++):1}
231		case $c {
232		(=)	break ;;
233		([A-Z])	(( v = 1#$c - 65 )) ;;
234		([a-z])	(( v = 1#$c - 71 )) ;;
235		([0-9])	(( v = 1#$c + 4 )) ;;
236		(+)	v=62 ;;
237		(/)	v=63 ;;
238		(*)	continue ;;
239		}
240		(( x = (x << 6) | v ))
241		case $((p++)) {
242		(0)	continue ;;
243		(1)	(( o = (x >> 4) & 255 )) ;;
244		(2)	(( o = (x >> 2) & 255 )) ;;
245		(3)	(( o = x & 255 ))
246			p=0
247			;;
248		}
249		t+=\\x${o#16#}
250		(( ++j & 4095 )) && continue
251		print -n $t
252		t=
253	done
254	print -n $t
255	(( u )) || set -U
256}
257
258set -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 \
259    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 + /
260function Lb64encode {
261	[[ -o utf8-mode ]]; local u=$?
262	set +U
263	local c s t
264	if (( $# )); then
265		read -raN-1 s <<<"$*"
266		unset s[${#s[*]}-1]
267	else
268		read -raN-1 s
269	fi
270	local -i i=0 n=${#s[*]} j v
271
272	while (( i < n )); do
273		(( v = s[i++] << 16 ))
274		(( j = i < n ? s[i++] : 0 ))
275		(( v |= j << 8 ))
276		(( j = i < n ? s[i++] : 0 ))
277		(( v |= j ))
278		t+=${Lb64encode_code[v >> 18]}${Lb64encode_code[v >> 12 & 63]}
279		c=${Lb64encode_code[v >> 6 & 63]}
280		if (( i <= n )); then
281			t+=$c${Lb64encode_code[v & 63]}
282		elif (( i == n + 1 )); then
283			t+=$c=
284		else
285			t+===
286		fi
287		if (( ${#t} == 76 || i >= n )); then
288			print $t
289			t=
290		fi
291	done
292	(( u )) || set -U
293}
294
295# mksh NUL counting, never zero
296typeset -Z11 -Uui16 Lnzathash_v
297function Lnzathash_add {
298	[[ -o utf8-mode ]]; local u=$?
299	set +U
300	local s
301	if (( $# )); then
302		read -raN-1 s <<<"$*"
303		unset s[${#s[*]}-1]
304	else
305		read -raN-1 s
306	fi
307	local -i i=0 n=${#s[*]}
308
309	while (( i < n )); do
310		((# Lnzathash_v = (Lnzathash_v + s[i++] + 1) * 1025 ))
311		((# Lnzathash_v ^= Lnzathash_v >> 6 ))
312	done
313
314	(( u )) || set -U
315}
316function Lnzaathash_end {
317	((# Lnzathash_v *= 1025 ))
318	((# Lnzathash_v ^= Lnzathash_v >> 6 ))
319	((# Lnzathash_v += Lnzathash_v << 3 ))
320	((# Lnzathash_v = (Lnzathash_v ^
321	    (Lnzathash_v >> 11)) * 32769 ))
322	print ${Lnzathash_v#16#}
323}
324function Lnzaathash {
325	Lnzathash_v=0
326	Lnzathash_add "$@"
327	Lnzaathash_end
328}
329function Lnzathash {
330	Lnzathash_v=0
331	Lnzathash_add "$@"
332	Lnzathash_end
333}
334function Lnzathash_end {
335	if (( Lnzathash_v )); then
336		Lnzaathash_end
337	else
338		Lnzathash_v=1
339		print ${Lnzathash_v#16#}
340	fi
341}
342
343# strip comments (and leading/trailing whitespace if IFS is set) from
344# any file(s) given as argument, or stdin if none, and spew to stdout
345function Lstripcom {
346	cat "$@" | { set -o noglob; while read _line; do
347		_line=${_line%%#*}
348		[[ -n $_line ]] && print -r -- $_line
349	done; }
350}
351
352# give MidnightBSD's laffer1 a bit of csh feeling
353function setenv {
354	eval export "\"$1\""'="$2"'
355}
356
357: place customisations below this line
358
359for p in ~/.etc/bin ~/bin; do
360	[[ -d $p/. ]] || continue
361	[[ :$PATH: = *:$p:* ]] || PATH=$p:$PATH
362done
363
364export SHELL=$MKSH MANWIDTH=80 LESSHISTFILE=-
365alias cls='print -n \\033c'
366
367#unset LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_IDENTIFICATION LC_MONETARY \
368#    LC_NAME LC_NUMERIC LC_TELEPHONE LC_TIME
369#p=en_GB.UTF-8
370#set -U
371#export LANG=C LC_CTYPE=$p LC_MEASUREMENT=$p LC_MESSAGES=$p LC_PAPER=$p
372
373unset p
374
375: place customisations above this line
376