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

Annotation of mandoc/mdoc.c, Revision 1.72

1.72    ! kristaps    1: /* $Id: mdoc.c,v 1.71 2009/03/23 15:20:51 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.72    ! kristaps  207: mdoc_endparse(struct mdoc *m)
1.20      kristaps  208: {
                    209:
1.72    ! kristaps  210:        if (MDOC_HALT & m->flags)
1.20      kristaps  211:                return(0);
1.72    ! kristaps  212:        else if (mdoc_macroend(m))
1.20      kristaps  213:                return(1);
1.72    ! kristaps  214:        m->flags |= MDOC_HALT;
        !           215:        return(0);
1.20      kristaps  216: }
                    217:
                    218:
1.50      kristaps  219: /*
1.53      kristaps  220:  * Main parse routine.  Parses a single line -- really just hands off to
                    221:  * the macro or text parser.
1.50      kristaps  222:  */
1.20      kristaps  223: int
1.53      kristaps  224: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1       kristaps  225: {
                    226:
1.53      kristaps  227:        /* If in error-mode, then we parse no more. */
1.50      kristaps  228:
1.53      kristaps  229:        if (MDOC_HALT & m->flags)
1.20      kristaps  230:                return(0);
1.50      kristaps  231:
1.53      kristaps  232:        return('.' == *buf ? parsemacro(m, ln, buf) :
                    233:                        parsetext(m, ln, buf));
1.1       kristaps  234: }
                    235:
                    236:
                    237: void
1.31      kristaps  238: mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1       kristaps  239: {
1.31      kristaps  240:        char              buf[256];
1.23      kristaps  241:        va_list           ap;
1.1       kristaps  242:
                    243:        if (NULL == mdoc->cb.mdoc_msg)
                    244:                return;
                    245:
                    246:        va_start(ap, fmt);
1.31      kristaps  247:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1       kristaps  248:        va_end(ap);
1.31      kristaps  249:        (*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf);
1.1       kristaps  250: }
                    251:
                    252:
                    253: int
1.31      kristaps  254: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    255:                const char *fmt, ...)
1.1       kristaps  256: {
1.31      kristaps  257:        char             buf[256];
                    258:        va_list          ap;
1.1       kristaps  259:
                    260:        if (NULL == mdoc->cb.mdoc_err)
                    261:                return(0);
1.31      kristaps  262:
                    263:        va_start(ap, fmt);
                    264:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    265:        va_end(ap);
                    266:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1       kristaps  267: }
                    268:
                    269:
                    270: int
1.31      kristaps  271: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
                    272:                enum mdoc_warn type, const char *fmt, ...)
1.1       kristaps  273: {
1.31      kristaps  274:        char             buf[256];
                    275:        va_list          ap;
1.1       kristaps  276:
                    277:        if (NULL == mdoc->cb.mdoc_warn)
                    278:                return(0);
1.31      kristaps  279:
                    280:        va_start(ap, fmt);
                    281:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    282:        va_end(ap);
                    283:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
1.1       kristaps  284: }
                    285:
                    286:
                    287: int
1.53      kristaps  288: mdoc_macro(struct mdoc *m, int tok,
                    289:                int ln, int pp, int *pos, char *buf)
1.1       kristaps  290: {
                    291:
1.53      kristaps  292:        /* FIXME - these should happen during validation. */
1.31      kristaps  293:
1.44      kristaps  294:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.53      kristaps  295:                        SEC_PROLOGUE != m->lastnamed)
                    296:                return(mdoc_perr(m, ln, pp,
                    297:                                "disallowed in document body"));
                    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 prologue"));
                    303:
                    304:        if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
1.64      kristaps  305:                return(mdoc_perr(m, ln, pp, "%s not callable",
                    306:                                        mdoc_macronames[tok]));
1.53      kristaps  307:
                    308:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.1       kristaps  309: }
                    310:
                    311:
1.23      kristaps  312: static int
                    313: mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  314: {
                    315:
1.25      kristaps  316:        assert(mdoc->last);
                    317:        assert(mdoc->first);
                    318:        assert(MDOC_ROOT != p->type);
1.1       kristaps  319:
1.13      kristaps  320:        switch (mdoc->next) {
                    321:        case (MDOC_NEXT_SIBLING):
1.6       kristaps  322:                mdoc->last->next = p;
                    323:                p->prev = mdoc->last;
1.13      kristaps  324:                p->parent = mdoc->last->parent;
1.1       kristaps  325:                break;
1.13      kristaps  326:        case (MDOC_NEXT_CHILD):
                    327:                mdoc->last->child = p;
1.1       kristaps  328:                p->parent = mdoc->last;
                    329:                break;
                    330:        default:
1.13      kristaps  331:                abort();
                    332:                /* NOTREACHED */
1.1       kristaps  333:        }
                    334:
1.23      kristaps  335:        if ( ! mdoc_valid_pre(mdoc, p))
                    336:                return(0);
1.68      kristaps  337:        if ( ! mdoc_action_pre(mdoc, p))
                    338:                return(0);
1.27      kristaps  339:
                    340:        switch (p->type) {
                    341:        case (MDOC_HEAD):
                    342:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  343:                p->parent->head = p;
1.27      kristaps  344:                break;
                    345:        case (MDOC_TAIL):
                    346:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  347:                p->parent->tail = p;
1.27      kristaps  348:                break;
                    349:        case (MDOC_BODY):
                    350:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  351:                p->parent->body = p;
1.27      kristaps  352:                break;
                    353:        default:
                    354:                break;
                    355:        }
                    356:
1.1       kristaps  357:        mdoc->last = p;
1.71      kristaps  358:
                    359:        switch (p->type) {
                    360:        case (MDOC_TEXT):
                    361:                if ( ! mdoc_valid_post(mdoc))
                    362:                        return(0);
                    363:                if ( ! mdoc_action_post(mdoc))
                    364:                        return(0);
                    365:                break;
                    366:        default:
                    367:                break;
                    368:        }
                    369:
1.23      kristaps  370:        return(1);
1.1       kristaps  371: }
                    372:
                    373:
1.45      kristaps  374: static struct mdoc_node *
1.46      kristaps  375: mdoc_node_alloc(const struct mdoc *mdoc)
1.45      kristaps  376: {
1.46      kristaps  377:        struct mdoc_node *p;
                    378:
1.70      kristaps  379:        if (NULL == (p = calloc(1, sizeof(struct mdoc_node))))
                    380:                err(1, "calloc");
1.46      kristaps  381:        p->sec = mdoc->lastsec;
1.45      kristaps  382:
1.46      kristaps  383:        return(p);
1.45      kristaps  384: }
                    385:
                    386:
1.23      kristaps  387: int
1.22      kristaps  388: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.17      kristaps  389: {
                    390:        struct mdoc_node *p;
                    391:
                    392:        assert(mdoc->first);
                    393:        assert(mdoc->last);
                    394:
1.46      kristaps  395:        p = mdoc_node_alloc(mdoc);
1.17      kristaps  396:
1.22      kristaps  397:        p->line = line;
                    398:        p->pos = pos;
1.17      kristaps  399:        p->type = MDOC_TAIL;
1.27      kristaps  400:        p->tok = tok;
1.17      kristaps  401:
1.23      kristaps  402:        return(mdoc_node_append(mdoc, p));
1.17      kristaps  403: }
                    404:
                    405:
1.23      kristaps  406: int
1.22      kristaps  407: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1       kristaps  408: {
                    409:        struct mdoc_node *p;
                    410:
                    411:        assert(mdoc->first);
                    412:        assert(mdoc->last);
                    413:
1.46      kristaps  414:        p = mdoc_node_alloc(mdoc);
1.13      kristaps  415:
1.22      kristaps  416:        p->line = line;
                    417:        p->pos = pos;
1.1       kristaps  418:        p->type = MDOC_HEAD;
1.27      kristaps  419:        p->tok = tok;
1.1       kristaps  420:
1.23      kristaps  421:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  422: }
                    423:
                    424:
