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

Annotation of mandoc/term_ascii.c, Revision 1.23

1.23    ! schwarze    1: /*     $Id: term_ascii.c,v 1.22 2014/03/23 11:25:26 schwarze Exp $ */
1.1       kristaps    2: /*
1.18      schwarze    3:  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.23    ! schwarze    4:  * Copyright (c) 2014 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:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     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:  */
                     18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
                     22: #include <sys/types.h>
                     23:
1.15      kristaps   24: #ifdef USE_WCHAR
                     25: # include <locale.h>
                     26: #endif
1.1       kristaps   27: #include <stdint.h>
                     28: #include <stdio.h>
                     29: #include <stdlib.h>
1.2       kristaps   30: #include <unistd.h>
1.15      kristaps   31: #ifdef USE_WCHAR
                     32: # include <wchar.h>
                     33: #endif
1.1       kristaps   34:
1.11      kristaps   35: #include "mandoc.h"
1.22      schwarze   36: #include "mandoc_aux.h"
1.1       kristaps   37: #include "out.h"
                     38: #include "term.h"
                     39: #include "main.h"
                     40:
1.16      kristaps   41: /*
                     42:  * Sadly, this doesn't seem to be defined on systems even when they
                     43:  * support it.  For the time being, remove it and let those compiling
                     44:  * the software decide for themselves what to use.
                     45:  */
                     46: #if 0
1.15      kristaps   47: #if ! defined(__STDC_ISO_10646__)
                     48: # undef USE_WCHAR
1.16      kristaps   49: #endif
1.15      kristaps   50: #endif
                     51:
1.14      kristaps   52: static struct termp     *ascii_init(enum termenc, char *);
1.8       kristaps   53: static double            ascii_hspan(const struct termp *,
1.7       kristaps   54:                                const struct roffsu *);
1.13      kristaps   55: static size_t            ascii_width(const struct termp *, int);
1.7       kristaps   56: static void              ascii_advance(struct termp *, size_t);
                     57: static void              ascii_begin(struct termp *);
                     58: static void              ascii_end(struct termp *);
1.1       kristaps   59: static void              ascii_endline(struct termp *);
1.13      kristaps   60: static void              ascii_letter(struct termp *, int);
1.23    ! schwarze   61: static void              ascii_setwidth(struct termp *, size_t);
1.1       kristaps   62:
1.15      kristaps   63: #ifdef USE_WCHAR
                     64: static void              locale_advance(struct termp *, size_t);
                     65: static void              locale_endline(struct termp *);
                     66: static void              locale_letter(struct termp *, int);
                     67: static size_t            locale_width(const struct termp *, int);
                     68: #endif
                     69:
1.14      kristaps   70: static struct termp *
                     71: ascii_init(enum termenc enc, char *outopts)
1.1       kristaps   72: {
1.20      schwarze   73:        const char      *toks[4];
1.1       kristaps   74:        char            *v;
1.14      kristaps   75:        struct termp    *p;
1.1       kristaps   76:
1.14      kristaps   77:        p = mandoc_calloc(1, sizeof(struct termp));
1.1       kristaps   78:
1.5       kristaps   79:        p->tabwidth = 5;
1.23    ! schwarze   80:        p->defrmargin = p->lastrmargin = 78;
1.5       kristaps   81:
1.1       kristaps   82:        p->begin = ascii_begin;
                     83:        p->end = ascii_end;
1.15      kristaps   84:        p->hspan = ascii_hspan;
                     85:        p->type = TERMTYPE_CHAR;
                     86:
                     87:        p->enc = TERMENC_ASCII;
                     88:        p->advance = ascii_advance;
1.1       kristaps   89:        p->endline = ascii_endline;
1.7       kristaps   90:        p->letter = ascii_letter;
1.23    ! schwarze   91:        p->setwidth = ascii_setwidth;
1.5       kristaps   92:        p->width = ascii_width;
1.1       kristaps   93:
1.17      kristaps   94: #ifdef USE_WCHAR
                     95:        if (TERMENC_ASCII != enc) {
                     96:                v = TERMENC_LOCALE == enc ?
                     97:                        setlocale(LC_ALL, "") :
1.21      schwarze   98:                        setlocale(LC_CTYPE, "en_US.UTF-8");
1.17      kristaps   99:                if (NULL != v && MB_CUR_MAX > 1) {
1.15      kristaps  100:                        p->enc = enc;
                    101:                        p->advance = locale_advance;
                    102:                        p->endline = locale_endline;
                    103:                        p->letter = locale_letter;
                    104:                        p->width = locale_width;
                    105:                }
1.17      kristaps  106:        }
1.15      kristaps  107: #endif
                    108:
1.19      schwarze  109:        toks[0] = "indent";
                    110:        toks[1] = "width";
1.20      schwarze  111:        toks[2] = "mdoc";
                    112:        toks[3] = NULL;
1.1       kristaps  113:
                    114:        while (outopts && *outopts)
                    115:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    116:                case (0):
1.19      schwarze  117:                        p->defindent = (size_t)atoi(v);
                    118:                        break;
                    119:                case (1):
1.1       kristaps  120:                        p->defrmargin = (size_t)atoi(v);
1.20      schwarze  121:                        break;
                    122:                case (2):
                    123:                        /*
                    124:                         * Temporary, undocumented mode
                    125:                         * to imitate mdoc(7) output style.
                    126:                         */
                    127:                        p->mdocstyle = 1;
                    128:                        p->defindent = 5;
1.1       kristaps  129:                        break;
                    130:                default:
                    131:                        break;
                    132:                }
                    133:
                    134:        /* Enforce a lower boundary. */
                    135:        if (p->defrmargin < 58)
                    136:                p->defrmargin = 58;
                    137:
                    138:        return(p);
