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

Annotation of mandoc/mdoc.c, Revision 1.131

1.131   ! kristaps    1: /*     $Id: mdoc.c,v 1.130 2010/05/13 06:22:11 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 section", /* EBADSEC */
                     72:        "invalid font mode", /* EFONT */
                     73:        "invalid date syntax", /* EBADDATE */
                     74:        "invalid number format", /* ENUMFMT */
                     75:        "superfluous width argument", /* ENOWIDTH */
                     76:        "system: utsname error", /* EUTSNAME */
                     77:        "obsolete macro", /* EOBS */
                     78:        "end-of-line scope violation", /* EIMPBRK */
                     79:        "empty macro ignored", /* EIGNE */
                     80:        "unclosed explicit scope", /* EOPEN */
                     81:        "unterminated quoted phrase", /* EQUOTPHR */
                     82:        "closure macro without prior context", /* ENOCTX */
1.107     kristaps   83:        "no description found for library", /* ELIB */
                     84:        "bad child for parent context", /* EBADCHILD */
1.109     kristaps   85:        "list arguments preceding type", /* ENOTYPE */
1.123     kristaps   86:        "deprecated comment style", /* EBADCOMMENT */
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.121     kristaps  144: static void              mdoc_node_free(struct mdoc_node *);
                    145: static void              mdoc_node_unlink(struct mdoc *,
                    146:                                struct mdoc_node *);
1.73      kristaps  147: static void              mdoc_free1(struct mdoc *);
1.113     kristaps  148: static void              mdoc_alloc1(struct mdoc *);
1.73      kristaps  149: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.117     kristaps  150:                                enum mdoct, enum mdoc_type);
1.73      kristaps  151: static int               node_append(struct mdoc *,
1.71      kristaps  152:                                struct mdoc_node *);
1.123     kristaps  153: static int               mdoc_ptext(struct mdoc *, int, char *);
                    154: static int               mdoc_pmacro(struct mdoc *, int, char *);
1.71      kristaps  155: static int               macrowarn(struct mdoc *, int, const char *);
1.124     kristaps  156:
1.88      kristaps  157:
1.1       kristaps  158: const struct mdoc_node *
1.71      kristaps  159: mdoc_node(const struct mdoc *m)
1.1       kristaps  160: {
                    161:
1.71      kristaps  162:        return(MDOC_HALT & m->flags ? NULL : m->first);
1.1       kristaps  163: }
                    164:
                    165:
1.37      kristaps  166: const struct mdoc_meta *
1.71      kristaps  167: mdoc_meta(const struct mdoc *m)
1.37      kristaps  168: {
                    169:
1.71      kristaps  170:        return(MDOC_HALT & m->flags ? NULL : &m->meta);
1.37      kristaps  171: }
                    172:
                    173:
1.85      kristaps  174: /*
                    175:  * Frees volatile resources (parse tree, meta-data, fields).
                    176:  */
1.73      kristaps  177: static void
                    178: mdoc_free1(struct mdoc *mdoc)
1.67      kristaps  179: {
                    180:
                    181:        if (mdoc->first)
1.121     kristaps  182:                mdoc_node_delete(mdoc, mdoc->first);
1.67      kristaps  183:        if (mdoc->meta.title)
                    184:                free(mdoc->meta.title);
                    185:        if (mdoc->meta.os)
                    186:                free(mdoc->meta.os);
                    187:        if (mdoc->meta.name)
                    188:                free(mdoc->meta.name);
                    189:        if (mdoc->meta.arch)
                    190:                free(mdoc->meta.arch);
                    191:        if (mdoc->meta.vol)
                    192:                free(mdoc->meta.vol);
1.73      kristaps  193: }
                    194:
                    195:
1.85      kristaps  196: /*
                    197:  * Allocate all volatile resources (parse tree, meta-data, fields).
                    198:  */
