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

Annotation of mandoc/mandoc.c, Revision 1.63

1.63    ! schwarze    1: /*     $Id: mandoc.c,v 1.62 2011/12/03 16:08:51 schwarze Exp $ */
1.1       kristaps    2: /*
1.59      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.63    ! schwarze    4:  * Copyright (c) 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
1.36      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.36      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       kristaps   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
1.9       kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
1.7       kristaps   20: #endif
                     21:
1.2       kristaps   22: #include <sys/types.h>
                     23:
1.1       kristaps   24: #include <assert.h>
                     25: #include <ctype.h>
1.50      kristaps   26: #include <errno.h>
                     27: #include <limits.h>
1.1       kristaps   28: #include <stdlib.h>
1.4       kristaps   29: #include <stdio.h>
                     30: #include <string.h>
1.7       kristaps   31: #include <time.h>
1.1       kristaps   32:
1.18      kristaps   33: #include "mandoc.h"
1.1       kristaps   34: #include "libmandoc.h"
                     35:
1.37      schwarze   36: #define DATESIZE 32
                     37:
1.18      kristaps   38: static int      a2time(time_t *, const char *, const char *);
1.37      schwarze   39: static char    *time2a(time_t);
1.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.63    ! schwarze  144:
        !           145:        /*
        !           146:         * The \z escape is supposed to output the following
        !           147:         * character without advancing the cursor position.
        !           148:         * Since we are mostly dealing with terminal mode,
        !           149:         * let us just skip the next character.
        !           150:         */
        !           151:        case ('z'):
        !           152:                (*end)++;
        !           153:                return(ESCAPE_SKIPCHAR);
1.1       kristaps  154:
1.45      kristaps  155:        /*
                    156:         * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
                    157:         * 'X' is the trigger.  These have opaque sub-strings.
                    158:         */
                    159:        case ('F'):
                    160:                /* FALLTHROUGH */
                    161:        case ('g'):
1.24      kristaps  162:                /* FALLTHROUGH */
1.45      kristaps  163:        case ('k'):
1.24      kristaps  164:                /* FALLTHROUGH */
1.45      kristaps  165:        case ('M'):
1.24      kristaps  166:                /* FALLTHROUGH */
1.45      kristaps  167:        case ('m'):
1.24      kristaps  168:                /* FALLTHROUGH */
1.45      kristaps  169:        case ('n'):
1.24      kristaps  170:                /* FALLTHROUGH */
1.45      kristaps  171:        case ('V'):
1.24      kristaps  172:                /* FALLTHROUGH */
1.45      kristaps  173:        case ('Y'):
1.60      schwarze  174:                gly = ESCAPE_IGNORE;
1.24      kristaps  175:                /* FALLTHROUGH */
1.45      kristaps  176:        case ('f'):
                    177:                if (ESCAPE_ERROR == gly)
                    178:                        gly = ESCAPE_FONT;
                    179:
                    180:                rstart= &cp[i];
                    181:                if (start)
                    182:                        *start = rstart;
                    183:
                    184:                switch (cp[i++]) {
                    185:                case ('('):
                    186:                        lim = 2;
                    187:                        break;
                    188:                case ('['):
                    189:                        term = ']';
                    190:                        break;
                    191:                default:
                    192:                        lim = 1;
                    193:                        i--;
                    194:                        break;
                    195:                }
                    196:                break;
                    197:
                    198:        /*
                    199:         * These escapes are of the form \X'Y', where 'X' is the trigger
                    200:         * and 'Y' is any string.  These have opaque sub-strings.
                    201:         */
                    202:        case ('A'):
1.24      kristaps  203:                /* FALLTHROUGH */
1.45      kristaps  204:        case ('b'):
1.24      kristaps  205:                /* FALLTHROUGH */
                    206:        case ('D'):
                    207:                /* FALLTHROUGH */
1.45      kristaps  208:        case ('o'):
1.24      kristaps  209:                /* FALLTHROUGH */
1.45      kristaps  210:        case ('R'):
1.24      kristaps  211:                /* FALLTHROUGH */
1.45      kristaps  212:        case ('X'):
1.24      kristaps  213:                /* FALLTHROUGH */
1.45      kristaps  214:        case ('Z'):
                    215:                if ('\'' != cp[i++])
                    216:                        return(ESCAPE_ERROR);
                    217:                gly = ESCAPE_IGNORE;
