1 .\" $Id: mandoc.3,v 1.24 2014/03/23 11:25:26 schwarze Exp $
3 .\" Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 .\" Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
6 .\" Permission to use, copy, modify, and distribute this software for any
7 .\" purpose with or without fee is hereby granted, provided that the above
8 .\" copyright notice and this permission notice appear in all copies.
10 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 .Dd $Mdocdate: March 23 2014 $
49 .Nd mandoc macro compiler library
54 .Fd "#define ASCII_NBRSP"
55 .Fd "#define ASCII_HYPH"
56 .Fd "#define ASCII_BREAK"
64 .Fa "const char **end"
65 .Fa "const char **start"
69 .Fn mandoc_malloc "size_t size"
77 .Fn mchars_alloc "void"
79 .Fn mchars_free "struct mchars *p"
81 .Fn mchars_num2char "const char *cp" "size_t sz"
83 .Fn mchars_num2uc "const char *cp" "size_t sz"
86 .Fa "const struct mchars *p"
93 .Fa "const struct mchars *p"
99 .Fa "enum mparset inttype"
100 .Fa "enum mandoclevel wlevel"
107 .Fa "enum mandocerr errtype"
108 .Fa "enum mandoclevel level"
109 .Fa "const char *file"
112 .Fa "const char *msg"
116 .Fa "struct mparse *parse"
120 .Fa "const struct mparse *parse"
124 .Fa "struct mparse *parse"
126 .Ft "enum mandoclevel"
128 .Fa "struct mparse *parse"
130 .Fa "const char *fname"
134 .Fa "struct mparse *parse"
138 .Fa "struct mparse *parse"
139 .Fa "struct mdoc **mdoc"
140 .Fa "struct man **man"
148 .Fa "enum mandoclevel"
152 .Ft "const struct mdoc_meta *"
154 .Fa "const struct mdoc *mdoc"
156 .Ft "const struct mdoc_node *"
158 .Fa "const struct mdoc *mdoc"
160 .Vt extern const char * const * mdoc_argnames;
161 .Vt extern const char * const * mdoc_macronames;
164 .Ft "const struct man_meta *"
166 .Fa "const struct man *man"
168 .Ft "const struct mparse *"
170 .Fa "const struct man *man"
172 .Ft "const struct man_node *"
174 .Fa "const struct man *man"
176 .Vt extern const char * const * man_macronames;
182 manual into an abstract syntax tree (AST).
184 manuals are composed of
188 and may be mixed with
195 The following describes a general parse sequence:
198 initiate a parsing sequence with
201 parse files or file descriptors with
204 retrieve a parsed syntax tree, if the parse was successful, with
207 iterate over parse nodes with
212 free all allocated memory with
221 library also contains routines for translating character strings into glyphs
222 .Pq see Fn mchars_alloc
223 and parsing escape sequences from strings
224 .Pq see Fn mandoc_escape .
226 This section documents the functions, types, and variables available
231 .It Vt "enum mandoc_esc"
232 An escape sequence classification.
233 .It Vt "enum mandocerr"
234 A fatal error, error, or warning message during parsing.
235 .It Vt "enum mandoclevel"
236 A classification of an
238 as regards system operation.
239 .It Vt "struct mchars"
240 An opaque pointer to an object allowing for translation between
241 character strings and glyphs.
244 .It Vt "enum mparset"
245 The type of parser when reading input.
246 This should usually be
249 .It Vt "struct mparse"
250 An opaque pointer to a running parse sequence.
255 This may be used across parsed input if
257 is called between parses.
259 A prototype for a function to handle fatal error, error, and warning
260 messages emitted by the parser.
265 Scan an escape sequence, i.e., a character string beginning with
267 Pass a pointer to the character after the
271 it will be set to the supremum of the parsed escape sequence unless
274 in which case the string is bogus and should be
281 is set to the first relevant character of the substring (font, glyph,
295 Obtain the meta-data of a successful parse.
296 This may only be used on a pointer returned by
303 Get the parser used for the current output.
309 Obtain the root node of a successful parse.
310 This may only be used on a pointer returned by
318 .Vt "struct mchars *"
319 object for translating special characters into glyphs.
322 for an overview of special characters.
323 The object must be freed with
330 Free an object created with
336 .It Fn mchars_num2char
337 Convert a character index (e.g., the \eN\(aq\(aq escape) into a
338 printable ASCII character.
339 Returns \e0 (the nil character) if the input sequence is malformed.
345 Convert a hexadecimal character index (e.g., the \e[uNNNN] escape) into
347 Returns \e0 (the nil character) if the input sequence is malformed.
352 .It Fn mchars_spec2cp
353 Convert a special character into a valid Unicode codepoint.
354 Returns \-1 on failure or a non-zero Unicode codepoint on success.
359 .It Fn mchars_spec2str
360 Convert a special character into an ASCII string.
369 Obtain the meta-data of a successful parse.
370 This may only be used on a pointer returned by
377 Obtain the root node of a successful parse.
378 This may only be used on a pointer returned by
386 The arguments have the following effect:
387 .Bl -tag -offset 5n -width inttype
393 only that parser will be used.
396 the document type will be automatically detected.
399 .Dv MANDOCLEVEL_FATAL ,
400 .Dv MANDOCLEVEL_ERROR ,
402 .Dv MANDOCLEVEL_WARNING .
403 Messages below the selected level will be suppressed.
405 A callback function to handle errors and warnings.
410 A default string for the
413 macro, overriding the
415 preprocessor definition and the results of
418 When set, parsing is aborted after the NAME section.
419 This is for example useful to quickly build minimal databases.
422 The same parser may be used for multiple files so long as
424 is called between parses.
426 must be called to free the memory allocated by this function.
432 Free all memory allocated by
438 .It Fn mparse_getkeep
439 Acquire the keep buffer.
440 Must follow a call of
447 Instruct the parser to retain a copy of its parsed input.
448 This can be acquired with subsequent
456 Parse a file or file descriptor.
461 is opened for reading.
464 is assumed to be the name associated with
466 This may be called multiple times with different parameters; however,
468 should be invoked between parses.
474 Reset a parser so that
482 Obtain the result of a parse.
483 Only successful parses
487 returned less than MANDOCLEVEL_FATAL
489 should invoke this function, in which case one of the two pointers will
495 .It Fn mparse_strerror
496 Return a statically-allocated string representation of an error code.
501 .It Fn mparse_strlevel
502 Return a statically-allocated string representation of a level code.
510 .It Va man_macronames
511 The string representation of a man macro as indexed by
514 The string representation of a mdoc macro argument as indexed by
515 .Vt "enum mdocargt" .
516 .It Va mdoc_macronames
517 The string representation of a mdoc macro as indexed by
520 .Sh IMPLEMENTATION NOTES
521 This section consists of structural documentation for
525 syntax trees and strings.
526 .Ss Man and Mdoc Strings
527 Strings may be extracted from mdoc and man meta-data, or from text
528 nodes (MDOC_TEXT and MAN_TEXT, respectively).
529 These strings have special non-printing formatting cues embedded in the
530 text itself, as well as
532 escapes preserved from input.
533 Implementing systems will need to handle both situations to produce
535 In general, strings may be assumed to consist of 7-bit ASCII characters.
537 The following non-printing characters may be embedded in text strings:
540 A non-breaking space character.
545 Escape characters are also passed verbatim into text strings.
546 An escape character is a sequence of characters beginning with the
549 To construct human-readable text, these should be intercepted with
551 and converted with one of
552 .Fn mchars_num2char ,
553 .Fn mchars_spec2str ,
555 .Ss Man Abstract Syntax Tree
556 This AST is governed by the ontological rules dictated in
558 and derives its terminology accordingly.
560 The AST is composed of
562 nodes with element, root and text types as declared by the
565 Each node also provides its parse point (the
570 fields), its position in the tree (the
576 fields) and some type-specific data.
578 The tree itself is arranged according to the following normal form,
579 where capitalised non-terminals represent nodes.
581 .Bl -tag -width "ELEMENTXX" -compact
585 \(<- ELEMENT | TEXT | BLOCK
598 The only elements capable of nesting other elements are those with
599 next-lint scope as documented in
601 .Ss Mdoc Abstract Syntax Tree
602 This AST is governed by the ontological
605 and derives its terminology accordingly.
607 elements described in
609 are described simply as
612 The AST is composed of
614 nodes with block, head, body, element, root and text types as declared
618 Each node also provides its parse point (the
623 fields), its position in the tree (the
630 fields) and some type-specific data, in particular, for nodes generated
631 from macros, the generating macro in the
635 The tree itself is arranged according to the following normal form,
636 where capitalised non-terminals represent nodes.
638 .Bl -tag -width "ELEMENTXX" -compact
642 \(<- BLOCK | ELEMENT | TEXT
644 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
650 \(<- mnode* [ENDBODY mnode*]
657 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
658 the BLOCK production: these refer to punctuation marks.
659 Furthermore, although a TEXT node will generally have a non-zero-length
660 string, in the specific case of
661 .Sq \&.Bd \-literal ,
662 an empty line will produce a zero-length string.
663 Multiple body parts are only found in invocations of
665 where a new body introduces a new phrase.
669 syntax tree accommodates for broken block structures as well.
670 The ENDBODY node is available to end the formatting associated
671 with a given block before the physical end of that block.
674 field, is of the BODY
678 as the BLOCK it is ending, and has a
680 field pointing to that BLOCK's BODY node.
681 It is an indirect child of that BODY node
682 and has no children of its own.
684 An ENDBODY node is generated when a block ends while one of its child
685 blocks is still open, like in the following example:
686 .Bd -literal -offset indent
693 This example results in the following block structure:
694 .Bd -literal -offset indent
699 BLOCK Bo, pending -> Ao
704 ENDBODY Ao, pending -> Ao
709 Here, the formatting of the
711 block extends from TEXT ao to TEXT ac,
712 while the formatting of the
714 block extends from TEXT bo to TEXT bc.
715 It renders as follows in
719 .Dl <ao [bo ac> bc] end
721 Support for badly-nested blocks is only provided for backward
722 compatibility with some older
725 Using badly-nested blocks is
726 .Em strongly discouraged ;
733 are unable to render them in any meaningful way.
734 Furthermore, behaviour when encountering badly-nested blocks is not
735 consistent across troff implementations, especially when using multiple
736 levels of badly-nested blocks.
748 library was written by
749 .An Kristaps Dzonsons Aq Mt kristaps@bsd.lv .