libcx 1.01
==========

A library for building command-line interfaces: a runtime that parses a
line against a grammar, completes it, answers a user's questions about
it, and dispatches to application code; a compiler, cxgen(1), that turns
a .cx interface description into that grammar -- as C tables, as a
data2obj(1) description, as a manual page, or as a LaTeX chapter of the
manual, all four from the same checked source; and a front end,
cx-shell(8), that an appliance links against its own grammar so an
authenticated SSH user's entire session is that interface and nothing
else.

The standard this package is held to is TOPS-20's COMND JSYS: a `?'
that lists exactly what may come next at any point on the line, an ESC
or TAB that completes as far as the line is unambiguous, guide words
that make a half-typed command read like a sentence, and defaults shown
in brackets.

What each piece is for
-----------------------

libcx
    The runtime. No authentication, no process spawning, no file-scope
    mutable state -- a cx_session is threaded through every call that
    touches session state, so two sessions coexist cleanly in one
    process. Includes a small session-scoped variable store
    (cx_session_set_var()/get_var()/unset_var()) an argument's default
    can resolve against, the analog of a shell's environment without
    the line-syntax expansion a shell relies on. See cx(3).

cxgen
    The compiler. Reads one .cx grammar and writes it out as C tables
    (the default), a data2obj(1) description, an mdoc(7) manual page,
    or a LaTeX manual chapter -- so a command's help text, its
    completion behaviour, and its documentation cannot drift apart,
    because all four come from the same checked description. The
    grammar language covers typed arguments with ranges and units,
    enums, sub-modes, privilege and confirmation, a greedy `*' argument
    that consumes the rest of the line verbatim, an argument default
    that names a session variable instead of a literal, and `macro' --
    a one-word shortcut that re-dispatches a fixed command line under
    the identical privilege, confirmation and audit checks a typed one
    would get. See cxgen(1).

cx-shell
    The front end that makes an interface the whole of an SSH login.
    Not a standalone binary: an appliance links cx_shell_main() against
    its own compiled-in grammar, in a main() normally three lines long,
    and the result is what sshd(8) runs as a ForceCommand. Can also run
    a file of commands once, silently, before a session's first real
    input -- a start-up profile for priming session state at boot. See
    cx-shell(8).

examples/thermo
    A complete worked appliance -- a thermostat controller -- exercising
    every feature of the grammar language: typed arguments with a range
    and a unit, an enum whose members carry their own help, a dynamic
    completer, a privilege ladder, a confirmed command, a session
    variable and a macro built on it, and a sub-mode with its own
    prompt. It builds both the --emit-c and --emit-ds ways, and its own
    interface reference (in the manual and as a man page) is generated
    from the same thermo.cx the binary is compiled from.

examples/notes
    A second worked appliance -- a note-taking tool -- showing the same
    grammar language against a real datastore instead of a struct in
    memory: every handler talks to SQLite through ordinary sqlite3(3)
    calls, and its dynamic completer runs a live query instead of
    walking an array. Built only when <sqlite3.h> is found, so its
    absence skips this one example rather than failing the build.

Building
--------

There is no configure script and no autoconf -- neither is installed on
any of the machines this package is developed on. Platform differences
are handled by a small `uname -s' block in the Makefile, which relies on
GNU make syntax throughout.

    tar xzf libcx-1.01.tar.gz
    cd libcx-1.01
    make
    make test
    sudo make install

**On FreeBSD, use `gmake` in place of `make`** in every command above.
The system `make` there is BSD make, which does not understand this
Makefile's `$(shell ...)` and `ifeq` syntax and fails immediately with
"Invalid character in variable name"; `pkg install gmake` if it is not
already present.

`make test' builds and runs the whole suite: one test program per
library module, a whole-session transcript test through an in-memory
channel, a test that the --emit-c and --emit-ds paths agree on one
non-trivial grammar, and a check that neither the library nor any
appliance binary contains a process-spawning symbol. `make
sanitize-test' repeats it all under AddressSanitizer and
UndefinedBehaviorSanitizer. `make install' builds the manual and
installs the library, <cx/cx.h>, a pkg-config file, cxgen(1), the three
man pages, and the manual as a PDF, honouring PREFIX and DESTDIR.  A
program then builds against it with

    cc -o myapp myapp.c `pkg-config --cflags --libs libcx`

The .pc file goes where each platform's pkg-config looks:
$(PREFIX)/lib/pkgconfig on Linux, $(PREFIX)/libdata/pkgconfig on FreeBSD.

Targets: all lib cxgen shell examples tests docs man manual test check
sanitize-test install install-lib uninstall clean distclean dist fuzz
fuzz-run.

Fuzzing
-------

