Skip to content

wasm port using unix-tui-to-wasm - #15

Closed
neuhaus wants to merge 116 commits into
Critlist:mainfrom
neuhaus:wasm-port
Closed

neuhaus wants to merge 116 commits into
Critlist:mainfrom
neuhaus:wasm-port

Conversation

@neuhaus

@neuhaus neuhaus commented Oct 20, 2026 •

Copy link
Copy Markdown

This PR lets everyone play protoHack in their browser!

Pretty slim PR utilizing the unix-tui-to-wasm template.

If you want to keep the main branch clear of this change, perhaps put it on a wasm branch?

You can check it out at https://neuhaus.github.io/protoHack/

DainialPadraig and others added 30 commits July 18, 2025 22:47
… source. Add src/ with K&R-to-ANSI restoration work (hack.c, hack.main.c, mklev.c, rnd.c converted; hack.h updated with prototypes) and shared compat.h shim for BSD-to-POSIX portability.

Add CMakeLists.txt build system producing hack-root and mklev-root targets against ncurses. Add docs/ with CODING_STANDARDS.md, research timeline (corrected vs NetHack Wiki using ;login: primary source), and  cross-variant source comparison. Flatten src/exp/exp1/ to src/exp1/, remove redundant loose files from src/ top level, remove dev scratchpad files.
…modern C standards; update file paths and signatures for clarity and consistency.

initial refactor of mon.c for conversion.
… update prototypes in hack.h for clarity and consistency.
…, bug fixes, compatibility shims, and other changes; ensure clear guidelines for code requiring documentation.
- Adjusted CMakeLists.txt to copy mklev to both build root and hackdir.
- Added a new welcome image to the documentation.
- Updated hack.do1.c to include compatibility header and modernized function prototypes.
- Refined hack.h to include proper prototypes for functions in hack.do1.c.
- Expanded termcap buffer in hack.pri.c for modern systems and improved error handling.
…and source files; update bitfield definitions and add bounds checking for screen updates.
level generation and rendering appear stable
…ord handling in hack.c; add debug logging for cursor state in hack.pri.c
…cl_end function for better terminal compatibility
…hack.vars for compatibility and modern standards
…hance terminal handling and add safety checks; update data types for clarity and compatibility.
- Introduced a comprehensive conversion plan for the Fenlason hack experimental branch (exp1) to transition from K&R C to compilable ANSI C.
- Established the directory structure for source files and headers, ensuring the original root remains untouched.
- Documented critical architectural differences, including the consolidation into a single binary and the handling of headers and global variables.
- Outlined a detailed file-by-file conversion breakdown, specifying necessary changes for headers, core utilities, entry points, game logic, and build integration.
- Included a verification plan to ensure successful compilation and runtime functionality, alongside modern portability logic adaptations.
- Preserved historical artifacts and anomalies for future reference during the conversion process.
…enhance type safety with unsigned char; refactor load functions for clarity and efficiency; improve compatibility with modern compilers.
… improve item query handling in getobj; modernize pager fallback in rhack; adjust struct monst for signed char; ensure safe zeroing in savelev.
…che fields before deletion to prevent use-after-free errors; improve memory handling in savelev by utilizing a union for object structures.
- Introduced a new script `package-tarballs.sh` for creating source tarballs, including README and relevant directories.
- Updated `static-build.sh` to copy a README specific to static builds.
- Enhanced input handling in `hack.c`, `hack.do1.c`, and `hack.main.c` to ignore unwanted control characters (SIGQUIT) and improve user experience during prompts.
- Modified `hack.lev.c` to safely cast indices to unsigned char for array subscripting, ensuring safer memory access.
Critlist and others added 22 commits October 11, 2026 00:02
getlin() stored every character into the caller's buffer with no limit
and stored getchar()'s result straight into a char. All callers pass the
global buf[100], so typing more than 99 characters at any prompt wrote
past the end of buf (ASan global-buffer-overflow). On a terminal hangup
getchar() returns EOF forever, so getlin stored EOF bytes until it
overflowed, or on plain builds kept spinning.

Read into an int, stop storing at 99 characters (ringing the bell for the
extra keys; delete, Enter and ESC still work), and on EOF terminate the
string and take the existing hangup() save-and-exit path. All callers use
buf[100], so a fixed limit is used rather than a size parameter. The cap
of 99 keeps names set via 'o name=' safe for the %99[^,] record parser.

The 'Unknown option' message in set1() sprintf'd the option text into
pline's 60-byte pbuf, which a long option then overflowed; print at most
40 characters of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pline() formatted messages with an unbounded sprintf() into a 60-byte
pbuf (static in normal builds, on the stack under SMALL), so any
formatted message of 60 bytes or more overflowed it. Easily triggered
by typing a 60-character option name at 'o' (ASan: global-buffer-overflow
WRITE in vsprintf from pline).

Enlarge pbuf to 256 in both variants and use snprintf(pbuf,sizeof(pbuf),...)
so output is bounded regardless. This covers doname()'s buf[100] plus a
prefix. It is a prerequisite for the P3-07 change of pline(buf) calls to
pline("%s",buf): those inventory lines previously bypassed sprintf and
would otherwise now overflow pbuf. The ptr/^R last-message logic, savx
and the non-% branch are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pline() treats its first argument as a printf format whenever it
contains a '%'. Several callers build a line in buf[] with doname()
(which embeds the player-typed `c`/call names of potions, scrolls,
wands and rings) and then call pline(buf). Naming an object type
%s%s%s%s%s%s made sprintf() read garbage varargs and crash with
SIGSEGV the next time the item was shown (e.g. wielding it).

