1 .\" $Id: man.3,v 1.16 2010/04/13 05:26:49 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: April 13 2010 $
29 .Nd man macro compiler library
32 .Vt extern const char * const * man_macronames;
34 .Fn man_alloc "void *data" "int pflags" "const struct man_cb *cb"
36 .Fn man_reset "struct man *man"
38 .Fn man_free "struct man *man"
40 .Fn man_parseln "struct man *man" "int line" "char *buf"
41 .Ft "const struct man_node *"
42 .Fn man_node "const struct man *man"
43 .Ft "const struct man_meta *"
44 .Fn man_meta "const struct man *man"
46 .Fn man_endparse "struct man *man"
50 library parses lines of
54 man) into an abstract syntax tree (AST).
56 In general, applications initiate a parsing sequence with
58 parse each line in a document with
60 close the parsing session with
62 operate over the syntax tree returned by
66 then free all allocated memory with
70 function may be used in order to reset the parser for another input
73 section for a full example.
75 Beyond the full set of macros defined in
79 library also accepts the following macros:
81 .Bl -tag -width Ds -compact
87 Instructional macros in the original roff language. Blocks begun by
90 and may begin anywhere, although they may not break the next-line
91 scoping rules specified in
93 These blocks are discarded.
95 Has no effect. Handled as a current-scope line macro.
99 .Pq part of the standard preamble for Perl documentation .
100 Handled as a line macro.
102 Has no effect. Handled as a current-scope line macro.
106 .Pq part of the standard preamble for Perl documentation .
107 Handled as a current-scope line macro.
113 .Pq part of the standard preamble for Perl documentation .
114 Handled as a current-scope line macro.
117 Furthermore, the following escapes are accepted to allow
119 documents to be correctly formatted:
122 \e*(L" (left double-quote),
123 \e*(R" (right double-quote),
125 \e*(C` (left single-quote),
126 \e*(C' (right single-quote),
131 \e*/ (forward slash),
141 This section further defines the
146 available to programmers. Following that, the
147 .Sx Abstract Syntax Tree
148 section documents the output tree.
154 may use the following types:
157 An opaque type defined in
159 Its values are only used privately within the library.
161 A set of message callbacks defined in
163 .It Vt struct man_node
164 A parsed node. Defined in
167 .Sx Abstract Syntax Tree
171 Function descriptions follow:
174 Allocates a parsing structure. The
176 pointer is passed to callbacks in
178 which are documented further in the header file.
181 arguments are defined in
183 Returns NULL on failure. If non-NULL, the pointer must be freed with
186 Reset the parser for another parse routine. After its use,
188 behaves as if invoked for the first time.
190 Free all resources of a parser. The pointer is no longer valid after
193 Parse a nil-terminated line of input. This line should not contain the
194 trailing newline. Returns 0 on failure, 1 on success. The input buffer
196 is modified by this function.
198 Signals that the parse is complete. Note that if
200 is called subsequent to
202 the resulting tree is incomplete. Returns 0 on failure, 1 on success.
204 Returns the first node of the parse. Note that if
208 return 0, the tree will be incomplete.
210 Returns the document's parsed meta-data. If this information has not
215 return 0, the data will be incomplete.
218 The following variables are also defined:
220 .It Va man_macronames
221 An array of string-ified token names.
223 .Ss Abstract Syntax Tree
226 functions produce an abstract syntax tree (AST) describing input in a
227 regular form. It may be reviewed at any time with
229 however, if called before
235 fail, it may be incomplete.
237 This AST is governed by the ontological
240 and derives its terminology accordingly.
242 The AST is composed of
244 nodes with element, root and text types as declared
247 field. Each node also provides its parse point (the
252 fields), its position in the tree (the
258 fields) and some type-specific data.
260 The tree itself is arranged according to the following normal form,
261 where capitalised non-terminals represent nodes.
263 .Bl -tag -width "ELEMENTXX" -compact
267 \(<- ELEMENT | TEXT | BLOCK
280 The only elements capable of nesting other elements are those with
281 next-lint scope as documented in
284 The following example reads lines from stdin and parses them, operating
285 on the finished parse tree with
287 This example does not error-check nor free memory upon failure.
288 .Bd -literal -offset indent
290 struct man_node *node;
296 man = man_alloc(NULL, 0, NULL);
300 while ((len = getline(&buf, &alloc_len, stdin)) >= 0) {
301 if (len && buflen[len - 1] = '\en')
302 buf[len - 1] = '\e0';
303 if ( ! man_parseln(man, line, buf))
304 errx(1, "man_parseln");
310 if ( ! man_endparse(man))
311 errx(1, "man_endparse");
312 if (NULL == (node = man_node(man)))
324 utility was written by
325 .An Kristaps Dzonsons Aq kristaps@bsd.lv .