MANDOC(3)
π MANDOC(3) β the mandoc macro compiler library π
okay bestie, sit down, hydrate, because we're about to talk about mandoc β yes, the library one, the one from the Library Functions Manual, not the command. This is the whole gang: mandoc, deroff, mparse_alloc, mparse_copy, mparse_free, mparse_open, mparse_readfd, mparse_reset, mparse_result. The mandoc macro compiler library. iconic behavior fr fr π
β¨ NAME β¨
mandoc, deroff, mparse_alloc, mparse_copy, mparse_free, mparse_open, mparse_readfd, mparse_reset, mparse_result β mandoc macro compiler library. that's the whole squad right there π«Ά
πΈ SYNOPSIS πΈ
#include <sys/types.h>
#include <stdio.h>
#include <mandoc.h>
#define ASCII_NBRSP
#define ASCII_HYPH
#define ASCII_BREAK
struct mparse *
mparse_alloc(int options, enum mandoc_os oe_e, char *os_s);
void
mparse_free(struct mparse *parse);
void
mparse_copy(const struct mparse *parse);
int
mparse_open(struct mparse *parse, const char *fname);
void
mparse_readfd(struct mparse *parse, int fd, const char *fname);
void
mparse_reset(struct mparse *parse);
struct roff_meta *
mparse_result(struct mparse *parse);
#include <roff.h>
void
deroff(char **dest, const struct roff_node *node);
#include <sys/types.h>
#include <mandoc.h>
#include <mdoc.h>
extern const char * const * mdoc_argnames;
extern const char * const * mdoc_macronames;
#include <sys/types.h>
#include <mandoc.h>
#include <man.h>
extern const char * const * man_macronames;
like. look at all those prototypes. they're serving β¨
π DESCRIPTION π
The mandoc library parses a Unix manual into an abstract syntax tree (AST). Periodt. Unix manuals are composed of mdoc(7) or man(7), and may be mixed with roff(7), tbl(7), and eqn(7) invocations.
The following describes a general parse sequence β think of it as the group chat itinerary, step by step, no skipping:
- initiate a parsing sequence with
mchars_alloc(3)andmparse_alloc(); - open a file with
open(2)ormparse_open(); - parse it with
mparse_readfd(); - close it with
close(2); - retrieve the syntax tree with
mparse_result(); - if information about the validity of the input is needed, fetch it with
mparse_updaterc(); - iterate over parse nodes with starting from the first member of the returned
struct roff_meta; - free all allocated memory with
mparse_free()andmchars_free(3), or invokemparse_reset()and go back to step 2 to parse new files.
like, that's the loop. that's the whole vibe. π
π₯ REFERENCE π₯
This section documents the functions, types, and variables available via <mandoc.h>, with the exception of those documented in mandoc_escape(3) and mchars_alloc(3). (those two got their own pages, they don't need to be here, they're doing their own thing π
)
π Types π
enum mandocerrβ an error or warning message during parsing. yes bestie, things can go wrong, and this is the enum that names it.enum mandoclevelβ a classification of anenum mandocerras regards system operation. See the DIAGNOSTICS section inmandoc(1)regarding the meanings of the levels. go read that one for the deets, i'm not making stuff up for you.struct mparseβ an opaque pointer to a running parse sequence. Created withmparse_alloc()and freed withmparse_free(). This may be used across parsed input ifmparse_reset()is called between parses. she's opaque, she's mysterious, she's reusable (with consent, i.e. a reset). π
π¦ Functions π¦
deroff()β Obtain a text-only representation of astruct roff_node, including text contained in its child nodes. To be used on children of the first member ofstruct roff_meta. When it is no longer needed, the pointer returned fromderoff()can be passed tofree(3). so like, strip the formatting, keep the words, and don't forget to free her.mparse_alloc()β Allocate a parser. The arguments have the following effect:optionsβ When theMPARSE_MDOCorMPARSE_MANbit is set, only that parser is used. Otherwise, the document type is automatically detected.
When the MPARSE_SO bit is set, roff(7) so file inclusion requests are always honoured. Otherwise, if the request is the only content in an input file, only the file name is remembered, to be returned in the sodest field of struct roff_meta.
When the MPARSE_QUICK bit is set, parsing is aborted after the NAME section. This is for example useful in makewhatis(8) -Q to quickly build minimal databases. speedrun strats πββοΈ
When the MARSE_VALIDATE bit is set, mparse_result() runs the validation functions before returning the syntax tree. This is almost always required, except in certain debugging scenarios, for example to dump unvalidated syntax trees. (yes, it's spelled MARSE_VALIDATE up in here, i don't make the rules, i just report them π)
os_eβ Operating system to check base system conventions for. IfMANDOC_OS_OTHER, the system is automatically detected fromOs,-Ios, oruname(3).os_sβ A default string for themdoc(7)Osmacro, overriding theOSNAMEpreprocessor definition and the results ofuname(3). PassingNULLsets no default.
The same parser may be used for multiple files so long as mparse_reset() is called between parses. mparse_free() must be called to free the memory allocated by this function. Declared in <mandoc.h>, implemented in read.c.
mparse_free()β Free all memory allocated bymparse_alloc(). Declared in<mandoc.h>, implemented inread.c. clean up after yourself bestie, no cap.mparse_copy()β Dump a copy of the input to the standard output; used for-man -Tman. Declared in<mandoc.h>, implemented inread.c.mparse_open()β Open the file for reading. If that fails andfnamedoes not already end in β.gzβ, try again after appending β.gzβ. Save the information whether the file is zipped or not. Return a file descriptor open for reading or-1on failure. It can be passed tomparse_readfd()or used directly. Declared in<mandoc.h>, implemented inread.c. she tries, she tries again with.gz, she remembers if you're zipped. πmparse_readfd()β Parse a file descriptor opened withopen(2)ormparse_open(). Pass the associated filename infname. This function may be called multiple times with different parameters; however,close(2)andmparse_reset()should be invoked between parses. Declared in<mandoc.h>, implemented inread.c.mparse_reset()β Reset a parser so thatmparse_readfd()may be used again. Declared in<mandoc.h>, implemented inread.c. the glow-up between parses β¨mparse_result()β Obtain the result of a parse. Declared in<mandoc.h>, implemented inread.c. the moment you've all been waiting for.
π§ Variables π§
man_macronamesβ The string representation of aman(7)macro as indexed byenum mant.mdoc_argnamesβ The string representation of anmdoc(7)macro argument as indexed byenum mdocargt.mdoc_macronamesβ The string representation of anmdoc(7)macro as indexed byenum mdoct.
three lookup tables, three besties, all string representations, all indexed by their own lil enum. πΈ
ok that's part one bestie, there's more coming so don't go anywhere π
π IMPLEMENTATION NOTES π
okay bestie, this part is the structural tea β β how mdoc(7) and man(7) syntax trees and strings are actually built. Like, the skeleton, the lore, the internal plumbing. Let's get into it, no cap.
π§΅ Man and Mdoc Strings π§΅
Strings can be pulled out of mdoc and man meta-data, or from text nodes (MDOC_TEXT and MAN_TEXT, respectively). These strings have special non-printing formatting cues embedded right in the text itself, plus roff(7) escapes preserved from input. So if you're implementing this, you're gonna have to handle both situations to get human-readable text. And generally, you can assume strings consist of 7-bit ASCII characters.
The following non-printing characters may be embedded in text strings:
ASCII_NBRSPβ a non-breaking space character.ASCII_HYPHβ a soft hyphen.ASCII_BREAKβ a breakable zero-width space.
Escape characters also get passed verbatim into text strings. An escape character is a sequence of characters beginning with the backslash (\). To construct human-readable text, these should be intercepted with mandoc_escape(3) and converted with one of the functions described in mchars_alloc(3).
π³ Man Abstract Syntax Tree π³
This AST is governed by the ontological rules laid down in man(7) and takes its terminology from there. It's made of struct roff_node nodes with element, root and text types as declared by the type field. Every node also gives you its parse point β the line, pos, and sec fields β plus its position in the tree β the parent, child, next and prev fields β and some type-specific data.
The tree itself is arranged according to this normal form, where capitalised non-terminals represent nodes:
ROOT β mnode+
mnode β ELEMENT | TEXT | BLOCK
BLOCK β HEAD BODY
HEAD β mnode*
BODY β mnode*
ELEMENT β ELEMENT | TEXT*
TEXT β [[:ascii:]]*
The only elements that can nest other elements are the ones with next-line scope, as documented in man(7).
π¦ Mdoc Abstract Syntax Tree π¦
This AST is governed by the ontological rules laid down in mdoc(7) and takes its terminology from there. The "in-line" elements described in mdoc(7) are just called "elements" here, no extra spice.
The AST is made of struct roff_node nodes with block, head, body, element, root and text types as declared by the type field. Every node also gives you its parse point β the line, pos, and sec fields β plus its position in the tree β the parent, child, last, next and prev fields β and some type-specific data. In particular, for nodes generated from macros, the generating macro lives in the tok field.
The tree itself is arranged according to this normal form, where capitalised non-terminals represent nodes:
ROOT β mnode+
mnode β BLOCK | ELEMENT | TEXT
BLOCK β HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
ELEMENT β TEXT*
HEAD β mnode*
BODY β mnode* [ENDBODY mnode*]
TAIL β mnode*
TEXT β [[:ascii:]]*
Now the notable bit: those TEXT nodes following the HEAD, BODY and TAIL nodes of the BLOCK production? Those refer to punctuation marks. And even though a TEXT node will generally have a non-zero-length string, there's one specific case β .Bd -literal β where an empty line will produce a zero-length string. Multiple body parts only show up in invocations of Bl -column, where a new body introduces a new phrase.
The mdoc(7) syntax tree also accommodates broken block structures. The ENDBODY node exists to end the formatting associated with a given block before the physical end of that block. It has a non-null end field, is of the BODY type, has the same tok as the BLOCK it is ending, and has a pending field pointing to that BLOCK's BODY node. It is an indirect child of that BODY node and has no children of its own.
An ENDBODY node gets generated when a block ends while one of its child blocks is still open, like in this example:
.Ao ao
.Bo bo ac
.Ac bc
.Bc end
This example results in the following block structure:
BLOCK Ao
HEAD Ao
BODY Ao
TEXT ao
BLOCK Bo, pending -> Ao
HEAD Bo
BODY Bo
TEXT bo
TEXT ac
ENDBODY Ao, pending -> Ao
TEXT bc
TEXT end
Here, the formatting of the Ao block extends from TEXT ao to TEXT ac, while the formatting of the Bo block extends from TEXT bo to TEXT bc. It renders as follows in -Tascii mode:
<ao [bo ac> bc] end
Support for badly-nested blocks is only there for backward compatibility with some older mdoc(7) implementations. Using badly-nested blocks is strongly discouraged β for example, the -Thtml front-end to mandoc(1) is unable to render them in any meaningful way. And honestly, behaviour when encountering badly-nested blocks is not consistent across troff implementations, especially when you're using multiple levels of badly-nested blocks. So like, don't. Periodt. π
π SEE ALSO π
mandoc(1)man.cgi(3)mandoc_escape(3)mandoc_headers(3)mandoc_malloc(3)mansearch(3)mchars_alloc(3)tbl(3)eqn(7)man(7)mandoc_char(7)mdoc(7)roff(7)tbl(7)
π AUTHORS π
The mandoc library was written by Kristaps Dzonsons <kristaps@bsd.lv> and is maintained by Ingo Schwarze <schwarze@openbsd.org>. Iconic behaviour, fr fr. π
That's the whole page, bestie. This one came from Debian, dated December 30, 2018, and it's MANDOC(3). Go off. πβ¨
π¦ where this page comes from
- alpine 3.22: mandoc-doc 1.14.6-r13 Β· ISC
- alpine 3.23: mandoc-doc 1.14.6-r14 Β· ISC
- alpine 3.24: mandoc-doc 1.14.6-r15 Β· ISC
- debian trixie: libmandoc-dev 1.14.6-4 Β· ISC
- epel 10: libmandoc-devel 1.14.6-11.el10_1 Β· ISC AND BSD-2-Clause AND BSD-3-Clause
- epel 9: libmandoc-devel 1.14.6-2.el9 Β· ISC
- fedora 43: libmandoc-devel 1.14.6-12.fc43 Β· ISC AND BSD-2-Clause AND BSD-3-Clause
- fedora 43: libmandoc-devel 1.14.6-12.fc43 Β· ISC AND BSD-2-Clause AND BSD-3-Clause
- fedora 44: libmandoc-devel 1.14.6-13.fc44 Β· ISC AND BSD-2-Clause AND BSD-3-Clause
- fedora 44: libmandoc-devel 1.14.6-13.fc44 Β· ISC AND BSD-2-Clause AND BSD-3-Clause
- ubuntu questing: libmandoc-dev 1.14.6-4
- ubuntu resolute: libmandoc-dev 1.14.6-4build1 Β· ISC
- ubuntu stonking: libmandoc-dev 1.14.6-4build1 Β· ISC
- Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
- Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms