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

Annotation of mandoc/mdoc.c, Revision 1.41

1.41    ! kristaps    1: /* $Id: mdoc.c,v 1.40 2009/01/20 13:05:28 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19: #include <assert.h>
                     20: #include <ctype.h>
                     21: #include <err.h>
                     22: #include <stdarg.h>
                     23: #include <stdlib.h>
                     24: #include <stdio.h>
                     25: #include <string.h>
                     26:
                     27: #include "private.h"
                     28:
1.41    ! kristaps   29: /*
        !            30:  * Main caller in the libmdoc library.  This begins the parsing routine,
        !            31:  * handles allocation of data, and so forth.  Most of the "work" is done
        !            32:  * in macro.c, but this orchestrates who does what, when.
        !            33:  */
        !            34:
1.1       kristaps   35: const  char *const __mdoc_macronames[MDOC_MAX] = {
                     36:        "\\\"",         "Dd",           "Dt",           "Os",
                     37:        "Sh",           "Ss",           "Pp",           "D1",
                     38:        "Dl",           "Bd",           "Ed",           "Bl",
                     39:        "El",           "It",           "Ad",           "An",
                     40:        "Ar",           "Cd",           "Cm",           "Dv",
                     41:        "Er",           "Ev",           "Ex",           "Fa",
                     42:        "Fd",           "Fl",           "Fn",           "Ft",
                     43:        "Ic",           "In",           "Li",           "Nd",
                     44:        "Nm",           "Op",           "Ot",           "Pa",
                     45:        "Rv",           "St",           "Va",           "Vt",
                     46:        /* LINTED */
                     47:        "Xr",           "\%A",          "\%B",          "\%D",
                     48:        /* LINTED */
                     49:        "\%I",          "\%J",          "\%N",          "\%O",
                     50:        /* LINTED */
                     51:        "\%P",          "\%R",          "\%T",          "\%V",
                     52:        "Ac",           "Ao",           "Aq",           "At",
                     53:        "Bc",           "Bf",           "Bo",           "Bq",
                     54:        "Bsx",          "Bx",           "Db",           "Dc",
                     55:        "Do",           "Dq",           "Ec",           "Ef",
                     56:        "Em",           "Eo",           "Fx",           "Ms",
                     57:        "No",           "Ns",           "Nx",           "Ox",
                     58:        "Pc",           "Pf",           "Po",           "Pq",
                     59:        "Qc",           "Ql",           "Qo",           "Qq",
                     60:        "Re",           "Rs",           "Sc",           "So",
                     61:        "Sq",           "Sm",           "Sx",           "Sy",
                     62:        "Tn",           "Ux",           "Xc",           "Xo",
                     63:        "Fo",           "Fc",           "Oo",           "Oc",
                     64:        "Bk",           "Ek",           "Bt",           "Hf",
                     65:        "Fr",           "Ud",
                     66:        };
                     67:
                     68: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                     69:        "split",                "nosplit",              "ragged",
                     70:        "unfilled",             "literal",              "file",
                     71:        "offset",               "bullet",               "dash",
                     72:        "hyphen",               "item",                 "enum",
                     73:        "tag",                  "diag",                 "hang",
                     74:        "ohang",                "inset",                "column",
                     75:        "width",                "compact",              "std",
                     76:        "p1003.1-88",           "p1003.1-90",           "p1003.1-96",
                     77:        "p1003.1-2001",         "p1003.1-2004",         "p1003.1",
                     78:        "p1003.1b",             "p1003.1b-93",          "p1003.1c-95",
                     79:        "p1003.1g-2000",        "p1003.2-92",           "p1387.2-95",
                     80:        "p1003.2",              "p1387.2",              "isoC-90",
                     81:        "isoC-amd1",            "isoC-tcor1",           "isoC-tcor2",
                     82:        "isoC-99",              "ansiC",                "ansiC-89",
                     83:        "ansiC-99",             "ieee754",              "iso8802-3",
                     84:        "xpg3",                 "xpg4",                 "xpg4.2",
                     85:        "xpg4.3",               "xbd5",                 "xcu5",
                     86:        "xsh5",                 "xns5",                 "xns5.2d2.0",
                     87:        "xcurses4.2",           "susv2",                "susv3",
                     88:        "svid4",                "filled",               "words",
