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

Annotation of mandoc/mdoc.c, Revision 1.71

1.71    ! kristaps    1: /* $Id: mdoc.c,v 1.70 2009/03/23 14:22:11 kristaps Exp $ */
1.1       kristaps    2: /*
1.66      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
1.1       kristaps    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:
1.70      kristaps   27: #include "libmdoc.h"
1.1       kristaps   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
1.68      kristaps   32:  * in macro.c, validate.c and action.c.
1.41      kristaps   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",
1.57      kristaps   65:        "Fr",           "Ud",           "Lb",           "Ap",
1.61      kristaps   66:        "Lp",           "Lk",           "Mt",           "Brq",
1.64      kristaps   67:        /* LINTED */
1.65      kristaps   68:        "Bro",          "Brc",          "\%C",          "Es",
1.69      kristaps   69:        /* LINTED */
                     70:        "En",           "Dx",           "\%Q"
1.1       kristaps   71:        };
                     72:
                     73: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                     74:        "split",                "nosplit",              "ragged",
                     75:        "unfilled",             "literal",              "file",
                     76:        "offset",               "bullet",               "dash",
                     77:        "hyphen",               "item",                 "enum",
                     78:        "tag",                  "diag",                 "hang",
                     79:        "ohang",                "inset",                "column",
                     80:        "width",                "compact",              "std",
1.52      kristaps   81:        "filled",               "words",                "emphasis",
1.64      kristaps   82:        "symbolic",             "nested"
1.1       kristaps   83:        };
                     84:
                     85: const  char * const *mdoc_macronames = __mdoc_macronames;
                     86: const  char * const *mdoc_argnames = __mdoc_argnames;
                     87:
1.71    ! kristaps   88: /* FIXME: have this accept line/pos/tok. */
        !            89: /* FIXME: mdoc_alloc1 and mdoc_free1 like in man.c. */
        !            90: static struct mdoc_node *mdoc_node_alloc(const struct mdoc *);
        !            91: static int               mdoc_node_append(struct mdoc *,
        !            92:                                struct mdoc_node *);
        !            93:
        !            94: static int               parsetext(struct mdoc *, int, char *);
        !            95: static int               parsemacro(struct mdoc *, int, char *);
        !            96: static int               macrowarn(struct mdoc *, int, const char *);
        !            97:
1.45      kristaps   98:
1.68      kristaps   99: /*
                    100:  * Get the first (root) node of the parse tree.
                    101:  */
1.1       kristaps  102: const struct mdoc_node *
1.71    ! kristaps  103: mdoc_node(const struct mdoc *m)
1.1       kristaps  104: {
                    105:
1.71    ! kristaps  106:        return(MDOC_HALT & m->flags ? NULL : m->first);
1.1       kristaps  107: }
                    108:
                    109:
1.37      kristaps  110: const struct mdoc_meta *
1.71    ! kristaps  111: mdoc_meta(const struct mdoc *m)
1.37      kristaps  112: {
                    113:
1.71    ! kristaps  114:        return(MDOC_HALT & m->flags ? NULL : &m->meta);
1.37      kristaps  115: }
                    116:
                    117:
