summaryrefslogtreecommitdiff
path: root/.kshrc
blob: 9eac123d8abc772dafb9cdd144a6775889f22638 (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
# sh/ksh initialization

[ -f $HOME/.kshrc.private ] && . $HOME/.kshrc.private

#############################################################################
# ALIASES
#############################################################################

if command -v colorls > /dev/null ; then
	LS='colorls'
	export CLICOLOR=1
  else
	LS='ls'
fi

if [[ $(uname -s) == "Linux" ]]; then
	alias ls='ls --color=auto -h --file-type -s'
	alias ll='ls -l'
	alias rm='rm --preserve-root --one-file-system -I'
	alias cps='sync ; cvs up ; sync'
	if [ -x /usr/bin/dircolors ]; then
		test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
	fi
	bind -m '^L'=clear'^J'
elif [[ $(uname -s) == "OpenBSD" ]]; then
	alias ls='$LS'
	alias ll='$LS -Flho'
	alias cps='sync ; opencvs up ; sync'

	bind '^L'=clear-screen

	# Enable SIGINFO with ^T
	stty status ^T
elif [[ $(uname -s) == "FreeBSD" ]]; then
	alias ls='$LS -G'
	alias ll='$LS -GFlho'

	bind '^L'=clear-screen

	# Enable SIGINFO with ^T
	stty status ^T

	export TERM=xterm-256color
fi

alias j='jump'
alias c='cvs'
alias h='history -60 | sort -k2 | uniq -f2 | sort -bn'
alias sudo='sudo -H'
alias mc='mc --color'
alias pwgen='pwgen -s'
alias pkg_add='pkg_add -V'
alias mpv='mpv --no-audio-display --audio-channels=stereo --geometry=50%+950+200 --sub-scale=0.7'
alias dt='dtoggle'
alias tty-clock='tty-clock -s -c'
alias chromium='chromium --disk-cache-dir=/tmp'
alias open="xdg-open"
alias ffplay='ffplay -hide_banner'
alias gps='sync ; git pull ; sync'
alias cal='cal -m -w'
alias ed='ed -p*'
alias qw='pkill -9 xidle'
alias cc='cc -fdiagnostics-color'
alias weather='curl http://wttr.in/Karlsruhe'

#############################################################################
# FUNCTIONS
#############################################################################

# fzf stuff is from gemini://perso.pw/blog/articles/ksh-fzf.gmi
function fzf-histo {
    RES=$(fzf --tac --no-sort -e < $HISTFILE)
    test -n "$RES" || exit 0
    eval "$RES"
}
bind -m ^R=fzf-histo^J

# Following three functions from "shell FU by Isaac Levy" presentation
yell() { echo "$0: $*" >&2; }
die()  { yell "$*"; exit 111; }
try()  { "$@" || die "cannot $*"; }

# Neat trick from https://github.com/lf94/peek-for-tmux/blob/master/README.md
p() {
	tmux split-window -p 33 more $@ || exit;
}

g() {
	if [ -d ".got" ]; then
		got "$@"
	else
		git "$@"
	fi
}

checklatestsnap() {
	ftp -MVo- ftp://[fd00:23:42:5:20d:b9ff:fe49:71ec]/pub/OpenBSD/snapshots/$(uname -m)/BUILDINFO
	ftp -MVo- "$(egrep -m 1 "^(ftp|http|https)" /etc/installurl)/snapshots/$(uname -m)/BUILDINFO"
	ftp -MVo- http://ftp.openbsd.org/pub/OpenBSD/snapshots/$(uname -m)/BUILDINFO
}

lostandfoundcheck() {
	for mp in $(grep ffs /etc/fstab | cut -d ' ' -f 2); do
		[[ -d ${mp}/lost+found ]] && echo "${mp}/lost+found exists"
	done
}

updatesrc() {
	if [ ! -d "/var/git" ]; then
		local _oldpwd="$PWD"
		cd /usr/src && {
			sync && git pull ; sync
		}
		cd /usr/ports && {
			sync && git pull ; sync
		}
		cd "$_oldpwd"
	else
		local _oldpwd="$PWD"
		cd /var/git/src.git && {
			sync && got fetch; sync
		}
		cd /usr/src && {
			sync && got up && sync
		}
		cd /var/git/ports.git && {
			sync && got fetch; sync
		}
		cd /usr/ports && {
			sync && got up && sync
		}
		cd "$_oldpwd"
	fi
}

updatepkgs() {
	local _option="" _args=$1
	if [[ -n $(sysctl -n kern.version | cut -d ' ' -f2 | grep beta) ]]; then
		_option="-D snap"
	fi

	sync && doas pkg_add -ui $_option $_args
	sync

	return

	if [ ! -d /var/pkg_mirror ]; then
		doas mkdir /var/pkg_mirror
	fi
	doas mount_nfs -s -a 4 -T -r 32768 -w 32768 -o noatime,ro,intr mx.xosc.net:/pkg /var/pkg_mirror
	doas umount /var/pkg_mirror/
}

getbsdrd() {
	local _mirror="$(egrep -m 1 "^(ftp|http|https)" /etc/installurl)/snapshots/$(uname -m)"

	ftp -V -o /tmp/bsd.rd "$_mirror/bsd.rd" || return 1
	ftp -V -o /tmp/SHA256.sig "$_mirror/SHA256.sig" || return 1

	cd /tmp && signify -C -p "/etc/signify/openbsd-$(uname -r | tr -d '.')-base.pub" -x /tmp/SHA256.sig bsd.rd
}

getbsdvm() {
	local _mirror="$(egrep -m 1 "^(ftp|http|https)" /etc/installurl)/snapshots/$(uname -m)"

	ftp -V -o /tmp/bsd "$_mirror/bsd" || return 1
	ftp -V -o /tmp/SHA256.sig "$_mirror/SHA256.sig" || return 1

	cd /tmp && signify -C -p "/etc/signify/openbsd-$(uname -r | tr -d '.')-base.pub" -x /tmp/SHA256.sig bsd && doas mv /tmp/bsd /bsd.vm
}

openports() {
	local _filter1='internet'
	local _filter2='icmp|raw|<-|->'

	if [ "$1" = "-4" ]; then
		_filter2="internet6|${_filter2}"
	elif [ "$1" = "-6" ]; then
		_filter1="internet6"
	fi

	printf "%-10s %-13s %-5s %-5s %-9s %s\n" "USER" "COMMAND" "PID" "PROTO" "FAMILY" "LOCAL ADDRESS"
	fstat | grep "${_filter1}" | grep -vE "${_filter2}" | awk '{ printf("%-10s %-13s %5s %-5s %-9s %s\n", $1,$2,$3,$7,$5,$9); }' | sort -u
}

psearch() {
        local pt="/usr/ports"

        [[ -z ${1} ]] && echo "Need a keyword to search for" && return
        [[ ! -d ${pt} ]] && echo "Cannot find ports tree" && return

        cd ${pt} && make search name=${1}
}

sshopen() {
		# The following is only needed on WSL not on other OSes
		[ -f "/proc/version" ] && grep -qE "(Microsoft|WSL)" /proc/version 2> /dev/null
		if [ $? -eq 0 ]; then
			local AGPATH="$HOME/.ssh/$(hostname).agent"

			[[ -f ${AGPATH} ]] && rm -f $AGPATH
			command ssh-agent -t 345600 | grep -v echo > $AGPATH
			. $AGPATH
		fi

        # Find all public keys...
        for i in $(find $HOME/.ssh/ -maxdepth 1 -name "*.pub"); do
                # ... and strip the .pub suffix
                _key=`echo $i | sed -e 's/\.pub//'`
                command ssh-add $_key
        done
}

cvspsdiff() {
	[ -z "$1" ] && return
	cvsps -q -s "$1" -g | cdiff
}

psg() {
	ps aux | grep "$@" | grep -v "grep $@"
}

qrcodegen() {
	# Idea from https://dataswamp.org/~solene/2021-03-25-computer-to-phone-text.html
	xclip -o | qrencode -o - > ~/qrclip.png && sxiv -g 600x600 ~/qrclip.png && rm ~/qrclip.png
}

cds() {
	test -z "$1" && cd $HOME && ls -lh && return
	cd "$1" && ls -lah
}

pkg_search() {
	pkglocate "$1" | cut -d ':' -f 1 | sort -u
}

mkcd() {
	[[ -n $1 ]] || return 0
	[[ -d $1 ]] || mkdir -p "$1"
	[[ -d $1 ]] && builtin cd "$1"
}

cget() {
	curl -OL --compressed "$@"
}

showwifi() {
	ifconfig | grep ieee | awk {'print $3'}
}

# Show infos about my external IP address
showmyipaddress() {
	echo "My external IPv4 : $(ftp -4 -M -o - http://icanhazip.com 2> /dev/null)"
	echo "My external IPv6 : $(ftp -6 -M -o - http://icanhazip.com 2> /dev/null)"
	echo "My external PTR  : $(ftp -M -o - http://icanhazptr.com 2> /dev/null)"
}

calc() {
	echo "scale=3;$@" | bc -l
}

# Copied from https://github.com/agkozak/polyglot/ MIT license
_polyglot_branch_status() {
  POLYGLOT_REF="$(env git symbolic-ref --quiet HEAD 2> /dev/null)"
  case $? in        # See what the exit code is.
    0) ;;           # $POLYGLOT_REF contains the name of a checked-out branch.
    128) return ;;  # No Git repository here.
    # Otherwise, see if HEAD is in a detached state.
    *) POLYGLOT_REF="$(env git rev-parse --short HEAD 2> /dev/null)" || return ;;
  esac

  if [ -n "$POLYGLOT_REF" ]; then
    if [ "${POLYGLOT_SHOW_UNTRACKED:-1}" -eq 0 ]; then
      POLYGLOT_GIT_STATUS=$(LC_ALL=C GIT_OPTIONAL_LOCKS=0 env git status -uno 2>&1)
    else
      POLYGLOT_GIT_STATUS=$(LC_ALL=C GIT_OPTIONAL_LOCKS=0 env git status 2>&1)
    fi

    POLYGLOT_SYMBOLS=''

    case $POLYGLOT_GIT_STATUS in
      *' have diverged,'*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}&*" ;;
    esac
    case $POLYGLOT_GIT_STATUS in
      *'Your branch is behind '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}&" ;;
    esac
    case $POLYGLOT_GIT_STATUS in
      *'Your branch is ahead of '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}*" ;;
    esac
    case $POLYGLOT_GIT_STATUS in
      *'new file:   '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}+" ;;
    esac
    case $POLYGLOT_GIT_STATUS in
      *'deleted:    '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}x" ;;
    esac
    case $POLYGLOT_GIT_STATUS in
      *'modified:   '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}!!" ;;
    esac
    case $POLYGLOT_GIT_STATUS in
      *'renamed:    '*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}->" ;;
    esac
    case $POLYGLOT_GIT_STATUS in
      *'Untracked files:'*) POLYGLOT_SYMBOLS="${POLYGLOT_SYMBOLS}?" ;;
    esac

    [ -n "$POLYGLOT_SYMBOLS" ] && POLYGLOT_SYMBOLS=" $POLYGLOT_SYMBOLS"

    printf ' (%s%s)' "${POLYGLOT_REF#refs/heads/}" "$POLYGLOT_SYMBOLS"
  fi

  unset POLYGLOT_REF POLYGLOT_GIT_STATUS POLYGLOT_SYMBOLS
}

