version 1.43, 2010/06/27 15:52:41 |
version 1.52, 2011/01/01 12:18:37 |
|
|
.\" $Id$ |
.\" $Id$ |
.\" |
.\" |
.\" Copyright (c) 2009-2010 Kristaps Dzonsons <kristaps@bsd.lv> |
.\" Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv> |
|
.\" Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org> |
.\" |
.\" |
.\" Permission to use, copy, modify, and distribute this software for any |
.\" Permission to use, copy, modify, and distribute this software for any |
.\" purpose with or without fee is hereby granted, provided that the above |
.\" purpose with or without fee is hereby granted, provided that the above |
|
|
.Nd mdoc macro compiler library |
.Nd mdoc macro compiler library |
.Sh SYNOPSIS |
.Sh SYNOPSIS |
.In mandoc.h |
.In mandoc.h |
.In regs.h |
|
.In mdoc.h |
.In mdoc.h |
.Vt extern const char * const * mdoc_macronames; |
.Vt extern const char * const * mdoc_macronames; |
.Vt extern const char * const * mdoc_argnames; |
.Vt extern const char * const * mdoc_argnames; |
|
.Ft int |
|
.Fo mdoc_addspan |
|
.Fa "struct mdoc *mdoc" |
|
.Fa "const struct tbl_span *span" |
|
.Fc |
.Ft "struct mdoc *" |
.Ft "struct mdoc *" |
.Fo mdoc_alloc |
.Fo mdoc_alloc |
.Fa "const struct regset *regs" |
.Fa "struct regset *regs" |
.Fa "void *data" |
.Fa "void *data" |
.Fa "int pflags" |
|
.Fa "mandocmsg msgs" |
.Fa "mandocmsg msgs" |
.Fc |
.Fc |
.Ft int |
.Ft int |
|
|
.Fn mdoc_reset |
.Fn mdoc_reset |
function may be used in order to reset the parser for another input |
function may be used in order to reset the parser for another input |
sequence. |
sequence. |
See the |
|
.Sx EXAMPLES |
|
section for a simple example. |
|
.Pp |
|
This section further defines the |
|
.Sx Types , |
|
.Sx Functions |
|
and |
|
.Sx Variables |
|
available to programmers. |
|
Following that, the |
|
.Sx Abstract Syntax Tree |
|
section documents the output tree. |
|
.Ss Types |
.Ss Types |
Both functions (see |
|
.Sx Functions ) |
|
and variables (see |
|
.Sx Variables ) |
|
may use the following types: |
|
.Bl -ohang |
.Bl -ohang |
.It Vt struct mdoc |
.It Vt struct mdoc |
An opaque type defined in |
An opaque type. |
.Pa mdoc.c . |
|
Its values are only used privately within the library. |
Its values are only used privately within the library. |
.It Vt struct mdoc_node |
.It Vt struct mdoc_node |
A parsed node. |
A parsed node. |
Defined in |
|
.Pa mdoc.h . |
|
See |
See |
.Sx Abstract Syntax Tree |
.Sx Abstract Syntax Tree |
for details. |
for details. |
.It Vt mandocmsg |
|
A function callback type defined in |
|
.Pa mandoc.h . |
|
.El |
.El |
.Ss Functions |
.Ss Functions |
Function descriptions follow: |
|
.Bl -ohang |
.Bl -ohang |
|
.It Fn mdoc_addspan |
|
Add a table span to the parsing stream. |
|
Returns 0 on failure, 1 on success. |
.It Fn mdoc_alloc |
.It Fn mdoc_alloc |
Allocates a parsing structure. |
Allocates a parsing structure. |
The |
The |
.Fa data |
.Fa data |
pointer is passed to |
pointer is passed to |
.Fa msgs . |
.Fa msgs . |
The |
|
.Fa pflags |
|
arguments are defined in |
|
.Pa mdoc.h . |
|
Returns NULL on failure. |
Returns NULL on failure. |
If non-NULL, the pointer must be freed with |
If non-NULL, the pointer must be freed with |
.Fn mdoc_free . |
.Fn mdoc_free . |
|
|
return 0, the data will be incomplete. |
return 0, the data will be incomplete. |
.El |
.El |
.Ss Variables |
.Ss Variables |
The following variables are also defined: |
|
.Bl -ohang |
.Bl -ohang |
.It Va mdoc_macronames |
.It Va mdoc_macronames |
An array of string-ified token names. |
An array of string-ified token names. |
|
|
fields), its position in the tree (the |
fields), its position in the tree (the |
.Va parent , |
.Va parent , |
.Va child , |
.Va child , |
|
.Va nchild , |
.Va next |
.Va next |
and |
and |
.Va prev |
.Va prev |
fields) and some type-specific data. |
fields) and some type-specific data, in particular, for nodes generated |
|
from macros, the generating macro in the |
|
.Va tok |
|
field. |
.Pp |
.Pp |
The tree itself is arranged according to the following normal form, |
The tree itself is arranged according to the following normal form, |
where capitalised non-terminals represent nodes. |
where capitalised non-terminals represent nodes. |
Line 235 where capitalised non-terminals represent nodes. |
|
Line 216 where capitalised non-terminals represent nodes. |
|
.It ELEMENT |
.It ELEMENT |
\(<- TEXT* |
\(<- TEXT* |
.It HEAD |
.It HEAD |
\(<- mnode+ |
\(<- mnode* |
.It BODY |
.It BODY |
\(<- mnode+ |
\(<- mnode* [ENDBODY mnode*] |
.It TAIL |
.It TAIL |
\(<- mnode+ |
\(<- mnode* |
.It TEXT |
.It TEXT |
\(<- [[:printable:],0x1e]* |
\(<- [[:printable:],0x1e]* |
.El |
.El |
Line 253 an empty line will produce a zero-length string. |
|
Line 234 an empty line will produce a zero-length string. |
|
Multiple body parts are only found in invocations of |
Multiple body parts are only found in invocations of |
.Sq \&Bl \-column , |
.Sq \&Bl \-column , |
where a new body introduces a new phrase. |
where a new body introduces a new phrase. |
|
.Ss Badly-nested Blocks |
|
The ENDBODY node is available to end the formatting associated |
|
with a given block before the physical end of that block. |
|
It has a non-null |
|
.Va end |
|
field, is of the BODY |
|
.Va type , |
|
has the same |
|
.Va tok |
|
as the BLOCK it is ending, and has a |
|
.Va pending |
|
field pointing to that BLOCK's BODY node. |
|
It is an indirect child of that BODY node |
|
and has no children of its own. |
|
.Pp |
|
An ENDBODY node is generated when a block ends while one of its child |
|
blocks is still open, like in the following example: |
|
.Bd -literal -offset indent |
|
\&.Ao ao |
|
\&.Bo bo ac |
|
\&.Ac bc |
|
\&.Bc end |
|
.Ed |
|
.Pp |
|
This example results in the following block structure: |
|
.Bd -literal -offset indent |
|
BLOCK Ao |
|
HEAD Ao |
|
BODY Ao |
|
TEXT ao |
|
BLOCK Bo, pending -> Ao |
|
HEAD Bo |
|
BODY Bo |
|
TEXT bo |
|
TEXT ac |
|
ENDBODY Ao, pending -> Ao |
|
TEXT bc |
|
TEXT end |
|
.Ed |
|
.Pp |
|
Here, the formatting of the |
|
.Sq \&Ao |
|
block extends from TEXT ao to TEXT ac, |
|
while the formatting of the |
|
.Sq \&Bo |
|
block extends from TEXT bo to TEXT bc. |
|
It renders as follows in |
|
.Fl T Ns Cm ascii |
|
mode: |
|
.Pp |
|
.Dl <ao [bo ac> bc] end |
|
.Pp |
|
Support for badly-nested blocks is only provided for backward |
|
compatibility with some older |
|
.Xr mdoc 7 |
|
implementations. |
|
Using badly-nested blocks is |
|
.Em strongly discouraged : |
|
the |
|
.Fl T Ns Cm html |
|
and |
|
.Fl T Ns Cm xhtml |
|
front-ends are unable to render them in any meaningful way. |
|
Furthermore, behaviour when encountering badly-nested blocks is not |
|
consistent across troff implementations, especially when using multiple |
|
levels of badly-nested blocks. |
.Sh EXAMPLES |
.Sh EXAMPLES |
The following example reads lines from stdin and parses them, operating |
The following example reads lines from stdin and parses them, operating |
on the finished parse tree with |
on the finished parse tree with |
.Fn parsed . |
.Fn parsed . |
This example does not error-check nor free memory upon failure. |
This example does not error-check nor free memory upon failure. |
.Bd -literal -offset indent |
.Bd -literal -offset indent |
|
struct regset regs; |
struct mdoc *mdoc; |
struct mdoc *mdoc; |
const struct mdoc_node *node; |
const struct mdoc_node *node; |
char *buf; |
char *buf; |
size_t len; |
size_t len; |
int line; |
int line; |
|
|
|
bzero(®s, sizeof(struct regset)); |
line = 1; |
line = 1; |
mdoc = mdoc_alloc(NULL, 0, NULL); |
mdoc = mdoc_alloc(®s, NULL, NULL); |
buf = NULL; |
buf = NULL; |
alloc_len = 0; |
alloc_len = 0; |
|
|
Line 287 parsed(mdoc, node); |
|
Line 336 parsed(mdoc, node); |
|
mdoc_free(mdoc); |
mdoc_free(mdoc); |
.Ed |
.Ed |
.Pp |
.Pp |
Please see |
To compile this, execute |
|
.Pp |
|
.Dl % cc main.c libmdoc.a libmandoc.a |
|
.Pp |
|
where |
.Pa main.c |
.Pa main.c |
in the source archive for a rigorous reference. |
is the example file. |
.Sh SEE ALSO |
.Sh SEE ALSO |
.Xr mandoc 1 , |
.Xr mandoc 1 , |
.Xr mdoc 7 |
.Xr mdoc 7 |