Pass the text as an argument, pline("%s",buf), at every callsite whose
format text can include an object name. pline() itself is unchanged.

Callsites changed:
- hack.do.c: inventory-letter lookup message (doname)
- hack.do.c: "You dropped ..." message
- hack.do.c: were(): "You were wearing ..."
- hack.c: wizard-mode dump of stolen objects (three calls)
- hack.c: wizard-mode 'O' create-object echo (two calls)
- hack.c: wizard-mode object list ("At x, y ...")
- hack.c: prinv() ("a - <name>")
- hack.mon.c: "She stole ..." message
- hack.do1.c: step-mode inventory listing

Not changed: hack.c monster debug line (only mname and numbers), and
all other pline(buf)-free callers, which pass string literals or
fixed message macros. Note pline("%s",buf) always goes through the
60-byte pbuf, so lines of 60 or more bytes now overflow it even
without a '%' (P3-08, not fixed here).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The comment cited pline()'s 60-byte pbuf, which P3-08 raised to 256.
Keep the %.40s cap, which now just keeps the echo on the top line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fix Ctrl-R segfault and comma-in-name high-score wipe
Fix remaining S1 crashes: drain death, exploding monsters, getlin/pline overflows, format-string names
check_resize() only called docrt() when the size differed from
last_cols/last_rows, which the too-small loop never updates. Shrinking
the terminal and restoring the same 80x24 left the warning on screen
over a stale map. Found by the soak harness port.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dev/lib + run-{asan,ubsan,asan-ubsan,hardened,all}.py from restoHack
master (faa0110), adapted: protoHack build flags (no PIE: CMakeLists
forces -fno-pie), no -D flag, no libFuzzer step, and a protoHack
game_runner (pyte screen, BFS stair seeking, targeted keys for the S1
fixes, save/hangup + restore, resize, new game after death).
HACK_SEED seeds the RNG in -DHACK_TEST_SEED builds only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…; docs

- quit reads the screen (dismiss --More--, cancel prompts, then Q):
  blind ' ' passed turns and new --More-- pages swallowed the Q
- restore check looks only at the respawned game's output
- a TERMINAL TOO SMALL screen still up after restoring 80x24 fails
  the run (the bug fixed in Critlist#13)
- dev/README.md: lanes, what a run does, soak recipe, replay
- CHANGELOG entry

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Redraw the map after the TERMINAL TOO SMALL screen
dev: port restoHack's sanitizer/soak harness
Build with -fsigned-char to fix mklev crash on ARM64
Size makemaz() DFS stack for the full maze grid
@Critlist

Copy link
Copy Markdown
Owner

Hey Sven, thanks! This is a really nice slim port. I played your build at neuhaus.github.io/protoHack: the news screen, level generation and status line all came up right, and it's great to see the 1982 game running in a browser. Happy to take it on main, same as restoHack#31. A few asks, all following what we settled on over there:

  1. Submodule URL: please point .gitmodules at https://github.com/Critlist/unix-tui-to-wasm.git. The fork already has your pinned commit 0038185, so only the URL changes. Nothing changes for your upstream: I'll pull from it when we bump the pin. (Same as restoHack#31: Wasm port restoHack#31 (comment))

  2. Keep the original pline()/panic() lines: the stdarg conversion makes sense (wasm needs typed calls, and the old pseudo-varargs are shaky on 64-bit anyway). Since protoHack is focused on preserving the 1982 source, I'd like to keep the original K&R definitions verbatim as /* Original 1982: ... */ comments or an #if 0 block at their original spots, like we did in restoHack#59: Finish the ANSI C conversion restoHack#59 (review)

  3. Modern: notes on the __EMSCRIPTEN__ blocks, like you did in restoHack#31. The one I'd most like explained is wasm_mklev() in hack.lev.c: the native build deliberately keeps mklev as a separate binary (the original PDP-11 memory design), so a sentence on why the browser build runs it in-process would help future readers.

With those in, I'll run the native build checks and I'm happy to merge. Really appreciate all the work you've been putting into these ports.

- Point the unix-tui-to-wasm submodule at the Critlist fork, keeping the
  same pinned commit (0038185).
- Keep the original 1982 K&R panic()/pline() definitions verbatim as
  "Original 1982:" comments at their original spots.
- Document each __EMSCRIPTEN__ branch with a Modern: note, explaining in
  particular why the browser runs mklev in-process instead of forking the
  separate generator binary.
@neuhaus

neuhaus commented Nov 12, 2026

Copy link
Copy Markdown
Author

How about now?

Builds the browser bundle with the pinned Emscripten SDK and publishes it
to https://neuhaus.github.io/protoHack/ on every push to main, on release
tags, and on manual dispatch.
GitHub only offers workflow_dispatch once a workflow is on the default
branch, and push events do run workflows from other branches, so the port
branch is added to the branch trigger temporarily.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants