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

Annotation of mandoc/mandoc.c, Revision 1.72

1.72    ! schwarze    1: /*     $Id: mandoc.c,v 1.71 2013/12/25 00:50:05 schwarze Exp $ */
1.1       kristaps    2: /*
1.59      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.68      schwarze    4:  * Copyright (c) 2011, 2012, 2013 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
1.69      schwarze   43: mandoc_escape(const char const **end, const char const **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);
1.65      schwarze   96:                *start = ++*end;
1.70      schwarze   97:                if ('u' == (*start)[0] && '\'' != (*start)[1])
                     98:                        gly = ESCAPE_UNICODE;
                     99:                else
                    100:                        gly = ESCAPE_SPECIAL;
1.45      kristaps  101:                term = '\'';
                    102:                break;
1.72    ! schwarze  103:
        !           104:        /*
        !           105:         * Escapes taking no arguments at all.
        !           106:         */
        !           107:        case ('d'):
        !           108:                /* FALLTHROUGH */
        !           109:        case ('u'):
        !           110:                return(ESCAPE_IGNORE);
1.63      schwarze  111:
                    112:        /*
                    113:         * The \z escape is supposed to output the following
                    114:         * character without advancing the cursor position.
                    115:         * Since we are mostly dealing with terminal mode,
                    116:         * let us just skip the next character.
                    117:         */
                    118:        case ('z'):
                    119:                return(ESCAPE_SKIPCHAR);
1.1       kristaps  120:
1.45      kristaps  121:        /*
                    122:         * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
                    123:         * 'X' is the trigger.  These have opaque sub-strings.
                    124:         */
                    125:        case ('F'):
                    126:                /* FALLTHROUGH */
                    127:        case ('g'):
1.24      kristaps  128:                /* FALLTHROUGH */
1.45      kristaps  129:        case ('k'):
1.24      kristaps  130:                /* FALLTHROUGH */
1.45      kristaps  131:        case ('M'):
1.24      kristaps  132:                /* FALLTHROUGH */
1.45      kristaps  133:        case ('m'):
1.24      kristaps  134:                /* FALLTHROUGH */
1.45      kristaps  135:        case ('n'):
1.24      kristaps  136:                /* FALLTHROUGH */
1.45      kristaps  137:        case ('V'):
1.24      kristaps  138:                /* FALLTHROUGH */
1.45      kristaps  139:        case ('Y'):
1.60      schwarze  140:                gly = ESCAPE_IGNORE;
1.24      kristaps  141:                /* FALLTHROUGH */
1.45      kristaps  142:        case ('f'):
                    143:                if (ESCAPE_ERROR == gly)
                    144:                        gly = ESCAPE_FONT;
1.65      schwarze  145:                switch (**start) {
1.45      kristaps  146:                case ('('):
1.65      schwarze  147:                        *start = ++*end;
                    148:                        *sz = 2;
1.45      kristaps  149:                        break;
                    150:                case ('['):
1.65      schwarze  151:                        *start = ++*end;
1.45      kristaps  152:                        term = ']';
                    153:                        break;
                    154:                default:
1.65      schwarze  155:                        *sz = 1;
1.45      kristaps  156:                        break;
                    157:                }
                    158:                break;
                    159:
                    160:        /*
                    161:         * These escapes are of the form \X'Y', where 'X' is the trigger
                    162:         * and 'Y' is any string.  These have opaque sub-strings.
                    163:         */
                    164:        case ('A'):
1.24      kristaps  165:                /* FALLTHROUGH */
1.45      kristaps  166:        case ('b'):
1.24      kristaps  167:                /* FALLTHROUGH */
                    168:        case ('D'):
                    169:                /* FALLTHROUGH */
1.45      kristaps  170:        case ('o'):
1.24      kristaps  171:                /* FALLTHROUGH */
1.45      kristaps  172:        case ('R'):
1.24      kristaps  173:                /* FALLTHROUGH */
1.45      kristaps  174:        case ('X'):
1.24      kristaps  175:                /* FALLTHROUGH */
1.45      kristaps  176:        case ('Z'):
1.65      schwarze  177:                if ('\'' != **start)
1.45      kristaps  178:                        return(ESCAPE_ERROR);
                    179:                gly = ESCAPE_IGNORE;
