[BACK]Return to man.3 CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/man.3, Revision 1.23

1.23    ! kristaps    1: .\"    $Id: man.3,v 1.22 2010/07/07 15:04:54 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.23    ! kristaps   17: .Dd $Mdocdate: July 7 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;
                     34: .Ft "struct man *"
1.20      kristaps   35: .Fo man_alloc
1.21      kristaps   36: .Fa "struct regset *regs"
1.20      kristaps   37: .Fa "void *data"
                     38: .Fa "int pflags"
                     39: .Fa "mandocmsg msgs"
                     40: .Fc
1.17      kristaps   41: .Ft int
                     42: .Fn man_endparse "struct man *man"
1.1       kristaps   43: .Ft void
                     44: .Fn man_free "struct man *man"
1.17      kristaps   45: .Ft "const struct man_meta *"
                     46: .Fn man_meta "const struct man *man"
1.1       kristaps   47: .Ft "const struct man_node *"
1.6       kristaps   48: .Fn man_node "const struct man *man"
1.1       kristaps   49: .Ft int
1.19      kristaps   50: .Fo man_parseln
                     51: .Fa "struct man *man"
                     52: .Fa "int line"
                     53: .Fa "char *buf"
                     54: .Fc
1.17      kristaps   55: .Ft void
                     56: .Fn man_reset "struct man *man"
1.1       kristaps   57: .Sh DESCRIPTION
                     58: The
1.13      kristaps   59: .Nm
1.7       kristaps   60: library parses lines of
1.1       kristaps   61: .Xr man 7
1.17      kristaps   62: input into an abstract syntax tree (AST).
1.1       kristaps   63: .Pp
                     64: In general, applications initiate a parsing sequence with
                     65: .Fn man_alloc ,
1.7       kristaps   66: parse each line in a document with
1.1       kristaps   67: .Fn man_parseln ,
                     68: close the parsing session with
                     69: .Fn man_endparse ,
                     70: operate over the syntax tree returned by
1.7       kristaps   71: .Fn man_node
1.1       kristaps   72: and
                     73: .Fn man_meta ,
                     74: then free all allocated memory with
                     75: .Fn man_free .
                     76: The
                     77: .Fn man_reset
                     78: function may be used in order to reset the parser for another input
1.17      kristaps   79: sequence.
                     80: See the
1.1       kristaps   81: .Sx EXAMPLES
                     82: section for a full example.
1.13      kristaps   83: .Pp
                     84: Beyond the full set of macros defined in
                     85: .Xr man 7 ,
                     86: the
                     87: .Nm
                     88: library also accepts the following macros:
1.1       kristaps   89: .Pp
1.13      kristaps   90: .Bl -tag -width Ds -compact
                     91: .It PD
                     92: Has no effect.  Handled as a current-scope line macro.
                     93: .It Sp
                     94: A synonym for
                     95: .Sq sp 0.5v
                     96: .Pq part of the standard preamble for Perl documentation .
                     97: Handled as a line macro.
                     98: .It Vb
                     99: A synonym for
                    100: .Sq nf
                    101: .Pq part of the standard preamble for Perl documentation .
                    102: Handled as a current-scope line macro.
                    103: .It Ve
                    104: A synonym for
                    105: .Sq fi ,
                    106: closing
                    107: .Sq Vb
                    108: .Pq part of the standard preamble for Perl documentation .
                    109: Handled as a current-scope line macro.
                    110: .El
1.14      kristaps  111: .Pp
                    112: Furthermore, the following escapes are accepted to allow
                    113: .Xr pod2man 1