1.68      kristaps  118: /*
1.70      kristaps  119:  * Free up all resources contributed by a parse:  the node tree,
                    120:  * meta-data and so on.  Then reallocate the root node for another
                    121:  * parse.
1.68      kristaps  122:  */
1.1       kristaps  123: void
1.67      kristaps  124: mdoc_reset(struct mdoc *mdoc)
                    125: {
                    126:
                    127:        if (mdoc->first)
                    128:                mdoc_node_freelist(mdoc->first);
                    129:        if (mdoc->meta.title)
                    130:                free(mdoc->meta.title);
                    131:        if (mdoc->meta.os)
                    132:                free(mdoc->meta.os);
                    133:        if (mdoc->meta.name)
                    134:                free(mdoc->meta.name);
                    135:        if (mdoc->meta.arch)
                    136:                free(mdoc->meta.arch);
                    137:        if (mdoc->meta.vol)
                    138:                free(mdoc->meta.vol);
                    139:
                    140:        bzero(&mdoc->meta, sizeof(struct mdoc_meta));
                    141:        mdoc->flags = 0;
                    142:        mdoc->lastnamed = mdoc->lastsec = 0;
1.70      kristaps  143:        mdoc->last = calloc(1, sizeof(struct mdoc_node));
                    144:        if (NULL == mdoc->last)
                    145:                err(1, "calloc");
                    146:        mdoc->first = mdoc->last;
1.67      kristaps  147:        mdoc->last->type = MDOC_ROOT;
                    148:        mdoc->next = MDOC_NEXT_CHILD;
                    149: }
                    150:
                    151:
1.68      kristaps  152: /*
                    153:  * Completely free up all resources.
                    154:  */
1.67      kristaps  155: void
1.38      kristaps  156: mdoc_free(struct mdoc *mdoc)
1.34      kristaps  157: {
                    158:
1.38      kristaps  159:        if (mdoc->first)
                    160:                mdoc_node_freelist(mdoc->first);
1.34      kristaps  161:        if (mdoc->meta.title)
                    162:                free(mdoc->meta.title);
                    163:        if (mdoc->meta.os)
                    164:                free(mdoc->meta.os);
                    165:        if (mdoc->meta.name)
                    166:                free(mdoc->meta.name);
1.52      kristaps  167:        if (mdoc->meta.arch)
                    168:                free(mdoc->meta.arch);
                    169:        if (mdoc->meta.vol)
                    170:                free(mdoc->meta.vol);
1.34      kristaps  171:
1.68      kristaps  172:        if (mdoc->htab)
                    173:                mdoc_tokhash_free(mdoc->htab);
                    174:
1.1       kristaps  175:        free(mdoc);
                    176: }
                    177:
                    178:
                    179: struct mdoc *
1.55      kristaps  180: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1       kristaps  181: {
                    182:        struct mdoc     *p;
                    183:
1.70      kristaps  184:        if (NULL == (p = calloc(1, sizeof(struct mdoc))))
                    185:                err(1, "calloc");
1.1       kristaps  186:
                    187:        p->data = data;
1.33      kristaps  188:        if (cb)
                    189:                (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.1       kristaps  190:
1.70      kristaps  191:        if (NULL == (p->first = calloc(1, sizeof(struct mdoc_node))))
                    192:                err(1, "calloc");
                    193:        p->last = p->first;
1.25      kristaps  194:        p->last->type = MDOC_ROOT;
1.55      kristaps  195:        p->pflags = pflags;
1.25      kristaps  196:        p->next = MDOC_NEXT_CHILD;
1.4       kristaps  197:        p->htab = mdoc_tokhash_alloc();
1.1       kristaps  198:        return(p);
                    199: }
                    200:
                    201:
1.68      kristaps  202: /*
                    203:  * Climb back up the parse tree, validating open scopes.  Mostly calls
                    204:  * through to macro_end in macro.c.
                    205:  */
1.1       kristaps  206: int
1.20      kristaps  207: mdoc_endparse(struct mdoc *mdoc)
                    208: {
                    209:
                    210:        if (MDOC_HALT & mdoc->flags)
                    211:                return(0);
                    212:        if (NULL == mdoc->first)
                    213:                return(1);
                    214:
                    215:        assert(mdoc->last);
                    216:        if ( ! macro_end(mdoc)) {
                    217:                mdoc->flags |= MDOC_HALT;
                    218:                return(0);
                    219:        }
                    220:        return(1);
                    221: }
                    222:
                    223:
1.50      kristaps  224: /*
1.53      kristaps  225:  * Main parse routine.  Parses a single line -- really just hands off to
                    226:  * the macro or text parser.
1.50      kristaps  227:  */
1.20      kristaps  228: int
1.53      kristaps  229: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1       kristaps  230: {
                    231:
1.53      kristaps  232:        /* If in error-mode, then we parse no more. */
1.50      kristaps  233:
1.53      kristaps  234:        if (MDOC_HALT & m->flags)
1.20      kristaps  235:                return(0);
1.50      kristaps  236:
1.53      kristaps  237:        return('.' == *buf ? parsemacro(m, ln, buf) :
                    238:                        parsetext(m, ln, buf));
1.1       kristaps  239: }
                    240:
                    241:
                    242: void
1.31      kristaps  243: mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1       kristaps  244: {
1.31      kristaps  245:        char              buf[256];
1.23      kristaps  246:        va_list           ap;
1.1       kristaps  247:
                    248:        if (NULL == mdoc->cb.mdoc_msg)
                    249:                return;
                    250:
                    251:        va_start(ap, fmt);
1.31      kristaps  252:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1       kristaps  253:        va_end(ap);
1.31      kristaps  254:        (*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf);
1.1       kristaps  255: }
                    256:
                    257:
                    258: int
1.31      kristaps  259: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    260:                const char *fmt, ...)
1.1       kristaps  261: {
1.31      kristaps  262:        char             buf[256];
                    263:        va_list          ap;
1.1       kristaps  264:
                    265:        if (NULL == mdoc->cb.mdoc_err)
                    266:                return(0);
1.31      kristaps  267:
                    268:        va_start(ap, fmt);
                    269:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    270:        va_end(ap);
                    271:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1       kristaps  272: }
                    273:
                    274:
                    275: int