1.113     kristaps  199: static void
1.73      kristaps  200: mdoc_alloc1(struct mdoc *mdoc)
                    201: {
1.67      kristaps  202:
1.112     kristaps  203:        memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.67      kristaps  204:        mdoc->flags = 0;
1.85      kristaps  205:        mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.113     kristaps  206:        mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.70      kristaps  207:        mdoc->first = mdoc->last;
1.67      kristaps  208:        mdoc->last->type = MDOC_ROOT;
                    209:        mdoc->next = MDOC_NEXT_CHILD;
1.73      kristaps  210: }
                    211:
                    212:
                    213: /*
1.85      kristaps  214:  * Free up volatile resources (see mdoc_free1()) then re-initialises the
                    215:  * data with mdoc_alloc1().  After invocation, parse data has been reset
                    216:  * and the parser is ready for re-invocation on a new tree; however,
                    217:  * cross-parse non-volatile data is kept intact.
1.73      kristaps  218:  */
1.113     kristaps  219: void
1.73      kristaps  220: mdoc_reset(struct mdoc *mdoc)
                    221: {
                    222:
                    223:        mdoc_free1(mdoc);
1.113     kristaps  224:        mdoc_alloc1(mdoc);
1.67      kristaps  225: }
                    226:
                    227:
1.68      kristaps  228: /*
1.85      kristaps  229:  * Completely free up all volatile and non-volatile parse resources.
                    230:  * After invocation, the pointer is no longer usable.
1.68      kristaps  231:  */
1.67      kristaps  232: void
1.38      kristaps  233: mdoc_free(struct mdoc *mdoc)
1.34      kristaps  234: {
                    235:
1.73      kristaps  236:        mdoc_free1(mdoc);
1.1       kristaps  237:        free(mdoc);
                    238: }
                    239:
                    240:
1.85      kristaps  241: /*
                    242:  * Allocate volatile and non-volatile parse resources.
                    243:  */
1.1       kristaps  244: struct mdoc *
1.55      kristaps  245: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1       kristaps  246: {
                    247:        struct mdoc     *p;
                    248:
1.113     kristaps  249:        p = mandoc_calloc(1, sizeof(struct mdoc));
                    250:
1.74      kristaps  251:        if (cb)
1.113     kristaps  252:                memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.105     kristaps  253:
1.1       kristaps  254:        p->data = data;
1.73      kristaps  255:        p->pflags = pflags;
                    256:
1.113     kristaps  257:        mdoc_hash_init();
                    258:        mdoc_alloc1(p);
                    259:        return(p);
1.1       kristaps  260: }
                    261:
                    262:
1.68      kristaps  263: /*
                    264:  * Climb back up the parse tree, validating open scopes.  Mostly calls
1.85      kristaps  265:  * through to macro_end() in macro.c.
1.68      kristaps  266:  */
1.1       kristaps  267: int
1.72      kristaps  268: mdoc_endparse(struct mdoc *m)
1.20      kristaps  269: {
                    270:
1.72      kristaps  271:        if (MDOC_HALT & m->flags)
1.20      kristaps  272:                return(0);
1.72      kristaps  273:        else if (mdoc_macroend(m))
1.20      kristaps  274:                return(1);
1.72      kristaps  275:        m->flags |= MDOC_HALT;
                    276:        return(0);
1.20      kristaps  277: }
                    278:
                    279:
1.50      kristaps  280: /*
1.53      kristaps  281:  * Main parse routine.  Parses a single line -- really just hands off to
1.123     kristaps  282:  * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50      kristaps  283:  */
1.20      kristaps  284: int
1.53      kristaps  285: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1       kristaps  286: {
                    287:
1.53      kristaps  288:        if (MDOC_HALT & m->flags)
1.20      kristaps  289:                return(0);
1.50      kristaps  290:
1.130     kristaps  291:        m->flags |= MDOC_NEWLINE;
                    292:        return('.' == *buf ?
                    293:                        mdoc_pmacro(m, ln, buf) :
1.123     kristaps  294:                        mdoc_ptext(m, ln, buf));
1.1       kristaps  295: }
                    296:
                    297:
                    298: int
