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

Annotation of mandoc/term_ascii.c, Revision 1.42

1.42    ! schwarze    1: /*     $Id: term_ascii.c,v 1.41 2014/12/19 17:12:04 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.39      schwarze   22: #include <assert.h>
1.31      schwarze   23: #if HAVE_WCHAR
1.28      schwarze   24: #include <locale.h>
1.15      kristaps   25: #endif
1.1       kristaps   26: #include <stdint.h>
                     27: #include <stdio.h>
                     28: #include <stdlib.h>
1.2       kristaps   29: #include <unistd.h>
1.31      schwarze   30: #if HAVE_WCHAR
1.28      schwarze   31: #include <wchar.h>
1.15      kristaps   32: #endif
1.1       kristaps   33:
1.11      kristaps   34: #include "mandoc.h"
1.22      schwarze   35: #include "mandoc_aux.h"
1.1       kristaps   36: #include "out.h"
                     37: #include "term.h"
                     38: #include "main.h"
                     39:
1.38      schwarze   40: static struct termp     *ascii_init(enum termenc,
                     41:                                const struct mchars *, char *);
1.8       kristaps   42: static double            ascii_hspan(const struct termp *,
1.7       kristaps   43:                                const struct roffsu *);
1.13      kristaps   44: static size_t            ascii_width(const struct termp *, int);
1.7       kristaps   45: static void              ascii_advance(struct termp *, size_t);
                     46: static void              ascii_begin(struct termp *);
                     47: static void              ascii_end(struct termp *);
1.1       kristaps   48: static void              ascii_endline(struct termp *);
1.13      kristaps   49: static void              ascii_letter(struct termp *, int);
1.24      schwarze   50: static void              ascii_setwidth(struct termp *, int, size_t);
1.1       kristaps   51:
1.31      schwarze   52: #if HAVE_WCHAR
1.15      kristaps   53: static void              locale_advance(struct termp *, size_t);
                     54: static void              locale_endline(struct termp *);
                     55: static void              locale_letter(struct termp *, int);
                     56: static size_t            locale_width(const struct termp *, int);
                     57: #endif
                     58:
1.25      schwarze   59:
1.14      kristaps   60: static struct termp *
1.38      schwarze   61: ascii_init(enum termenc enc, const struct mchars *mchars, char *outopts)
1.1       kristaps   62: {
1.33      schwarze   63:        const char      *toks[5];
1.1       kristaps   64:        char            *v;
1.14      kristaps   65:        struct termp    *p;
1.1       kristaps   66:
1.14      kristaps   67:        p = mandoc_calloc(1, sizeof(struct termp));
1.1       kristaps   68:
1.38      schwarze   69:        p->symtab = mchars;
1.5       kristaps   70:        p->tabwidth = 5;
1.23      schwarze   71:        p->defrmargin = p->lastrmargin = 78;
1.41      schwarze   72:        p->fontq = mandoc_reallocarray(NULL,
                     73:             (p->fontsz = 8), sizeof(enum termfont));
                     74:        p->fontq[0] = p->fontl = TERMFONT_NONE;
1.5       kristaps   75:
1.1       kristaps   76:        p->begin = ascii_begin;
                     77:        p->end = ascii_end;
1.15      kristaps   78:        p->hspan = ascii_hspan;
                     79:        p->type = TERMTYPE_CHAR;
                     80:
                     81:        p->enc = TERMENC_ASCII;
                     82:        p->advance = ascii_advance;
1.1       kristaps   83:        p->endline = ascii_endline;
1.7       kristaps   84:        p->letter = ascii_letter;
1.23      schwarze   85:        p->setwidth = ascii_setwidth;
1.5       kristaps   86:        p->width = ascii_width;
1.1       kristaps   87:
1.31      schwarze   88: #if HAVE_WCHAR
1.17      kristaps   89:        if (TERMENC_ASCII != enc) {
                     90:                v = TERMENC_LOCALE == enc ?
1.25      schwarze   91:                    setlocale(LC_ALL, "") :
                     92:                    setlocale(LC_CTYPE, "en_US.UTF-8");
1.17      kristaps   93:                if (NULL != v && MB_CUR_MAX > 1) {
1.15      kristaps   94:                        p->enc = enc;
                     95:                        p->advance = locale_advance;
                     96:                        p->endline = locale_endline;
                     97:                        p->letter = locale_letter;
                     98:                        p->width = locale_width;
                     99:                }
1.17      kristaps  100:        }
1.15      kristaps  101: #endif
                    102:
1.19      schwarze  103:        toks[0] = "indent";
                    104:        toks[1] = "width";
1.20      schwarze  105:        toks[2] = "mdoc";
1.33      schwarze  106:        toks[3] = "synopsis";
                    107:        toks[4] = NULL;
1.1       kristaps  108:
                    109:        while (outopts && *outopts)
                    110:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.25      schwarze  111:                case 0:
1.19      schwarze  112:                        p->defindent = (size_t)atoi(v);
                    113:                        break;
1.25      schwarze  114:                case 1:
1.1       kristaps  115:                        p->defrmargin = (size_t)atoi(v);
1.20      schwarze  116:                        break;
1.25      schwarze  117:                case 2:
1.20      schwarze  118:                        /*
                    119:                         * Temporary, undocumented mode
                    120:                         * to imitate mdoc(7) output style.
                    121:                         */
                    122:                        p->mdocstyle = 1;
                    123:                        p->defindent = 5;
