374. The manual index
This page explains how apropos and whatis get the index they search. Read it if either command prints nothing on a machine that has manual pages.
man finds a page by walking the manual tree, but apropos and whatis search an index. Without one they print nothing and exit 0, which looks exactly like “nothing matches”.
The build writes no index while it installs packages: script/env/chroot.env sets KPKG_SKIP_INDEX=man, so kpkg’s manual trigger is skipped on every install. script/phases/70_image/080_whatis.sh writes the index once, over the finished tree, with mandoc’s makewhatis, and checks that it was written.
The manual roots
# `makewhatis` is the name mandoc's BINM_MAKEWHATIS installs into /usr/sbin;
# the binary behind it is mandoc itself.
ROOTS="/usr/share/man /usr/local/share/man"
if ! command -v makewhatis >/dev/null 2>&1; then
echo "[whatis] makewhatis is not installed — skipping" >&2
exit 0
fi
script/phases/70_image/080_whatis.sh, lines 41–48
mandoc keeps one database, mandoc.db, in each manual root, and apropos opens the one in each directory of its manual path. The two roots here are the two the mandoc port compiles into MANPATH_DEFAULT, /usr/share/man and /usr/local/share/man. makewhatis is the name the port installs in /usr/sbin for mandoc’s indexer. Without it the step says so and exits 0.
dirs=""
for root in $ROOTS; do
[ -d "$root" ] || continue
# THE STALE INDEX GOES FIRST. The build tree is incremental, and a root
# that no longer holds a page keeps its old database otherwise — the one
# case where apropos answers with programs the image does not carry.
rm -f "$root/mandoc.db"
dirs="$dirs $root"
done
if [ -z "$dirs" ]; then
echo "FATAL: none of $ROOTS exists; nothing installed a manual page." >&2
exit 1
fi
script/phases/70_image/080_whatis.sh, lines 50–63
The old database of each root is removed first. The tree is incremental, and a root left with an old database would answer apropos with programs the image does not carry. Only the roots that exist are passed on, because makewhatis treats a missing directory as an error; none existing at all means no package installed a manual page, and stops the phase.
Writing and checking the index
echo "[whatis] indexing$dirs"
# NOT `set -e`-FATAL. The indexer sets a non-zero status for a single
# unreadable page or dangling link inside the tree and still writes a complete
# database for everything else, so the database is the assertion and the status
# is a warning.
makewhatis $dirs || echo "[whatis] makewhatis reported errors — see above" >&2
script/phases/70_image/080_whatis.sh, lines 65–71
makewhatis given directories creates a database in each, from the pages under it. Its exit status is not trusted: it is non-zero for a single unreadable page or dangling link in the tree, and the database it writes is still complete for everything else. A non-zero status is printed as a warning, and the phase goes on.
indexed=0
for root in $dirs; do
[ -s "$root/mandoc.db" ] || continue
echo "[whatis] $root/mandoc.db: $(wc -c < "$root/mandoc.db") bytes"
indexed=$(( indexed + 1 ))
done
# A root with no manual pages in it gets no database by design, so one empty
# root is not an error — all of them being empty is.
if [ "$indexed" -eq 0 ]; then
echo "FATAL: no mandoc.db was written under$dirs." >&2
echo " apropos and whatis would answer nothing for every query." >&2
exit 1
fi
script/phases/70_image/080_whatis.sh, lines 73–86
The database is the check. A root with no pages in it gets no database, which is not an error, so the phase stops only when no root has one.
A page’s one-line description
apropos and whatis print, after each page’s name, the one-line description makewhatis stored from the page’s NAME section, and makewhatis stores no more than its first 150 bytes. The bytes are counted in the page’s roff source, after the name and its dash, so an escape counts at its written length: \(dq for a straight double quote is four bytes for one printed character, \e for a backslash two. A longer description is printed cut at the 150th byte, mid-word as often as not, while man shows the whole NAME line.
The KDOS pages are written within the limit. kdos-doc takes each command’s description from its row of the command index and ends one that would not fit before the last clause that does, or at the last word, and the rows are short enough that none needs it. A port that ships its own page keeps its NAME line under 150 bytes the same way.
Why it runs here
After 040_orphans.sh, so the pages of a removed package are not indexed, and before 090_initramfs.sh and 110_iso.sh. On an installed system KPKG_SKIP_INDEX is not set, and kpkg keeps the index current on every install and removal.
What it leaves
/usr/share/man/mandoc.db, and /usr/local/share/man/mandoc.db when that root holds pages, on the image. On a running system:
apropos editor
whatis ls
See also
- Databases stamped into the image: the three indexes of this phase
- Manual pages: what a port installs
- Shared indexes: kpkg’s manual trigger and
KPKG_SKIP_INDEX