1.65      schwarze  180:                *start = ++*end;
1.24      kristaps  181:                term = '\'';
                    182:                break;
1.45      kristaps  183:
                    184:        /*
                    185:         * These escapes are of the form \X'N', where 'X' is the trigger
                    186:         * and 'N' resolves to a numerical expression.
                    187:         */
                    188:        case ('B'):
                    189:                /* FALLTHROUGH */
1.28      kristaps  190:        case ('h'):
                    191:                /* FALLTHROUGH */
1.45      kristaps  192:        case ('H'):
                    193:                /* FALLTHROUGH */
                    194:        case ('L'):
                    195:                /* FALLTHROUGH */
                    196:        case ('l'):
1.60      schwarze  197:                gly = ESCAPE_NUMBERED;
1.45      kristaps  198:                /* FALLTHROUGH */
                    199:        case ('S'):
                    200:                /* FALLTHROUGH */
1.28      kristaps  201:        case ('v'):
                    202:                /* FALLTHROUGH */
1.45      kristaps  203:        case ('w'):
                    204:                /* FALLTHROUGH */
                    205:        case ('x'):
1.65      schwarze  206:                if ('\'' != **start)
                    207:                        return(ESCAPE_ERROR);
1.45      kristaps  208:                if (ESCAPE_ERROR == gly)
                    209:                        gly = ESCAPE_IGNORE;
1.65      schwarze  210:                *start = ++*end;
1.64      schwarze  211:                term = '\'';
1.45      kristaps  212:                break;
1.60      schwarze  213:
                    214:        /*
                    215:         * Special handling for the numbered character escape.
                    216:         * XXX Do any other escapes need similar handling?
                    217:         */
                    218:        case ('N'):
1.65      schwarze  219:                if ('\0' == **start)
1.60      schwarze  220:                        return(ESCAPE_ERROR);
1.65      schwarze  221:                (*end)++;
                    222:                if (isdigit((unsigned char)**start)) {
                    223:                        *sz = 1;
1.60      schwarze  224:                        return(ESCAPE_IGNORE);
1.65      schwarze  225:                }
                    226:                (*start)++;
1.60      schwarze  227:                while (isdigit((unsigned char)**end))
                    228:                        (*end)++;
1.65      schwarze  229:                *sz = *end - *start;
1.60      schwarze  230:                if ('\0' != **end)
                    231:                        (*end)++;
                    232:                return(ESCAPE_NUMBERED);
1.45      kristaps  233:
                    234:        /*
                    235:         * Sizes get a special category of their own.
                    236:         */
1.8       kristaps  237:        case ('s'):
1.45      kristaps  238:                gly = ESCAPE_IGNORE;
1.28      kristaps  239:
1.45      kristaps  240:                /* See +/- counts as a sign. */
1.65      schwarze  241:                if ('+' == **end || '-' == **end || ASCII_HYPH == **end)
                    242:                        (*end)++;
1.8       kristaps  243:
1.65      schwarze  244:                switch (**end) {
1.22      kristaps  245:                case ('('):
1.65      schwarze  246:                        *start = ++*end;
                    247:                        *sz = 2;
1.22      kristaps  248:                        break;
                    249:                case ('['):
1.65      schwarze  250:                        *start = ++*end;
1.64      schwarze  251:                        term = ']';
1.22      kristaps  252:                        break;
                    253:                case ('\''):
1.65      schwarze  254:                        *start = ++*end;
1.64      schwarze  255:                        term = '\'';
1.22      kristaps  256:                        break;
                    257:                default:
1.65      schwarze  258:                        *sz = 1;
1.22      kristaps  259:                        break;
1.8       kristaps  260:                }
                    261:
1.45      kristaps  262:                break;
1.33      kristaps  263:
1.45      kristaps  264:        /*
                    265:         * Anything else is assumed to be a glyph.
1.65      schwarze  266:         * In this case, pass back the character after the backslash.
1.45      kristaps  267:         */
                    268:        default:
                    269:                gly = ESCAPE_SPECIAL;
1.65      schwarze  270:                *start = --*end;
                    271:                *sz = 1;
1.22      kristaps  272:                break;
1.45      kristaps  273:        }
                    274:
                    275:        assert(ESCAPE_ERROR != gly);
                    276:
                    277:        /*
1.64      schwarze  278:         * Read up to the terminating character,
                    279:         * paying attention to nested escapes.
1.45      kristaps  280:         */
                    281:
                    282:        if ('\0' != term) {
1.64      schwarze  283:                while (**end != term) {
                    284:                        switch (**end) {
                    285:                        case ('\0'):
                    286:                                return(ESCAPE_ERROR);
                    287:                        case ('\\'):
                    288:                                (*end)++;
                    289:                                if (ESCAPE_ERROR ==
                    290:                                    mandoc_escape(end, NULL, NULL))
                    291:                                        return(ESCAPE_ERROR);
                    292:                                break;
                    293:                        default:
                    294:                                (*end)++;
                    295:                                break;
                    296:                        }
                    297:                }
1.65      schwarze  298:                *sz = (*end)++ - *start;
1.64      schwarze  299:        } else {
1.65      schwarze  300:                assert(*sz > 0);
                    301:                if ((size_t)*sz > strlen(*start))
1.45      kristaps  302:                        return(ESCAPE_ERROR);
1.65      schwarze  303:                *end += *sz;
1.45      kristaps  304:        }
                    305:
                    306:        /* Run post-processors. */
                    307:
                    308:        switch (gly) {
                    309:        case (ESCAPE_FONT):
1.68      schwarze  310:                if (2 == *sz) {
                    311:                        if ('C' == **start) {
                    312:                                /*
                    313:                                 * Treat constant-width font modes
                    314:                                 * just like regular font modes.
                    315:                                 */
                    316:                                (*start)++;
                    317:                                (*sz)--;
                    318:                        } else {
                    319:                                if ('B' == (*start)[0] && 'I' == (*start)[1])
                    320:                                        gly = ESCAPE_FONTBI;
                    321:                                break;
                    322:                        }
1.65      schwarze  323:                } else if (1 != *sz)
1.45      kristaps  324:                        break;
1.61      kristaps  325:
1.65      schwarze  326:                switch (**start) {
1.45      kristaps  327:                case ('3'):
                    328:                        /* FALLTHROUGH */
                    329:                case ('B'):
                    330:                        gly = ESCAPE_FONTBOLD;
                    331:                        break;
                    332:                case ('2'):
                    333:                        /* FALLTHROUGH */
                    334:                case ('I'):
                    335:                        gly = ESCAPE_FONTITALIC;
1.22      kristaps  336:                        break;
1.45      kristaps  337:                case ('P'):
                    338:                        gly = ESCAPE_FONTPREV;
1.22      kristaps  339:                        break;
1.45      kristaps  340:                case ('1'):
                    341:                        /* FALLTHROUGH */
                    342:                case ('R'):
                    343:                        gly = ESCAPE_FONTROMAN;
1.1       kristaps  344:                        break;
                    345:                }
1.46      kristaps  346:                break;
1.45      kristaps  347:        case (ESCAPE_SPECIAL):
1.65      schwarze  348:                if (1 == *sz && 'c' == **start)
1.45      kristaps  349:                        gly = ESCAPE_NOSPACE;
1.22      kristaps  350:                break;
1.1       kristaps  351:        default:
1.22      kristaps  352:                break;
1.1       kristaps  353:        }
                    354:
1.45      kristaps  355:        return(gly);
1.1       kristaps  356: }
1.4       kristaps  357:
                    358: void *
                    359: mandoc_calloc(size_t num, size_t size)
                    360: {
                    361:        void            *ptr;
                    362:
                    363:        ptr = calloc(num, size);
                    364:        if (NULL == ptr) {
1.6       kristaps  365:                perror(NULL);
1.35      kristaps  366:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  367:        }
                    368:
                    369:        return(ptr);
                    370: }
                    371:
                    372:
                    373: void *
                    374: mandoc_malloc(size_t size)
                    375: {
                    376:        void            *ptr;
                    377:
                    378:        ptr = malloc(size);
                    379:        if (NULL == ptr) {
1.6       kristaps  380:                perror(NULL);
1.35      kristaps  381:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  382:        }
                    383:
                    384:        return(ptr);
                    385: }
                    386:
                    387:
                    388: void *
                    389: mandoc_realloc(void *ptr, size_t size)
                    390: {
                    391:
                    392:        ptr = realloc(ptr, size);
                    393:        if (NULL == ptr) {
1.6       kristaps  394:                perror(NULL);
1.35      kristaps  395:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  396:        }
                    397:
                    398:        return(ptr);
                    399: }
                    400:
1.55      kristaps  401: char *
                    402: mandoc_strndup(const char *ptr, size_t sz)
                    403: {
                    404:        char            *p;
                    405:
                    406:        p = mandoc_malloc(sz + 1);
                    407:        memcpy(p, ptr, sz);
                    408:        p[(int)sz] = '\0';
                    409:        return(p);
                    410: }
1.4       kristaps  411:
                    412: char *
                    413: mandoc_strdup(const char *ptr)
                    414: {
                    415:        char            *p;
                    416:
                    417:        p = strdup(ptr);
                    418:        if (NULL == p) {
1.6       kristaps  419:                perror(NULL);
1.35      kristaps  420:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  421:        }
                    422:
                    423:        return(p);
1.36      schwarze  424: }
                    425:
                    426: /*
                    427:  * Parse a quoted or unquoted roff-style request or macro argument.
                    428:  * Return a pointer to the parsed argument, which is either the original
                    429:  * pointer or advanced by one byte in case the argument is quoted.
1.71      schwarze  430:  * NUL-terminate the argument in place.
1.36      schwarze  431:  * Collapse pairs of quotes inside quoted arguments.
                    432:  * Advance the argument pointer to the next argument,
1.71      schwarze  433:  * or to the NUL byte terminating the argument line.
1.36      schwarze  434:  */
                    435: char *
