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

Annotation of mandoc/mdoc.c, Revision 1.120

1.120   ! kristaps    1: /*     $Id: mdoc.c,v 1.119 2010/04/03 13:02:35 kristaps Exp $ */
1.1       kristaps    2: /*
1.76      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.75      kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.75      kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.114     kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.106     kristaps   21: #include <sys/types.h>
                     22:
1.1       kristaps   23: #include <assert.h>
                     24: #include <ctype.h>
                     25: #include <stdarg.h>
1.73      kristaps   26: #include <stdio.h>
1.1       kristaps   27: #include <stdlib.h>
                     28: #include <string.h>
1.120   ! kristaps   29: #include <time.h>
1.1       kristaps   30:
1.70      kristaps   31: #include "libmdoc.h"
1.113     kristaps   32: #include "libmandoc.h"
1.1       kristaps   33:
1.89      kristaps   34: const  char *const __mdoc_merrnames[MERRMAX] = {
                     35:        "trailing whitespace", /* ETAILWS */
                     36:        "unexpected quoted parameter", /* EQUOTPARM */
                     37:        "unterminated quoted parameter", /* EQUOTTERM */
                     38:        "argument parameter suggested", /* EARGVAL */
                     39:        "macro disallowed in prologue", /* EBODYPROL */
                     40:        "macro disallowed in body", /* EPROLBODY */
                     41:        "text disallowed in prologue", /* ETEXTPROL */
                     42:        "blank line disallowed", /* ENOBLANK */
                     43:        "text parameter too long", /* ETOOLONG */
                     44:        "invalid escape sequence", /* EESCAPE */
                     45:        "invalid character", /* EPRINT */
                     46:        "document has no body", /* ENODAT */
                     47:        "document has no prologue", /* ENOPROLOGUE */
                     48:        "expected line arguments", /* ELINE */
                     49:        "invalid AT&T argument", /* EATT */
                     50:        "default name not yet set", /* ENAME */
                     51:        "missing list type", /* ELISTTYPE */
                     52:        "missing display type", /* EDISPTYPE */
                     53:        "too many display types", /* EMULTIDISP */
                     54:        "too many list types", /* EMULTILIST */
                     55:        "NAME section must be first", /* ESECNAME */
                     56:        "badly-formed NAME section", /* ENAMESECINC */
                     57:        "argument repeated", /* EARGREP */
                     58:        "expected boolean parameter", /* EBOOL */
                     59:        "inconsistent column syntax", /* ECOLMIS */
                     60:        "nested display invalid", /* ENESTDISP */
                     61:        "width argument missing", /* EMISSWIDTH */
                     62:        "invalid section for this manual section", /* EWRONGMSEC */
                     63:        "section out of conventional order", /* ESECOOO */
                     64:        "section repeated", /* ESECREP */
                     65:        "invalid standard argument", /* EBADSTAND */
                     66:        "multi-line arguments discouraged", /* ENOMULTILINE */
                     67:        "multi-line arguments suggested", /* EMULTILINE */
                     68:        "line arguments discouraged", /* ENOLINE */
                     69:        "prologue macro out of conventional order", /* EPROLOOO */
                     70:        "prologue macro repeated", /* EPROLREP */
                     71:        "invalid manual section", /* EBADMSEC */
                     72:        "invalid section", /* EBADSEC */
                     73:        "invalid font mode", /* EFONT */
                     74:        "invalid date syntax", /* EBADDATE */
                     75:        "invalid number format", /* ENUMFMT */
                     76:        "superfluous width argument", /* ENOWIDTH */
                     77:        "system: utsname error", /* EUTSNAME */
                     78:        "obsolete macro", /* EOBS */
                     79:        "end-of-line scope violation", /* EIMPBRK */
                     80:        "empty macro ignored", /* EIGNE */
                     81:        "unclosed explicit scope", /* EOPEN */
                     82:        "unterminated quoted phrase", /* EQUOTPHR */
                     83:        "closure macro without prior context", /* ENOCTX */
1.107     kristaps   84:        "no description found for library", /* ELIB */
                     85:        "bad child for parent context", /* EBADCHILD */