1.23      kristaps  425: int
1.22      kristaps  426: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1       kristaps  427: {
                    428:        struct mdoc_node *p;
                    429:
                    430:        assert(mdoc->first);
                    431:        assert(mdoc->last);
                    432:
1.46      kristaps  433:        p = mdoc_node_alloc(mdoc);
1.1       kristaps  434:
1.22      kristaps  435:        p->line = line;
                    436:        p->pos = pos;
1.1       kristaps  437:        p->type = MDOC_BODY;
1.27      kristaps  438:        p->tok = tok;
1.1       kristaps  439:
1.23      kristaps  440:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  441: }
                    442:
                    443:
1.23      kristaps  444: int
1.22      kristaps  445: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
1.53      kristaps  446:                int tok, struct mdoc_arg *args)
1.1       kristaps  447: {
                    448:        struct mdoc_node *p;
                    449:
1.46      kristaps  450:        p = mdoc_node_alloc(mdoc);
1.1       kristaps  451:
1.22      kristaps  452:        p->pos = pos;
                    453:        p->line = line;
1.1       kristaps  454:        p->type = MDOC_BLOCK;
1.27      kristaps  455:        p->tok = tok;
1.53      kristaps  456:        p->args = args;
                    457:
                    458:        if (args)
                    459:                (args->refcnt)++;
1.1       kristaps  460:
1.23      kristaps  461:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  462: }
                    463:
                    464:
1.23      kristaps  465: int
1.22      kristaps  466: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
1.53      kristaps  467:                int tok, struct mdoc_arg *args)
1.1       kristaps  468: {
                    469:        struct mdoc_node *p;
                    470:
1.46      kristaps  471:        p = mdoc_node_alloc(mdoc);
1.22      kristaps  472:
                    473:        p->line = line;
                    474:        p->pos = pos;
1.1       kristaps  475:        p->type = MDOC_ELEM;
1.27      kristaps  476:        p->tok = tok;
1.53      kristaps  477:        p->args = args;
                    478:
                    479:        if (args)
                    480:                (args->refcnt)++;
1.1       kristaps  481:
1.23      kristaps  482:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  483: }
                    484:
                    485:
1.23      kristaps  486: int
1.22      kristaps  487: mdoc_word_alloc(struct mdoc *mdoc,
                    488:                int line, int pos, const char *word)
1.1       kristaps  489: {
                    490:        struct mdoc_node *p;
                    491:
1.46      kristaps  492:        p = mdoc_node_alloc(mdoc);
1.45      kristaps  493:
1.22      kristaps  494:        p->line = line;
                    495:        p->pos = pos;
1.1       kristaps  496:        p->type = MDOC_TEXT;
1.70      kristaps  497:        if (NULL == (p->string = strdup(word)))
                    498:                err(1, "strdup");
1.1       kristaps  499:
1.23      kristaps  500:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  501: }
                    502:
                    503:
1.53      kristaps  504: void
                    505: mdoc_node_free(struct mdoc_node *p)
1.1       kristaps  506: {
                    507:
1.53      kristaps  508:        if (p->string)
                    509:                free(p->string);
                    510:        if (p->args)
                    511:                mdoc_argv_free(p->args);
1.1       kristaps  512:        free(p);
                    513: }
                    514:
                    515:
1.53      kristaps  516: void
                    517: mdoc_node_freelist(struct mdoc_node *p)
1.1       kristaps  518: {
                    519:
1.53      kristaps  520:        if (p->child)
                    521:                mdoc_node_freelist(p->child);
                    522:        if (p->next)
                    523:                mdoc_node_freelist(p->next);
1.1       kristaps  524:
1.53      kristaps  525:        mdoc_node_free(p);
1.1       kristaps  526: }
                    527:
                    528:
1.53      kristaps  529: /*
                    530:  * Parse free-form text, that is, a line that does not begin with the
                    531:  * control character.
                    532:  */
                    533: static int
