1 .\" $Id: mandoc.3,v 1.14 2011/10/06 22:29:12 kristaps 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 6 2011 $
44 .Nd mandoc macro compiler library
54 .Fa "const char **seq"
57 .Ft "const struct man_meta *"
59 .Fa "const struct man *man"
61 .Ft "const struct mparse *"
63 .Fa "const struct man *man"
65 .Ft "const struct man_node *"
67 .Fa "const struct man *man"
72 .Fn mchars_free "struct mchars *p"
74 .Fn mchars_num2char "const char *cp" "size_t sz"
76 .Fn mchars_num2uc "const char *cp" "size_t sz"
79 .Fa "struct mchars *p"
86 .Fa "struct mchars *p"
91 .Ft "const struct mdoc_meta *"
93 .Fa "const struct mdoc *mdoc"
95 .Ft "const struct mdoc_node *"
97 .Fa "const struct mdoc *mdoc"
101 .Fa "enum mparset type"
102 .Fa "enum mandoclevel wlevel"
108 .Fa "struct mparse *parse"
112 .Fa "const struct mparse *parse"
116 .Fa "struct mparse *parse"
118 .Ft "enum mandoclevel"
120 .Fa "struct mparse *parse"
122 .Fa "const char *fname"
126 .Fa "struct mparse *parse"
130 .Fa "struct mparse *parse"
131 .Fa "struct mdoc **mdoc"
132 .Fa "struct man **man"
140 .Fa "enum mandoclevel"
142 .Vt extern const char * const * man_macronames;
143 .Vt extern const char * const * mdoc_argnames;
144 .Vt extern const char * const * mdoc_macronames;
145 .Fd "#define ASCII_NBRSP"
146 .Fd "#define ASCII_HYPH"
152 manual into an abstract syntax tree (AST).
154 manuals are composed of
158 and may be mixed with
165 The following describes a general parse sequence:
168 initiate a parsing sequence with
171 parse files or file descriptors with
174 retrieve a parsed syntax tree, if the parse was successful, with
177 iterate over parse nodes with
182 free all allocated memory with
191 library also contains routines for translating character strings into glyphs
192 .Pq see Fn mchars_alloc
193 and parsing escape sequences from strings
194 .Pq see Fn mandoc_escape .
196 This section documents the functions, types, and variables available
201 .It Vt "enum mandoc_esc"
202 An escape sequence classification.
203 .It Vt "enum mandocerr"
204 A fatal error, error, or warning message during parsing.
205 .It Vt "enum mandoclevel"
206 A classification of an
207 .Vt "enum mandoclevel"
208 as regards system operation.
209 .It Vt "struct mchars"
210 An opaque pointer to an object allowing for translation between
211 character strings and glyphs.
214 .It Vt "enum mparset"
215 The type of parser when reading input.
216 This should usually be
219 .It Vt "struct mparse"
220 An opaque pointer to a running parse sequence.
225 This may be used across parsed input if
227 is called between parses.
229 A prototype for a function to handle fatal error, error, and warning
230 messages emitted by the parser.
235 Scan an escape sequence, i.e., a character string beginning with
237 Pass a pointer to this string as
239 it will be set to the supremum of the parsed escape sequence unless
242 in which case the string is bogus and should be
249 is set to the first relevant character of the substring (font, glyph,
259 Obtain the meta-data of a successful parse.
260 This may only be used on a pointer returned by
263 Get the parser used for the current output.
265 Obtain the root node of a successful parse.
266 This may only be used on a pointer returned by
270 .Vt "struct mchars *"
271 object for translating special characters into glyphs.
274 for an overview of special characters.
275 The object must be freed with
278 Free an object created with
280 .It Fn mchars_num2char
281 Convert a character index (e.g., the \eN\(aq\(aq escape) into a
282 printable ASCII character.
283 Returns \e0 (the nil character) if the input sequence is malformed.
285 Convert a hexadecimal character index (e.g., the \e[uNNNN] escape) into
287 Returns \e0 (the nil character) if the input sequence is malformed.
288 .It Fn mchars_spec2cp
289 Convert a special character into a valid Unicode codepoint.
290 Returns \-1 on failure or a non-zero Unicode codepoint on success.
291 .It Fn mchars_spec2str
292 Convert a special character into an ASCII string.
297 Obtain the meta-data of a successful parse.
298 This may only be used on a pointer returned by
301 Obtain the root node of a successful parse.
302 This may only be used on a pointer returned by
306 The same parser may be used for multiple files so long as
308 is called between parses.
310 must be called to free the memory allocated by this function.
312 Free all memory allocated by
314 .It Fn mparse_getkeep
315 Acquire the keep buffer.
316 Must follow a call of
319 Instruct the parser to retain a copy of its parsed input.
320 This can be acquired with subsequent
324 Parse a file or file descriptor.
329 is opened for reading.
332 is assumed to be the name associated with
334 This may be called multiple times with different parameters; however,
336 should be invoked between parses.
338 Reset a parser so that
342 Obtain the result of a parse.
343 Only successful parses
347 returned less than MANDOCLEVEL_FATAL
349 should invoke this function, in which case one of the two pointers will
351 .It Fn mparse_strerror
352 Return a statically-allocated string representation of an error code.
353 .It Fn mparse_strlevel
354 Return a statically-allocated string representation of a level code.
358 .It Va man_macronames
359 The string representation of a man macro as indexed by
362 The string representation of a mdoc macro argument as indexed by
363 .Vt "enum mdocargt" .
364 .It Va mdoc_macronames
365 The string representation of a mdoc macro as indexed by
368 .Sh IMPLEMENTATION NOTES
369 This section consists of structural documentation for
373 syntax trees and strings.
374 .Ss Man and Mdoc Strings
375 Strings may be extracted from mdoc and man meta-data, or from text
376 nodes (MDOC_TEXT and MAN_TEXT, respectively).
377 These strings have special non-printing formatting cues embedded in the
378 text itself, as well as
380 escapes preserved from input.
381 Implementing systems will need to handle both situations to produce
383 In general, strings may be assumed to consist of 7-bit ASCII characters.
385 The following non-printing characters may be embedded in text strings:
388 A non-breaking space character.
393 Escape characters are also passed verbatim into text strings.
394 An escape character is a sequence of characters beginning with the
397 To construct human-readable text, these should be intercepted with
399 and converted with one of
400 .Fn mchars_num2char ,
401 .Fn mchars_spec2str ,
403 .Ss Man Abstract Syntax Tree
404 This AST is governed by the ontological rules dictated in
406 and derives its terminology accordingly.
408 The AST is composed of
410 nodes with element, root and text types as declared by the
413 Each node also provides its parse point (the
418 fields), its position in the tree (the
424 fields) and some type-specific data.
426 The tree itself is arranged according to the following normal form,
427 where capitalised non-terminals represent nodes.
429 .Bl -tag -width "ELEMENTXX" -compact
433 \(<- ELEMENT | TEXT | BLOCK
446 The only elements capable of nesting other elements are those with
447 next-lint scope as documented in
449 .Ss Mdoc Abstract Syntax Tree
450 This AST is governed by the ontological
453 and derives its terminology accordingly.
455 elements described in
457 are described simply as
460 The AST is composed of
462 nodes with block, head, body, element, root and text types as declared
466 Each node also provides its parse point (the
471 fields), its position in the tree (the
478 fields) and some type-specific data, in particular, for nodes generated
479 from macros, the generating macro in the
483 The tree itself is arranged according to the following normal form,
484 where capitalised non-terminals represent nodes.
486 .Bl -tag -width "ELEMENTXX" -compact
490 \(<- BLOCK | ELEMENT | TEXT
492 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
498 \(<- mnode* [ENDBODY mnode*]
505 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
506 the BLOCK production: these refer to punctuation marks.
507 Furthermore, although a TEXT node will generally have a non-zero-length
508 string, in the specific case of
509 .Sq \&.Bd \-literal ,
510 an empty line will produce a zero-length string.
511 Multiple body parts are only found in invocations of
513 where a new body introduces a new phrase.
517 syntax tree accommodates for broken block structures as well.
518 The ENDBODY node is available to end the formatting associated
519 with a given block before the physical end of that block.
522 field, is of the BODY
526 as the BLOCK it is ending, and has a
528 field pointing to that BLOCK's BODY node.
529 It is an indirect child of that BODY node
530 and has no children of its own.
532 An ENDBODY node is generated when a block ends while one of its child
533 blocks is still open, like in the following example:
534 .Bd -literal -offset indent
541 This example results in the following block structure:
542 .Bd -literal -offset indent
547 BLOCK Bo, pending -> Ao
552 ENDBODY Ao, pending -> Ao
557 Here, the formatting of the
559 block extends from TEXT ao to TEXT ac,
560 while the formatting of the
562 block extends from TEXT bo to TEXT bc.
563 It renders as follows in
567 .Dl <ao [bo ac> bc] end
569 Support for badly-nested blocks is only provided for backward
570 compatibility with some older
573 Using badly-nested blocks is
574 .Em strongly discouraged ;
581 are unable to render them in any meaningful way.
582 Furthermore, behaviour when encountering badly-nested blocks is not
583 consistent across troff implementations, especially when using multiple
584 levels of badly-nested blocks.
596 library was written by
597 .An Kristaps Dzonsons ,
598 .Mt kristaps@bsd.lv .