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

Annotation of mandoc/roff_escape.c, Revision 1.9

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:                if (buf[iarg] != '\'') {
                    210:                        rval = ESCAPE_ERROR;
                    211:                        goto out;
                    212:                }
                    213:                rval = ESCAPE_SPECIAL;
                    214:                term = '\b';
                    215:                break;
                    216:        case 'N':
                    217:                rval = ESCAPE_NUMBERED;
                    218:                term = '\b';
                    219:                break;
                    220:        case 'h':
                    221:                rval = ESCAPE_HORIZ;
                    222:                term = '\b';
                    223:                break;
                    224:        case 'l':
                    225:                rval = ESCAPE_HLINE;
                    226:                term = '\b';
                    227:                break;
                    228:        case 'o':
                    229:                rval = ESCAPE_OVERSTRIKE;
                    230:                term = '\b';
                    231:                break;
                    232:
                    233:        /* Sizes support both forms, with additional peculiarities. */
                    234:
                    235:        case 's':
                    236:                rval = ESCAPE_IGNORE;
                    237:                if (buf[iarg] == '+' || buf[iarg] == '-'||
                    238:                    buf[iarg] == ASCII_HYPH)
                    239:                        iarg++;
                    240:                switch (buf[iarg]) {
                    241:                case '(':
                    242:                        maxl = 2;
                    243:                        iarg++;
                    244:                        break;
                    245:                case '[':
                    246:                        term = ']';
                    247:                        iarg++;
                    248:                        break;
                    249:                case '\'':
                    250:                        term = '\'';
                    251:                        iarg++;
                    252:                        break;
                    253:                case '1':
                    254:                case '2':
                    255:                case '3':
                    256:                        if (buf[iarg - 1] == 's' &&
                    257:                            isdigit((unsigned char)buf[iarg + 1])) {
                    258:                                maxl = 2;
                    259:                                break;
                    260:                        }
                    261:                        /* FALLTHROUGH */
                    262:                default:
                    263:                        maxl = 1;
                    264:                        break;
                    265:                }
                    266:                iendarg = iend = iarg;
                    267:        }
                    268:
                    269:        /* Decide how to end the argument. */
                    270:
                    271:        if ((term == '\b' || (term == '\0' && maxl == INT_MAX)) &&
                    272:            buf[iarg] == buf[iesc] && roff_escape(buf, ln, iendarg,
1.7       schwarze  273:            &sesc, &snam, &sarg, &sendarg, &send) == ESCAPE_EXPAND)
1.1       schwarze  274:                goto out_sub;
                    275:
                    276:        if (term == '\b') {
1.7       schwarze  277:                if ((buf[inam] == 'N' && isdigit((unsigned char)buf[iarg])) ||
                    278:                    (buf[inam] == 'h' && strchr(" %&()*+-./0123456789:<=>",
1.1       schwarze  279:                     buf[iarg]) != NULL)) {
                    280:                        iendarg = iend = iarg + 1;
                    281:                        rval = ESCAPE_ERROR;
                    282:                        goto out;
                    283:                }
                    284:                term = buf[iarg++];
                    285:        } else if (term == '\0' && maxl == INT_MAX) {
1.7       schwarze  286:                if (buf[inam] == 'n' && (buf[iarg] == '+' || buf[iarg] == '-'))
1.1       schwarze  287:                        iarg++;
                    288:                switch (buf[iarg]) {
                    289:                case '(':
                    290:                        maxl = 2;
                    291:                        iarg++;
                    292:                        break;
                    293:                case '[':
                    294:                        if (buf[++iarg] == ' ') {
                    295:                                iendarg = iend = iarg + 1;
                    296:                                rval = ESCAPE_ERROR;
                    297:                                goto out;
                    298:                        }
                    299:                        term = ']';
                    300:                        break;
                    301:                default:
                    302:                        maxl = 1;
                    303:                        break;
                    304:                }
                    305:        }
                    306:
                    307:        /* Advance to the end of the argument. */
                    308:
