• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# $Id$
2# $MirOS: src/bin/mksh/dot.mkshrc,v 1.104 2015/12/31 21:00:12 tg Exp $
3#-
4# Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010,
5#		2011, 2012, 2013, 2014, 2015
6#	mirabilos <m@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
25# catch non-mksh (including lksh) trying to run this file
26case ${KSH_VERSION:-} in
27*MIRBSD\ KSH*) ;;
28*) return 0 ;;
29esac
30
31PS1='#'; (( USER_ID )) && PS1='$'; \: "${TERM:=vt100}${HOSTNAME:=$(\ulimit -c \
32    0; hostname 2>/dev/null)}${EDITOR:=/bin/ed}${USER:=$(\ulimit -c 0; id -un \
33    2>/dev/null || \echo \?)}${MKSH:=$(\builtin whence -p mksh)}"
34HOSTNAME=${HOSTNAME%%*([	 ]).*}; HOSTNAME=${HOSTNAME##*([	 ])}
35[[ $HOSTNAME = ?(ip6-)localhost?(6) ]] && HOSTNAME=
36\: "${HOSTNAME:=nil}${MKSH:=/bin/mksh}"; \export EDITOR HOSTNAME MKSH TERM USER
37PS4='[$EPOCHREALTIME] '; PS1=$'\001\r''${|
38	\typeset e=$?
39
40	(( e )) && REPLY+="$e|"
41	REPLY+=${USER}@${HOSTNAME%%.*}:
42
43	\typeset d=${PWD:-?} p=~; [[ $p = ?(*/) ]] || d=${d/#$p/\~}
44	\typeset m=${%d} n p=...; (( m > 0 )) || m=${#d}
45	(( m > (n = (COLUMNS/3 < 7 ? 7 : COLUMNS/3)) )) && d=${d:(-n)} || p=
46	REPLY+=$p$d
47
48	\return $e
49} '"$PS1 "
50\alias ls=ls
51\unalias ls
52\alias l='ls -F'
53\alias la='l -a'
54\alias ll='l -l'
55\alias lo='l -alo'
56\alias doch='sudo mksh -c "$(\builtin fc -ln -1)"'
57\command -v rot13 >/dev/null || \alias rot13='tr \
58    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \
59    nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
60if \command -v hd >/dev/null; then \:; elif \command -v hexdump >/dev/null; then
61	function hd {
62		hexdump -e '"%08.8_ax  " 8/1 "%02X " " - " 8/1 "%02X "' \
63		    -e '"  |" "%_p"' -e '"|\n"' "$@"
64	}
65else
66	function hd {
67		\typeset -Uui16 -Z11 pos=0
68		\typeset -Uui16 -Z5 hv=2147483647
69		\typeset dasc line i
70		\set +U
71
72		\cat "$@" | if \read -arN -1 line; then
73			\typeset -i1 'line[*]'
74			i=0
75			while (( i < ${#line[*]} )); do
76				hv=${line[i++]}
77				if (( (pos & 15) == 0 )); then
78					(( pos )) && \
79					    \builtin print -r -- "$dasc|"
80					\builtin print -n "${pos#16#}  "
81					dasc=' |'
82				fi
83				\builtin print -n "${hv#16#} "
84				#XXX EBCDIC, but we need [[:print:]] to fix this
85				if (( (hv < 32) || (hv > 126) )); then
86					dasc+=.
87				else
88					dasc+=${line[i-1]#1#}
89				fi
90				(( (pos++ & 15) == 7 )) && \
91				    \builtin print -n -- '- '
92			done
93			while (( pos & 15 )); do
94				\builtin print -n '   '
95				(( (pos++ & 15) == 7 )) && \
96				    \builtin print -n -- '- '
97			done
98			(( hv == 2147483647 )) || \builtin print -r -- "$dasc|"
99		fi
100	}
101fi
102
103# Berkeley C shell compatible dirs, popd, and pushd functions
104# Z shell compatible chpwd() hook, used to update DIRSTACK[0]
105DIRSTACKBASE=$(\builtin realpath ~/. 2>/dev/null || \
106    \builtin print -nr -- "${HOME:-/}")
107set -A DIRSTACK
108function chpwd {
109	DIRSTACK[0]=$(\builtin realpath . 2>/dev/null || \
110	    \builtin print -r -- "$PWD")
111	[[ $DIRSTACKBASE = ?(*/) ]] || \
112	    DIRSTACK[0]=${DIRSTACK[0]/#$DIRSTACKBASE/\~}
113	\:
114}
115\chpwd .
116cd() {
117	\builtin cd "$@" || \return $?
118	\chpwd "$@"
119}
120function cd_csh {
121	\typeset d t=${1/#\~/$DIRSTACKBASE}
122
123	if ! d=$(\builtin cd "$t" 2>&1); then
124		\builtin print -u2 "${1}: ${d##*cd: $t: }."
125		\return 1
126	fi
127	\cd "$t"
128}
129function dirs {
130	\typeset d dwidth
131	\typeset -i fl=0 fv=0 fn=0 cpos=0
132
133	while \getopts ":lvn" d; do
134		case $d {
135		(l)	fl=1 ;;
136		(v)	fv=1 ;;
137		(n)	fn=1 ;;
138		(*)	\builtin print -u2 'Usage: dirs [-lvn].'
139			\return 1 ;;
140		}
141	done
142	\shift $((OPTIND - 1))
143	if (( $# > 0 )); then
144		\builtin print -u2 'Usage: dirs [-lvn].'
145		\return 1
146	fi
147	if (( fv )); then
148		fv=0
149		while (( fv < ${#DIRSTACK[*]} )); do
150			d=${DIRSTACK[fv]}
151			(( fl )) && d=${d/#\~/$DIRSTACKBASE}
152			\builtin print -r -- "$fv	$d"
153			\builtin let fv++
154		done
155	else
156		fv=0
157		while (( fv < ${#DIRSTACK[*]} )); do
158			d=${DIRSTACK[fv]}
159			(( fl )) && d=${d/#\~/$DIRSTACKBASE}
160			(( dwidth = (${%d} > 0 ? ${%d} : ${#d}) ))
161			if (( fn && (cpos += dwidth + 1) >= 79 && \
162			    dwidth < 80 )); then
163				\builtin print
164				(( cpos = dwidth + 1 ))
165			fi
166			\builtin print -nr -- "$d "
167			\builtin let fv++
168		done
169		\builtin print
170	fi
171	\return 0
172}
173function popd {
174	\typeset d fa
175	\typeset -i n=1
176
177	while \getopts ":0123456789lvn" d; do
178		case $d {
179		(l|v|n)	fa+=" -$d" ;;
180		(+*)	n=2
181			\break ;;
182		(*)	\builtin print -u2 'Usage: popd [-lvn] [+<n>].'
183			\return 1 ;;
184		}
185	done
186	\shift $((OPTIND - n))
187	n=0
188	if (( $# > 1 )); then
189		\builtin print -u2 popd: Too many arguments.
190		\return 1
191	elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
192		if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
193			\builtin print -u2 popd: Directory stack not that deep.
194			\return 1
195		fi
196	elif [[ -n $1 ]]; then
197		\builtin print -u2 popd: Bad directory.
198		\return 1
199	fi
200	if (( ${#DIRSTACK[*]} < 2 )); then
201		\builtin print -u2 popd: Directory stack empty.
202		\return 1
203	fi
204	\unset DIRSTACK[n]
205	\set -A DIRSTACK -- "${DIRSTACK[@]}"
206	\cd_csh "${DIRSTACK[0]}" || \return 1
207	\dirs $fa
208}
209function pushd {
210	\typeset d fa
211	\typeset -i n=1
212
213	while \getopts ":0123456789lvn" d; do
214		case $d {
215		(l|v|n)	fa+=" -$d" ;;
216		(+*)	n=2
217			\break ;;
218		(*)	\builtin print -u2 'Usage: pushd [-lvn] [<dir>|+<n>].'
219			\return 1 ;;
220		}
221	done
222	\shift $((OPTIND - n))
223	if (( $# == 0 )); then
224		if (( ${#DIRSTACK[*]} < 2 )); then
225			\builtin print -u2 pushd: No other directory.
226			\return 1
227		fi
228		d=${DIRSTACK[1]}
229		DIRSTACK[1]=${DIRSTACK[0]}
230		\cd_csh "$d" || \return 1
231	elif (( $# > 1 )); then
232		\builtin print -u2 pushd: Too many arguments.
233		\return 1
234	elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
235		if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
236			\builtin print -u2 pushd: Directory stack not that deep.
237			\return 1
238		fi
239		while (( n-- )); do
240			d=${DIRSTACK[0]}
241			\unset DIRSTACK[0]
242			\set -A DIRSTACK -- "${DIRSTACK[@]}" "$d"
243		done
244		\cd_csh "${DIRSTACK[0]}" || \return 1
245	else
246		\set -A DIRSTACK -- placeholder "${DIRSTACK[@]}"
247		\cd_csh "$1" || \return 1
248	fi
249	\dirs $fa
250}
251
252# pager (not control character safe)
253function smores {
254	(
255		\set +m
256		\cat "$@" |&
257		\trap "rv=\$?; 'kill' $! >/dev/null 2>&1; 'exit' \$rv" EXIT
258		while IFS= \read -pr line; do
259			llen=${%line}
260			(( llen == -1 )) && llen=${#line}
261			(( llen = llen ? (llen + COLUMNS - 1) / COLUMNS : 1 ))
262			if (( (curlin += llen) >= LINES )); then
263				\builtin print -n -- '\e[7m--more--\e[0m'
264				\read -u1 || \exit $?
265				[[ $REPLY = [Qq]* ]] && \exit 0
266				curlin=$llen
267			fi
268			\builtin print -r -- "$line"
269		done
270	)
271}
272
273# base64 encoder and decoder, RFC compliant, NUL safe, not EBCDIC safe
274function Lb64decode {
275	\set +U
276	\typeset c s="$*" t
277	[[ -n $s ]] || { s=$(\cat; \builtin print x); s=${s%x}; }
278	\typeset -i i=0 j=0 n=${#s} p=0 v x
279	\typeset -i16 o
280
281	while (( i < n )); do
282		c=${s:(i++):1}
283		case $c {
284		(=)	\break ;;
285		([A-Z])	(( v = 1#$c - 65 )) ;;
286		([a-z])	(( v = 1#$c - 71 )) ;;
287		([0-9])	(( v = 1#$c + 4 )) ;;
288		(+)	v=62 ;;
289		(/)	v=63 ;;
290		(*)	\continue ;;
291		}
292		(( x = (x << 6) | v ))
293		case $((p++)) {
294		(0)	\continue ;;
295		(1)	(( o = (x >> 4) & 255 )) ;;
296		(2)	(( o = (x >> 2) & 255 )) ;;
297		(3)	(( o = x & 255 ))
298			p=0
299			;;
300		}
301		t+=\\x${o#16#}
302		(( ++j & 4095 )) && \continue
303		\builtin print -n $t
304		t=
305	done
306	\builtin print -n $t
307}
308
309\set -A Lb64encode_tbl -- 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 \
310    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 + /
311function Lb64encode {
312	\set +U
313	\typeset c s t
314	if (( $# )); then
315		\read -raN-1 s <<<"$*"
316		\unset s[${#s[*]}-1]
317	else
318		\read -raN-1 s
319	fi
320	\typeset -i i=0 n=${#s[*]} j v
321
322	while (( i < n )); do
323		(( v = s[i++] << 16 ))
324		(( j = i < n ? s[i++] : 0 ))
325		(( v |= j << 8 ))
326		(( j = i < n ? s[i++] : 0 ))
327		(( v |= j ))
328		t+=${Lb64encode_tbl[v >> 18]}${Lb64encode_tbl[v >> 12 & 63]}
329		c=${Lb64encode_tbl[v >> 6 & 63]}
330		if (( i <= n )); then
331			t+=$c${Lb64encode_tbl[v & 63]}
332		elif (( i == n + 1 )); then
333			t+=$c=
334		else
335			t+===
336		fi
337		if (( ${#t} == 76 || i >= n )); then
338			\builtin print $t
339			t=
340		fi
341	done
342}
343
344# Better Avalanche for the Jenkins Hash
345\typeset -Z11 -Uui16 Lbafh_v
346function Lbafh_init {
347	Lbafh_v=0
348}
349function Lbafh_add {
350	\set +U
351	\typeset s
352	if (( $# )); then
353		\read -raN-1 s <<<"$*"
354		\unset s[${#s[*]}-1]
355	else
356		\read -raN-1 s
357	fi
358	\typeset -i i=0 n=${#s[*]}
359
360	while (( i < n )); do
361		((# Lbafh_v = (Lbafh_v + s[i++] + 1) * 1025 ))
362		((# Lbafh_v ^= Lbafh_v >> 6 ))
363	done
364}
365function Lbafh_finish {
366	\typeset -Ui t
367
368	((# t = (((Lbafh_v >> 7) & 0x01010101) * 0x1B) ^ \
369	    ((Lbafh_v << 1) & 0xFEFEFEFE) ))
370	((# Lbafh_v = t ^ (t >>> 8) ^ (Lbafh_v >>> 8) ^ \
371	    (Lbafh_v >>> 16) ^ (Lbafh_v >>> 24) ))
372	\:
373}
374
375# strip comments (and leading/trailing whitespace if IFS is set) from
376# any file(s) given as argument, or stdin if none, and spew to stdout
377function Lstripcom {
378	\set -o noglob
379	\cat "$@" | while \read _line; do
380		_line=${_line%%#*}
381		[[ -n $_line ]] && \builtin print -r -- $_line
382	done
383}
384
385# give MidnightBSD's laffer1 a bit of csh feeling
386function setenv {
387	if (( $# )); then
388		\eval '\export "$1"="${2:-}"'
389	else
390		\typeset -x
391	fi
392}
393
394# toggle built-in aliases and utilities, and aliases and functions from mkshrc
395function enable {
396	\typeset doprnt=0 mode=1 x y z rv=0
397	\typeset b_alias i_alias i_func nalias=0 nfunc=0 i_all
398	\set -A b_alias
399	\set -A i_alias
400	\set -A i_func
401
402	# accumulate mksh built-in aliases, in ASCIIbetical order
403	i_alias[nalias]=autoload; b_alias[nalias++]='\typeset -fu'
404	i_alias[nalias]=functions; b_alias[nalias++]='\typeset -f'
405	i_alias[nalias]=hash; b_alias[nalias++]='\builtin alias -t'
406	i_alias[nalias]=history; b_alias[nalias++]='\builtin fc -l'
407	i_alias[nalias]=integer; b_alias[nalias++]='\typeset -i'
408	i_alias[nalias]=local; b_alias[nalias++]='\typeset'
409	i_alias[nalias]=login; b_alias[nalias++]='\exec login'
410	i_alias[nalias]=nameref; b_alias[nalias++]='\typeset -n'
411	i_alias[nalias]=nohup; b_alias[nalias++]='nohup '
412	i_alias[nalias]=r; b_alias[nalias++]='\builtin fc -e -'
413	i_alias[nalias]=type; b_alias[nalias++]='\builtin whence -v'
414
415	# accumulate mksh built-in utilities, in definition order, even ifndef
416	i_func[nfunc++]=.
417	i_func[nfunc++]=:
418	i_func[nfunc++]='['
419	i_func[nfunc++]=alias
420	i_func[nfunc++]=break
421	i_func[nfunc++]=builtin
422	i_func[nfunc++]=cat
423	i_func[nfunc++]=cd
424	i_func[nfunc++]=chdir
425	i_func[nfunc++]=command
426	i_func[nfunc++]=continue
427	i_func[nfunc++]=echo
428	i_func[nfunc++]=eval
429	i_func[nfunc++]=exec
430	i_func[nfunc++]=exit
431	i_func[nfunc++]=export
432	i_func[nfunc++]=false
433	i_func[nfunc++]=fc
434	i_func[nfunc++]=getopts
435	i_func[nfunc++]=global
436	i_func[nfunc++]=jobs
437	i_func[nfunc++]=kill
438	i_func[nfunc++]=let
439	i_func[nfunc++]='let]'
440	i_func[nfunc++]=print
441	i_func[nfunc++]=pwd
442	i_func[nfunc++]=read
443	i_func[nfunc++]=readonly
444	i_func[nfunc++]=realpath
445	i_func[nfunc++]=rename
446	i_func[nfunc++]=return
447	i_func[nfunc++]=set
448	i_func[nfunc++]=shift
449	i_func[nfunc++]=source
450	i_func[nfunc++]=suspend
451	i_func[nfunc++]=test
452	i_func[nfunc++]=times
453	i_func[nfunc++]=trap
454	i_func[nfunc++]=true
455	i_func[nfunc++]=typeset
456	i_func[nfunc++]=ulimit
457	i_func[nfunc++]=umask
458	i_func[nfunc++]=unalias
459	i_func[nfunc++]=unset
460	i_func[nfunc++]=wait
461	i_func[nfunc++]=whence
462	i_func[nfunc++]=bg
463	i_func[nfunc++]=fg
464	i_func[nfunc++]=bind
465	i_func[nfunc++]=mknod
466	i_func[nfunc++]=printf
467	i_func[nfunc++]=sleep
468	i_func[nfunc++]=domainname
469	i_func[nfunc++]=extproc
470
471	# accumulate aliases from dot.mkshrc, in definition order
472	i_alias[nalias]=l; b_alias[nalias++]='ls -F'
473	i_alias[nalias]=la; b_alias[nalias++]='l -a'
474	i_alias[nalias]=ll; b_alias[nalias++]='l -l'
475	i_alias[nalias]=lo; b_alias[nalias++]='l -alo'
476	i_alias[nalias]=doch; b_alias[nalias++]='sudo mksh -c "$(\builtin fc -ln -1)"'
477	i_alias[nalias]=rot13; b_alias[nalias++]='tr abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
478	i_alias[nalias]=cls; b_alias[nalias++]='\builtin print -n \\ec'
479
480	# accumulate functions from dot.mkshrc, in definition order
481	i_func[nfunc++]=hd
482	i_func[nfunc++]=chpwd
483	i_func[nfunc++]=cd_csh
484	i_func[nfunc++]=dirs
485	i_func[nfunc++]=popd
486	i_func[nfunc++]=pushd
487	i_func[nfunc++]=smores
488	i_func[nfunc++]=Lb64decode
489	i_func[nfunc++]=Lb64encode
490	i_func[nfunc++]=Lbafh_init
491	i_func[nfunc++]=Lbafh_add
492	i_func[nfunc++]=Lbafh_finish
493	i_func[nfunc++]=Lstripcom
494	i_func[nfunc++]=setenv
495	i_func[nfunc++]=enable
496
497	# collect all identifiers, sorted ASCIIbetically
498	\set -sA i_all -- "${i_alias[@]}" "${i_func[@]}"
499
500	# handle options, we don't do dynamic loading
501	while \getopts "adf:nps" x; do
502		case $x {
503		(a)
504			mode=-1
505			;;
506		(d)
507			# deliberately causing an error, like bash-static
508			;|
509		(f)
510			\builtin print -u2 enable: dynamic loading not available
511			\return 2
512			;;
513		(n)
514			mode=0
515			;;
516		(p)
517			doprnt=1
518			;;
519		(s)
520			\set -sA i_all -- . : break continue eval exec exit \
521			    export readonly return set shift times trap unset
522			;;
523		(*)
524			\builtin print -u2 enable: usage: \
525			    "enable [-adnps] [-f filename] [name ...]"
526			return 2
527			;;
528		}
529	done
530	\shift $((OPTIND - 1))
531
532	# display builtins enabled/disabled/all/special?
533	if (( doprnt || ($# == 0) )); then
534		for x in "${i_all[@]}"; do
535			y=$(\alias "$x") || y=
536			[[ $y = "$x='\\builtin whence -p $x >/dev/null || (\\builtin print mksh: $x: not found; exit 127) && \$(\\builtin whence -p $x)'" ]]; z=$?
537			case $mode:$z {
538			(-1:0|0:0)
539				\print -r -- "enable -n $x"
540				;;
541			(-1:1|1:1)
542				\print -r -- "enable $x"
543				;;
544			}
545		done
546		\return 0
547	fi
548
549	for x in "$@"; do
550		z=0
551		for y in "${i_alias[@]}" "${i_func[@]}"; do
552			[[ $x = "$y" ]] || \continue
553			z=1
554			\break
555		done
556		if (( !z )); then
557			\builtin print -ru2 enable: "$x": not a shell builtin
558			rv=1
559			\continue
560		fi
561		if (( !mode )); then
562			# disable this
563			\alias "$x=\\builtin whence -p $x >/dev/null || (\\builtin print mksh: $x: not found; exit 127) && \$(\\builtin whence -p $x)"
564		else
565			# find out if this is an alias or not, first
566			z=0
567			y=-1
568			while (( ++y < nalias )); do
569				[[ $x = "${i_alias[y]}" ]] || \continue
570				z=1
571				\break
572			done
573			if (( z )); then
574				# re-enable the original alias body
575				\alias "$x=${b_alias[y]}"
576			else
577				# re-enable the original utility/function
578				\unalias "$x"
579			fi
580		fi
581	done
582	\return $rv
583}
584
585\: place customisations below this line
586
587for p in ~/.etc/bin ~/bin; do
588	[[ -d $p/. ]] || \continue
589	#XXX OS/2
590	[[ :$PATH: = *:$p:* ]] || PATH=$p:$PATH
591done
592
593\export SHELL=$MKSH MANWIDTH=80 LESSHISTFILE=-
594\alias cls='\builtin print -n \\ec'
595
596#\unset LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_IDENTIFICATION LC_MONETARY \
597#    LC_NAME LC_NUMERIC LC_TELEPHONE LC_TIME
598#p=en_GB.UTF-8
599#\export LANG=C LC_CTYPE=$p LC_MEASUREMENT=$p LC_MESSAGES=$p LC_PAPER=$p
600#\set -U
601
602\unset p
603
604\: place customisations above this line
605