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

Annotation of mandoc/mandoc.c, Revision 1.67

1.67    ! schwarze    1: /*     $Id: mandoc.c,v 1.66 2012/06/12 20:21:04 kristaps Exp $ */
1.1       kristaps    2: /*
1.59      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.63      schwarze    4:  * Copyright (c) 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
1.36      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.36      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       kristaps   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
1.9       kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
1.7       kristaps   20: #endif
                     21:
1.2       kristaps   22: #include <sys/types.h>
                     23:
1.1       kristaps   24: #include <assert.h>
                     25: #include <ctype.h>
1.50      kristaps   26: #include <errno.h>
                     27: #include <limits.h>
1.1       kristaps   28: #include <stdlib.h>
1.4       kristaps   29: #include <stdio.h>
                     30: #include <string.h>
1.7       kristaps   31: #include <time.h>
1.1       kristaps   32:
1.18      kristaps   33: #include "mandoc.h"
1.1       kristaps   34: #include "libmandoc.h"
                     35:
1.37      schwarze   36: #define DATESIZE 32
                     37:
1.18      kristaps   38: static int      a2time(time_t *, const char *, const char *);
1.37      schwarze   39: static char    *time2a(time_t);
1.7       kristaps   40:
1.45      kristaps   41:
                     42: enum mandoc_esc
                     43: mandoc_escape(const char **end, const char **start, int *sz)
1.1       kristaps   44: {
1.65      schwarze   45:        const char      *local_start;
                     46:        int              local_sz;
                     47:        char             term;
1.45      kristaps   48:        enum mandoc_esc  gly;
                     49:
1.65      schwarze   50:        /*
                     51:         * When the caller doesn't provide return storage,
                     52:         * use local storage.
                     53:         */
                     54:
                     55:        if (NULL == start)
                     56:                start = &local_start;
                     57:        if (NULL == sz)
                     58:                sz = &local_sz;
                     59:
                     60:        /*
                     61:         * Beyond the backslash, at least one input character
                     62:         * is part of the escape sequence.  With one exception
                     63:         * (see below), that character won't be returned.
                     64:         */
                     65:
1.45      kristaps   66:        gly = ESCAPE_ERROR;
1.65      schwarze   67:        *start = ++*end;
                     68:        *sz = 0;
