Skip to content

feat(compat): drop-in termbox2.h + editor example (DoD #8)#55

Merged
rizukirr merged 7 commits into
mainfrom
compat-termbox2
Jun 13, 2026
Merged

feat(compat): drop-in termbox2.h + editor example (DoD #8)#55
rizukirr merged 7 commits into
mainfrom
compat-termbox2

Conversation

@rizukirr

@rizukirr rizukirr commented Jun 13, 2026

Copy link
Copy Markdown
Owner

Summary

Drop-in compat/termbox2.h mapping the supported termbox2 tb_/TB_ API onto libterm's lt_/LT_ symbols, plus a minimal real text editor that builds against it — ROADMAP DoD #8.

Changes

  • compat/termbox2.h — aliases struct tags (tb_cell/tb_event), constants, ~37 functions; adapts tb_get_cell (snapshot, back-buffer only) and tb_put_cell; #errors the genuinely-unsupported functions with a message naming the lt_ path.
  • tests/compat_smoke.c — exhaustively references every alias (130 constants + 39 funcs) so a typo in any alias fails the build.
  • tests/test_compat_get_cell.c — pty-backed tb_get_cell round-trip + back-buffer-only contract.
  • tests/test_compat_editor.c — white-box buffer-model regression test (POSIX-only; guards the insert_newline use-after-free under ASan).
  • examples/editor.c — minimal real editor built against the compat header.

Runtime fixes (found by running the editor)

  • insert_newline use-after-free → "out of memory" on Enter: it held a g_rows pointer across realloc. Re-index after the grow; ASan-guarded by the new test.
  • Ctrl-S / Ctrl-Q not firing: libterm defaults to the modern key model (Ctrl+letter → ch+LT_MOD_CTRL, key==0). The editor opts into tb_set_input_mode(LT_INPUT_COMPAT) — the one documented adaptation.

Testing

  • ctest → 100% passed, 37 tests; full -DLIBTERM_WARNINGS_AS_ERRORS=ON build clean (gcc + clang); clang-format clean tree-wide.
  • examples/editor.c references exactly one LT_* symbol (the documented compat opt-in); everything else is tb_/TB_.

rizukirr added 7 commits June 13, 2026 17:07
Reference all 130 value constants + 39 function/allocator aliases (plus TB_RGB
and the tb_get_cell/tb_put_cell adapters), so a typo in any alias fails the
build here instead of silently when a consumer first uses the symbol. Bucket-C
unsupported macros are deliberately not referenced.
…f memory')

append_row() reallocs g_rows and may move it, but insert_newline held a pointer
into the old array across that call and dereferenced it — a garbage length led
to a huge failing malloc reported as 'out of memory' when Enter split a line.
Realloc the array directly and re-index g_rows[g_cy] afterwards (also drops a
latent 1-byte leak). Add a terminal-free white-box regression test that includes
the editor TU (main() guarded by EDITOR_NO_MAIN) and asserts split/join
contents; it trips ASan heap-use-after-free against the old code.
libterm defaults to its modern key model (Ctrl+letter -> ch + LT_MOD_CTRL,
key == 0), so the editor's termbox2-idiom checks (ev.key == TB_KEY_CTRL_S/Q)
never matched and Ctrl-S/Ctrl-Q typed 's'/'q' instead. Opt into termbox2
control-byte semantics with the documented one-line adaptation.
clang-format: reflow compat/termbox2.h, compat_smoke.c, editor.c to satisfy the
format check. windows-mingw-native: test_compat_editor #includes editor.c, which
uses POSIX getline() (undeclared on MinGW); gate it to if(NOT WIN32) — examples
are already EXAMPLES=OFF on the Windows lanes, and the buffer logic is covered on
Linux/macOS.
@rizukirr rizukirr merged commit dee0c5d into main Jun 13, 2026
42 checks passed
@rizukirr rizukirr deleted the compat-termbox2 branch June 13, 2026 11:26
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.

1 participant