1 .\" $Id: mandoc.3,v 1.7 2011/05/17 11:50:20 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 17 2011 $
43 .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"
86 .Fa "struct mchars *p"
93 .Fa "struct mchars *p"
98 .Ft "const struct mdoc_meta *"
100 .Fa "const struct mdoc *mdoc"
102 .Ft "const struct mdoc_node *"
104 .Fa "const struct mdoc *mdoc"
108 .Fa "enum mparset type"
109 .Fa "enum mandoclevel wlevel"
115 .Fa "struct mparse *parse"
117 .Ft "enum mandoclevel"
119 .Fa "struct mparse *parse"
121 .Fa "const char *fname"
125 .Fa "struct mparse *parse"
129 .Fa "struct mparse *parse"
130 .Fa "struct mdoc **mdoc"
131 .Fa "struct man **man"
139 .Fa "enum mandoclevel"
141 .Vt extern const char * const * man_macronames;
142 .Vt extern const char * const * mdoc_argnames;
143 .Vt extern const char * const * mdoc_macronames;
144 .Fd "#define ASCII_NBRSP"
145 .Fd "#define ASCII_HYPH"
151 manual into an abstract syntax tree (AST).
153 manuals are composed of
157 and may be mixed with
164 The following describes a general parse sequence:
167 initiate a parsing sequence with
170 parse files or file descriptors with
173 retrieve a parsed syntax tree, if the parse was successful, with
176 iterate over parse nodes with
181 free all allocated memory with
190 library also contains routines for translating character strings into glyphs
191 .Pq see Fn mchars_alloc
192 and parsing escape sequences from strings
193 .Pq see Fn mandoc_escape .
198 This section documents the functions, types, and variables available
203 .It Vt "enum mandoc_esc"
204 .It Vt "enum mandocerr"
205 .It Vt "enum mandoclevel"
206 .It Vt "struct mchars"
207 An opaque pointer to an object allowing for translation between
208 character strings and glyphs.
211 .It Vt "enum mparset"
212 .It Vt "struct mparse"
218 Scan an escape sequence, i.e., a character string beginning with
220 Pass a pointer to this string as
222 it will be set to the supremum of the parsed escape sequence unless
223 returning ESCAPE_ERROR, in which case the string is bogus and should be
225 If not ESCAPE_ERROR or ESCAPE_IGNORE,
227 is set to the first relevant character of the substring (font, glyph,
236 Obtain the meta-data of a successful parse.
237 This may only be used on a pointer returned by
240 Obtain the root node of a successful parse.
241 This may only be used on a pointer returned by
245 .Vt "struct mchars *"
246 object for translating special characters into glyphs.
249 for an overview of special characters.
250 The object must be freed with
253 Free an object created with
255 .It Fn mchars_num2char
256 Convert a character index (e.g., the \eN\(aq\(aq escape) into a
257 printable ASCII character.
258 Returns \e0 (the nil character) if the input sequence is malformed.
260 Convert a hexadecimal character index (e.g., the \e[uNNNN] escape) into
262 Returns \e0 (the nil character) if the input sequence is malformed.
264 Convert a predefined character into a valid Unicode codepoint.
265 Returns \-1 on failure and 0 if no code-point exists (if this occurs,
266 the caller should fall back to
267 .Fn mchars_res2str ) .
268 .It Fn mchars_res2str
269 Convert a predefined character into an ASCII string.
270 Returns NULL on failure.
271 .It Fn mchars_spec2cp
272 Convert a special character into a valid Unicode codepoint.
273 Returns \-1 on failure and 0 if no code-point exists (if this occurs,
274 the caller should fall back to
275 .Fn mchars_spec2str ) .
276 .It Fn mchars_spec2str
277 Convert a special character into an ASCII string.
278 Returns NULL on failure.
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
348 .Ss Man Abstract Syntax Tree
349 This AST is governed by the ontological rules dictated in
351 and derives its terminology accordingly.
353 The AST is composed of
355 nodes with element, root and text types as declared by the
358 Each node also provides its parse point (the
363 fields), its position in the tree (the
369 fields) and some type-specific data.
371 The tree itself is arranged according to the following normal form,
372 where capitalised non-terminals represent nodes.
374 .Bl -tag -width "ELEMENTXX" -compact
378 \(<- ELEMENT | TEXT | BLOCK
391 The only elements capable of nesting other elements are those with
392 next-lint scope as documented in
394 .Ss Mdoc Abstract Syntax Tree
395 This AST is governed by the ontological
398 and derives its terminology accordingly.
400 elements described in
402 are described simply as
405 The AST is composed of
407 nodes with block, head, body, element, root and text types as declared
411 Each node also provides its parse point (the
416 fields), its position in the tree (the
423 fields) and some type-specific data, in particular, for nodes generated
424 from macros, the generating macro in the
428 The tree itself is arranged according to the following normal form,
429 where capitalised non-terminals represent nodes.
431 .Bl -tag -width "ELEMENTXX" -compact
435 \(<- BLOCK | ELEMENT | TEXT
437 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
443 \(<- mnode* [ENDBODY mnode*]
447 \(<- [[:printable:],0x1e]*
450 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
451 the BLOCK production: these refer to punctuation marks.
452 Furthermore, although a TEXT node will generally have a non-zero-length
453 string, in the specific case of
454 .Sq \&.Bd \-literal ,
455 an empty line will produce a zero-length string.
456 Multiple body parts are only found in invocations of
458 where a new body introduces a new phrase.
462 syntax tree accommodates for broken block structures as well.
463 The ENDBODY node is available to end the formatting associated
464 with a given block before the physical end of that block.
467 field, is of the BODY
471 as the BLOCK it is ending, and has a
473 field pointing to that BLOCK's BODY node.
474 It is an indirect child of that BODY node
475 and has no children of its own.
477 An ENDBODY node is generated when a block ends while one of its child
478 blocks is still open, like in the following example:
479 .Bd -literal -offset indent
486 This example results in the following block structure:
487 .Bd -literal -offset indent
492 BLOCK Bo, pending -> Ao
497 ENDBODY Ao, pending -> Ao
502 Here, the formatting of the
504 block extends from TEXT ao to TEXT ac,
505 while the formatting of the
507 block extends from TEXT bo to TEXT bc.
508 It renders as follows in
512 .Dl <ao [bo ac> bc] end
514 Support for badly-nested blocks is only provided for backward
515 compatibility with some older
518 Using badly-nested blocks is
519 .Em strongly discouraged ;
526 are unable to render them in any meaningful way.
527 Furthermore, behaviour when encountering badly-nested blocks is not
528 consistent across troff implementations, especially when using multiple
529 levels of badly-nested blocks.
541 library was written by
542 .An Kristaps Dzonsons Aq kristaps@bsd.lv .