1.64      schwarze   69:        term = '\0';
1.18      kristaps   70:
1.65      schwarze   71:        switch ((*start)[-1]) {
1.45      kristaps   72:        /*
                     73:         * First the glyphs.  There are several different forms of
                     74:         * these, but each eventually returns a substring of the glyph
                     75:         * name.
                     76:         */
                     77:        case ('('):
                     78:                gly = ESCAPE_SPECIAL;
1.65      schwarze   79:                *sz = 2;
1.45      kristaps   80:                break;
                     81:        case ('['):
                     82:                gly = ESCAPE_SPECIAL;
1.52      kristaps   83:                /*
                     84:                 * Unicode escapes are defined in groff as \[uXXXX] to
                     85:                 * \[u10FFFF], where the contained value must be a valid
                     86:                 * Unicode codepoint.  Here, however, only check whether
                     87:                 * it's not a zero-width escape.
                     88:                 */
1.65      schwarze   89:                if ('u' == (*start)[0] && ']' != (*start)[1])
1.52      kristaps   90:                        gly = ESCAPE_UNICODE;
1.45      kristaps   91:                term = ']';
                     92:                break;
                     93:        case ('C'):
1.65      schwarze   94:                if ('\'' != **start)
1.45      kristaps   95:                        return(ESCAPE_ERROR);
                     96:                gly = ESCAPE_SPECIAL;
1.65      schwarze   97:                *start = ++*end;
1.45      kristaps   98:                term = '\'';
                     99:                break;
1.63      schwarze  100:
                    101:        /*
                    102:         * The \z escape is supposed to output the following
                    103:         * character without advancing the cursor position.
                    104:         * Since we are mostly dealing with terminal mode,
                    105:         * let us just skip the next character.
                    106:         */
                    107:        case ('z'):
                    108:                return(ESCAPE_SKIPCHAR);
1.1       kristaps  109:
1.45      kristaps  110:        /*
                    111:         * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
                    112:         * 'X' is the trigger.  These have opaque sub-strings.
                    113:         */
                    114:        case ('F'):
                    115:                /* FALLTHROUGH */
                    116:        case ('g'):
1.24      kristaps  117:                /* FALLTHROUGH */
1.45      kristaps  118:        case ('k'):
1.24      kristaps  119:                /* FALLTHROUGH */
1.45      kristaps  120:        case ('M'):
1.24      kristaps  121:                /* FALLTHROUGH */
1.45      kristaps  122:        case ('m'):
1.24      kristaps  123:                /* FALLTHROUGH */
1.45      kristaps  124:        case ('n'):
1.24      kristaps  125:                /* FALLTHROUGH */
1.45      kristaps  126:        case ('V'):
1.24      kristaps  127:                /* FALLTHROUGH */
1.45      kristaps  128:        case ('Y'):
1.60      schwarze  129:                gly = ESCAPE_IGNORE;
1.24      kristaps  130:                /* FALLTHROUGH */
1.45      kristaps  131:        case ('f'):
                    132:                if (ESCAPE_ERROR == gly)
                    133:                        gly = ESCAPE_FONT;
1.65      schwarze  134:                switch (**start) {
1.45      kristaps  135:                case ('('):
1.65      schwarze  136:                        *start = ++*end;
                    137:                        *sz = 2;
1.45      kristaps  138:                        break;
                    139:                case ('['):
1.65      schwarze  140:                        *start = ++*end;
1.45      kristaps  141:                        term = ']';
                    142:                        break;
                    143:                default:
1.65      schwarze  144:                        *sz = 1;
1.45      kristaps  145:                        break;
                    146:                }
                    147:                break;
                    148:
                    149:        /*
                    150:         * These escapes are of the form \X'Y', where 'X' is the trigger
                    151:         * and 'Y' is any string.  These have opaque sub-strings.
                    152:         */
                    153:        case ('A'):
1.24      kristaps  154:                /* FALLTHROUGH */
1.45      kristaps  155:        case ('b'):
1.24      kristaps  156:                /* FALLTHROUGH */
                    157:        case ('D'):
                    158:                /* FALLTHROUGH */
1.45      kristaps  159:        case ('o'):
1.24      kristaps  160:                /* FALLTHROUGH */
1.45      kristaps  161:        case ('R'):
1.24      kristaps  162:                /* FALLTHROUGH */
1.45      kristaps  163:        case ('X'):
1.24      kristaps  164:                /* FALLTHROUGH */
1.45      kristaps  165:        case ('Z'):
1.65      schwarze  166:                if ('\'' != **start)
1.45      kristaps  167:                        return(ESCAPE_ERROR);
                    168:                gly = ESCAPE_IGNORE;
1.65      schwarze  169:                *start = ++*end;
1.24      kristaps  170:                term = '\'';
                    171:                break;
1.45      kristaps  172:
                    173:        /*
                    174:         * These escapes are of the form \X'N', where 'X' is the trigger
                    175:         * and 'N' resolves to a numerical expression.
                    176:         */
                    177:        case ('B'):
                    178:                /* FALLTHROUGH */
1.28      kristaps  179:        case ('h'):
                    180:                /* FALLTHROUGH */
1.45      kristaps  181:        case ('H'):
                    182:                /* FALLTHROUGH */
                    183:        case ('L'):
                    184:                /* FALLTHROUGH */
                    185:        case ('l'):
1.60      schwarze  186:                gly = ESCAPE_NUMBERED;
1.45      kristaps  187:                /* FALLTHROUGH */
                    188:        case ('S'):
                    189:                /* FALLTHROUGH */
1.28      kristaps  190:        case ('v'):
                    191:                /* FALLTHROUGH */
1.45      kristaps  192:        case ('w'):
                    193:                /* FALLTHROUGH */
                    194:        case ('x'):
1.65      schwarze  195:                if ('\'' != **start)
                    196:                        return(ESCAPE_ERROR);
1.45      kristaps  197:                if (ESCAPE_ERROR == gly)
                    198:                        gly = ESCAPE_IGNORE;
1.65      schwarze  199:                *start = ++*end;
1.64      schwarze  200:                term = '\'';
1.45      kristaps  201:                break;
1.60      schwarze  202:
                    203:        /*
                    204:         * Special handling for the numbered character escape.
                    205:         * XXX Do any other escapes need similar handling?
                    206:         */
                    207:        case ('N'):
1.65      schwarze  208:                if ('\0' == **start)
1.60      schwarze  209:                        return(ESCAPE_ERROR);
1.65      schwarze  210:                (*end)++;
                    211:                if (isdigit((unsigned char)**start)) {
                    212:                        *sz = 1;
1.60      schwarze  213:                        return(ESCAPE_IGNORE);
1.65      schwarze  214:                }
                    215:                (*start)++;
1.60      schwarze  216:                while (isdigit((unsigned char)**end))
                    217:                        (*end)++;
1.65      schwarze  218:                *sz = *end - *start;
1.60      schwarze  219:                if ('\0' != **end)
                    220:                        (*end)++;
                    221:                return(ESCAPE_NUMBERED);
1.45      kristaps  222:
                    223:        /*
                    224:         * Sizes get a special category of their own.
                    225:         */
1.8       kristaps  226:        case ('s'):
1.45      kristaps  227:                gly = ESCAPE_IGNORE;
1.28      kristaps  228:
1.45      kristaps  229:                /* See +/- counts as a sign. */
1.65      schwarze  230:                if ('+' == **end || '-' == **end || ASCII_HYPH == **end)
                    231:                        (*end)++;
1.8       kristaps  232:
1.65      schwarze  233:                switch (**end) {
1.22      kristaps  234:                case ('('):
1.65      schwarze  235:                        *start = ++*end;
                    236:                        *sz = 2;
1.22      kristaps  237:                        break;
                    238:                case ('['):
1.65      schwarze  239:                        *start = ++*end;
1.64      schwarze  240:                        term = ']';
1.22      kristaps  241:                        break;
                    242:                case ('\''):
1.65      schwarze  243:                        *start = ++*end;
1.64      schwarze  244:                        term = '\'';
1.22      kristaps  245:                        break;
                    246:                default:
1.65      schwarze  247:                        *sz = 1;
1.22      kristaps  248:                        break;
1.8       kristaps  249:                }
                    250:
1.45      kristaps  251:                break;
1.33      kristaps  252:
1.45      kristaps  253:        /*
                    254:         * Anything else is assumed to be a glyph.
1.65      schwarze  255:         * In this case, pass back the character after the backslash.
1.45      kristaps  256:         */
                    257:        default:
                    258:                gly = ESCAPE_SPECIAL;
1.65      schwarze  259:                *start = --*end;
                    260:                *sz = 1;
1.22      kristaps  261:                break;
1.45      kristaps  262:        }
                    263:
                    264:        assert(ESCAPE_ERROR != gly);
                    265:
                    266:        /*
1.64      schwarze  267:         * Read up to the terminating character,
                    268:         * paying attention to nested escapes.
1.45      kristaps  269:         */
                    270:
                    271:        if ('\0' != term) {
1.64      schwarze  272:                while (**end != term) {
                    273:                        switch (**end) {
                    274:                        case ('\0'):
                    275:                                return(ESCAPE_ERROR);
                    276:                        case ('\\'):
                    277:                                (*end)++;
                    278:                                if (ESCAPE_ERROR ==
                    279:                                    mandoc_escape(end, NULL, NULL))
                    280:                                        return(ESCAPE_ERROR);
                    281:                                break;
                    282:                        default:
                    283:                                (*end)++;
                    284:                                break;
                    285:                        }
                    286:                }
1.65      schwarze  287:                *sz = (*end)++ - *start;
1.64      schwarze  288:        } else {
1.65      schwarze  289:                assert(*sz > 0);
                    290:                if ((size_t)*sz > strlen(*start))
1.45      kristaps  291:                        return(ESCAPE_ERROR);
1.65      schwarze  292:                *end += *sz;
1.45      kristaps  293:        }
                    294:
                    295:        /* Run post-processors. */
                    296:
                    297:        switch (gly) {
                    298:        case (ESCAPE_FONT):
1.61      kristaps  299:                /*
                    300:                 * Pretend that the constant-width font modes are the
                    301:                 * same as the regular font modes.
                    302:                 */
1.65      schwarze  303:                if (2 == *sz && 'C' == **start) {
                    304:                        (*start)++;
                    305:                        (*sz)--;
                    306:                } else if (1 != *sz)
1.45      kristaps  307:                        break;
1.61      kristaps  308:
1.65      schwarze  309:                switch (**start) {
1.45      kristaps  310:                case ('3'):
                    311:                        /* FALLTHROUGH */
                    312:                case ('B'):
                    313:                        gly = ESCAPE_FONTBOLD;
                    314:                        break;
                    315:                case ('2'):
                    316:                        /* FALLTHROUGH */
                    317:                case ('I'):
                    318:                        gly = ESCAPE_FONTITALIC;
1.22      kristaps  319:                        break;
1.45      kristaps  320:                case ('P'):
                    321:                        gly = ESCAPE_FONTPREV;
1.22      kristaps  322:                        break;
1.45      kristaps  323:                case ('1'):
                    324:                        /* FALLTHROUGH */
                    325:                case ('R'):
                    326:                        gly = ESCAPE_FONTROMAN;
1.1       kristaps  327:                        break;
                    328:                }
1.46      kristaps  329:                break;
1.45      kristaps  330:        case (ESCAPE_SPECIAL):
1.65      schwarze  331:                if (1 == *sz && 'c' == **start)
1.45      kristaps  332:                        gly = ESCAPE_NOSPACE;
1.22      kristaps  333:                break;
1.1       kristaps  334:        default:
1.22      kristaps  335:                break;
1.1       kristaps  336:        }
                    337:
1.45      kristaps  338:        return(gly);
1.1       kristaps  339: }
1.4       kristaps  340:
                    341: void *
                    342: mandoc_calloc(size_t num, size_t size)
                    343: {
                    344:        void            *ptr;
                    345:
                    346:        ptr = calloc(num, size);
                    347:        if (NULL == ptr) {
1.6       kristaps  348:                perror(NULL);
1.35      kristaps  349:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  350:        }
                    351:
                    352:        return(ptr);
                    353: }
                    354:
                    355:
                    356: void *
                    357: mandoc_malloc(size_t size)
                    358: {
                    359:        void            *ptr;
                    360:
                    361:        ptr = malloc(size);
                    362:        if (NULL == ptr) {
1.6       kristaps  363:                perror(NULL);
1.35      kristaps  364:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  365:        }
                    366:
                    367:        return(ptr);
                    368: }
                    369:
                    370:
                    371: void *
                    372: mandoc_realloc(void *ptr, size_t size)
                    373: {
                    374:
                    375:        ptr = realloc(ptr, size);
                    376:        if (NULL == ptr) {
1.6       kristaps  377:                perror(NULL);
1.35      kristaps  378:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  379:        }
                    380:
                    381:        return(ptr);
                    382: }
                    383:
1.55      kristaps  384: char *
                    385: mandoc_strndup(const char *ptr, size_t sz)
                    386: {
                    387:        char            *p;
                    388:
                    389:        p = mandoc_malloc(sz + 1);
                    390:        memcpy(p, ptr, sz);
                    391:        p[(int)sz] = '\0';
                    392:        return(p);
                    393: }
