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