1.14      kristaps   89:        "emphasis",             "symbolic",
1.1       kristaps   90:        };
                     91:
1.41    ! kristaps   92: /* Central table of library: who gets parsed how. */
        !            93:
1.1       kristaps   94: const  struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
                     95:        { NULL, 0 }, /* \" */
1.39      kristaps   96:        { macro_constant, MDOC_PROLOGUE }, /* Dd */
                     97:        { macro_constant, MDOC_PROLOGUE }, /* Dt */
                     98:        { macro_constant, MDOC_PROLOGUE }, /* Os */
1.13      kristaps   99:        { macro_scoped, 0 }, /* Sh */
                    100:        { macro_scoped, 0 }, /* Ss */
1.5       kristaps  101:        { macro_text, 0 }, /* Pp */
1.19      kristaps  102:        { macro_scoped_line, MDOC_PARSED }, /* D1 */
                    103:        { macro_scoped_line, MDOC_PARSED }, /* Dl */
1.13      kristaps  104:        { macro_scoped, MDOC_EXPLICIT }, /* Bd */
1.29      kristaps  105:        { macro_scoped_close, MDOC_EXPLICIT }, /* Ed */
1.13      kristaps  106:        { macro_scoped, MDOC_EXPLICIT }, /* Bl */
1.29      kristaps  107:        { macro_scoped_close, MDOC_EXPLICIT }, /* El */
                    108:        { macro_scoped, MDOC_PARSED | MDOC_TABSEP}, /* It */
1.19      kristaps  109:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
1.32      kristaps  110:        { macro_text, MDOC_PARSED }, /* An */
1.19      kristaps  111:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
1.12      kristaps  112:        { macro_constant, MDOC_QUOTABLE }, /* Cd */
1.19      kristaps  113:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
                    114:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
                    115:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
                    116:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
1.13      kristaps  117:        { macro_constant, 0 }, /* Ex */
1.19      kristaps  118:        { macro_text, MDOC_CALLABLE | MDOC_QUOTABLE | MDOC_PARSED }, /* Fa */
1.13      kristaps  119:        { macro_constant, 0 }, /* Fd */
1.19      kristaps  120:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
                    121:        { macro_text, MDOC_CALLABLE | MDOC_QUOTABLE | MDOC_PARSED }, /* Fn */
