Initial commit

This commit is contained in:
styan 2020-06-10 08:16:08 +00:00
commit d6116f15c3
5 changed files with 360 additions and 0 deletions

45
Makefile Normal file
View File

@ -0,0 +1,45 @@
.POSIX:
default:
all:
install:\
install-baseenc\
install-basedec\
install-gmi2html\
install-tac
install-baseenc:
install -m755 baseenc.sh\
"$$BASEDIR$${PREFIX-/usr/local}/bin/base64enc"
ln -f -- "$$BASEDIR$${PREFIX-/usr/local}/bin/base64enc"\
"$$BASEDIR$${PREFIX-/usr/local}/bin/base32enc"
ln -f -- "$$BASEDIR$${PREFIX-/usr/local}/bin/base64enc"\
"$$BASEDIR$${PREFIX-/usr/local}/bin/base16enc"
install-basedec:
install -m755 basedec.sh\
"$$BASEDIR$${PREFIX-/usr/local}/bin/base64dec"
ln -f -- "$$BASEDIR$${PREFIX-/usr/local}/bin/base64dec"\
"$$BASEDIR$${PREFIX-/usr/local}/bin/base32dec"
ln -f -- "$$BASEDIR$${PREFIX-/usr/local}/bin/base64dec"\
"$$BASEDIR$${PREFIX-/usr/local}/bin/base16dec"
install-gmi2html:
install -m755 gmi2html.sh\
"$$BASEDIR$${PREFIX-/usr/local}/bin/gmi2html"
install-tac:
install -m755 tac.sh "$$BASEDIR$${PREFIX-/usr/local}/bin/tac"
linstall:\
linstall-baseenc\
linstall-basedec\
linstall-gmi2html\
linstall-tac
linstall-baseenc:
install -m755 baseenc.sh "$$HOME/bin/base64enc"
ln -f -- "$$HOME/bin/base64enc" "$$HOME/bin/base32enc"
ln -f -- "$$HOME/bin/base64enc" "$$HOME/bin/base16enc"
linstall-basedec:
install -m755 basedec.sh "$$HOME/bin/base64dec"
ln -f -- "$$HOME/bin/base64dec" "$$HOME/bin/base32dec"
ln -f -- "$$HOME/bin/base64dec" "$$HOME/bin/base16dec"
linstall-gmi2html:
install -m755 gmi2html.sh "$$HOME/bin/gmi2html"
linstall-tac:
install -m755 tac.sh "$$HOME/bin/tac"
clean:

86
basedec.sh Normal file
View File

@ -0,0 +1,86 @@
#!/bin/sh
usage() {
printf 'usage: %s [-t type] [file ...]\n' "${0##*/}" >&2
exit 64
}
case "$(printf %s "${0##*/}" | tr A-Z a-z)" in
*16*) type=16;;
*32hex*|*32x*)
type=32x;;
*32*) type=32;;
*64s*) type=64s;;
*64*) type=64;;
*) type=64;;
esac
b16=0123456789ABCDEF
b32=ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
b32hex=0123456789ABCDEFGHIJKLMNOPQRSTUV
b64=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
b64safe=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
while getopts t: opt; do
case "$opt" in
t) case "$(printf %s "$OPTARG" | tr A-Z a-z)" in
16) type=16;;
32) type=32;;
32x) type=32x;;
64) type=64;;
64s|64safe)
type=64s;;
*) usage;;
esac
;;
*) usage;;
esac
done
shift $((OPTIND - 1))
case "$type" in
16) alphabet="$b16"
insensitive=1
bits=4;;
32) alphabet="$b32"
insensitive=1
bits=5;;
32x) alphabet="$b32hex"
insensitive=1
bits=5;;
64) alphabet="$b64"
insensitive=0
bits=6;;
64s) alphabet="$b64safe"
insensitive=0
bits=6;;
*) usage;; # Unreachable
esac
cat -- "$@" | awk -vbits="$bits" -valphabet="$alphabet" \
-vinsensitive="$insensitive" '
function leftshift(a, b) {
return a * 2 ** b
}
function rightshift(a, b) {
return int(a / 2 ** b)
}
function mask(a, b) {
return a > 0 ? a % leftshift(1, b) : a
}
BEGIN {
FS=""
split(alphabet, al, "")
for (n in al)
a[al[n]] = n - 1
if (insensitive)
for (c in a)
a[tolower(c)] = a[c]
} {
for (i = 1; i <= NF; i++) {
if ($i in a) {
t = leftshift(t, bits) + a[$i]
b += bits
while (b >= 8) {
b -= 8
printf "%c", rightshift(t, b)
t = mask(t, b)
count++
}
}
}
}'

112
baseenc.sh Normal file
View File