1.68      kristaps  534: parsetext(struct mdoc *m, int line, char *buf)
1.1       kristaps  535: {
                    536:
1.68      kristaps  537:        if (SEC_PROLOGUE == m->lastnamed)
                    538:                return(mdoc_perr(m, line, 0,
1.53      kristaps  539:                        "text disallowed in prologue"));
1.1       kristaps  540:
1.68      kristaps  541:        if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
                    542:                return(mdoc_perr(m, line, 0,
                    543:                        "blank lines only in literal context"));
                    544:
                    545:        if ( ! mdoc_word_alloc(m, line, 0, buf))
1.53      kristaps  546:                return(0);
1.1       kristaps  547:
1.68      kristaps  548:        m->next = MDOC_NEXT_SIBLING;
1.53      kristaps  549:        return(1);
1.1       kristaps  550: }
                    551:
                    552:
1.58      kristaps  553: static int
                    554: macrowarn(struct mdoc *m, int ln, const char *buf)
                    555: {
                    556:        if ( ! (MDOC_IGN_MACRO & m->pflags))
                    557:                return(mdoc_perr(m, ln, 1, "unknown macro: %s%s",
1.59      kristaps  558:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  559:        return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
                    560:                                "unknown macro: %s%s",
1.59      kristaps  561:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  562: }
                    563:
                    564:
                    565:
1.53      kristaps  566: /*
                    567:  * Parse a macro line, that is, a line beginning with the control
                    568:  * character.
                    569:  */
                    570: int
                    571: parsemacro(struct mdoc *m, int ln, char *buf)
1.1       kristaps  572: {
1.53      kristaps  573:        int               i, c;
                    574:        char              mac[5];
1.1       kristaps  575:
1.63      kristaps  576:        /* Comments and empties are quickly ignored. */
                    577:
                    578:        if (0 == buf[1])
                    579:                return(1);
                    580:
1.65      kristaps  581:        if (' ' == buf[1]) {
1.63      kristaps  582:                i = 2;
1.65      kristaps  583:                while (buf[i] && ' ' == buf[i])
1.63      kristaps  584:                        i++;
                    585:                if (0 == buf[i])
                    586:                        return(1);
                    587:                return(mdoc_perr(m, ln, 1, "invalid syntax"));
                    588:        }
1.1       kristaps  589:
1.53      kristaps  590:        if (buf[1] && '\\' == buf[1])
                    591:                if (buf[2] && '\"' == buf[2])
                    592:                        return(1);
1.1       kristaps  593:
1.53      kristaps  594:        /* Copy the first word into a nil-terminated buffer. */
1.1       kristaps  595:
1.53      kristaps  596:        for (i = 1; i < 5; i++) {
                    597:                if (0 == (mac[i - 1] = buf[i]))
                    598:                        break;
1.65      kristaps  599:                else if (' ' == buf[i])
1.53      kristaps  600:                        break;
                    601:        }
1.1       kristaps  602:
1.53      kristaps  603:        mac[i - 1] = 0;
1.1       kristaps  604:
1.53      kristaps  605:        if (i == 5 || i <= 2) {
1.58      kristaps  606:                if ( ! macrowarn(m, ln, mac))
                    607:                        goto err;
                    608:                return(1);
1.53      kristaps  609:        }
                    610:
                    611:        if (MDOC_MAX == (c = mdoc_tokhash_find(m->htab, mac))) {
1.58      kristaps  612:                if ( ! macrowarn(m, ln, mac))
                    613:                        goto err;
                    614:                return(1);
1.53      kristaps  615:        }
1.1       kristaps  616:
1.53      kristaps  617:        /* The macro is sane.  Jump to the next word. */
1.1       kristaps  618:
1.65      kristaps  619:        while (buf[i] && ' ' == buf[i])
1.53      kristaps  620:                i++;
1.1       kristaps  621:
1.53      kristaps  622:        /* Begin recursive parse sequence. */
1.1       kristaps  623:
1.53      kristaps  624:        if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
                    625:                goto err;
1.1       kristaps  626:
1.53      kristaps  627:        return(1);
1.1       kristaps  628:
1.53      kristaps  629: err:   /* Error out. */
1.1       kristaps  630:
1.53      kristaps  631:        m->flags |= MDOC_HALT;
                    632:        return(0);
1.1       kristaps  633: }

CVSweb