1.24      kristaps  218:                term = '\'';
                    219:                break;
1.45      kristaps  220:
                    221:        /*
                    222:         * These escapes are of the form \X'N', where 'X' is the trigger
                    223:         * and 'N' resolves to a numerical expression.
                    224:         */
                    225:        case ('B'):
                    226:                /* FALLTHROUGH */
1.28      kristaps  227:        case ('h'):
                    228:                /* FALLTHROUGH */
1.45      kristaps  229:        case ('H'):
                    230:                /* FALLTHROUGH */
                    231:        case ('L'):
                    232:                /* FALLTHROUGH */
                    233:        case ('l'):
1.60      schwarze  234:                gly = ESCAPE_NUMBERED;
1.45      kristaps  235:                /* FALLTHROUGH */
                    236:        case ('S'):
                    237:                /* FALLTHROUGH */
1.28      kristaps  238:        case ('v'):
                    239:                /* FALLTHROUGH */
1.45      kristaps  240:        case ('w'):
                    241:                /* FALLTHROUGH */
                    242:        case ('x'):
                    243:                if (ESCAPE_ERROR == gly)
                    244:                        gly = ESCAPE_IGNORE;
                    245:                if ('\'' != cp[i++])
                    246:                        return(ESCAPE_ERROR);
                    247:                term = numeric = '\'';
                    248:                break;
1.60      schwarze  249:
                    250:        /*
                    251:         * Special handling for the numbered character escape.
                    252:         * XXX Do any other escapes need similar handling?
                    253:         */
                    254:        case ('N'):
                    255:                if ('\0' == cp[i])
                    256:                        return(ESCAPE_ERROR);
                    257:                *end = &cp[++i];
                    258:                if (isdigit((unsigned char)cp[i-1]))
                    259:                        return(ESCAPE_IGNORE);
                    260:                while (isdigit((unsigned char)**end))
                    261:                        (*end)++;
                    262:                if (start)
                    263:                        *start = &cp[i];
                    264:                if (sz)
                    265:                        *sz = *end - &cp[i];
                    266:                if ('\0' != **end)
                    267:                        (*end)++;
                    268:                return(ESCAPE_NUMBERED);
1.45      kristaps  269:
                    270:        /*
                    271:         * Sizes get a special category of their own.
                    272:         */
1.8       kristaps  273:        case ('s'):
1.45      kristaps  274:                gly = ESCAPE_IGNORE;
1.28      kristaps  275:
1.45      kristaps  276:                rstart = &cp[i];
                    277:                if (start)
                    278:                        *start = rstart;
                    279:
                    280:                /* See +/- counts as a sign. */
                    281:                c = cp[i];
                    282:                if ('+' == c || '-' == c || ASCII_HYPH == c)
                    283:                        ++i;
1.8       kristaps  284:
1.45      kristaps  285:                switch (cp[i++]) {
1.22      kristaps  286:                case ('('):
1.45      kristaps  287:                        lim = 2;
1.22      kristaps  288:                        break;
                    289:                case ('['):
1.45      kristaps  290:                        term = numeric = ']';
1.22      kristaps  291:                        break;
                    292:                case ('\''):
1.45      kristaps  293:                        term = numeric = '\'';
1.22      kristaps  294:                        break;
                    295:                default:
1.45      kristaps  296:                        lim = 1;
                    297:                        i--;
1.22      kristaps  298:                        break;
1.8       kristaps  299:                }
                    300:
1.45      kristaps  301:                /* See +/- counts as a sign. */
                    302:                c = cp[i];
                    303:                if ('+' == c || '-' == c || ASCII_HYPH == c)
                    304:                        ++i;
                    305:
                    306:                break;
1.33      kristaps  307:
1.45      kristaps  308:        /*
                    309:         * Anything else is assumed to be a glyph.
                    310:         */
                    311:        default:
                    312:                gly = ESCAPE_SPECIAL;
                    313:                lim = 1;
                    314:                i--;
