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

Annotation of mandoc/mdoc.c, Revision 1.128

1.128   ! kristaps    1: /*     $Id: mdoc.c,v 1.127 2010/05/08 10:25:27 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.123     kristaps   87:        "deprecated comment style", /* EBADCOMMENT */
1.89      kristaps   88: };
                     89:
1.1       kristaps   90: const  char *const __mdoc_macronames[MDOC_MAX] = {
1.82      kristaps   91:        "Ap",           "Dd",           "Dt",           "Os",
1.1       kristaps   92:        "Sh",           "Ss",           "Pp",           "D1",
                     93:        "Dl",           "Bd",           "Ed",           "Bl",
                     94:        "El",           "It",           "Ad",           "An",
                     95:        "Ar",           "Cd",           "Cm",           "Dv",
                     96:        "Er",           "Ev",           "Ex",           "Fa",
                     97:        "Fd",           "Fl",           "Fn",           "Ft",
                     98:        "Ic",           "In",           "Li",           "Nd",
                     99:        "Nm",           "Op",           "Ot",           "Pa",
                    100:        "Rv",           "St",           "Va",           "Vt",
                    101:        /* LINTED */
1.114     kristaps  102:        "Xr",           "%A",           "%B",           "%D",
1.1       kristaps  103:        /* LINTED */
1.114     kristaps  104:        "%I",           "%J",           "%N",           "%O",
1.1       kristaps  105:        /* LINTED */
1.114     kristaps  106:        "%P",           "%R",           "%T",           "%V",
1.1       kristaps  107:        "Ac",           "Ao",           "Aq",           "At",
                    108:        "Bc",           "Bf",           "Bo",           "Bq",
                    109:        "Bsx",          "Bx",           "Db",           "Dc",
                    110:        "Do",           "Dq",           "Ec",           "Ef",
                    111:        "Em",           "Eo",           "Fx",           "Ms",
                    112:        "No",           "Ns",           "Nx",           "Ox",
                    113:        "Pc",           "Pf",           "Po",           "Pq",
                    114:        "Qc",           "Ql",           "Qo",           "Qq",
                    115:        "Re",           "Rs",           "Sc",           "So",
                    116:        "Sq",           "Sm",           "Sx",           "Sy",
                    117:        "Tn",           "Ux",           "Xc",           "Xo",
                    118:        "Fo",           "Fc",           "Oo",           "Oc",
                    119:        "Bk",           "Ek",           "Bt",           "Hf",
1.82      kristaps  120:        "Fr",           "Ud",           "Lb",           "Lp",
                    121:        "Lk",           "Mt",           "Brq",          "Bro",
1.64      kristaps  122:        /* LINTED */
1.114     kristaps  123:        "Brc",          "%C",           "Es",           "En",
1.69      kristaps  124:        /* LINTED */
1.114     kristaps  125:        "Dx",           "%Q",           "br",           "sp",
1.110     kristaps  126:        /* LINTED */
1.114     kristaps  127:        "%U"
1.1       kristaps  128:        };
                    129:
                    130: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                    131:        "split",                "nosplit",              "ragged",
                    132:        "unfilled",             "literal",              "file",
                    133:        "offset",               "bullet",               "dash",
                    134:        "hyphen",               "item",                 "enum",
                    135:        "tag",                  "diag",                 "hang",
                    136:        "ohang",                "inset",                "column",
                    137:        "width",                "compact",              "std",
1.52      kristaps  138:        "filled",               "words",                "emphasis",
1.108     kristaps  139:        "symbolic",             "nested",               "centered"
1.1       kristaps  140:        };
                    141:
                    142: const  char * const *mdoc_macronames = __mdoc_macronames;
                    143: const  char * const *mdoc_argnames = __mdoc_argnames;
                    144:
1.121     kristaps  145: static void              mdoc_node_free(struct mdoc_node *);
                    146: static void              mdoc_node_unlink(struct mdoc *,
                    147:                                struct mdoc_node *);
1.73      kristaps  148: static void              mdoc_free1(struct mdoc *);
1.113     kristaps  149: static void              mdoc_alloc1(struct mdoc *);
1.73      kristaps  150: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.117     kristaps  151:                                enum mdoct, enum mdoc_type);
1.73      kristaps  152: static int               node_append(struct mdoc *,
1.71      kristaps  153:                                struct mdoc_node *);
1.123     kristaps  154: static int               mdoc_ptext(struct mdoc *, int, char *);
                    155: static int               mdoc_pmacro(struct mdoc *, int, char *);