1.48      kristaps  436: mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
1.36      schwarze  437: {
                    438:        char     *start, *cp;
                    439:        int       quoted, pairs, white;
                    440:
                    441:        /* Quoting can only start with a new word. */
                    442:        start = *cpp;
1.47      kristaps  443:        quoted = 0;
1.36      schwarze  444:        if ('"' == *start) {
                    445:                quoted = 1;
                    446:                start++;
1.47      kristaps  447:        }
1.36      schwarze  448:
                    449:        pairs = 0;
                    450:        white = 0;
                    451:        for (cp = start; '\0' != *cp; cp++) {
1.67      schwarze  452:
                    453:                /*
                    454:                 * Move the following text left
                    455:                 * after quoted quotes and after "\\" and "\t".
                    456:                 */
1.36      schwarze  457:                if (pairs)
                    458:                        cp[-pairs] = cp[0];
1.67      schwarze  459:
1.36      schwarze  460:                if ('\\' == cp[0]) {
1.67      schwarze  461:                        /*
                    462:                         * In copy mode, translate double to single
                    463:                         * backslashes and backslash-t to literal tabs.
                    464:                         */
                    465:                        switch (cp[1]) {
                    466:                        case ('t'):
                    467:                                cp[0] = '\t';
                    468:                                /* FALLTHROUGH */
                    469:                        case ('\\'):
1.36      schwarze  470:                                pairs++;
                    471:                                cp++;
1.67      schwarze  472:                                break;
                    473:                        case (' '):
1.36      schwarze  474:                                /* Skip escaped blanks. */
1.67      schwarze  475:                                if (0 == quoted)
                    476:                                        cp++;
                    477:                                break;
                    478:                        default:
                    479:                                break;
                    480:                        }
1.36      schwarze  481:                } else if (0 == quoted) {
                    482:                        if (' ' == cp[0]) {
                    483:                                /* Unescaped blanks end unquoted args. */
                    484:                                white = 1;
                    485:                                break;
                    486:                        }
                    487:                } else if ('"' == cp[0]) {
                    488:                        if ('"' == cp[1]) {
                    489:                                /* Quoted quotes collapse. */
                    490:                                pairs++;
                    491:                                cp++;
                    492:                        } else {
                    493:                                /* Unquoted quotes end quoted args. */
                    494:                                quoted = 2;
                    495:                                break;
                    496:                        }
                    497:                }
                    498:        }
                    499:
                    500:        /* Quoted argument without a closing quote. */
1.48      kristaps  501:        if (1 == quoted)
1.42      kristaps  502:                mandoc_msg(MANDOCERR_BADQUOTE, parse, ln, *pos, NULL);
1.36      schwarze  503:
1.71      schwarze  504:        /* NUL-terminate this argument and move to the next one. */
1.36      schwarze  505:        if (pairs)
                    506:                cp[-pairs] = '\0';
                    507:        if ('\0' != *cp) {
                    508:                *cp++ = '\0';
                    509:                while (' ' == *cp)
                    510:                        cp++;
                    511:        }
1.39      kristaps  512:        *pos += (int)(cp - start) + (quoted ? 1 : 0);
1.36      schwarze  513:        *cpp = cp;
                    514:
1.48      kristaps  515:        if ('\0' == *cp && (white || ' ' == cp[-1]))
1.42      kristaps  516:                mandoc_msg(MANDOCERR_EOLNSPACE, parse, ln, *pos, NULL);
1.36      schwarze  517:
                    518:        return(start);
1.4       kristaps  519: }
1.7       kristaps  520:
                    521: static int
                    522: a2time(time_t *t, const char *fmt, const char *p)
                    523: {
                    524:        struct tm        tm;
                    525:        char            *pp;
                    526:
                    527:        memset(&tm, 0, sizeof(struct tm));
                    528:
1.56      kristaps  529:        pp = NULL;
                    530: #ifdef HAVE_STRPTIME
1.7       kristaps  531:        pp = strptime(p, fmt, &tm);
1.56      kristaps  532: #endif
1.7       kristaps  533:        if (NULL != pp && '\0' == *pp) {
                    534:                *t = mktime(&tm);
                    535:                return(1);
                    536:        }
                    537:
                    538:        return(0);
                    539: }
                    540:
1.37      schwarze  541: static char *
                    542: time2a(time_t t)
                    543: {
1.56      kristaps  544:        struct tm       *tm;
1.38      schwarze  545:        char            *buf, *p;
                    546:        size_t           ssz;
1.37      schwarze  547:        int              isz;
                    548:
1.56      kristaps  549:        tm = localtime(&t);
1.37      schwarze  550:
1.38      schwarze  551:        /*
                    552:         * Reserve space:
                    553:         * up to 9 characters for the month (September) + blank
                    554:         * up to 2 characters for the day + comma + blank
                    555:         * 4 characters for the year and a terminating '\0'
                    556:         */
                    557:        p = buf = mandoc_malloc(10 + 4 + 4 + 1);
                    558:
1.56      kristaps  559:        if (0 == (ssz = strftime(p, 10 + 1, "%B ", tm)))
1.38      schwarze  560:                goto fail;
                    561:        p += (int)ssz;
1.37      schwarze  562:
1.56      kristaps  563:        if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)))
1.38      schwarze  564:                goto fail;
1.37      schwarze  565:        p += isz;
                    566:
1.56      kristaps  567:        if (0 == strftime(p, 4 + 1, "%Y", tm))
1.38      schwarze  568:                goto fail;
                    569:        return(buf);
                    570:
                    571: fail:
                    572:        free(buf);
                    573:        return(NULL);
1.37      schwarze  574: }
                    575:
                    576: char *
