1 .\" $Id: mandoc.3,v 1.6 2011/05/01 10:40:52 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: May 1 2011 $
42 .Nd mandoc macro compiler library
50 .Fa "const char **seq"
53 .Ft "const struct man_meta *"
55 .Fa "const struct man *man"
57 .Ft "const struct man_node *"
59 .Fa "const struct man *man"
64 .Fn mchars_free "struct mchars *p"
66 .Fn mchars_num2char "const char *cp" "size_t sz"
69 .Fa "struct mchars *p"
76 .Fa "struct mchars *p"
83 .Fa "struct mchars *p"
90 .Fa "struct mchars *p"
95 .Ft "const struct mdoc_meta *"
97 .Fa "const struct mdoc *mdoc"
99 .Ft "const struct mdoc_node *"
101 .Fa "const struct mdoc *mdoc"
105 .Fa "enum mparset type"
106 .Fa "enum mandoclevel wlevel"
112 .Fa "struct mparse *parse"
114 .Ft "enum mandoclevel"
116 .Fa "struct mparse *parse"
118 .Fa "const char *fname"
122 .Fa "struct mparse *parse"
126 .Fa "struct mparse *parse"
127 .Fa "struct mdoc **mdoc"
128 .Fa "struct man **man"
136 .Fa "enum mandoclevel"
138 .Vt extern const char * const * man_macronames;
139 .Vt extern const char * const * mdoc_argnames;
140 .Vt extern const char * const * mdoc_macronames;
141 .Fd "#define ASCII_NBRSP"
142 .Fd "#define ASCII_HYPH"
148 manual into an abstract syntax tree (AST).
150 manuals are composed of
154 and may be mixed with
161 The following describes a general parse sequence:
164 initiate a parsing sequence with
167 parse files or file descriptors with
170 retrieve a parsed syntax tree, if the parse was successful, with
173 iterate over parse nodes with
178 free all allocated memory with
187 library also contains routines for translating character strings into glyphs
188 .Pq see Fn mchars_alloc
189 and parsing escape sequences from strings
190 .Pq see Fn mandoc_escape .
192 This section documents the functions, types, and variables available
197 .It Vt "enum mandoc_esc"
198 .It Vt "enum mandocerr"
199 .It Vt "enum mandoclevel"
200 .It Vt "struct mchars"
201 An opaque pointer to an object allowing for translation between
202 character strings and glyphs.
205 .It Vt "enum mparset"
206 .It Vt "struct mparse"
212 Scan an escape sequence, i.e., a character string beginning with
214 Pass a pointer to this string as
216 it will be set to the supremum of the parsed escape sequence unless
217 returning ESCAPE_ERROR, in which case the string is bogus and should be
219 If not ESCAPE_ERROR or ESCAPE_IGNORE,
221 is set to the first relevant character of the substring (font, glyph,
230 Obtain the meta-data of a successful parse.
231 This may only be used on a pointer returned by
234 Obtain the root node of a successful parse.
235 This may only be used on a pointer returned by
239 .Vt "struct mchars *"
240 object for translating special characters into glyphs.
243 for an overview of special characters.
244 The object must be freed with
247 Free an object created with
249 .It Fn mchars_num2char
250 Convert a character index as found in \eN\(aq\(aq into a printable
252 Returns \e0 (the nil character) if the input sequence is malformed.
254 Convert a predefined character into a valid Unicode codepoint.
255 Returns \-1 on failure and 0 if no code-point exists (if this occurs,
256 the caller should fall back to
257 .Fn mchars_res2str ) .
258 .It Fn mchars_res2str
259 Convert a predefined character into an ASCII string.
260 Returns NULL on failure.
261 .It Fn mchars_spec2cp
262 Convert a special character into a valid Unicode codepoint.
263 Returns \-1 on failure and 0 if no code-point exists (if this occurs,
264 the caller should fall back to
265 .Fn mchars_spec2str ) .
266 .It Fn mchars_spec2str
267 Convert a special character into an ASCII string.
268 Returns NULL on failure.
270 Obtain the meta-data of a successful parse.
271 This may only be used on a pointer returned by
274 Obtain the root node of a successful parse.
275 This may only be used on a pointer returned by
279 The same parser may be used for multiple files so long as
281 is called between parses.
283 must be called to free the memory allocated by this function.
285 Free all memory allocated by
288 Parse a file or file descriptor.
293 is opened for reading.
296 is assumed to be the name associated with
298 This may be called multiple times with different parameters; however,
300 should be invoked between parses.
302 Reset a parser so that
306 Obtain the result of a parse.
307 Only successful parses
311 returned less than MANDOCLEVEL_FATAL
313 should invoke this function, in which case one of the two pointers will
315 .It Fn mparse_strerror
316 Return a statically-allocated string representation of an error code.
317 .It Fn mparse_strlevel
318 Return a statically-allocated string representation of a level code.
322 .It Va man_macronames
323 The string representation of a man macro as indexed by
326 The string representation of a mdoc macro argument as indexed by
327 .Vt "enum mdocargt" .
328 .It Va mdoc_macronames
329 The string representation of a mdoc macro as indexed by
332 .Sh IMPLEMENTATION NOTES
333 This section consists of structural documentation for
338 .Ss Man Abstract Syntax Tree
339 This AST is governed by the ontological rules dictated in
341 and derives its terminology accordingly.
343 The AST is composed of
345 nodes with element, root and text types as declared by the
348 Each node also provides its parse point (the
353 fields), its position in the tree (the
359 fields) and some type-specific data.
361 The tree itself is arranged according to the following normal form,
362 where capitalised non-terminals represent nodes.
364 .Bl -tag -width "ELEMENTXX" -compact
368 \(<- ELEMENT | TEXT | BLOCK
381 The only elements capable of nesting other elements are those with
382 next-lint scope as documented in
384 .Ss Mdoc Abstract Syntax Tree
385 This AST is governed by the ontological
388 and derives its terminology accordingly.
390 elements described in
392 are described simply as
395 The AST is composed of
397 nodes with block, head, body, element, root and text types as declared
401 Each node also provides its parse point (the
406 fields), its position in the tree (the
413 fields) and some type-specific data, in particular, for nodes generated
414 from macros, the generating macro in the
418 The tree itself is arranged according to the following normal form,
419 where capitalised non-terminals represent nodes.
421 .Bl -tag -width "ELEMENTXX" -compact
425 \(<- BLOCK | ELEMENT | TEXT
427 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
433 \(<- mnode* [ENDBODY mnode*]
437 \(<- [[:printable:],0x1e]*
440 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
441 the BLOCK production: these refer to punctuation marks.
442 Furthermore, although a TEXT node will generally have a non-zero-length
443 string, in the specific case of
444 .Sq \&.Bd \-literal ,
445 an empty line will produce a zero-length string.
446 Multiple body parts are only found in invocations of
448 where a new body introduces a new phrase.
452 syntax tree accommodates for broken block structures as well.
453 The ENDBODY node is available to end the formatting associated
454 with a given block before the physical end of that block.
457 field, is of the BODY
461 as the BLOCK it is ending, and has a
463 field pointing to that BLOCK's BODY node.
464 It is an indirect child of that BODY node
465 and has no children of its own.
467 An ENDBODY node is generated when a block ends while one of its child
468 blocks is still open, like in the following example:
469 .Bd -literal -offset indent
476 This example results in the following block structure:
477 .Bd -literal -offset indent
482 BLOCK Bo, pending -> Ao
487 ENDBODY Ao, pending -> Ao
492 Here, the formatting of the
494 block extends from TEXT ao to TEXT ac,
495 while the formatting of the
497 block extends from TEXT bo to TEXT bc.
498 It renders as follows in
502 .Dl <ao [bo ac> bc] end
504 Support for badly-nested blocks is only provided for backward
505 compatibility with some older
508 Using badly-nested blocks is
509 .Em strongly discouraged ;
516 are unable to render them in any meaningful way.
517 Furthermore, behaviour when encountering badly-nested blocks is not
518 consistent across troff implementations, especially when using multiple
519 levels of badly-nested blocks.
531 library was written by
532 .An Kristaps Dzonsons Aq kristaps@bsd.lv .