[BACK]Return to mandoc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mandoc.c, Revision 1.30

1.30    ! schwarze    1: /*     $Id: mandoc.c,v 1.29 2011/10/24 20:29:21 schwarze Exp $ */
1.1       schwarze    2: /*
1.24      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.21      schwarze    4:  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    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.21      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       schwarze   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.21      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       schwarze   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.2       schwarze   18: #include <sys/types.h>
                     19:
1.1       schwarze   20: #include <assert.h>
                     21: #include <ctype.h>
1.26      schwarze   22: #include <errno.h>
                     23: #include <limits.h>
1.1       schwarze   24: #include <stdlib.h>
1.4       schwarze   25: #include <stdio.h>
                     26: #include <string.h>
1.5       schwarze   27: #include <time.h>
1.1       schwarze   28:
1.14      schwarze   29: #include "mandoc.h"
1.1       schwarze   30: #include "libmandoc.h"
                     31:
1.22      schwarze   32: #define DATESIZE 32
                     33:
1.14      schwarze   34: static int      a2time(time_t *, const char *, const char *);
1.22      schwarze   35: static char    *time2a(time_t);
1.26      schwarze   36: static int      numescape(const char *);
1.5       schwarze   37:
1.26      schwarze   38: /*
                     39:  * Pass over recursive numerical expressions.  This context of this
                     40:  * function is important: it's only called within character-terminating
                     41:  * escapes (e.g., \s[xxxyyy]), so all we need to do is handle initial
                     42:  * recursion: we don't care about what's in these blocks.
                     43:  * This returns the number of characters skipped or -1 if an error
                     44:  * occurs (the caller should bail).
                     45:  */
                     46: static int
                     47: numescape(const char *start)