tests/fuzz/ holds three libFuzzer harnesses: a whole interactive or
piped session on the thermo appliance (fuzz_session), one-line dispatch
and the completion engine (fuzz_exec), and the cxgen compiler
(fuzz_cxgen).  `make fuzz' builds them -- it needs clang with libFuzzer,
FUZZCC to name another -- and

    make fuzz-run FUZZTIME=600 FUZZJOBS=16

runs each for FUZZTIME seconds on FUZZJOBS cores, starting from the seed
inputs in tests/fuzz/seeds_*.txt.  Any crash is left in
tests/fuzz/crash-*.  `make test' needs no clang: it replays every seed
through the same harnesses built with the ordinary compiler, so a seed
that once crashed the library stays in the suite as a regression test.

Requires gcc 13 or later, or clang 19 or later, with -std=c2x. Building
the manual needs a LaTeX distribution (pdflatex, makeindex); building
--emit-ds output or running its test needs data2obj(1) from
elf_tools >= 1.08 (https://rcousins.com/pub/elf_tools-1.08); building
examples/notes needs <sqlite3.h> and libsqlite3. All three are optional,
and each one's absence is reported plainly rather than failing the parts
of the build that do not need it.

Documentation
-------------

cxgen(1), cx-shell(8) and cx(3) are the reference. doc/libcx_manual.tex
builds (`make manual') into a fuller manual with the same content
narrated, an architecture diagram, and a performance chapter with
numbers from both build platforms.

New in 1.01
-----------

Eight additions, and the bugs found while making them:

  - an idle timeout (cx_session_set_idle_timeout(), cxs_app.idle_timeout):
    a session nobody has typed into for that long is closed
  - questions from inside a handler (cx_ask(), with CX_ASK_SECRET for a
    password, and cx_confirm()), refused rather than guessed at when
    nobody is watching
  - audit records to syslog (cxs_app.syslog_facility), alongside or
    instead of the audit file
  - UTF-8 line editing: the cursor, deletion and the display work in
    characters and terminal columns
  - a pager: a handler's output stops at --More-- before it scrolls off
    the screen (CX_SESSION_NOPAGER turns it off)
  - ^R reverse incremental history search, and ^Y to paste what ^K, ^U or
    ^W cut; the Delete key deletes
  - per-session line and history limits (cx_session_set_limits(),
    cxs_app.line_max and .history_max), and a pkg-config file
  - fuzz harnesses for the session, the parser and cxgen, with their seeds
    replayed by `make test'

Fixed, all present in 1.00:

  - an audit record could be forged: a quoted argument containing a
    newline was written raw, so one command could add a second, fake
    record to the trail.  Records are now escaped.
  - typed-ahead input was discarded at every prompt (TCSAFLUSH), so
    pasting several commands ran only the first.
  - a piped line longer than the limit was refused, but its remainder was
    then run as a command of its own.
  - ^C at a confirmation was a SIGINT, which under a ForceCommand logged
    the user out; it now declines.
  - declining a confirmation after a parse error repeated the old error
    instead of saying "cancelled".
  - cxs_app.banner was documented and accepted but never printed.
  - shell/cx_shell.h had no extern "C" guard, so an appliance written in
    C++ could not link cx_shell_main().  libcx is now tested from C++
    (tests/t_cplusplus.sh, -std=c++17 -pedantic -Werror).
  - cxgen, under fuzzing: every declaration that failed to parse leaked
    its half-built object, two more paths leaked strings, and
    `include ".";' made it size a buffer from a directory's ftell(3) --
    an abort under the sanitizers, a misleading allocation failure
    without them.  It now reads only regular files of sane size.
  - with the cursor mid-line on a line wider than the screen, the line
    wrapped and the cursor was drawn on the wrong row.
  - sshd hardening: the documented sshd_config fragment was a bare
    ForceCommand.  cx-shell(8) and the manual now give the full Match
    block (DisableForwarding, PermitUserRC, PermitTunnel) and explain the
    account set-up it needs; tests/t_shell.sh runs under exactly that.

The shared library's soname is unchanged (libcx.so.1): nothing was
removed or changed in meaning, and cx_session_config did not grow, so a
program built against 1.00 runs against 1.01 without rebuilding.

New in 1.00
-----------

Everything: this is the first release. The library, the compiler with
all four output forms, the SSH front end, both worked appliances, and
the manual were built together, so that the different views of one
grammar -- parser, completion, manual page, and the manual's own
generated chapter -- could be checked against each other from the
start. Included in this first release:

  - the grammar language: typed arguments (int/string/bool/enum) with
    ranges, units and dynamic completion; noise words; sub-modes with
    their own prompt; privilege levels; confirmed commands; a greedy
    `*' argument that consumes the rest of the line verbatim
  - a session-scoped variable store, and an argument default that
    resolves against one instead of a literal
  - `macro': a one-word shortcut for a fixed command line, dispatched
    under the same privilege, confirmation and audit checks a typed
    line would get
  - a start-up profile cx-shell(8) can run once, silently, before a
    session's first real input
  - two worked appliances: examples/thermo (state in a struct) and
    examples/notes (state in SQLite), showing the same grammar language
    against both
  - a full test suite (18+ programs, both unit and whole-session,
    driven with no terminal via an in-memory channel), clean under
    AddressSanitizer and UndefinedBehaviorSanitizer, verified on both
    Linux and FreeBSD