1.71      kristaps  156: static int               macrowarn(struct mdoc *, int, const char *);
1.124     kristaps  157:
1.88      kristaps  158:
1.1       kristaps  159: const struct mdoc_node *
1.71      kristaps  160: mdoc_node(const struct mdoc *m)
1.1       kristaps  161: {
                    162:
1.71      kristaps  163:        return(MDOC_HALT & m->flags ? NULL : m->first);
1.1       kristaps  164: }
                    165:
                    166:
1.37      kristaps  167: const struct mdoc_meta *
1.71      kristaps  168: mdoc_meta(const struct mdoc *m)
1.37      kristaps  169: {
                    170:
1.71      kristaps  171:        return(MDOC_HALT & m->flags ? NULL : &m->meta);
1.37      kristaps  172: }
                    173:
                    174:
1.85      kristaps  175: /*
                    176:  * Frees volatile resources (parse tree, meta-data, fields).
                    177:  */
1.73      kristaps  178: static void
                    179: mdoc_free1(struct mdoc *mdoc)
1.67      kristaps  180: {
                    181:
                    182:        if (mdoc->first)
1.121     kristaps  183:                mdoc_node_delete(mdoc, mdoc->first);
1.67      kristaps  184:        if (mdoc->meta.title)
                    185:                free(mdoc->meta.title);
                    186:        if (mdoc->meta.os)
                    187:                free(mdoc->meta.os);
                    188:        if (mdoc->meta.name)
                    189:                free(mdoc->meta.name);
                    190:        if (mdoc->meta.arch)
                    191:                free(mdoc->meta.arch);
                    192:        if (mdoc->meta.vol)
                    193:                free(mdoc->meta.vol);
1.73      kristaps  194: }
                    195:
                    196:
1.85      kristaps  197: /*
                    198:  * Allocate all volatile resources (parse tree, meta-data, fields).
                    199:  */
1.113     kristaps  200: static void
1.73      kristaps  201: mdoc_alloc1(struct mdoc *mdoc)
                    202: {
1.67      kristaps  203:
1.112     kristaps  204:        memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.67      kristaps  205:        mdoc->flags = 0;
1.85      kristaps  206:        mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.113     kristaps  207:        mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.70      kristaps  208:        mdoc->first = mdoc->last;
1.67      kristaps  209:        mdoc->last->type = MDOC_ROOT;
                    210:        mdoc->next = MDOC_NEXT_CHILD;
1.73      kristaps  211: }
                    212:
                    213:
                    214: /*
1.85      kristaps  215:  * Free up volatile resources (see mdoc_free1()) then re-initialises the
                    216:  * data with mdoc_alloc1().  After invocation, parse data has been reset
                    217:  * and the parser is ready for re-invocation on a new tree; however,
                    218:  * cross-parse non-volatile data is kept intact.
1.73      kristaps  219:  */
1.113     kristaps  220: void
1.73      kristaps  221: mdoc_reset(struct mdoc *mdoc)
                    222: {
                    223:
                    224:        mdoc_free1(mdoc);
1.113     kristaps  225:        mdoc_alloc1(mdoc);
1.67      kristaps  226: }
                    227:
                    228:
1.68      kristaps  229: /*
1.85      kristaps  230:  * Completely free up all volatile and non-volatile parse resources.
                    231:  * After invocation, the pointer is no longer usable.
1.68      kristaps  232:  */
1.67      kristaps  233: void
1.38      kristaps  234: mdoc_free(struct mdoc *mdoc)
1.34      kristaps  235: {
                    236:
1.73      kristaps  237:        mdoc_free1(mdoc);
1.1       kristaps  238:        free(mdoc);
                    239: }
                    240:
                    241:
1.85      kristaps  242: /*
                    243:  * Allocate volatile and non-volatile parse resources.
                    244:  */