1.31      kristaps  299: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    300:                const char *fmt, ...)
1.1       kristaps  301: {
1.31      kristaps  302:        char             buf[256];
                    303:        va_list          ap;
1.1       kristaps  304:
                    305:        if (NULL == mdoc->cb.mdoc_err)
                    306:                return(0);
1.31      kristaps  307:
                    308:        va_start(ap, fmt);
                    309:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    310:        va_end(ap);
1.88      kristaps  311:
1.31      kristaps  312:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1       kristaps  313: }
                    314:
                    315:
                    316: int
1.87      kristaps  317: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1       kristaps  318: {
1.31      kristaps  319:        char             buf[256];
                    320:        va_list          ap;
1.1       kristaps  321:
                    322:        if (NULL == mdoc->cb.mdoc_warn)
                    323:                return(0);
1.31      kristaps  324:
                    325:        va_start(ap, fmt);
                    326:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    327:        va_end(ap);
1.88      kristaps  328:
1.87      kristaps  329:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, buf));
1.1       kristaps  330: }
                    331:
                    332:
                    333: int
1.88      kristaps  334: mdoc_err(struct mdoc *m, int line, int pos, int iserr, enum merr type)
1.78      kristaps  335: {
1.89      kristaps  336:        const char      *p;
1.88      kristaps  337:
1.89      kristaps  338:        p = __mdoc_merrnames[(int)type];
1.73      kristaps  339:        assert(p);
1.88      kristaps  340:
                    341:        if (iserr)
                    342:                return(mdoc_verr(m, line, pos, p));
                    343:
                    344:        return(mdoc_vwarn(m, line, pos, p));
                    345: }
                    346:
                    347:
                    348: int
1.117     kristaps  349: mdoc_macro(struct mdoc *m, enum mdoct tok,
1.88      kristaps  350:                int ln, int pp, int *pos, char *buf)
                    351: {
1.122     kristaps  352:        assert(tok < MDOC_MAX);
                    353:
                    354:        /* If we're in the body, deny prologue calls. */
1.117     kristaps  355:
1.88      kristaps  356:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.122     kristaps  357:                        MDOC_PBODY & m->flags)
                    358:                return(mdoc_perr(m, ln, pp, EPROLBODY));
                    359:
                    360:        /* If we're in the prologue, deny "body" macros.  */
                    361:
                    362:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
                    363:                        ! (MDOC_PBODY & m->flags)) {
1.120     kristaps  364:                if ( ! mdoc_pwarn(m, ln, pp, EBODYPROL))
                    365:                        return(0);
                    366:                if (NULL == m->meta.title)
                    367:                        m->meta.title = mandoc_strdup("unknown");
                    368:                if (NULL == m->meta.vol)
                    369:                        m->meta.vol = mandoc_strdup("local");
                    370:                if (NULL == m->meta.os)
                    371:                        m->meta.os = mandoc_strdup("local");
                    372:                if (0 == m->meta.date)
                    373:                        m->meta.date = time(NULL);
                    374:                m->flags |= MDOC_PBODY;
                    375:        }
