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

Annotation of mandoc/out.c, Revision 1.22

1.22    ! kristaps    1: /*     $Id: out.c,v 1.21 2010/07/21 20:35:03 kristaps Exp $ */
1.1       kristaps    2: /*
1.18      kristaps    3:  * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
1.12      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.1       kristaps   21: #include <sys/types.h>
                     22:
1.6       kristaps   23: #include <assert.h>
1.1       kristaps   24: #include <ctype.h>
1.3       kristaps   25: #include <stdio.h>
1.1       kristaps   26: #include <stdlib.h>
1.6       kristaps   27: #include <string.h>
1.7       kristaps   28: #include <time.h>
1.1       kristaps   29:
                     30: #include "out.h"
                     31:
1.3       kristaps   32: /*
                     33:  * Convert a `scaling unit' to a consistent form, or fail.  Scaling
1.5       kristaps   34:  * units are documented in groff.7, mdoc.7, man.7.
1.3       kristaps   35:  */
1.1       kristaps   36: int
1.5       kristaps   37: a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
1.1       kristaps   38: {
1.4       kristaps   39:        char             buf[BUFSIZ], hasd;
1.1       kristaps   40:        int              i;
1.3       kristaps   41:        enum roffscale   unit;
1.1       kristaps   42:
1.5       kristaps   43:        if ('\0' == *src)
                     44:                return(0);
                     45:
1.4       kristaps   46:        i = hasd = 0;
                     47:
                     48:        switch (*src) {
                     49:        case ('+'):
                     50:                src++;
                     51:                break;
                     52:        case ('-'):
                     53:                buf[i++] = *src++;
                     54:                break;
                     55:        default:
                     56:                break;
                     57:        }
                     58:
1.5       kristaps   59:        if ('\0' == *src)
                     60:                return(0);
                     61:
1.4       kristaps   62:        while (i < BUFSIZ) {
                     63:                if ( ! isdigit((u_char)*src)) {
                     64:                        if ('.' != *src)
                     65:                                break;
                     66:                        else if (hasd)
                     67:                                break;
                     68:                        else
                     69:                                hasd = 1;
                     70:                }
                     71:                buf[i++] = *src++;
                     72:        }
1.1       kristaps   73:
1.3       kristaps   74:        if (BUFSIZ == i || (*src && *(src + 1)))
1.1       kristaps   75:                return(0);
                     76:
1.4       kristaps   77:        buf[i] = '\0';
1.1       kristaps   78:
1.3       kristaps   79:        switch (*src) {
                     80:        case ('c'):
                     81:                unit = SCALE_CM;
                     82:                break;
                     83:        case ('i'):
                     84:                unit = SCALE_IN;
                     85:                break;
                     86:        case ('P'):
                     87:                unit = SCALE_PC;
                     88:                break;
                     89:        case ('p'):
                     90:                unit = SCALE_PT;
                     91:                break;
                     92:        case ('f'):
                     93:                unit = SCALE_FS;
                     94:                break;
                     95:        case ('v'):
                     96:                unit = SCALE_VS;
                     97:                break;
                     98:        case ('m'):
                     99:                unit = SCALE_EM;
                    100:                break;
                    101:        case ('\0'):
1.5       kristaps  102:                if (SCALE_MAX == def)
                    103:                        return(0);
                    104:                unit = SCALE_BU;
                    105:                break;
1.3       kristaps  106:        case ('u'):
                    107:                unit = SCALE_BU;
                    108:                break;
                    109:        case ('M'):
                    110:                unit = SCALE_MM;
                    111:                break;
                    112:        case ('n'):
                    113:                unit = SCALE_EN;
                    114:                break;
                    115:        default:
1.1       kristaps  116:                return(0);
1.3       kristaps  117:        }
1.1       kristaps  118:
1.4       kristaps  119:        if ((dst->scale = atof(buf)) < 0)
1.3       kristaps  120:                dst->scale = 0;
                    121:        dst->unit = unit;
                    122:        return(1);
1.1       kristaps  123: }
1.6       kristaps  124:
                    125:
                    126: /*
                    127:  * Correctly writes the time in nroff form, which differs from standard
                    128:  * form in that a space isn't printed in lieu of the extra %e field for
                    129:  * single-digit dates.
                    130:  */
                    131: void
                    132: time2a(time_t t, char *dst, size_t sz)
                    133: {
                    134:        struct tm        tm;
                    135:        char             buf[5];
                    136:        char            *p;
                    137:        size_t           nsz;
                    138:
                    139:        assert(sz > 1);
                    140:        localtime_r(&t, &tm);
                    141:
                    142:        p = dst;
                    143:        nsz = 0;
                    144:
                    145:        dst[0] = '\0';
                    146:
                    147:        if (0 == (nsz = strftime(p, sz, "%B ", &tm)))
                    148:                return;
                    149:
                    150:        p += (int)nsz;
                    151:        sz -= nsz;
                    152:
                    153:        if (0 == strftime(buf, sizeof(buf), "%e, ", &tm))
                    154:                return;
                    155:
                    156:        nsz = strlcat(p, buf + (' ' == buf[0] ? 1 : 0), sz);
                    157:
                    158:        if (nsz >= sz)
                    159:                return;
                    160:
                    161:        p += (int)nsz;
                    162:        sz -= nsz;
                    163:
                    164:        (void)strftime(p, sz, "%Y", &tm);
                    165: }
                    166:
1.8       kristaps  167:
                    168: int