@ -0,0 +1,112 @@
#!/bin/sh
usage() {
printf 'usage: %s [-np] [-a alphabet] [-t type] [file ...]\n' \
"${0##*/}" >&2
exit 64
}
case "$(printf %s "${0##*/}" | tr A-Z a-z)" in
*16l*|*hexl*)
type=16l;;
*16*) type=16u;;
*32hexl*|*32x*l)
type=32x;;
*32hex*|*32x*)
type=32X;;
*32l*) type=32l;;
*32*) type=32u;;
*64s*) type=64s;;
*) type=64;;
esac
b16upper=0123456789ABCDEF
b16lower=0123456789abcdef
b32upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
b32lower=abcdefghijklmnopqrstuvwxyz234567
b32hexupper=0123456789ABCDEFGHIJKLMNOPQRSTUV
b32hexlower=0123456789abcdefghijklmnopqrstuv
b64=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
b64safe=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
alphabet=
pad=1
nl=1
while getopts a:npt: opt; do
case "$opt" in
a) alphabet="$OPTARG";;
n) nl=0;;
p) pad=0;;
t) case "$(printf %s "$OPTARG" | tr A-WYZ a-wyz)" in
16|16u|16upper)
type=16u;;
16l|16lower)
type=16l;;
32|32u|32upper)
type=32u;;
32l|32lower)
type=32l;;
32X) type=32X;;
32x) type=32x;;
64) type=64;;
64s|64safe)
type=64s;;
*) usage;;
esac
;;
*) usage;;
esac
done
shift $((OPTIND - 1))
case "$type" in
16u) alphabet="${alphabet:-$b16upper}"
bits=4;;
16l) alphabet="${alphabet:-$b16lower}"
bits=4;;
32u) alphabet="${alphabet:-$b32upper}"
bits=5;;
32l) alphabet="${alphabet:-$b32lower}"
bits=5;;
32X) alphabet="${alphabet:-$b32hexupper}"
bits=5;;
32x) alphabet="${alphabet:-$b32hexlower}"
bits=5;;
64) alphabet="${alphabet:-$b64}"
bits=6;;
64s) alphabet="${alphabet:-$b64safe}"
bits=6;;
*) usage;; # Unreachable
esac
od -An -tu1 -v -- "$@" | sed 's/^ *//' | tr -s ' ' '\n' |
sed 's/^00*//' | awk -vbits="$bits" -valphabet="$alphabet" \
-vnl="$nl" -vpad="$pad" '
function leftshift(a, b) {
return a * 2 ** b
}
function rightshift(a, b) {
return int(a / 2 ** b)
}
function mask(a, b) {
return a > 0 ? a % leftshift(1, b) : a
}
BEGIN {
split(alphabet, a, "")
} {
t = leftshift(t, 8) + $0
b += 8
while (b >= bits) {
b -= bits
printf "%s", a[rightshift(t, b) + 1]
t = mask(t, b)
count++
}
} END {
if (b > 0) {
printf "%s", a[leftshift(t, bits - b) + 1]
count++
}
if (pad && bits == 5)
for (count += 0; count % 8 != 0; count++)
printf "="
if (pad && bits == 6)
for (count += 0; count % 4 != 0; count++)
printf "="
if (nl)
print ""
}'

91
gmi2html.sh Normal file
View File

@ -0,0 +1,91 @@
#!/bin/sh
usage() {
echo >&2 \
'usage: gmi2html [-h header] [-p prefix] [-s suffix] [file ...]'
exit 64
}
gmi2html() {
if [ "$prefix" ]; then
printf '%s\n' "$prefix"
fi
awk '
function hescape(s) {
gsub(/&/, "&amp;", s)
gsub(/</, "&lt;", s)
gsub(/>/, "&gt;", s)
gsub(/"/, "&quot;", s)
return s
}
/^```/ {
sub(/^```/, "")
pre = !pre
if (pre)
print "\t<pre alt=\"" hescape($0) "\">"
else
print "\t</pre>"
} !pre && /^=>/ {
u = $2
$1 = ""
$2 = ""
sub(/^[[:space:]]+/, "")
if ($0 !~ /./)
$0 = u
print "\t<a href=\"" hescape(u) "\">" hescape($0) "</a>"
next
} !pre && /^###/ {
sub(/^###[[:space:]]*/, "")
id = tolower($0)
gsub(/[^[:alnum:]]+/, "-", id)
print "\t<h1 id=\"" hescape(id) "\">" \
hescape($0) "</h1>"
next
} !pre && /^##/ {
sub(/^##[[:space:]]*/, "")
id = tolower($0)
gsub(/[^[:alnum:]]+/, "-", id)
print "\t<h2 id=\"" hescape(id) "\">" \
hescape($0) "</h2>"
next
} !pre && /^#/ {
sub(/^#[[:space:]]*/, "")
id = tolower($0)
gsub(/[^[:alnum:]]+/, "-", id)
print "\t<h3 id=\"" hescape(id) "\">" \
hescape($0) "</h3>"
next
} !pre && /^[[:space:]]*$/ {
print "\t<br/>"
next
} {
print hescape($0)
}
END {
if (pre)
print "\t</pre>"
}'
if [ "$suffix" ]; then
printf '%s\n' "$suffix"
fi
}
header='<!doctype html>'
prefix='<html><head></head><body><article><p>'
suffix='</p></article></body></html>'
while getopts h:p:s: opt; do
case "$opt" in
h) header="$OPTARG";;
p) prefix="$OPTARG";;
s) suffix="$OPTARG";;
*) usage;;
esac
done
shift $((OPTIND - 1))
if [ "$header" ]; then
printf '%s\n' "$header"
fi
if [ "$#" -eq 0 ]; then
gmi2html
exit
fi
for f in "$@"; do
gmi2html < "$f"
done

26
tac.sh Normal file
View File

@ -0,0 +1,26 @@
#!/bin/sh
usage() {
echo 'usage: tac [-b] [-s separator] [file ...]' >&2
exit 64
}
bflag=
rs='\n'
while getopts bs: opt; do
case "$opt" in
b) bflag=1;;
s) rs="$OPTARG";;
*) usage;;
esac
done
shift $((OPTIND - 1))
rs="$(printf %s "$rs" | sed 's/[][)(}{.+?*^$]/\\&/g')"
awk -vRS="$rs" -vORS="$rs" -vb="$bflag" '{
a[NR] = $0
} END {
if (b)
for (i = NR; i > 0; i--)
printf "%s%s", ORS, a[i]
else
for (i = NR; i > 0; i--)
printf "%s%s", a[i], ORS
}' "$@"