1.109     kristaps   86:        "list arguments preceding type", /* ENOTYPE */
1.89      kristaps   87: };
                     88:
1.1       kristaps   89: const  char *const __mdoc_macronames[MDOC_MAX] = {
1.82      kristaps   90:        "Ap",           "Dd",           "Dt",           "Os",
1.1       kristaps   91:        "Sh",           "Ss",           "Pp",           "D1",
                     92:        "Dl",           "Bd",           "Ed",           "Bl",
                     93:        "El",           "It",           "Ad",           "An",
                     94:        "Ar",           "Cd",           "Cm",           "Dv",
                     95:        "Er",           "Ev",           "Ex",           "Fa",
                     96:        "Fd",           "Fl",           "Fn",           "Ft",
                     97:        "Ic",           "In",           "Li",           "Nd",
                     98:        "Nm",           "Op",           "Ot",           "Pa",
                     99:        "Rv",           "St",           "Va",           "Vt",
                    100:        /* LINTED */
1.114     kristaps  101:        "Xr",           "%A",           "%B",           "%D",
1.1       kristaps  102:        /* LINTED */
1.114     kristaps  103:        "%I",           "%J",           "%N",           "%O",
1.1       kristaps  104:        /* LINTED */
1.114     kristaps  105:        "%P",           "%R",           "%T",           "%V",
1.1       kristaps  106:        "Ac",           "Ao",           "Aq",           "At",
                    107:        "Bc",           "Bf",           "Bo",           "Bq",
                    108:        "Bsx",          "Bx",           "Db",           "Dc",
                    109:        "Do",           "Dq",           "Ec",           "Ef",
                    110:        "Em",           "Eo",           "Fx",           "Ms",
                    111:        "No",           "Ns",           "Nx",           "Ox",
                    112:        "Pc",           "Pf",           "Po",           "Pq",
                    113:        "Qc",           "Ql",           "Qo",           "Qq",
                    114:        "Re",           "Rs",           "Sc",           "So",
                    115:        "Sq",           "Sm",           "Sx",           "Sy",
                    116:        "Tn",           "Ux",           "Xc",           "Xo",
                    117:        "Fo",           "Fc",           "Oo",           "Oc",
                    118:        "Bk",           "Ek",           "Bt",           "Hf",
1.82      kristaps  119:        "Fr",           "Ud",           "Lb",           "Lp",
                    120:        "Lk",           "Mt",           "Brq",          "Bro",
1.64      kristaps  121:        /* LINTED */
1.114     kristaps  122:        "Brc",          "%C",           "Es",           "En",
1.69      kristaps  123:        /* LINTED */
1.114     kristaps  124:        "Dx",           "%Q",           "br",           "sp",
1.110     kristaps  125:        /* LINTED */
1.114     kristaps  126:        "%U"
1.1       kristaps  127:        };
                    128:
                    129: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                    130:        "split",                "nosplit",              "ragged",
                    131:        "unfilled",             "literal",              "file",
                    132:        "offset",               "bullet",               "dash",
                    133:        "hyphen",               "item",                 "enum",
                    134:        "tag",                  "diag",                 "hang",
                    135:        "ohang",                "inset",                "column",
                    136:        "width",                "compact",              "std",
1.52      kristaps  137:        "filled",               "words",                "emphasis",
1.108     kristaps  138:        "symbolic",             "nested",               "centered"
1.1       kristaps  139:        };
                    140:
                    141: const  char * const *mdoc_macronames = __mdoc_macronames;
                    142: const  char * const *mdoc_argnames = __mdoc_argnames;
                    143:
1.73      kristaps  144: static void              mdoc_free1(struct mdoc *);
1.113     kristaps  145: static void              mdoc_alloc1(struct mdoc *);
1.73      kristaps  146: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.117     kristaps  147:                                enum mdoct, enum mdoc_type);
1.73      kristaps  148: static int               node_append(struct mdoc *,
1.71      kristaps  149:                                struct mdoc_node *);
                    150: static int               parsetext(struct mdoc *, int, char *);
                    151: static int               parsemacro(struct mdoc *, int, char *);
                    152: static int               macrowarn(struct mdoc *, int, const char *);
