Annotation of mandoc/mdoc.3, Revision 1.56
1.56 ! kristaps 1: .\" $Id: mdoc.3,v 1.55 2011/01/07 15:07:21 kristaps Exp $
1.6 kristaps 2: .\"
1.47 schwarze 3: .\" Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4: .\" Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
1.6 kristaps 5: .\"
6: .\" Permission to use, copy, modify, and distribute this software for any
1.28 kristaps 7: .\" purpose with or without fee is hereby granted, provided that the above
8: .\" copyright notice and this permission notice appear in all copies.
1.6 kristaps 9: .\"
1.28 kristaps 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.
1.33 kristaps 17: .\"
1.56 ! kristaps 18: .Dd $Mdocdate: January 7 2011 $
1.27 kristaps 19: .Dt MDOC 3
1.1 kristaps 20: .Os
21: .Sh NAME
1.39 kristaps 22: .Nm mdoc ,
1.1 kristaps 23: .Nm mdoc_alloc ,
24: .Nm mdoc_endparse ,
1.38 kristaps 25: .Nm mdoc_free ,
26: .Nm mdoc_meta ,
1.4 kristaps 27: .Nm mdoc_node ,
1.38 kristaps 28: .Nm mdoc_parseln ,
1.20 kristaps 29: .Nm mdoc_reset
1.2 kristaps 30: .Nd mdoc macro compiler library
1.1 kristaps 31: .Sh SYNOPSIS
1.38 kristaps 32: .In mandoc.h
1.35 kristaps 33: .In mdoc.h
1.4 kristaps 34: .Vt extern const char * const * mdoc_macronames;
35: .Vt extern const char * const * mdoc_argnames;
1.52 kristaps 36: .Ft int
1.56 ! kristaps 37: .Fo mdoc_addeqn
! 38: .Fa "struct mdoc *mdoc"
! 39: .Fa "const struct eqn *eqn"
! 40: .Fc
! 41: .Ft int
1.55 kristaps 42: .Fo mdoc_addspan
1.52 kristaps 43: .Fa "struct mdoc *mdoc"
44: .Fa "const struct tbl_span *span"
45: .Fc
1.1 kristaps 46: .Ft "struct mdoc *"
1.43 kristaps 47: .Fo mdoc_alloc
1.44 kristaps 48: .Fa "struct regset *regs"
1.43 kristaps 49: .Fa "void *data"
50: .Fa "mandocmsg msgs"
51: .Fc
1.26 kristaps 52: .Ft int
1.38 kristaps 53: .Fn mdoc_endparse "struct mdoc *mdoc"
1.1 kristaps 54: .Ft void
1.2 kristaps 55: .Fn mdoc_free "struct mdoc *mdoc"
1.38 kristaps 56: .Ft "const struct mdoc_meta *"
57: .Fn mdoc_meta "const struct mdoc *mdoc"
58: .Ft "const struct mdoc_node *"
59: .Fn mdoc_node "const struct mdoc *mdoc"
1.1 kristaps 60: .Ft int
1.42 kristaps 61: .Fo mdoc_parseln
62: .Fa "struct mdoc *mdoc"
63: .Fa "int line"
64: .Fa "char *buf"
65: .Fc
1.1 kristaps 66: .Ft int
1.38 kristaps 67: .Fn mdoc_reset "struct mdoc *mdoc"
1.1 kristaps 68: .Sh DESCRIPTION
69: The
70: .Nm mdoc
1.33 kristaps 71: library parses lines of
1.17 kristaps 72: .Xr mdoc 7
1.38 kristaps 73: input
74: into an abstract syntax tree (AST).
1.6 kristaps 75: .Pp
1.1 kristaps 76: In general, applications initiate a parsing sequence with
77: .Fn mdoc_alloc ,
1.33 kristaps 78: parse each line in a document with
1.1 kristaps 79: .Fn mdoc_parseln ,
80: close the parsing session with
81: .Fn mdoc_endparse ,
82: operate over the syntax tree returned by
1.33 kristaps 83: .Fn mdoc_node
1.4 kristaps 84: and
85: .Fn mdoc_meta ,
1.1 kristaps 86: then free all allocated memory with
87: .Fn mdoc_free .
1.20 kristaps 88: The
89: .Fn mdoc_reset
90: function may be used in order to reset the parser for another input
1.38 kristaps 91: sequence.
1.6 kristaps 92: .Ss Types
1.37 kristaps 93: .Bl -ohang
1.6 kristaps 94: .It Vt struct mdoc
1.50 kristaps 95: An opaque type.
1.6 kristaps 96: Its values are only used privately within the library.
97: .It Vt struct mdoc_node
1.38 kristaps 98: A parsed node.
1.33 kristaps 99: See
1.6 kristaps 100: .Sx Abstract Syntax Tree
101: for details.
102: .El
103: .Ss Functions
1.53 kristaps 104: If
1.56 ! kristaps 105: .Fn mdoc_addeqn ,
1.53 kristaps 106: .Fn mdoc_addspan ,
107: .Fn mdoc_parseln ,
108: or
109: .Fn mdoc_endparse
110: return 0, calls to any function but
111: .Fn mdoc_reset
112: or
113: .Fn mdoc_free
114: will raise an assertion.
1.37 kristaps 115: .Bl -ohang
1.56 ! kristaps 116: .It Fn mdoc_addeqn
! 117: Add an equation to the parsing stream.
! 118: Returns 0 on failure, 1 on success.
1.52 kristaps 119: .It Fn mdoc_addspan
120: Add a table span to the parsing stream.
121: Returns 0 on failure, 1 on success.
1.2 kristaps 122: .It Fn mdoc_alloc
1.38 kristaps 123: Allocates a parsing structure.
124: The
1.2 kristaps 125: .Fa data
1.40 kristaps 126: pointer is passed to
127: .Fa msgs .
1.53 kristaps 128: Always returns a valid pointer.
129: The pointer must be freed with
1.2 kristaps 130: .Fn mdoc_free .
1.20 kristaps 131: .It Fn mdoc_reset
1.38 kristaps 132: Reset the parser for another parse routine.
133: After its use,
1.20 kristaps 134: .Fn mdoc_parseln
1.38 kristaps 135: behaves as if invoked for the first time.
136: If it returns 0, memory could not be allocated.
1.2 kristaps 137: .It Fn mdoc_free
1.38 kristaps 138: Free all resources of a parser.
139: The pointer is no longer valid after invocation.
1.2 kristaps 140: .It Fn mdoc_parseln
1.38 kristaps 141: Parse a nil-terminated line of input.
142: This line should not contain the trailing newline.
143: Returns 0 on failure, 1 on success.
144: The input buffer
1.2 kristaps 145: .Fa buf
146: is modified by this function.
147: .It Fn mdoc_endparse
1.38 kristaps 148: Signals that the parse is complete.
149: Returns 0 on failure, 1 on success.
1.4 kristaps 150: .It Fn mdoc_node
1.38 kristaps 151: Returns the first node of the parse.
1.4 kristaps 152: .It Fn mdoc_meta
1.38 kristaps 153: Returns the document's parsed meta-data.
1.4 kristaps 154: .El
1.6 kristaps 155: .Ss Variables
1.37 kristaps 156: .Bl -ohang
1.4 kristaps 157: .It Va mdoc_macronames
158: An array of string-ified token names.
159: .It Va mdoc_argnames
160: An array of string-ified token argument names.
1.2 kristaps 161: .El
1.6 kristaps 162: .Ss Abstract Syntax Tree
1.33 kristaps 163: The
1.6 kristaps 164: .Nm
1.17 kristaps 165: functions produce an abstract syntax tree (AST) describing input in a
1.38 kristaps 166: regular form.
167: It may be reviewed at any time with
1.6 kristaps 168: .Fn mdoc_nodes ;
169: however, if called before
170: .Fn mdoc_endparse ,
171: or after
1.33 kristaps 172: .Fn mdoc_endparse
1.6 kristaps 173: or
174: .Fn mdoc_parseln
1.33 kristaps 175: fail, it may be incomplete.
1.18 kristaps 176: .Pp
177: This AST is governed by the ontological
1.17 kristaps 178: rules dictated in
179: .Xr mdoc 7
1.33 kristaps 180: and derives its terminology accordingly.
1.17 kristaps 181: .Qq In-line
182: elements described in
183: .Xr mdoc 7
1.33 kristaps 184: are described simply as
1.17 kristaps 185: .Qq elements .
1.6 kristaps 186: .Pp
1.33 kristaps 187: The AST is composed of
1.6 kristaps 188: .Vt struct mdoc_node
189: nodes with block, head, body, element, root and text types as declared
190: by the
191: .Va type
1.38 kristaps 192: field.
193: Each node also provides its parse point (the
1.6 kristaps 194: .Va line ,
195: .Va sec ,
196: and
197: .Va pos
198: fields), its position in the tree (the
199: .Va parent ,
200: .Va child ,
1.45 schwarze 201: .Va nchild ,
1.33 kristaps 202: .Va next
1.6 kristaps 203: and
1.33 kristaps 204: .Va prev
1.45 schwarze 205: fields) and some type-specific data, in particular, for nodes generated
206: from macros, the generating macro in the
207: .Va tok
208: field.
1.6 kristaps 209: .Pp
210: The tree itself is arranged according to the following normal form,
211: where capitalised non-terminals represent nodes.
212: .Pp
1.37 kristaps 213: .Bl -tag -width "ELEMENTXX" -compact
1.6 kristaps 214: .It ROOT
215: \(<- mnode+
216: .It mnode
217: \(<- BLOCK | ELEMENT | TEXT
218: .It BLOCK
1.41 kristaps 219: \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
1.6 kristaps 220: .It ELEMENT
221: \(<- TEXT*
222: .It HEAD
1.45 schwarze 223: \(<- mnode*
1.6 kristaps 224: .It BODY
1.45 schwarze 225: \(<- mnode* [ENDBODY mnode*]
1.6 kristaps 226: .It TAIL
1.45 schwarze 227: \(<- mnode*
1.6 kristaps 228: .It TEXT
1.38 kristaps 229: \(<- [[:printable:],0x1e]*
1.6 kristaps 230: .El
1.2 kristaps 231: .Pp
1.6 kristaps 232: Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
1.41 kristaps 233: the BLOCK production: these refer to punctuation marks.
1.38 kristaps 234: Furthermore, although a TEXT node will generally have a non-zero-length
235: string, in the specific case of
1.8 kristaps 236: .Sq \&.Bd \-literal ,
1.6 kristaps 237: an empty line will produce a zero-length string.
1.41 kristaps 238: Multiple body parts are only found in invocations of
239: .Sq \&Bl \-column ,
240: where a new body introduces a new phrase.
1.46 kristaps 241: .Ss Badly-nested Blocks
242: The ENDBODY node is available to end the formatting associated
243: with a given block before the physical end of that block.
244: It has a non-null
1.45 schwarze 245: .Va end
246: field, is of the BODY
247: .Va type ,
248: has the same
249: .Va tok
250: as the BLOCK it is ending, and has a
251: .Va pending
252: field pointing to that BLOCK's BODY node.
253: It is an indirect child of that BODY node
254: and has no children of its own.
255: .Pp
256: An ENDBODY node is generated when a block ends while one of its child
257: blocks is still open, like in the following example:
258: .Bd -literal -offset indent
259: \&.Ao ao
260: \&.Bo bo ac
261: \&.Ac bc
262: \&.Bc end
263: .Ed
264: .Pp
265: This example results in the following block structure:
266: .Bd -literal -offset indent
267: BLOCK Ao
268: HEAD Ao
269: BODY Ao
270: TEXT ao
271: BLOCK Bo, pending -> Ao
272: HEAD Bo
273: BODY Bo
274: TEXT bo
275: TEXT ac
276: ENDBODY Ao, pending -> Ao
277: TEXT bc
278: TEXT end
279: .Ed
280: .Pp
1.46 kristaps 281: Here, the formatting of the
282: .Sq \&Ao
283: block extends from TEXT ao to TEXT ac,
284: while the formatting of the
285: .Sq \&Bo
286: block extends from TEXT bo to TEXT bc.
287: It renders as follows in
1.45 schwarze 288: .Fl T Ns Cm ascii
289: mode:
1.46 kristaps 290: .Pp
1.45 schwarze 291: .Dl <ao [bo ac> bc] end
1.46 kristaps 292: .Pp
293: Support for badly-nested blocks is only provided for backward
1.45 schwarze 294: compatibility with some older
295: .Xr mdoc 7
296: implementations.
1.46 kristaps 297: Using badly-nested blocks is
298: .Em strongly discouraged :
299: the
300: .Fl T Ns Cm html
301: and
302: .Fl T Ns Cm xhtml
303: front-ends are unable to render them in any meaningful way.
304: Furthermore, behaviour when encountering badly-nested blocks is not
305: consistent across troff implementations, especially when using multiple
306: levels of badly-nested blocks.
1.2 kristaps 307: .Sh EXAMPLES
308: The following example reads lines from stdin and parses them, operating
1.33 kristaps 309: on the finished parse tree with
1.2 kristaps 310: .Fn parsed .
1.37 kristaps 311: This example does not error-check nor free memory upon failure.
312: .Bd -literal -offset indent
1.44 kristaps 313: struct regset regs;
1.2 kristaps 314: struct mdoc *mdoc;
1.31 kristaps 315: const struct mdoc_node *node;
1.2 kristaps 316: char *buf;
317: size_t len;
318: int line;
319:
1.44 kristaps 320: bzero(®s, sizeof(struct regset));
1.2 kristaps 321: line = 1;
1.49 schwarze 322: mdoc = mdoc_alloc(®s, NULL, NULL);
1.37 kristaps 323: buf = NULL;
324: alloc_len = 0;
1.2 kristaps 325:
1.37 kristaps 326: while ((len = getline(&buf, &alloc_len, stdin)) >= 0) {
327: if (len && buflen[len - 1] = '\en')
328: buf[len - 1] = '\e0';
329: if ( ! mdoc_parseln(mdoc, line, buf))
330: errx(1, "mdoc_parseln");
331: line++;
1.2 kristaps 332: }
333:
334: if ( ! mdoc_endparse(mdoc))
1.37 kristaps 335: errx(1, "mdoc_endparse");
1.4 kristaps 336: if (NULL == (node = mdoc_node(mdoc)))
1.37 kristaps 337: errx(1, "mdoc_node");
1.2 kristaps 338:
339: parsed(mdoc, node);
340: mdoc_free(mdoc);
341: .Ed
1.38 kristaps 342: .Pp
1.50 kristaps 343: To compile this, execute
344: .Pp
1.51 kristaps 345: .Dl % cc main.c libmdoc.a libmandoc.a
1.50 kristaps 346: .Pp
347: where
1.38 kristaps 348: .Pa main.c
1.50 kristaps 349: is the example file.
1.17 kristaps 350: .Sh SEE ALSO
1.20 kristaps 351: .Xr mandoc 1 ,
1.14 kristaps 352: .Xr mdoc 7
1.2 kristaps 353: .Sh AUTHORS
354: The
355: .Nm
1.38 kristaps 356: library was written by
1.37 kristaps 357: .An Kristaps Dzonsons Aq kristaps@bsd.lv .
CVSweb