1.33      schwarze  124:                        break;
                    125:                case 3:
                    126:                        p->synopsisonly = 1;
1.1       kristaps  127:                        break;
                    128:                default:
                    129:                        break;
                    130:                }
                    131:
                    132:        /* Enforce a lower boundary. */
                    133:        if (p->defrmargin < 58)
                    134:                p->defrmargin = 58;
                    135:
                    136:        return(p);
1.5       kristaps  137: }
                    138:
1.14      kristaps  139: void *
1.38      schwarze  140: ascii_alloc(const struct mchars *mchars, char *outopts)
1.14      kristaps  141: {
                    142:
1.38      schwarze  143:        return(ascii_init(TERMENC_ASCII, mchars, outopts));
1.14      kristaps  144: }
1.17      kristaps  145:
                    146: void *
1.38      schwarze  147: utf8_alloc(const struct mchars *mchars, char *outopts)
1.17      kristaps  148: {
                    149:
1.38      schwarze  150:        return(ascii_init(TERMENC_UTF8, mchars, outopts));
1.17      kristaps  151: }
                    152:
1.14      kristaps  153: void *
1.38      schwarze  154: locale_alloc(const struct mchars *mchars, char *outopts)
1.14      kristaps  155: {
                    156:
1.38      schwarze  157:        return(ascii_init(TERMENC_LOCALE, mchars, outopts));
1.23      schwarze  158: }
                    159:
                    160: static void
1.24      schwarze  161: ascii_setwidth(struct termp *p, int iop, size_t width)
1.23      schwarze  162: {
                    163:
1.24      schwarze  164:        p->rmargin = p->defrmargin;
1.40      schwarze  165:        if (iop > 0)
1.24      schwarze  166:                p->defrmargin += width;
1.40      schwarze  167:        else if (iop == 0)
                    168:                p->defrmargin = width ? width : p->lastrmargin;
                    169:        else if (p->defrmargin > width)
1.24      schwarze  170:                p->defrmargin -= width;
                    171:        else
1.40      schwarze  172:                p->defrmargin = 0;
1.24      schwarze  173:        p->lastrmargin = p->rmargin;
                    174:        p->rmargin = p->maxrmargin = p->defrmargin;
1.42    ! schwarze  175: }
        !           176:
        !           177: void
        !           178: ascii_sepline(void *arg)
        !           179: {
        !           180:        struct termp    *p;
        !           181:        size_t           i;
        !           182:
        !           183:        p = (struct termp *)arg;
        !           184:        putchar('\n');
        !           185:        for (i = 0; i < p->defrmargin; i++)
        !           186:                putchar('-');
        !           187:        putchar('\n');
        !           188:        putchar('\n');
1.34      schwarze  189: }
                    190:
1.5       kristaps  191: static size_t
1.13      kristaps  192: ascii_width(const struct termp *p, int c)
1.5       kristaps  193: {
                    194:
                    195:        return(1);
1.1       kristaps  196: }
                    197:
                    198: void
                    199: ascii_free(void *arg)
                    200: {
                    201:
                    202:        term_free((struct termp *)arg);
                    203: }
                    204:
                    205: static void
1.13      kristaps  206: ascii_letter(struct termp *p, int c)
1.1       kristaps  207: {
1.25      schwarze  208:
1.1       kristaps  209:        putchar(c);
                    210: }
                    211:
                    212: static void
                    213: ascii_begin(struct termp *p)
                    214: {
                    215:
                    216:        (*p->headf)(p, p->argf);
                    217: }
                    218:
                    219: static void
                    220: ascii_end(struct termp *p)
                    221: {
                    222:
                    223:        (*p->footf)(p, p->argf);
                    224: }
                    225:
                    226: static void
                    227: ascii_endline(struct termp *p)
                    228: {
                    229:
                    230:        putchar('\n');
                    231: }
                    232:
                    233: static void
                    234: ascii_advance(struct termp *p, size_t len)
                    235: {
1.25      schwarze  236:        size_t          i;
1.1       kristaps  237:
                    238:        for (i = 0; i < len; i++)
                    239:                putchar(' ');
                    240: }