1.4       kristaps  394:
                    395: char *
                    396: mandoc_strdup(const char *ptr)
                    397: {
                    398:        char            *p;
                    399:
                    400:        p = strdup(ptr);
                    401:        if (NULL == p) {
1.6       kristaps  402:                perror(NULL);
1.35      kristaps  403:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  404:        }
                    405:
                    406:        return(p);
1.36      schwarze  407: }
                    408:
                    409: /*
                    410:  * Parse a quoted or unquoted roff-style request or macro argument.
                    411:  * Return a pointer to the parsed argument, which is either the original
                    412:  * pointer or advanced by one byte in case the argument is quoted.
                    413:  * Null-terminate the argument in place.
                    414:  * Collapse pairs of quotes inside quoted arguments.
                    415:  * Advance the argument pointer to the next argument,
                    416:  * or to the null byte terminating the argument line.
                    417:  */
                    418: char *
1.48      kristaps  419: mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
1.36      schwarze  420: {
                    421:        char     *start, *cp;
                    422:        int       quoted, pairs, white;
                    423:
                    424:        /* Quoting can only start with a new word. */
                    425:        start = *cpp;
1.47      kristaps  426:        quoted = 0;
1.36      schwarze  427:        if ('"' == *start) {
                    428:                quoted = 1;
                    429:                start++;
1.47      kristaps  430:        }
1.36      schwarze  431:
                    432:        pairs = 0;
                    433:        white = 0;
                    434:        for (cp = start; '\0' != *cp; cp++) {
1.67    ! schwarze  435:
        !           436:                /*
        !           437:                 * Move the following text left
        !           438:                 * after quoted quotes and after "\\" and "\t".
        !           439:                 */
1.36      schwarze  440:                if (pairs)
                    441:                        cp[-pairs] = cp[0];
1.67    ! schwarze  442:
1.36      schwarze  443:                if ('\\' == cp[0]) {
1.67    ! schwarze  444:                        /*
        !           445:                         * In copy mode, translate double to single
        !           446:                         * backslashes and backslash-t to literal tabs.
        !           447:                         */
        !           448:                        switch (cp[1]) {
        !           449:                        case ('t'):
        !           450:                                cp[0] = '\t';
        !           451:                                /* FALLTHROUGH */
        !           452:                        case ('\\'):
1.36      schwarze  453:                                pairs++;
                    454:                                cp++;
1.67    ! schwarze  455:                                break;
        !           456:                        case (' '):
1.36      schwarze  457:                                /* Skip escaped blanks. */
1.67    ! schwarze  458:                                if (0 == quoted)
        !           459:                                        cp++;
        !           460:                                break;
        !           461:                        default:
        !           462:                                break;
        !           463:                        }
1.36      schwarze  464:                } else if (0 == quoted) {
                    465:                        if (' ' == cp[0]) {
                    466:                                /* Unescaped blanks end unquoted args. */
                    467:                                white = 1;
                    468:                                break;
                    469:                        }
                    470:                } else if ('"' == cp[0]) {
                    471:                        if ('"' == cp[1]) {
                    472:                                /* Quoted quotes collapse. */
                    473:                                pairs++;
                    474:                                cp++;
                    475:                        } else {
                    476:                                /* Unquoted quotes end quoted args. */
                    477:                                quoted = 2;
                    478:                                break;
                    479:                        }
                    480:                }
                    481:        }
                    482:
                    483:        /* Quoted argument without a closing quote. */
1.48      kristaps  484:        if (1 == quoted)
1.42      kristaps  485:                mandoc_msg(MANDOCERR_BADQUOTE, parse, ln, *pos, NULL);
1.36      schwarze  486:
                    487:        /* Null-terminate this argument and move to the next one. */
                    488:        if (pairs)
                    489:                cp[-pairs] = '\0';
                    490:        if ('\0' != *cp) {
                    491:                *cp++ = '\0';
                    492:                while (' ' == *cp)
                    493:                        cp++;
                    494:        }
1.39      kristaps  495:        *pos += (int)(cp - start) + (quoted ? 1 : 0);
1.36      schwarze  496:        *cpp = cp;
                    497:
1.48      kristaps  498:        if ('\0' == *cp && (white || ' ' == cp[-1]))
1.42      kristaps  499:                mandoc_msg(MANDOCERR_EOLNSPACE, parse, ln, *pos, NULL);
1.36      schwarze  500:
                    501:        return(start);
1.4       kristaps  502: }
1.7       kristaps  503:
                    504: static int
                    505: a2time(time_t *t, const char *fmt, const char *p)
                    506: {
                    507:        struct tm        tm;
                    508:        char            *pp;
                    509:
                    510:        memset(&tm, 0, sizeof(struct tm));
                    511:
1.56      kristaps  512:        pp = NULL;
                    513: #ifdef HAVE_STRPTIME
1.7       kristaps  514:        pp = strptime(p, fmt, &tm);
1.56      kristaps  515: #endif
1.7       kristaps  516:        if (NULL != pp && '\0' == *pp) {
                    517:                *t = mktime(&tm);
                    518:                return(1);
                    519:        }
                    520:
                    521:        return(0);
                    522: }
                    523:
