1 .\" $Id: mandoc.3,v 1.36 2016/01/08 17:48:09 schwarze Exp $
3 .\" Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 .\" Copyright (c) 2010-2016 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: January 8 2016 $
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"
78 .Fa "struct mparse *parse"
79 .Fa "const char *fname"
81 .Ft "enum mandoclevel"
83 .Fa "struct mparse *parse"
85 .Fa "const char *fname"
89 .Fa "struct mparse *parse"
93 .Fa "struct mparse *parse"
94 .Fa "struct mdoc **mdoc"
95 .Fa "struct man **man"
104 .Fa "enum mandoclevel"
112 .Fa "const struct mdoc_node *node"
114 .Ft "const struct mdoc_meta *"
116 .Fa "const struct mdoc *mdoc"
118 .Ft "const struct mdoc_node *"
120 .Fa "const struct mdoc *mdoc"
122 .Vt extern const char * const * mdoc_argnames;
123 .Vt extern const char * const * mdoc_macronames;
130 .Fa "const struct man_node *node"
132 .Ft "const struct man_meta *"
134 .Fa "const struct man *man"
136 .Ft "const struct mparse *"
138 .Fa "const struct man *man"
140 .Ft "const struct man_node *"
142 .Fa "const struct man *man"
144 .Vt extern const char * const * man_macronames;
150 manual into an abstract syntax tree (AST).
152 manuals are composed of
156 and may be mixed with
163 The following describes a general parse sequence:
166 initiate a parsing sequence with
182 retrieve the syntax tree 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 An 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 mparse"
215 An opaque pointer to a running parse sequence.
220 This may be used across parsed input if
222 is called between parses.
224 A prototype for a function to handle error and warning
225 messages emitted by the parser.
230 Obtain a text-only representation of a
231 .Vt struct man_node ,
232 including text contained in its child nodes.
233 To be used on children of the pointer returned from
235 When it is no longer needed, the pointer returned from
240 Obtain the meta-data of a successful
243 This may only be used on a pointer returned by
250 Get the parser used for the current output.
256 Obtain the root node of a successful
259 This may only be used on a pointer returned by
266 Obtain a text-only representation of a
267 .Vt struct mdoc_node ,
268 including text contained in its child nodes.
269 To be used on children of the pointer returned from
271 When it is no longer needed, the pointer returned from
276 Obtain the meta-data of a successful
279 This may only be used on a pointer returned by
286 Obtain the root node of a successful
289 This may only be used on a pointer returned by
297 The arguments have the following effect:
298 .Bl -tag -offset 5n -width inttype
304 bit is set, only that parser is used.
305 Otherwise, the document type is automatically detected.
312 file inclusion requests are always honoured.
313 Otherwise, if the request is the only content in an input file,
314 only the file name is remembered, to be returned in the
321 bit is set, parsing is aborted after the NAME section.
322 This is for example useful in
325 to quickly build minimal databases.
328 .Dv MANDOCLEVEL_BADARG ,
329 .Dv MANDOCLEVEL_ERROR ,
331 .Dv MANDOCLEVEL_WARNING .
332 Messages below the selected level will be suppressed.
334 A callback function to handle errors and warnings.
339 A default string for the
342 macro, overriding the
344 preprocessor definition and the results of
348 The same parser may be used for multiple files so long as
350 is called between parses.
352 must be called to free the memory allocated by this function.
358 Free all memory allocated by
364 .It Fn mparse_getkeep
365 Acquire the keep buffer.
366 Must follow a call of
373 Instruct the parser to retain a copy of its parsed input.
374 This can be acquired with subsequent
382 Open the file for reading.
385 does not already end in
387 try again after appending
389 Save the information whether the file is zipped or not.
390 Return a file descriptor open for reading or -1 on failure.
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,
409 should be invoked between parses.
415 Reset a parser so that
423 Obtain the result of a parse.
424 One of the three pointers will be filled in.
429 .It Fn mparse_strerror
430 Return a statically-allocated string representation of an error code.
435 .It Fn mparse_strlevel
436 Return a statically-allocated string representation of a level code.
444 .It Va man_macronames
445 The string representation of a man macro as indexed by
448 The string representation of a mdoc macro argument as indexed by
449 .Vt "enum mdocargt" .
450 .It Va mdoc_macronames
451 The string representation of a mdoc macro as indexed by
454 .Sh IMPLEMENTATION NOTES
455 This section consists of structural documentation for
459 syntax trees and strings.
460 .Ss Man and Mdoc Strings
461 Strings may be extracted from mdoc and man meta-data, or from text
462 nodes (MDOC_TEXT and MAN_TEXT, respectively).
463 These strings have special non-printing formatting cues embedded in the
464 text itself, as well as
466 escapes preserved from input.
467 Implementing systems will need to handle both situations to produce
469 In general, strings may be assumed to consist of 7-bit ASCII characters.
471 The following non-printing characters may be embedded in text strings:
474 A non-breaking space character.
478 A breakable zero-width space.
481 Escape characters are also passed verbatim into text strings.
482 An escape character is a sequence of characters beginning with the
485 To construct human-readable text, these should be intercepted with
487 and converted with one the functions described in
489 .Ss Man Abstract Syntax Tree
490 This AST is governed by the ontological rules dictated in
492 and derives its terminology accordingly.
494 The AST is composed of
496 nodes with element, root and text types as declared by the
499 Each node also provides its parse point (the
504 fields), its position in the tree (the
510 fields) and some type-specific data.
512 The tree itself is arranged according to the following normal form,
513 where capitalised non-terminals represent nodes.
515 .Bl -tag -width "ELEMENTXX" -compact
519 \(<- ELEMENT | TEXT | BLOCK
532 The only elements capable of nesting other elements are those with
533 next-line scope as documented in
535 .Ss Mdoc Abstract Syntax Tree
536 This AST is governed by the ontological
539 and derives its terminology accordingly.
541 elements described in
543 are described simply as
546 The AST is composed of
548 nodes with block, head, body, element, root and text types as declared
552 Each node also provides its parse point (the
557 fields), its position in the tree (the
564 fields) and some type-specific data, in particular, for nodes generated
565 from macros, the generating macro in the
569 The tree itself is arranged according to the following normal form,
570 where capitalised non-terminals represent nodes.
572 .Bl -tag -width "ELEMENTXX" -compact
576 \(<- BLOCK | ELEMENT | TEXT
578 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
584 \(<- mnode* [ENDBODY mnode*]
591 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
592 the BLOCK production: these refer to punctuation marks.
593 Furthermore, although a TEXT node will generally have a non-zero-length
594 string, in the specific case of
595 .Sq \&.Bd \-literal ,
596 an empty line will produce a zero-length string.
597 Multiple body parts are only found in invocations of
599 where a new body introduces a new phrase.
603 syntax tree accommodates for broken block structures as well.
604 The ENDBODY node is available to end the formatting associated
605 with a given block before the physical end of that block.
608 field, is of the BODY
612 as the BLOCK it is ending, and has a
614 field pointing to that BLOCK's BODY node.
615 It is an indirect child of that BODY node
616 and has no children of its own.
618 An ENDBODY node is generated when a block ends while one of its child
619 blocks is still open, like in the following example:
620 .Bd -literal -offset indent
627 This example results in the following block structure:
628 .Bd -literal -offset indent
633 BLOCK Bo, pending -> Ao
638 ENDBODY Ao, pending -> Ao
643 Here, the formatting of the
645 block extends from TEXT ao to TEXT ac,
646 while the formatting of the
648 block extends from TEXT bo to TEXT bc.
649 It renders as follows in
653 .Dl <ao [bo ac> bc] end
655 Support for badly-nested blocks is only provided for backward
656 compatibility with some older
659 Using badly-nested blocks is
660 .Em strongly discouraged ;
667 are unable to render them in any meaningful way.
668 Furthermore, behaviour when encountering badly-nested blocks is not
669 consistent across troff implementations, especially when using multiple
670 levels of badly-nested blocks.
673 .Xr mandoc_escape 3 ,
674 .Xr mandoc_malloc 3 ,
685 library was written by
686 .An Kristaps Dzonsons Aq Mt kristaps@bsd.lv .