1.22      kristaps  315:                break;
1.45      kristaps  316:        }
                    317:
                    318:        assert(ESCAPE_ERROR != gly);
                    319:
                    320:        rstart = &cp[i];
                    321:        if (start)
                    322:                *start = rstart;
                    323:
                    324:        /*
                    325:         * If a terminating block has been specified, we need to
                    326:         * handle the case of recursion, which could have their
                    327:         * own terminating blocks that mess up our parse.  This, by the
                    328:         * way, means that the "start" and "size" values will be
                    329:         * effectively meaningless.
                    330:         */
                    331:
                    332:        ssz = 0;
                    333:        if (numeric && -1 == (ssz = numescape(&cp[i])))
                    334:                return(ESCAPE_ERROR);
                    335:
                    336:        i += ssz;
                    337:        rlim = -1;
                    338:
                    339:        /*
                    340:         * We have a character terminator.  Try to read up to that
                    341:         * character.  If we can't (i.e., we hit the nil), then return
                    342:         * an error; if we can, calculate our length, read past the
                    343:         * terminating character, and exit.
                    344:         */
                    345:
                    346:        if ('\0' != term) {
                    347:                *end = strchr(&cp[i], term);
                    348:                if ('\0' == *end)
                    349:                        return(ESCAPE_ERROR);
                    350:
                    351:                rlim = *end - &cp[i];
                    352:                if (sz)
                    353:                        *sz = rlim;
                    354:                (*end)++;
                    355:                goto out;
                    356:        }
                    357:
                    358:        assert(lim > 0);
                    359:
                    360:        /*
                    361:         * We have a numeric limit.  If the string is shorter than that,
                    362:         * stop and return an error.  Else adjust our endpoint, length,
                    363:         * and return the current glyph.
                    364:         */
                    365:
                    366:        if ((size_t)lim > strlen(&cp[i]))
                    367:                return(ESCAPE_ERROR);
                    368:
                    369:        rlim = lim;
                    370:        if (sz)
                    371:                *sz = rlim;
                    372:
                    373:        *end = &cp[i] + lim;
                    374:
                    375: out:
                    376:        assert(rlim >= 0 && rstart);
                    377:
                    378:        /* Run post-processors. */
                    379:
                    380:        switch (gly) {
                    381:        case (ESCAPE_FONT):
1.61      kristaps  382:                /*
                    383:                 * Pretend that the constant-width font modes are the
                    384:                 * same as the regular font modes.
                    385:                 */
                    386:                if (2 == rlim && 'C' == *rstart)
                    387:                        rstart++;
                    388:                else if (1 != rlim)
1.45      kristaps  389:                        break;
1.61      kristaps  390:
1.45      kristaps  391:                switch (*rstart) {
                    392:                case ('3'):
                    393:                        /* FALLTHROUGH */
                    394:                case ('B'):
                    395:                        gly = ESCAPE_FONTBOLD;
                    396:                        break;
                    397:                case ('2'):
                    398:                        /* FALLTHROUGH */
                    399:                case ('I'):
                    400:                        gly = ESCAPE_FONTITALIC;
1.22      kristaps  401:                        break;
1.45      kristaps  402:                case ('P'):
                    403:                        gly = ESCAPE_FONTPREV;
1.22      kristaps  404:                        break;
1.45      kristaps  405:                case ('1'):
                    406:                        /* FALLTHROUGH */
                    407:                case ('R'):
                    408:                        gly = ESCAPE_FONTROMAN;
1.1       kristaps  409:                        break;
                    410:                }
1.46      kristaps  411:                break;
1.45      kristaps  412:        case (ESCAPE_SPECIAL):
                    413:                if (1 != rlim)
                    414:                        break;
                    415:                if ('c' == *rstart)
                    416:                        gly = ESCAPE_NOSPACE;
1.22      kristaps  417:                break;
1.1       kristaps  418:        default:
1.22      kristaps  419:                break;
1.1       kristaps  420:        }
                    421:
1.45      kristaps  422:        return(gly);
1.1       kristaps  423: }
1.4       kristaps  424:
                    425: void *
                    426: mandoc_calloc(size_t num, size_t size)
                    427: {
                    428:        void            *ptr;
                    429:
                    430:        ptr = calloc(num, size);
                    431:        if (NULL == ptr) {
1.6       kristaps  432:                perror(NULL);
1.35      kristaps  433:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  434:        }
                    435:
                    436:        return(ptr);
                    437: }
                    438:
                    439:
                    440: void *
                    441: mandoc_malloc(size_t size)
                    442: {
                    443:        void            *ptr;
                    444:
                    445:        ptr = malloc(size);
                    446:        if (NULL == ptr) {
1.6       kristaps  447:                perror(NULL);
1.35      kristaps  448:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  449:        }
                    450:
                    451:        return(ptr);
                    452: }
                    453:
                    454:
                    455: void *
                    456: mandoc_realloc(void *ptr, size_t size)
                    457: {
                    458:
                    459:        ptr = realloc(ptr, size);
                    460:        if (NULL == ptr) {
1.6       kristaps  461:                perror(NULL);
1.35      kristaps  462:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  463:        }
                    464:
                    465:        return(ptr);
                    466: }
                    467:
1.55      kristaps  468: char *
                    469: mandoc_strndup(const char *ptr, size_t sz)
                    470: {
                    471:        char            *p;
                    472:
                    473:        p = mandoc_malloc(sz + 1);
                    474:        memcpy(p, ptr, sz);
                    475:        p[(int)sz] = '\0';
                    476:        return(p);
                    477: }
1.4       kristaps  478:
                    479: char *
                    480: mandoc_strdup(const char *ptr)
                    481: {
                    482:        char            *p;
                    483:
                    484:        p = strdup(ptr);
                    485:        if (NULL == p) {
1.6       kristaps  486:                perror(NULL);
1.35      kristaps  487:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps  488:        }
                    489:
                    490:        return(p);
1.36      schwarze  491: }
                    492:
                    493: /*
                    494:  * Parse a quoted or unquoted roff-style request or macro argument.
                    495:  * Return a pointer to the parsed argument, which is either the original
                    496:  * pointer or advanced by one byte in case the argument is quoted.
                    497:  * Null-terminate the argument in place.
                    498:  * Collapse pairs of quotes inside quoted arguments.
                    499:  * Advance the argument pointer to the next argument,
                    500:  * or to the null byte terminating the argument line.
                    501:  */
                    502: char *