1.37      schwarze  524: static char *
                    525: time2a(time_t t)
                    526: {
1.56      kristaps  527:        struct tm       *tm;
1.38      schwarze  528:        char            *buf, *p;
                    529:        size_t           ssz;
1.37      schwarze  530:        int              isz;
                    531:
1.56      kristaps  532:        tm = localtime(&t);
1.37      schwarze  533:
1.38      schwarze  534:        /*
                    535:         * Reserve space:
                    536:         * up to 9 characters for the month (September) + blank
                    537:         * up to 2 characters for the day + comma + blank
                    538:         * 4 characters for the year and a terminating '\0'
                    539:         */
                    540:        p = buf = mandoc_malloc(10 + 4 + 4 + 1);
                    541:
1.56      kristaps  542:        if (0 == (ssz = strftime(p, 10 + 1, "%B ", tm)))
1.38      schwarze  543:                goto fail;
                    544:        p += (int)ssz;
1.37      schwarze  545:
1.56      kristaps  546:        if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)))
1.38      schwarze  547:                goto fail;
1.37      schwarze  548:        p += isz;
                    549:
1.56      kristaps  550:        if (0 == strftime(p, 4 + 1, "%Y", tm))
1.38      schwarze  551:                goto fail;
                    552:        return(buf);
                    553:
                    554: fail:
                    555:        free(buf);
                    556:        return(NULL);
