summaryrefslogtreecommitdiff
path: root/mksh/mkshrc
blob: 8d17b8eec950394eb4773da117110368d792762f (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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# $Id$
# $MirOS: src/bin/mksh/dot.mkshrc,v 1.128 2020/04/13 18:39:03 tg Exp $
#-
# Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010,
#		2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019,
#		2020
#	mirabilos <m@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, non-lksh, trying to run this file
case ${KSH_VERSION:-} in
*LEGACY\ KSH*|*MIRBSD\ KSH*) ;;
*) . $CONFIG/kshrc && exit ;;
esac

[ -f "$HOME/.config/sh/aliases" ] && . "$HOME/.config/sh/aliases"
[ -f "$HOME/.config/sh/functions" ] && . "$HOME/.config/sh/functions"

# give MidnightBSD's laffer1 a bit of csh feeling
function setenv {
	if (( $# )); then
		\\builtin eval '\\builtin export "$1"="${2:-}"'
	else
		\\builtin typeset -x
	fi
}

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

# customise your favourite editor here; the first one found is used
for EDITOR in "${EDITOR:-}" jupp jstar mcedit ed vi; do
	EDITOR=$(\\builtin whence -p "$EDITOR") || EDITOR=
	[[ -n $EDITOR && -x $EDITOR ]] && break
	EDITOR=nvim
done

\\builtin alias ls=ls l='ls -F' la='l -a' ll='l -l' lo='l -alo'
\: "${EDITOR:=/bin/ed}${TERM:=vt100}${USER:=$(\\builtin ulimit -c 0; id -un \
    2>/dev/null)}${HOSTNAME:=$(\\builtin ulimit -c 0; hostname 2>/dev/null)}"
[[ $HOSTNAME = ?(?(ip6-)localhost?(6)) ]] && HOSTNAME=nil; \\builtin unalias ls
\\builtin export EDITOR HOSTNAME TERM USER="${USER:-?}"

# minimal support for lksh users
if [[ $KSH_VERSION = *LEGACY\ KSH* ]]; then
	PS1='$USER@${HOSTNAME%%.*}:$PWD>'
	\\builtin return 0
else
        PS1='$(print -n "\E[1;32m[`logname`@`hostname`]:\E[1;37m";if [ "${PWD#$HOME}" = "$PWD" ]; then print -n "$PWD"; else print -n "~${PWD#$HOME}"; fi; print "\E[1;32m$\E[0m ")'
fi

# mksh-specific from here
\: "${MKSH:=$(\\builtin whence -p mksh)}${MKSH:=/bin/mksh}"
\\builtin export MKSH

# prompts
#PS4='[$EPOCHREALTIME] '; PS1='#'; (( USER_ID )) && PS1='$'; PS1=$'\001\r''${|
#	\\builtin typeset e=$?
#
#	(( e )) && REPLY+="$e|"
#        REPLY+=${USER}@${HOSTNAME%%.*}:
#
#	\\builtin typeset d=${PWD:-?}/ p=~; [[ $p = ?(*/) ]] || d=${d/#$p\//\~/}
#	d=${d%/}; \\builtin typeset m=${%d} n p=...; (( m > 0 )) || m=${#d}
#	(( m > (n = (COLUMNS/3 < 7 ? 7 : COLUMNS/3)) )) && d=${d:(-n)} || p=
#	REPLY+=$p$d
#
#	\\builtin return $e
#} '"$PS1 "

# utilities
\\builtin alias doch='sudo mksh -c "$(\\builtin fc -ln -1)"'
\\builtin command -v rot13 >/dev/null || \\builtin alias rot13='tr \
    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \
    nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
if \\builtin command -v hd >/dev/null; then
	\:
elif \\builtin command -v hexdump >/dev/null; then
	function hd {
		hexdump -e '"%08.8_ax  " 8/1 "%02X " " - " 8/1 "%02X "' \
		    -e '"  |" "%_p"' -e '"|\n"' "$@"
	}
else
	function hd {
		\\builtin cat "$@" | hd_mksh
	}
fi

# NUL-safe and EBCDIC-safe hexdump (from stdin)
function hd_mksh {
	\\builtin typeset -Uui16 -Z11 pos=0
	\\builtin typeset -Uui16 -Z5 hv=2147483647
	\\builtin typeset dasc dn line i
	\\builtin set +U

	while \\builtin read -arn 512 line; do
		\\builtin typeset -i1 'line[*]'
		i=0
		while (( i < ${#line[*]} )); do
			dn=
			(( (hv = line[i++]) != 0 )) && dn=${line[i-1]#1#}
			if (( (pos & 15) == 0 )); then
				(( pos )) && \
				    \\builtin print -r -- "$dasc|"
				\\builtin print -nr "${pos#16#}  "
				dasc=' |'
			fi
			\\builtin print -nr "${hv#16#} "
			if [[ $dn = [[:print:]] ]]; then
				dasc+=$dn
			else
				dasc+=.
			fi
			(( (pos++ & 15) == 7 )) && \
			    \\builtin print -nr -- '- '
		done
	done
	while (( pos & 15 )); do
		\\builtin print -nr '   '
		(( (pos++ & 15) == 7 )) && \
		    \\builtin print -nr -- '- '
	done
	(( hv == 2147483647 )) || \\builtin print -r -- "$dasc|"
}

function which {
	\\builtin typeset p x c
	\\builtin typeset -i a=0 rv=2 e
	\\builtin set +e
	\\builtin set -o noglob

	while \\builtin getopts "a" x; do
		case $x {
		(a)	a=1 ;;
		(+a)	a=0 ;;
		(*)	\\builtin print -ru2 'Usage: which [-a] name [...]'
			\\builtin return 255 ;;
		}
	done
	\\builtin shift $((OPTIND - 1))

	#        vvvvvvvvvvvvvvvvvvvv should be def_path
	p=${PATH-/usr/bin$PATHSEP/bin}
	#       ^ no colon!

	# trailing PATHSEP vs field splitting
	[[ $p = *"$PATHSEP" ]] && p+=.

	IFS=$PATHSEP
	\\builtin set -A p -- ${p:-.}
	IFS=$' \t\n'

	for x in "$@"; do
		if (( !a )) || [[ $x = */* ]]; then
			\\builtin whence -p -- "$x"
			e=$?
		else
			e=1
			for c in "${p[@]}"; do
				PATH=${c:-.} \\builtin whence -p -- "$x" && e=0
			done
		fi
		(( rv = (e == 0) ? (rv & ~2) : (rv == 2 ? 2 : 1) ))
	done
	\\builtin return $rv
}

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

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

	while \\builtin getopts ":lvn" d; do
		case $d {
		(l)	fl=1 ;;
		(v)	fv=1 ;;
		(n)	fn=1 ;;
		(*)	\\builtin print -ru2 'Usage: dirs [-lvn].'
			\\builtin return 1 ;;
		}
	done
	\\builtin shift $((OPTIND - 1))
	if (( $# > 0 )); then
		\\builtin print -ru2 'Usage: dirs [-lvn].'
		\\builtin return 1
	fi
	if (( fv )); then
		fv=0
		while (( fv < ${#DIRSTACK[*]} )); do
			d=${DIRSTACK[fv]}
			(( fl )) && d=${d/#\~/$DIRSTACKBASE}
			\\builtin print -r -- "$fv	$d"
			(( ++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
				\\builtin print
				(( cpos = dwidth + 1 ))
			fi
			\\builtin print -nr -- "$d "
			(( ++fv ))
		done
		\\builtin print
	fi
	\\builtin return 0
}
function popd {
	\\builtin typeset d fa
	\\builtin typeset -i n=1

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

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

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

	while (( i < n )); do
		c=${s:(i++):1}
		case $c {
		(=)	\\builtin break ;;
		([A-Z])	(( v = 1#$c - 65 )) ;;
		([a-z])	(( v = 1#$c - 71 )) ;;
		([0-9])	(( v = 1#$c + 4 )) ;;
		(+)	v=62 ;;
		(/)	v=63 ;;
		(*)	\\builtin continue ;;
		}
		(( x = (x << 6) | v ))
		case $((p++)) {
		(0)	\\builtin continue ;;
		(1)	(( o = (x >> 4) & 255 )) ;;
		(2)	(( o = (x >> 2) & 255 )) ;;
		(3)	(( o = x & 255 ))
			p=0
			;;
		}
		t+=\\x${o#16#}
		(( ++j & 4095 )) && \\builtin continue
		\\builtin print -n $t
		t=
	done
	\\builtin print -n $t
}
function Lb64encode {
	\\builtin set +U
	\\builtin typeset c s t table
	\\builtin set -A table -- 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 + /
	if (( $# )); then
		\\builtin read -raN-1 s <<<"$*"
		\\builtin unset s[${#s[*]}-1]
	else
		\\builtin read -raN-1 s
	fi
	\\builtin typeset -i i=0 n=${#s[*]} v

	while (( i < n )); do
		(( v = s[i++] << 16 ))
		(( v |= s[i++] << 8 ))
		(( v |= s[i++] ))
		t+=${table[v >> 18]}${table[v >> 12 & 63]}
		c=${table[v >> 6 & 63]}
		if (( i <= n )); then
			t+=$c${table[v & 63]}
		elif (( i == n + 1 )); then
			t+=$c=
		else
			t+===
		fi
		if (( ${#t} == 76 || i >= n )); then
			\\builtin print -r $t
			t=
		fi
	done
}

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

	while (( i < n )); do
		((# Lbafh_v = (Lbafh_v + s[i++] + 1) * 1025 ))
		((# Lbafh_v ^= Lbafh_v >> 6 ))
	done
}
function Lbafh_finish {
	\\builtin typeset -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 {
	\\builtin set -o noglob
	\\builtin cat "$@" | while \\builtin read _line; do
		_line=${_line%%#*}
		[[ -n $_line ]] && \\builtin print -r -- $_line
	done
}

# toggle built-in aliases and utilities, and aliases and functions from mkshrc
function enable {
	\\builtin typeset doprnt=0 mode=1 x y z rv=0
	\\builtin typeset b_alias i_alias i_func nalias=0 nfunc=0 i_all
	\\builtin set -A b_alias
	\\builtin set -A i_alias
	\\builtin set -A i_func

	# accumulate mksh built-in aliases, in ASCIIbetical order
	i_alias[nalias]=autoload; b_alias[nalias++]='\\builtin typeset -fu'
	i_alias[nalias]=functions; b_alias[nalias++]='\\builtin typeset -f'
	i_alias[nalias]=hash; b_alias[nalias++]='\\builtin alias -t'
	i_alias[nalias]=history; b_alias[nalias++]='\\builtin fc -l'
	i_alias[nalias]=integer; b_alias[nalias++]='\\builtin typeset -i'
	i_alias[nalias]=local; b_alias[nalias++]='\\builtin typeset'
	i_alias[nalias]=login; b_alias[nalias++]='\\builtin exec login'
	i_alias[nalias]=nameref; b_alias[nalias++]='\\builtin typeset -n'
	i_alias[nalias]=nohup; b_alias[nalias++]='nohup '
	i_alias[nalias]=r; b_alias[nalias++]='\\builtin fc -e -'
	i_alias[nalias]=type; b_alias[nalias++]='\\builtin whence -v'

	# accumulate mksh built-in utilities, in definition order, even ifndef
	i_func[nfunc++]=.
	i_func[nfunc++]=:
	i_func[nfunc++]='['
	i_func[nfunc++]=alias
	i_func[nfunc++]=break
	# \\builtin cannot, by design, be overridden
	i_func[nfunc++]=builtin
	i_func[nfunc++]=cat
	i_func[nfunc++]=cd
	i_func[nfunc++]=chdir
	i_func[nfunc++]=command
	i_func[nfunc++]=continue
	i_func[nfunc++]=echo
	i_func[nfunc++]=eval
	i_func[nfunc++]=exec
	i_func[nfunc++]=exit
	i_func[nfunc++]=export
	i_func[nfunc++]=false
	i_func[nfunc++]=fc
	i_func[nfunc++]=getopts
	i_func[nfunc++]=jobs
	i_func[nfunc++]=kill
	i_func[nfunc++]=let
	i_func[nfunc++]=print
	i_func[nfunc++]=pwd
	i_func[nfunc++]=read
	i_func[nfunc++]=readonly
	i_func[nfunc++]=realpath
	i_func[nfunc++]=rename
	i_func[nfunc++]=return
	i_func[nfunc++]=set
	i_func[nfunc++]=shift
	i_func[nfunc++]=source
	i_func[nfunc++]=suspend
	i_func[nfunc++]=test
	i_func[nfunc++]=times
	i_func[nfunc++]=trap
	i_func[nfunc++]=true
	i_func[nfunc++]=typeset
	i_func[nfunc++]=ulimit
	i_func[nfunc++]=umask
	i_func[nfunc++]=unalias
	i_func[nfunc++]=unset
	i_func[nfunc++]=wait
	i_func[nfunc++]=whence
	i_func[nfunc++]=bg
	i_func[nfunc++]=fg
	i_func[nfunc++]=bind
	i_func[nfunc++]=mknod
	i_func[nfunc++]=printf
	i_func[nfunc++]=sleep
	i_func[nfunc++]=domainname
	i_func[nfunc++]=extproc

	# accumulate aliases from dot.mkshrc, in definition order
	i_alias[nalias]=l; b_alias[nalias++]='ls -F'
	i_alias[nalias]=la; b_alias[nalias++]='l -a'
	i_alias[nalias]=ll; b_alias[nalias++]='l -l'
	i_alias[nalias]=lo; b_alias[nalias++]='l -alo'
	i_alias[nalias]=doch; b_alias[nalias++]='sudo mksh -c "$(\\builtin fc -ln -1)"'
	i_alias[nalias]=rot13; b_alias[nalias++]='tr abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
	i_alias[nalias]=cls; b_alias[nalias++]='\\builtin print -n \\ec'

	# accumulate functions from dot.mkshrc, in definition order
	i_func[nfunc++]=setenv
	i_func[nfunc++]=smores
	i_func[nfunc++]=hd
	i_func[nfunc++]=hd_mksh
	i_func[nfunc++]=which
	i_func[nfunc++]=chpwd
	i_func[nfunc++]=cd
	i_func[nfunc++]=cd_csh
	i_func[nfunc++]=dirs
	i_func[nfunc++]=popd
	i_func[nfunc++]=pushd
	i_func[nfunc++]=Lb64decode
	i_func[nfunc++]=Lb64encode
	i_func[nfunc++]=Lbafh_init
	i_func[nfunc++]=Lbafh_add
	i_func[nfunc++]=Lbafh_finish
	i_func[nfunc++]=Lstripcom
	i_func[nfunc++]=enable

	# collect all identifiers, sorted ASCIIbetically
	\\builtin set -sA i_all -- "${i_alias[@]}" "${i_func[@]}"

	# handle options, we don't do dynamic loading
	while \\builtin getopts "adf:nps" x; do
		case $x {
		(a)
			mode=-1
			;;
		(d)
			# deliberately causing an error, like bash-static
			;|
		(f)
			\\builtin print -ru2 enable: dynamic loading not available
			\\builtin return 2
			;;
		(n)
			mode=0
			;;
		(p)
			doprnt=1
			;;
		(s)
			\\builtin set -sA i_all -- . : break continue eval \
			    exec exit export readonly return set shift times \
			    trap unset
			;;
		(*)
			\\builtin print -ru2 enable: usage: \
			    "enable [-adnps] [-f filename] [name ...]"
			return 2
			;;
		}
	done
	\\builtin shift $((OPTIND - 1))

	# display builtins enabled/disabled/all/special?
	if (( doprnt || ($# == 0) )); then
		for x in "${i_all[@]}"; do
			y=$(\\builtin alias "$x") || y=
			[[ $y = "$x='\\\\builtin whence -p $x >/dev/null || (\\\\builtin print -r mksh: $x: not found; \\\\builtin exit 127) && \$(\\\\builtin whence -p $x)'" ]]; z=$?
			case $mode:$z {
			(-1:0|0:0)
				\\builtin print -r -- "enable -n $x"
				;;
			(-1:1|1:1)
				\\builtin print -r -- "enable $x"
				;;
			}
		done
		\\builtin return 0
	fi

	for x in "$@"; do
		z=0
		for y in "${i_alias[@]}" "${i_func[@]}"; do
			[[ $x = "$y" ]] || \\builtin continue
			z=1
			\\builtin break
		done
		if (( !z )); then
			\\builtin print -ru2 enable: "$x": not a shell builtin
			rv=1
			\\builtin continue
		fi
		if (( !mode )); then
			# disable this
			\\builtin alias "$x=\\\\builtin whence -p $x >/dev/null || (\\\\builtin print -r mksh: $x: not found; \\\\builtin exit 127) && \$(\\\\builtin whence -p $x)"
		else
			# find out if this is an alias or not, first
			z=0
			y=-1
			while (( ++y < nalias )); do
				[[ $x = "${i_alias[y]}" ]] || \\builtin continue
				z=1
				\\builtin break
			done
			if (( z )); then
				# re-enable the original alias body
				\\builtin alias "$x=${b_alias[y]}"
			else
				# re-enable the original utility/function
				\\builtin unalias "$x"
			fi
		fi
	done
	\\builtin return $rv
}

\: place customisations below this line

# some defaults / samples which you are supposed to adjust to your
# liking; by default we add ~/.etc/bin and ~/bin (whichever exist)
# to $PATH, set $SHELL to mksh, set some defaults for man and less
# and show a few more possible things for users to begin moving in

for p in ~/.etc/bin ~/bin; do
	[[ -d $p/. ]] || \\builtin continue
	[[ $PATHSEP$PATH$PATHSEP = *"$PATHSEP$p$PATHSEP"* ]] || \
	    PATH=$p$PATHSEP$PATH
done

\\builtin export SHELL=$MKSH MANWIDTH=80 LESSHISTFILE=-
\\builtin alias cls='\\builtin print -n \\ec'

#\\builtin unset LC_ADDRESS LC_COLLATE LC_CTYPE LC_IDENTIFICATION \
#    LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER \
#    LC_TELEPHONE LC_TIME LANGUAGE LANG LC_ALL
#p=en_GB.UTF-8
#\\builtin export LANG=C LC_CTYPE=$p LC_MEASUREMENT=$p LC_MESSAGES=$p LC_PAPER=$p
#\\builtin export LANG=C.UTF-8 LC_CTYPE=C.UTF-8
#\\builtin export LC_ALL=C.UTF-8
#\\builtin set -U
#[[ ${LC_ALL:-${LC_CTYPE:-${LANG:-}}} = *[Uu][Tt][Ff]?(-)8* ]] || \\builtin set +U

\\builtin unset p

set -o vi

[ -r $MKSHDIR/mkshfunc ] && source $MKSHDIR/mkshfunc
[ -r $MKSHDIR/mkshalias ] && source $MKSHDIR/mkshalias

HISTFILE="$MKSHDIR"/history

\: place customisations above this line