1 .\" $Id: mandoc.3,v 1.29 2014/11/26 23:42:14 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: November 26 2014 $
41 .Nd mandoc macro compiler library
47 .Fd "#define ASCII_NBRSP"
48 .Fd "#define ASCII_HYPH"
49 .Fd "#define ASCII_BREAK"
53 .Fa "enum mandoclevel wlevel"
55 .Fa "const struct mchars *mchars"
60 .Fa "enum mandocerr errtype"
61 .Fa "enum mandoclevel level"
62 .Fa "const char *file"
69 .Fa "struct mparse *parse"
73 .Fa "const struct mparse *parse"
77 .Fa "struct mparse *parse"
79 .Ft "enum mandoclevel"
81 .Fa "struct mparse *parse"
83 .Fa "const char *fname"
85 .Ft "enum mandoclevel"
87 .Fa "struct mparse *parse"
89 .Fa "const char *fname"
93 .Fa "struct mparse *parse"
97 .Fa "struct mparse *parse"
98 .Fa "struct mdoc **mdoc"
99 .Fa "struct man **man"
108 .Fa "enum mandoclevel"
110 .Ft "enum mandoclevel"
112 .Fa "struct mparse *parse"
120 .Fa "const struct mdoc_node *node"
122 .Ft "const struct mdoc_meta *"
124 .Fa "const struct mdoc *mdoc"
126 .Ft "const struct mdoc_node *"
128 .Fa "const struct mdoc *mdoc"
130 .Vt extern const char * const * mdoc_argnames;
131 .Vt extern const char * const * mdoc_macronames;
138 .Fa "const struct man_node *node"
140 .Ft "const struct man_meta *"
142 .Fa "const struct man *man"
144 .Ft "const struct mparse *"
146 .Fa "const struct man *man"
148 .Ft "const struct man_node *"
150 .Fa "const struct man *man"
152 .Vt extern const char * const * man_macronames;
158 manual into an abstract syntax tree (AST).
160 manuals are composed of
164 and may be mixed with
171 The following describes a general parse sequence:
174 initiate a parsing sequence with
179 parse files or file descriptors with
182 retrieve a parsed syntax tree, if the parse was successful, with
185 iterate over parse nodes with
190 free all allocated memory with
199 This section documents the functions, types, and variables available
202 with the exception of those documented in
208 .It Vt "enum mandocerr"
209 A fatal error, error, or warning message during parsing.
210 .It Vt "enum mandoclevel"
211 A classification of an
213 as regards system operation.
214 .It Vt "struct mchars"
215 An opaque pointer to a a character table.
220 .It Vt "struct mparse"
221 An opaque pointer to a running parse sequence.
226 This may be used across parsed input if
228 is called between parses.
230 A prototype for a function to handle fatal error, error, and warning
231 messages emitted by the parser.
236 Obtain a text-only representation of a
237 .Vt struct man_node ,
238 including text contained in its child nodes.
239 To be used on children of the pointer returned from
241 When it is no longer needed, the pointer returned from
246 Obtain the meta-data of a successful
249 This may only be used on a pointer returned by
256 Get the parser used for the current output.
262 Obtain the root node of a successful
265 This may only be used on a pointer returned by
272 Obtain a text-only representation of a
273 .Vt struct mdoc_node ,
274 including text contained in its child nodes.
275 To be used on children of the pointer returned from
277 When it is no longer needed, the pointer returned from
282 Obtain the meta-data of a successful
285 This may only be used on a pointer returned by
292 Obtain the root node of a successful
295 This may only be used on a pointer returned by
303 The arguments have the following effect:
304 .Bl -tag -offset 5n -width inttype
310 bit is set, only that parser is used.
311 Otherwise, the document type is automatically detected.
318 file inclusion requests are always honoured.
319 Otherwise, if the request is the only content in an input file,
320 only the file name is remembered, to be returned in the
327 bit is set, parsing is aborted after the NAME section.
328 This is for example useful in
331 to quickly build minimal databases.
334 .Dv MANDOCLEVEL_FATAL ,
335 .Dv MANDOCLEVEL_ERROR ,
337 .Dv MANDOCLEVEL_WARNING .
338 Messages below the selected level will be suppressed.
340 A callback function to handle errors and warnings.
345 An opaque pointer to a a character table obtained from
348 A default string for the
351 macro, overriding the
353 preprocessor definition and the results of
357 The same parser may be used for multiple files so long as
359 is called between parses.
361 must be called to free the memory allocated by this function.
367 Free all memory allocated by
373 .It Fn mparse_getkeep
374 Acquire the keep buffer.
375 Must follow a call of
382 Instruct the parser to retain a copy of its parsed input.
383 This can be acquired with subsequent
405 Return a file descriptor open for reading in
416 Parse a file or file descriptor.
425 is assumed to be the name associated with
430 This function may be called multiple times with different parameters; however,
432 should be invoked between parses.
438 Reset a parser so that
446 Obtain the result of a parse.
447 Only successful parses
451 returned less than MANDOCLEVEL_FATAL
453 should invoke this function, in which case one of the three pointers will
459 .It Fn mparse_strerror
460 Return a statically-allocated string representation of an error code.
465 .It Fn mparse_strlevel
466 Return a statically-allocated string representation of a level code.
474 child process that was spawned with
476 To be called after the parse sequence is complete.
479 but does no harm in that case, either.
483 .Dv MANDOCLEVEL_SYSERR
484 on failure, that is, when
488 died from a signal or exited with non-zero status.
496 .It Va man_macronames
497 The string representation of a man macro as indexed by
500 The string representation of a mdoc macro argument as indexed by
501 .Vt "enum mdocargt" .
502 .It Va mdoc_macronames
503 The string representation of a mdoc macro as indexed by
506 .Sh IMPLEMENTATION NOTES
507 This section consists of structural documentation for
511 syntax trees and strings.
512 .Ss Man and Mdoc Strings
513 Strings may be extracted from mdoc and man meta-data, or from text
514 nodes (MDOC_TEXT and MAN_TEXT, respectively).
515 These strings have special non-printing formatting cues embedded in the
516 text itself, as well as
518 escapes preserved from input.
519 Implementing systems will need to handle both situations to produce
521 In general, strings may be assumed to consist of 7-bit ASCII characters.
523 The following non-printing characters may be embedded in text strings:
526 A non-breaking space character.
530 A breakable zero-width space.
533 Escape characters are also passed verbatim into text strings.
534 An escape character is a sequence of characters beginning with the
537 To construct human-readable text, these should be intercepted with
539 and converted with one the functions described in
541 .Ss Man Abstract Syntax Tree
542 This AST is governed by the ontological rules dictated in
544 and derives its terminology accordingly.
546 The AST is composed of
548 nodes with element, root and text types as declared by the
551 Each node also provides its parse point (the
556 fields), its position in the tree (the
562 fields) and some type-specific data.
564 The tree itself is arranged according to the following normal form,
565 where capitalised non-terminals represent nodes.
567 .Bl -tag -width "ELEMENTXX" -compact
571 \(<- ELEMENT | TEXT | BLOCK
584 The only elements capable of nesting other elements are those with
585 next-line scope as documented in
587 .Ss Mdoc Abstract Syntax Tree
588 This AST is governed by the ontological
591 and derives its terminology accordingly.
593 elements described in
595 are described simply as
598 The AST is composed of
600 nodes with block, head, body, element, root and text types as declared
604 Each node also provides its parse point (the
609 fields), its position in the tree (the
616 fields) and some type-specific data, in particular, for nodes generated
617 from macros, the generating macro in the
621 The tree itself is arranged according to the following normal form,
622 where capitalised non-terminals represent nodes.
624 .Bl -tag -width "ELEMENTXX" -compact
628 \(<- BLOCK | ELEMENT | TEXT
630 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
636 \(<- mnode* [ENDBODY mnode*]
643 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
644 the BLOCK production: these refer to punctuation marks.
645 Furthermore, although a TEXT node will generally have a non-zero-length
646 string, in the specific case of
647 .Sq \&.Bd \-literal ,
648 an empty line will produce a zero-length string.
649 Multiple body parts are only found in invocations of
651 where a new body introduces a new phrase.
655 syntax tree accommodates for broken block structures as well.
656 The ENDBODY node is available to end the formatting associated
657 with a given block before the physical end of that block.
660 field, is of the BODY
664 as the BLOCK it is ending, and has a
666 field pointing to that BLOCK's BODY node.
667 It is an indirect child of that BODY node
668 and has no children of its own.
670 An ENDBODY node is generated when a block ends while one of its child
671 blocks is still open, like in the following example:
672 .Bd -literal -offset indent
679 This example results in the following block structure:
680 .Bd -literal -offset indent
685 BLOCK Bo, pending -> Ao
690 ENDBODY Ao, pending -> Ao
695 Here, the formatting of the
697 block extends from TEXT ao to TEXT ac,
698 while the formatting of the
700 block extends from TEXT bo to TEXT bc.
701 It renders as follows in
705 .Dl <ao [bo ac> bc] end
707 Support for badly-nested blocks is only provided for backward
708 compatibility with some older
711 Using badly-nested blocks is
712 .Em strongly discouraged ;
719 are unable to render them in any meaningful way.
720 Furthermore, behaviour when encountering badly-nested blocks is not
721 consistent across troff implementations, especially when using multiple
722 levels of badly-nested blocks.
725 .Xr mandoc_escape 3 ,
726 .Xr mandoc_malloc 3 ,
737 library was written by
738 .An Kristaps Dzonsons Aq Mt kristaps@bsd.lv .