1.31      kristaps  276: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
                    277:                enum mdoc_warn type, const char *fmt, ...)
1.1       kristaps  278: {
1.31      kristaps  279:        char             buf[256];
                    280:        va_list          ap;
1.1       kristaps  281:
                    282:        if (NULL == mdoc->cb.mdoc_warn)
                    283:                return(0);
1.31      kristaps  284:
                    285:        va_start(ap, fmt);
                    286:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    287:        va_end(ap);
                    288:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
1.1       kristaps  289: }
                    290:
                    291:
                    292: int
1.53      kristaps  293: mdoc_macro(struct mdoc *m, int tok,
                    294:                int ln, int pp, int *pos, char *buf)
1.1       kristaps  295: {
                    296:
1.53      kristaps  297:        /* FIXME - these should happen during validation. */
1.31      kristaps  298:
1.44      kristaps  299:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.53      kristaps  300:                        SEC_PROLOGUE != m->lastnamed)
                    301:                return(mdoc_perr(m, ln, pp,
                    302:                                "disallowed in document body"));
                    303:
1.44      kristaps  304:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.53      kristaps  305:                        SEC_PROLOGUE == m->lastnamed)
                    306:                return(mdoc_perr(m, ln, pp,
                    307:                                "disallowed in prologue"));
                    308:
                    309:        if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
1.64      kristaps  310:                return(mdoc_perr(m, ln, pp, "%s not callable",
                    311:                                        mdoc_macronames[tok]));
1.53      kristaps  312:
                    313:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.1       kristaps  314: }
                    315:
                    316:
1.23      kristaps  317: static int
                    318: mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  319: {
                    320:
1.25      kristaps  321:        assert(mdoc->last);
                    322:        assert(mdoc->first);
                    323:        assert(MDOC_ROOT != p->type);
1.1       kristaps  324:
1.13      kristaps  325:        switch (mdoc->next) {
                    326:        case (MDOC_NEXT_SIBLING):
1.6       kristaps  327:                mdoc->last->next = p;
                    328:                p->prev = mdoc->last;
1.13      kristaps  329:                p->parent = mdoc->last->parent;
1.1       kristaps  330:                break;
1.13      kristaps  331:        case (MDOC_NEXT_CHILD):
                    332:                mdoc->last->child = p;
1.1       kristaps  333:                p->parent = mdoc->last;
                    334:                break;
                    335:        default:
1.13      kristaps  336:                abort();
                    337:                /* NOTREACHED */
1.1       kristaps  338:        }
                    339:
1.23      kristaps  340:        if ( ! mdoc_valid_pre(mdoc, p))
                    341:                return(0);
1.68      kristaps  342:        if ( ! mdoc_action_pre(mdoc, p))
                    343:                return(0);
1.27      kristaps  344:
                    345:        switch (p->type) {
                    346:        case (MDOC_HEAD):
                    347:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  348:                p->parent->head = p;
1.27      kristaps  349:                break;
                    350:        case (MDOC_TAIL):
                    351:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  352:                p->parent->tail = p;
1.27      kristaps  353:                break;
                    354:        case (MDOC_BODY):
                    355:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  356:                p->parent->body = p;
1.27      kristaps  357:                break;
                    358:        default:
                    359:                break;
                    360:        }
                    361:
1.1       kristaps  362:        mdoc->last = p;
1.71    ! kristaps  363:
        !           364:        switch (p->type) {
        !           365:        case (MDOC_TEXT):
        !           366:                if ( ! mdoc_valid_post(mdoc))
        !           367:                        return(0);
        !           368:                if ( ! mdoc_action_post(mdoc))
        !           369:                        return(0);
        !           370:                break;
        !           371:        default:
        !           372:                break;
        !           373:        }
        !           374:
1.23      kristaps  375:        return(1);
1.1       kristaps  376: }
                    377:
                    378:
1.45      kristaps  379: static struct mdoc_node *
1.46      kristaps  380: mdoc_node_alloc(const struct mdoc *mdoc)
1.45      kristaps  381: {
1.46      kristaps  382:        struct mdoc_node *p;
                    383:
1.70      kristaps  384:        if (NULL == (p = calloc(1, sizeof(struct mdoc_node))))
                    385:                err(1, "calloc");
1.46      kristaps  386:        p->sec = mdoc->lastsec;
1.45      kristaps  387:
1.46      kristaps  388:        return(p);
1.45      kristaps  389: }
                    390:
                    391:
1.23      kristaps  392: int
1.22      kristaps  393: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.17      kristaps  394: {
                    395:        struct mdoc_node *p;
                    396:
                    397:        assert(mdoc->first);
                    398:        assert(mdoc->last);
                    399:
1.46      kristaps  400:        p = mdoc_node_alloc(mdoc);
1.17      kristaps  401:
1.22      kristaps  402:        p->line = line;
                    403:        p->pos = pos;
1.17      kristaps  404:        p->type = MDOC_TAIL;
1.27      kristaps  405:        p->tok = tok;
1.17      kristaps  406:
1.23      kristaps  407:        return(mdoc_node_append(mdoc, p));
1.17      kristaps  408: }
                    409:
                    410:
1.23      kristaps  411: int
1.22      kristaps  412: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1       kristaps  413: {
                    414:        struct mdoc_node *p;
                    415:
                    416:        assert(mdoc->first);
                    417:        assert(mdoc->last);
                    418:
1.46      kristaps  419:        p = mdoc_node_alloc(mdoc);
1.13      kristaps  420:
1.22      kristaps  421:        p->line = line;
                    422:        p->pos = pos;
1.1       kristaps  423:        p->type = MDOC_HEAD;
1.27      kristaps  424:        p->tok = tok;
1.1       kristaps  425:
1.23      kristaps  426:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  427: }
                    428:
                    429:
1.23      kristaps  430: int
1.22      kristaps  431: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1       kristaps  432: {
                    433:        struct mdoc_node *p;
                    434:
                    435:        assert(mdoc->first);
                    436:        assert(mdoc->last);
                    437:
1.46      kristaps  438:        p = mdoc_node_alloc(mdoc);
1.1       kristaps  439:
1.22      kristaps  440:        p->line = line;
                    441:        p->pos = pos;
1.1       kristaps  442:        p->type = MDOC_BODY;
1.27      kristaps  443:        p->tok = tok;
1.1       kristaps  444:
1.23      kristaps  445:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  446: }
                    447:
                    448:
1.23      kristaps  449: int
1.22      kristaps  450: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
1.53      kristaps  451:                int tok, struct mdoc_arg *args)
1.1       kristaps  452: {
                    453:        struct mdoc_node *p;
                    454:
1.46      kristaps  455:        p = mdoc_node_alloc(mdoc);
1.1       kristaps  456:
1.22      kristaps  457:        p->pos = pos;
                    458:        p->line = line;
1.1       kristaps  459:        p->type = MDOC_BLOCK;
1.27      kristaps  460:        p->tok = tok;
1.53      kristaps  461:        p->args = args;
                    462:
                    463:        if (args)
                    464:                (args->refcnt)++;
1.1       kristaps  465:
1.23      kristaps  466:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  467: }
                    468:
                    469:
1.23      kristaps  470: int
1.22      kristaps  471: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
1.53      kristaps  472:                int tok, struct mdoc_arg *args)
1.1       kristaps  473: {
                    474:        struct mdoc_node *p;
                    475:
1.46      kristaps  476:        p = mdoc_node_alloc(mdoc);
1.22      kristaps  477:
                    478:        p->line = line;
                    479:        p->pos = pos;
1.1       kristaps  480:        p->type = MDOC_ELEM;
1.27      kristaps  481:        p->tok = tok;
1.53      kristaps  482:        p->args = args;
                    483:
                    484:        if (args)
                    485:                (args->refcnt)++;
1.1       kristaps  486:
1.23      kristaps  487:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  488: }
                    489:
                    490:
1.23      kristaps  491: int
1.22      kristaps  492: mdoc_word_alloc(struct mdoc *mdoc,
                    493:                int line, int pos, const char *word)
1.1       kristaps  494: {
                    495:        struct mdoc_node *p;
                    496:
1.46      kristaps  497:        p = mdoc_node_alloc(mdoc);
1.45      kristaps  498:
1.22      kristaps  499:        p->line = line;
                    500:        p->pos = pos;
1.1       kristaps  501:        p->type = MDOC_TEXT;
1.70      kristaps  502:        if (NULL == (p->string = strdup(word)))
                    503:                err(1, "strdup");
1.1       kristaps  504:
1.23      kristaps  505:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  506: }
                    507:
                    508:
1.53      kristaps  509: void
                    510: mdoc_node_free(struct mdoc_node *p)
1.1       kristaps  511: {
                    512:
1.53      kristaps  513:        if (p->string)
                    514:                free(p->string);
                    515:        if (p->args)
                    516:                mdoc_argv_free(p->args);
1.1       kristaps  517:        free(p);
                    518: }
                    519:
                    520:
1.53      kristaps  521: void
                    522: mdoc_node_freelist(struct mdoc_node *p)
1.1       kristaps  523: {
                    524:
1.53      kristaps  525:        if (p->child)
                    526:                mdoc_node_freelist(p->child);
                    527:        if (p->next)
                    528:                mdoc_node_freelist(p->next);
1.1       kristaps  529:
1.53      kristaps  530:        mdoc_node_free(p);
1.1       kristaps  531: }
                    532:
                    533:
1.53      kristaps  534: /*
                    535:  * Parse free-form text, that is, a line that does not begin with the
                    536:  * control character.
                    537:  */
                    538: static int