lpf() {
	local _pf=${1:-/etc/pf.conf}
	doas pfctl -n -f ${_pf} && doas pfctl -F rules && doas pfctl -f ${_pf}
}

# Disable history logging for this shell
nohistory() {
	HISTFILE=/dev/null
}

tog() {
	TOG_COLORS=1 TERM=xterm /usr/local/bin/tog "$@"
}

# http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html

export MARKPATH=$HOME/.marks
jump() {
    cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
}
mark() {
    mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}
unmark() {
    rm -i "$MARKPATH/$1"
}
marks() {
    ls -l "$MARKPATH" | sed 's/  / /g' | cut -d' ' -f9- | sed 's/ -/\t-/g' && echo
}

#############################################################################
# COMPLETIONS
#############################################################################

# Mostly copied from
# https://github.com/qbit/dotfiles/blob/master/common/dot_ksh_completions

if [ -d ~/.password-store ]; then
	PASS_LIST=$(
		cd ~/.password-store
		find . -type f -name \*.gpg | sed 's/^\.\///' | sed 's/\.gpg$//g'
	)

	set -A complete_tpm_1 -- $PASS_LIST usage
	set -A complete_tpm_2 -- $PASS_LIST edit insert show rm
fi

set -A complete_kill_1 -- -9 -HUP -INFO -KILL -TERM

