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

Annotation of mandoc/mandoc.c, Revision 1.45

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

CVSweb