1.5       schwarze  309:        valid_A = 1;
1.1       schwarze  310:        iendarg = iarg;
                    311:        while (maxl > 0) {
                    312:                if (buf[iendarg] == '\0') {
                    313:                        /* Ignore an incomplete argument except for \w. */
1.7       schwarze  314:                        if (buf[inam] != 'w')
1.1       schwarze  315:                                iendarg = iarg;
1.9     ! schwarze  316:                        if (rval == ESCAPE_EXPAND)
        !           317:                                err = MANDOCERR_ESC_BAD;
        !           318:                        else
        !           319:                                rval = ESCAPE_ERROR;
1.1       schwarze  320:                        break;
                    321:                }
                    322:                if (buf[iendarg] == term) {
                    323:                        iend = iendarg + 1;
                    324:                        break;
                    325:                }
1.7       schwarze  326:                if (buf[inam] == 'N' &&
1.1       schwarze  327:                    isdigit((unsigned char)buf[iendarg]) == 0) {
                    328:                        iend = iendarg + 1;
                    329:                        break;
                    330:                }
                    331:                if (buf[iendarg] == buf[iesc]) {
1.5       schwarze  332:                        switch (roff_escape(buf, ln, iendarg,
1.7       schwarze  333:                            &sesc, &snam, &sarg, &sendarg, &send)) {
1.5       schwarze  334:                        case ESCAPE_EXPAND:
1.1       schwarze  335:                                goto out_sub;
1.5       schwarze  336:                        case ESCAPE_UNDEF:
                    337:                                break;
                    338:                        default:
                    339:                                valid_A = 0;
                    340:                                break;
                    341:                        }
1.1       schwarze  342:                        iendarg = iend = send;
                    343:                } else {
1.5       schwarze  344:                        if (buf[iendarg] == ' ' || buf[iendarg] == '\t')
                    345:                                valid_A = 0;
1.1       schwarze  346:                        if (maxl != INT_MAX)
                    347:                                maxl--;
                    348:                        iend = ++iendarg;
                    349:                }
                    350:        }
                    351:
                    352:        /* Post-process depending on the content of the argument. */
                    353:
                    354:        argl = iendarg - iarg;
