#!/bin/sh
# The reference in docs/reference/ against the code: every flag, setting,
# environment variable, slash command, RPC command, JSON event and hook event
# named in src/ has a row (or a line in test/pic/docs.allow), and every row
# names something the code has. Also the README's budget, relative links, and
# the key table in docs/reference/keys.md, which is generated from
# src/tui/keymap.c.
#
# sh test/pic/docs.sh check
# sh test/pic/docs.sh --keys print the generated key table
set -eu
cd "$(dirname -- "$0")/../.."
REF=docs/reference
ALLOW=test/pic/docs.allow
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-docs.XXXXXX")
trap 'rm -rf -- "$T"' EXIT HUP INT TERM
failed=0
err() { printf 'docs: %s\n' "$*" >&2; failed=1; }
# The bindings in keymap.c's defs[]: action, default keys, description.
# A macro's keys are its first #define (the macOS one).
keys_table() {
awk '
/^#define [A-Z_]+ \{/ && !($2 in mac) { v = $0; sub(/^#define [A-Z_]+ /, "", v); mac[$2] = v }
/static const action_def defs\[\] = \{/ { on = 1; next }
on && /^\};/ { on = 0 }
on && /^ \{"/ {
line = $0
sub(/^ \{"/, "", line); name = line; sub(/".*/, "", name)
sub(/^[^"]*", /, "", line)
if (line ~ /^\{/) { keys = line; sub(/\}.*/, "", keys); sub(/^\{/, "", keys); line = substr(line, index(line, "}") + 3) }
else { m = line; sub(/,.*/, "", m); keys = mac[m]; gsub(/[{}]/, "", keys); line = substr(line, length(m) + 3) }
desc = line; sub(/^"/, "", desc); sub(/", [A-Z_]+, [A-Z_0-9]+\},?$/, "", desc)
inert = line ~ /", NO_KIND, /
gsub(/"/, "", keys); gsub(/, /, ", ", keys)
if (keys == "NULL") keys = "none"
if (inert) desc = desc " (accepted; pic has no view for it)"
printf "| `%s` | %s | %s |\n", name, keys, desc
}' src/tui/keymap.c
}
if [ "${1:-}" = --keys ]; then
printf '| Action | Default keys | What it does |\n|---|---|---|\n'
keys_table
exit 0
fi
# The names in the first column of FILE's tables (backticked tokens), one per
# line; header rows (those followed by |---) are skipped.
rows() {
awk '
/^\|/ { if (prev != "" && $0 !~ /^\|[-| ]+$/) emit(prev); prev = ($0 ~ /^\|[-| ]+$/) ? "" : $0; next }
{ if (prev != "") emit(prev); prev = "" }
END { if (prev != "") emit(prev) }
function emit(r, cell, t) {
cell = r; sub(/^\| */, "", cell); sub(/ *\|.*/, "", cell)
while (match(cell, /`[^`]+`/)) { t = substr(cell, RSTART + 1, RLENGTH - 2); print t; cell = substr(cell, RSTART + RLENGTH) }
}' "$1" | LC_ALL=C sort -u
}
allowed() { awk -v n="$1" '$1 == n { f = 1 } END { exit !f }' "$ALLOW"; }
# check KIND FILE CODE-LIST: each name the code has needs a row or an allow
# line; each row must name something the code has.
check() {
kind=$1 file=$2 code=$3
rows "$file" > "$T/rows"
while IFS= read -r n; do
grep -qxF -- "$n" "$T/rows" || allowed "$n" || err "$kind $n has no row in $file (or line in $ALLOW)"
done < "$code"
while IFS= read -r n; do
grep -qxF -- "$n" "$code" || code_has_row "$kind" "$n" || err "$file names $kind $n, which src/ does not have"
done < "$T/rows"
}
# A row the code-side search cannot list but the code still has: a dotted
# setting whose parent is a setting and whose parts appear in src/, or a
# variable pic sets (PIC_HOOK=1).
code_has_row() {
case "$1:$2" in
setting:*.*)
grep -qxF -- "${2%%.*}" "$T/settings" || return 1
grep -rqF -- "\"${2#*.}\"" src || grep -rqF -- "\$.${2#*.}" src || grep -rqF -- "\"\$.${2#*.}\"" src ;;
env:*) grep -rqF -- "\"$2=" src ;;
*) return 1 ;;
esac
}
grep -ohE '"--?[a-z][a-z0-9-]*"' src/main.c src/procs.c | tr -d '"' | LC_ALL=C sort -u > "$T/flags"
{
grep -rhoE '(^|[^a-z_])(pic_setting(_set(_json)?)?|set_setting|setting|fs_setting)\("[A-Za-z][A-Za-z.]*"' src |
sed -E 's/.*\("//; s/"$//'
grep -hoE 'bench_value\([a-z_]+, "[A-Z_]+", "[A-Za-z]+"' src/main.c | sed -E 's/.*, "//; s/"$//'
grep -rhoE '"thinkingLevel:%s"' src | sed 's/%s"/MODEL/; s/^"//'
grep -rhoE '#define [A-Z_]+_KEY "[a-z._]+"' src | sed -E 's/.* "//; s/"$//'
} | LC_ALL=C sort -u > "$T/settings"
{
grep -rhoE 'getenv\("[A-Za-z_0-9]+"\)' src | sed -E 's/getenv\("//; s/"\)//'
grep -rhoE '"PIC?_[A-Z_0-9]+"' src | tr -d '"'
grep -rhoE '"[A-Z][A-Z0-9_]*_API_KEY"|"HF_TOKEN"' src | tr -d '"'
} | LC_ALL=C sort -u > "$T/env"
{
awk '/\} commands\[\] = \{/ { on = 1; next } on && /^\};/ { on = 0 } on && /^ \{"/ { s = $0; sub(/^ \{"/, "", s); sub(/".*/, "", s); print "/" s }' src/tui/app.c
grep -ohE 'strcmp\(line, "/[a-z-]+"' src/tui/app.c | sed -E 's/.*"(\/[a-z-]+)"/\1/'
grep -qF 'strncmp(line, "/skill:"' src/tui/app.c && echo '/skill:NAME'
} | LC_ALL=C sort -u > "$T/slash"
{
grep -ohE 'strcmp\(type, "[a-z_]+"' src/rpc.c | sed -E 's/.*"([a-z_]+)"/\1/'
# The components' commands (rpc.h's pic_rpc_command).
grep -rhoE 'pic_rpc_command [a-z_]+ = \{"[a-z_]+"' src | sed -E 's/.*\{"([a-z_]+)"/\1/'
} | LC_ALL=C sort -u > "$T/rpc"
{
grep -hoE '\\"type\\":\\"[a-z_]+' src/events.c src/rpc.c | sed 's/.*\\"//'
grep -rhoE "json_object\('type', ?'[a-z_]+_(update|returned)'" src | sed -E "s/.*'([a-z_]+)'$/\1/"
} | LC_ALL=C sort -u > "$T/events"
grep -rhoE '(is\(event, |fire_init\(&f, |pic_hooks_has\([a-z>.-]+, |pic_hooks_ask\(s, |f\.event = )"[A-Z][A-Za-z]+"' src |
sed -E 's/.*"([A-Za-z]+)"$/\1/' | LC_ALL=C sort -u > "$T/hooks"
for f in "$T/flags" "$T/settings" "$T/env" "$T/slash" "$T/rpc" "$T/events" "$T/hooks"; do
[ -s "$f" ] || err "found no names for $(basename "$f") in src/: the search is out of date"
done
check flag "$REF/cli.md" "$T/flags"
check setting "$REF/settings.md" "$T/settings"
check env "$REF/env.md" "$T/env"
check "slash command" "$REF/slash.md" "$T/slash"
check "RPC command" "$REF/rpc-commands.md" "$T/rpc"
check event "$REF/events.md" "$T/events"
check "hook event" "$REF/hook-events.md" "$T/hooks"
# An allow line must name something src/ still has, and give a reason.
while read -r n reason; do
case "$n" in ''|'#'*) continue ;; esac
[ -n "$reason" ] || err "$ALLOW: $n has no reason"
grep -rqF -- "$n" src || err "$ALLOW: $n is not in src/ any more"
done < "$ALLOW"
# The key table is generated: docs/reference/keys.md holds keys_table's output
# between its markers.
awk '/<!-- keys:begin -->/ { on = 1; next } /<!-- keys:end -->/ { on = 0 } on' "$REF/keys.md" | sed '1,2d' > "$T/keys.doc"
keys_table > "$T/keys.gen"
if ! cmp -s "$T/keys.doc" "$T/keys.gen"; then
err "$REF/keys.md's key table differs from src/tui/keymap.c; regenerate it with: sh test/pic/docs.sh --keys"
diff "$T/keys.doc" "$T/keys.gen" >&2 || true
fi
# The hand-written rows of keys.md name actions keymap.c has.
awk '/<!-- keys:begin -->/ { exit } { print }' "$REF/keys.md" > "$T/keys.hand"
rows "$T/keys.hand" | grep -E '^(app|tui)\.' | while IFS= read -r n; do
grep -qF -- "\"$n\"" src/tui/keymap.c || printf '%s\n' "$n"
done > "$T/keys.stale"
[ ! -s "$T/keys.stale" ] || err "$REF/keys.md names actions keymap.c does not have: $(tr '\n' ' ' < "$T/keys.stale")"
# The README is the front door, not the manual.
lines=$(wc -l < README.md | tr -d ' ')
bytes=$(wc -c < README.md | tr -d ' ')
[ "$lines" -le 90 ] || err "README.md is $lines lines; the budget is 90 (move text to docs/manual/)"
[ "$bytes" -le 6000 ] || err "README.md is $bytes bytes; the budget is 6000 (move text to docs/manual/)"
awk 'length($0) > 300 { printf "docs: README.md:%d is %d characters; the budget is 300\n", NR, length($0); bad = 1 } END { exit bad }' README.md >&2 || failed=1
! grep -n '^ *|' README.md >&2 || err "README.md has a table; tables go in docs/reference/"
# Relative links resolve. Links into reference/pi/ (a local checkout, not in
# the repository) and outside the repository are not checked.
find README.md AGENTS.md docs -name '*.md' -type f | LC_ALL=C sort | while IFS= read -r f; do
grep -oE '\]\([^)[:space:]]+\)' "$f" | sed -E 's/^\]\(//; s/\)$//; s/#.*//' | while IFS= read -r t; do
case "$t" in ''|*:*|/*) continue ;; esac
# The target relative to the repository root, resolved lexically.
p=$(printf '%s/%s\n' "$(dirname -- "$f")" "$t" | awk -F/ '{
n = 0
for (i = 1; i <= NF; i++) {
if ($i == "" || $i == ".") continue
if ($i == "..") { if (n == 0) { print "OUTSIDE"; exit } n--; continue }
part[++n] = $i
}
s = ""; for (i = 1; i <= n; i++) s = s (i > 1 ? "/" : "") part[i]; print s
}')
case "$p" in OUTSIDE|reference/pi|reference/pi/*) continue ;; esac
[ -e "$p" ] || printf '%s: %s\n' "$f" "$t"
done
done > "$T/links"
[ ! -s "$T/links" ] || err "broken relative links:
$(cat "$T/links")"
[ "$failed" -eq 0 ] || exit 1
printf 'PASS: docs reference matches src/ (%s flags, %s settings, %s variables, %s slash, %s RPC, %s events, %s hook events), README budget, links, key table\n' \
"$(wc -l < "$T/flags" | tr -d ' ')" "$(wc -l < "$T/settings" | tr -d ' ')" "$(wc -l < "$T/env" | tr -d ' ')" \
"$(wc -l < "$T/slash" | tr -d ' ')" "$(wc -l < "$T/rpc" | tr -d ' ')" "$(wc -l < "$T/events" | tr -d ' ')" \
"$(wc -l < "$T/hooks" | tr -d ' ')"