1.15      kristaps  114: documents to be correctly formatted:
1.14      kristaps  115: \e*(-- (dash),
                    116: \e*(PI (pi),
                    117: \e*(L" (left double-quote),
                    118: \e*(R" (right double-quote),
                    119: \e*(C` (left single-quote),
                    120: \e*(C' (right single-quote),
                    121: \e*(Aq (apostrophe),
                    122: \e*^ (hat),
                    123: \e*, (comma),
                    124: \e*~ (tilde),
                    125: \e*/ (forward slash),
                    126: \e*: (umlaut),
                    127: \e*8 (beta),
                    128: \e*o (degree),
                    129: \e*(D- (Eth),
                    130: \e*(d- (eth),
                    131: \e*(Th (Thorn),
                    132: and
                    133: \e*(th (thorn).
1.13      kristaps  134: .Sh REFERENCE
1.7       kristaps  135: This section further defines the
1.1       kristaps  136: .Sx Types ,
1.7       kristaps  137: .Sx Functions
1.1       kristaps  138: and
                    139: .Sx Variables
1.17      kristaps  140: available to programmers.
                    141: Following that, the
1.7       kristaps  142: .Sx Abstract Syntax Tree
1.1       kristaps  143: section documents the output tree.
                    144: .Ss Types
                    145: Both functions (see
                    146: .Sx Functions )
                    147: and variables (see
                    148: .Sx Variables )
                    149: may use the following types:
1.12      kristaps  150: .Bl -ohang
1.1       kristaps  151: .It Vt struct man
                    152: An opaque type defined in
                    153: .Pa man.c .
                    154: Its values are only used privately within the library.
1.17      kristaps  155: .It Vt mandocmsg
                    156: A function callback type defined in
                    157: .Pa mandoc.h .
1.1       kristaps  158: .It Vt struct man_node
1.17      kristaps  159: A parsed node.
                    160: Defined in
1.1       kristaps  161: .Pa man.h .
1.7       kristaps  162: See
1.1       kristaps  163: .Sx Abstract Syntax Tree
                    164: for details.
                    165: .El
                    166: .Ss Functions
                    167: Function descriptions follow:
1.12      kristaps  168: .Bl -ohang
1.1       kristaps  169: .It Fn man_alloc
1.17      kristaps  170: Allocates a parsing structure.
                    171: The
1.1       kristaps  172: .Fa data
1.18      kristaps  173: pointer is passed to
                    174: .Fa msgs .
1.1       kristaps  175: The
                    176: .Fa pflags
                    177: arguments are defined in
                    178: .Pa man.h .
1.17      kristaps  179: Returns NULL on failure.
                    180: If non-NULL, the pointer must be freed with
1.1       kristaps  181: .Fn man_free .
                    182: .It Fn man_reset
1.17      kristaps  183: Reset the parser for another parse routine.
                    184: After its use,
1.1       kristaps  185: .Fn man_parseln
                    186: behaves as if invoked for the first time.
                    187: .It Fn man_free
1.17      kristaps  188: Free all resources of a parser.
                    189: The pointer is no longer valid after invocation.
1.1       kristaps  190: .It Fn man_parseln
1.17      kristaps  191: Parse a nil-terminated line of input.
                    192: This line should not contain the trailing newline.
                    193: Returns 0 on failure, 1 on success.
                    194: The input buffer
1.1       kristaps  195: .Fa buf
                    196: is modified by this function.
                    197: .It Fn man_endparse
1.17      kristaps  198: Signals that the parse is complete.
                    199: Note that if
1.1       kristaps  200: .Fn man_endparse
                    201: is called subsequent to
                    202: .Fn man_node ,
1.17      kristaps  203: the resulting tree is incomplete.
                    204: Returns 0 on failure, 1 on success.
1.1       kristaps  205: .It Fn man_node
1.17      kristaps  206: Returns the first node of the parse.
                    207: Note that if
1.1       kristaps  208: .Fn man_parseln
                    209: or
                    210: .Fn man_endparse
                    211: return 0, the tree will be incomplete.
                    212: .It Fn man_meta
1.17      kristaps  213: Returns the document's parsed meta-data.
                    214: If this information has not yet been supplied or
1.1       kristaps  215: .Fn man_parseln
                    216: or
                    217: .Fn man_endparse
                    218: return 0, the data will be incomplete.
                    219: .El
                    220: .Ss Variables
                    221: The following variables are also defined:
1.12      kristaps  222: .Bl -ohang
1.1       kristaps  223: .It Va man_macronames
                    224: An array of string-ified token names.
                    225: .El
                    226: .Ss Abstract Syntax Tree
1.7       kristaps  227: The
1.1       kristaps  228: .Nm
                    229: functions produce an abstract syntax tree (AST) describing input in a
1.17      kristaps  230: regular form.
                    231: It may be reviewed at any time with
1.1       kristaps  232: .Fn man_nodes ;
                    233: however, if called before
                    234: .Fn man_endparse ,
                    235: or after
1.7       kristaps  236: .Fn man_endparse
1.1       kristaps  237: or
                    238: .Fn man_parseln
1.7       kristaps  239: fail, it may be incomplete.
1.1       kristaps  240: .Pp
1.17      kristaps  241: This AST is governed by the ontological rules dictated in
1.1       kristaps  242: .Xr man 7
1.7       kristaps  243: and derives its terminology accordingly.
1.1       kristaps  244: .Pp
1.7       kristaps  245: The AST is composed of
1.1       kristaps  246: .Vt struct man_node
1.17      kristaps  247: nodes with element, root and text types as declared by the
1.1       kristaps  248: .Va type
1.17      kristaps  249: field.
                    250: Each node also provides its parse point (the
1.1       kristaps  251: .Va line ,
                    252: .Va sec ,
                    253: and
                    254: .Va pos
                    255: fields), its position in the tree (the
                    256: .Va parent ,
                    257: .Va child ,
1.7       kristaps  258: .Va next
1.1       kristaps  259: and
1.7       kristaps  260: .Va prev
1.1       kristaps  261: fields) and some type-specific data.
                    262: .Pp
                    263: The tree itself is arranged according to the following normal form,
                    264: where capitalised non-terminals represent nodes.
                    265: .Pp
1.12      kristaps  266: .Bl -tag -width "ELEMENTXX" -compact
1.1       kristaps  267: .It ROOT
                    268: \(<- mnode+
                    269: .It mnode
1.8       kristaps  270: \(<- ELEMENT | TEXT | BLOCK
                    271: .It BLOCK
                    272: \(<- HEAD BODY
                    273: .It HEAD
                    274: \(<- mnode*
                    275: .It BODY
                    276: \(<- mnode*
1.1       kristaps  277: .It ELEMENT
                    278: \(<- ELEMENT | TEXT*
                    279: .It TEXT
                    280: \(<- [[:alpha:]]*
                    281: .El
                    282: .Pp
                    283: The only elements capable of nesting other elements are those with
                    284: next-lint scope as documented in
                    285: .Xr man 7 .
                    286: .Sh EXAMPLES
                    287: The following example reads lines from stdin and parses them, operating
1.7       kristaps  288: on the finished parse tree with
1.1       kristaps  289: .Fn parsed .
1.12      kristaps  290: This example does not error-check nor free memory upon failure.
                    291: .Bd -literal -offset indent
1.21      kristaps  292: struct regset regs;
1.1       kristaps  293: struct man *man;
                    294: struct man_node *node;
                    295: char *buf;
                    296: size_t len;
                    297: int line;
                    298:
1.21      kristaps  299: bzero(&regs, sizeof(struct regset));
1.1       kristaps  300: line = 1;
1.21      kristaps  301: man = man_alloc(&regs, NULL, 0, NULL);
1.12      kristaps  302: buf = NULL;
                    303: alloc_len = 0;
1.1       kristaps  304:
1.12      kristaps  305: while ((len = getline(&buf, &alloc_len, stdin)) >= 0) {
                    306:     if (len && buflen[len - 1] = '\en')
                    307:         buf[len - 1] = '\e0';
                    308:     if ( ! man_parseln(man, line, buf))
                    309:         errx(1, "man_parseln");
                    310:     line++;
1.1       kristaps  311: }
                    312:
1.12      kristaps  313: free(buf);
                    314:
1.1       kristaps  315: if ( ! man_endparse(man))
1.12      kristaps  316:     errx(1, "man_endparse");
1.1       kristaps  317: if (NULL == (node = man_node(man)))
1.12      kristaps  318:     errx(1, "man_node");
1.1       kristaps  319:
                    320: parsed(man, node);
                    321: man_free(man);
                    322: .Ed
1.17      kristaps  323: .Pp
                    324: Please see
                    325: .Pa main.c
                    326: in the source archive for a rigorous reference.
1.1       kristaps  327: .Sh SEE ALSO
                    328: .Xr mandoc 1 ,
                    329: .Xr man 7
                    330: .Sh AUTHORS
                    331: The
                    332: .Nm
1.17      kristaps  333: library was written by
1.12      kristaps  334: .An Kristaps Dzonsons Aq kristaps@bsd.lv .

CVSweb