1.88      kristaps  376:
                    377:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.73      kristaps  378: }
                    379:
                    380:
                    381: static int
                    382: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  383: {
                    384:
1.25      kristaps  385:        assert(mdoc->last);
                    386:        assert(mdoc->first);
                    387:        assert(MDOC_ROOT != p->type);
1.1       kristaps  388:
1.13      kristaps  389:        switch (mdoc->next) {
                    390:        case (MDOC_NEXT_SIBLING):
1.6       kristaps  391:                mdoc->last->next = p;
                    392:                p->prev = mdoc->last;
1.13      kristaps  393:                p->parent = mdoc->last->parent;
1.1       kristaps  394:                break;
1.13      kristaps  395:        case (MDOC_NEXT_CHILD):
                    396:                mdoc->last->child = p;
1.1       kristaps  397:                p->parent = mdoc->last;
                    398:                break;
                    399:        default:
1.13      kristaps  400:                abort();
                    401:                /* NOTREACHED */
1.1       kristaps  402:        }
                    403:
1.86      kristaps  404:        p->parent->nchild++;
                    405:
1.23      kristaps  406:        if ( ! mdoc_valid_pre(mdoc, p))
                    407:                return(0);
1.68      kristaps  408:        if ( ! mdoc_action_pre(mdoc, p))
                    409:                return(0);
1.27      kristaps  410:
                    411:        switch (p->type) {
                    412:        case (MDOC_HEAD):
                    413:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  414:                p->parent->head = p;
1.27      kristaps  415:                break;
                    416:        case (MDOC_TAIL):
                    417:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  418:                p->parent->tail = p;
1.27      kristaps  419:                break;
                    420:        case (MDOC_BODY):
                    421:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  422:                p->parent->body = p;
1.27      kristaps  423:                break;
                    424:        default:
                    425:                break;
                    426:        }
                    427:
1.1       kristaps  428:        mdoc->last = p;
1.71      kristaps  429:
                    430:        switch (p->type) {
                    431:        case (MDOC_TEXT):
                    432:                if ( ! mdoc_valid_post(mdoc))
                    433:                        return(0);
                    434:                if ( ! mdoc_action_post(mdoc))
                    435:                        return(0);
                    436:                break;
                    437:        default:
                    438:                break;
                    439:        }
                    440:
1.23      kristaps  441:        return(1);
1.1       kristaps  442: }
                    443:
                    444:
1.45      kristaps  445: static struct mdoc_node *
1.117     kristaps  446: node_alloc(struct mdoc *m, int line, int pos,
                    447:                enum mdoct tok, enum mdoc_type type)
1.45      kristaps  448: {
1.46      kristaps  449:        struct mdoc_node *p;
                    450:
1.113     kristaps  451:        p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.91      kristaps  452:        p->sec = m->lastsec;
1.73      kristaps  453:        p->line = line;
                    454:        p->pos = pos;
                    455:        p->tok = tok;
1.118     kristaps  456:        p->type = type;
1.130     kristaps  457:        if (MDOC_NEWLINE & m->flags)
                    458:                p->flags |= MDOC_LINE;
                    459:        m->flags &= ~MDOC_NEWLINE;
1.46      kristaps  460:        return(p);
1.45      kristaps  461: }
                    462:
                    463:
1.23      kristaps  464: int
1.117     kristaps  465: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.17      kristaps  466: {
                    467:        struct mdoc_node *p;
                    468:
1.91      kristaps  469:        p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.102     kristaps  470:        if ( ! node_append(m, p))
                    471:                return(0);
                    472:        m->next = MDOC_NEXT_CHILD;
                    473:        return(1);
1.17      kristaps  474: }
                    475:
                    476:
1.23      kristaps  477: int
1.117     kristaps  478: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  479: {
                    480:        struct mdoc_node *p;
                    481:
1.91      kristaps  482:        assert(m->first);
                    483:        assert(m->last);
1.1       kristaps  484:
1.91      kristaps  485:        p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.102     kristaps  486:        if ( ! node_append(m, p))
                    487:                return(0);
                    488:        m->next = MDOC_NEXT_CHILD;
                    489:        return(1);
1.1       kristaps  490: }
                    491:
                    492:
1.23      kristaps  493: int
1.117     kristaps  494: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  495: {
                    496:        struct mdoc_node *p;
                    497:
1.91      kristaps  498:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.102     kristaps  499:        if ( ! node_append(m, p))
                    500:                return(0);
                    501:        m->next = MDOC_NEXT_CHILD;
                    502:        return(1);
1.1       kristaps  503: }
                    504:
                    505:
1.23      kristaps  506: int
1.91      kristaps  507: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.117     kristaps  508:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  509: {
                    510:        struct mdoc_node *p;
                    511:
1.91      kristaps  512:        p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.77      kristaps  513:        p->args = args;
                    514:        if (p->args)
1.53      kristaps  515:                (args->refcnt)++;
1.102     kristaps  516:        if ( ! node_append(m, p))
                    517:                return(0);
                    518:        m->next = MDOC_NEXT_CHILD;
                    519:        return(1);
1.1       kristaps  520: }
                    521:
                    522:
1.23      kristaps  523: int
1.91      kristaps  524: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.117     kristaps  525:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  526: {
                    527:        struct mdoc_node *p;
                    528:
1.91      kristaps  529:        p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.77      kristaps  530:        p->args = args;
                    531:        if (p->args)
1.53      kristaps  532:                (args->refcnt)++;
1.102     kristaps  533:        if ( ! node_append(m, p))
                    534:                return(0);
                    535:        m->next = MDOC_NEXT_CHILD;
                    536:        return(1);
1.1       kristaps  537: }
                    538:
                    539:
1.124     kristaps  540: int
                    541: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1       kristaps  542: {
1.91      kristaps  543:        struct mdoc_node *n;
1.124     kristaps  544:        size_t            sv, len;
                    545:
                    546:        len = strlen(p);
1.1       kristaps  547:
1.125     kristaps  548:        n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.113     kristaps  549:        n->string = mandoc_malloc(len + 1);
1.91      kristaps  550:        sv = strlcpy(n->string, p, len + 1);
                    551:
                    552:        /* Prohibit truncation. */
                    553:        assert(sv < len + 1);
                    554:
1.101     kristaps  555:        if ( ! node_append(m, n))
                    556:                return(0);
1.124     kristaps  557:
1.101     kristaps  558:        m->next = MDOC_NEXT_SIBLING;
                    559:        return(1);
1.91      kristaps  560: }
                    561:
                    562:
1.53      kristaps  563: void
                    564: mdoc_node_free(struct mdoc_node *p)
1.1       kristaps  565: {
                    566:
1.53      kristaps  567:        if (p->string)
                    568:                free(p->string);
                    569:        if (p->args)
                    570:                mdoc_argv_free(p->args);
1.1       kristaps  571:        free(p);
                    572: }
                    573:
                    574:
1.121     kristaps  575: static void
                    576: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
                    577: {
                    578:
                    579:        /* Adjust siblings. */
                    580:
                    581:        if (n->prev)
                    582:                n->prev->next = n->next;
                    583:        if (n->next)
                    584:                n->next->prev = n->prev;
                    585:
                    586:        /* Adjust parent. */
                    587:
                    588:        if (n->parent) {
                    589:                n->parent->nchild--;
                    590:                if (n->parent->child == n)
                    591:                        n->parent->child = n->prev ? n->prev : n->next;
                    592:        }
                    593:
                    594:        /* Adjust parse point, if applicable. */
                    595:
                    596:        if (m && m->last == n) {
                    597:                if (n->prev) {
                    598:                        m->last = n->prev;
                    599:                        m->next = MDOC_NEXT_SIBLING;
                    600:                } else {
                    601:                        m->last = n->parent;
                    602:                        m->next = MDOC_NEXT_CHILD;
                    603:                }
                    604:        }
                    605:
                    606:        if (m && m->first == n)
                    607:                m->first = NULL;
                    608: }
                    609:
                    610:
1.53      kristaps  611: void
1.121     kristaps  612: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1       kristaps  613: {
                    614:
1.121     kristaps  615:        while (p->child) {
                    616:                assert(p->nchild);
                    617:                mdoc_node_delete(m, p->child);
                    618:        }
                    619:        assert(0 == p->nchild);
1.1       kristaps  620:
1.121     kristaps  621:        mdoc_node_unlink(m, p);
1.53      kristaps  622:        mdoc_node_free(p);
1.1       kristaps  623: }
                    624:
                    625:
1.53      kristaps  626: /*
                    627:  * Parse free-form text, that is, a line that does not begin with the
                    628:  * control character.
                    629:  */
                    630: static int