1.7       schwarze  355:        switch (buf[inam]) {
1.1       schwarze  356:        case '*':
                    357:                if (resc == NULL && argl == 2 &&
                    358:                    buf[iarg] == '.' && buf[iarg + 1] == 'T')
                    359:                        rval = ESCAPE_DEVICE;
1.5       schwarze  360:                break;
                    361:        case 'A':
                    362:                if (valid_A == 0)
                    363:                        iendarg = iarg;
1.1       schwarze  364:                break;
                    365:        case 'O':
                    366:                switch (buf[iarg]) {
                    367:                case '0':
                    368:                        rval = ESCAPE_UNSUPP;
                    369:                        break;
                    370:                case '1':
                    371:                case '2':
                    372:                case '3':
                    373:                case '4':
                    374:                        rval = argl == 1 ? ESCAPE_IGNORE : ESCAPE_ERROR;
                    375:                        break;
                    376:                case '5':
                    377:                        rval = buf[iarg - 1] == '[' ? ESCAPE_UNSUPP :
                    378:                            ESCAPE_ERROR;
                    379:                        break;
                    380:                default:
                    381:                        rval = ESCAPE_ERROR;
                    382:                        break;
                    383:                }
                    384:                break;
                    385:        default:
                    386:                break;
                    387:        }
                    388:
                    389:        switch (rval) {
                    390:        case ESCAPE_FONT:
                    391:                rval = mandoc_font(buf + iarg, argl);
                    392:                break;
                    393:
                    394:        case ESCAPE_SPECIAL:
                    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] != '-') {
                    404:                        rval = ESCAPE_ERROR;
                    405:                        break;
                    406:                }
                    407:
                    408:                /* Treat \[char...] as an alias for \N'...'. */
                    409:
                    410:                if (buf[iarg] == 'c') {
                    411:                        if (argl < 6 || argl > 7 ||
                    412:                            strncmp(buf + iarg, "char", 4) != 0 ||
                    413:                            (int)strspn(buf + iarg + 4, "0123456789")
                    414:                             + 4 < argl)
                    415:                                break;
                    416:                        c = 0;
                    417:                        for (i = iarg; i < iendarg; i++)
                    418:                                c = 10 * c + (buf[i] - '0');
                    419:                        if (c < 0x21 || (c > 0x7e && c < 0xa0) || c > 0xff)
                    420:                                break;
                    421:                        iarg += 4;
                    422:                        rval = ESCAPE_NUMBERED;
                    423:                        break;
                    424:                }
                    425:
                    426:                /*
                    427:                 * Unicode escapes are defined in groff as \[u0000]
                    428:                 * to \[u10FFFF], where the contained value must be
                    429:                 * a valid Unicode codepoint.  Here, however, only
                    430:                 * check the length and range.
                    431:                 */
                    432:
                    433:                if (buf[iarg] != 'u' || argl < 5 || argl > 7)
                    434:                        break;
                    435:                if (argl == 7 &&
                    436:                    (buf[iarg + 1] != '1' || buf[iarg + 2] != '0'))
                    437:                        break;
                    438:                if (argl == 6 && buf[iarg + 1] == '0')
                    439:                        break;
                    440:                if (argl == 5 && buf[iarg + 1] == 'D' &&
                    441:                    strchr("89ABCDEF", buf[iarg + 2]) != NULL)
                    442:                        break;
                    443:                if ((int)strspn(buf + iarg + 1, "0123456789ABCDEFabcdef")
                    444:                    + 1 == argl)
                    445:                        rval = ESCAPE_UNICODE;
                    446:                break;
                    447:        default:
                    448:                break;
                    449:        }
                    450:        goto out;
                    451:
                    452: out_sub:
                    453:        iesc = sesc;
1.7       schwarze  454:        inam = snam;
1.1       schwarze  455:        iarg = sarg;
                    456:        iendarg = sendarg;
                    457:        iend = send;
                    458:        rval = ESCAPE_EXPAND;
                    459:
                    460: out:
1.7       schwarze  461:        if (rnam != NULL)
                    462:                *rnam = inam;
1.1       schwarze  463:        if (rarg != NULL)
                    464:                *rarg = iarg;
                    465:        if (rendarg != NULL)
                    466:                *rendarg = iendarg;
                    467:        if (rend != NULL)
                    468:                *rend = iend;
                    469:        if (resc == NULL)
                    470:                return rval;
                    471:
                    472:        /*
                    473:         * Diagnostic messages are only issued when called
                    474:         * from the parser, not when called from the formatters.
                    475:         */
                    476:
                    477:        *resc = iesc;
                    478:        switch (rval) {
                    479:        case ESCAPE_ERROR:
                    480:                err = MANDOCERR_ESC_BAD;
                    481:                break;
                    482:        case ESCAPE_UNSUPP:
                    483:                err = MANDOCERR_ESC_UNSUPP;
                    484:                break;
                    485:        case ESCAPE_UNDEF:
1.9     ! schwarze  486:                if (buf[inam] != '\\' && buf[inam] != '.')
        !           487:                        err = MANDOCERR_ESC_UNDEF;
1.1       schwarze  488:                break;
                    489:        case ESCAPE_SPECIAL:
1.9     ! schwarze  490:                if (mchars_spec2cp(buf + iarg, argl) < 0)
        !           491:                        err = MANDOCERR_ESC_BAD;
1.1       schwarze  492:                break;
                    493:        default:
1.9     ! schwarze  494:                break;
1.1       schwarze  495:        }
1.9     ! schwarze  496:        if (err != MANDOCERR_OK)
        !           497:                mandoc_msg(err, ln, iesc, "%.*s", iend - iesc, buf + iesc);
1.1       schwarze  498:        return rval;
                    499: }

CVSweb