[BACK]Return to mdoc.3 CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mdoc.3, Revision 1.6

1.6     ! schwarze    1: .\"    $Id: mdoc.3,v 1.5 2009/10/20 10:15:04 schwarze Exp $
1.1       kristaps    2: .\"
1.6     ! 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.
1.1       kristaps    8: .\"
1.2       schwarze    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.4       schwarze   16: .\"
1.6     ! schwarze   17: .Dd $Mdocdate: October 20 2009 $
1.2       schwarze   18: .Dt MDOC 3
1.1       kristaps   19: .Os
                     20: .\" SECTION
                     21: .Sh NAME
                     22: .Nm mdoc_alloc ,
                     23: .Nm mdoc_parseln ,
                     24: .Nm mdoc_endparse ,
                     25: .Nm mdoc_node ,
                     26: .Nm mdoc_meta ,
                     27: .Nm mdoc_free ,
                     28: .Nm mdoc_reset
                     29: .Nd mdoc macro compiler library
                     30: .\" SECTION
                     31: .Sh SYNOPSIS
1.5       schwarze   32: .In mdoc.h
1.1       kristaps   33: .Vt extern const char * const * mdoc_macronames;
                     34: .Vt extern const char * const * mdoc_argnames;
                     35: .Ft "struct mdoc *"
                     36: .Fn mdoc_alloc "void *data" "int pflags" "const struct mdoc_cb *cb"
                     37: .Ft int
                     38: .Fn mdoc_reset "struct mdoc *mdoc"
                     39: .Ft void
                     40: .Fn mdoc_free "struct mdoc *mdoc"
                     41: .Ft int
                     42: .Fn mdoc_parseln "struct mdoc *mdoc" "int line" "char *buf"
                     43: .Ft "const struct mdoc_node *"
1.3       schwarze   44: .Fn mdoc_node "const struct mdoc *mdoc"
1.1       kristaps   45: .Ft "const struct mdoc_meta *"
1.3       schwarze   46: .Fn mdoc_meta "const struct mdoc *mdoc"
1.1       kristaps   47: .Ft int
                     48: .Fn mdoc_endparse "struct mdoc *mdoc"
                     49: .\" SECTION
                     50: .Sh DESCRIPTION
                     51: The
                     52: .Nm mdoc
1.4       schwarze   53: library parses lines of
1.1       kristaps   54: .Xr mdoc 7
                     55: input (and
                     56: .Em only
                     57: mdoc) into an abstract syntax tree (AST).
                     58: .\" PARAGRAPH
                     59: .Pp
                     60: In general, applications initiate a parsing sequence with
                     61: .Fn mdoc_alloc ,
1.4       schwarze   62: parse each line in a document with
1.1       kristaps   63: .Fn mdoc_parseln ,
                     64: close the parsing session with
                     65: .Fn mdoc_endparse ,
                     66: operate over the syntax tree returned by
1.4       schwarze   67: .Fn mdoc_node
1.1       kristaps   68: and
                     69: .Fn mdoc_meta ,
                     70: then free all allocated memory with
                     71: .Fn mdoc_free .
                     72: The
                     73: .Fn mdoc_reset
                     74: function may be used in order to reset the parser for another input
                     75: sequence.  See the
                     76: .Sx EXAMPLES
                     77: section for a full example.
                     78: .\" PARAGRAPH
                     79: .Pp
1.4       schwarze   80: This section further defines the
1.1       kristaps   81: .Sx Types ,
1.4       schwarze   82: .Sx Functions
1.1       kristaps   83: and
                     84: .Sx Variables
                     85: available to programmers.  Following that, the
1.4       schwarze   86: .Sx Abstract Syntax Tree
1.1       kristaps   87: section documents the output tree.
                     88: .\" SUBSECTION
                     89: .Ss Types
                     90: Both functions (see
                     91: .Sx Functions )
                     92: and variables (see
                     93: .Sx Variables )
                     94: may use the following types:
1.6     ! schwarze   95: .Bl -ohang
1.1       kristaps   96: .\" LIST-ITEM
                     97: .It Vt struct mdoc
                     98: An opaque type defined in
                     99: .Pa mdoc.c .
                    100: Its values are only used privately within the library.
                    101: .\" LIST-ITEM
                    102: .It Vt struct mdoc_cb
                    103: A set of message callbacks defined in
                    104: .Pa mdoc.h .
                    105: .\" LIST-ITEM
                    106: .It Vt struct mdoc_node
                    107: A parsed node.  Defined in
                    108: .Pa mdoc.h .
1.4       schwarze  109: See
1.1       kristaps  110: .Sx Abstract Syntax Tree
                    111: for details.
                    112: .El
                    113: .\" SUBSECTION
                    114: .Ss Functions
                    115: Function descriptions follow:
1.6     ! schwarze  116: .Bl -ohang
1.1       kristaps  117: .\" LIST-ITEM
                    118: .It Fn mdoc_alloc
                    119: Allocates a parsing structure.  The
                    120: .Fa data
                    121: pointer is passed to callbacks in
1.4       schwarze  122: .Fa cb ,
                    123: which are documented further in the header file.
1.1       kristaps  124: The
                    125: .Fa pflags
                    126: arguments are defined in
                    127: .Pa mdoc.h .
                    128: Returns NULL on failure.  If non-NULL, the pointer must be freed with
                    129: .Fn mdoc_free .
                    130: .\" LIST-ITEM
                    131: .It Fn mdoc_reset
1.4       schwarze  132: Reset the parser for another parse routine.  After its use,
1.1       kristaps  133: .Fn mdoc_parseln
                    134: behaves as if invoked for the first time.  If it returns 0, memory could
                    135: not be allocated.
                    136: .\" LIST-ITEM
                    137: .It Fn mdoc_free
                    138: Free all resources of a parser.  The pointer is no longer valid after
                    139: invocation.
                    140: .\" LIST-ITEM
                    141: .It Fn mdoc_parseln
                    142: Parse a nil-terminated line of input.  This line should not contain the
1.4       schwarze  143: trailing newline.  Returns 0 on failure, 1 on success.  The input buffer
1.1       kristaps  144: .Fa buf
                    145: is modified by this function.
                    146: .\" LIST-ITEM
                    147: .It Fn mdoc_endparse
1.4       schwarze  148: Signals that the parse is complete.  Note that if
1.1       kristaps  149: .Fn mdoc_endparse
                    150: is called subsequent to
                    151: .Fn mdoc_node ,
                    152: the resulting tree is incomplete.  Returns 0 on failure, 1 on success.
                    153: .\" LIST-ITEM
                    154: .It Fn mdoc_node
1.4       schwarze  155: Returns the first node of the parse.  Note that if
1.1       kristaps  156: .Fn mdoc_parseln
                    157: or
                    158: .Fn mdoc_endparse
                    159: return 0, the tree will be incomplete.
                    160: .It Fn mdoc_meta
                    161: Returns the document's parsed meta-data.  If this information has not
1.4       schwarze  162: yet been supplied or
1.1       kristaps  163: .Fn mdoc_parseln
                    164: or
                    165: .Fn mdoc_endparse
                    166: return 0, the data will be incomplete.
                    167: .El
                    168: .\" SUBSECTION
                    169: .Ss Variables
                    170: The following variables are also defined:
1.6     ! schwarze  171: .Bl -ohang
1.1       kristaps  172: .\" LIST-ITEM
                    173: .It Va mdoc_macronames
                    174: An array of string-ified token names.
                    175: .\" LIST-ITEM
                    176: .It Va mdoc_argnames
                    177: An array of string-ified token argument names.
                    178: .El
                    179: .\" SUBSECTION
                    180: .Ss Abstract Syntax Tree
1.4       schwarze  181: The
1.1       kristaps  182: .Nm
                    183: functions produce an abstract syntax tree (AST) describing input in a
                    184: regular form.  It may be reviewed at any time with
                    185: .Fn mdoc_nodes ;
                    186: however, if called before
                    187: .Fn mdoc_endparse ,
                    188: or after
1.4       schwarze  189: .Fn mdoc_endparse
1.1       kristaps  190: or
                    191: .Fn mdoc_parseln
1.4       schwarze  192: fail, it may be incomplete.
1.1       kristaps  193: .\" PARAGRAPH
                    194: .Pp
                    195: This AST is governed by the ontological
                    196: rules dictated in
                    197: .Xr mdoc 7
1.4       schwarze  198: and derives its terminology accordingly.
1.1       kristaps  199: .Qq In-line
                    200: elements described in
                    201: .Xr mdoc 7
1.4       schwarze  202: are described simply as
1.1       kristaps  203: .Qq elements .
                    204: .\" PARAGRAPH
                    205: .Pp
1.4       schwarze  206: The AST is composed of
1.1       kristaps  207: .Vt struct mdoc_node
                    208: nodes with block, head, body, element, root and text types as declared
                    209: by the
                    210: .Va type
                    211: field.  Each node also provides its parse point (the
                    212: .Va line ,
                    213: .Va sec ,
                    214: and
                    215: .Va pos
                    216: fields), its position in the tree (the
                    217: .Va parent ,
                    218: .Va child ,
1.4       schwarze  219: .Va next
1.1       kristaps  220: and
1.4       schwarze  221: .Va prev
1.1       kristaps  222: fields) and some type-specific data.
                    223: .\" PARAGRAPH
                    224: .Pp
                    225: The tree itself is arranged according to the following normal form,
                    226: where capitalised non-terminals represent nodes.
                    227: .Pp
