aboutsummaryrefslogtreecommitdiff
path: root/mkshrc
blob: 98880e0db822393c79917eb4b46d2cfcab93034e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# $Id$
# $MirOS: src/bin/mksh/dot.mkshrc,v 1.89 2014/07/28 21:45:44 tg Exp $
#-
# Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010,
#		2011, 2012, 2013, 2014
#	Thorsten Glaser <tg@mirbsd.org>
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
# is granted to deal in this work without restriction, including un-
# limited rights to use, publicly perform, distribute, sell, modify,
# merge, give away, or sublicence.
#
# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
# the utmost extent permitted by applicable law, neither express nor
# implied; without malicious intent or gross negligence. In no event
# may a licensor, author or contributor be held liable for indirect,
# direct, other damage, loss, or other issues arising in any way out
# of dealing in the work, even if advised of the possibility of such
# damage or existence of a defect, except proven that it results out
# of said person's immediate fault when using the work as intended.
#-
# ${ENV:-~/.mkshrc}: mksh initialisation file for interactive shells

# catch non-mksh (including lksh) trying to shell this file
case $KSH_VERSION in
*MIRBSD\ KSH*) ;;
*) return 0 ;;
esac

PS1='#'; (( USER_ID )) && PS1='$'; [[ ${HOSTNAME:=$(ulimit -c 0; hostname -s \
    2>/dev/null)} = *([	 ]|localhost) ]] && HOSTNAME=$(ulimit -c 0; hostname \
    2>/dev/null); : ${EDITOR:=/bin/ed} ${HOSTNAME:=nil} ${TERM:=vt100}
