NAME
mandoc, man_deroff, man_meta, man_mparse, man_node, mdoc_deroff, mdoc_meta, mdoc_node, mparse_alloc, mparse_free, mparse_getkeep, mparse_keep, mparse_open, mparse_readfd, mparse_reset, mparse_result, mparse_strerror, mparse_strlevel mparse_wait, — mandoc macro compiler librarySYNOPSIS
#include <sys/types.h>#include <mandoc.h>
#define ASCII_NBRSP
#define ASCII_HYPH
#define ASCII_BREAK
mparse_alloc(int options, enum mandoclevel wlevel, mandocmsg mmsg, const struct mchars *mchars, char *defos);
(*mandocmsg)(enum mandocerr errtype, enum mandoclevel level, const char *file, int line, int col, const char *msg);
mparse_free(struct mparse *parse);
mparse_getkeep(const struct mparse *parse);
mparse_keep(struct mparse *parse);
mparse_open(struct mparse *parse, int *fd, const char *fname);
mparse_readfd(struct mparse *parse, int fd, const char *fname);
mparse_reset(struct mparse *parse);
mparse_result(struct mparse *parse, struct mdoc **mdoc, struct man **man, char **sodest);
mparse_strerror(enum mandocerr);
mparse_strlevel(enum mandoclevel);
mparse_wait(struct mparse *parse);
#include <mandoc.h>
#include <mdoc.h>
mdoc_deroff(char **dest, const struct mdoc_node *node);
mdoc_meta(const struct mdoc *mdoc);
mdoc_node(const struct mdoc *mdoc);
extern const char * const * mdoc_macronames;
#include <mandoc.h>
#include <man.h>
man_deroff(char **dest, const struct man_node *node);
man_meta(const struct man *man);
man_mparse(const struct man *man);
man_node(const struct man *man);
DESCRIPTION
The mandoc library parses a UNIX manual into an abstract syntax tree (AST). UNIX manuals are composed of mdoc(7) or man(7), and may be mixed with roff(7), tbl(7), and eqn(7) invocations.- initiate a parsing sequence with mchars_alloc(3) and mparse_alloc();
- open a file with open(2) or mparse_open();
- parse it with mparse_readfd();
- retrieve the syntax tree with mparse_result();
- iterate over parse nodes with mdoc_node() or man_node();
- free all allocated memory with mparse_free() and mchars_free(3), or invoke mparse_reset() and parse new files.
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).Types
- enum mandocerr
- An error or warning message during parsing.
- enum mandoclevel
- A classification of an enum mandocerr as regards system operation.
- struct mchars
- An opaque pointer to a a character table. Created with mchars_alloc(3) and freed with mchars_free(3).
- struct mparse
- An opaque pointer to a running parse sequence. Created with mparse_alloc() and freed with mparse_free(). This may be used across parsed input if mparse_reset() is called between parses.
- mandocmsg
- A prototype for a function to handle error and warning messages emitted by the parser.
Functions
- man_deroff()
- Obtain a text-only representation of a struct man_node, including text contained in its child nodes. To be used on children of the pointer returned from man_node(). When it is no longer needed, the pointer returned from man_deroff() can be passed to free(3).
- man_meta()
- Obtain the meta-data of a successful man(7) parse. This may only be used on a pointer returned by mparse_result(). Declared in <man.h>, implemented in man.c.
- man_mparse()
- Get the parser used for the current output. Declared in <man.h>, implemented in man.c.
- man_node()
- Obtain the root node of a successful man(7) parse. This may only be used on a pointer returned by mparse_result(). Declared in <man.h>, implemented in man.c.
- mdoc_deroff()
- Obtain a text-only representation of a struct mdoc_node, including text contained in its child nodes. To be used on children of the pointer returned from mdoc_node(). When it is no longer needed, the pointer returned from mdoc_deroff() can be passed to free(3).
- mdoc_meta()
- Obtain the meta-data of a successful mdoc parse. This may only be used on a pointer returned by mparse_result(). Declared in <mdoc.h>, implemented in mdoc.c.
- mdoc_node()
- Obtain the root node of a successful mdoc parse. This may only be used on a pointer returned by mparse_result(). Declared in <mdoc.h>, implemented in mdoc.c.
- mparse_alloc()
-
Allocate a parser. The arguments have the following effect:
- options
-
When the MPARSE_MDOC or MPARSE_MAN bit is set, only that parser is used. Otherwise, the document type is automatically detected.
- wlevel
- Can be set to MANDOCLEVEL_BADARG, MANDOCLEVEL_ERROR, or MANDOCLEVEL_WARNING. Messages below the selected level will be suppressed.
- mmsg
- A callback function to handle errors and warnings. See main.c for an example.
- mchars
- An opaque pointer to a a character table obtained from mchars_alloc(3).
- defos
- A default string for the mdoc(7) ‘Os’ macro, overriding the OSNAME preprocessor definition and the results of uname(3).
- mparse_free()
- Free all memory allocated by mparse_alloc(). Declared in <mandoc.h>, implemented in read.c.
- mparse_getkeep()
- Acquire the keep buffer. Must follow a call of mparse_keep(). Declared in <mandoc.h>, implemented in read.c.
- mparse_keep()
- Instruct the parser to retain a copy of its parsed input. This can be acquired with subsequent mparse_getkeep() calls. Declared in <mandoc.h>, implemented in read.c.
- mparse_open()
- If the fname ends in .gz, open with gunzip(1); otherwise, with open(2). If open(2) fails, append .gz and try with gunzip(1). Return a file descriptor open for reading in fd, or -1 on failure. It can be passed to mparse_readfd() or used directly. Declared in <mandoc.h>, implemented in read.c.
- mparse_readfd()
- Parse a file descriptor opened with open(2) or mparse_open(). Pass the associated filename in fname. Calls mparse_wait() before returning. This function may be called multiple times with different parameters; however, mparse_reset() should be invoked between parses. Declared in <mandoc.h>, implemented in read.c.
- mparse_reset()
- Reset a parser so that mparse_readfd() may be used again. Declared in <mandoc.h>, implemented in read.c.
- mparse_result()
- Obtain the result of a parse. One of the three pointers will be filled in. Declared in <mandoc.h>, implemented in read.c.
- mparse_strerror()
- Return a statically-allocated string representation of an error code. Declared in <mandoc.h>, implemented in read.c.
- mparse_strlevel()
- Return a statically-allocated string representation of a level code. Declared in <mandoc.h>, implemented in read.c.
- mparse_wait()
- Bury a gunzip(1) child process that was spawned with mparse_open(). To be called after the parse sequence is complete. Not needed after mparse_readfd(), but does no harm in that case, either. Returns MANDOCLEVEL_OK on success and MANDOCLEVEL_SYSERR on failure, that is, when wait(2) fails, or when gunzip(1) died from a signal or exited with non-zero status. Declared in <mandoc.h>, implemented in read.c.
Variables
- man_macronames
- The string representation of a man macro as indexed by enum mant.
- mdoc_argnames
- The string representation of a mdoc macro argument as indexed by enum mdocargt.
- mdoc_macronames
- The string representation of a mdoc macro as indexed by enum mdoct.
IMPLEMENTATION NOTES
This section consists of structural documentation for mdoc(7) and man(7) syntax trees and strings.Man and Mdoc Strings
Strings may be extracted from mdoc and man meta-data, or from text nodes (MDOC_TEXT and MAN_TEXT, respectively). These strings have special non-printing formatting cues embedded in the text itself, as well as roff(7) escapes preserved from input. Implementing systems will need to handle both situations to produce human-readable text. In general, strings may be assumed to consist of 7-bit ASCII characters.- ASCII_NBRSP
- A non-breaking space character.
- ASCII_HYPH
- A soft hyphen.
- ASCII_BREAK
- A breakable zero-width space.
Man Abstract Syntax Tree
This AST is governed by the ontological rules dictated in man(7) and derives its terminology accordingly.- ROOT
- ← mnode+
- mnode
- ← ELEMENT | TEXT | BLOCK
- BLOCK
- ← HEAD BODY
- HEAD
- ← mnode*
- BODY
- ← mnode*
- ELEMENT
- ← ELEMENT | TEXT*
- TEXT
- ← [[:ascii:]]*
Mdoc Abstract Syntax Tree
This AST is governed by the ontological rules dictated in mdoc(7) and derives its terminology accordingly. “In-line” elements described in mdoc(7) are described simply as “elements”.- 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:]]*
.Ao ao .Bo bo ac .Ac bc .Bc end
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
<ao [bo ac> bc] end