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

Annotation of mandoc/out.c, Revision 1.24

1.24    ! kristaps    1: /*     $Id: out.c,v 1.23 2010/07/22 23:03:15 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.23      kristaps  119:        /* FIXME: do this in the caller. */
1.4       kristaps  120:        if ((dst->scale = atof(buf)) < 0)
1.3       kristaps  121:                dst->scale = 0;
                    122:        dst->unit = unit;
                    123:        return(1);
1.1       kristaps  124: }
1.6       kristaps  125:
                    126:
                    127: /*
                    128:  * Correctly writes the time in nroff form, which differs from standard
                    129:  * form in that a space isn't printed in lieu of the extra %e field for
                    130:  * single-digit dates.
                    131:  */
                    132: void
                    133: time2a(time_t t, char *dst, size_t sz)
                    134: {
                    135:        struct tm        tm;
                    136:        char             buf[5];
                    137:        char            *p;
                    138:        size_t           nsz;
                    139:
                    140:        assert(sz > 1);
                    141:        localtime_r(&t, &tm);
                    142:
                    143:        p = dst;
                    144:        nsz = 0;
                    145:
                    146:        dst[0] = '\0';
                    147:
                    148:        if (0 == (nsz = strftime(p, sz, "%B ", &tm)))
                    149:                return;
                    150:
                    151:        p += (int)nsz;
                    152:        sz -= nsz;
                    153:
                    154:        if (0 == strftime(buf, sizeof(buf), "%e, ", &tm))
                    155:                return;
                    156:
                    157:        nsz = strlcat(p, buf + (' ' == buf[0] ? 1 : 0), sz);
                    158:
                    159:        if (nsz >= sz)
                    160:                return;
                    161:
                    162:        p += (int)nsz;
                    163:        sz -= nsz;
                    164:
                    165:        (void)strftime(p, sz, "%Y", &tm);
                    166: }
                    167:
1.8       kristaps  168:
                    169: int