1.91      kristaps  153: static int               pstring(struct mdoc *, int, int,
                    154:                                const char *, size_t);
1.88      kristaps  155:
1.1       kristaps  156: const struct mdoc_node *
1.71      kristaps  157: mdoc_node(const struct mdoc *m)
1.1       kristaps  158: {
                    159:
1.71      kristaps  160:        return(MDOC_HALT & m->flags ? NULL : m->first);
1.1       kristaps  161: }
                    162:
                    163:
1.37      kristaps  164: const struct mdoc_meta *
1.71      kristaps  165: mdoc_meta(const struct mdoc *m)
1.37      kristaps  166: {
                    167:
1.71      kristaps  168:        return(MDOC_HALT & m->flags ? NULL : &m->meta);
1.37      kristaps  169: }
                    170:
                    171:
1.85      kristaps  172: /*
                    173:  * Frees volatile resources (parse tree, meta-data, fields).
                    174:  */
1.73      kristaps  175: static void
                    176: mdoc_free1(struct mdoc *mdoc)
1.67      kristaps  177: {
                    178:
                    179:        if (mdoc->first)
                    180:                mdoc_node_freelist(mdoc->first);
                    181:        if (mdoc->meta.title)
                    182:                free(mdoc->meta.title);
                    183:        if (mdoc->meta.os)
                    184:                free(mdoc->meta.os);
                    185:        if (mdoc->meta.name)
                    186:                free(mdoc->meta.name);
                    187:        if (mdoc->meta.arch)
                    188:                free(mdoc->meta.arch);
                    189:        if (mdoc->meta.vol)
                    190:                free(mdoc->meta.vol);
1.73      kristaps  191: }
                    192:
                    193:
1.85      kristaps  194: /*
                    195:  * Allocate all volatile resources (parse tree, meta-data, fields).
                    196:  */
1.113     kristaps  197: static void
1.73      kristaps  198: mdoc_alloc1(struct mdoc *mdoc)
                    199: {
1.67      kristaps  200:
1.112     kristaps  201:        memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.67      kristaps  202:        mdoc->flags = 0;
1.85      kristaps  203:        mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.113     kristaps  204:        mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.70      kristaps  205:        mdoc->first = mdoc->last;
1.67      kristaps  206:        mdoc->last->type = MDOC_ROOT;
                    207:        mdoc->next = MDOC_NEXT_CHILD;
1.73      kristaps  208: }
                    209:
                    210:
                    211: /*
1.85      kristaps  212:  * Free up volatile resources (see mdoc_free1()) then re-initialises the
                    213:  * data with mdoc_alloc1().  After invocation, parse data has been reset
                    214:  * and the parser is ready for re-invocation on a new tree; however,
                    215:  * cross-parse non-volatile data is kept intact.
1.73      kristaps  216:  */
1.113     kristaps  217: void
1.73      kristaps  218: mdoc_reset(struct mdoc *mdoc)
                    219: {
                    220:
                    221:        mdoc_free1(mdoc);
1.113     kristaps  222:        mdoc_alloc1(mdoc);
1.67      kristaps  223: }
                    224:
                    225:
1.68      kristaps  226: /*
1.85      kristaps  227:  * Completely free up all volatile and non-volatile parse resources.
                    228:  * After invocation, the pointer is no longer usable.
1.68      kristaps  229:  */
1.67      kristaps  230: void
1.38      kristaps  231: mdoc_free(struct mdoc *mdoc)
1.34      kristaps  232: {
                    233:
1.73      kristaps  234:        mdoc_free1(mdoc);
1.1       kristaps  235:        free(mdoc);
                    236: }
                    237:
                    238:
1.85      kristaps  239: /*
                    240:  * Allocate volatile and non-volatile parse resources.
                    241:  */