1.1       schwarze   48: {
1.26      schwarze   49:        int              i;
                     50:        size_t           sz;
                     51:        const char      *cp;
                     52:
                     53:        i = 0;
                     54:
                     55:        /* The expression consists of a subexpression. */
                     56:
                     57:        if ('\\' == start[i]) {
                     58:                cp = &start[++i];
                     59:                /*
                     60:                 * Read past the end of the subexpression.
                     61:                 * Bail immediately on errors.
                     62:                 */
                     63:                if (ESCAPE_ERROR == mandoc_escape(&cp, NULL, NULL))
                     64:                        return(-1);
                     65:                return(i + cp - &start[i]);
                     66:        }
                     67:
                     68:        if ('(' != start[i++])
                     69:                return(0);
1.14      schwarze   70:
1.26      schwarze   71:        /*
                     72:         * A parenthesised subexpression.  Read until the closing
                     73:         * parenthesis, making sure to handle any nested subexpressions
                     74:         * that might ruin our parse.
                     75:         */
                     76:
                     77:        while (')' != start[i]) {
                     78:                sz = strcspn(&start[i], ")\\");
                     79:                i += (int)sz;
                     80:
                     81:                if ('\0' == start[i])
                     82:                        return(-1);
                     83:                else if ('\\' != start[i])
                     84:                        continue;
                     85:
                     86:                cp = &start[++i];
                     87:                if (ESCAPE_ERROR == mandoc_escape(&cp, NULL, NULL))
                     88:                        return(-1);
                     89:                i += cp - &start[i];
                     90:        }
                     91:
                     92:        /* Read past the terminating ')'. */
                     93:        return(++i);
                     94: }
                     95:
                     96: enum mandoc_esc
                     97: mandoc_escape(const char **end, const char **start, int *sz)
                     98: {
                     99:        char             c, term, numeric;
                    100:        int              i, lim, ssz, rlim;
                    101:        const char      *cp, *rstart;
                    102:        enum mandoc_esc  gly;
                    103:
                    104:        cp = *end;
                    105:        rstart = cp;
                    106:        if (start)
                    107:                *start = rstart;
                    108:        i = lim = 0;
                    109:        gly = ESCAPE_ERROR;
                    110:        term = numeric = '\0';
                    111:
                    112:        switch ((c = cp[i++])) {
                    113:        /*
                    114:         * First the glyphs.  There are several different forms of
                    115:         * these, but each eventually returns a substring of the glyph
                    116:         * name.
                    117:         */
                    118:        case ('('):
                    119:                gly = ESCAPE_SPECIAL;
                    120:                lim = 2;
                    121:                break;
                    122:        case ('['):
                    123:                gly = ESCAPE_SPECIAL;
                    124:                /*
                    125:                 * Unicode escapes are defined in groff as \[uXXXX] to
                    126:                 * \[u10FFFF], where the contained value must be a valid
                    127:                 * Unicode codepoint.  Here, however, only check whether
                    128:                 * it's not a zero-width escape.
                    129:                 */
                    130:                if ('u' == cp[i] && ']' != cp[i + 1])
                    131:                        gly = ESCAPE_UNICODE;
                    132:                term = ']';
                    133:                break;
                    134:        case ('C'):
                    135:                if ('\'' != cp[i])
                    136:                        return(ESCAPE_ERROR);
                    137:                gly = ESCAPE_SPECIAL;
                    138:                term = '\'';
                    139:                break;
1.1       schwarze  140:
1.26      schwarze  141:        /*
                    142:         * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
                    143:         * 'X' is the trigger.  These have opaque sub-strings.
                    144:         */
                    145:        case ('F'):
1.16      schwarze  146:                /* FALLTHROUGH */
1.26      schwarze  147:        case ('g'):
1.16      schwarze  148:                /* FALLTHROUGH */
1.26      schwarze  149:        case ('k'):
1.1       schwarze  150:                /* FALLTHROUGH */
1.26      schwarze  151:        case ('M'):
1.14      schwarze  152:                /* FALLTHROUGH */
1.26      schwarze  153:        case ('m'):
1.1       schwarze  154:                /* FALLTHROUGH */
1.26      schwarze  155:        case ('n'):
1.1       schwarze  156:                /* FALLTHROUGH */
1.26      schwarze  157:        case ('V'):
1.1       schwarze  158:                /* FALLTHROUGH */
1.26      schwarze  159:        case ('Y'):
1.29      schwarze  160:                gly = ESCAPE_IGNORE;
1.1       schwarze  161:                /* FALLTHROUGH */
1.26      schwarze  162:        case ('f'):
                    163:                if (ESCAPE_ERROR == gly)
                    164:                        gly = ESCAPE_FONT;
                    165:
                    166:                rstart= &cp[i];
                    167:                if (start)
                    168:                        *start = rstart;
                    169:
                    170:                switch (cp[i++]) {
                    171:                case ('('):
                    172:                        lim = 2;
                    173:                        break;
                    174:                case ('['):
                    175:                        term = ']';
                    176:                        break;
                    177:                default:
                    178:                        lim = 1;
                    179:                        i--;
                    180:                        break;
                    181:                }
                    182:                break;
                    183:
                    184:        /*
                    185:         * These escapes are of the form \X'Y', where 'X' is the trigger
                    186:         * and 'Y' is any string.  These have opaque sub-strings.
                    187:         */
                    188:        case ('A'):
1.13      schwarze  189:                /* FALLTHROUGH */
1.26      schwarze  190:        case ('b'):
1.1       schwarze  191:                /* FALLTHROUGH */
1.16      schwarze  192:        case ('D'):
1.1       schwarze  193:                /* FALLTHROUGH */
1.26      schwarze  194:        case ('o'):
1.1       schwarze  195:                /* FALLTHROUGH */
1.26      schwarze  196:        case ('R'):
1.1       schwarze  197:                /* FALLTHROUGH */
1.26      schwarze  198:        case ('X'):
1.1       schwarze  199:                /* FALLTHROUGH */
1.26      schwarze  200:        case ('Z'):
                    201:                if ('\'' != cp[i++])
                    202:                        return(ESCAPE_ERROR);
                    203:                gly = ESCAPE_IGNORE;
1.16      schwarze  204:                term = '\'';
                    205:                break;
1.26      schwarze  206:
                    207:        /*
                    208:         * These escapes are of the form \X'N', where 'X' is the trigger
                    209:         * and 'N' resolves to a numerical expression.
                    210:         */
                    211:        case ('B'):
                    212:                /* FALLTHROUGH */
1.17      schwarze  213:        case ('h'):
                    214:                /* FALLTHROUGH */
1.26      schwarze  215:        case ('H'):
                    216:                /* FALLTHROUGH */
                    217:        case ('L'):
                    218:                /* FALLTHROUGH */
                    219:        case ('l'):
1.29      schwarze  220:                gly = ESCAPE_NUMBERED;
1.26      schwarze  221:                /* FALLTHROUGH */
                    222:        case ('S'):
                    223:                /* FALLTHROUGH */
1.17      schwarze  224:        case ('v'):
                    225:                /* FALLTHROUGH */
1.26      schwarze  226:        case ('w'):
                    227:                /* FALLTHROUGH */
                    228:        case ('x'):
                    229:                if (ESCAPE_ERROR == gly)
                    230:                        gly = ESCAPE_IGNORE;
                    231:                if ('\'' != cp[i++])
                    232:                        return(ESCAPE_ERROR);
                    233:                term = numeric = '\'';
                    234:                break;
1.29      schwarze  235:
                    236:        /*
                    237:         * Special handling for the numbered character escape.
                    238:         * XXX Do any other escapes need similar handling?
                    239:         */
                    240:        case ('N'):
                    241:                if ('\0' == cp[i])
                    242:                        return(ESCAPE_ERROR);
                    243:                *end = &cp[++i];
                    244:                if (isdigit((unsigned char)cp[i-1]))
                    245:                        return(ESCAPE_IGNORE);
                    246:                while (isdigit((unsigned char)**end))
                    247:                        (*end)++;
                    248:                if (start)
                    249:                        *start = &cp[i];
                    250:                if (sz)
                    251:                        *sz = *end - &cp[i];
                    252:                if ('\0' != **end)
                    253:                        (*end)++;
                    254:                return(ESCAPE_NUMBERED);
1.26      schwarze  255:
                    256:        /*
                    257:         * Sizes get a special category of their own.
                    258:         */
1.6       schwarze  259:        case ('s'):
1.26      schwarze  260:                gly = ESCAPE_IGNORE;
1.17      schwarze  261:
1.26      schwarze  262:                rstart = &cp[i];
                    263:                if (start)
                    264:                        *start = rstart;
                    265:
                    266:                /* See +/- counts as a sign. */
                    267:                c = cp[i];
                    268:                if ('+' == c || '-' == c || ASCII_HYPH == c)
                    269:                        ++i;
1.6       schwarze  270:
1.26      schwarze  271:                switch (cp[i++]) {
1.16      schwarze  272:                case ('('):
1.26      schwarze  273:                        lim = 2;
1.16      schwarze  274:                        break;
                    275:                case ('['):
1.26      schwarze  276:                        term = numeric = ']';
1.16      schwarze  277:                        break;
                    278:                case ('\''):
1.26      schwarze  279:                        term = numeric = '\'';
1.16      schwarze  280:                        break;
                    281:                default:
1.26      schwarze  282:                        lim = 1;
                    283:                        i--;
1.16      schwarze  284:                        break;
1.6       schwarze  285:                }
                    286:
1.26      schwarze  287:                /* See +/- counts as a sign. */
                    288:                c = cp[i];
                    289:                if ('+' == c || '-' == c || ASCII_HYPH == c)
                    290:                        ++i;
                    291:
                    292:                break;
                    293:
                    294:        /*
                    295:         * Anything else is assumed to be a glyph.
                    296:         */
                    297:        default:
                    298:                gly = ESCAPE_SPECIAL;
                    299:                lim = 1;
                    300:                i--;
                    301:                break;
                    302:        }
                    303:
                    304:        assert(ESCAPE_ERROR != gly);
                    305:
                    306:        rstart = &cp[i];
                    307:        if (start)
                    308:                *start = rstart;
                    309:
                    310:        /*
                    311:         * If a terminating block has been specified, we need to
                    312:         * handle the case of recursion, which could have their
                    313:         * own terminating blocks that mess up our parse.  This, by the
                    314:         * way, means that the "start" and "size" values will be
                    315:         * effectively meaningless.
                    316:         */
                    317:
                    318:        ssz = 0;
                    319:        if (numeric && -1 == (ssz = numescape(&cp[i])))
                    320:                return(ESCAPE_ERROR);
                    321:
                    322:        i += ssz;
                    323:        rlim = -1;
                    324:
                    325:        /*
                    326:         * We have a character terminator.  Try to read up to that
                    327:         * character.  If we can't (i.e., we hit the nil), then return
                    328:         * an error; if we can, calculate our length, read past the
                    329:         * terminating character, and exit.
                    330:         */
                    331:
                    332:        if ('\0' != term) {
                    333:                *end = strchr(&cp[i], term);
                    334:                if ('\0' == *end)
                    335:                        return(ESCAPE_ERROR);
                    336:
                    337:                rlim = *end - &cp[i];
                    338:                if (sz)
                    339:                        *sz = rlim;
                    340:                (*end)++;
                    341:                goto out;
                    342:        }
                    343:
                    344:        assert(lim > 0);
                    345:
                    346:        /*
                    347:         * We have a numeric limit.  If the string is shorter than that,
                    348:         * stop and return an error.  Else adjust our endpoint, length,
                    349:         * and return the current glyph.
                    350:         */
                    351:
                    352:        if ((size_t)lim > strlen(&cp[i]))
                    353:                return(ESCAPE_ERROR);
                    354:
                    355:        rlim = lim;
                    356:        if (sz)
                    357:                *sz = rlim;
1.19      schwarze  358:
1.26      schwarze  359:        *end = &cp[i] + lim;
                    360:
                    361: out:
                    362:        assert(rlim >= 0 && rstart);
1.19      schwarze  363:
1.26      schwarze  364:        /* Run post-processors. */
1.19      schwarze  365:
1.26      schwarze  366:        switch (gly) {
                    367:        case (ESCAPE_FONT):
1.30    ! schwarze  368:                /*
        !           369:                 * Pretend that the constant-width font modes are the
        !           370:                 * same as the regular font modes.
        !           371:                 */
        !           372:                if (2 == rlim && 'C' == *rstart)
        !           373:                        rstart++;
        !           374:                else if (1 != rlim)
1.26      schwarze  375:                        break;
1.30    ! schwarze  376:
1.26      schwarze  377:                switch (*rstart) {
                    378:                case ('3'):
                    379:                        /* FALLTHROUGH */
                    380:                case ('B'):
                    381:                        gly = ESCAPE_FONTBOLD;
                    382:                        break;
                    383:                case ('2'):
                    384:                        /* FALLTHROUGH */
                    385:                case ('I'):
                    386:                        gly = ESCAPE_FONTITALIC;
1.16      schwarze  387:                        break;
1.26      schwarze  388:                case ('P'):
                    389:                        gly = ESCAPE_FONTPREV;
1.16      schwarze  390:                        break;
1.26      schwarze  391:                case ('1'):
                    392:                        /* FALLTHROUGH */
                    393:                case ('R'):
                    394:                        gly = ESCAPE_FONTROMAN;
1.1       schwarze  395:                        break;
                    396:                }
1.16      schwarze  397:                break;
1.26      schwarze  398:        case (ESCAPE_SPECIAL):
                    399:                if (1 != rlim)
                    400:                        break;
                    401:                if ('c' == *rstart)
                    402:                        gly = ESCAPE_NOSPACE;
1.16      schwarze  403:                break;
1.1       schwarze  404:        default:
1.16      schwarze  405:                break;
1.1       schwarze  406:        }
                    407:
1.26      schwarze  408:        return(gly);
1.1       schwarze  409: }
                    410:
1.4       schwarze  411: void *
                    412: mandoc_calloc(size_t num, size_t size)
                    413: {
                    414:        void            *ptr;
                    415:
                    416:        ptr = calloc(num, size);
                    417:        if (NULL == ptr) {
                    418:                perror(NULL);
1.20      schwarze  419:                exit((int)MANDOCLEVEL_SYSERR);
1.4       schwarze  420:        }
                    421:
                    422:        return(ptr);
                    423: }
                    424:
                    425:
                    426: void *
                    427: mandoc_malloc(size_t size)
                    428: {
                    429:        void            *ptr;
                    430:
                    431:        ptr = malloc(size);
                    432:        if (NULL == ptr) {
                    433:                perror(NULL);
1.20      schwarze  434:                exit((int)MANDOCLEVEL_SYSERR);
1.4       schwarze  435:        }
                    436:
                    437:        return(ptr);
                    438: }
                    439:
                    440:
                    441: void *
                    442: mandoc_realloc(void *ptr, size_t size)
                    443: {
                    444:
                    445:        ptr = realloc(ptr, size);
                    446:        if (NULL == ptr) {
                    447:                perror(NULL);
1.20      schwarze  448:                exit((int)MANDOCLEVEL_SYSERR);
1.4       schwarze  449:        }
                    450:
                    451:        return(ptr);
                    452: }
                    453:
1.27      schwarze  454: char *
                    455: mandoc_strndup(const char *ptr, size_t sz)
                    456: {
                    457:        char            *p;
                    458:
                    459:        p = mandoc_malloc(sz + 1);
                    460:        memcpy(p, ptr, sz);
                    461:        p[(int)sz] = '\0';
                    462:        return(p);
                    463: }
1.4       schwarze  464:
                    465: char *
                    466: mandoc_strdup(const char *ptr)
                    467: {
                    468:        char            *p;
                    469:
                    470:        p = strdup(ptr);
                    471:        if (NULL == p) {
                    472:                perror(NULL);
1.20      schwarze  473:                exit((int)MANDOCLEVEL_SYSERR);
1.4       schwarze  474:        }
                    475:
                    476:        return(p);
1.21      schwarze  477: }
                    478:
                    479: /*
                    480:  * Parse a quoted or unquoted roff-style request or macro argument.
                    481:  * Return a pointer to the parsed argument, which is either the original
                    482:  * pointer or advanced by one byte in case the argument is quoted.
                    483:  * Null-terminate the argument in place.
                    484:  * Collapse pairs of quotes inside quoted arguments.
                    485:  * Advance the argument pointer to the next argument,
                    486:  * or to the null byte terminating the argument line.
                    487:  */
                    488: char *