1.42      kristaps  577: mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
1.7       kristaps  578: {
1.37      schwarze  579:        char            *out;
1.7       kristaps  580:        time_t           t;
                    581:
1.37      schwarze  582:        if (NULL == in || '\0' == *in ||
                    583:            0 == strcmp(in, "$" "Mdocdate$")) {
1.42      kristaps  584:                mandoc_msg(MANDOCERR_NODATE, parse, ln, pos, NULL);
1.37      schwarze  585:                time(&t);
                    586:        }
1.62      schwarze  587:        else if (a2time(&t, "%Y-%m-%d", in))
                    588:                t = 0;
1.37      schwarze  589:        else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&
1.62      schwarze  590:            !a2time(&t, "%b %d, %Y", in)) {
1.42      kristaps  591:                mandoc_msg(MANDOCERR_BADDATE, parse, ln, pos, NULL);
1.37      schwarze  592:                t = 0;
1.7       kristaps  593:        }
1.37      schwarze  594:        out = t ? time2a(t) : NULL;
1.38      schwarze  595:        return(out ? out : mandoc_strdup(in));
1.7       kristaps  596: }
                    597:
1.12      kristaps  598: int
1.23      schwarze  599: mandoc_eos(const char *p, size_t sz, int enclosed)
1.12      kristaps  600: {
1.23      schwarze  601:        const char *q;
                    602:        int found;
1.12      kristaps  603:
1.13      kristaps  604:        if (0 == sz)
                    605:                return(0);
1.12      kristaps  606:
1.14      kristaps  607:        /*
                    608:         * End-of-sentence recognition must include situations where
                    609:         * some symbols, such as `)', allow prior EOS punctuation to
1.49      kristaps  610:         * propagate outward.
1.14      kristaps  611:         */
                    612:
1.23      schwarze  613:        found = 0;
1.25      kristaps  614:        for (q = p + (int)sz - 1; q >= p; q--) {
1.23      schwarze  615:                switch (*q) {
1.14      kristaps  616:                case ('\"'):
                    617:                        /* FALLTHROUGH */
                    618:                case ('\''):
1.15      kristaps  619:                        /* FALLTHROUGH */
                    620:                case (']'):
1.14      kristaps  621:                        /* FALLTHROUGH */
                    622:                case (')'):
1.23      schwarze  623:                        if (0 == found)
                    624:                                enclosed = 1;
1.14      kristaps  625:                        break;
                    626:                case ('.'):
                    627:                        /* FALLTHROUGH */
                    628:                case ('!'):
                    629:                        /* FALLTHROUGH */
                    630:                case ('?'):
1.23      schwarze  631:                        found = 1;
                    632:                        break;
1.14      kristaps  633:                default:
1.27      joerg     634:                        return(found && (!enclosed || isalnum((unsigned char)*q)));
1.14      kristaps  635:                }
1.12      kristaps  636:        }
                    637:
1.23      schwarze  638:        return(found && !enclosed);
1.44      kristaps  639: }
1.50      kristaps  640:
                    641: /*
                    642:  * Convert a string to a long that may not be <0.
                    643:  * If the string is invalid, or is less than 0, return -1.
                    644:  */
                    645: int
1.54      kristaps  646: mandoc_strntoi(const char *p, size_t sz, int base)
1.50      kristaps  647: {
                    648:        char             buf[32];
                    649:        char            *ep;
                    650:        long             v;
                    651:
                    652:        if (sz > 31)
                    653:                return(-1);
                    654:
                    655:        memcpy(buf, p, sz);
1.51      kristaps  656:        buf[(int)sz] = '\0';
1.50      kristaps  657:
                    658:        errno = 0;
                    659:        v = strtol(buf, &ep, base);
                    660:
                    661:        if (buf[0] == '\0' || *ep != '\0')
                    662:                return(-1);
                    663:
1.54      kristaps  664:        if (v > INT_MAX)
                    665:                v = INT_MAX;
                    666:        if (v < INT_MIN)
                    667:                v = INT_MIN;
1.50      kristaps  668:
                    669:        return((int)v);
                    670: }

CVSweb