Repository navigation
Expand file tree
/
Copy pathbasic
More file actions
executable file
·108 lines (101 loc) · 5.41 KB
/
Copy pathbasic
File metadata and controls
executable file
·108 lines (101 loc) · 5.41 KB
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
#!/bin/sh
# basic -- launcher for the TRS-80 LEVEL II BASIC interpreter (trs80basic.awk).
#
# ./basic interactive READY prompt
# ./basic mygame.bas LOAD + RUN it, then exit (see `./basic --help`)
# ./basic game.cmd load the /CMD file and run it (SYSTEM name, /)
#
# The companion Z80 core (USR routines executed): TRS80_Z80 names the command
# that runs it. When the variable is unset and the core is checked out beside
# this repo, as ../trs80_z80_core/core.py, the launcher names it itself; set
# TRS80_Z80 to override, or to the empty string for no core at all (the
# argument-returning stub, as before). See README "Run with the Z80 core".
#
# The "--" is what makes flags work: without it gawk tries to parse --seed
# and friends as its own long options. exec passes the interpreter's exit
# status straight through to the caller; at a terminal the launcher stays
# instead, to give the terminal back if gawk dies (see the end).
here=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) || exit 2
command -v gawk >/dev/null 2>&1 || {
echo "basic: gawk not found (GNU awk >= 5.0 required)" >&2
exit 2
}
[ -f "$here/trs80basic.awk" ] || {
echo "basic: $here/trs80basic.awk is missing" >&2
exit 2
}
# so `man` and `help` find their text from any working directory
: "${TRS80_MANFILE:=$here/support/manpages.txt}"
export TRS80_MANFILE
# the companion core beside this checkout, unless TRS80_Z80 is set (even empty).
# TRS80_Z80 is a COMMAND, which gawk runs through `sh -c`, so the path has to
# go in as one shell word: a checkout under "/My Code/" split into two and the
# stub ran instead, silently (the 2026-09-19 audit). Single-quote it,
# with the usual '\'' for a quote in the path itself.
if [ -z "${TRS80_Z80+set}" ] && [ -f "$here/../trs80_z80_core/core.py" ] \
&& command -v python3 >/dev/null 2>&1; then
corepath=$here/../trs80_z80_core/core.py
TRS80_Z80="python3 '$(printf '%s' "$corepath" | sed "s/'/'\\\\''/g")'"
export TRS80_Z80
unset corepath
fi
# gawk's time extension gives the keyboard matrix a wall clock (p30: a key
# "held" for TRS80_KMHOLD milliseconds rather than a poll count). Load it
# when this gawk has it; without it the interpreter falls back to polls.
# It must load SILENTLY: newer gawks (5.3, Ubuntu 24.04's) still ship it but
# print "The time extension is obsolete. Use the timex extension from
# gawkextlib instead." on stderr at every start, and stderr here is the
# BASIC program's error channel -- one stray line fails every caller that
# reads it. gawkextlib's timex has the same gettimeofday() and is taken
# when it is installed; with neither, the matrix holds a key for a poll
# count instead of a time, which is only the held-key feel at a terminal
# without the kitty protocol.
tm=
for ext in time timex; do
if out=$(gawk -l "$ext" 'BEGIN { }' 2>&1) && [ -z "$out" ]; then tm="-l $ext"; break; fi
done
# -b: BASIC strings are byte strings. Without it a UTF-8 locale turns
# CHR$(200) into a two-byte character and mangles raw bytes read from files
# (graphics, packed machine code); the interpreter builds its own UTF-8 for
# the terminal glyphs, so the display does not need the locale.
#
# At a terminal the interpreter runs the tty raw, and only its own exit path
# puts it back. A gawk fatal or a kill skips that path and leaves the shell
# with a terminal that does not echo. So with a terminal on stdin the
# launcher stays behind gawk and, on an exit status of 2 or more (gawk's
# fatal is 2, a signal is 128+n), restores the modes it found, the cursor,
# the main screen and the keyboard protocol -- but only when the modes are
# not the ones it found: the interpreter's own exit path puts them back, and
# the keyboard protocol and the grid come only with a raw tty, so after a
# clean exit 2 (a usage error, a missing file) the pop and the screen switch
# undid what was never done, a pop the shell's own push could pay for (the
# 2026-09-30 audit). Without a terminal on stdin it execs as before:
# a caller's timeout or alarm must land on gawk itself, not on a shell
# that would die and leave gawk running.
# The exact build, for --version and the `version` metacommand: git's
# describe in a checkout (the tag, or "v2.4.0-3-gabcdef0" past it, "-dirty"
# with edits); outside one the interpreter names its release alone.
# only when $here is ITSELF a checkout: from an unpacked release inside
# some other repository, git would walk up and describe THAT repository
# git names the top level by its PHYSICAL path, so $here is compared as one
# too: reached through a symlink (macOS /tmp is /private/tmp) the two
# spellings differed and the build id was lost (the 2026-09-30 audit)
if [ "$(git -C "$here" rev-parse --show-toplevel 2>/dev/null)" = "$(cd "$here" && pwd -P)" ]; then
bid=$(git -C "$here" describe --tags --always --dirty 2>/dev/null)
else
bid=
fi
if [ -t 0 ] && tty0=$(stty -g 2>/dev/null) && [ -n "$tty0" ]; then
gawk -b $tm -v "BUILDID=$bid" -f "$here/trs80basic.awk" -- "$@"
rc=$?
if [ "$rc" -ge 2 ] && [ "$(stty -g 2>/dev/null)" != "$tty0" ]; then
stty "$tty0" 2>/dev/null || stty sane 2>/dev/null
# "0" is off, as the interpreter reads it (p10 DUMB)
if { [ -z "$TRS80_DUMB" ] || [ "$TRS80_DUMB" = 0 ]; } && [ -t 1 ]; then
[ "$TRS80_KBPROTO" = 0 ] || printf '\033[<u'
printf '\033[?1049l\033[0m\033[?25h'
fi
fi
exit "$rc"
fi
exec gawk -b $tm -v "BUILDID=$bid" -f "$here/trs80basic.awk" -- "$@"