1.123     kristaps  631: mdoc_ptext(struct mdoc *m, int line, char *buf)
1.1       kristaps  632: {
1.124     kristaps  633:        int              i;
1.1       kristaps  634:
1.123     kristaps  635:        /* Ignore bogus comments. */
                    636:
                    637:        if ('\\' == buf[0] && '.' == buf[1] && '\"' == buf[2])
                    638:                return(mdoc_pwarn(m, line, 0, EBADCOMMENT));
                    639:
1.124     kristaps  640:        /* No text before an initial macro. */
                    641:
1.85      kristaps  642:        if (SEC_NONE == m->lastnamed)
1.88      kristaps  643:                return(mdoc_perr(m, line, 0, ETEXTPROL));
1.124     kristaps  644:
                    645:        /* Literal just gets pulled in as-is. */
1.91      kristaps  646:
1.101     kristaps  647:        if (MDOC_LITERAL & m->flags)
                    648:                return(mdoc_word_alloc(m, line, 0, buf));
1.91      kristaps  649:
1.124     kristaps  650:        /* Check for a blank line, which may also consist of spaces. */
1.1       kristaps  651:
1.91      kristaps  652:        for (i = 0; ' ' == buf[i]; i++)
1.124     kristaps  653:                /* Skip to first non-space. */ ;
1.115     kristaps  654:
1.119     kristaps  655:        if ('\0' == buf[i]) {
                    656:                if ( ! mdoc_pwarn(m, line, 0, ENOBLANK))
                    657:                        return(0);
1.124     kristaps  658:
1.119     kristaps  659:                /*
1.124     kristaps  660:                 * Insert a `Pp' in the case of a blank line.  Technically,
                    661:                 * blank lines aren't allowed, but enough manuals assume this
                    662:                 * behaviour that we want to work around it.
1.119     kristaps  663:                 */
                    664:                if ( ! mdoc_elem_alloc(m, line, 0, MDOC_Pp, NULL))
                    665:                        return(0);
1.124     kristaps  666:
                    667:                m->next = MDOC_NEXT_SIBLING;
                    668:                return(1);
1.119     kristaps  669:        }
1.68      kristaps  670:
1.126     kristaps  671:        /*
                    672:         * Warn if the last un-escaped character is whitespace. Then
                    673:         * strip away the remaining spaces (tabs stay!).
                    674:         */
1.91      kristaps  675:
1.124     kristaps  676:        i = (int)strlen(buf);
                    677:        assert(i);
1.116     kristaps  678:
1.126     kristaps  679:        if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.127     kristaps  680:                if (i > 1 && '\\' != buf[i - 2])
1.116     kristaps  681:                        if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
                    682:                                return(0);
1.126     kristaps  683:
                    684:                for (--i; i && ' ' == buf[i]; i--)
                    685:                        /* Spin back to non-space. */ ;
                    686:
                    687:                /* Jump ahead of escaped whitespace. */
                    688:                i += '\\' == buf[i] ? 2 : 1;
                    689:
                    690:                buf[i] = '\0';
                    691:        }
1.116     kristaps  692:
1.124     kristaps  693:        /* Allocate the whole word. */
1.1       kristaps  694:
1.128     kristaps  695:        if ( ! mdoc_word_alloc(m, line, 0, buf))
                    696:                return(0);
                    697:
                    698:        /*
                    699:         * End-of-sentence check.  If the last character is an unescaped
                    700:         * EOS character, then flag the node as being the end of a
                    701:         * sentence.  The front-end will know how to interpret this.
                    702:         */
                    703:
                    704:        assert(i);
                    705:
1.129     kristaps  706:        if (mandoc_eos(buf, (size_t)i))
1.128     kristaps  707:                m->last->flags |= MDOC_EOS;
                    708:
                    709:        return(1);
1.1       kristaps  710: }
                    711:
                    712:
1.58      kristaps  713: static int
                    714: macrowarn(struct mdoc *m, int ln, const char *buf)
                    715: {
                    716:        if ( ! (MDOC_IGN_MACRO & m->pflags))
1.125     kristaps  717:                return(mdoc_verr(m, ln, 0, "unknown macro: %s%s",
1.59      kristaps  718:                                buf, strlen(buf) > 3 ? "..." : ""));
1.96      kristaps  719:        return(mdoc_vwarn(m, ln, 0, "unknown macro: %s%s",
1.59      kristaps  720:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  721: }
                    722:
                    723:
1.53      kristaps  724: /*
                    725:  * Parse a macro line, that is, a line beginning with the control
                    726:  * character.
                    727:  */
                    728: int
1.123     kristaps  729: mdoc_pmacro(struct mdoc *m, int ln, char *buf)
1.1       kristaps  730: {
1.125     kristaps  731:        enum mdoct      tok;
1.130     kristaps  732:        int             i, j, sv;
1.125     kristaps  733:        char            mac[5];
1.1       kristaps  734:
1.81      kristaps  735:        /* Empty lines are ignored. */
1.63      kristaps  736:
1.115     kristaps  737:        if ('\0' == buf[1])
1.63      kristaps  738:                return(1);
                    739:
1.100     kristaps  740:        i = 1;
                    741:
                    742:        /* Accept whitespace after the initial control char. */
                    743:
                    744:        if (' ' == buf[i]) {
                    745:                i++;
1.65      kristaps  746:                while (buf[i] && ' ' == buf[i])
1.63      kristaps  747:                        i++;
1.115     kristaps  748:                if ('\0' == buf[i])
1.63      kristaps  749:                        return(1);
                    750:        }
1.1       kristaps  751:
1.130     kristaps  752:        sv = i;
                    753:
1.53      kristaps  754:        /* Copy the first word into a nil-terminated buffer. */
1.1       kristaps  755:
1.100     kristaps  756:        for (j = 0; j < 4; j++, i++) {
1.115     kristaps  757:                if ('\0' == (mac[j] = buf[i]))
1.53      kristaps  758:                        break;
1.65      kristaps  759:                else if (' ' == buf[i])
1.53      kristaps  760:                        break;
1.104     kristaps  761:
                    762:                /* Check for invalid characters. */
                    763:
                    764:                if (isgraph((u_char)buf[i]))
                    765:                        continue;
                    766:                return(mdoc_perr(m, ln, i, EPRINT));
1.53      kristaps  767:        }
1.1       kristaps  768:
1.100     kristaps  769:        mac[j] = 0;
1.1       kristaps  770:
1.100     kristaps  771:        if (j == 4 || j < 2) {
1.58      kristaps  772:                if ( ! macrowarn(m, ln, mac))
                    773:                        goto err;
                    774:                return(1);
1.53      kristaps  775:        }
                    776:
1.125     kristaps  777:        if (MDOC_MAX == (tok = mdoc_hash_find(mac))) {
1.58      kristaps  778:                if ( ! macrowarn(m, ln, mac))
                    779:                        goto err;
                    780:                return(1);
1.53      kristaps  781:        }
1.1       kristaps  782:
1.53      kristaps  783:        /* The macro is sane.  Jump to the next word. */
1.1       kristaps  784:
1.65      kristaps  785:        while (buf[i] && ' ' == buf[i])
1.53      kristaps  786:                i++;
1.1       kristaps  787:
1.125     kristaps  788:        /*
                    789:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    790:         * into the parser as "text", so we only warn about spaces here.
                    791:         */
1.115     kristaps  792:
                    793:        if ('\0' == buf[i] && ' ' == buf[i - 1])
                    794:                if ( ! mdoc_pwarn(m, ln, i - 1, ETAILWS))
                    795:                        goto err;
                    796:
1.98      kristaps  797:        /*
                    798:         * Begin recursive parse sequence.  Since we're at the start of
                    799:         * the line, we don't need to do callable/parseable checks.
                    800:         */
1.130     kristaps  801:        if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.53      kristaps  802:                goto err;
1.1       kristaps  803:
1.53      kristaps  804:        return(1);
1.1       kristaps  805:
1.53      kristaps  806: err:   /* Error out. */
1.1       kristaps  807:
1.53      kristaps  808:        m->flags |= MDOC_HALT;
                    809:        return(0);
1.1       kristaps  810: }
1.100     kristaps  811:
                    812:

CVSweb