1 .\" $Id: mandoc.3,v 1.12 2011/07/11 08:43:27 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: July 11 2011 $
41 .Nd mandoc macro compiler library
51 .Fa "const char **seq"
54 .Ft "const struct man_meta *"
56 .Fa "const struct man *man"
58 .Ft "const struct man_node *"
60 .Fa "const struct man *man"
65 .Fn mchars_free "struct mchars *p"
67 .Fn mchars_num2char "const char *cp" "size_t sz"
69 .Fn mchars_num2uc "const char *cp" "size_t sz"
72 .Fa "struct mchars *p"
79 .Fa "struct mchars *p"
84 .Ft "const struct mdoc_meta *"
86 .Fa "const struct mdoc *mdoc"
88 .Ft "const struct mdoc_node *"
90 .Fa "const struct mdoc *mdoc"
94 .Fa "enum mparset type"
95 .Fa "enum mandoclevel wlevel"
101 .Fa "struct mparse *parse"
103 .Ft "enum mandoclevel"
105 .Fa "struct mparse *parse"
107 .Fa "const char *fname"
111 .Fa "struct mparse *parse"
115 .Fa "struct mparse *parse"
116 .Fa "struct mdoc **mdoc"
117 .Fa "struct man **man"
125 .Fa "enum mandoclevel"
127 .Vt extern const char * const * man_macronames;
128 .Vt extern const char * const * mdoc_argnames;
129 .Vt extern const char * const * mdoc_macronames;
130 .Fd "#define ASCII_NBRSP"
131 .Fd "#define ASCII_HYPH"
137 manual into an abstract syntax tree (AST).
139 manuals are composed of
143 and may be mixed with
150 The following describes a general parse sequence:
153 initiate a parsing sequence with
156 parse files or file descriptors with
159 retrieve a parsed syntax tree, if the parse was successful, with
162 iterate over parse nodes with
167 free all allocated memory with
176 library also contains routines for translating character strings into glyphs
177 .Pq see Fn mchars_alloc
178 and parsing escape sequences from strings
179 .Pq see Fn mandoc_escape .
181 This section documents the functions, types, and variables available
186 .It Vt "enum mandoc_esc"
187 An escape sequence classification.
188 .It Vt "enum mandocerr"
189 A fatal error, error, or warning message during parsing.
190 .It Vt "enum mandoclevel"
191 A classification of an
192 .Vt "enum mandoclevel"
193 as regards system operation.
194 .It Vt "struct mchars"
195 An opaque pointer to an object allowing for translation between
196 character strings and glyphs.
199 .It Vt "enum mparset"
200 The type of parser when reading input.
201 This should usually be
204 .It Vt "struct mparse"
205 An opaque pointer to a running parse sequence.
210 This may be used across parsed input if
212 is called between parses.
214 A prototype for a function to handle fatal error, error, and warning
215 messages emitted by the parser.
220 Scan an escape sequence, i.e., a character string beginning with
222 Pass a pointer to this string as
224 it will be set to the supremum of the parsed escape sequence unless
227 in which case the string is bogus and should be
234 is set to the first relevant character of the substring (font, glyph,
244 Obtain the meta-data of a successful parse.
245 This may only be used on a pointer returned by
248 Obtain the root node of a successful parse.
249 This may only be used on a pointer returned by
253 .Vt "struct mchars *"
254 object for translating special characters into glyphs.
257 for an overview of special characters.
258 The object must be freed with
261 Free an object created with
263 .It Fn mchars_num2char
264 Convert a character index (e.g., the \eN\(aq\(aq escape) into a
265 printable ASCII character.
266 Returns \e0 (the nil character) if the input sequence is malformed.
268 Convert a hexadecimal character index (e.g., the \e[uNNNN] escape) into
270 Returns \e0 (the nil character) if the input sequence is malformed.
271 .It Fn mchars_spec2cp
272 Convert a special character into a valid Unicode codepoint.
273 Returns \-1 on failure or a non-zero Unicode codepoint on success.
274 .It Fn mchars_spec2str
275 Convert a special character into an ASCII string.
280 Obtain the meta-data of a successful parse.
281 This may only be used on a pointer returned by
284 Obtain the root node of a successful parse.
285 This may only be used on a pointer returned by
289 The same parser may be used for multiple files so long as
291 is called between parses.
293 must be called to free the memory allocated by this function.
295 Free all memory allocated by
298 Parse a file or file descriptor.
303 is opened for reading.
306 is assumed to be the name associated with
308 This may be called multiple times with different parameters; however,
310 should be invoked between parses.
312 Reset a parser so that
316 Obtain the result of a parse.
317 Only successful parses
321 returned less than MANDOCLEVEL_FATAL
323 should invoke this function, in which case one of the two pointers will
325 .It Fn mparse_strerror
326 Return a statically-allocated string representation of an error code.
327 .It Fn mparse_strlevel
328 Return a statically-allocated string representation of a level code.
332 .It Va man_macronames
333 The string representation of a man macro as indexed by
336 The string representation of a mdoc macro argument as indexed by
337 .Vt "enum mdocargt" .
338 .It Va mdoc_macronames
339 The string representation of a mdoc macro as indexed by
342 .Sh IMPLEMENTATION NOTES
343 This section consists of structural documentation for
347 syntax trees and strings.
348 .Ss Man and Mdoc Strings
349 Strings may be extracted from mdoc and man meta-data, or from text
350 nodes (MDOC_TEXT and MAN_TEXT, respectively).
351 These strings have special non-printing formatting cues embedded in the
352 text itself, as well as
354 escapes preserved from input.
355 Implementing systems will need to handle both situations to produce
357 In general, strings may be assumed to consist of 7-bit ASCII characters.
359 The following non-printing characters may be embedded in text strings:
362 A non-breaking space character.
367 Escape characters are also passed verbatim into text strings.
368 An escape character is a sequence of characters beginning with the
371 To construct human-readable text, these should be intercepted with
373 and converted with one of
374 .Fn mchars_num2char ,
375 .Fn mchars_spec2str ,
377 .Ss Man Abstract Syntax Tree
378 This AST is governed by the ontological rules dictated in
380 and derives its terminology accordingly.
382 The AST is composed of
384 nodes with element, root and text types as declared by the
387 Each node also provides its parse point (the
392 fields), its position in the tree (the
398 fields) and some type-specific data.
400 The tree itself is arranged according to the following normal form,
401 where capitalised non-terminals represent nodes.
403 .Bl -tag -width "ELEMENTXX" -compact
407 \(<- ELEMENT | TEXT | BLOCK
420 The only elements capable of nesting other elements are those with
421 next-lint scope as documented in
423 .Ss Mdoc Abstract Syntax Tree
424 This AST is governed by the ontological
427 and derives its terminology accordingly.
429 elements described in
431 are described simply as
434 The AST is composed of
436 nodes with block, head, body, element, root and text types as declared
440 Each node also provides its parse point (the
445 fields), its position in the tree (the
452 fields) and some type-specific data, in particular, for nodes generated
453 from macros, the generating macro in the
457 The tree itself is arranged according to the following normal form,
458 where capitalised non-terminals represent nodes.
460 .Bl -tag -width "ELEMENTXX" -compact
464 \(<- BLOCK | ELEMENT | TEXT
466 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
472 \(<- mnode* [ENDBODY mnode*]
479 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
480 the BLOCK production: these refer to punctuation marks.
481 Furthermore, although a TEXT node will generally have a non-zero-length
482 string, in the specific case of
483 .Sq \&.Bd \-literal ,
484 an empty line will produce a zero-length string.
485 Multiple body parts are only found in invocations of
487 where a new body introduces a new phrase.
491 syntax tree accommodates for broken block structures as well.
492 The ENDBODY node is available to end the formatting associated
493 with a given block before the physical end of that block.
496 field, is of the BODY
500 as the BLOCK it is ending, and has a
502 field pointing to that BLOCK's BODY node.
503 It is an indirect child of that BODY node
504 and has no children of its own.
506 An ENDBODY node is generated when a block ends while one of its child
507 blocks is still open, like in the following example:
508 .Bd -literal -offset indent
515 This example results in the following block structure:
516 .Bd -literal -offset indent
521 BLOCK Bo, pending -> Ao
526 ENDBODY Ao, pending -> Ao
531 Here, the formatting of the
533 block extends from TEXT ao to TEXT ac,
534 while the formatting of the
536 block extends from TEXT bo to TEXT bc.
537 It renders as follows in
541 .Dl <ao [bo ac> bc] end
543 Support for badly-nested blocks is only provided for backward
544 compatibility with some older
547 Using badly-nested blocks is
548 .Em strongly discouraged ;
555 are unable to render them in any meaningful way.
556 Furthermore, behaviour when encountering badly-nested blocks is not
557 consistent across troff implementations, especially when using multiple
558 levels of badly-nested blocks.
570 library was written by
571 .An Kristaps Dzonsons Aq kristaps@bsd.lv .