1 .\" $Id: mandoc.3,v 1.11 2011/06/22 22:10:02 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: June 22 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
225 returning ESCAPE_ERROR, in which case the string is bogus and should be
227 If not ESCAPE_ERROR or ESCAPE_IGNORE,
229 is set to the first relevant character of the substring (font, glyph,
238 Obtain the meta-data of a successful parse.
239 This may only be used on a pointer returned by
242 Obtain the root node of a successful parse.
243 This may only be used on a pointer returned by
247 .Vt "struct mchars *"
248 object for translating special characters into glyphs.
251 for an overview of special characters.
252 The object must be freed with
255 Free an object created with
257 .It Fn mchars_num2char
258 Convert a character index (e.g., the \eN\(aq\(aq escape) into a
259 printable ASCII character.
260 Returns \e0 (the nil character) if the input sequence is malformed.
262 Convert a hexadecimal character index (e.g., the \e[uNNNN] escape) into
264 Returns \e0 (the nil character) if the input sequence is malformed.
265 .It Fn mchars_spec2cp
266 Convert a special character into a valid Unicode codepoint.
267 Returns \-1 on failure or a non-zero Unicode codepoint on success.
268 .It Fn mchars_spec2str
269 Convert a special character into an ASCII string.
270 Returns NULL on failure.
272 Obtain the meta-data of a successful parse.
273 This may only be used on a pointer returned by
276 Obtain the root node of a successful parse.
277 This may only be used on a pointer returned by
281 The same parser may be used for multiple files so long as
283 is called between parses.
285 must be called to free the memory allocated by this function.
287 Free all memory allocated by
290 Parse a file or file descriptor.
295 is opened for reading.
298 is assumed to be the name associated with
300 This may be called multiple times with different parameters; however,
302 should be invoked between parses.
304 Reset a parser so that
308 Obtain the result of a parse.
309 Only successful parses
313 returned less than MANDOCLEVEL_FATAL
315 should invoke this function, in which case one of the two pointers will
317 .It Fn mparse_strerror
318 Return a statically-allocated string representation of an error code.
319 .It Fn mparse_strlevel
320 Return a statically-allocated string representation of a level code.
324 .It Va man_macronames
325 The string representation of a man macro as indexed by
328 The string representation of a mdoc macro argument as indexed by
329 .Vt "enum mdocargt" .
330 .It Va mdoc_macronames
331 The string representation of a mdoc macro as indexed by
334 .Sh IMPLEMENTATION NOTES
335 This section consists of structural documentation for
339 syntax trees and strings.
340 .Ss Man and Mdoc Strings
341 Strings may be extracted from mdoc and man meta-data, or from text
342 nodes (MDOC_TEXT and MAN_TEXT, respectively).
343 These strings have special non-printing formatting cues embedded in the
344 text itself, as well as
346 escapes preserved from input.
347 Implementing systems will need to handle both situations to produce
349 In general, strings may be assumed to consist of 7-bit ASCII characters.
351 The following non-printing characters may be embedded in text strings:
354 A non-breaking space character.
359 Escape characters are also passed verbatim into text strings.
360 An escape character is a sequence of characters beginning with the
363 To construct human-readable text, these should be intercepted with
365 and converted with one of
366 .Fn mchars_num2char ,
367 .Fn mchars_spec2str ,
369 .Ss Man Abstract Syntax Tree
370 This AST is governed by the ontological rules dictated in
372 and derives its terminology accordingly.
374 The AST is composed of
376 nodes with element, root and text types as declared by the
379 Each node also provides its parse point (the
384 fields), its position in the tree (the
390 fields) and some type-specific data.
392 The tree itself is arranged according to the following normal form,
393 where capitalised non-terminals represent nodes.
395 .Bl -tag -width "ELEMENTXX" -compact
399 \(<- ELEMENT | TEXT | BLOCK
412 The only elements capable of nesting other elements are those with
413 next-lint scope as documented in
415 .Ss Mdoc Abstract Syntax Tree
416 This AST is governed by the ontological
419 and derives its terminology accordingly.
421 elements described in
423 are described simply as
426 The AST is composed of
428 nodes with block, head, body, element, root and text types as declared
432 Each node also provides its parse point (the
437 fields), its position in the tree (the
444 fields) and some type-specific data, in particular, for nodes generated
445 from macros, the generating macro in the
449 The tree itself is arranged according to the following normal form,
450 where capitalised non-terminals represent nodes.
452 .Bl -tag -width "ELEMENTXX" -compact
456 \(<- BLOCK | ELEMENT | TEXT
458 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
464 \(<- mnode* [ENDBODY mnode*]
471 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
472 the BLOCK production: these refer to punctuation marks.
473 Furthermore, although a TEXT node will generally have a non-zero-length
474 string, in the specific case of
475 .Sq \&.Bd \-literal ,
476 an empty line will produce a zero-length string.
477 Multiple body parts are only found in invocations of
479 where a new body introduces a new phrase.
483 syntax tree accommodates for broken block structures as well.
484 The ENDBODY node is available to end the formatting associated
485 with a given block before the physical end of that block.
488 field, is of the BODY
492 as the BLOCK it is ending, and has a
494 field pointing to that BLOCK's BODY node.
495 It is an indirect child of that BODY node
496 and has no children of its own.
498 An ENDBODY node is generated when a block ends while one of its child
499 blocks is still open, like in the following example:
500 .Bd -literal -offset indent
507 This example results in the following block structure:
508 .Bd -literal -offset indent
513 BLOCK Bo, pending -> Ao
518 ENDBODY Ao, pending -> Ao
523 Here, the formatting of the
525 block extends from TEXT ao to TEXT ac,
526 while the formatting of the
528 block extends from TEXT bo to TEXT bc.
529 It renders as follows in
533 .Dl <ao [bo ac> bc] end
535 Support for badly-nested blocks is only provided for backward
536 compatibility with some older
539 Using badly-nested blocks is
540 .Em strongly discouraged ;
547 are unable to render them in any meaningful way.
548 Furthermore, behaviour when encountering badly-nested blocks is not
549 consistent across troff implementations, especially when using multiple
550 levels of badly-nested blocks.
562 library was written by
563 .An Kristaps Dzonsons Aq kristaps@bsd.lv .