1 .\" $Id: mandoc.3,v 1.8 2011/05/17 12:22:15 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
53 .Fa "const char **seq"
56 .Ft "const struct man_meta *"
58 .Fa "const struct man *man"
60 .Ft "const struct man_node *"
62 .Fa "const struct man *man"
67 .Fn mchars_free "struct mchars *p"
69 .Fn mchars_num2char "const char *cp" "size_t sz"
71 .Fn mchars_num2uc "const char *cp" "size_t sz"
74 .Fa "struct mchars *p"
81 .Fa "struct mchars *p"
88 .Fa "struct mchars *p"
95 .Fa "struct mchars *p"
100 .Ft "const struct mdoc_meta *"
102 .Fa "const struct mdoc *mdoc"
104 .Ft "const struct mdoc_node *"
106 .Fa "const struct mdoc *mdoc"
110 .Fa "enum mparset type"
111 .Fa "enum mandoclevel wlevel"
117 .Fa "struct mparse *parse"
119 .Ft "enum mandoclevel"
121 .Fa "struct mparse *parse"
123 .Fa "const char *fname"
127 .Fa "struct mparse *parse"
131 .Fa "struct mparse *parse"
132 .Fa "struct mdoc **mdoc"
133 .Fa "struct man **man"
141 .Fa "enum mandoclevel"
143 .Vt extern const char * const * man_macronames;
144 .Vt extern const char * const * mdoc_argnames;
145 .Vt extern const char * const * mdoc_macronames;
146 .Fd "#define ASCII_NBRSP"
147 .Fd "#define ASCII_HYPH"
153 manual into an abstract syntax tree (AST).
155 manuals are composed of
159 and may be mixed with
166 The following describes a general parse sequence:
169 initiate a parsing sequence with
172 parse files or file descriptors with
175 retrieve a parsed syntax tree, if the parse was successful, with
178 iterate over parse nodes with
183 free all allocated memory with
192 library also contains routines for translating character strings into glyphs
193 .Pq see Fn mchars_alloc
194 and parsing escape sequences from strings
195 .Pq see Fn mandoc_escape .
200 This section documents the functions, types, and variables available
205 .It Vt "enum mandoc_esc"
206 .It Vt "enum mandocerr"
207 .It Vt "enum mandoclevel"
208 .It Vt "struct mchars"
209 An opaque pointer to an object allowing for translation between
210 character strings and glyphs.
213 .It Vt "enum mparset"
214 .It Vt "struct mparse"
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.
266 Convert a predefined character into a valid Unicode codepoint.
267 Returns \-1 on failure and 0 if no code-point exists (if this occurs,
268 the caller should fall back to
269 .Fn mchars_res2str ) .
270 .It Fn mchars_res2str
271 Convert a predefined character into an ASCII string.
272 Returns NULL on failure.
273 .It Fn mchars_spec2cp
274 Convert a special character into a valid Unicode codepoint.
275 Returns \-1 on failure and 0 if no code-point exists (if this occurs,
276 the caller should fall back to
277 .Fn mchars_spec2str ) .
278 .It Fn mchars_spec2str
279 Convert a special character into an ASCII string.
280 Returns NULL on failure.
282 Obtain the meta-data of a successful parse.
283 This may only be used on a pointer returned by
286 Obtain the root node of a successful parse.
287 This may only be used on a pointer returned by
291 The same parser may be used for multiple files so long as
293 is called between parses.
295 must be called to free the memory allocated by this function.
297 Free all memory allocated by
300 Parse a file or file descriptor.
305 is opened for reading.
308 is assumed to be the name associated with
310 This may be called multiple times with different parameters; however,
312 should be invoked between parses.
314 Reset a parser so that
318 Obtain the result of a parse.
319 Only successful parses
323 returned less than MANDOCLEVEL_FATAL
325 should invoke this function, in which case one of the two pointers will
327 .It Fn mparse_strerror
328 Return a statically-allocated string representation of an error code.
329 .It Fn mparse_strlevel
330 Return a statically-allocated string representation of a level code.
334 .It Va man_macronames
335 The string representation of a man macro as indexed by
338 The string representation of a mdoc macro argument as indexed by
339 .Vt "enum mdocargt" .
340 .It Va mdoc_macronames
341 The string representation of a mdoc macro as indexed by
344 .Sh IMPLEMENTATION NOTES
345 This section consists of structural documentation for
350 .Ss Man Abstract Syntax Tree
351 This AST is governed by the ontological rules dictated in
353 and derives its terminology accordingly.
355 The AST is composed of
357 nodes with element, root and text types as declared by the
360 Each node also provides its parse point (the
365 fields), its position in the tree (the
371 fields) and some type-specific data.
373 The tree itself is arranged according to the following normal form,
374 where capitalised non-terminals represent nodes.
376 .Bl -tag -width "ELEMENTXX" -compact
380 \(<- ELEMENT | TEXT | BLOCK
393 The only elements capable of nesting other elements are those with
394 next-lint scope as documented in
396 .Ss Mdoc Abstract Syntax Tree
397 This AST is governed by the ontological
400 and derives its terminology accordingly.
402 elements described in
404 are described simply as
407 The AST is composed of
409 nodes with block, head, body, element, root and text types as declared
413 Each node also provides its parse point (the
418 fields), its position in the tree (the
425 fields) and some type-specific data, in particular, for nodes generated
426 from macros, the generating macro in the
430 The tree itself is arranged according to the following normal form,
431 where capitalised non-terminals represent nodes.
433 .Bl -tag -width "ELEMENTXX" -compact
437 \(<- BLOCK | ELEMENT | TEXT
439 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
445 \(<- mnode* [ENDBODY mnode*]
449 \(<- [[:printable:],0x1e]*
452 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
453 the BLOCK production: these refer to punctuation marks.
454 Furthermore, although a TEXT node will generally have a non-zero-length
455 string, in the specific case of
456 .Sq \&.Bd \-literal ,
457 an empty line will produce a zero-length string.
458 Multiple body parts are only found in invocations of
460 where a new body introduces a new phrase.
464 syntax tree accommodates for broken block structures as well.
465 The ENDBODY node is available to end the formatting associated
466 with a given block before the physical end of that block.
469 field, is of the BODY
473 as the BLOCK it is ending, and has a
475 field pointing to that BLOCK's BODY node.
476 It is an indirect child of that BODY node
477 and has no children of its own.
479 An ENDBODY node is generated when a block ends while one of its child
480 blocks is still open, like in the following example:
481 .Bd -literal -offset indent
488 This example results in the following block structure:
489 .Bd -literal -offset indent
494 BLOCK Bo, pending -> Ao
499 ENDBODY Ao, pending -> Ao
504 Here, the formatting of the
506 block extends from TEXT ao to TEXT ac,
507 while the formatting of the
509 block extends from TEXT bo to TEXT bc.
510 It renders as follows in
514 .Dl <ao [bo ac> bc] end
516 Support for badly-nested blocks is only provided for backward
517 compatibility with some older
520 Using badly-nested blocks is
521 .Em strongly discouraged ;
528 are unable to render them in any meaningful way.
529 Furthermore, behaviour when encountering badly-nested blocks is not
530 consistent across troff implementations, especially when using multiple
531 levels of badly-nested blocks.
543 library was written by
544 .An Kristaps Dzonsons Aq kristaps@bsd.lv .