1.6     ! schwarze  228: .Bl -tag -width "ELEMENTXX" -compact
1.1       kristaps  229: .\" LIST-ITEM
                    230: .It ROOT
                    231: \(<- mnode+
                    232: .It mnode
                    233: \(<- BLOCK | ELEMENT | TEXT
                    234: .It BLOCK
                    235: \(<- (HEAD [TEXT])+ [BODY [TEXT]] [TAIL [TEXT]]
                    236: .It BLOCK
                    237: \(<- BODY [TEXT] [TAIL [TEXT]]
                    238: .It ELEMENT
                    239: \(<- TEXT*
                    240: .It HEAD
                    241: \(<- mnode+
                    242: .It BODY
                    243: \(<- mnode+
                    244: .It TAIL
                    245: \(<- mnode+
                    246: .It TEXT
                    247: \(<- [[:alpha:]]*
                    248: .El
                    249: .\" PARAGRAPH
                    250: .Pp
                    251: Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
                    252: the BLOCK production.  These refer to punctuation marks.  Furthermore,
                    253: although a TEXT node will generally have a non-zero-length string, in
1.4       schwarze  254: the specific case of
1.1       kristaps  255: .Sq \&.Bd \-literal ,
                    256: an empty line will produce a zero-length string.
                    257: .\" SECTION
                    258: .Sh EXAMPLES
                    259: The following example reads lines from stdin and parses them, operating
1.4       schwarze  260: on the finished parse tree with
1.1       kristaps  261: .Fn parsed .
1.6     ! schwarze  262: This example does not error-check nor free memory upon failure.
        !           263: .Bd -literal -offset indent
1.1       kristaps  264: struct mdoc *mdoc;
1.3       schwarze  265: const struct mdoc_node *node;
1.1       kristaps  266: char *buf;
                    267: size_t len;
                    268: int line;
                    269:
                    270: line = 1;
                    271: mdoc = mdoc_alloc(NULL, 0, NULL);
1.6     ! schwarze  272: buf = NULL;
        !           273: alloc_len = 0;
1.1       kristaps  274:
1.6     ! schwarze  275: while ((len = getline(&buf, &alloc_len, stdin)) >= 0) {
        !           276:     if (len && buflen[len - 1] = '\en')
        !           277:         buf[len - 1] = '\e0';
        !           278:     if ( ! mdoc_parseln(mdoc, line, buf))
        !           279:         errx(1, "mdoc_parseln");
        !           280:     line++;
1.1       kristaps  281: }
                    282:
                    283: if ( ! mdoc_endparse(mdoc))
1.6     ! schwarze  284:     errx(1, "mdoc_endparse");
1.1       kristaps  285: if (NULL == (node = mdoc_node(mdoc)))
1.6     ! schwarze  286:     errx(1, "mdoc_node");
1.1       kristaps  287:
                    288: parsed(mdoc, node);
                    289: mdoc_free(mdoc);
                    290: .Ed
                    291: .\" SECTION
                    292: .Sh SEE ALSO
                    293: .Xr mandoc 1 ,
                    294: .Xr mdoc 7
                    295: .\" SECTION
                    296: .Sh AUTHORS
                    297: The
                    298: .Nm
1.4       schwarze  299: utility was written by
1.6     ! schwarze  300: .An Kristaps Dzonsons Aq kristaps@bsd.lv .
1.1       kristaps  301: .\" SECTION
                    302: .Sh CAVEATS
                    303: .Bl -dash -compact
                    304: .\" LIST-ITEM
                    305: .It
1.4       schwarze  306: The
1.1       kristaps  307: .Sq \&.Xc
                    308: and
                    309: .Sq \&.Xo
                    310: macros aren't handled when used to span lines for the
                    311: .Sq \&.It
1.4       schwarze  312: macro.
1.1       kristaps  313: .\" LIST-ITEM
                    314: .It
1.4       schwarze  315: The
1.1       kristaps  316: .Sq \&.Bsx
                    317: macro family doesn't yet understand version arguments.
                    318: .\" LIST-ITEM
                    319: .It
                    320: If not given a value, the \-offset argument to
                    321: .Sq \&.Bd
                    322: and
                    323: .Sq \&.Bl
1.4       schwarze  324: should be the width of
1.1       kristaps  325: .Qq <string> ;
1.4       schwarze  326: instead, a value of
1.1       kristaps  327: .Li 10n
                    328: is provided.
                    329: .\" LIST-ITEM
                    330: .It
                    331: Columns widths in
                    332: .Sq \&.Bl \-column
                    333: should default to width
                    334: .Qq <stringx>
                    335: if not included.
                    336: .El