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

Annotation of mandoc/mdoc.c, Revision 1.70

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

CVSweb