-
Notifications
You must be signed in to change notification settings - Fork 1
/
catcom.sh
executable file
·64 lines (44 loc) · 852 Bytes
/
catcom.sh
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
#!/usr/bin/env bash
read -rd '' doc <<'EOF'
catcom - simple cat based script acting as a "terminal"
USAGE
catcom TTY Connect to a TTY device
OPTIONS
-h, --help Show help
EXAMPLES
Connect to /dev/ttyACM0
$ catcom /dev/ttyACM0
EOF
old_settings=""
tty=""
child=""
cleanup () {
stty "$old_settings" < "$tty"
if [[ -n $child ]]; then
kill $child
fi
echo
echo catcom: original settings restored
}
main () {
tty="$1"; shift || { help; return 1; }
old_settings="$(stty -g < "$tty")"
trap cleanup EXIT
stty raw -echo "$@" < "$tty"
cat "$tty" &
child=$?
cat > "$tty"
}
help () {
echo "$doc"
echo
}
declare -A COMMANDS=(
[main]=main
[--help]=help
[-h]=help
)
argparse () {
"${COMMANDS["${1:-main}"]:-${COMMANDS[main]}}" "$@"
}
argparse "$@"