[BACK]Return to mdoc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mdoc.c, Revision 1.53

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