1.1       kristaps  242: struct mdoc *
1.55      kristaps  243: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1       kristaps  244: {
                    245:        struct mdoc     *p;
                    246:
1.113     kristaps  247:        p = mandoc_calloc(1, sizeof(struct mdoc));
                    248:
1.74      kristaps  249:        if (cb)
1.113     kristaps  250:                memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.105     kristaps  251:
1.1       kristaps  252:        p->data = data;
1.73      kristaps  253:        p->pflags = pflags;
                    254:
1.113     kristaps  255:        mdoc_hash_init();
                    256:        mdoc_alloc1(p);
                    257:        return(p);
1.1       kristaps  258: }
                    259:
                    260:
1.68      kristaps  261: /*
                    262:  * Climb back up the parse tree, validating open scopes.  Mostly calls
1.85      kristaps  263:  * through to macro_end() in macro.c.
1.68      kristaps  264:  */
1.1       kristaps  265: int
1.72      kristaps  266: mdoc_endparse(struct mdoc *m)
1.20      kristaps  267: {
                    268:
1.72      kristaps  269:        if (MDOC_HALT & m->flags)
1.20      kristaps  270:                return(0);
1.72      kristaps  271:        else if (mdoc_macroend(m))
1.20      kristaps  272:                return(1);
1.72      kristaps  273:        m->flags |= MDOC_HALT;
                    274:        return(0);
1.20      kristaps  275: }
                    276:
                    277:
1.50      kristaps  278: /*
1.53      kristaps  279:  * Main parse routine.  Parses a single line -- really just hands off to
1.85      kristaps  280:  * the macro (parsemacro()) or text parser (parsetext()).
1.50      kristaps  281:  */
1.20      kristaps  282: int
1.53      kristaps  283: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1       kristaps  284: {
                    285:
1.53      kristaps  286:        if (MDOC_HALT & m->flags)
1.20      kristaps  287:                return(0);
1.50      kristaps  288:
1.53      kristaps  289:        return('.' == *buf ? parsemacro(m, ln, buf) :
                    290:                        parsetext(m, ln, buf));
1.1       kristaps  291: }
                    292:
                    293:
                    294: int
1.31      kristaps  295: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    296:                const char *fmt, ...)
1.1       kristaps  297: {
1.31      kristaps  298:        char             buf[256];
                    299:        va_list          ap;
1.1       kristaps  300:
                    301:        if (NULL == mdoc->cb.mdoc_err)
                    302:                return(0);
1.31      kristaps  303:
                    304:        va_start(ap, fmt);
                    305:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    306:        va_end(ap);
1.88      kristaps  307:
1.31      kristaps  308:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1       kristaps  309: }
                    310:
                    311:
                    312: int
1.87      kristaps  313: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1       kristaps  314: {
1.31      kristaps  315:        char             buf[256];
                    316:        va_list          ap;
1.1       kristaps  317:
                    318:        if (NULL == mdoc->cb.mdoc_warn)
                    319:                return(0);
1.31      kristaps  320:
                    321:        va_start(ap, fmt);
                    322:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    323:        va_end(ap);
1.88      kristaps  324:
1.87      kristaps  325:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, buf));
1.1       kristaps  326: }
                    327:
                    328:
                    329: int
1.88      kristaps  330: mdoc_err(struct mdoc *m, int line, int pos, int iserr, enum merr type)
1.78      kristaps  331: {
1.89      kristaps  332:        const char      *p;
1.88      kristaps  333:
1.89      kristaps  334:        p = __mdoc_merrnames[(int)type];
1.73      kristaps  335:        assert(p);
1.88      kristaps  336:
                    337:        if (iserr)
                    338:                return(mdoc_verr(m, line, pos, p));
                    339:
                    340:        return(mdoc_vwarn(m, line, pos, p));
                    341: }
                    342:
                    343:
                    344: int
