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

Annotation of mandoc/mandoc.c, Revision 1.60

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

CVSweb