1.25      schwarze  489: mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
1.21      schwarze  490: {
                    491:        char     *start, *cp;
                    492:        int       quoted, pairs, white;
                    493:
                    494:        /* Quoting can only start with a new word. */
                    495:        start = *cpp;
1.26      schwarze  496:        quoted = 0;
1.21      schwarze  497:        if ('"' == *start) {
                    498:                quoted = 1;
                    499:                start++;
1.26      schwarze  500:        }
1.21      schwarze  501:
                    502:        pairs = 0;
                    503:        white = 0;
                    504:        for (cp = start; '\0' != *cp; cp++) {
                    505:                /* Move left after quoted quotes and escaped backslashes. */
                    506:                if (pairs)
                    507:                        cp[-pairs] = cp[0];
                    508:                if ('\\' == cp[0]) {
                    509:                        if ('\\' == cp[1]) {
                    510:                                /* Poor man's copy mode. */
                    511:                                pairs++;
                    512:                                cp++;
                    513:                        } else if (0 == quoted && ' ' == cp[1])
                    514:                                /* Skip escaped blanks. */
                    515:                                cp++;
                    516:                } else if (0 == quoted) {
                    517:                        if (' ' == cp[0]) {
                    518:                                /* Unescaped blanks end unquoted args. */
                    519:                                white = 1;
                    520:                                break;
                    521:                        }
                    522:                } else if ('"' == cp[0]) {
                    523:                        if ('"' == cp[1]) {
                    524:                                /* Quoted quotes collapse. */
                    525:                                pairs++;
                    526:                                cp++;
                    527:                        } else {
                    528:                                /* Unquoted quotes end quoted args. */
                    529:                                quoted = 2;
                    530:                                break;
                    531:                        }
                    532:                }
                    533:        }
                    534:
                    535:        /* Quoted argument without a closing quote. */
1.25      schwarze  536:        if (1 == quoted)
                    537:                mandoc_msg(MANDOCERR_BADQUOTE, parse, ln, *pos, NULL);
1.21      schwarze  538:
                    539:        /* Null-terminate this argument and move to the next one. */
                    540:        if (pairs)
                    541:                cp[-pairs] = '\0';
                    542:        if ('\0' != *cp) {
                    543:                *cp++ = '\0';
                    544:                while (' ' == *cp)
                    545:                        cp++;
                    546:        }
1.24      schwarze  547:        *pos += (int)(cp - start) + (quoted ? 1 : 0);
1.21      schwarze  548:        *cpp = cp;
                    549:
1.25      schwarze  550:        if ('\0' == *cp && (white || ' ' == cp[-1]))
                    551:                mandoc_msg(MANDOCERR_EOLNSPACE, parse, ln, *pos, NULL);
1.21      schwarze  552:
                    553:        return(start);
1.4       schwarze  554: }
1.5       schwarze  555:
                    556: static int
                    557: a2time(time_t *t, const char *fmt, const char *p)
                    558: {
                    559:        struct tm        tm;
                    560:        char            *pp;
                    561:
                    562:        memset(&tm, 0, sizeof(struct tm));
                    563:
                    564:        pp = strptime(p, fmt, &tm);
                    565:        if (NULL != pp && '\0' == *pp) {
                    566:                *t = mktime(&tm);
                    567:                return(1);
                    568:        }
                    569:
                    570:        return(0);
                    571: }
                    572:
1.22      schwarze  573: static char *
                    574: time2a(time_t t)
                    575: {
1.28      schwarze  576:        struct tm       *tm;
1.23      schwarze  577:        char            *buf, *p;
                    578:        size_t           ssz;
1.22      schwarze  579:        int              isz;
                    580:
1.28      schwarze  581:        tm = localtime(&t);
1.22      schwarze  582:
1.23      schwarze  583:        /*
                    584:         * Reserve space:
                    585:         * up to 9 characters for the month (September) + blank
                    586:         * up to 2 characters for the day + comma + blank
                    587:         * 4 characters for the year and a terminating '\0'
                    588:         */
                    589:        p = buf = mandoc_malloc(10 + 4 + 4 + 1);
                    590:
1.28      schwarze  591:        if (0 == (ssz = strftime(p, 10 + 1, "%B ", tm)))
1.23      schwarze  592:                goto fail;
                    593:        p += (int)ssz;
1.22      schwarze  594:
1.28      schwarze  595:        if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)))
1.23      schwarze  596:                goto fail;
1.22      schwarze  597:        p += isz;
                    598:
1.28      schwarze  599:        if (0 == strftime(p, 4 + 1, "%Y", tm))
1.23      schwarze  600:                goto fail;
                    601:        return(buf);
                    602:
                    603: fail:
                    604:        free(buf);
                    605:        return(NULL);
1.22      schwarze  606: }
                    607:
                    608: char *
1.25      schwarze  609: mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
1.5       schwarze  610: {
1.22      schwarze  611:        char            *out;
1.5       schwarze  612:        time_t           t;
                    613:
1.22      schwarze  614:        if (NULL == in || '\0' == *in ||
                    615:            0 == strcmp(in, "$" "Mdocdate$")) {
1.25      schwarze  616:                mandoc_msg(MANDOCERR_NODATE, parse, ln, pos, NULL);
1.22      schwarze  617:                time(&t);
                    618:        }
                    619:        else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&
                    620:            !a2time(&t, "%b %d, %Y", in) &&
                    621:            !a2time(&t, "%Y-%m-%d", in)) {
1.25      schwarze  622:                mandoc_msg(MANDOCERR_BADDATE, parse, ln, pos, NULL);
1.22      schwarze  623:                t = 0;
1.5       schwarze  624:        }
1.22      schwarze  625:        out = t ? time2a(t) : NULL;
1.23      schwarze  626:        return(out ? out : mandoc_strdup(in));
1.5       schwarze  627: }
                    628:
1.9       schwarze  629: int
1.15      schwarze  630: mandoc_eos(const char *p, size_t sz, int enclosed)
1.9       schwarze  631: {
1.15      schwarze  632:        const char *q;
1.16      schwarze  633:        int found;
1.9       schwarze  634:
1.10      schwarze  635:        if (0 == sz)
                    636:                return(0);
1.9       schwarze  637:
1.11      schwarze  638:        /*
                    639:         * End-of-sentence recognition must include situations where
                    640:         * some symbols, such as `)', allow prior EOS punctuation to
1.26      schwarze  641:         * propagate outward.
1.11      schwarze  642:         */
                    643:
1.16      schwarze  644:        found = 0;
                    645:        for (q = p + (int)sz - 1; q >= p; q--) {
1.15      schwarze  646:                switch (*q) {
1.11      schwarze  647:                case ('\"'):
                    648:                        /* FALLTHROUGH */
                    649:                case ('\''):
                    650:                        /* FALLTHROUGH */
                    651:                case (']'):
                    652:                        /* FALLTHROUGH */
                    653:                case (')'):
1.15      schwarze  654:                        if (0 == found)
                    655:                                enclosed = 1;
1.11      schwarze  656:                        break;
                    657:                case ('.'):
                    658:                        /* FALLTHROUGH */
                    659:                case ('!'):
                    660:                        /* FALLTHROUGH */
                    661:                case ('?'):
1.15      schwarze  662:                        found = 1;
                    663:                        break;
1.11      schwarze  664:                default:
1.20      schwarze  665:                        return(found && (!enclosed || isalnum((unsigned char)*q)));
1.11      schwarze  666:                }
1.9       schwarze  667:        }
                    668:
1.15      schwarze  669:        return(found && !enclosed);
1.24      schwarze  670: }
                    671:
                    672: /*
1.25      schwarze  673:  * Find out whether a line is a macro line or not.  If it is, adjust the
                    674:  * current position and return one; if it isn't, return zero and don't
                    675:  * change the current position.
1.24      schwarze  676:  */
1.25      schwarze  677: int
                    678: mandoc_getcontrol(const char *cp, int *ppos)
1.24      schwarze  679: {
1.25      schwarze  680:        int             pos;
1.24      schwarze  681:
1.25      schwarze  682:        pos = *ppos;
1.24      schwarze  683:
1.25      schwarze  684:        if ('\\' == cp[pos] && '.' == cp[pos + 1])
                    685:                pos += 2;
                    686:        else if ('.' == cp[pos] || '\'' == cp[pos])
                    687:                pos++;
                    688:        else
                    689:                return(0);
1.24      schwarze  690:
1.25      schwarze  691:        while (' ' == cp[pos] || '\t' == cp[pos])
                    692:                pos++;
1.24      schwarze  693:
1.25      schwarze  694:        *ppos = pos;
                    695:        return(1);
1.9       schwarze  696: }
1.26      schwarze  697:
                    698: /*
                    699:  * Convert a string to a long that may not be <0.
                    700:  * If the string is invalid, or is less than 0, return -1.
                    701:  */
                    702: int
1.27      schwarze  703: mandoc_strntoi(const char *p, size_t sz, int base)
1.26      schwarze  704: {
                    705:        char             buf[32];
                    706:        char            *ep;
                    707:        long             v;
                    708:
                    709:        if (sz > 31)
                    710:                return(-1);
                    711:
                    712:        memcpy(buf, p, sz);
                    713:        buf[(int)sz] = '\0';
                    714:
                    715:        errno = 0;
                    716:        v = strtol(buf, &ep, base);
                    717:
                    718:        if (buf[0] == '\0' || *ep != '\0')
                    719:                return(-1);
                    720:
1.27      schwarze  721:        if (v > INT_MAX)
                    722:                v = INT_MAX;
                    723:        if (v < INT_MIN)
                    724:                v = INT_MIN;
1.26      schwarze  725:
                    726:        return((int)v);
                    727: }