1.117     kristaps  345: mdoc_macro(struct mdoc *m, enum mdoct tok,
1.88      kristaps  346:                int ln, int pp, int *pos, char *buf)
                    347: {
1.117     kristaps  348:
                    349:        assert(tok < MDOC_MAX);
1.97      kristaps  350:        /*
                    351:         * If we're in the prologue, deny "body" macros.  Similarly, if
                    352:         * we're in the body, deny prologue calls.
                    353:         */
1.88      kristaps  354:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.120   ! kristaps  355:                        MDOC_PBODY & m->flags) {
        !           356:                if ( ! mdoc_pwarn(m, ln, pp, EBODYPROL))
        !           357:                        return(0);
        !           358:                /*
        !           359:                 * FIXME: do this in mdoc_action.c.
        !           360:                 */
        !           361:                if (NULL == m->meta.title)
        !           362:                        m->meta.title = mandoc_strdup("unknown");
        !           363:                if (NULL == m->meta.vol)
        !           364:                        m->meta.vol = mandoc_strdup("local");
        !           365:                if (NULL == m->meta.os)
        !           366:                        m->meta.os = mandoc_strdup("local");
        !           367:                if (0 == m->meta.date)
        !           368:                        m->meta.date = time(NULL);
        !           369:                m->flags |= MDOC_PBODY;
        !           370:        }
1.88      kristaps  371:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
                    372:                        ! (MDOC_PBODY & m->flags))
                    373:                return(mdoc_perr(m, ln, pp, EBODYPROL));
                    374:
                    375:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.73      kristaps  376: }
                    377:
                    378:
                    379: static int
                    380: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  381: {
                    382:
1.25      kristaps  383:        assert(mdoc->last);
                    384:        assert(mdoc->first);
                    385:        assert(MDOC_ROOT != p->type);
1.1       kristaps  386:
1.13      kristaps  387:        switch (mdoc->next) {
                    388:        case (MDOC_NEXT_SIBLING):
1.6       kristaps  389:                mdoc->last->next = p;
                    390:                p->prev = mdoc->last;
1.13      kristaps  391:                p->parent = mdoc->last->parent;
1.1       kristaps  392:                break;
1.13      kristaps  393:        case (MDOC_NEXT_CHILD):
                    394:                mdoc->last->child = p;
1.1       kristaps  395:                p->parent = mdoc->last;
                    396:                break;
                    397:        default:
1.13      kristaps  398:                abort();
                    399:                /* NOTREACHED */
1.1       kristaps  400:        }
                    401:
1.86      kristaps  402:        p->parent->nchild++;
                    403:
1.23      kristaps  404:        if ( ! mdoc_valid_pre(mdoc, p))
                    405:                return(0);
1.68      kristaps  406:        if ( ! mdoc_action_pre(mdoc, p))
                    407:                return(0);
1.27      kristaps  408:
                    409:        switch (p->type) {
                    410:        case (MDOC_HEAD):
                    411:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  412:                p->parent->head = p;
1.27      kristaps  413:                break;
                    414:        case (MDOC_TAIL):
                    415:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  416:                p->parent->tail = p;
1.27      kristaps  417:                break;
                    418:        case (MDOC_BODY):
                    419:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  420:                p->parent->body = p;
1.27      kristaps  421:                break;
                    422:        default:
                    423:                break;
                    424:        }
                    425:
1.1       kristaps  426:        mdoc->last = p;
1.71      kristaps  427:
                    428:        switch (p->type) {
                    429:        case (MDOC_TEXT):
                    430:                if ( ! mdoc_valid_post(mdoc))
                    431:                        return(0);
                    432:                if ( ! mdoc_action_post(mdoc))
                    433:                        return(0);
                    434:                break;
                    435:        default:
                    436:                break;
                    437:        }
                    438:
1.23      kristaps  439:        return(1);
1.1       kristaps  440: }
                    441:
                    442:
1.45      kristaps  443: static struct mdoc_node *
1.117     kristaps  444: node_alloc(struct mdoc *m, int line, int pos,
                    445:                enum mdoct tok, enum mdoc_type type)
1.45      kristaps  446: {
1.46      kristaps  447:        struct mdoc_node *p;
                    448:
1.113     kristaps  449:        p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.91      kristaps  450:        p->sec = m->lastsec;
1.73      kristaps  451:        p->line = line;
                    452:        p->pos = pos;
                    453:        p->tok = tok;
1.118     kristaps  454:        p->type = type;
1.45      kristaps  455:
1.46      kristaps  456:        return(p);
1.45      kristaps  457: }
                    458:
                    459:
1.23      kristaps  460: int
1.117     kristaps  461: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.17      kristaps  462: {
                    463:        struct mdoc_node *p;
                    464:
1.91      kristaps  465:        p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.102     kristaps  466:        if ( ! node_append(m, p))
                    467:                return(0);
                    468:        m->next = MDOC_NEXT_CHILD;
                    469:        return(1);
1.17      kristaps  470: }
                    471:
                    472:
1.23      kristaps  473: int
1.117     kristaps  474: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  475: {
                    476:        struct mdoc_node *p;
                    477:
1.91      kristaps  478:        assert(m->first);
                    479:        assert(m->last);
1.1       kristaps  480:
1.91      kristaps  481:        p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.102     kristaps  482:        if ( ! node_append(m, p))
                    483:                return(0);
                    484:        m->next = MDOC_NEXT_CHILD;
                    485:        return(1);
1.1       kristaps  486: }
                    487:
                    488:
1.23      kristaps  489: int
1.117     kristaps  490: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  491: {
                    492:        struct mdoc_node *p;
                    493:
1.91      kristaps  494:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.102     kristaps  495:        if ( ! node_append(m, p))
                    496:                return(0);
                    497:        m->next = MDOC_NEXT_CHILD;
                    498:        return(1);
1.1       kristaps  499: }
                    500:
                    501:
1.23      kristaps  502: int
1.91      kristaps  503: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.117     kristaps  504:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  505: {
                    506:        struct mdoc_node *p;
                    507:
1.91      kristaps  508:        p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.77      kristaps  509:        p->args = args;
                    510:        if (p->args)
1.53      kristaps  511:                (args->refcnt)++;
1.102     kristaps  512:        if ( ! node_append(m, p))
                    513:                return(0);
                    514:        m->next = MDOC_NEXT_CHILD;
                    515:        return(1);
1.1       kristaps  516: }
                    517:
                    518:
1.23      kristaps  519: int
1.91      kristaps  520: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.117     kristaps  521:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  522: {
                    523:        struct mdoc_node *p;
                    524:
1.91      kristaps  525:        p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.77      kristaps  526:        p->args = args;
                    527:        if (p->args)
1.53      kristaps  528:                (args->refcnt)++;
1.102     kristaps  529:        if ( ! node_append(m, p))
                    530:                return(0);
                    531:        m->next = MDOC_NEXT_CHILD;
                    532:        return(1);
1.1       kristaps  533: }
                    534:
                    535:
1.91      kristaps  536: static int
                    537: pstring(struct mdoc *m, int line, int pos, const char *p, size_t len)
1.1       kristaps  538: {
1.91      kristaps  539:        struct mdoc_node *n;
                    540:        size_t            sv;
1.1       kristaps  541:
1.91      kristaps  542:        n = node_alloc(m, line, pos, -1, MDOC_TEXT);
1.113     kristaps  543:        n->string = mandoc_malloc(len + 1);
1.91      kristaps  544:        sv = strlcpy(n->string, p, len + 1);
                    545:
                    546:        /* Prohibit truncation. */
                    547:        assert(sv < len + 1);
                    548:
1.101     kristaps  549:        if ( ! node_append(m, n))
                    550:                return(0);
                    551:        m->next = MDOC_NEXT_SIBLING;
                    552:        return(1);
1.91      kristaps  553: }
                    554:
                    555:
                    556: int
                    557: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
                    558: {
                    559:
                    560:        return(pstring(m, line, pos, p, strlen(p)));
1.1       kristaps  561: }
                    562:
                    563:
1.53      kristaps  564: void
                    565: mdoc_node_free(struct mdoc_node *p)
1.1       kristaps  566: {
                    567:
1.86      kristaps  568:        if (p->parent)
                    569:                p->parent->nchild--;
1.53      kristaps  570:        if (p->string)
                    571:                free(p->string);
                    572:        if (p->args)
                    573:                mdoc_argv_free(p->args);
1.1       kristaps  574:        free(p);
                    575: }
                    576:
                    577:
1.53      kristaps  578: void
                    579: mdoc_node_freelist(struct mdoc_node *p)