1.68      kristaps  539: parsetext(struct mdoc *m, int line, char *buf)
1.1       kristaps  540: {
                    541:
1.68      kristaps  542:        if (SEC_PROLOGUE == m->lastnamed)
                    543:                return(mdoc_perr(m, line, 0,
1.53      kristaps  544:                        "text disallowed in prologue"));
1.1       kristaps  545:
1.68      kristaps  546:        if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
                    547:                return(mdoc_perr(m, line, 0,
                    548:                        "blank lines only in literal context"));
                    549:
                    550:        if ( ! mdoc_word_alloc(m, line, 0, buf))
1.53      kristaps  551:                return(0);
1.1       kristaps  552:
1.68      kristaps  553:        m->next = MDOC_NEXT_SIBLING;
1.53      kristaps  554:        return(1);
1.1       kristaps  555: }
                    556:
                    557:
1.58      kristaps  558: static int
                    559: macrowarn(struct mdoc *m, int ln, const char *buf)
                    560: {
                    561:        if ( ! (MDOC_IGN_MACRO & m->pflags))
                    562:                return(mdoc_perr(m, ln, 1, "unknown macro: %s%s",
1.59      kristaps  563:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  564:        return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
                    565:                                "unknown macro: %s%s",
1.59      kristaps  566:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  567: }
                    568:
                    569:
                    570:
1.53      kristaps  571: /*
                    572:  * Parse a macro line, that is, a line beginning with the control
                    573:  * character.
                    574:  */
                    575: int
                    576: parsemacro(struct mdoc *m, int ln, char *buf)
1.1       kristaps  577: {
1.53      kristaps  578:        int               i, c;
                    579:        char              mac[5];
1.1       kristaps  580:
1.63      kristaps  581:        /* Comments and empties are quickly ignored. */
                    582:
                    583:        if (0 == buf[1])
                    584:                return(1);
                    585:
1.65      kristaps  586:        if (' ' == buf[1]) {
1.63      kristaps  587:                i = 2;
1.65      kristaps  588:                while (buf[i] && ' ' == buf[i])
1.63      kristaps  589:                        i++;
                    590:                if (0 == buf[i])
                    591:                        return(1);
                    592:                return(mdoc_perr(m, ln, 1, "invalid syntax"));
                    593:        }
1.1       kristaps  594:
1.53      kristaps  595:        if (buf[1] && '\\' == buf[1])
                    596:                if (buf[2] && '\"' == buf[2])
                    597:                        return(1);
1.1       kristaps  598:
1.53      kristaps  599:        /* Copy the first word into a nil-terminated buffer. */
1.1       kristaps  600:
1.53      kristaps  601:        for (i = 1; i < 5; i++) {
                    602:                if (0 == (mac[i - 1] = buf[i]))
                    603:                        break;
1.65      kristaps  604:                else if (' ' == buf[i])
1.53      kristaps  605:                        break;
                    606:        }
1.1       kristaps  607:
1.53      kristaps  608:        mac[i - 1] = 0;
1.1       kristaps  609:
1.53      kristaps  610:        if (i == 5 || i <= 2) {
1.58      kristaps  611:                if ( ! macrowarn(m, ln, mac))
                    612:                        goto err;
                    613:                return(1);
1.53      kristaps  614:        }
                    615:
                    616:        if (MDOC_MAX == (c = mdoc_tokhash_find(m->htab, mac))) {
1.58      kristaps  617:                if ( ! macrowarn(m, ln, mac))
                    618:                        goto err;
                    619:                return(1);
1.53      kristaps  620:        }
1.1       kristaps  621:
1.53      kristaps  622:        /* The macro is sane.  Jump to the next word. */
1.1       kristaps  623:
1.65      kristaps  624:        while (buf[i] && ' ' == buf[i])
1.53      kristaps  625:                i++;
1.1       kristaps  626:
1.53      kristaps  627:        /* Begin recursive parse sequence. */
1.1       kristaps  628:
1.53      kristaps  629:        if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
                    630:                goto err;
1.1       kristaps  631:
1.53      kristaps  632:        return(1);
1.1       kristaps  633:
1.53      kristaps  634: err:   /* Error out. */
1.1       kristaps  635:
1.53      kristaps  636:        m->flags |= MDOC_HALT;
                    637:        return(0);
1.1       kristaps  638: }

CVSweb