1 .\" $Id: man.3,v 1.15 2010/03/31 06:37:57 kristaps Exp $
3 .\" Copyright (c) 2009-2010 Kristaps Dzonsons <kristaps@bsd.lv>
5 .\" Permission to use, copy, modify, and distribute this software for any
6 .\" purpose with or without fee is hereby granted, provided that the above
7 .\" copyright notice and this permission notice appear in all copies.
9 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 .Dd $Mdocdate: March 31 2010 $
31 .Nd man macro compiler library
36 .Vt extern const char * const * man_macronames;
38 .Fn man_alloc "void *data" "int pflags" "const struct man_cb *cb"
40 .Fn man_reset "struct man *man"
42 .Fn man_free "struct man *man"
44 .Fn man_parseln "struct man *man" "int line" "char *buf"
45 .Ft "const struct man_node *"
46 .Fn man_node "const struct man *man"
47 .Ft "const struct man_meta *"
48 .Fn man_meta "const struct man *man"
50 .Fn man_endparse "struct man *man"
56 library parses lines of
60 man) into an abstract syntax tree (AST).
63 In general, applications initiate a parsing sequence with
65 parse each line in a document with
67 close the parsing session with
69 operate over the syntax tree returned by
73 then free all allocated memory with
77 function may be used in order to reset the parser for another input
80 section for a full example.
83 Beyond the full set of macros defined in
87 library also accepts the following macros:
90 .Bl -tag -width Ds -compact
96 Instructional macros in the original roff language. Blocks begun by
99 and may begin anywhere, although they may not break the next-line
100 scoping rules specified in
102 These blocks are discarded.
105 Has no effect. Handled as a current-scope line macro.
110 .Pq part of the standard preamble for Perl documentation .
111 Handled as a line macro.
114 Has no effect. Handled as a current-scope line macro.
119 .Pq part of the standard preamble for Perl documentation .
120 Handled as a current-scope line macro.
127 .Pq part of the standard preamble for Perl documentation .
128 Handled as a current-scope line macro.
131 Furthermore, the following escapes are accepted to allow
133 documents to be correctly formatted:
136 \e*(L" (left double-quote),
137 \e*(R" (right double-quote),
139 \e*(C` (left single-quote),
140 \e*(C' (right single-quote),
145 \e*/ (forward slash),
157 This section further defines the
162 available to programmers. Following that, the
163 .Sx Abstract Syntax Tree
164 section documents the output tree.
172 may use the following types:
176 An opaque type defined in
178 Its values are only used privately within the library.
181 A set of message callbacks defined in
184 .It Vt struct man_node
185 A parsed node. Defined in
188 .Sx Abstract Syntax Tree
194 Function descriptions follow:
198 Allocates a parsing structure. The
200 pointer is passed to callbacks in
202 which are documented further in the header file.
205 arguments are defined in
207 Returns NULL on failure. If non-NULL, the pointer must be freed with
211 Reset the parser for another parse routine. After its use,
213 behaves as if invoked for the first time.
216 Free all resources of a parser. The pointer is no longer valid after
220 Parse a nil-terminated line of input. This line should not contain the
221 trailing newline. Returns 0 on failure, 1 on success. The input buffer
223 is modified by this function.
226 Signals that the parse is complete. Note that if
228 is called subsequent to
230 the resulting tree is incomplete. Returns 0 on failure, 1 on success.
233 Returns the first node of the parse. Note that if
237 return 0, the tree will be incomplete.
239 Returns the document's parsed meta-data. If this information has not
244 return 0, the data will be incomplete.
249 The following variables are also defined:
252 .It Va man_macronames
253 An array of string-ified token names.
257 .Ss Abstract Syntax Tree
260 functions produce an abstract syntax tree (AST) describing input in a
261 regular form. It may be reviewed at any time with
263 however, if called before
269 fail, it may be incomplete.
272 This AST is governed by the ontological
275 and derives its terminology accordingly.
278 The AST is composed of
280 nodes with element, root and text types as declared
283 field. Each node also provides its parse point (the
288 fields), its position in the tree (the
294 fields) and some type-specific data.
297 The tree itself is arranged according to the following normal form,
298 where capitalised non-terminals represent nodes.
300 .Bl -tag -width "ELEMENTXX" -compact
304 \(<- ELEMENT | TEXT | BLOCK
318 The only elements capable of nesting other elements are those with
319 next-lint scope as documented in
324 The following example reads lines from stdin and parses them, operating
325 on the finished parse tree with
327 This example does not error-check nor free memory upon failure.
328 .Bd -literal -offset indent
330 struct man_node *node;
336 man = man_alloc(NULL, 0, NULL);
340 while ((len = getline(&buf, &alloc_len, stdin)) >= 0) {
341 if (len && buflen[len - 1] = '\en')
342 buf[len - 1] = '\e0';
343 if ( ! man_parseln(man, line, buf))
344 errx(1, "man_parseln");
350 if ( ! man_endparse(man))
351 errx(1, "man_endparse");
352 if (NULL == (node = man_node(man)))
368 utility was written by
369 .An Kristaps Dzonsons Aq kristaps@bsd.lv .