1.1       kristaps  245: struct mdoc *
1.55      kristaps  246: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1       kristaps  247: {
                    248:        struct mdoc     *p;
                    249:
1.113     kristaps  250:        p = mandoc_calloc(1, sizeof(struct mdoc));
                    251:
1.74      kristaps  252:        if (cb)
1.113     kristaps  253:                memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.105     kristaps  254:
1.1       kristaps  255:        p->data = data;
1.73      kristaps  256:        p->pflags = pflags;
                    257:
1.113     kristaps  258:        mdoc_hash_init();
                    259:        mdoc_alloc1(p);
                    260:        return(p);
1.1       kristaps  261: }
                    262:
                    263:
1.68      kristaps  264: /*
                    265:  * Climb back up the parse tree, validating open scopes.  Mostly calls
1.85      kristaps  266:  * through to macro_end() in macro.c.
1.68      kristaps  267:  */
1.1       kristaps  268: int
1.72      kristaps  269: mdoc_endparse(struct mdoc *m)
1.20      kristaps  270: {
                    271:
1.72      kristaps  272:        if (MDOC_HALT & m->flags)
1.20      kristaps  273:                return(0);
1.72      kristaps  274:        else if (mdoc_macroend(m))
1.20      kristaps  275:                return(1);
1.72      kristaps  276:        m->flags |= MDOC_HALT;
                    277:        return(0);
1.20      kristaps  278: }
                    279:
                    280:
1.50      kristaps  281: /*
1.53      kristaps  282:  * Main parse routine.  Parses a single line -- really just hands off to
1.123     kristaps  283:  * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50      kristaps  284:  */
1.20      kristaps  285: int
1.53      kristaps  286: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1       kristaps  287: {
                    288:
1.53      kristaps  289:        if (MDOC_HALT & m->flags)
1.20      kristaps  290:                return(0);
1.50      kristaps  291:
1.123     kristaps  292:        return('.' == *buf ? mdoc_pmacro(m, ln, buf) :
                    293:                        mdoc_ptext(m, ln, buf));
1.1       kristaps  294: }
                    295:
                    296:
                    297: int
1.31      kristaps  298: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    299:                const char *fmt, ...)
1.1       kristaps  300: {
1.31      kristaps  301:        char             buf[256];
                    302:        va_list          ap;
1.1       kristaps  303:
                    304:        if (NULL == mdoc->cb.mdoc_err)
                    305:                return(0);
1.31      kristaps  306:
                    307:        va_start(ap, fmt);
                    308:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    309:        va_end(ap);
1.88      kristaps  310:
1.31      kristaps  311:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1       kristaps  312: }
                    313:
                    314:
                    315: int
1.87      kristaps  316: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1       kristaps  317: {
1.31      kristaps  318:        char             buf[256];
                    319:        va_list          ap;
1.1       kristaps  320:
                    321:        if (NULL == mdoc->cb.mdoc_warn)
                    322:                return(0);
1.31      kristaps  323:
                    324:        va_start(ap, fmt);
                    325:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    326:        va_end(ap);
1.88      kristaps  327:
1.87      kristaps  328:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, buf));
1.1       kristaps  329: }
                    330:
                    331:
                    332: int
1.88      kristaps  333: mdoc_err(struct mdoc *m, int line, int pos, int iserr, enum merr type)
1.78      kristaps  334: {
1.89      kristaps  335:        const char      *p;
1.88      kristaps  336:
1.89      kristaps  337:        p = __mdoc_merrnames[(int)type];
1.73      kristaps  338:        assert(p);
1.88      kristaps  339:
                    340:        if (iserr)
                    341:                return(mdoc_verr(m, line, pos, p));
                    342:
                    343:        return(mdoc_vwarn(m, line, pos, p));
                    344: }
                    345:
                    346:
                    347: int