1.32      kristaps  122:        { macro_text, MDOC_PARSED | MDOC_QUOTABLE }, /* Ft */
1.19      kristaps  123:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
1.13      kristaps  124:        { macro_constant, 0 }, /* In */
1.19      kristaps  125:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
1.10      kristaps  126:        { macro_constant, 0 }, /* Nd */
1.19      kristaps  127:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
                    128:        { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
1.13      kristaps  129:        { macro_obsolete, 0 }, /* Ot */
1.19      kristaps  130:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
1.13      kristaps  131:        { macro_constant, 0 }, /* Rv */
1.22      kristaps  132:        /* XXX - .St supposed to be (but isn't) callable. */
1.19      kristaps  133:        { macro_constant_delimited, MDOC_PARSED }, /* St */
                    134:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
                    135:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
                    136:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
1.23      kristaps  137:        { macro_constant, MDOC_QUOTABLE }, /* %A */
                    138:        { macro_constant, MDOC_QUOTABLE }, /* %B */
1.13      kristaps  139:        { macro_constant, MDOC_QUOTABLE }, /* %D */
1.23      kristaps  140:        { macro_constant, MDOC_QUOTABLE }, /* %I */
                    141:        { macro_constant, MDOC_QUOTABLE }, /* %J */
1.13      kristaps  142:        { macro_constant, MDOC_QUOTABLE }, /* %N */
                    143:        { macro_constant, MDOC_QUOTABLE }, /* %O */
                    144:        { macro_constant, MDOC_QUOTABLE }, /* %P */
                    145:        { macro_constant, MDOC_QUOTABLE }, /* %R */
1.23      kristaps  146:        { macro_constant, MDOC_QUOTABLE }, /* %T */
1.13      kristaps  147:        { macro_constant, MDOC_QUOTABLE }, /* %V */
1.29      kristaps  148:        { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
                    149:        { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
1.19      kristaps  150:        { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
1.34      kristaps  151:        { macro_constant_delimited, 0 }, /* At */
1.29      kristaps  152:        { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
1.16      kristaps  153:        { macro_scoped, MDOC_EXPLICIT }, /* Bf */
1.29      kristaps  154:        { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
1.19      kristaps  155:        { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */
                    156:        { macro_constant_delimited, MDOC_PARSED }, /* Bsx */
                    157:        { macro_constant_delimited, MDOC_PARSED }, /* Bx */
1.13      kristaps  158:        { macro_constant, 0 }, /* Db */
1.29      kristaps  159:        { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
                    160:        { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
1.19      kristaps  161:        { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
1.29      kristaps  162:        { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
                    163:        { macro_scoped_close, MDOC_EXPLICIT }, /* Ef */
1.19      kristaps  164:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
1.29      kristaps  165:        { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
1.19      kristaps  166:        { macro_constant_delimited, MDOC_PARSED }, /* Fx */
                    167:        { macro_text, MDOC_PARSED }, /* Ms */
                    168:        { macro_constant_delimited, MDOC_CALLABLE | MDOC_PARSED }, /* No */
                    169:        { macro_constant_delimited, MDOC_CALLABLE | MDOC_PARSED }, /* Ns */
                    170:        { macro_constant_delimited, MDOC_PARSED }, /* Nx */
                    171:        { macro_constant_delimited, MDOC_PARSED }, /* Ox */
1.29      kristaps  172:        { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
1.32      kristaps  173:        { macro_constant_delimited, MDOC_PARSED }, /* Pf */
1.29      kristaps  174:        { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
1.19      kristaps  175:        { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
1.29      kristaps  176:        { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
1.19      kristaps  177:        { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
1.29      kristaps  178:        { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
1.19      kristaps  179:        { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
1.29      kristaps  180:        { macro_scoped_close, MDOC_EXPLICIT }, /* Re */
1.13      kristaps  181:        { macro_scoped, MDOC_EXPLICIT }, /* Rs */
1.29      kristaps  182:        { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
                    183:        { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
1.19      kristaps  184:        { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
1.13      kristaps  185:        { macro_constant, 0 }, /* Sm */
1.19      kristaps  186:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
                    187:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
                    188:        { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
                    189:        { macro_constant_delimited, MDOC_PARSED }, /* Ux */
1.29      kristaps  190:        { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
                    191:        { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
1.19      kristaps  192:        /* XXX - .Fo supposed to be (but isn't) callable. */
                    193:        { macro_scoped, MDOC_EXPLICIT | MDOC_PARSED }, /* Fo */
                    194:        /* XXX - .Fc supposed to be (but isn't) callable. */
1.29      kristaps  195:        { macro_scoped_close, MDOC_EXPLICIT | MDOC_PARSED }, /* Fc */
                    196:        { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
                    197:        { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
1.17      kristaps  198:        { macro_scoped, MDOC_EXPLICIT }, /* Bk */
1.29      kristaps  199:        { macro_scoped_close, MDOC_EXPLICIT }, /* Ek */
1.9       kristaps  200:        { macro_constant, 0 }, /* Bt */
1.10      kristaps  201:        { macro_constant, 0 }, /* Hf */
1.12      kristaps  202:        { macro_obsolete, 0 }, /* Fr */
1.9       kristaps  203:        { macro_constant, 0 }, /* Ud */
1.1       kristaps  204: };
                    205:
                    206: const  char * const *mdoc_macronames = __mdoc_macronames;
                    207: const  char * const *mdoc_argnames = __mdoc_argnames;
                    208: const  struct mdoc_macro * const mdoc_macros = __mdoc_macros;
                    209:
                    210: static struct mdoc_arg  *argdup(size_t, const struct mdoc_arg *);
                    211: static void              argfree(size_t, struct mdoc_arg *);
                    212: static void              argcpy(struct mdoc_arg *,
                    213:                                const struct mdoc_arg *);
                    214:
1.23      kristaps  215: static int               mdoc_node_append(struct mdoc *,
1.1       kristaps  216:                                struct mdoc_node *);
                    217: static void              mdoc_elem_free(struct mdoc_elem *);
                    218: static void              mdoc_text_free(struct mdoc_text *);
                    219:
                    220:
                    221: const struct mdoc_node *
1.37      kristaps  222: mdoc_node(struct mdoc *mdoc)
1.1       kristaps  223: {
                    224:
                    225:        return(mdoc->first);
                    226: }
                    227:
                    228:
1.37      kristaps  229: const struct mdoc_meta *
                    230: mdoc_meta(struct mdoc *mdoc)
                    231: {
                    232:
                    233:        return(&mdoc->meta);
                    234: }
                    235:
                    236:
1.1       kristaps  237: void
1.38      kristaps  238: mdoc_free(struct mdoc *mdoc)
1.34      kristaps  239: {
                    240:
1.38      kristaps  241:        if (mdoc->first)
                    242:                mdoc_node_freelist(mdoc->first);
                    243:        if (mdoc->htab)
                    244:                mdoc_tokhash_free(mdoc->htab);
1.34      kristaps  245:        if (mdoc->meta.title)
                    246:                free(mdoc->meta.title);
                    247:        if (mdoc->meta.os)
                    248:                free(mdoc->meta.os);
                    249:        if (mdoc->meta.name)
                    250:                free(mdoc->meta.name);
                    251:
1.1       kristaps  252:        free(mdoc);
                    253: }
                    254:
                    255:
                    256: struct mdoc *
                    257: mdoc_alloc(void *data, const struct mdoc_cb *cb)
                    258: {
                    259:        struct mdoc     *p;
                    260:
                    261:        p = xcalloc(1, sizeof(struct mdoc));
                    262:
                    263:        p->data = data;
1.33      kristaps  264:        if (cb)
                    265:                (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.1       kristaps  266:
1.25      kristaps  267:        p->last = xcalloc(1, sizeof(struct mdoc_node));
                    268:        p->last->type = MDOC_ROOT;
                    269:        p->first = p->last;
                    270:
                    271:        p->next = MDOC_NEXT_CHILD;
1.4       kristaps  272:        p->htab = mdoc_tokhash_alloc();
1.25      kristaps  273:
1.1       kristaps  274:        return(p);
                    275: }
                    276:
                    277:
                    278: int
1.20      kristaps  279: mdoc_endparse(struct mdoc *mdoc)
                    280: {
                    281:
                    282:        if (MDOC_HALT & mdoc->flags)
                    283:                return(0);
                    284:        if (NULL == mdoc->first)
                    285:                return(1);
                    286:
                    287:        assert(mdoc->last);
                    288:        if ( ! macro_end(mdoc)) {
                    289:                mdoc->flags |= MDOC_HALT;
                    290:                return(0);
                    291:        }
                    292:        return(1);
                    293: }
                    294:
                    295:
                    296: int
                    297: mdoc_parseln(struct mdoc *mdoc, int line, char *buf)
1.1       kristaps  298: {
                    299:        int               c, i;
                    300:        char              tmp[5];
                    301:
1.20      kristaps  302:        if (MDOC_HALT & mdoc->flags)
                    303:                return(0);
                    304:
1.39      kristaps  305:        mdoc->linetok = 0;
                    306:
1.5       kristaps  307:        if ('.' != *buf) {
1.39      kristaps  308:                if ( ! (MDOC_BODYPARSE & mdoc->flags))
                    309:                        return(mdoc_perr(mdoc, line, 0, "text disallowed"));
                    310:                if ( ! mdoc_word_alloc(mdoc, line, 0, buf))
                    311:                        return(0);
                    312:                mdoc->next = MDOC_NEXT_SIBLING;
                    313:                return(1);
1.1       kristaps  314:        }
                    315:
                    316:        if (buf[1] && '\\' == buf[1])
                    317:                if (buf[2] && '\"' == buf[2])
                    318:                        return(1);
                    319:
                    320:        i = 1;
                    321:        while (buf[i] && ! isspace(buf[i]) && i < (int)sizeof(tmp))
                    322:                i++;
                    323:
1.20      kristaps  324:        if (i == (int)sizeof(tmp)) {
                    325:                mdoc->flags |= MDOC_HALT;
1.31      kristaps  326:                return(mdoc_perr(mdoc, line, 1, "unknown macro"));
1.20      kristaps  327:        } else if (i <= 2) {
                    328:                mdoc->flags |= MDOC_HALT;
1.31      kristaps  329:                return(mdoc_perr(mdoc, line, 1, "unknown macro"));
1.20      kristaps  330:        }
1.1       kristaps  331:
                    332:        i--;
                    333:
1.2       kristaps  334:        (void)memcpy(tmp, buf + 1, (size_t)i);
1.1       kristaps  335:        tmp[i++] = 0;
                    336:
1.20      kristaps  337:        if (MDOC_MAX == (c = mdoc_find(mdoc, tmp))) {
                    338:                mdoc->flags |= MDOC_HALT;
1.31      kristaps  339:                return(mdoc_perr(mdoc, line, 1, "unknown macro"));
1.20      kristaps  340:        }
1.1       kristaps  341:
                    342:        while (buf[i] && isspace(buf[i]))
                    343:                i++;
                    344:
1.21      kristaps  345:        if ( ! mdoc_macro(mdoc, c, line, 1, &i, buf)) {
1.20      kristaps  346:                mdoc->flags |= MDOC_HALT;
                    347:                return(0);
                    348:        }
                    349:        return(1);
1.1       kristaps  350: }
                    351:
                    352:
                    353: void
1.31      kristaps  354: mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1       kristaps  355: {
1.31      kristaps  356:        char              buf[256];
1.23      kristaps  357:        va_list           ap;
1.1       kristaps  358:
                    359:        if (NULL == mdoc->cb.mdoc_msg)
                    360:                return;
                    361:
                    362:        va_start(ap, fmt);
1.31      kristaps  363:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1       kristaps  364:        va_end(ap);
1.31      kristaps  365:        (*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf);
1.1       kristaps  366: }
                    367:
                    368:
                    369: int
1.31      kristaps  370: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    371:                const char *fmt, ...)
1.1       kristaps  372: {
1.31      kristaps  373:        char             buf[256];
                    374:        va_list          ap;
1.1       kristaps  375:
                    376:        if (NULL == mdoc->cb.mdoc_err)
                    377:                return(0);
1.31      kristaps  378:
                    379:        va_start(ap, fmt);
                    380:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    381:        va_end(ap);
                    382:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1       kristaps  383: }
                    384:
                    385:
                    386: int
1.31      kristaps  387: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
                    388:                enum mdoc_warn type, const char *fmt, ...)
1.1       kristaps  389: {
1.31      kristaps  390:        char             buf[256];
                    391:        va_list          ap;
1.1       kristaps  392:
                    393:        if (NULL == mdoc->cb.mdoc_warn)
                    394:                return(0);
1.31      kristaps  395:
                    396:        va_start(ap, fmt);
                    397:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    398:        va_end(ap);
                    399:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
1.1       kristaps  400: }
                    401:
                    402:
                    403: int
1.21      kristaps  404: mdoc_macro(struct mdoc *mdoc, int tok,
1.23      kristaps  405:                int ln, int ppos, int *pos, char *buf)
1.1       kristaps  406: {
                    407:
1.31      kristaps  408:        assert(mdoc_macros[tok].fp);
                    409:
1.17      kristaps  410:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.39      kristaps  411:                        ! (MDOC_BODYPARSE & mdoc->flags))
                    412:                return(mdoc_perr(mdoc, ln, ppos, "macro disallowed: not in document body"));
1.23      kristaps  413:        if (1 != ppos && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
1.31      kristaps  414:                return(mdoc_perr(mdoc, ln, ppos, "macro not callable"));
1.23      kristaps  415:        return((*mdoc_macros[tok].fp)(mdoc, tok, ln, ppos, pos, buf));
1.1       kristaps  416: }
                    417:
                    418:
1.23      kristaps  419: static int
                    420: mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  421: {
1.30      kristaps  422:        const char       *nn, *nt, *on, *ot, *act;
1.1       kristaps  423:
1.25      kristaps  424:        assert(mdoc->last);
                    425:        assert(mdoc->first);
                    426:        assert(MDOC_ROOT != p->type);
1.1       kristaps  427:
1.39      kristaps  428:        /* See if we exceed the suggest line-max. */
                    429:
                    430:        switch (p->type) {
                    431:        case (MDOC_TEXT):
                    432:                /* FALLTHROUGH */
                    433:        case (MDOC_ELEM):
                    434:                /* FALLTHROUGH */
                    435:        case (MDOC_BLOCK):
                    436:                mdoc->linetok++;
                    437:                break;
                    438:        default:
                    439:                break;
                    440:        }
                    441:
1.40      kristaps  442:        /* This sort-of works (re-opening of text macros...). */
1.39      kristaps  443:        if (mdoc->linetok > MDOC_LINEARG_SOFTMAX)
                    444:                if ( ! mdoc_nwarn(mdoc, p, WARN_COMPAT,
                    445:                                        "suggested %d tokens per line exceeded (has %d)",
                    446:                                        MDOC_LINEARG_SOFTMAX, mdoc->linetok))
                    447:                        return(0);
                    448:
1.27      kristaps  449:        if (MDOC_TEXT == mdoc->last->type)
                    450:                on = "<text>";
                    451:        else if (MDOC_ROOT == mdoc->last->type)
                    452:                on = "<root>";
                    453:        else
                    454:                on = mdoc_macronames[mdoc->last->tok];
                    455:
1.30      kristaps  456:        if (MDOC_TEXT == p->type)
                    457:                nn = "<text>";
                    458:        else if (MDOC_ROOT == p->type)
                    459:                nn = "<root>";
                    460:        else
                    461:                nn = mdoc_macronames[p->tok];
                    462:
                    463:        ot = mdoc_type2a(mdoc->last->type);
                    464:        nt = mdoc_type2a(p->type);
1.1       kristaps  465:
1.13      kristaps  466:        switch (mdoc->next) {
                    467:        case (MDOC_NEXT_SIBLING):
1.6       kristaps  468:                mdoc->last->next = p;
                    469:                p->prev = mdoc->last;
1.13      kristaps  470:                p->parent = mdoc->last->parent;
1.6       kristaps  471:                act = "sibling";
1.1       kristaps  472:                break;
1.13      kristaps  473:        case (MDOC_NEXT_CHILD):
                    474:                mdoc->last->child = p;
1.1       kristaps  475:                p->parent = mdoc->last;
                    476:                act = "child";
                    477:                break;
                    478:        default:
1.13      kristaps  479:                abort();
                    480:                /* NOTREACHED */
1.1       kristaps  481:        }
                    482:
1.23      kristaps  483:        if ( ! mdoc_valid_pre(mdoc, p))
                    484:                return(0);
1.27      kristaps  485:
                    486:        switch (p->type) {
                    487:        case (MDOC_HEAD):
                    488:                assert(MDOC_BLOCK == p->parent->type);
                    489:                p->parent->data.block.head = p;
                    490:                break;
                    491:        case (MDOC_TAIL):
                    492:                assert(MDOC_BLOCK == p->parent->type);
                    493:                p->parent->data.block.tail = p;
                    494:                break;
                    495:        case (MDOC_BODY):
                    496:                assert(MDOC_BLOCK == p->parent->type);
                    497:                p->parent->data.block.body = p;
                    498:                break;
                    499:        default:
                    500:                break;
                    501:        }
                    502:
1.1       kristaps  503:        mdoc->last = p;
1.30      kristaps  504:        mdoc_msg(mdoc, "parse: %s `%s' %s of %s `%s'",
                    505:                        nt, nn, act, ot, on);
1.23      kristaps  506:        return(1);
1.1       kristaps  507: }
                    508:
                    509:
1.23      kristaps  510: int
1.22      kristaps  511: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.17      kristaps  512: {
                    513:        struct mdoc_node *p;
                    514:
                    515:        assert(mdoc->first);
                    516:        assert(mdoc->last);
                    517:
                    518:        p = xcalloc(1, sizeof(struct mdoc_node));
                    519:
1.22      kristaps  520:        p->line = line;
                    521:        p->pos = pos;
1.17      kristaps  522:        p->type = MDOC_TAIL;
1.27      kristaps  523:        p->tok = tok;
1.17      kristaps  524:
1.23      kristaps  525:        return(mdoc_node_append(mdoc, p));
1.17      kristaps  526: }
                    527:
                    528:
1.23      kristaps  529: int
1.22      kristaps  530: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1       kristaps  531: {
                    532:        struct mdoc_node *p;
                    533:
                    534:        assert(mdoc->first);
                    535:        assert(mdoc->last);
                    536:
                    537:        p = xcalloc(1, sizeof(struct mdoc_node));
1.13      kristaps  538:
1.22      kristaps  539:        p->line = line;
                    540:        p->pos = pos;
1.1       kristaps  541:        p->type = MDOC_HEAD;
1.27      kristaps  542:        p->tok = tok;
1.1       kristaps  543:
1.23      kristaps  544:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  545: }
                    546:
                    547:
1.23      kristaps  548: int
1.22      kristaps  549: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1       kristaps  550: {
                    551:        struct mdoc_node *p;
                    552:
                    553:        assert(mdoc->first);
                    554:        assert(mdoc->last);
                    555:
                    556:        p = xcalloc(1, sizeof(struct mdoc_node));
                    557:
1.22      kristaps  558:        p->line = line;
                    559:        p->pos = pos;
1.1       kristaps  560:        p->type = MDOC_BODY;
1.27      kristaps  561:        p->tok = tok;
1.1       kristaps  562:
1.23      kristaps  563:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  564: }
                    565:
                    566:
1.23      kristaps  567: int
1.25      kristaps  568: mdoc_root_alloc(struct mdoc *mdoc)
                    569: {
                    570:        struct mdoc_node *p;
                    571:
                    572:        p = xcalloc(1, sizeof(struct mdoc_node));
                    573:
                    574:        p->type = MDOC_ROOT;
                    575:
                    576:        return(mdoc_node_append(mdoc, p));
                    577: }
                    578:
                    579:
                    580: int
1.22      kristaps  581: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
                    582:                int tok, size_t argsz, const struct mdoc_arg *args)
1.1       kristaps  583: {
                    584:        struct mdoc_node *p;
                    585:
                    586:        p = xcalloc(1, sizeof(struct mdoc_node));
                    587:
1.22      kristaps  588:        p->pos = pos;
                    589:        p->line = line;
1.1       kristaps  590:        p->type = MDOC_BLOCK;
1.27      kristaps  591:        p->tok = tok;
1.1       kristaps  592:        p->data.block.argc = argsz;
                    593:        p->data.block.argv = argdup(argsz, args);
                    594:
1.23      kristaps  595:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  596: }
                    597:
                    598:
1.23      kristaps  599: int
1.22      kristaps  600: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
                    601:                int tok, size_t argsz, const struct mdoc_arg *args)
1.1       kristaps  602: {
                    603:        struct mdoc_node *p;
                    604:
                    605:        p = xcalloc(1, sizeof(struct mdoc_node));
1.22      kristaps  606:
                    607:        p->line = line;
                    608:        p->pos = pos;
1.1       kristaps  609:        p->type = MDOC_ELEM;
1.27      kristaps  610:        p->tok = tok;
1.1       kristaps  611:        p->data.elem.argc = argsz;
                    612:        p->data.elem.argv = argdup(argsz, args);
                    613:
1.23      kristaps  614:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  615: }
                    616:
                    617:
1.23      kristaps  618: int
1.22      kristaps  619: mdoc_word_alloc(struct mdoc *mdoc,
                    620:                int line, int pos, const char *word)
1.1       kristaps  621: {
                    622:        struct mdoc_node *p;
                    623:
                    624:        p = xcalloc(1, sizeof(struct mdoc_node));
1.22      kristaps  625:        p->line = line;
                    626:        p->pos = pos;
1.1       kristaps  627:        p->type = MDOC_TEXT;
                    628:        p->data.text.string = xstrdup(word);
                    629:
1.23      kristaps  630:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  631: }
                    632:
                    633:
                    634: static void
                    635: argfree(size_t sz, struct mdoc_arg *p)
                    636: {
1.2       kristaps  637:        int              i, j;
1.1       kristaps  638:
                    639:        if (0 == sz)
                    640:                return;
                    641:
                    642:        assert(p);
1.2       kristaps  643:        /* LINTED */
                    644:        for (i = 0; i < (int)sz; i++)
1.1       kristaps  645:                if (p[i].sz > 0) {
                    646:                        assert(p[i].value);
1.2       kristaps  647:                        /* LINTED */
                    648:                        for (j = 0; j < (int)p[i].sz; j++)
1.1       kristaps  649:                                free(p[i].value[j]);
1.19      kristaps  650:                        free(p[i].value);
1.1       kristaps  651:                }
                    652:        free(p);
                    653: }
                    654:
                    655:
                    656: static void
                    657: mdoc_elem_free(struct mdoc_elem *p)
                    658: {
                    659:
                    660:        argfree(p->argc, p->argv);
                    661: }
                    662:
                    663:
                    664: static void
                    665: mdoc_block_free(struct mdoc_block *p)
                    666: {
                    667:
                    668:        argfree(p->argc, p->argv);
                    669: }
                    670:
                    671:
                    672: static void
                    673: mdoc_text_free(struct mdoc_text *p)
                    674: {
                    675:
                    676:        if (p->string)
                    677:                free(p->string);
                    678: }
                    679:
                    680:
                    681: void
                    682: mdoc_node_free(struct mdoc_node *p)
                    683: {
                    684:
                    685:        switch (p->type) {
                    686:        case (MDOC_TEXT):
                    687:                mdoc_text_free(&p->data.text);
                    688:                break;
                    689:        case (MDOC_ELEM):
                    690:                mdoc_elem_free(&p->data.elem);
                    691:                break;
                    692:        case (MDOC_BLOCK):
                    693:                mdoc_block_free(&p->data.block);
                    694:                break;
                    695:        default:
                    696:                break;
                    697:        }
                    698:
                    699:        free(p);
                    700: }
                    701:
                    702:
1.37      kristaps  703: void
1.1       kristaps  704: mdoc_node_freelist(struct mdoc_node *p)
                    705: {
                    706:
                    707:        if (p->child)
                    708:                mdoc_node_freelist(p->child);
                    709:        if (p->next)
                    710:                mdoc_node_freelist(p->next);
                    711:
                    712:        mdoc_node_free(p);
                    713: }
                    714:
                    715:
                    716: int
                    717: mdoc_find(const struct mdoc *mdoc, const char *key)
                    718: {
                    719:
1.4       kristaps  720:        return(mdoc_tokhash_find(mdoc->htab, key));
1.1       kristaps  721: }
                    722:
                    723:
                    724: static void
                    725: argcpy(struct mdoc_arg *dst, const struct mdoc_arg *src)
                    726: {
1.2       kristaps  727:        int              i;
1.1       kristaps  728:
1.26      kristaps  729:        dst->line = src->line;
                    730:        dst->pos = src->pos;
1.1       kristaps  731:        dst->arg = src->arg;
                    732:        if (0 == (dst->sz = src->sz))
                    733:                return;
                    734:        dst->value = xcalloc(dst->sz, sizeof(char *));
1.2       kristaps  735:        for (i = 0; i < (int)dst->sz; i++)
1.1       kristaps  736:                dst->value[i] = xstrdup(src->value[i]);
                    737: }
                    738:
                    739:
                    740: static struct mdoc_arg *
                    741: argdup(size_t argsz, const struct mdoc_arg *args)
                    742: {
                    743:        struct mdoc_arg *pp;
1.2       kristaps  744:        int              i;
1.1       kristaps  745:
                    746:        if (0 == argsz)
                    747:                return(NULL);
                    748:
                    749:        pp = xcalloc((size_t)argsz, sizeof(struct mdoc_arg));
1.2       kristaps  750:        for (i = 0; i < (int)argsz; i++)
1.1       kristaps  751:                argcpy(&pp[i], &args[i]);
                    752:
                    753:        return(pp);
                    754: }
                    755:
1.35      kristaps  756:
1.36      kristaps  757: /* FIXME: deprecate. */
1.35      kristaps  758: char *
                    759: mdoc_node2a(struct mdoc_node *node)
                    760: {
                    761:        static char      buf[64];
                    762:
                    763:        assert(node);
                    764:
                    765:        buf[0] = 0;
                    766:        (void)xstrlcat(buf, mdoc_type2a(node->type), 64);
                    767:        if (MDOC_ROOT == node->type)
                    768:                return(buf);
                    769:        (void)xstrlcat(buf, " `", 64);
                    770:        if (MDOC_TEXT == node->type)
                    771:                (void)xstrlcat(buf, node->data.text.string, 64);
                    772:        else
                    773:                (void)xstrlcat(buf, mdoc_macronames[node->tok], 64);
1.36      kristaps  774:        (void)xstrlcat(buf, "'", 64);
1.35      kristaps  775:
                    776:        return(buf);
                    777: }
                    778:
                    779:

CVSweb