1.18      kristaps  169: a2roffdeco(enum roffdeco *d, const char **word, size_t *sz)
1.8       kristaps  170: {
1.18      kristaps  171:        int              i, j, lim;
                    172:        char             term, c;
                    173:        const char      *wp;
1.8       kristaps  174:
                    175:        *d = DECO_NONE;
1.18      kristaps  176:        lim = i = 0;
                    177:        term = '\0';
1.8       kristaps  178:        wp = *word;
                    179:
1.18      kristaps  180:        switch ((c = wp[i++])) {
1.8       kristaps  181:        case ('('):
                    182:                *d = DECO_SPECIAL;
1.18      kristaps  183:                lim = 2;
                    184:                break;
1.14      kristaps  185:        case ('F'):
                    186:                /* FALLTHROUGH */
                    187:        case ('f'):
1.18      kristaps  188:                *d = 'F' == c ? DECO_FFONT : DECO_FONT;
                    189:
                    190:                switch (wp[i++]) {
                    191:                case ('('):
                    192:                        lim = 2;
                    193:                        break;
                    194:                case ('['):
                    195:                        term = ']';
                    196:                        break;
1.14      kristaps  197:                case ('3'):
                    198:                        /* FALLTHROUGH */
                    199:                case ('B'):
                    200:                        *d = DECO_BOLD;
1.18      kristaps  201:                        return(i);
1.14      kristaps  202:                case ('2'):
                    203:                        /* FALLTHROUGH */
                    204:                case ('I'):
                    205:                        *d = DECO_ITALIC;
1.18      kristaps  206:                        return(i);
1.14      kristaps  207:                case ('P'):
                    208:                        *d = DECO_PREVIOUS;
1.18      kristaps  209:                        return(i);
1.14      kristaps  210:                case ('1'):
                    211:                        /* FALLTHROUGH */
                    212:                case ('R'):
                    213:                        *d = DECO_ROMAN;
1.18      kristaps  214:                        return(i);
1.14      kristaps  215:                default:
1.18      kristaps  216:                        i--;
                    217:                        lim = 1;
1.14      kristaps  218:                        break;
                    219:                }
1.18      kristaps  220:                break;
1.19      kristaps  221:        case ('M'):
                    222:                /* FALLTHROUGH */
                    223:        case ('m'):
1.20      kristaps  224:                /* FALLTHROUGH */
                    225:        case ('*'):
                    226:                if ('*' == c)
                    227:                        *d = DECO_RESERVED;
                    228:
1.18      kristaps  229:                switch (wp[i++]) {
1.8       kristaps  230:                case ('('):
1.18      kristaps  231:                        lim = 2;
                    232:                        break;
1.8       kristaps  233:                case ('['):
1.18      kristaps  234:                        term = ']';
                    235:                        break;
1.8       kristaps  236:                default:
1.18      kristaps  237:                        i--;
                    238:                        lim = 1;
1.14      kristaps  239:                        break;
1.8       kristaps  240:                }
1.18      kristaps  241:                break;
1.8       kristaps  242:        case ('s'):
1.18      kristaps  243:                if ('+' == wp[i] || '-' == wp[i])
                    244:                        i++;
1.8       kristaps  245:
1.18      kristaps  246:                j = ('s' != wp[i - 1]);
1.8       kristaps  247:
1.18      kristaps  248:                switch (wp[i++]) {
                    249:                case ('('):
                    250:                        lim = 2;
                    251:                        break;
                    252:                case ('['):
                    253:                        term = ']';
                    254:                        break;
1.10      kristaps  255:                case ('\''):
1.18      kristaps  256:                        term = '\'';
1.10      kristaps  257:                        break;
1.22    ! kristaps  258:                case ('0'):
        !           259:                        j++;
        !           260:                        /* FALLTHROUGH */
1.10      kristaps  261:                default:
1.18      kristaps  262:                        i--;
                    263:                        lim = 1;
1.10      kristaps  264:                        break;
1.8       kristaps  265:                }
                    266:
1.18      kristaps  267:                if ('+' == wp[i] || '-' == wp[i]) {
                    268:                        if (j++)
                    269:                                return(i);
                    270:                        i++;
                    271:                }
                    272:
                    273:                if (0 == j)
                    274:                        return(i);
                    275:                break;
                    276:        case ('['):
                    277:                *d = DECO_SPECIAL;
                    278:                term = ']';
                    279:                break;
                    280:        case ('c'):
                    281:                *d = DECO_NOSPACE;
                    282:                return(i);
                    283:        default:
1.21      kristaps  284:                *d = DECO_SSPECIAL;
1.18      kristaps  285:                i--;
                    286:                lim = 1;
                    287:                break;
                    288:        }
1.8       kristaps  289:
1.18      kristaps  290:        assert(term || lim);
                    291:        *word = &wp[i];
1.8       kristaps  292:
1.18      kristaps  293:        if (term) {
                    294:                j = i;
                    295:                while (wp[i] && wp[i] != term)
                    296:                        i++;
                    297:                if ('\0' == wp[i]) {
                    298:                        *d = DECO_NONE;
                    299:                        return(i);
1.8       kristaps  300:                }
1.10      kristaps  301:
1.18      kristaps  302:                assert(i >= j);
                    303:                *sz = (size_t)(i - j);
1.8       kristaps  304:
1.18      kristaps  305:                return(i + 1);
                    306:        }
1.8       kristaps  307:
1.18      kristaps  308:        assert(lim > 0);
                    309:        *sz = (size_t)lim;
1.11      kristaps  310:
1.18      kristaps  311:        for (j = 0; wp[i] && j < lim; j++)
                    312:                i++;
                    313:        if (j < lim)
                    314:                *d = DECO_NONE;
1.8       kristaps  315:
1.18      kristaps  316:        return(i);
1.8       kristaps  317: }

CVSweb