Annotation of src/usr.bin/mandoc/man.3, Revision 1.17
1.17 ! schwarze 1: .\" $Id: man.3,v 1.16 2010/10/23 17:11:31 schwarze Exp $
1.1 kristaps 2: .\"
1.7 schwarze 3: .\" Copyright (c) 2009-2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: .\"
5: .\" Permission to use, copy, modify, and distribute this software for any
1.2 schwarze 6: .\" purpose with or without fee is hereby granted, provided that the above
7: .\" copyright notice and this permission notice appear in all copies.
8: .\"
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.
1.1 kristaps 16: .\"
1.17 ! schwarze 17: .Dd $Mdocdate: October 23 2010 $
1.2 schwarze 18: .Dt MAN 3
1.1 kristaps 19: .Os
20: .Sh NAME
1.8 schwarze 21: .Nm man ,
1.1 kristaps 22: .Nm man_alloc ,
23: .Nm man_endparse ,
1.11 schwarze 24: .Nm man_free ,
25: .Nm man_meta ,
1.1 kristaps 26: .Nm man_node ,
1.11 schwarze 27: .Nm man_parseln ,
1.1 kristaps 28: .Nm man_reset
29: .Nd man macro compiler library
30: .Sh SYNOPSIS
1.11 schwarze 31: .In mandoc.h
1.6 schwarze 32: .In man.h
1.1 kristaps 33: .Vt extern const char * const * man_macronames;
34: .Ft "struct man *"
1.12 schwarze 35: .Fo man_alloc
36: .Fa "struct regset *regs"
37: .Fa "void *data"
38: .Fa "mandocmsg msgs"
39: .Fc
1.11 schwarze 40: .Ft int
41: .Fn man_endparse "struct man *man"
1.1 kristaps 42: .Ft void
43: .Fn man_free "struct man *man"
1.11 schwarze 44: .Ft "const struct man_meta *"
45: .Fn man_meta "const struct man *man"
1.1 kristaps 46: .Ft "const struct man_node *"
1.3 schwarze 47: .Fn man_node "const struct man *man"
1.1 kristaps 48: .Ft int
1.12 schwarze 49: .Fo man_parseln
50: .Fa "struct man *man"
51: .Fa "int line"
52: .Fa "char *buf"
53: .Fc
1.11 schwarze 54: .Ft void
55: .Fn man_reset "struct man *man"
1.1 kristaps 56: .Sh DESCRIPTION
57: The
1.8 schwarze 58: .Nm
1.4 schwarze 59: library parses lines of
1.1 kristaps 60: .Xr man 7
1.11 schwarze 61: input into an abstract syntax tree (AST).
1.1 kristaps 62: .Pp
63: In general, applications initiate a parsing sequence with
64: .Fn man_alloc ,
1.4 schwarze 65: parse each line in a document with
1.1 kristaps 66: .Fn man_parseln ,
67: close the parsing session with
68: .Fn man_endparse ,
69: operate over the syntax tree returned by
1.4 schwarze 70: .Fn man_node
1.1 kristaps 71: and
72: .Fn man_meta ,
73: then free all allocated memory with
74: .Fn man_free .
75: The
76: .Fn man_reset
77: function may be used in order to reset the parser for another input
1.11 schwarze 78: sequence.
1.8 schwarze 79: .Pp
80: Beyond the full set of macros defined in
81: .Xr man 7 ,
82: the
83: .Nm
1.17 ! schwarze 84: library also accepts the following macro:
1.1 kristaps 85: .Pp
1.8 schwarze 86: .Bl -tag -width Ds -compact
87: .It PD
88: Has no effect. Handled as a current-scope line macro.
89: .El
1.1 kristaps 90: .Ss Types
1.7 schwarze 91: .Bl -ohang
1.1 kristaps 92: .It Vt struct man
1.16 schwarze 93: An opaque type.
1.1 kristaps 94: Its values are only used privately within the library.
95: .It Vt struct man_node
1.11 schwarze 96: A parsed node.
1.4 schwarze 97: See
1.1 kristaps 98: .Sx Abstract Syntax Tree
99: for details.
100: .El
101: .Ss Functions
1.7 schwarze 102: .Bl -ohang
1.1 kristaps 103: .It Fn man_alloc
1.11 schwarze 104: Allocates a parsing structure.
105: The
1.1 kristaps 106: .Fa data
1.11 schwarze 107: pointer is passed to
108: .Fa msgs .
109: Returns NULL on failure.
110: If non-NULL, the pointer must be freed with
1.1 kristaps 111: .Fn man_free .
112: .It Fn man_reset
1.11 schwarze 113: Reset the parser for another parse routine.
114: After its use,
1.1 kristaps 115: .Fn man_parseln
116: behaves as if invoked for the first time.
117: .It Fn man_free
1.11 schwarze 118: Free all resources of a parser.
119: The pointer is no longer valid after invocation.
1.1 kristaps 120: .It Fn man_parseln
1.11 schwarze 121: Parse a nil-terminated line of input.
122: This line should not contain the trailing newline.
123: Returns 0 on failure, 1 on success.
124: The input buffer
1.1 kristaps 125: .Fa buf
126: is modified by this function.
127: .It Fn man_endparse
1.11 schwarze 128: Signals that the parse is complete.
129: Note that if
1.1 kristaps 130: .Fn man_endparse
131: is called subsequent to
132: .Fn man_node ,
1.11 schwarze 133: the resulting tree is incomplete.
134: Returns 0 on failure, 1 on success.
1.1 kristaps 135: .It Fn man_node
1.11 schwarze 136: Returns the first node of the parse.
137: Note that if
1.1 kristaps 138: .Fn man_parseln
139: or
140: .Fn man_endparse
141: return 0, the tree will be incomplete.
142: .It Fn man_meta
1.11 schwarze 143: Returns the document's parsed meta-data.
144: If this information has not yet been supplied or
1.1 kristaps 145: .Fn man_parseln
146: or
147: .Fn man_endparse
148: return 0, the data will be incomplete.
149: .El
150: .Ss Variables
151: The following variables are also defined:
1.7 schwarze 152: .Bl -ohang
1.1 kristaps 153: .It Va man_macronames
154: An array of string-ified token names.
155: .El
156: .Ss Abstract Syntax Tree
1.4 schwarze 157: The
1.1 kristaps 158: .Nm
159: functions produce an abstract syntax tree (AST) describing input in a
1.11 schwarze 160: regular form.
161: It may be reviewed at any time with
1.1 kristaps 162: .Fn man_nodes ;
163: however, if called before
164: .Fn man_endparse ,
165: or after
1.4 schwarze 166: .Fn man_endparse
1.1 kristaps 167: or
168: .Fn man_parseln
1.4 schwarze 169: fail, it may be incomplete.
1.1 kristaps 170: .Pp
1.11 schwarze 171: This AST is governed by the ontological rules dictated in
1.1 kristaps 172: .Xr man 7
1.4 schwarze 173: and derives its terminology accordingly.
1.1 kristaps 174: .Pp
1.4 schwarze 175: The AST is composed of
1.1 kristaps 176: .Vt struct man_node
1.11 schwarze 177: nodes with element, root and text types as declared by the
1.1 kristaps 178: .Va type
1.11 schwarze 179: field.
180: Each node also provides its parse point (the
1.1 kristaps 181: .Va line ,
182: .Va sec ,
183: and
184: .Va pos
185: fields), its position in the tree (the
186: .Va parent ,
187: .Va child ,
1.4 schwarze 188: .Va next
1.1 kristaps 189: and
1.4 schwarze 190: .Va prev
1.1 kristaps 191: fields) and some type-specific data.
192: .Pp
193: The tree itself is arranged according to the following normal form,
194: where capitalised non-terminals represent nodes.
195: .Pp
1.7 schwarze 196: .Bl -tag -width "ELEMENTXX" -compact
1.1 kristaps 197: .It ROOT
198: \(<- mnode+
199: .It mnode
1.5 schwarze 200: \(<- ELEMENT | TEXT | BLOCK
201: .It BLOCK
202: \(<- HEAD BODY
203: .It HEAD
204: \(<- mnode*
205: .It BODY
206: \(<- mnode*
1.1 kristaps 207: .It ELEMENT
208: \(<- ELEMENT | TEXT*
209: .It TEXT
210: \(<- [[:alpha:]]*
211: .El
212: .Pp
213: The only elements capable of nesting other elements are those with
214: next-lint scope as documented in
215: .Xr man 7 .
216: .Sh EXAMPLES
217: The following example reads lines from stdin and parses them, operating
1.4 schwarze 218: on the finished parse tree with
1.1 kristaps 219: .Fn parsed .
1.7 schwarze 220: This example does not error-check nor free memory upon failure.
221: .Bd -literal -offset indent
1.12 schwarze 222: struct regset regs;
1.1 kristaps 223: struct man *man;
224: struct man_node *node;
225: char *buf;
226: size_t len;
227: int line;
228:
1.12 schwarze 229: bzero(®s, sizeof(struct regset));
1.1 kristaps 230: line = 1;
1.15 schwarze 231: man = man_alloc(®s, NULL, NULL);
1.7 schwarze 232: buf = NULL;
233: alloc_len = 0;
1.1 kristaps 234:
1.7 schwarze 235: while ((len = getline(&buf, &alloc_len, stdin)) >= 0) {
236: if (len && buflen[len - 1] = '\en')
237: buf[len - 1] = '\e0';
238: if ( ! man_parseln(man, line, buf))
239: errx(1, "man_parseln");
240: line++;
1.1 kristaps 241: }
242:
1.7 schwarze 243: free(buf);
244:
1.1 kristaps 245: if ( ! man_endparse(man))
1.7 schwarze 246: errx(1, "man_endparse");
1.1 kristaps 247: if (NULL == (node = man_node(man)))
1.7 schwarze 248: errx(1, "man_node");
1.1 kristaps 249:
250: parsed(man, node);
251: man_free(man);
252: .Ed
1.11 schwarze 253: .Pp
1.16 schwarze 254: To compile this, execute
255: .Pp
256: .D1 % cc main.c libman.a libmandoc.a
257: .Pp
258: where
1.11 schwarze 259: .Pa main.c
1.16 schwarze 260: is the example file.
1.1 kristaps 261: .Sh SEE ALSO
262: .Xr mandoc 1 ,
263: .Xr man 7
264: .Sh AUTHORS
265: The
266: .Nm
1.11 schwarze 267: library was written by
1.7 schwarze 268: .An Kristaps Dzonsons Aq kristaps@bsd.lv .