morsh/morsh

78 lines
2.2 KiB
Plaintext
Raw Normal View History

2021-08-23 18:32:45 +00:00
#!/bin/sh
2021-10-18 08:00:00 +00:00
# -. --- - .. -.-. . / -- . / -- . . .--. . .-.
2021-09-02 22:32:45 +00:00
# shellcheck disable=SC2086,SC1083,SC2154
# SC2086 - required for IFS replacement
# SC1083 - required for eval
# SC2154 - eval sets $cur
2021-08-23 18:32:45 +00:00
IFS=""; LANG=C
# index's
2021-10-08 06:23:58 +00:00
index="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:1:2:3:4:5:6:7:8:9:0: " # 36 items
indexl="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:1:2:3:4:5:6:7:8:9:0: " # lowercase index for ascii
exb=".-:-...:-.-.:-..:.:..-.:--.:....:..:.---:-.-:.-..:--:-.:---:.--.:--.-:.-.:...:-:..-:...-:.--:-..-:-.--:--..:.----:..---:...--:....-:.....:-....:--...:---..:----.:-----:/"
2021-08-23 18:32:45 +00:00
2021-09-02 22:32:45 +00:00
ma() {
2021-08-23 18:32:45 +00:00
eval letter=\${$#}
2021-10-08 06:23:58 +00:00
eval cur=\${$letter}; [ "$cur" = 37 ] && cur=" " # IFS=": " causes #37 in $index to be lost
2021-10-10 13:47:54 +00:00
printf '%s' "${cur}"
2021-08-23 18:32:45 +00:00
}
2021-09-02 22:32:45 +00:00
am() {
IFS=""
eval letter=\${$#};
2021-10-08 06:23:58 +00:00
[ $letter -gt 37 ] && : $((letter-=37))
2021-09-02 22:32:45 +00:00
eval cur=\${$letter}; printf '%s ' "$cur"
}
2021-08-23 18:32:45 +00:00
2021-10-10 13:47:54 +00:00
unset bang; line=1; while read -r p || [ -n "$p" ]; do
2021-08-23 18:32:45 +00:00
p=${p# }; p=${p% }
2021-10-10 13:47:54 +00:00
[ "$line" -eq 1 -o "$bang" ] 2>/dev/null && { # start comment check # this is basically a shitpost lol
case "$p" in
'#'!*) bang=" ";;
2021-10-18 08:00:00 +00:00
'#'?-*|'#'?.*) p="${p#\#}"; printf '%sl: %s\n# ' "${bang# }" "${line}"
2021-10-10 13:47:54 +00:00
bang='
' # big hack to add newlines to start if >1
n1="0"; IFS=": "; for i in ${p}; do
for ii in $exb; do
: $((n1+=1));
case "$ii" in
"$i") ma ${index} "$n1"
esac
done
n1=0
done ;;
esac # this only converts morse
2021-10-18 08:00:00 +00:00
[ "$bang" ] && p="?"
2021-10-10 13:47:54 +00:00
}
2021-09-02 22:32:45 +00:00
case "$p" in
2021-10-18 08:00:00 +00:00
"") echo ;; # skip empty lines
2021-09-02 22:32:45 +00:00
-*|.*)
# for morse to ascii
n1="0"; IFS=": "; for i in ${p}; do
for ii in $exb; do
: $((n1+=1));
case "$ii" in
"$i") ma ${index} "$n1"
esac
done
n1=0
done ;;
*)
# ascii to morse
while [ "$p" ]; do
next="${p#?}"; # ${p%$next} is the current letter
IFS=":"; na="0"; for ai in ${index} ${indexl}; do
# echo $ai
: $((na+=1))
2021-10-08 06:23:58 +00:00
case "${p%$next}" in
"$ai") am $exb $na; break;;
2021-09-02 22:32:45 +00:00
esac
done
na=0
p="$next"
done
;;
esac
2021-10-10 13:47:54 +00:00
: $((line+=1))
2021-08-23 18:32:45 +00:00
done # stdin read; fuck you
echo