1 .\" $Id: mandoc.3,v 1.27 2014/10/28 17:36:19 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: October 28 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"
84 .Fa "pid_t *child_pid"
86 .Ft "enum mandoclevel"
88 .Fa "struct mparse *parse"
90 .Fa "const char *fname"
94 .Fa "struct mparse *parse"
98 .Fa "struct mparse *parse"
99 .Fa "struct mdoc **mdoc"
100 .Fa "struct man **man"
109 .Fa "enum mandoclevel"
111 .Ft "enum mandoclevel"
113 .Fa "struct mparse *parse"
114 .Fa "pid_t child_pid"
122 .Fa "const struct mdoc_node *node"
124 .Ft "const struct mdoc_meta *"
126 .Fa "const struct mdoc *mdoc"
128 .Ft "const struct mdoc_node *"
130 .Fa "const struct mdoc *mdoc"
132 .Vt extern const char * const * mdoc_argnames;
133 .Vt extern const char * const * mdoc_macronames;
140 .Fa "const struct man_node *node"
142 .Ft "const struct man_meta *"
144 .Fa "const struct man *man"
146 .Ft "const struct mparse *"
148 .Fa "const struct man *man"
150 .Ft "const struct man_node *"
152 .Fa "const struct man *man"
154 .Vt extern const char * const * man_macronames;
160 manual into an abstract syntax tree (AST).
162 manuals are composed of
166 and may be mixed with
173 The following describes a general parse sequence:
176 initiate a parsing sequence with
181 parse files or file descriptors with
184 retrieve a parsed syntax tree, if the parse was successful, with
187 iterate over parse nodes with
192 free all allocated memory with
201 This section documents the functions, types, and variables available
204 with the exception of those documented in
210 .It Vt "enum mandocerr"
211 A fatal error, error, or warning message during parsing.
212 .It Vt "enum mandoclevel"
213 A classification of an
215 as regards system operation.
216 .It Vt "struct mchars"
217 An opaque pointer to a a character table.
222 .It Vt "struct mparse"
223 An opaque pointer to a running parse sequence.
228 This may be used across parsed input if
230 is called between parses.
232 A prototype for a function to handle fatal error, error, and warning
233 messages emitted by the parser.
238 Obtain a text-only representation of a
239 .Vt struct man_node ,
240 including text contained in its child nodes.
241 To be used on children of the pointer returned from
243 When it is no longer needed, the pointer returned from
248 Obtain the meta-data of a successful
251 This may only be used on a pointer returned by
258 Get the parser used for the current output.
264 Obtain the root node of a successful
267 This may only be used on a pointer returned by
274 Obtain a text-only representation of a
275 .Vt struct mdoc_node ,
276 including text contained in its child nodes.
277 To be used on children of the pointer returned from
279 When it is no longer needed, the pointer returned from
284 Obtain the meta-data of a successful
287 This may only be used on a pointer returned by
294 Obtain the root node of a successful
297 This may only be used on a pointer returned by
305 The arguments have the following effect:
306 .Bl -tag -offset 5n -width inttype
312 bit is set, only that parser is used.
313 Otherwise, the document type is automatically detected.
320 file inclusion requests are always honoured.
321 Otherwise, if the request is the only content in an input file,
322 only the file name is remembered, to be returned in the
329 bit is set, parsing is aborted after the NAME section.
330 This is for example useful in
333 to quickly build minimal databases.
336 .Dv MANDOCLEVEL_FATAL ,
337 .Dv MANDOCLEVEL_ERROR ,
339 .Dv MANDOCLEVEL_WARNING .
340 Messages below the selected level will be suppressed.
342 A callback function to handle errors and warnings.
347 An opaque pointer to a a character table obtained from
350 A default string for the
353 macro, overriding the
355 preprocessor definition and the results of
359 The same parser may be used for multiple files so long as
361 is called between parses.
363 must be called to free the memory allocated by this function.
369 Free all memory allocated by
375 .It Fn mparse_getkeep
376 Acquire the keep buffer.
377 Must follow a call of
384 Instruct the parser to retain a copy of its parsed input.
385 This can be acquired with subsequent
401 Return a file descriptor open for reading in
407 If applicable, return the
412 If non-zero, it should be passed to
414 after completing the parse sequence.
420 Parse a file or file descriptor.
425 is opened for reading.
428 is assumed to be the name associated with
430 This 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.
476 that was spawned with
478 To be called after the parse sequence is complete.
482 .Dv MANDOCLEVEL_SYSERR
483 on failure, that is, when
487 died from a signal or exited with non-zero status.
495 .It Va man_macronames
496 The string representation of a man macro as indexed by
499 The string representation of a mdoc macro argument as indexed by
500 .Vt "enum mdocargt" .
501 .It Va mdoc_macronames
502 The string representation of a mdoc macro as indexed by
505 .Sh IMPLEMENTATION NOTES
506 This section consists of structural documentation for
510 syntax trees and strings.
511 .Ss Man and Mdoc Strings
512 Strings may be extracted from mdoc and man meta-data, or from text
513 nodes (MDOC_TEXT and MAN_TEXT, respectively).
514 These strings have special non-printing formatting cues embedded in the
515 text itself, as well as
517 escapes preserved from input.
518 Implementing systems will need to handle both situations to produce
520 In general, strings may be assumed to consist of 7-bit ASCII characters.
522 The following non-printing characters may be embedded in text strings:
525 A non-breaking space character.
529 A breakable zero-width space.
532 Escape characters are also passed verbatim into text strings.
533 An escape character is a sequence of characters beginning with the
536 To construct human-readable text, these should be intercepted with
538 and converted with one the functions described in
540 .Ss Man Abstract Syntax Tree
541 This AST is governed by the ontological rules dictated in
543 and derives its terminology accordingly.
545 The AST is composed of
547 nodes with element, root and text types as declared by the
550 Each node also provides its parse point (the
555 fields), its position in the tree (the
561 fields) and some type-specific data.
563 The tree itself is arranged according to the following normal form,
564 where capitalised non-terminals represent nodes.
566 .Bl -tag -width "ELEMENTXX" -compact
570 \(<- ELEMENT | TEXT | BLOCK
583 The only elements capable of nesting other elements are those with
584 next-line scope as documented in
586 .Ss Mdoc Abstract Syntax Tree
587 This AST is governed by the ontological
590 and derives its terminology accordingly.
592 elements described in
594 are described simply as
597 The AST is composed of
599 nodes with block, head, body, element, root and text types as declared
603 Each node also provides its parse point (the
608 fields), its position in the tree (the
615 fields) and some type-specific data, in particular, for nodes generated
616 from macros, the generating macro in the
620 The tree itself is arranged according to the following normal form,
621 where capitalised non-terminals represent nodes.
623 .Bl -tag -width "ELEMENTXX" -compact
627 \(<- BLOCK | ELEMENT | TEXT
629 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
635 \(<- mnode* [ENDBODY mnode*]
642 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
643 the BLOCK production: these refer to punctuation marks.
644 Furthermore, although a TEXT node will generally have a non-zero-length
645 string, in the specific case of
646 .Sq \&.Bd \-literal ,
647 an empty line will produce a zero-length string.
648 Multiple body parts are only found in invocations of
650 where a new body introduces a new phrase.
654 syntax tree accommodates for broken block structures as well.
655 The ENDBODY node is available to end the formatting associated
656 with a given block before the physical end of that block.
659 field, is of the BODY
663 as the BLOCK it is ending, and has a
665 field pointing to that BLOCK's BODY node.
666 It is an indirect child of that BODY node
667 and has no children of its own.
669 An ENDBODY node is generated when a block ends while one of its child
670 blocks is still open, like in the following example:
671 .Bd -literal -offset indent
678 This example results in the following block structure:
679 .Bd -literal -offset indent
684 BLOCK Bo, pending -> Ao
689 ENDBODY Ao, pending -> Ao
694 Here, the formatting of the
696 block extends from TEXT ao to TEXT ac,
697 while the formatting of the
699 block extends from TEXT bo to TEXT bc.
700 It renders as follows in
704 .Dl <ao [bo ac> bc] end
706 Support for badly-nested blocks is only provided for backward
707 compatibility with some older
710 Using badly-nested blocks is
711 .Em strongly discouraged ;
718 are unable to render them in any meaningful way.
719 Furthermore, behaviour when encountering badly-nested blocks is not
720 consistent across troff implementations, especially when using multiple
721 levels of badly-nested blocks.
724 .Xr mandoc_escape 3 ,
725 .Xr mandoc_malloc 3 ,
736 library was written by
737 .An Kristaps Dzonsons Aq Mt kristaps@bsd.lv .