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

Annotation of mandoc/term_ascii.c, Revision 1.28

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

CVSweb