1.18      kristaps  170: a2roffdeco(enum roffdeco *d, const char **word, size_t *sz)
1.8       kristaps  171: {
1.18      kristaps  172:        int              i, j, lim;
                    173:        char             term, c;
                    174:        const char      *wp;
1.8       kristaps  175:
                    176:        *d = DECO_NONE;
1.18      kristaps  177:        lim = i = 0;
                    178:        term = '\0';
1.8       kristaps  179:        wp = *word;
                    180:
1.18      kristaps  181:        switch ((c = wp[i++])) {
1.8       kristaps  182:        case ('('):
                    183:                *d = DECO_SPECIAL;
1.18      kristaps  184:                lim = 2;
                    185:                break;
1.14      kristaps  186:        case ('F'):
                    187:                /* FALLTHROUGH */
                    188:        case ('f'):
1.18      kristaps  189:                *d = 'F' == c ? DECO_FFONT : DECO_FONT;
                    190:
                    191:                switch (wp[i++]) {
                    192:                case ('('):
                    193:                        lim = 2;
                    194:                        break;
                    195:                case ('['):
                    196:                        term = ']';
                    197:                        break;
1.14      kristaps  198:                case ('3'):
                    199:                        /* FALLTHROUGH */
                    200:                case ('B'):
                    201:                        *d = DECO_BOLD;
1.18      kristaps  202:                        return(i);
1.14      kristaps  203:                case ('2'):
                    204:                        /* FALLTHROUGH */
                    205:                case ('I'):
                    206:                        *d = DECO_ITALIC;
1.18      kristaps  207:                        return(i);
1.14      kristaps  208:                case ('P'):
                    209:                        *d = DECO_PREVIOUS;
1.18      kristaps  210:                        return(i);
1.14      kristaps  211:                case ('1'):
                    212:                        /* FALLTHROUGH */
                    213:                case ('R'):
                    214:                        *d = DECO_ROMAN;
1.18      kristaps  215:                        return(i);
1.14      kristaps  216:                default:
1.18      kristaps  217:                        i--;
                    218:                        lim = 1;
1.14      kristaps  219:                        break;
                    220:                }
1.18      kristaps  221:                break;
1.19      kristaps  222:        case ('M'):
                    223:                /* FALLTHROUGH */
                    224:        case ('m'):
1.20      kristaps  225:                /* FALLTHROUGH */
                    226:        case ('*'):
                    227:                if ('*' == c)
                    228:                        *d = DECO_RESERVED;
                    229:
1.18      kristaps  230:                switch (wp[i++]) {
1.8       kristaps  231:                case ('('):
1.18      kristaps  232:                        lim = 2;
                    233:                        break;
1.8       kristaps  234:                case ('['):
1.18      kristaps  235:                        term = ']';
                    236:                        break;
1.8       kristaps  237:                default:
1.18      kristaps  238:                        i--;
                    239:                        lim = 1;
1.14      kristaps  240:                        break;
1.8       kristaps  241:                }
1.18      kristaps  242:                break;
1.24    ! kristaps  243:        case ('h'):
        !           244:                /* FALLTHROUGH */
        !           245:        case ('v'):
        !           246:                /* FALLTHROUGH */
1.8       kristaps  247:        case ('s'):
1.24    ! kristaps  248:                j = 0;
        !           249:                if ('+' == wp[i] || '-' == wp[i]) {
1.18      kristaps  250:                        i++;
1.24    ! kristaps  251:                        j = 1;
        !           252:                }
1.8       kristaps  253:
1.18      kristaps  254:                switch (wp[i++]) {
                    255:                case ('('):
                    256:                        lim = 2;
                    257:                        break;
                    258:                case ('['):
                    259:                        term = ']';
                    260:                        break;
1.10      kristaps  261:                case ('\''):
1.18      kristaps  262:                        term = '\'';
1.10      kristaps  263:                        break;
1.22      kristaps  264:                case ('0'):
1.24    ! kristaps  265:                        j = 1;
1.22      kristaps  266:                        /* FALLTHROUGH */
1.10      kristaps  267:                default:
1.18      kristaps  268:                        i--;
                    269:                        lim = 1;
1.10      kristaps  270:                        break;
1.8       kristaps  271:                }
                    272:
1.18      kristaps  273:                if ('+' == wp[i] || '-' == wp[i]) {
1.24    ! kristaps  274:                        if (j)
1.18      kristaps  275:                                return(i);
                    276:                        i++;
                    277:                }
                    278:
                    279:                break;
                    280:        case ('['):
                    281:                *d = DECO_SPECIAL;
                    282:                term = ']';
                    283:                break;
                    284:        case ('c'):
                    285:                *d = DECO_NOSPACE;
                    286:                return(i);
                    287:        default:
1.21      kristaps  288:                *d = DECO_SSPECIAL;
1.18      kristaps  289:                i--;
                    290:                lim = 1;
                    291:                break;
                    292:        }
1.8       kristaps  293:
1.18      kristaps  294:        assert(term || lim);
                    295:        *word = &wp[i];
1.8       kristaps  296:
1.18      kristaps  297:        if (term) {
                    298:                j = i;
                    299:                while (wp[i] && wp[i] != term)
                    300:                        i++;
                    301:                if ('\0' == wp[i]) {
                    302:                        *d = DECO_NONE;
                    303:                        return(i);
1.8       kristaps  304:                }
1.10      kristaps  305:
1.18      kristaps  306:                assert(i >= j);
                    307:                *sz = (size_t)(i - j);
1.8       kristaps  308:
1.18      kristaps  309:                return(i + 1);
                    310:        }
1.8       kristaps  311:
1.18      kristaps  312:        assert(lim > 0);
                    313:        *sz = (size_t)lim;
1.11      kristaps  314:
1.18      kristaps  315:        for (j = 0; wp[i] && j < lim; j++)
                    316:                i++;
                    317:        if (j < lim)
                    318:                *d = DECO_NONE;
1.8       kristaps  319:
1.18      kristaps  320:        return(i);
1.8       kristaps  321: }

CVSweb