morsh/morsh

78 lines
2.2 KiB
Bash
Executable File

#!/bin/sh
# .. / ..-. . . .-.. / .. -. / .-.. --- ...- . / .-- .. - .... / .... . .-. / .-- .-. .. - .. -. --. / - .... .. ...
# shellcheck disable=SC2086,SC1083,SC2154
# SC2086 - required for IFS replacement
# SC1083 - required for eval
# SC2154 - eval sets $cur
IFS=""; LANG=C
# index's
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=".-:-...:-.-.:-..:.:..-.:--.:....:..:.---:-.-:.-..:--:-.:---:.--.:--.-:.-.:...:-:..-:...-:.--:-..-:-.--:--..:.----:..---:...--:....-:.....:-....:--...:---..:----.:-----:/"
ma() {
eval letter=\${$#}
eval cur=\${$letter}; [ "$cur" = 37 ] && cur=" " # IFS=": " causes #37 in $index to be lost
printf '%s' "${cur}"
}
am() {
IFS=""
eval letter=\${$#};
[ $letter -gt 37 ] && : $((letter-=37))
eval cur=\${$letter}; printf '%s ' "$cur"
}
unset bang; line=1; while read -r p || [ -n "$p" ]; do
p=${p# }; p=${p% }
[ "$line" -eq 1 -o "$bang" ] 2>/dev/null && { # start comment check # this is basically a shitpost lol
case "$p" in
'#'!*) bang=" ";;
'#'?-*|'#'?.*) p="${p#\#}"; printf '%sl: %s\n# ' "${bang# }" "${line}"
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 ${indexl} "$n1"
esac
done
n1=0
done ;;
esac # this only converts morse
[ "$bang" ] && p="?"
}
case "$p" in
"") echo ;; # skip empty lines
-*|.*)
# for morse to ascii
n1="0"; IFS=": "; for i in ${p}; do
for ii in $exb; do
: $((n1+=1));
case "$ii" in
"$i") ma ${indexl} "$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))
case "${p%$next}" in
"$ai") am $exb $na; break;;
esac
done
na=0
p="$next"
done
;;
esac
: $((line+=1))
done # stdin read; fuck you
echo