1.5       kristaps  139: }
                    140:
1.14      kristaps  141: void *
                    142: ascii_alloc(char *outopts)
                    143: {
                    144:
                    145:        return(ascii_init(TERMENC_ASCII, outopts));
                    146: }
1.17      kristaps  147:
                    148: void *
                    149: utf8_alloc(char *outopts)
                    150: {
                    151:
                    152:        return(ascii_init(TERMENC_UTF8, outopts));
                    153: }
                    154:
1.14      kristaps  155:
                    156: void *
                    157: locale_alloc(char *outopts)
                    158: {
                    159:
                    160:        return(ascii_init(TERMENC_LOCALE, outopts));
1.23    ! schwarze  161: }
        !           162:
        !           163: static void
        !           164: ascii_setwidth(struct termp *p, size_t width)
        !           165: {
        !           166:        size_t   lastwidth;
        !           167:
        !           168:        lastwidth = p->defrmargin;
        !           169:        p->rmargin = p->maxrmargin = p->defrmargin =
        !           170:            width ? width : p->lastrmargin;
        !           171:        p->lastrmargin = lastwidth;
1.14      kristaps  172: }
1.5       kristaps  173:
1.6       kristaps  174: /* ARGSUSED */
1.5       kristaps  175: static size_t
1.13      kristaps  176: ascii_width(const struct termp *p, int c)
1.5       kristaps  177: {
                    178:
                    179:        return(1);
1.1       kristaps  180: }
                    181:
                    182: void
                    183: ascii_free(void *arg)
                    184: {
                    185:
                    186:        term_free((struct termp *)arg);
                    187: }
                    188:
                    189: /* ARGSUSED */
                    190: static void
1.13      kristaps  191: ascii_letter(struct termp *p, int c)
1.1       kristaps  192: {
                    193:
                    194:        putchar(c);
                    195: }
                    196:
                    197: static void
                    198: ascii_begin(struct termp *p)
                    199: {
                    200:
                    201:        (*p->headf)(p, p->argf);
                    202: }
                    203:
                    204: static void
                    205: ascii_end(struct termp *p)
                    206: {
                    207:
                    208:        (*p->footf)(p, p->argf);
                    209: }
                    210:
                    211: /* ARGSUSED */
                    212: static void
                    213: ascii_endline(struct termp *p)
                    214: {
                    215:
                    216:        putchar('\n');
                    217: }
                    218:
                    219: /* ARGSUSED */
                    220: static void
                    221: ascii_advance(struct termp *p, size_t len)
                    222: {
                    223:        size_t          i;
                    224:
                    225:        for (i = 0; i < len; i++)
                    226:                putchar(' ');
                    227: }
1.7       kristaps  228:
                    229: /* ARGSUSED */
1.8       kristaps  230: static double
1.7       kristaps  231: ascii_hspan(const struct termp *p, const struct roffsu *su)
                    232: {
                    233:        double           r;
                    234:
                    235:        /*
                    236:         * Approximate based on character width.  These are generated
                    237:         * entirely by eyeballing the screen, but appear to be correct.
                    238:         */
                    239:
                    240:        switch (su->unit) {
                    241:        case (SCALE_CM):
                    242:                r = 4 * su->scale;
                    243:                break;
                    244:        case (SCALE_IN):
                    245:                r = 10 * su->scale;
                    246:                break;
                    247:        case (SCALE_PC):
                    248:                r = (10 * su->scale) / 6;
                    249:                break;
                    250:        case (SCALE_PT):
                    251:                r = (10 * su->scale) / 72;
                    252:                break;
                    253:        case (SCALE_MM):
                    254:                r = su->scale / 1000;
                    255:                break;
                    256:        case (SCALE_VS):
                    257:                r = su->scale * 2 - 1;
                    258:                break;
                    259:        default:
                    260:                r = su->scale;
                    261:                break;
                    262:        }
                    263:
1.8       kristaps  264:        return(r);
1.7       kristaps  265: }
                    266:
1.15      kristaps  267: #ifdef USE_WCHAR
                    268: /* ARGSUSED */
                    269: static size_t
                    270: locale_width(const struct termp *p, int c)
                    271: {
                    272:        int             rc;
                    273:
                    274:        return((rc = wcwidth(c)) < 0 ? 0 : rc);
                    275: }
                    276:
                    277: /* ARGSUSED */
                    278: static void
                    279: locale_advance(struct termp *p, size_t len)
                    280: {
                    281:        size_t          i;
                    282:
                    283:        for (i = 0; i < len; i++)
                    284:                putwchar(L' ');
                    285: }
                    286:
                    287: /* ARGSUSED */
                    288: static void
                    289: locale_endline(struct termp *p)
                    290: {
                    291:
                    292:        putwchar(L'\n');
                    293: }
                    294:
                    295: /* ARGSUSED */
                    296: static void
                    297: locale_letter(struct termp *p, int c)
                    298: {
                    299:
                    300:        putwchar(c);
                    301: }
                    302: #endif

CVSweb