1.117     kristaps  348: mdoc_macro(struct mdoc *m, enum mdoct tok,
1.88      kristaps  349:                int ln, int pp, int *pos, char *buf)
                    350: {
1.122     kristaps  351:        assert(tok < MDOC_MAX);
                    352:
                    353:        /* If we're in the body, deny prologue calls. */
1.117     kristaps  354:
1.88      kristaps  355:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.122     kristaps  356:                        MDOC_PBODY & m->flags)
                    357:                return(mdoc_perr(m, ln, pp, EPROLBODY));
                    358:
                    359:        /* If we're in the prologue, deny "body" macros.  */
                    360:
                    361:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
                    362:                        ! (MDOC_PBODY & m->flags)) {
1.120     kristaps  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.88      kristaps  375:
                    376:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.73      kristaps  377: }
                    378:
                    379:
                    380: static int
                    381: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  382: {
                    383:
1.25      kristaps  384:        assert(mdoc->last);
                    385:        assert(mdoc->first);
                    386:        assert(MDOC_ROOT != p->type);
1.1       kristaps  387:
1.13      kristaps  388:        switch (mdoc->next) {
                    389:        case (MDOC_NEXT_SIBLING):
1.6       kristaps  390:                mdoc->last->next = p;
                    391:                p->prev = mdoc->last;
1.13      kristaps  392:                p->parent = mdoc->last->parent;
1.1       kristaps  393:                break;
1.13      kristaps  394:        case (MDOC_NEXT_CHILD):
                    395:                mdoc->last->child = p;
1.1       kristaps  396:                p->parent = mdoc->last;
                    397:                break;
                    398:        default:
1.13      kristaps  399:                abort();
                    400:                /* NOTREACHED */
1.1       kristaps  401:        }
                    402:
1.86      kristaps  403:        p->parent->nchild++;
                    404:
1.23      kristaps  405:        if ( ! mdoc_valid_pre(mdoc, p))
                    406:                return(0);
1.68      kristaps  407:        if ( ! mdoc_action_pre(mdoc, p))
                    408:                return(0);
1.27      kristaps  409:
                    410:        switch (p->type) {
                    411:        case (MDOC_HEAD):
                    412:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  413:                p->parent->head = p;
1.27      kristaps  414:                break;
                    415:        case (MDOC_TAIL):
                    416:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  417:                p->parent->tail = p;
1.27      kristaps  418:                break;
                    419:        case (MDOC_BODY):
                    420:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  421:                p->parent->body = p;
1.27      kristaps  422:                break;
                    423:        default:
                    424:                break;
                    425:        }
                    426:
1.1       kristaps  427:        mdoc->last = p;
1.71      kristaps  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:
1.23      kristaps  440:        return(1);
1.1       kristaps  441: }
                    442:
                    443:
1.45      kristaps  444: static struct mdoc_node *
1.117     kristaps  445: node_alloc(struct mdoc *m, int line, int pos,
                    446:                enum mdoct tok, enum mdoc_type type)
1.45      kristaps  447: {
1.46      kristaps  448:        struct mdoc_node *p;
                    449:
1.113     kristaps  450:        p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.91      kristaps  451:        p->sec = m->lastsec;
1.73      kristaps  452:        p->line = line;
                    453:        p->pos = pos;
                    454:        p->tok = tok;
1.118     kristaps  455:        p->type = type;
1.45      kristaps  456:
1.46      kristaps  457:        return(p);
1.45      kristaps  458: }
                    459:
                    460:
1.23      kristaps  461: int
1.117     kristaps  462: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.17      kristaps  463: {
                    464:        struct mdoc_node *p;
                    465:
1.91      kristaps  466:        p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.102     kristaps  467:        if ( ! node_append(m, p))
                    468:                return(0);
                    469:        m->next = MDOC_NEXT_CHILD;
                    470:        return(1);
1.17      kristaps  471: }
                    472:
                    473:
1.23      kristaps  474: int
1.117     kristaps  475: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  476: {
                    477:        struct mdoc_node *p;
                    478:
1.91      kristaps  479:        assert(m->first);
                    480:        assert(m->last);
1.1       kristaps  481:
1.91      kristaps  482:        p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.102     kristaps  483:        if ( ! node_append(m, p))
                    484:                return(0);
                    485:        m->next = MDOC_NEXT_CHILD;
                    486:        return(1);
1.1       kristaps  487: }
                    488:
                    489:
1.23      kristaps  490: int
1.117     kristaps  491: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  492: {
                    493:        struct mdoc_node *p;
                    494:
1.91      kristaps  495:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.102     kristaps  496:        if ( ! node_append(m, p))
                    497:                return(0);
                    498:        m->next = MDOC_NEXT_CHILD;
                    499:        return(1);
1.1       kristaps  500: }
                    501:
                    502:
1.23      kristaps  503: int
1.91      kristaps  504: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.117     kristaps  505:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  506: {
                    507:        struct mdoc_node *p;
                    508:
1.91      kristaps  509:        p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.77      kristaps  510:        p->args = args;
                    511:        if (p->args)
1.53      kristaps  512:                (args->refcnt)++;
1.102     kristaps  513:        if ( ! node_append(m, p))
                    514:                return(0);
                    515:        m->next = MDOC_NEXT_CHILD;
                    516:        return(1);
1.1       kristaps  517: }
                    518:
                    519:
1.23      kristaps  520: int
1.91      kristaps  521: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.117     kristaps  522:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  523: {
                    524:        struct mdoc_node *p;
                    525:
1.91      kristaps  526:        p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.77      kristaps  527:        p->args = args;
                    528:        if (p->args)
1.53      kristaps  529:                (args->refcnt)++;
1.102     kristaps  530:        if ( ! node_append(m, p))
                    531:                return(0);
                    532:        m->next = MDOC_NEXT_CHILD;
                    533:        return(1);
1.1       kristaps  534: }
                    535:
                    536:
1.124     kristaps  537: int
                    538: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1       kristaps  539: {
1.91      kristaps  540:        struct mdoc_node *n;
1.124     kristaps  541:        size_t            sv, len;
                    542:
                    543:        len = strlen(p);
1.1       kristaps  544:
1.125     kristaps  545:        n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.113     kristaps  546:        n->string = mandoc_malloc(len + 1);
1.91      kristaps  547:        sv = strlcpy(n->string, p, len + 1);
                    548:
                    549:        /* Prohibit truncation. */
                    550:        assert(sv < len + 1);
                    551:
1.101     kristaps  552:        if ( ! node_append(m, n))
                    553:                return(0);
1.124     kristaps  554:
1.101     kristaps  555:        m->next = MDOC_NEXT_SIBLING;
                    556:        return(1);
1.91      kristaps  557: }
                    558:
                    559:
1.53      kristaps  560: void
                    561: mdoc_node_free(struct mdoc_node *p)
1.1       kristaps  562: {
                    563:
1.53      kristaps  564:        if (p->string)
                    565:                free(p->string);
                    566:        if (p->args)
                    567:                mdoc_argv_free(p->args);
1.1       kristaps  568:        free(p);
                    569: }
                    570:
                    571:
1.121     kristaps  572: static void
                    573: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
                    574: {
                    575:
                    576:        /* Adjust siblings. */
                    577:
                    578:        if (n->prev)
                    579:                n->prev->next = n->next;
                    580:        if (n->next)
                    581:                n->next->prev = n->prev;
                    582:
                    583:        /* Adjust parent. */
                    584:
                    585:        if (n->parent) {
                    586:                n->parent->nchild--;
                    587:                if (n->parent->child == n)
                    588:                        n->parent->child = n->prev ? n->prev : n->next;
                    589:        }
                    590:
                    591:        /* Adjust parse point, if applicable. */
                    592:
                    593:        if (m && m->last == n) {
                    594:                if (n->prev) {
                    595:                        m->last = n->prev;
                    596:                        m->next = MDOC_NEXT_SIBLING;
                    597:                } else {
                    598:                        m->last = n->parent;
                    599:                        m->next = MDOC_NEXT_CHILD;
                    600:                }
                    601:        }
                    602:
                    603:        if (m && m->first == n)
                    604:                m->first = NULL;
                    605: }
                    606:
                    607:
1.53      kristaps  608: void
1.121     kristaps  609: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1       kristaps  610: {
                    611:
1.121     kristaps  612:        while (p->child) {
                    613:                assert(p->nchild);
                    614:                mdoc_node_delete(m, p->child);
                    615:        }
                    616:        assert(0 == p->nchild);
1.1       kristaps  617:
1.121     kristaps  618:        mdoc_node_unlink(m, p);
1.53      kristaps  619:        mdoc_node_free(p);
1.1       kristaps  620: }
                    621:
                    622:
1.53      kristaps  623: /*
                    624:  * Parse free-form text, that is, a line that does not begin with the
                    625:  * control character.
                    626:  */
                    627: static int
