EN JA
MANDOC(3)
MANDOC(3) FreeBSD Library Functions Manual MANDOC(3)

NAME

mandoc, mandoc_escape, man_meta, man_mparse, man_node, mchars_alloc, mchars_free, mchars_num2char, mchars_num2uc, mchars_spec2cp, mchars_spec2str, mdoc_meta, mdoc_node, mparse_alloc, mparse_free, mparse_getkeep, mparse_keep, mparse_readfd, mparse_reset, mparse_result, mparse_strerror, mparse_strlevelmandoc macro compiler library

LIBRARY

library “mandoc”

SYNOPSIS

#include < man.h>
#include < mdoc.h>
#include < mandoc.h>

enum mandoc_esc
mandoc_escape( const char **end, const char **start, int *sz);

const struct man_meta *
man_meta( const struct man *man);

const struct mparse *
man_mparse( const struct man *man);

const struct man_node *
man_node( const struct man *man);

struct mchars *
mchars_alloc();

void
mchars_free( struct mchars *p);

char
mchars_num2char( const char *cp, size_t sz);

int
mchars_num2uc( const char *cp, size_t sz);

const char *
mchars_spec2str( const struct mchars *p, const char *cp, size_t sz, size_t *rsz);

int
mchars_spec2cp( const struct mchars *p, const char *cp, size_t sz
const char *);

const struct mdoc_meta *
mdoc_meta( const struct mdoc *mdoc);

const struct mdoc_node *
mdoc_node( const struct mdoc *mdoc);

void
mparse_alloc( enum mparset type, enum mandoclevel wlevel, mandocmsg msg, void *msgarg);

void
mparse_free( struct mparse *parse);

void
mparse_getkeep( const struct mparse *parse);

void
mparse_keep( struct mparse *parse);

enum mandoclevel
mparse_readfd( struct mparse *parse, int fd, const char *fname);

void
mparse_reset( struct mparse *parse);

void
mparse_result( struct mparse *parse, struct mdoc **mdoc, struct man **man);

const char *
mparse_strerror( enum mandocerr);

const char *
mparse_strlevel( enum mandoclevel);

extern const char * const * man_macronames;
extern const char * const * mdoc_argnames;
extern const char * const * mdoc_macronames;

#define ASCII_NBRSP
#define ASCII_HYPH

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.

The following describes a general parse sequence:

  1. initiate a parsing sequence with mparse_alloc();
  2. parse files or file descriptors with mparse_readfd();
  3. retrieve a parsed syntax tree, if the parse was successful, with mparse_result();
  4. iterate over parse nodes with mdoc_node() or man_node();
  5. free all allocated memory with mparse_free(), or invoke mparse_reset() and parse new files.

The mandoc library also contains routines for translating character strings into glyphs (see mchars_alloc()) and parsing escape sequences from strings (see mandoc_escape()).

REFERENCE

This section documents the functions, types, and variables available via < mandoc.h>.

Types

enum mandoc_esc
An escape sequence classification.
enum mandocerr
A fatal error, error, or warning message during parsing.
enum mandoclevel
A classification of an enum mandoclevel as regards system operation.
struct mchars
An opaque pointer to an object allowing for translation between character strings and glyphs. See mchars_alloc().
enum mparset
The type of parser when reading input. This should usually be MPARSE_AUTO for auto-detection.
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 fatal error, error, and warning messages emitted by the parser.

Functions

mandoc_escape()
Scan an escape sequence, i.e., a character string beginning with ‘\’. Pass a pointer to the character after the ‘\’ as end; it will be set to the supremum of the parsed escape sequence unless returning ESCAPE_ERROR, in which case the string is bogus and should be thrown away. If not ESCAPE_ERROR or ESCAPE_IGNORE, start is set to the first relevant character of the substring (font, glyph, whatever) of length sz. Both start and sz may be NULL.
man_meta()
Obtain the meta-data of a successful parse. This may only be used on a pointer returned by mparse_result().
man_mparse()
Get the parser used for the current output.
man_node()
Obtain the root node of a successful parse. This may only be used on a pointer returned by mparse_result().
mchars_alloc()
Allocate an struct mchars * object for translating special characters into glyphs. See mandoc_char(7) for an overview of special characters. The object must be freed with mchars_free().
mchars_free()
Free an object created with mchars_alloc().
mchars_num2char()
Convert a character index (e.g., the \N'' escape) into a printable ASCII character. Returns \0 (the nil character) if the input sequence is malformed.
mchars_num2uc()
Convert a hexadecimal character index (e.g., the \[uNNNN] escape) into a Unicode codepoint. Returns \0 (the nil character) if the input sequence is malformed.
mchars_spec2cp()
Convert a special character into a valid Unicode codepoint. Returns -1 on failure or a non-zero Unicode codepoint on success.
mchars_spec2str()
Convert a special character into an ASCII string. Returns NULL on failure.
mdoc_meta()
Obtain the meta-data of a successful parse. This may only be used on a pointer returned by mparse_result().
mdoc_node()
Obtain the root node of a successful parse. This may only be used on a pointer returned by mparse_result().
mparse_alloc()
Allocate a parser. 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.
mparse_free()
Free all memory allocated by mparse_alloc().
mparse_getkeep()
Acquire the keep buffer. Must follow a call of mparse_keep().
mparse_keep()
Instruct the parser to retain a copy of its parsed input. This can be acquired with subsequent mparse_getkeep() calls.
mparse_readfd()
Parse a file or file descriptor. If fd is -1, fname is opened for reading. Otherwise, fname is assumed to be the name associated with fd. This may be called multiple times with different parameters; however, mparse_reset() should be invoked between parses.
mparse_reset()
Reset a parser so that mparse_readfd() may be used again.
mparse_result()
Obtain the result of a parse. Only successful parses (i.e., those where mparse_readfd() returned less than MANDOCLEVEL_FATAL) should invoke this function, in which case one of the two pointers will be filled in.
mparse_strerror()
Return a statically-allocated string representation of an error code.
mparse_strlevel()
Return a statically-allocated string representation of a level code.

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.

The following non-printing characters may be embedded in text strings:

ASCII_NBRSP
A non-breaking space character.
ASCII_HYPH
A soft hyphen.

Escape characters are also 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() and converted with one of mchars_num2char(), mchars_spec2str(), and so on.

Man Abstract Syntax Tree

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
← [[:ascii:]]*

The only elements capable of nesting other elements are those with next-lint scope as documented in man(7).

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”.

The AST is composed of struct mdoc_node nodes with block, head, body, 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, nchild, next and prev fields) and some type-specific data, in particular, for nodes generated from macros, the generating macro in the tok field.

The tree itself is arranged according to the following 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:]]*

Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of the BLOCK production: these refer to punctuation marks. Furthermore, although a TEXT node will generally have a non-zero-length string, in the specific case of ‘.Bd -literal’, an empty line will produce a zero-length string. Multiple body parts are only found in invocations of ‘Bl -column’, where a new body introduces a new phrase.

The mdoc(7) syntax tree accommodates for broken block structures as well. The ENDBODY node is available 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 is generated when a block ends while one of its child blocks is still open, like in the following 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 provided for backward compatibility with some older mdoc(7) implementations. Using badly-nested blocks is strongly discouraged; for example, the -Thtml and -Txhtml front-ends to mandoc(1) are unable to render them in any meaningful way. Furthermore, behaviour when encountering badly-nested blocks is not consistent across troff implementations, especially when using multiple levels of badly-nested blocks.

AUTHORS

The mandoc library was written by Kristaps Dzonsons, kristaps@bsd.lv.
January 13, 2012 FreeBSD