1.1       kristaps  580: {
                    581:
1.53      kristaps  582:        if (p->child)
                    583:                mdoc_node_freelist(p->child);
                    584:        if (p->next)
                    585:                mdoc_node_freelist(p->next);
1.1       kristaps  586:
1.86      kristaps  587:        assert(0 == p->nchild);
1.53      kristaps  588:        mdoc_node_free(p);
1.1       kristaps  589: }
                    590:
                    591:
1.53      kristaps  592: /*
                    593:  * Parse free-form text, that is, a line that does not begin with the
                    594:  * control character.
                    595:  */
                    596: static int
1.68      kristaps  597: parsetext(struct mdoc *m, int line, char *buf)
1.1       kristaps  598: {
1.91      kristaps  599:        int              i, j;
1.116     kristaps  600:        char             sv;
1.1       kristaps  601:
1.85      kristaps  602:        if (SEC_NONE == m->lastnamed)
1.88      kristaps  603:                return(mdoc_perr(m, line, 0, ETEXTPROL));
1.91      kristaps  604:
                    605:        /*
                    606:         * If in literal mode, then pass the buffer directly to the
                    607:         * back-end, as it should be preserved as a single term.
                    608:         */
                    609:
1.101     kristaps  610:        if (MDOC_LITERAL & m->flags)
                    611:                return(mdoc_word_alloc(m, line, 0, buf));
1.91      kristaps  612:
                    613:        /* Disallow blank/white-space lines in non-literal mode. */
1.1       kristaps  614:
1.91      kristaps  615:        for (i = 0; ' ' == buf[i]; i++)
                    616:                /* Skip leading whitespace. */ ;
1.115     kristaps  617:
1.119     kristaps  618:        if ('\0' == buf[i]) {
                    619:                if ( ! mdoc_pwarn(m, line, 0, ENOBLANK))
                    620:                        return(0);
                    621:                /*
                    622:                 * Assume that a `Pp' should be inserted in the case of
                    623:                 * a blank line.  Technically, blank lines aren't
                    624:                 * allowed, but enough manuals assume this behaviour
                    625:                 * that we want to work around it.
                    626:                 */
                    627:                if ( ! mdoc_elem_alloc(m, line, 0, MDOC_Pp, NULL))
                    628:                        return(0);
                    629:        }
1.68      kristaps  630:
1.91      kristaps  631:        /*
                    632:         * Break apart a free-form line into tokens.  Spaces are
                    633:         * stripped out of the input.
                    634:         */
                    635:
                    636:        for (j = i; buf[i]; i++) {
                    637:                if (' ' != buf[i])
                    638:                        continue;
                    639:
                    640:                /* Escaped whitespace. */
                    641:                if (i && ' ' == buf[i] && '\\' == buf[i - 1])
                    642:                        continue;
                    643:
1.116     kristaps  644:                sv = buf[i];
1.115     kristaps  645:                buf[i++] = '\0';
                    646:
1.91      kristaps  647:                if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
                    648:                        return(0);
                    649:
1.116     kristaps  650:                /* Trailing whitespace?  Check at overwritten byte. */
                    651:
                    652:                if (' ' == sv && '\0' == buf[i])
                    653:                        if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
                    654:                                return(0);
                    655:
1.91      kristaps  656:                for ( ; ' ' == buf[i]; i++)
                    657:                        /* Skip trailing whitespace. */ ;
                    658:
                    659:                j = i;
1.116     kristaps  660:
                    661:                /* Trailing whitespace? */
                    662:
                    663:                if (' ' == buf[i - 1] && '\0' == buf[i])
                    664:                        if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
                    665:                                return(0);
                    666:
1.115     kristaps  667:                if ('\0' == buf[i])
1.91      kristaps  668:                        break;
                    669:        }
                    670:
                    671:        if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
1.53      kristaps  672:                return(0);
1.1       kristaps  673:
1.68      kristaps  674:        m->next = MDOC_NEXT_SIBLING;
1.53      kristaps  675:        return(1);
1.1       kristaps  676: }
                    677:
                    678:
