MAN(3) BSD Library Functions Manual MAN(3)NAME
man, man_alloc, man_endparse, man_free, man_meta, man_node, man_parseln,
man_reset — man macro compiler library
SYNOPSIS
#include <mandoc.h>
#include <man.h>
extern const char * const * man_macronames;
int
man_addspan(struct man *man, const struct tbl_span *span);
struct man *
man_alloc(struct regset *regs, void *data, mandocmsg msgs);
int
man_endparse(struct man *man);
void
man_free(struct man *man);
const struct man_meta *
man_meta(const struct man *man);
const struct man_node *
man_node(const struct man *man);
int
man_parseln(struct man *man, int line, char *buf);
void
man_reset(struct man *man);
DESCRIPTION
The man library parses lines of man(7) input into an abstract syntax tree
(AST).
In general, applications initiate a parsing sequence with man_alloc(),
parse each line in a document with man_parseln(), close the parsing ses‐
sion with man_endparse(), operate over the syntax tree returned by
man_node() and man_meta(), then free all allocated memory with
man_free(). The man_reset() function may be used in order to reset the
parser for another input sequence.
Beyond the full set of macros defined in man(7), the man library also
accepts the following macro:
PD Has no effect. Handled as a current-scope line macro.
Types
struct man
An opaque type. Its values are only used privately within the library.
struct man_node
A parsed node. See Abstract Syntax Tree for details.
Functions
If man_addspan(), man_parseln(), or man_endparse() return 0, calls to any
function but man_reset() or man_free() will raise an assertion.
man_addspan()
Add a table span to the parsing stream. Returns 0 on failure, 1 on suc‐
cess.
man_alloc()
Allocates a parsing structure. The data pointer is passed to msgs.
Always returns a valid pointer. The pointer must be freed with
man_free().
man_reset()
Reset the parser for another parse routine. After its use, man_parseln()
behaves as if invoked for the first time.
man_free()
Free all resources of a parser. The pointer is no longer valid after
invocation.
man_parseln()
Parse a nil-terminated line of input. This line should not contain the
trailing newline. Returns 0 on failure, 1 on success. The input buffer
buf is modified by this function.
man_endparse()
Signals that the parse is complete. Returns 0 on failure, 1 on success.
man_node()
Returns the first node of the parse.
man_meta()
Returns the document's parsed meta-data.
Variables
The following variables are also defined:
man_macronames
An array of string-ified token names.
Abstract Syntax Tree
The man functions produce an abstract syntax tree (AST) describing input
in a regular form. It may be reviewed at any time with man_nodes(); how‐
ever, if called before man_endparse(), or after man_endparse() or
man_parseln() fail, it may be incomplete.
This AST is governed by the ontological rules dictated in man(7) and
derives its terminology accordingly.
The AST is composed of struct man_node nodes with element, root and text
types as declared by the type field. Each node also provides its parse
point (the line, sec, and pos fields), its position in the tree (the
parent, child, next and prev fields) and some type-specific data.
The tree itself is arranged according to the following 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 ← [[:alpha:]]*
The only elements capable of nesting other elements are those with next-
lint scope as documented in man(7).
EXAMPLES
The following example reads lines from stdin and parses them, operating
on the finished parse tree with parsed(). This example does not error-
check nor free memory upon failure.
struct regset regs;
struct man *man;
struct man_node *node;
char *buf;
size_t len;
int line;
bzero(®s, sizeof(struct regset));
line = 1;
man = man_alloc(®s, NULL, NULL);
buf = NULL;
alloc_len = 0;
while ((len = getline(&buf, &alloc_len, stdin)) >= 0) {
if (len && buflen[len - 1] = '\n')
buf[len - 1] = '\0';
if ( ! man_parseln(man, line, buf))
errx(1, "man_parseln");
line++;
}
free(buf);
if ( ! man_endparse(man))
errx(1, "man_endparse");
if (NULL == (node = man_node(man)))
errx(1, "man_node");
parsed(man, node);
man_free(man);
To compile this, execute
% cc main.c libman.a libmandoc.a
where main.c is the example file.
SEE ALSOmandoc(1), man(7)AUTHORS
The man library was written by Kristaps Dzonsons ⟨kristaps@bsd.lv⟩.
BSD January 3, 2011 BSD