1 .\" $Id: mandoc.3,v 1.33 2015/10/13 22:59:54 schwarze Exp $
3 .\" Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 .\" Copyright (c) 2010, 2013, 2014, 2015 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: October 13 2015 $
40 .Nd mandoc macro compiler library
45 .Fd "#define ASCII_NBRSP"
46 .Fd "#define ASCII_HYPH"
47 .Fd "#define ASCII_BREAK"
51 .Fa "enum mandoclevel wlevel"
57 .Fa "enum mandocerr errtype"
58 .Fa "enum mandoclevel level"
59 .Fa "const char *file"
66 .Fa "struct mparse *parse"
70 .Fa "const struct mparse *parse"
74 .Fa "struct mparse *parse"
76 .Ft "enum mandoclevel"
78 .Fa "struct mparse *parse"
80 .Fa "const char *fname"
82 .Ft "enum mandoclevel"
84 .Fa "struct mparse *parse"
86 .Fa "const char *fname"
90 .Fa "struct mparse *parse"
94 .Fa "struct mparse *parse"
95 .Fa "struct mdoc **mdoc"
96 .Fa "struct man **man"
105 .Fa "enum mandoclevel"
113 .Fa "const struct mdoc_node *node"
115 .Ft "const struct mdoc_meta *"
117 .Fa "const struct mdoc *mdoc"
119 .Ft "const struct mdoc_node *"
121 .Fa "const struct mdoc *mdoc"
123 .Vt extern const char * const * mdoc_argnames;
124 .Vt extern const char * const * mdoc_macronames;
131 .Fa "const struct man_node *node"
133 .Ft "const struct man_meta *"
135 .Fa "const struct man *man"
137 .Ft "const struct mparse *"
139 .Fa "const struct man *man"
141 .Ft "const struct man_node *"
143 .Fa "const struct man *man"
145 .Vt extern const char * const * man_macronames;
151 manual into an abstract syntax tree (AST).
153 manuals are composed of
157 and may be mixed with
164 The following describes a general parse sequence:
167 initiate a parsing sequence with
180 retrieve the syntax tree with
183 iterate over parse nodes with
188 free all allocated memory with
197 This section documents the functions, types, and variables available
200 with the exception of those documented in
206 .It Vt "enum mandocerr"
207 An error or warning message during parsing.
208 .It Vt "enum mandoclevel"
209 A classification of an
211 as regards system operation.
212 .It Vt "struct mparse"
213 An opaque pointer to a running parse sequence.
218 This may be used across parsed input if
220 is called between parses.
222 A prototype for a function to handle error and warning
223 messages emitted by the parser.
228 Obtain a text-only representation of a
229 .Vt struct man_node ,
230 including text contained in its child nodes.
231 To be used on children of the pointer returned from
233 When it is no longer needed, the pointer returned from
238 Obtain the meta-data of a successful
241 This may only be used on a pointer returned by
248 Get the parser used for the current output.
254 Obtain the root node of a successful
257 This may only be used on a pointer returned by
264 Obtain a text-only representation of a
265 .Vt struct mdoc_node ,
266 including text contained in its child nodes.
267 To be used on children of the pointer returned from
269 When it is no longer needed, the pointer returned from
274 Obtain the meta-data of a successful
277 This may only be used on a pointer returned by
284 Obtain the root node of a successful
287 This may only be used on a pointer returned by
295 The arguments have the following effect:
296 .Bl -tag -offset 5n -width inttype
302 bit is set, only that parser is used.
303 Otherwise, the document type is automatically detected.
310 file inclusion requests are always honoured.
311 Otherwise, if the request is the only content in an input file,
312 only the file name is remembered, to be returned in the
319 bit is set, parsing is aborted after the NAME section.
320 This is for example useful in
323 to quickly build minimal databases.
326 .Dv MANDOCLEVEL_BADARG ,
327 .Dv MANDOCLEVEL_ERROR ,
329 .Dv MANDOCLEVEL_WARNING .
330 Messages below the selected level will be suppressed.
332 A callback function to handle errors and warnings.
337 A default string for the
340 macro, overriding the
342 preprocessor definition and the results of
346 The same parser may be used for multiple files so long as
348 is called between parses.
350 must be called to free the memory allocated by this function.
356 Free all memory allocated by
362 .It Fn mparse_getkeep
363 Acquire the keep buffer.
364 Must follow a call of
371 Instruct the parser to retain a copy of its parsed input.
372 This can be acquired with subsequent
380 Open the file for reading.
383 does not already end in
385 try again after appending
387 Save the information whether the file is zipped or not.
388 Return a file descriptor open for reading in
399 Parse a file descriptor opened with
403 Pass the associated filename in
405 This function may be called multiple times with different parameters; however,
407 should be invoked between parses.
413 Reset a parser so that
421 Obtain the result of a parse.
422 One of the three pointers will be filled in.
427 .It Fn mparse_strerror
428 Return a statically-allocated string representation of an error code.
433 .It Fn mparse_strlevel
434 Return a statically-allocated string representation of a level code.
442 .It Va man_macronames
443 The string representation of a man macro as indexed by
446 The string representation of a mdoc macro argument as indexed by
447 .Vt "enum mdocargt" .
448 .It Va mdoc_macronames
449 The string representation of a mdoc macro as indexed by
452 .Sh IMPLEMENTATION NOTES
453 This section consists of structural documentation for
457 syntax trees and strings.
458 .Ss Man and Mdoc Strings
459 Strings may be extracted from mdoc and man meta-data, or from text
460 nodes (MDOC_TEXT and MAN_TEXT, respectively).
461 These strings have special non-printing formatting cues embedded in the
462 text itself, as well as
464 escapes preserved from input.
465 Implementing systems will need to handle both situations to produce
467 In general, strings may be assumed to consist of 7-bit ASCII characters.
469 The following non-printing characters may be embedded in text strings:
472 A non-breaking space character.
476 A breakable zero-width space.
479 Escape characters are also passed verbatim into text strings.
480 An escape character is a sequence of characters beginning with the
483 To construct human-readable text, these should be intercepted with
485 and converted with one the functions described in
487 .Ss Man Abstract Syntax Tree
488 This AST is governed by the ontological rules dictated in
490 and derives its terminology accordingly.
492 The AST is composed of
494 nodes with element, root and text types as declared by the
497 Each node also provides its parse point (the
502 fields), its position in the tree (the
508 fields) and some type-specific data.
510 The tree itself is arranged according to the following normal form,
511 where capitalised non-terminals represent nodes.
513 .Bl -tag -width "ELEMENTXX" -compact
517 \(<- ELEMENT | TEXT | BLOCK
530 The only elements capable of nesting other elements are those with
531 next-line scope as documented in
533 .Ss Mdoc Abstract Syntax Tree
534 This AST is governed by the ontological
537 and derives its terminology accordingly.
539 elements described in
541 are described simply as
544 The AST is composed of
546 nodes with block, head, body, element, root and text types as declared
550 Each node also provides its parse point (the
555 fields), its position in the tree (the
562 fields) and some type-specific data, in particular, for nodes generated
563 from macros, the generating macro in the
567 The tree itself is arranged according to the following normal form,
568 where capitalised non-terminals represent nodes.
570 .Bl -tag -width "ELEMENTXX" -compact
574 \(<- BLOCK | ELEMENT | TEXT
576 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
582 \(<- mnode* [ENDBODY mnode*]
589 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
590 the BLOCK production: these refer to punctuation marks.
591 Furthermore, although a TEXT node will generally have a non-zero-length
592 string, in the specific case of
593 .Sq \&.Bd \-literal ,
594 an empty line will produce a zero-length string.
595 Multiple body parts are only found in invocations of
597 where a new body introduces a new phrase.
601 syntax tree accommodates for broken block structures as well.
602 The ENDBODY node is available to end the formatting associated
603 with a given block before the physical end of that block.
606 field, is of the BODY
610 as the BLOCK it is ending, and has a
612 field pointing to that BLOCK's BODY node.
613 It is an indirect child of that BODY node
614 and has no children of its own.
616 An ENDBODY node is generated when a block ends while one of its child
617 blocks is still open, like in the following example:
618 .Bd -literal -offset indent
625 This example results in the following block structure:
626 .Bd -literal -offset indent
631 BLOCK Bo, pending -> Ao
636 ENDBODY Ao, pending -> Ao
641 Here, the formatting of the
643 block extends from TEXT ao to TEXT ac,
644 while the formatting of the
646 block extends from TEXT bo to TEXT bc.
647 It renders as follows in
651 .Dl <ao [bo ac> bc] end
653 Support for badly-nested blocks is only provided for backward
654 compatibility with some older
657 Using badly-nested blocks is
658 .Em strongly discouraged ;
665 are unable to render them in any meaningful way.
666 Furthermore, behaviour when encountering badly-nested blocks is not
667 consistent across troff implementations, especially when using multiple
668 levels of badly-nested blocks.
671 .Xr mandoc_escape 3 ,
672 .Xr mandoc_malloc 3 ,
683 library was written by
684 .An Kristaps Dzonsons Aq Mt kristaps@bsd.lv .