1.91      kristaps  679:
1.58      kristaps  680: static int
                    681: macrowarn(struct mdoc *m, int ln, const char *buf)
                    682: {
                    683:        if ( ! (MDOC_IGN_MACRO & m->pflags))
1.96      kristaps  684:                return(mdoc_verr(m, ln, 0,
1.73      kristaps  685:                                "unknown macro: %s%s",
1.59      kristaps  686:                                buf, strlen(buf) > 3 ? "..." : ""));
1.96      kristaps  687:        return(mdoc_vwarn(m, ln, 0, "unknown macro: %s%s",
1.59      kristaps  688:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  689: }
                    690:
                    691:
1.53      kristaps  692: /*
                    693:  * Parse a macro line, that is, a line beginning with the control
                    694:  * character.
                    695:  */
                    696: int
                    697: parsemacro(struct mdoc *m, int ln, char *buf)
1.1       kristaps  698: {
1.103     kristaps  699:        int               i, j, c;
1.53      kristaps  700:        char              mac[5];
1.1       kristaps  701:
1.81      kristaps  702:        /* Empty lines are ignored. */
1.63      kristaps  703:
1.115     kristaps  704:        if ('\0' == buf[1])
1.63      kristaps  705:                return(1);
                    706:
1.100     kristaps  707:        i = 1;
                    708:
                    709:        /* Accept whitespace after the initial control char. */
                    710:
                    711:        if (' ' == buf[i]) {
                    712:                i++;
1.65      kristaps  713:                while (buf[i] && ' ' == buf[i])
1.63      kristaps  714:                        i++;
1.115     kristaps  715:                if ('\0' == buf[i])
1.63      kristaps  716:                        return(1);
                    717:        }
1.1       kristaps  718:
1.53      kristaps  719:        /* Copy the first word into a nil-terminated buffer. */
1.1       kristaps  720:
1.100     kristaps  721:        for (j = 0; j < 4; j++, i++) {
1.115     kristaps  722:                if ('\0' == (mac[j] = buf[i]))
1.53      kristaps  723:                        break;
1.65      kristaps  724:                else if (' ' == buf[i])
1.53      kristaps  725:                        break;
1.104     kristaps  726:
                    727:                /* Check for invalid characters. */
                    728:
                    729:                if (isgraph((u_char)buf[i]))
                    730:                        continue;
                    731:                return(mdoc_perr(m, ln, i, EPRINT));
1.53      kristaps  732:        }
1.1       kristaps  733:
1.100     kristaps  734:        mac[j] = 0;
1.1       kristaps  735:
1.100     kristaps  736:        if (j == 4 || j < 2) {
1.58      kristaps  737:                if ( ! macrowarn(m, ln, mac))
                    738:                        goto err;
                    739:                return(1);
1.53      kristaps  740:        }
                    741:
1.105     kristaps  742:        if (MDOC_MAX == (c = mdoc_hash_find(mac))) {
1.58      kristaps  743:                if ( ! macrowarn(m, ln, mac))
                    744:                        goto err;
                    745:                return(1);
1.53      kristaps  746:        }
1.1       kristaps  747:
1.53      kristaps  748:        /* The macro is sane.  Jump to the next word. */
1.1       kristaps  749:
1.65      kristaps  750:        while (buf[i] && ' ' == buf[i])
1.53      kristaps  751:                i++;
1.1       kristaps  752:
1.115     kristaps  753:        /* Trailing whitespace? */
                    754:
                    755:        if ('\0' == buf[i] && ' ' == buf[i - 1])
                    756:                if ( ! mdoc_pwarn(m, ln, i - 1, ETAILWS))
                    757:                        goto err;
                    758:
1.98      kristaps  759:        /*
                    760:         * Begin recursive parse sequence.  Since we're at the start of
                    761:         * the line, we don't need to do callable/parseable checks.
                    762:         */
1.103     kristaps  763:        if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
1.53      kristaps  764:                goto err;
1.1       kristaps  765:
1.53      kristaps  766:        return(1);
1.1       kristaps  767:
1.53      kristaps  768: err:   /* Error out. */
1.1       kristaps  769:
1.53      kristaps  770:        m->flags |= MDOC_HALT;
                    771:        return(0);
1.1       kristaps  772: }
1.100     kristaps  773:
                    774:

CVSweb