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

Annotation of mandoc/roff_escape.c, Revision 1.11

1.1       schwarze    1: /* $OpenBSD$ */
                      2: /*
                      3:  * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2017, 2018, 2020, 2022
                      4:  *               Ingo Schwarze <schwarze@openbsd.org>
                      5:  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
                      8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
                     10:  *
                     11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
                     12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
                     14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     16:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     17:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     18:  *
                     19:  * Parser for roff(7) escape sequences.
                     20:  * To be used by all mandoc(1) parsers and formatters.
                     21:  */
                     22: #include <assert.h>
                     23: #include <ctype.h>
                     24: #include <limits.h>
                     25: #include <stdio.h>
                     26: #include <string.h>
                     27:
                     28: #include "mandoc.h"
                     29: #include "roff.h"
                     30: #include "roff_int.h"
                     31:
                     32: /*
                     33:  * Traditional escape sequence interpreter for general use
                     34:  * including in high-level formatters.  This function does not issue
                     35:  * diagnostics and is not usable for expansion in the roff(7) parser.
                     36:  * It is documented in the mandoc_escape(3) manual page.
                     37:  */
                     38: enum mandoc_esc
                     39: mandoc_escape(const char **rendarg, const char **rarg, int *rargl)
                     40: {
                     41:         int             iarg, iendarg, iend;
                     42:         enum mandoc_esc  rval;
                     43:
1.7       schwarze   44:         rval = roff_escape(--*rendarg, 0, 0,
                     45:            NULL, NULL, &iarg, &iendarg, &iend);
1.1       schwarze   46:         assert(rval != ESCAPE_EXPAND);
                     47:         if (rarg != NULL)
                     48:               *rarg = *rendarg + iarg;
                     49:         if (rargl != NULL)
                     50:               *rargl = iendarg - iarg;
                     51:         *rendarg += iend;
                     52:         return rval;
                     53: }
                     54:
                     55: /*
                     56:  * Full-featured escape sequence parser.
                     57:  * If it encounters a nested escape sequence that requires expansion
                     58:  * by the parser and re-parsing, the positions of that inner escape
                     59:  * sequence are returned in *resc ... *rend.
                     60:  * Otherwise, *resc is set to aesc and the positions of the escape
                     61:  * sequence starting at aesc are returned.
                     62:  * Diagnostic messages are generated if and only if resc != NULL,
                     63:  * that is, if and only if called by roff_expand().
                     64:  */
                     65: enum mandoc_esc
                     66: roff_escape(const char *buf, const int ln, const int aesc,
1.7       schwarze   67:     int *resc, int *rnam, int *rarg, int *rendarg, int *rend)
1.1       schwarze   68: {
                     69:        int              iesc;          /* index of leading escape char */
1.7       schwarze   70:        int              inam;          /* index of escape name */
1.1       schwarze   71:        int              iarg;          /* index beginning the argument */
                     72:        int              iendarg;       /* index right after the argument */
                     73:        int              iend;          /* index right after the sequence */
1.7       schwarze   74:        int              sesc, snam, sarg, sendarg, send; /* for sub-escape */
1.1       schwarze   75:        int              maxl;          /* expected length of the argument */
                     76:        int              argl;          /* actual length of the argument */
                     77:        int              c, i;          /* for \[char...] parsing */
1.5       schwarze   78:        int              valid_A;       /* for \A parsing */
1.1       schwarze   79:        enum mandoc_esc  rval;          /* return value */
                     80:        enum mandocerr   err;           /* diagnostic code */
                     81:        char             term;          /* byte terminating the argument */
                     82:
                     83:        /*
                     84:         * Treat "\E" just like "\";
                     85:         * it only makes a difference in copy mode.
                     86:         */
                     87:
1.7       schwarze   88:        iesc = inam = aesc;
1.1       schwarze   89:        do {
1.7       schwarze   90:                inam++;
                     91:        } while (buf[inam] == 'E');
1.1       schwarze   92:
                     93:        /*
                     94:         * Sort the following cases first by syntax category,
                     95:         * then by escape sequence type, and finally by ASCII code.
                     96:         */
                     97:
1.7       schwarze   98:        iarg = iendarg = iend = inam + 1;
1.1       schwarze   99:        maxl = INT_MAX;
                    100:        term = '\0';
1.9       schwarze  101:        err = MANDOCERR_OK;
1.7       schwarze  102:        switch (buf[inam]) {
1.1       schwarze  103:
                    104:        /* Escape sequences taking no arguments at all. */
                    105:
                    106:        case '!':
                    107:        case '?':
1.2       schwarze  108:        case 'r':
1.1       schwarze  109:                rval = ESCAPE_UNSUPP;
                    110:                goto out;
                    111:
                    112:        case '%':
                    113:        case '&':
                    114:        case ')':
                    115:        case ',':
                    116:        case '/':
                    117:        case '^':
                    118:        case 'a':
                    119:        case 'd':
                    120:        case 't':
                    121:        case 'u':
                    122:        case '{':
                    123:        case '|':
                    124:        case '}':
                    125:                rval = ESCAPE_IGNORE;
                    126:                goto out;
                    127:
1.6       schwarze  128:        case '\0':
                    129:                iendarg = --iend;
                    130:                /* FALLTHROUGH */
1.8       schwarze  131:        case '.':
1.1       schwarze  132:        case '\\':
                    133:        default:
                    134:                iarg--;
                    135:                rval = ESCAPE_UNDEF;
                    136:                goto out;
                    137:
                    138:        case ' ':
                    139:        case '\'':
                    140:        case '-':
                    141:        case '0':
                    142:        case ':':
                    143:        case '_':
                    144:        case '`':
                    145:        case 'e':
                    146:        case '~':
                    147:                iarg--;
                    148:                argl = 1;
                    149:                rval = ESCAPE_SPECIAL;
                    150:                goto out;
                    151:        case 'p':
                    152:                rval = ESCAPE_BREAK;
                    153:                goto out;
                    154:        case 'c':
                    155:                rval = ESCAPE_NOSPACE;
                    156:                goto out;
                    157:        case 'z':
                    158:                rval = ESCAPE_SKIPCHAR;
                    159:                goto out;
                    160:
                    161:        /* Standard argument format. */
                    162:
                    163:        case '$':
                    164:        case '*':
1.3       schwarze  165:        case 'V':
1.4       schwarze  166:        case 'g':
1.1       schwarze  167:        case 'n':
                    168:                rval = ESCAPE_EXPAND;
                    169:                break;
                    170:        case 'F':
                    171:        case 'M':
                    172:        case 'O':
                    173:        case 'Y':
                    174:        case 'k':
                    175:        case 'm':
                    176:                rval = ESCAPE_IGNORE;
                    177:                break;
                    178:        case '(':
                    179:        case '[':
                    180:                rval = ESCAPE_SPECIAL;
                    181:                iendarg = iend = --iarg;
                    182:                break;
                    183:        case 'f':
                    184:                rval = ESCAPE_FONT;
                    185:                break;
                    186:
                    187:        /* Quoted arguments */
                    188:
1.5       schwarze  189:        case 'A':
1.1       schwarze  190:        case 'B':
                    191:        case 'w':
                    192:                rval = ESCAPE_EXPAND;
                    193:                term = '\b';
                    194:                break;
                    195:        case 'D':
                    196:        case 'H':
                    197:        case 'L':
                    198:        case 'R':
                    199:        case 'S':
                    200:        case 'X':
                    201:        case 'Z':
                    202:        case 'b':
                    203:        case 'v':
                    204:        case 'x':
                    205:                rval = ESCAPE_IGNORE;
                    206:                term = '\b';
                    207:                break;
                    208:        case 'C':
                    209:                rval = ESCAPE_SPECIAL;
                    210:                term = '\b';
                    211:                break;
                    212:        case 'N':
                    213:                rval = ESCAPE_NUMBERED;
                    214:                term = '\b';
                    215:                break;
                    216:        case 'h':
                    217:                rval = ESCAPE_HORIZ;
                    218:                term = '\b';
                    219:                break;
                    220:        case 'l':
                    221:                rval = ESCAPE_HLINE;
                    222:                term = '\b';
                    223:                break;
                    224:        case 'o':
                    225:                rval = ESCAPE_OVERSTRIKE;
                    226:                term = '\b';
                    227:                break;
                    228:
                    229:        /* Sizes support both forms, with additional peculiarities. */
                    230:
                    231:        case 's':
                    232:                rval = ESCAPE_IGNORE;
                    233:                if (buf[iarg] == '+' || buf[iarg] == '-'||
                    234:                    buf[iarg] == ASCII_HYPH)
                    235:                        iarg++;
                    236:                switch (buf[iarg]) {
                    237:                case '(':
                    238:                        maxl = 2;
                    239:                        iarg++;
                    240:                        break;
                    241:                case '[':
                    242:                        term = ']';
                    243:                        iarg++;
                    244:                        break;
                    245:                case '\'':
                    246:                        term = '\'';
                    247:                        iarg++;
                    248:                        break;
                    249:                case '1':
                    250:                case '2':
                    251:                case '3':
                    252:                        if (buf[iarg - 1] == 's' &&
                    253:                            isdigit((unsigned char)buf[iarg + 1])) {
                    254:                                maxl = 2;
                    255:                                break;
                    256:                        }
                    257:                        /* FALLTHROUGH */
                    258:                default:
                    259:                        maxl = 1;
                    260:                        break;
                    261:                }
                    262:                iendarg = iend = iarg;
                    263:        }
                    264:
                    265:        /* Decide how to end the argument. */
                    266:
                    267:        if ((term == '\b' || (term == '\0' && maxl == INT_MAX)) &&
                    268:            buf[iarg] == buf[iesc] && roff_escape(buf, ln, iendarg,
1.7       schwarze  269:            &sesc, &snam, &sarg, &sendarg, &send) == ESCAPE_EXPAND)
1.1       schwarze  270:                goto out_sub;
                    271:
                    272:        if (term == '\b') {
1.7       schwarze  273:                if ((buf[inam] == 'N' && isdigit((unsigned char)buf[iarg])) ||
                    274:                    (buf[inam] == 'h' && strchr(" %&()*+-./0123456789:<=>",
1.1       schwarze  275:                     buf[iarg]) != NULL)) {
                    276:                        iendarg = iend = iarg + 1;
                    277:                        rval = ESCAPE_ERROR;
                    278:                        goto out;
                    279:                }
                    280:                term = buf[iarg++];
                    281:        } else if (term == '\0' && maxl == INT_MAX) {
1.7       schwarze  282:                if (buf[inam] == 'n' && (buf[iarg] == '+' || buf[iarg] == '-'))
1.1       schwarze  283:                        iarg++;
                    284:                switch (buf[iarg]) {
                    285:                case '(':
                    286:                        maxl = 2;
                    287:                        iarg++;
                    288:                        break;
                    289:                case '[':
                    290:                        if (buf[++iarg] == ' ') {
                    291:                                iendarg = iend = iarg + 1;
                    292:                                rval = ESCAPE_ERROR;
                    293:                                goto out;
                    294:                        }
                    295:                        term = ']';
                    296:                        break;
                    297:                default:
                    298:                        maxl = 1;
                    299:                        break;
                    300:                }
                    301:        }
                    302:
                    303:        /* Advance to the end of the argument. */
                    304:
1.5       schwarze  305:        valid_A = 1;
1.1       schwarze  306:        iendarg = iarg;
                    307:        while (maxl > 0) {
                    308:                if (buf[iendarg] == '\0') {
1.10      schwarze  309:                        err = MANDOCERR_ESC_INCOMPLETE;
                    310:                        if (rval != ESCAPE_EXPAND)
                    311:                                rval = ESCAPE_ERROR;
1.1       schwarze  312:                        /* Ignore an incomplete argument except for \w. */
1.7       schwarze  313:                        if (buf[inam] != 'w')
1.1       schwarze  314:                                iendarg = iarg;
                    315:                        break;
                    316:                }
                    317:                if (buf[iendarg] == term) {
                    318:                        iend = iendarg + 1;
                    319:                        break;
                    320:                }
1.7       schwarze  321:                if (buf[inam] == 'N' &&
1.1       schwarze  322:                    isdigit((unsigned char)buf[iendarg]) == 0) {
                    323:                        iend = iendarg + 1;
                    324:                        break;
                    325:                }
                    326:                if (buf[iendarg] == buf[iesc]) {
1.5       schwarze  327:                        switch (roff_escape(buf, ln, iendarg,
1.7       schwarze  328:                            &sesc, &snam, &sarg, &sendarg, &send)) {
1.5       schwarze  329:                        case ESCAPE_EXPAND:
1.1       schwarze  330:                                goto out_sub;
1.5       schwarze  331:                        case ESCAPE_UNDEF:
                    332:                                break;
                    333:                        default:
                    334:                                valid_A = 0;
                    335:                                break;
                    336:                        }
1.1       schwarze  337:                        iendarg = iend = send;
                    338:                } else {
1.5       schwarze  339:                        if (buf[iendarg] == ' ' || buf[iendarg] == '\t')
                    340:                                valid_A = 0;
1.1       schwarze  341:                        if (maxl != INT_MAX)
                    342:                                maxl--;
                    343:                        iend = ++iendarg;
                    344:                }
                    345:        }
                    346:
                    347:        /* Post-process depending on the content of the argument. */
                    348:
                    349:        argl = iendarg - iarg;
1.7       schwarze  350:        switch (buf[inam]) {
1.1       schwarze  351:        case '*':
                    352:                if (resc == NULL && argl == 2 &&
                    353:                    buf[iarg] == '.' && buf[iarg + 1] == 'T')
                    354:                        rval = ESCAPE_DEVICE;
1.5       schwarze  355:                break;
                    356:        case 'A':
                    357:                if (valid_A == 0)
                    358:                        iendarg = iarg;
1.1       schwarze  359:                break;
                    360:        case 'O':
                    361:                switch (buf[iarg]) {
                    362:                case '0':
                    363:                        rval = ESCAPE_UNSUPP;
                    364:                        break;
                    365:                case '1':
                    366:                case '2':
                    367:                case '3':
                    368:                case '4':
                    369:                        rval = argl == 1 ? ESCAPE_IGNORE : ESCAPE_ERROR;
                    370:                        break;
                    371:                case '5':
                    372:                        rval = buf[iarg - 1] == '[' ? ESCAPE_UNSUPP :
                    373:                            ESCAPE_ERROR;
                    374:                        break;
                    375:                default:
                    376:                        rval = ESCAPE_ERROR;
                    377:                        break;
                    378:                }
                    379:                break;
                    380:        default:
                    381:                break;
                    382:        }
                    383:
                    384:        switch (rval) {
                    385:        case ESCAPE_FONT:
                    386:                rval = mandoc_font(buf + iarg, argl);
                    387:                break;
                    388:
                    389:        case ESCAPE_SPECIAL:
1.11    ! schwarze  390:                if (argl == 0) {
        !           391:                        err = MANDOCERR_ESC_BADCHAR;
        !           392:                        rval = ESCAPE_ERROR;
        !           393:                        break;
        !           394:                }
1.1       schwarze  395:
                    396:                /*
                    397:                 * The file chars.c only provides one common list of
                    398:                 * character names, but \[-] == \- is the only one of
                    399:                 * the characters with one-byte names that allows
                    400:                 * enclosing the name in brackets.
                    401:                 */
                    402:
                    403:                if (term != '\0' && argl == 1 && buf[iarg] != '-') {
1.10      schwarze  404:                        err = MANDOCERR_ESC_BADCHAR;
1.1       schwarze  405:                        rval = ESCAPE_ERROR;
                    406:                        break;
                    407:                }
                    408:
                    409:                /* Treat \[char...] as an alias for \N'...'. */
                    410:
                    411:                if (buf[iarg] == 'c') {
                    412:                        if (argl < 6 || argl > 7 ||
                    413:                            strncmp(buf + iarg, "char", 4) != 0 ||
                    414:                            (int)strspn(buf + iarg + 4, "0123456789")
                    415:                             + 4 < argl)
                    416:                                break;
                    417:                        c = 0;
                    418:                        for (i = iarg; i < iendarg; i++)
                    419:                                c = 10 * c + (buf[i] - '0');
1.10      schwarze  420:                        if (c < 0x21 || (c > 0x7e && c < 0xa0) || c > 0xff) {
                    421:                                err = MANDOCERR_ESC_BADCHAR;
1.1       schwarze  422:                                break;
1.10      schwarze  423:                        }
1.1       schwarze  424:                        iarg += 4;
                    425:                        rval = ESCAPE_NUMBERED;
                    426:                        break;
                    427:                }
                    428:
                    429:                /*
                    430:                 * Unicode escapes are defined in groff as \[u0000]
                    431:                 * to \[u10FFFF], where the contained value must be
                    432:                 * a valid Unicode codepoint.  Here, however, only
                    433:                 * check the length and range.
                    434:                 */
                    435:
                    436:                if (buf[iarg] != 'u' || argl < 5 || argl > 7)
                    437:                        break;
                    438:                if (argl == 7 &&
1.10      schwarze  439:                    (buf[iarg + 1] != '1' || buf[iarg + 2] != '0')) {
                    440:                        err = MANDOCERR_ESC_BADCHAR;
1.1       schwarze  441:                        break;
1.10      schwarze  442:                }
                    443:                if (argl == 6 && buf[iarg + 1] == '0') {
                    444:                        err = MANDOCERR_ESC_BADCHAR;
1.1       schwarze  445:                        break;
1.10      schwarze  446:                }
1.1       schwarze  447:                if (argl == 5 && buf[iarg + 1] == 'D' &&
1.10      schwarze  448:                    strchr("89ABCDEF", buf[iarg + 2]) != NULL) {
                    449:                        err = MANDOCERR_ESC_BADCHAR;
1.1       schwarze  450:                        break;
1.10      schwarze  451:                }
1.1       schwarze  452:                if ((int)strspn(buf + iarg + 1, "0123456789ABCDEFabcdef")
                    453:                    + 1 == argl)
                    454:                        rval = ESCAPE_UNICODE;
                    455:                break;
                    456:        default:
                    457:                break;
                    458:        }
                    459:        goto out;
                    460:
                    461: out_sub:
                    462:        iesc = sesc;
1.7       schwarze  463:        inam = snam;
1.1       schwarze  464:        iarg = sarg;
                    465:        iendarg = sendarg;
                    466:        iend = send;
                    467:        rval = ESCAPE_EXPAND;
                    468:
                    469: out:
1.7       schwarze  470:        if (rnam != NULL)
                    471:                *rnam = inam;
1.1       schwarze  472:        if (rarg != NULL)
                    473:                *rarg = iarg;
                    474:        if (rendarg != NULL)
                    475:                *rendarg = iendarg;
                    476:        if (rend != NULL)
                    477:                *rend = iend;
                    478:        if (resc == NULL)
                    479:                return rval;
                    480:
                    481:        /*
                    482:         * Diagnostic messages are only issued when called
                    483:         * from the parser, not when called from the formatters.
                    484:         */
                    485:
                    486:        *resc = iesc;
                    487:        switch (rval) {
                    488:        case ESCAPE_ERROR:
1.10      schwarze  489:                if (err == MANDOCERR_OK)
                    490:                        err = MANDOCERR_ESC_BAD;
1.1       schwarze  491:                break;
                    492:        case ESCAPE_UNSUPP:
                    493:                err = MANDOCERR_ESC_UNSUPP;
                    494:                break;
                    495:        case ESCAPE_UNDEF:
1.9       schwarze  496:                if (buf[inam] != '\\' && buf[inam] != '.')
                    497:                        err = MANDOCERR_ESC_UNDEF;
1.1       schwarze  498:                break;
                    499:        case ESCAPE_SPECIAL:
1.10      schwarze  500:                if (mchars_spec2cp(buf + iarg, argl) >= 0)
                    501:                        err = MANDOCERR_OK;
                    502:                else if (err == MANDOCERR_OK)
                    503:                        err = MANDOCERR_ESC_UNKCHAR;
1.1       schwarze  504:                break;
                    505:        default:
1.9       schwarze  506:                break;
1.1       schwarze  507:        }
1.9       schwarze  508:        if (err != MANDOCERR_OK)
                    509:                mandoc_msg(err, ln, iesc, "%.*s", iend - iesc, buf + iesc);
1.1       schwarze  510:        return rval;
                    511: }

CVSweb