1.123     kristaps  628: mdoc_ptext(struct mdoc *m, int line, char *buf)
1.1       kristaps  629: {
1.124     kristaps  630:        int              i;
1.1       kristaps  631:
1.123     kristaps  632:        /* Ignore bogus comments. */
                    633:
                    634:        if ('\\' == buf[0] && '.' == buf[1] && '\"' == buf[2])
                    635:                return(mdoc_pwarn(m, line, 0, EBADCOMMENT));
                    636:
1.124     kristaps  637:        /* No text before an initial macro. */
                    638:
1.85      kristaps  639:        if (SEC_NONE == m->lastnamed)
1.88      kristaps  640:                return(mdoc_perr(m, line, 0, ETEXTPROL));
1.124     kristaps  641:
                    642:        /* Literal just gets pulled in as-is. */
1.91      kristaps  643:
1.101     kristaps  644:        if (MDOC_LITERAL & m->flags)
                    645:                return(mdoc_word_alloc(m, line, 0, buf));
1.91      kristaps  646:
1.124     kristaps  647:        /* Check for a blank line, which may also consist of spaces. */
1.1       kristaps  648:
1.91      kristaps  649:        for (i = 0; ' ' == buf[i]; i++)
1.124     kristaps  650:                /* Skip to first non-space. */ ;
1.115     kristaps  651:
1.119     kristaps  652:        if ('\0' == buf[i]) {
                    653:                if ( ! mdoc_pwarn(m, line, 0, ENOBLANK))
                    654:                        return(0);
1.124     kristaps  655:
1.119     kristaps  656:                /*
1.124     kristaps  657:                 * Insert a `Pp' in the case of a blank line.  Technically,
                    658:                 * blank lines aren't allowed, but enough manuals assume this
                    659:                 * behaviour that we want to work around it.
1.119     kristaps  660:                 */
                    661:                if ( ! mdoc_elem_alloc(m, line, 0, MDOC_Pp, NULL))
                    662:                        return(0);
1.124     kristaps  663:
                    664:                m->next = MDOC_NEXT_SIBLING;
                    665:                return(1);
1.119     kristaps  666:        }
1.68      kristaps  667:
1.126     kristaps  668:        /*
                    669:         * Warn if the last un-escaped character is whitespace. Then
                    670:         * strip away the remaining spaces (tabs stay!).
                    671:         */
1.91      kristaps  672:
1.124     kristaps  673:        i = (int)strlen(buf);
                    674:        assert(i);
1.116     kristaps  675:
1.126     kristaps  676:        if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.127     kristaps  677:                if (i > 1 && '\\' != buf[i - 2])
1.116     kristaps  678:                        if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
                    679:                                return(0);
1.126     kristaps  680:
                    681:                for (--i; i && ' ' == buf[i]; i--)
                    682:                        /* Spin back to non-space. */ ;
                    683:
                    684:                /* Jump ahead of escaped whitespace. */
                    685:                i += '\\' == buf[i] ? 2 : 1;
                    686:
                    687:                buf[i] = '\0';
                    688:        }
1.116     kristaps  689:
1.124     kristaps  690:        /* Allocate the whole word. */
1.1       kristaps  691:
1.128   ! kristaps  692:        if ( ! mdoc_word_alloc(m, line, 0, buf))
        !           693:                return(0);
        !           694:
        !           695:        /*
        !           696:         * End-of-sentence check.  If the last character is an unescaped
        !           697:         * EOS character, then flag the node as being the end of a
        !           698:         * sentence.  The front-end will know how to interpret this.
        !           699:         */
        !           700:
        !           701:        assert(i);
        !           702:
        !           703:        switch (buf[i - 1]) {
        !           704:        case ('.'):
        !           705:                if (i > 1 && '\\' == buf[i - 2])
        !           706:                        break;
        !           707:                /* FALLTHROUGH */
        !           708:        case ('!'):
        !           709:                /* FALLTHROUGH */
        !           710:        case ('?'):
        !           711:                m->last->flags |= MDOC_EOS;
        !           712:                break;
        !           713:        default:
        !           714:                break;
        !           715:
        !           716:        }
        !           717:
        !           718:        return(1);
1.1       kristaps  719: }
                    720:
                    721:
1.58      kristaps  722: static int
                    723: macrowarn(struct mdoc *m, int ln, const char *buf)
                    724: {
                    725:        if ( ! (MDOC_IGN_MACRO & m->pflags))
1.125     kristaps  726:                return(mdoc_verr(m, ln, 0, "unknown macro: %s%s",
1.59      kristaps  727:                                buf, strlen(buf) > 3 ? "..." : ""));
1.96      kristaps  728:        return(mdoc_vwarn(m, ln, 0, "unknown macro: %s%s",
1.59      kristaps  729:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  730: }
                    731:
                    732:
1.53      kristaps  733: /*
                    734:  * Parse a macro line, that is, a line beginning with the control
                    735:  * character.
                    736:  */
                    737: int
1.123     kristaps  738: mdoc_pmacro(struct mdoc *m, int ln, char *buf)
1.1       kristaps  739: {
1.125     kristaps  740:        enum mdoct      tok;
                    741:        int             i, j;
                    742:        char            mac[5];
1.1       kristaps  743:
1.81      kristaps  744:        /* Empty lines are ignored. */
1.63      kristaps  745:
1.115     kristaps  746:        if ('\0' == buf[1])
1.63      kristaps  747:                return(1);
                    748:
1.100     kristaps  749:        i = 1;
                    750:
                    751:        /* Accept whitespace after the initial control char. */
                    752:
                    753:        if (' ' == buf[i]) {
                    754:                i++;
1.65      kristaps  755:                while (buf[i] && ' ' == buf[i])
1.63      kristaps  756:                        i++;
1.115     kristaps  757:                if ('\0' == buf[i])
1.63      kristaps  758:                        return(1);
                    759:        }
1.1       kristaps  760:
1.53      kristaps  761:        /* Copy the first word into a nil-terminated buffer. */
1.1       kristaps  762:
1.100     kristaps  763:        for (j = 0; j < 4; j++, i++) {
1.115     kristaps  764:                if ('\0' == (mac[j] = buf[i]))
1.53      kristaps  765:                        break;
1.65      kristaps  766:                else if (' ' == buf[i])
1.53      kristaps  767:                        break;
1.104     kristaps  768:
                    769:                /* Check for invalid characters. */
                    770:
                    771:                if (isgraph((u_char)buf[i]))
                    772:                        continue;
                    773:                return(mdoc_perr(m, ln, i, EPRINT));
1.53      kristaps  774:        }
1.1       kristaps  775:
1.100     kristaps  776:        mac[j] = 0;
1.1       kristaps  777:
1.100     kristaps  778:        if (j == 4 || j < 2) {
1.58      kristaps  779:                if ( ! macrowarn(m, ln, mac))
                    780:                        goto err;
                    781:                return(1);
1.53      kristaps  782:        }
                    783:
1.125     kristaps  784:        if (MDOC_MAX == (tok = mdoc_hash_find(mac))) {
1.58      kristaps  785:                if ( ! macrowarn(m, ln, mac))
                    786:                        goto err;
                    787:                return(1);
1.53      kristaps  788:        }
1.1       kristaps  789:
1.53      kristaps  790:        /* The macro is sane.  Jump to the next word. */
1.1       kristaps  791:
1.65      kristaps  792:        while (buf[i] && ' ' == buf[i])
1.53      kristaps  793:                i++;
1.1       kristaps  794:
1.125     kristaps  795:        /*
                    796:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    797:         * into the parser as "text", so we only warn about spaces here.
                    798:         */
1.115     kristaps  799:
                    800:        if ('\0' == buf[i] && ' ' == buf[i - 1])
                    801:                if ( ! mdoc_pwarn(m, ln, i - 1, ETAILWS))
                    802:                        goto err;
                    803:
1.98      kristaps  804:        /*
                    805:         * Begin recursive parse sequence.  Since we're at the start of
                    806:         * the line, we don't need to do callable/parseable checks.
                    807:         */
1.125     kristaps  808:        if ( ! mdoc_macro(m, tok, ln, 1, &i, buf))
1.53      kristaps  809:                goto err;
1.1       kristaps  810:
1.53      kristaps  811:        return(1);
1.1       kristaps  812:
1.53      kristaps  813: err:   /* Error out. */
1.1       kristaps  814:
1.53      kristaps  815:        m->flags |= MDOC_HALT;
                    816:        return(0);
1.1       kristaps  817: }
1.100     kristaps  818:
                    819:

CVSweb