1.48      kristaps  503: mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
1.36      schwarze  504: {
                    505:        char     *start, *cp;
                    506:        int       quoted, pairs, white;
                    507:
                    508:        /* Quoting can only start with a new word. */
                    509:        start = *cpp;
1.47      kristaps  510:        quoted = 0;
1.36      schwarze  511:        if ('"' == *start) {
                    512:                quoted = 1;
                    513:                start++;
1.47      kristaps  514:        }
1.36      schwarze  515:
                    516:        pairs = 0;
                    517:        white = 0;
                    518:        for (cp = start; '\0' != *cp; cp++) {
                    519:                /* Move left after quoted quotes and escaped backslashes. */
                    520:                if (pairs)
                    521:                        cp[-pairs] = cp[0];
                    522:                if ('\\' == cp[0]) {
                    523:                        if ('\\' == cp[1]) {
                    524:                                /* Poor man's copy mode. */
                    525:                                pairs++;
                    526:                                cp++;
                    527:                        } else if (0 == quoted && ' ' == cp[1])
                    528:                                /* Skip escaped blanks. */
                    529:                                cp++;
                    530:                } else if (0 == quoted) {
                    531:                        if (' ' == cp[0]) {
                    532:                                /* Unescaped blanks end unquoted args. */
                    533:                                white = 1;
                    534:                                break;
                    535:                        }
                    536:                } else if ('"' == cp[0]) {
                    537:                        if ('"' == cp[1]) {
                    538:                                /* Quoted quotes collapse. */
                    539:                                pairs++;
                    540:                                cp++;
                    541:                        } else {
                    542:                                /* Unquoted quotes end quoted args. */
                    543:                                quoted = 2;
                    544:                                break;
                    545:                        }
                    546:                }
                    547:        }
                    548:
                    549:        /* Quoted argument without a closing quote. */
1.48      kristaps  550:        if (1 == quoted)
1.42      kristaps  551:                mandoc_msg(MANDOCERR_BADQUOTE, parse, ln, *pos, NULL);
1.36      schwarze  552:
                    553:        /* Null-terminate this argument and move to the next one. */
                    554:        if (pairs)
                    555:                cp[-pairs] = '\0';
                    556:        if ('\0' != *cp) {
                    557:                *cp++ = '\0';
                    558:                while (' ' == *cp)
                    559:                        cp++;
                    560:        }
1.39      kristaps  561:        *pos += (int)(cp - start) + (quoted ? 1 : 0);
1.36      schwarze  562:        *cpp = cp;
                    563:
1.48      kristaps  564:        if ('\0' == *cp && (white || ' ' == cp[-1]))
1.42      kristaps  565:                mandoc_msg(MANDOCERR_EOLNSPACE, parse, ln, *pos, NULL);
1.36      schwarze  566:
                    567:        return(start);
1.4       kristaps  568: }
1.7       kristaps  569:
                    570: static int
                    571: a2time(time_t *t, const char *fmt, const char *p)
                    572: {
                    573:        struct tm        tm;
                    574:        char            *pp;
                    575:
                    576:        memset(&tm, 0, sizeof(struct tm));
                    577:
1.56      kristaps  578:        pp = NULL;
                    579: #ifdef HAVE_STRPTIME
1.7       kristaps  580:        pp = strptime(p, fmt, &tm);
1.56      kristaps  581: #endif
1.7       kristaps  582:        if (NULL != pp && '\0' == *pp) {
                    583:                *t = mktime(&tm);
                    584:                return(1);
                    585:        }
                    586:
                    587:        return(0);
                    588: }
                    589:
1.37      schwarze  590: static char *
                    591: time2a(time_t t)
                    592: {
1.56      kristaps  593:        struct tm       *tm;
1.38      schwarze  594:        char            *buf, *p;
                    595:        size_t           ssz;
1.37      schwarze  596:        int              isz;
                    597:
1.56      kristaps  598:        tm = localtime(&t);
1.37      schwarze  599:
1.38      schwarze  600:        /*
                    601:         * Reserve space:
                    602:         * up to 9 characters for the month (September) + blank
                    603:         * up to 2 characters for the day + comma + blank
                    604:         * 4 characters for the year and a terminating '\0'
                    605:         */
                    606:        p = buf = mandoc_malloc(10 + 4 + 4 + 1);
                    607:
1.56      kristaps  608:        if (0 == (ssz = strftime(p, 10 + 1, "%B ", tm)))
1.38      schwarze  609:                goto fail;
                    610:        p += (int)ssz;
1.37      schwarze  611:
1.56      kristaps  612:        if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)))
1.38      schwarze  613:                goto fail;
1.37      schwarze  614:        p += isz;
                    615:
1.56      kristaps  616:        if (0 == strftime(p, 4 + 1, "%Y", tm))
1.38      schwarze  617:                goto fail;
                    618:        return(buf);
                    619:
                    620: fail:
                    621:        free(buf);
                    622:        return(NULL);
1.37      schwarze  623: }
                    624:
                    625: char *
1.42      kristaps  626: mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
1.7       kristaps  627: {
1.37      schwarze  628:        char            *out;
1.7       kristaps  629:        time_t           t;
                    630:
1.37      schwarze  631:        if (NULL == in || '\0' == *in ||
                    632:            0 == strcmp(in, "$" "Mdocdate$")) {
1.42      kristaps  633:                mandoc_msg(MANDOCERR_NODATE, parse, ln, pos, NULL);
1.37      schwarze  634:                time(&t);
                    635:        }
1.62      schwarze  636:        else if (a2time(&t, "%Y-%m-%d", in))
                    637:                t = 0;
1.37      schwarze  638:        else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&
1.62      schwarze  639:            !a2time(&t, "%b %d, %Y", in)) {
1.42      kristaps  640:                mandoc_msg(MANDOCERR_BADDATE, parse, ln, pos, NULL);
1.37      schwarze  641:                t = 0;
1.7       kristaps  642:        }
1.37      schwarze  643:        out = t ? time2a(t) : NULL;
1.38      schwarze  644:        return(out ? out : mandoc_strdup(in));
1.7       kristaps  645: }
                    646:
1.12      kristaps  647: int
1.23      schwarze  648: mandoc_eos(const char *p, size_t sz, int enclosed)
1.12      kristaps  649: {
1.23      schwarze  650:        const char *q;
                    651:        int found;
1.12      kristaps  652:
1.13      kristaps  653:        if (0 == sz)
                    654:                return(0);
1.12      kristaps  655:
1.14      kristaps  656:        /*
                    657:         * End-of-sentence recognition must include situations where
                    658:         * some symbols, such as `)', allow prior EOS punctuation to
1.49      kristaps  659:         * propagate outward.
1.14      kristaps  660:         */
                    661:
1.23      schwarze  662:        found = 0;
1.25      kristaps  663:        for (q = p + (int)sz - 1; q >= p; q--) {
1.23      schwarze  664:                switch (*q) {
1.14      kristaps  665:                case ('\"'):
                    666:                        /* FALLTHROUGH */
                    667:                case ('\''):
1.15      kristaps  668:                        /* FALLTHROUGH */
                    669:                case (']'):
1.14      kristaps  670:                        /* FALLTHROUGH */
                    671:                case (')'):
1.23      schwarze  672:                        if (0 == found)
                    673:                                enclosed = 1;
1.14      kristaps  674:                        break;
                    675:                case ('.'):
                    676:                        /* FALLTHROUGH */
                    677:                case ('!'):
                    678:                        /* FALLTHROUGH */
                    679:                case ('?'):
1.23      schwarze  680:                        found = 1;
                    681:                        break;
1.14      kristaps  682:                default:
1.27      joerg     683:                        return(found && (!enclosed || isalnum((unsigned char)*q)));
1.14      kristaps  684:                }
1.12      kristaps  685:        }
                    686:
1.23      schwarze  687:        return(found && !enclosed);
1.40      kristaps  688: }
                    689:
1.44      kristaps  690: /*
                    691:  * Find out whether a line is a macro line or not.  If it is, adjust the
                    692:  * current position and return one; if it isn't, return zero and don't
                    693:  * change the current position.
                    694:  */
                    695: int
                    696: mandoc_getcontrol(const char *cp, int *ppos)
                    697: {
                    698:        int             pos;
                    699:
                    700:        pos = *ppos;
                    701:
                    702:        if ('\\' == cp[pos] && '.' == cp[pos + 1])
                    703:                pos += 2;
                    704:        else if ('.' == cp[pos] || '\'' == cp[pos])
                    705:                pos++;
                    706:        else
                    707:                return(0);
                    708:
                    709:        while (' ' == cp[pos] || '\t' == cp[pos])
                    710:                pos++;
                    711:
                    712:        *ppos = pos;
                    713:        return(1);
                    714: }
1.50      kristaps  715:
                    716: /*
                    717:  * Convert a string to a long that may not be <0.
                    718:  * If the string is invalid, or is less than 0, return -1.
                    719:  */
                    720: int
1.54      kristaps  721: mandoc_strntoi(const char *p, size_t sz, int base)
1.50      kristaps  722: {
                    723:        char             buf[32];
                    724:        char            *ep;
                    725:        long             v;
                    726:
                    727:        if (sz > 31)
                    728:                return(-1);
                    729:
                    730:        memcpy(buf, p, sz);
1.51      kristaps  731:        buf[(int)sz] = '\0';
1.50      kristaps  732:
                    733:        errno = 0;
                    734:        v = strtol(buf, &ep, base);
                    735:
                    736:        if (buf[0] == '\0' || *ep != '\0')
                    737:                return(-1);
                    738:
1.54      kristaps  739:        if (v > INT_MAX)
                    740:                v = INT_MAX;
                    741:        if (v < INT_MIN)
                    742:                v = INT_MIN;
1.50      kristaps  743:
                    744:        return((int)v);
                    745: }

CVSweb