1.7       kristaps  241:
1.8       kristaps  242: static double
1.7       kristaps  243: ascii_hspan(const struct termp *p, const struct roffsu *su)
                    244: {
                    245:        double           r;
                    246:
                    247:        /*
1.29      kristaps  248:         * Approximate based on character width.
                    249:         * None of these will be actually correct given that an inch on
                    250:         * the screen depends on character size, terminal, etc., etc.
1.7       kristaps  251:         */
                    252:        switch (su->unit) {
1.29      kristaps  253:        case SCALE_BU:
                    254:                r = su->scale * 10.0 / 240.0;
                    255:                break;
1.25      schwarze  256:        case SCALE_CM:
1.29      kristaps  257:                r = su->scale * 10.0 / 2.54;
                    258:                break;
                    259:        case SCALE_FS:
                    260:                r = su->scale * 2730.666;
1.7       kristaps  261:                break;
1.25      schwarze  262:        case SCALE_IN:
1.27      schwarze  263:                r = su->scale * 10.0;
1.7       kristaps  264:                break;
1.29      kristaps  265:        case SCALE_MM:
                    266:                r = su->scale / 100.0;
                    267:                break;
1.25      schwarze  268:        case SCALE_PC:
1.29      kristaps  269:                r = su->scale * 10.0 / 6.0;
1.7       kristaps  270:                break;
1.25      schwarze  271:        case SCALE_PT:
1.29      kristaps  272:                r = su->scale * 10.0 / 72.0;
1.7       kristaps  273:                break;
1.25      schwarze  274:        case SCALE_VS:
1.27      schwarze  275:                r = su->scale * 2.0 - 1.0;
1.7       kristaps  276:                break;
1.29      kristaps  277:        case SCALE_EN:
1.30      schwarze  278:                /* FALLTHROUGH */
1.29      kristaps  279:        case SCALE_EM:
1.7       kristaps  280:                r = su->scale;
1.29      kristaps  281:                break;
1.32      schwarze  282:        default:
1.29      kristaps  283:                abort();
1.30      schwarze  284:                /* NOTREACHED */
1.7       kristaps  285:        }
                    286:
1.8       kristaps  287:        return(r);
1.36      schwarze  288: }
                    289:
                    290: const char *
                    291: ascii_uc2str(int uc)
                    292: {
                    293:        static const char nbrsp[2] = { ASCII_NBRSP, '\0' };
                    294:        static const char *tab[] = {
                    295:        "<NUL>","<SOH>","<STX>","<ETX>","<EOT>","<ENQ>","<ACK>","<BEL>",
                    296:        "<BS>", "\t",   "<LF>", "<VT>", "<FF>", "<CR>", "<SO>", "<SI>",
                    297:        "<DLE>","<DC1>","<DC2>","<DC3>","<DC4>","<NAK>","<SYN>","<ETB>",
                    298:        "<CAN>","<EM>", "<SUB>","<ESC>","<FS>", "<GS>", "<RS>", "<US>",
                    299:        " ",    "!",    "\"",   "#",    "$",    "%",    "&",    "'",
                    300:        "(",    ")",    "*",    "+",    ",",    "-",    ".",    "/",
                    301:        "0",    "1",    "2",    "3",    "4",    "5",    "6",    "7",
                    302:        "8",    "9",    ":",    ";",    "<",    "=",    ">",    "?",
                    303:        "@",    "A",    "B",    "C",    "D",    "E",    "F",    "G",
                    304:        "H",    "I",    "J",    "K",    "L",    "M",    "N",    "O",
                    305:        "P",    "Q",    "R",    "S",    "T",    "U",    "V",    "W",
                    306:        "X",    "Y",    "Z",    "[",    "\\",   "]",    "^",    "_",
                    307:        "`",    "a",    "b",    "c",    "d",    "e",    "f",    "g",
                    308:        "h",    "i",    "j",    "k",    "l",    "m",    "n",    "o",
                    309:        "p",    "q",    "r",    "s",    "t",    "u",    "v",    "w",
                    310:        "x",    "y",    "z",    "{",    "|",    "}",    "~",    "<DEL>",
                    311:        "<80>", "<81>", "<82>", "<83>", "<84>", "<85>", "<86>", "<87>",
                    312:        "<88>", "<89>", "<8A>", "<8B>", "<8C>", "<8D>", "<8E>", "<8F>",
                    313:        "<90>", "<91>", "<92>", "<93>", "<94>", "<95>", "<96>", "<97>",
                    314:        "<99>", "<99>", "<9A>", "<9B>", "<9C>", "<9D>", "<9E>", "<9F>",
1.37      schwarze  315:        nbrsp,  "!",    "/\bc", "GBP",  "o\bx", "=\bY", "|",    "<sec>",
                    316:        "\"",   "(C)",  "_\ba", "<<",   "~",    "",     "(R)",  "-",
                    317:        "<deg>","+-",   "2",    "3",    "'",    ",\bu", "<par>",".",
                    318:        ",",    "1",    "_\bo", ">>",   "1/4",  "1/2",  "3/4",  "?",
                    319:        "`\bA", "'\bA", "^\bA", "~\bA", "\"\bA","o\bA", "AE",   ",\bC",
                    320:        "`\bE", "'\bE", "^\bE", "\"\bE","`\bI", "'\bI", "^\bI", "\"\bI",
                    321:        "-\bD", "~\bN", "`\bO", "'\bO", "^\bO", "~\bO", "\"\bO","x",
                    322:        "/\bO", "`\bU", "'\bU", "^\bU", "\"\bU","'\bY", "Th",   "ss",
                    323:        "`\ba", "'\ba", "^\ba", "~\ba", "\"\ba","o\ba", "ae",   ",\bc",
                    324:        "`\be", "'\be", "^\be", "\"\be","`\bi", "'\bi", "^\bi", "\"\bi",
                    325:        "d",    "~\bn", "`\bo", "'\bo", "^\bo", "~\bo", "\"\bo","-:-",
                    326:        "/\bo", "`\bu", "'\bu", "^\bu", "\"\bu","'\by", "th",   "\"\by",
                    327:        "A",    "a",    "A",    "a",    "A",    "a",    "'\bC", "'\bc",
                    328:        "^\bC", "^\bc", "C",    "c",    "C",    "c",    "D",    "d",
                    329:        "/\bD", "/\bd", "E",    "e",    "E",    "e",    "E",    "e",
                    330:        "E",    "e",    "E",    "e",    "^\bG", "^\bg", "G",    "g",
                    331:        "G",    "g",    ",\bG", ",\bg", "^\bH", "^\bh", "/\bH", "/\bh",
                    332:        "~\bI", "~\bi", "I",    "i",    "I",    "i",    "I",    "i",
                    333:        "I",    "i",    "IJ",   "ij",   "^\bJ", "^\bj", ",\bK", ",\bk",
                    334:        "q",    "'\bL", "'\bl", ",\bL", ",\bl", "L",    "l",    "L",
                    335:        "l",    "/\bL", "/\bl", "'\bN", "'\bn", ",\bN", ",\bn", "N",
1.36      schwarze  336:        "n",    "'n",   "Ng",   "ng",   "O",    "o",    "O",    "o",
1.37      schwarze  337:        "O",    "o",    "OE",   "oe",   "'\bR", "'\br", ",\bR", ",\br",
                    338:        "R",    "r",    "'\bS", "'\bs", "^\bS", "^\bs", ",\bS", ",\bs",
                    339:        "S",    "s",    ",\bT", ",\bt", "T",    "t",    "/\bT", "/\bt",
                    340:        "~\bU", "~\bu", "U",    "u",    "U",    "u",    "U",    "u",
                    341:        "U",    "u",    "U",    "u",    "^\bW", "^\bw", "^\bY", "^\by",
                    342:        "\"\bY","'\bZ", "'\bz", "Z",    "z",    "Z",    "z",    "s",
1.36      schwarze  343:        "b",    "B",    "B",    "b",    "6",    "6",    "O",    "C",
                    344:        "c",    "D",    "D",    "D",    "d",    "d",    "3",    "@",
1.37      schwarze  345:        "E",    "F",    ",\bf", "G",    "G",    "hv",   "I",    "/\bI",
                    346:        "K",    "k",    "/\bl", "l",    "W",    "N",    "n",    "~\bO",
1.36      schwarze  347:        "O",    "o",    "OI",   "oi",   "P",    "p",    "YR",   "2",
                    348:        "2",    "SH",   "sh",   "t",    "T",    "t",    "T",    "U",
1.37      schwarze  349:        "u",    "Y",    "V",    "Y",    "y",    "/\bZ", "/\bz", "ZH",
                    350:        "ZH",   "zh",   "zh",   "/\b2", "5",    "5",    "ts",   "w",
1.36      schwarze  351:        "|",    "||",   "|=",   "!",    "DZ",   "Dz",   "dz",   "LJ",
                    352:        "Lj",   "lj",   "NJ",   "Nj",   "nj",   "A",    "a",    "I",
                    353:        "i",    "O",    "o",    "U",    "u",    "U",    "u",    "U",
                    354:        "u",    "U",    "u",    "U",    "u",    "@",    "A",    "a",
1.37      schwarze  355:        "A",    "a",    "AE",   "ae",   "/\bG", "/\bg", "G",    "g",
1.36      schwarze  356:        "K",    "k",    "O",    "o",    "O",    "o",    "ZH",   "zh",
1.37      schwarze  357:        "j",    "DZ",   "Dz",   "dz",   "'\bG", "'\bg", "HV",   "W",
                    358:        "`\bN", "`\bn", "A",    "a",    "'\bAE","'\bae","O",    "o"};
1.36      schwarze  359:
1.39      schwarze  360:        assert(uc >= 0);
1.36      schwarze  361:        if ((size_t)uc < sizeof(tab)/sizeof(tab[0]))
                    362:                return(tab[uc]);
                    363:        return(mchars_uc2str(uc));
1.7       kristaps  364: }
                    365:
1.31      schwarze  366: #if HAVE_WCHAR
1.15      kristaps  367: static size_t
                    368: locale_width(const struct termp *p, int c)
                    369: {
                    370:        int             rc;
                    371:
1.26      schwarze  372:        if (c == ASCII_NBRSP)
                    373:                c = ' ';
                    374:        rc = wcwidth(c);
                    375:        if (rc < 0)
                    376:                rc = 0;
                    377:        return(rc);
1.15      kristaps  378: }
                    379:
                    380: static void
                    381: locale_advance(struct termp *p, size_t len)
                    382: {
1.25      schwarze  383:        size_t          i;
1.15      kristaps  384:
                    385:        for (i = 0; i < len; i++)
                    386:                putwchar(L' ');
                    387: }
                    388:
                    389: static void
                    390: locale_endline(struct termp *p)
                    391: {
                    392:
                    393:        putwchar(L'\n');
                    394: }
                    395:
                    396: static void
                    397: locale_letter(struct termp *p, int c)
                    398: {
1.25      schwarze  399:
1.15      kristaps  400:        putwchar(c);
                    401: }
                    402: #endif

CVSweb