: ${MKSH:=$(whence -p mksh)}; PS4='[$EPOCHREALTIME] '; PS1=$'\001\r''${|

	local e=$?

	(( e )) && REPLY+="$e|"
	REPLY+=${USER:=$(ulimit -c 0; id -un 2>/dev/null || echo \?)}
	REPLY+=@${HOSTNAME%%.*}:

	local d=${PWD:-?} p=~; [[ $p = ?(*/) ]] || d=${d/#$p/~}
	local m=${%d} n p=...; (( m > 0 )) || m=${#d}
	(( m > (n = (COLUMNS/3 < 7 ? 7 : COLUMNS/3)) )) && d=${d:(-n)} || p=
	REPLY+=$p$d

	return $e
} '"$PS1 "; export EDITOR HOSTNAME MKSH TERM USER
alias ls=ls
unalias ls
alias l='ls -F'
alias la='l -a'
alias ll='l -l'
alias lo='l -alo'
alias doch='sudo mksh -c "$(fc -ln -1)"'
whence -p rot13 >/dev/null || alias rot13='tr \
    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \
    nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
if whence -p hd >/dev/null; then :; elif whence -p hexdump >/dev/null; then
	function hd {
		hexdump -e '"%08.8_ax  " 8/1 "%02X " " - " 8/1 "%02X "' \
		    -e '"  |" "%_p"' -e '"|\n"' "$@"
	}
else
	function hd {
		local -Uui16 -Z11 pos=0
		local -Uui16 -Z5 hv=2147483647
		local dasc line i

		cat "$@" | { set +U; if read -arN -1 line; then
			typeset -i1 line
			i=0
			while (( i < ${#line[*]} )); do
				hv=${line[i++]}
				if (( (pos & 15) == 0 )); then
					(( pos )) && print -r -- "$dasc|"
					print -n "${pos#16#}  "
					dasc=' |'
				fi
				print -n "${hv#16#} "
				if (( (hv < 32) || (hv > 126) )); then
					dasc+=.
				else
					dasc+=${line[i-1]#1#}
				fi
				(( (pos++ & 15) == 7 )) && print -n -- '- '
			done
			while (( pos & 15 )); do
				print -n '   '
				(( (pos++ & 15) == 7 )) && print -n -- '- '
			done
			(( hv == 2147483647 )) || print -r -- "$dasc|"
		fi; }
	}
fi

# Berkeley C shell compatible dirs, popd, and pushd functions
# Z shell compatible chpwd() hook, used to update DIRSTACK[0]
DIRSTACKBASE=$(realpath ~/. 2>/dev/null || print -nr -- "${HOME:-/}")
set -A DIRSTACK
function chpwd {
	DIRSTACK[0]=$(realpath . 2>/dev/null || print -r -- "$PWD")
	[[ $DIRSTACKBASE = ?(*/) ]] || \
	    DIRSTACK[0]=${DIRSTACK[0]/#$DIRSTACKBASE/~}
	:
}
chpwd .
function cd {
	builtin cd "$@" || return $?
	chpwd "$@"
}
function cd_csh {
	local d t=${1/#~/$DIRSTACKBASE}

	if ! d=$(builtin cd "$t" 2>&1); then
		print -u2 "${1}: ${d##*cd: $t: }."
		return 1
	fi
	cd "$t"
}
function dirs {
	local d dwidth
	local -i fl=0 fv=0 fn=0 cpos=0

	while getopts ":lvn" d; do
		case $d {
		(l)	fl=1 ;;
		(v)	fv=1 ;;
		(n)	fn=1 ;;
		(*)	print -u2 'Usage: dirs [-lvn].'
			return 1 ;;
		}
	done
	shift $((OPTIND - 1))
	if (( $# > 0 )); then
		print -u2 'Usage: dirs [-lvn].'
		return 1
	fi
	if (( fv )); then
		fv=0
		while (( fv < ${#DIRSTACK[*]} )); do
			d=${DIRSTACK[fv]}
			(( fl )) && d=${d/#~/$DIRSTACKBASE}
			print -r -- "$fv	$d"
			let fv++
		done
	else
		fv=0
		while (( fv < ${#DIRSTACK[*]} )); do
			d=${DIRSTACK[fv]}
			(( fl )) && d=${d/#~/$DIRSTACKBASE}
			(( dwidth = (${%d} > 0 ? ${%d} : ${#d}) ))
			if (( fn && (cpos += dwidth + 1) >= 79 && \
			    dwidth < 80 )); then
				print
				(( cpos = dwidth + 1 ))
			fi
			print -nr -- "$d "
			let fv++
		done
		print
	fi
	return 0
}
function popd {
	local d fa
	local -i n=1

	while getopts ":0123456789lvn" d; do
		case $d {
		(l|v|n)	fa+=" -$d" ;;
		(+*)	n=2
			break ;;
		(*)	print -u2 'Usage: popd [-lvn] [+<n>].'
			return 1 ;;
		}
	done
	shift $((OPTIND - n))
	n=0
	if (( $# > 1 )); then
		print -u2 popd: Too many arguments.
		return 1
	elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
		if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
			print -u2 popd: Directory stack not that deep.
			return 1
		fi
	elif [[ -n $1 ]]; then
		print -u2 popd: Bad directory.
		return 1
	fi
	if (( ${#DIRSTACK[*]} < 2 )); then
		print -u2 popd: Directory stack empty.
		return 1
	fi
	unset DIRSTACK[n]
	set -A DIRSTACK -- "${DIRSTACK[@]}"
	cd_csh "${DIRSTACK[0]}" || return 1
	dirs $fa
}
function pushd {
	local d fa
	local -i n=1

	while getopts ":0123456789lvn" d; do
		case $d {
		(l|v|n)	fa+=" -$d" ;;
		(+*)	n=2
			break ;;
		(*)	print -u2 'Usage: pushd [-lvn] [<dir>|+<n>].'
			return 1 ;;
		}
	done
	shift $((OPTIND - n))
	if (( $# == 0 )); then
		if (( ${#DIRSTACK[*]} < 2 )); then
			print -u2 pushd: No other directory.
			return 1
		fi
		d=${DIRSTACK[1]}
		DIRSTACK[1]=${DIRSTACK[0]}
		cd_csh "$d" || return 1
	elif (( $# > 1 )); then
		print -u2 pushd: Too many arguments.
		return 1
	elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
		if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
			print -u2 pushd: Directory stack not that deep.
			return 1
		fi
		while (( n-- )); do
			d=${DIRSTACK[0]}
			unset DIRSTACK[0]
			set -A DIRSTACK -- "${DIRSTACK[@]}" "$d"
		done
		cd_csh "${DIRSTACK[0]}" || return 1
	else
		set -A DIRSTACK -- placeholder "${DIRSTACK[@]}"
		cd_csh "$1" || return 1
	fi
	dirs $fa
}

# pager (not control character safe)
function smores {
	(
		set +m
		cat "$@" |&
		trap "rv=\$?; kill $! >/dev/null 2>&1; exit \$rv" EXIT
		while IFS= read -pr line; do
			llen=${%line}
			(( llen == -1 )) && llen=${#line}
			(( llen = llen ? (llen + COLUMNS - 1) / COLUMNS : 1 ))
			if (( (curlin += llen) >= LINES )); then
				print -n -- '\033[7m--more--\033[0m'
				read -u1 || exit $?
				[[ $REPLY = [Qq]* ]] && exit 0
				curlin=$llen
			fi
			print -r -- "$line"
		done
	)
}

# base64 encoder and decoder, RFC compliant, NUL safe
function Lb64decode {
	[[ -o utf8-mode ]]; local u=$?
	set +U
	local c s="$*" t=
	[[ -n $s ]] || { s=$(cat; print x); s=${s%x}; }
	local -i i=0 j=0 n=${#s} p=0 v x
	local -i16 o

	while (( i < n )); do
		c=${s:(i++):1}
		case $c {
		(=)	break ;;
		([A-Z])	(( v = 1#$c - 65 )) ;;
		([a-z])	(( v = 1#$c - 71 )) ;;
		([0-9])	(( v = 1#$c + 4 )) ;;
		(+)	v=62 ;;
		(/)	v=63 ;;
		(*)	continue ;;
		}
		(( x = (x << 6) | v ))
		case $((p++)) {
		(0)	continue ;;
		(1)	(( o = (x >> 4) & 255 )) ;;
		(2)	(( o = (x >> 2) & 255 )) ;;
		(3)	(( o = x & 255 ))
			p=0
			;;
		}
		t+=\\x${o#16#}
		(( ++j & 4095 )) && continue
		print -n $t
		t=
	done
	print -n $t
	(( u )) || set -U
}

set -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 \
    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 + /
function Lb64encode {
	[[ -o utf8-mode ]]; local u=$?
	set +U
	local c s t
	if (( $# )); then
		read -raN-1 s <<<"$*"
		unset s[${#s[*]}-1]
	else
		read -raN-1 s
	fi
	local -i i=0 n=${#s[*]} j v

	while (( i < n )); do
		(( v = s[i++] << 16 ))
		(( j = i < n ? s[i++] : 0 ))
		(( v |= j << 8 ))
		(( j = i < n ? s[i++] : 0 ))
		(( v |= j ))
		t+=${Lb64encode_code[v >> 18]}${Lb64encode_code[v >> 12 & 63]}
		c=${Lb64encode_code[v >> 6 & 63]}
		if (( i <= n )); then
			t+=$c${Lb64encode_code[v & 63]}
		elif (( i == n + 1 )); then
			t+=$c=
		else
			t+===
		fi
		if (( ${#t} == 76 || i >= n )); then
			print $t
			t=
		fi
	done
	(( u )) || set -U
}

# Better Avalanche for the Jenkins Hash
typeset -Z11 -Uui16 Lbafh_v
function Lbafh_init {
	Lbafh_v=0
}
function Lbafh_add {
	[[ -o utf8-mode ]]; local u=$?
	set +U
	local s
	if (( $# )); then
		read -raN-1 s <<<"$*"
		unset s[${#s[*]}-1]
	else
		read -raN-1 s
	fi
	local -i i=0 n=${#s[*]}

	while (( i < n )); do
		((# Lbafh_v = (Lbafh_v + s[i++] + 1) * 1025 ))
		((# Lbafh_v ^= Lbafh_v >> 6 ))
	done

	(( u )) || set -U
}
function Lbafh_finish {
	local -Ui t

	((# t = (((Lbafh_v >> 7) & 0x01010101) * 0x1B) ^ \
	    ((Lbafh_v << 1) & 0xFEFEFEFE) ))
	((# Lbafh_v = t ^ (t >>> 8) ^ (Lbafh_v >>> 8) ^ \
	    (Lbafh_v >>> 16) ^ (Lbafh_v >>> 24) ))
	:
}

# strip comments (and leading/trailing whitespace if IFS is set) from
# any file(s) given as argument, or stdin if none, and spew to stdout
function Lstripcom {
	cat "$@" | { set -o noglob; while read _line; do
		_line=${_line%%#*}
		[[ -n $_line ]] && print -r -- $_line
	done; }
}

# give MidnightBSD's laffer1 a bit of csh feeling
function setenv {
	eval export "\"$1\""'="$2"'
}

: place customisations below this line

HISTFILE=$HOME/.mksh_history
HISTSIZE=1000
SHELL=$MKSH
PATH="$HOME/bin:$PATH"
export HISTFILE HISTSIZE SHELL PATH

# fix del key
printf '\033[?1h\033=' >/dev/tty


if [ -d ~/.mksh.d ]; then
  for f in ~/.mksh.d/*.sh; do
    [ -x "$f" ] && . "$f"
  done
fi

: place customisations above this line