1 .\" $Id: mandoc.3,v 1.25 2014/08/05 05:48:56 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: August 5 2014 $
39 .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"
84 .Fa "struct mparse *parse"
88 .Fa "struct mparse *parse"
89 .Fa "struct mdoc **mdoc"
90 .Fa "struct man **man"
99 .Fa "enum mandoclevel"
107 .Fa "const struct mdoc_node *node"
109 .Ft "const struct mdoc_meta *"
111 .Fa "const struct mdoc *mdoc"
113 .Ft "const struct mdoc_node *"
115 .Fa "const struct mdoc *mdoc"
117 .Vt extern const char * const * mdoc_argnames;
118 .Vt extern const char * const * mdoc_macronames;
125 .Fa "const struct man_node *node"
127 .Ft "const struct man_meta *"
129 .Fa "const struct man *man"
131 .Ft "const struct mparse *"
133 .Fa "const struct man *man"
135 .Ft "const struct man_node *"
137 .Fa "const struct man *man"
139 .Vt extern const char * const * man_macronames;
145 manual into an abstract syntax tree (AST).
147 manuals are composed of
151 and may be mixed with
158 The following describes a general parse sequence:
161 initiate a parsing sequence with
164 parse files or file descriptors with
167 retrieve a parsed syntax tree, if the parse was successful, with
170 iterate over parse nodes with
175 free all allocated memory with
182 This section documents the functions, types, and variables available
185 with the exception of those documented in
191 .It Vt "enum mandocerr"
192 A fatal error, error, or warning message during parsing.
193 .It Vt "enum mandoclevel"
194 A classification of an
196 as regards system operation.
197 .It Vt "struct mparse"
198 An opaque pointer to a running parse sequence.
203 This may be used across parsed input if
205 is called between parses.
207 A prototype for a function to handle fatal error, error, and warning
208 messages emitted by the parser.
213 Obtain a text-only representation of a
214 .Vt struct man_node ,
215 including text contained in its child nodes.
216 To be used on children of the pointer returned from
218 When it is no longer needed, the pointer returned from
223 Obtain the meta-data of a successful
226 This may only be used on a pointer returned by
233 Get the parser used for the current output.
239 Obtain the root node of a successful
242 This may only be used on a pointer returned by
249 Obtain a text-only representation of a
250 .Vt struct mdoc_node ,
251 including text contained in its child nodes.
252 To be used on children of the pointer returned from
254 When it is no longer needed, the pointer returned from
259 Obtain the meta-data of a successful
262 This may only be used on a pointer returned by
269 Obtain the root node of a successful
272 This may only be used on a pointer returned by
280 The arguments have the following effect:
281 .Bl -tag -offset 5n -width inttype
287 bit is set, only that parser is used.
288 Otherwise, the document type is automatically detected.
295 file inclusion requests are always honoured.
296 Otherwise, if the request is the only content in an input file,
297 only the file name is remembered, to be returned in the
304 bit is set, parsing is aborted after the NAME section.
305 This is for example useful in
308 to quickly build minimal databases.
311 .Dv MANDOCLEVEL_FATAL ,
312 .Dv MANDOCLEVEL_ERROR ,
314 .Dv MANDOCLEVEL_WARNING .
315 Messages below the selected level will be suppressed.
317 A callback function to handle errors and warnings.
322 A default string for the
325 macro, overriding the
327 preprocessor definition and the results of
331 The same parser may be used for multiple files so long as
333 is called between parses.
335 must be called to free the memory allocated by this function.
341 Free all memory allocated by
347 .It Fn mparse_getkeep
348 Acquire the keep buffer.
349 Must follow a call of
356 Instruct the parser to retain a copy of its parsed input.
357 This can be acquired with subsequent
365 Parse a file or file descriptor.
370 is opened for reading.
373 is assumed to be the name associated with
375 This may be called multiple times with different parameters; however,
377 should be invoked between parses.
383 Reset a parser so that
391 Obtain the result of a parse.
392 Only successful parses
396 returned less than MANDOCLEVEL_FATAL
398 should invoke this function, in which case one of the three pointers will
404 .It Fn mparse_strerror
405 Return a statically-allocated string representation of an error code.
410 .It Fn mparse_strlevel
411 Return a statically-allocated string representation of a level code.
419 .It Va man_macronames
420 The string representation of a man macro as indexed by
423 The string representation of a mdoc macro argument as indexed by
424 .Vt "enum mdocargt" .
425 .It Va mdoc_macronames
426 The string representation of a mdoc macro as indexed by
429 .Sh IMPLEMENTATION NOTES
430 This section consists of structural documentation for
434 syntax trees and strings.
435 .Ss Man and Mdoc Strings
436 Strings may be extracted from mdoc and man meta-data, or from text
437 nodes (MDOC_TEXT and MAN_TEXT, respectively).
438 These strings have special non-printing formatting cues embedded in the
439 text itself, as well as
441 escapes preserved from input.
442 Implementing systems will need to handle both situations to produce
444 In general, strings may be assumed to consist of 7-bit ASCII characters.
446 The following non-printing characters may be embedded in text strings:
449 A non-breaking space character.
453 A breakable zero-width space.
456 Escape characters are also passed verbatim into text strings.
457 An escape character is a sequence of characters beginning with the
460 To construct human-readable text, these should be intercepted with
462 and converted with one the functions described in
464 .Ss Man Abstract Syntax Tree
465 This AST is governed by the ontological rules dictated in
467 and derives its terminology accordingly.
469 The AST is composed of
471 nodes with element, root and text types as declared by the
474 Each node also provides its parse point (the
479 fields), its position in the tree (the
485 fields) and some type-specific data.
487 The tree itself is arranged according to the following normal form,
488 where capitalised non-terminals represent nodes.
490 .Bl -tag -width "ELEMENTXX" -compact
494 \(<- ELEMENT | TEXT | BLOCK
507 The only elements capable of nesting other elements are those with
508 next-line scope as documented in
510 .Ss Mdoc Abstract Syntax Tree
511 This AST is governed by the ontological
514 and derives its terminology accordingly.
516 elements described in
518 are described simply as
521 The AST is composed of
523 nodes with block, head, body, element, root and text types as declared
527 Each node also provides its parse point (the
532 fields), its position in the tree (the
539 fields) and some type-specific data, in particular, for nodes generated
540 from macros, the generating macro in the
544 The tree itself is arranged according to the following normal form,
545 where capitalised non-terminals represent nodes.
547 .Bl -tag -width "ELEMENTXX" -compact
551 \(<- BLOCK | ELEMENT | TEXT
553 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
559 \(<- mnode* [ENDBODY mnode*]
566 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
567 the BLOCK production: these refer to punctuation marks.
568 Furthermore, although a TEXT node will generally have a non-zero-length
569 string, in the specific case of
570 .Sq \&.Bd \-literal ,
571 an empty line will produce a zero-length string.
572 Multiple body parts are only found in invocations of
574 where a new body introduces a new phrase.
578 syntax tree accommodates for broken block structures as well.
579 The ENDBODY node is available to end the formatting associated
580 with a given block before the physical end of that block.
583 field, is of the BODY
587 as the BLOCK it is ending, and has a
589 field pointing to that BLOCK's BODY node.
590 It is an indirect child of that BODY node
591 and has no children of its own.
593 An ENDBODY node is generated when a block ends while one of its child
594 blocks is still open, like in the following example:
595 .Bd -literal -offset indent
602 This example results in the following block structure:
603 .Bd -literal -offset indent
608 BLOCK Bo, pending -> Ao
613 ENDBODY Ao, pending -> Ao
618 Here, the formatting of the
620 block extends from TEXT ao to TEXT ac,
621 while the formatting of the
623 block extends from TEXT bo to TEXT bc.
624 It renders as follows in
628 .Dl <ao [bo ac> bc] end
630 Support for badly-nested blocks is only provided for backward
631 compatibility with some older
634 Using badly-nested blocks is
635 .Em strongly discouraged ;
642 are unable to render them in any meaningful way.
643 Furthermore, behaviour when encountering badly-nested blocks is not
644 consistent across troff implementations, especially when using multiple
645 levels of badly-nested blocks.
648 .Xr mandoc_escape 3 ,
649 .Xr mandoc_malloc 3 ,
660 library was written by
661 .An Kristaps Dzonsons Aq Mt kristaps@bsd.lv .