1.37      schwarze  557: }
                    558:
                    559: char *
1.42      kristaps  560: mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
1.7       kristaps  561: {
1.37      schwarze  562:        char            *out;
1.7       kristaps  563:        time_t           t;
                    564:
1.37      schwarze  565:        if (NULL == in || '\0' == *in ||
                    566:            0 == strcmp(in, "$" "Mdocdate$")) {
1.42      kristaps  567:                mandoc_msg(MANDOCERR_NODATE, parse, ln, pos, NULL);
1.37      schwarze  568:                time(&t);
                    569:        }
1.62      schwarze  570:        else if (a2time(&t, "%Y-%m-%d", in))
                    571:                t = 0;
1.37      schwarze  572:        else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&
1.62      schwarze  573:            !a2time(&t, "%b %d, %Y", in)) {
1.42      kristaps  574:                mandoc_msg(MANDOCERR_BADDATE, parse, ln, pos, NULL);
1.37      schwarze  575:                t = 0;
1.7       kristaps  576:        }
1.37      schwarze  577:        out = t ? time2a(t) : NULL;
1.38      schwarze  578:        return(out ? out : mandoc_strdup(in));
1.7       kristaps  579: }
                    580:
1.12      kristaps  581: int
1.23      schwarze  582: mandoc_eos(const char *p, size_t sz, int enclosed)
1.12      kristaps  583: {
1.23      schwarze  584:        const char *q;
                    585:        int found;
1.12      kristaps  586:
1.13      kristaps  587:        if (0 == sz)
                    588:                return(0);
1.12      kristaps  589:
1.14      kristaps  590:        /*
                    591:         * End-of-sentence recognition must include situations where
                    592:         * some symbols, such as `)', allow prior EOS punctuation to
1.49      kristaps  593:         * propagate outward.
1.14      kristaps  594:         */
                    595:
1.23      schwarze  596:        found = 0;
1.25      kristaps  597:        for (q = p + (int)sz - 1; q >= p; q--) {
1.23      schwarze  598:                switch (*q) {
1.14      kristaps  599:                case ('\"'):
                    600:                        /* FALLTHROUGH */
                    601:                case ('\''):
1.15      kristaps  602:                        /* FALLTHROUGH */
                    603:                case (']'):
1.14      kristaps  604:                        /* FALLTHROUGH */
                    605:                case (')'):
1.23      schwarze  606:                        if (0 == found)
                    607:                                enclosed = 1;
1.14      kristaps  608:                        break;
                    609:                case ('.'):
                    610:                        /* FALLTHROUGH */
                    611:                case ('!'):
                    612:                        /* FALLTHROUGH */
                    613:                case ('?'):
1.23      schwarze  614:                        found = 1;
                    615:                        break;
1.14      kristaps  616:                default:
1.27      joerg     617:                        return(found && (!enclosed || isalnum((unsigned char)*q)));
1.14      kristaps  618:                }
1.12      kristaps  619:        }
                    620:
1.23      schwarze  621:        return(found && !enclosed);
1.44      kristaps  622: }
1.50      kristaps  623:
                    624: /*
                    625:  * Convert a string to a long that may not be <0.
                    626:  * If the string is invalid, or is less than 0, return -1.
                    627:  */
                    628: int
1.54      kristaps  629: mandoc_strntoi(const char *p, size_t sz, int base)
1.50      kristaps  630: {
                    631:        char             buf[32];
                    632:        char            *ep;
                    633:        long             v;
                    634:
                    635:        if (sz > 31)
                    636:                return(-1);
                    637:
                    638:        memcpy(buf, p, sz);
1.51      kristaps  639:        buf[(int)sz] = '\0';
1.50      kristaps  640:
                    641:        errno = 0;
                    642:        v = strtol(buf, &ep, base);
                    643:
                    644:        if (buf[0] == '\0' || *ep != '\0')
                    645:                return(-1);
                    646:
1.54      kristaps  647:        if (v > INT_MAX)
                    648:                v = INT_MAX;
                    649:        if (v < INT_MIN)
                    650:                v = INT_MIN;
1.50      kristaps  651:
                    652:        return((int)v);
                    653: }

CVSweb