set -A complete_ifconfig_1 -- $(ifconfig | grep ^[a-z] | cut -d: -f1)

if [ -d /var/db/pkg ]; then
	PKG_LIST=$(/bin/ls -1 /var/db/pkg)
	set -A complete_pkg_info -- $PKG_LIST

	alias dpkgdel="doas pkg_delete"
	set -A complete_dpkgdel_1 -- $PKG_LIST
fi

# relayctl completion.  Second level only for 'show'
set -A complete_relayctl_1 -- monitor show load poll reload stop redirect table host log
set -A complete_relayctl_2 -- summary hosts redirects relays routers sessions

set -A complete_unwindctl_1 -- reload log status

if [ -d /etc/rc.d ]; then
	RCD_LIST=$(/bin/ls /etc/rc.d)
	set -A complete_rcctl_1 -- get getdef set check reload restart stop start disable enable order ls
	set -A complete_rcctl_2 -- $RCD_LIST

	alias drcctl="doas rcctl"
	set -A complete_drcctl_1 -- get getdef set check reload restart stop start disable enable order ls
	set -A complete_drcctl_2 -- $RCD_LIST
fi

set -A complete_tarsnap_1 -- --list-archives --print-stats --fsck --fsck-prune --nuke --verify-config --version --checkpoint-bytes --configfile --dry-run --exclude --humanize-numbers --keyfile --totals

# /tmp/.man-list is generated upon boot by /etc/rc.local with
# find /usr/share/man/ -type f | sed -e 's/.*\///' -e 's/\.[0-9]//' | sort -u
[[ -f /tmp/.man-list ]] && set -A complete_man -- $(cat /tmp/.man-list)

[[ -d $HOME/.marks ]] && set -A complete_j -- $(/bin/ls $HOME/.marks)

#############################################################################
# PROMPT
#############################################################################



if [ -z "$SSH_CLIENT" ]; then
	PS1_TRENNER="!!"
else
	PS1_TRENNER="@"
fi

if [[ $(id -u) -eq 0 ]]; then
	PS1='\h$PS1_TRENNER\\033[0;101m\u\\033[0m \w [$?]\n\$ '
else
	PS1='\h$PS1_TRENNER\u \w$(_polyglot_branch_status) [$?]\n\$ '
fi

#############################################################################
# MISC SETTINGS
#############################################################################

ulimit -c 0
set bell-style none

#############################################################################
# VARIABLES
#############################################################################

LSCOLORS=Dxfxcxdxbxegedabagacad
HISTSIZE=20000
HISTFILE=$HOME/.sh_history
HISTCONTROL=ignoredups:ignorespace
BLOCKSIZE=M
PATH=$HOME/Documents/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:/usr/games
LESSSECURE=1
PAGER='less -JWAceX'
LESS='-Xa'
FETCH_CMD="/usr/local/bin/curl -Ns"
PKG_PATH=http://172.23.5.31/pub/OpenBSD/%c/packages/%a:http://ftp.hostserver.de/pub/OpenBSD/%c/packages/%a
export PATH HOME TERM LSCOLORS HISTSIZE BLOCKSIZE PAGER LESSSECURE PKG_PATH FETCH_CMD
generated by cgit on OpenBSD