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

Annotation of mandoc/term_ascii.c, Revision 1.66

1.66    ! schwarze    1: /* $Id: term_ascii.c,v 1.65 2020/09/06 14:45:22 schwarze Exp $ */
1.1       kristaps    2: /*
1.18      schwarze    3:  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.65      schwarze    4:  * Copyright (c) 2014,2015,2017,2018,2020 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:  *
1.44      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.44      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       kristaps   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.60      schwarze   24: #include <langinfo.h>
1.28      schwarze   25: #include <locale.h>
1.15      kristaps   26: #endif
1.1       kristaps   27: #include <stdint.h>
                     28: #include <stdio.h>
                     29: #include <stdlib.h>
1.60      schwarze   30: #include <string.h>
1.2       kristaps   31: #include <unistd.h>
1.31      schwarze   32: #if HAVE_WCHAR
1.28      schwarze   33: #include <wchar.h>
1.15      kristaps   34: #endif
1.1       kristaps   35:
1.11      kristaps   36: #include "mandoc.h"
1.22      schwarze   37: #include "mandoc_aux.h"
1.1       kristaps   38: #include "out.h"
                     39: #include "term.h"
1.44      schwarze   40: #include "manconf.h"
1.1       kristaps   41: #include "main.h"
                     42:
1.51      schwarze   43: static struct termp     *ascii_init(enum termenc, const struct manoutput *);
1.45      schwarze   44: static int               ascii_hspan(const struct termp *,
1.7       kristaps   45:                                const struct roffsu *);
1.13      kristaps   46: static size_t            ascii_width(const struct termp *, int);
1.7       kristaps   47: static void              ascii_advance(struct termp *, size_t);
                     48: static void              ascii_begin(struct termp *);
                     49: static void              ascii_end(struct termp *);
1.1       kristaps   50: static void              ascii_endline(struct termp *);
1.13      kristaps   51: static void              ascii_letter(struct termp *, int);
1.45      schwarze   52: static void              ascii_setwidth(struct termp *, int, int);
1.1       kristaps   53:
1.31      schwarze   54: #if HAVE_WCHAR
1.15      kristaps   55: static void              locale_advance(struct termp *, size_t);
                     56: static void              locale_endline(struct termp *);
                     57: static void              locale_letter(struct termp *, int);
                     58: static size_t            locale_width(const struct termp *, int);
                     59: #endif
                     60:
1.25      schwarze   61:
1.14      kristaps   62: static struct termp *
1.51      schwarze   63: ascii_init(enum termenc enc, const struct manoutput *outopts)
1.1       kristaps   64: {
1.46      schwarze   65: #if HAVE_WCHAR
1.1       kristaps   66:        char            *v;
1.46      schwarze   67: #endif
1.14      kristaps   68:        struct termp    *p;
1.1       kristaps   69:
1.57      schwarze   70:        p = mandoc_calloc(1, sizeof(*p));
                     71:        p->tcol = p->tcols = mandoc_calloc(1, sizeof(*p->tcol));
                     72:        p->maxtcol = 1;
1.1       kristaps   73:
1.47      schwarze   74:        p->line = 1;
1.23      schwarze   75:        p->defrmargin = p->lastrmargin = 78;
1.41      schwarze   76:        p->fontq = mandoc_reallocarray(NULL,
1.57      schwarze   77:             (p->fontsz = 8), sizeof(*p->fontq));
1.41      schwarze   78:        p->fontq[0] = p->fontl = TERMFONT_NONE;
1.5       kristaps   79:
1.1       kristaps   80:        p->begin = ascii_begin;
                     81:        p->end = ascii_end;
1.15      kristaps   82:        p->hspan = ascii_hspan;
                     83:        p->type = TERMTYPE_CHAR;
                     84:
                     85:        p->enc = TERMENC_ASCII;
                     86:        p->advance = ascii_advance;
1.1       kristaps   87:        p->endline = ascii_endline;
1.7       kristaps   88:        p->letter = ascii_letter;
1.23      schwarze   89:        p->setwidth = ascii_setwidth;
1.5       kristaps   90:        p->width = ascii_width;
1.1       kristaps   91:
1.31      schwarze   92: #if HAVE_WCHAR
1.64      schwarze   93:        if (enc != TERMENC_ASCII) {
1.52      schwarze   94:
                     95:                /*
                     96:                 * Do not change any of this to LC_ALL.  It might break
                     97:                 * the formatting by subtly changing the behaviour of
                     98:                 * various functions, for example strftime(3).  As a
                     99:                 * worst case, it might even cause buffer overflows.
                    100:                 */
                    101:
1.64      schwarze  102:                v = enc == TERMENC_LOCALE ?
1.52      schwarze  103:                    setlocale(LC_CTYPE, "") :
1.54      schwarze  104:                    setlocale(LC_CTYPE, UTF8_LOCALE);
1.60      schwarze  105:
                    106:                /*
                    107:                 * We only support UTF-8,
                    108:                 * so revert to ASCII for anything else.
                    109:                 */
                    110:
                    111:                if (v != NULL &&
                    112:                    strcmp(nl_langinfo(CODESET), "UTF-8") != 0)
                    113:                        v = setlocale(LC_CTYPE, "C");
                    114:
                    115:                if (v != NULL && MB_CUR_MAX > 1) {
1.64      schwarze  116:                        p->enc = TERMENC_UTF8;
1.15      kristaps  117:                        p->advance = locale_advance;
                    118:                        p->endline = locale_endline;
                    119:                        p->letter = locale_letter;
                    120:                        p->width = locale_width;
                    121:                }
1.17      kristaps  122:        }
1.15      kristaps  123: #endif
                    124:
1.44      schwarze  125:        if (outopts->mdoc) {
                    126:                p->mdocstyle = 1;
                    127:                p->defindent = 5;
                    128:        }
                    129:        if (outopts->indent)
                    130:                p->defindent = outopts->indent;
                    131:        if (outopts->width)
                    132:                p->defrmargin = outopts->width;
                    133:        if (outopts->synopsisonly)
                    134:                p->synopsisonly = 1;
1.1       kristaps  135:
1.61      schwarze  136:        assert(p->defindent < UINT16_MAX);
                    137:        assert(p->defrmargin < UINT16_MAX);
1.49      schwarze  138:        return p;
1.5       kristaps  139: }
                    140:
1.14      kristaps  141: void *
1.51      schwarze  142: ascii_alloc(const struct manoutput *outopts)
1.14      kristaps  143: {
                    144:
1.51      schwarze  145:        return ascii_init(TERMENC_ASCII, outopts);
1.14      kristaps  146: }
1.17      kristaps  147:
                    148: void *
1.51      schwarze  149: utf8_alloc(const struct manoutput *outopts)
1.17      kristaps  150: {
                    151:
1.51      schwarze  152:        return ascii_init(TERMENC_UTF8, outopts);
1.17      kristaps  153: }
                    154:
1.14      kristaps  155: void *
1.51      schwarze  156: locale_alloc(const struct manoutput *outopts)
1.14      kristaps  157: {
                    158:
1.51      schwarze  159:        return ascii_init(TERMENC_LOCALE, outopts);
1.23      schwarze  160: }
                    161:
                    162: static void
1.45      schwarze  163: ascii_setwidth(struct termp *p, int iop, int width)
1.23      schwarze  164: {
                    165:
1.45      schwarze  166:        width /= 24;
1.57      schwarze  167:        p->tcol->rmargin = p->defrmargin;
1.40      schwarze  168:        if (iop > 0)
1.24      schwarze  169:                p->defrmargin += width;
1.40      schwarze  170:        else if (iop == 0)
1.45      schwarze  171:                p->defrmargin = width ? (size_t)width : p->lastrmargin;
                    172:        else if (p->defrmargin > (size_t)width)
1.24      schwarze  173:                p->defrmargin -= width;
                    174:        else
1.40      schwarze  175:                p->defrmargin = 0;
1.61      schwarze  176:        if (p->defrmargin > 1000)
                    177:                p->defrmargin = 1000;
1.57      schwarze  178:        p->lastrmargin = p->tcol->rmargin;
                    179:        p->tcol->rmargin = p->maxrmargin = p->defrmargin;
1.42      schwarze  180: }
                    181:
                    182: void
1.53      schwarze  183: terminal_sepline(void *arg)
1.42      schwarze  184: {
                    185:        struct termp    *p;
                    186:        size_t           i;
                    187:
                    188:        p = (struct termp *)arg;
1.53      schwarze  189:        (*p->endline)(p);
1.42      schwarze  190:        for (i = 0; i < p->defrmargin; i++)
1.53      schwarze  191:                (*p->letter)(p, '-');
                    192:        (*p->endline)(p);
                    193:        (*p->endline)(p);
1.34      schwarze  194: }
                    195:
1.5       kristaps  196: static size_t
1.13      kristaps  197: ascii_width(const struct termp *p, int c)
1.5       kristaps  198: {
1.62      schwarze  199:        return c != ASCII_BREAK;
1.1       kristaps  200: }
                    201:
                    202: void
                    203: ascii_free(void *arg)
                    204: {
                    205:
                    206:        term_free((struct termp *)arg);
                    207: }
                    208:
                    209: static void
1.13      kristaps  210: ascii_letter(struct termp *p, int c)
1.1       kristaps  211: {
1.25      schwarze  212:
1.1       kristaps  213:        putchar(c);
                    214: }
                    215:
                    216: static void
                    217: ascii_begin(struct termp *p)
                    218: {
                    219:
                    220:        (*p->headf)(p, p->argf);
                    221: }
                    222:
                    223: static void
                    224: ascii_end(struct termp *p)
                    225: {
                    226:
                    227:        (*p->footf)(p, p->argf);
                    228: }
                    229:
                    230: static void
                    231: ascii_endline(struct termp *p)
                    232: {
                    233:
1.47      schwarze  234:        p->line++;
1.65      schwarze  235:        if ((int)p->tcol->offset > p->ti)
                    236:                p->tcol->offset -= p->ti;
                    237:        else
                    238:                p->tcol->offset = 0;
1.56      schwarze  239:        p->ti = 0;
1.1       kristaps  240:        putchar('\n');
                    241: }
                    242:
                    243: static void
                    244: ascii_advance(struct termp *p, size_t len)
                    245: {
1.25      schwarze  246:        size_t          i;
1.1       kristaps  247:
1.66    ! schwarze  248:        /*
        !           249:         * XXX We used to have "assert(len < UINT16_MAX)" here.
        !           250:         * that is not quite right because the input document
        !           251:         * can trigger that by merely providing large input.
        !           252:         * For now, simply truncate.
        !           253:         */
        !           254:        if (len > 256)
        !           255:                len = 256;
1.1       kristaps  256:        for (i = 0; i < len; i++)
                    257:                putchar(' ');
                    258: }
1.7       kristaps  259:
1.45      schwarze  260: static int
1.7       kristaps  261: ascii_hspan(const struct termp *p, const struct roffsu *su)
                    262: {
                    263:        double           r;
                    264:
                    265:        switch (su->unit) {
1.29      kristaps  266:        case SCALE_BU:
1.45      schwarze  267:                r = su->scale;
1.29      kristaps  268:                break;
1.25      schwarze  269:        case SCALE_CM:
1.45      schwarze  270:                r = su->scale * 240.0 / 2.54;
1.29      kristaps  271:                break;
                    272:        case SCALE_FS:
1.45      schwarze  273:                r = su->scale * 65536.0;
1.7       kristaps  274:                break;
1.25      schwarze  275:        case SCALE_IN:
1.45      schwarze  276:                r = su->scale * 240.0;
1.7       kristaps  277:                break;
1.29      kristaps  278:        case SCALE_MM:
1.45      schwarze  279:                r = su->scale * 0.24;
1.29      kristaps  280:                break;
1.45      schwarze  281:        case SCALE_VS:
1.25      schwarze  282:        case SCALE_PC:
1.45      schwarze  283:                r = su->scale * 40.0;
1.7       kristaps  284:                break;
1.25      schwarze  285:        case SCALE_PT:
1.45      schwarze  286:                r = su->scale * 10.0 / 3.0;
1.7       kristaps  287:                break;
1.29      kristaps  288:        case SCALE_EN:
                    289:        case SCALE_EM:
1.45      schwarze  290:                r = su->scale * 24.0;
1.29      kristaps  291:                break;
1.32      schwarze  292:        default:
1.29      kristaps  293:                abort();
1.7       kristaps  294:        }
1.49      schwarze  295:        return r > 0.0 ? r + 0.01 : r - 0.01;
1.36      schwarze  296: }
                    297:
                    298: const char *
                    299: ascii_uc2str(int uc)
                    300: {
                    301:        static const char nbrsp[2] = { ASCII_NBRSP, '\0' };
                    302:        static const char *tab[] = {
                    303:        "<NUL>","<SOH>","<STX>","<ETX>","<EOT>","<ENQ>","<ACK>","<BEL>",
                    304:        "<BS>", "\t",   "<LF>", "<VT>", "<FF>", "<CR>", "<SO>", "<SI>",
                    305:        "<DLE>","<DC1>","<DC2>","<DC3>","<DC4>","<NAK>","<SYN>","<ETB>",
                    306:        "<CAN>","<EM>", "<SUB>","<ESC>","<FS>", "<GS>", "<RS>", "<US>",
                    307:        " ",    "!",    "\"",   "#",    "$",    "%",    "&",    "'",
                    308:        "(",    ")",    "*",    "+",    ",",    "-",    ".",    "/",
                    309:        "0",    "1",    "2",    "3",    "4",    "5",    "6",    "7",
                    310:        "8",    "9",    ":",    ";",    "<",    "=",    ">",    "?",
                    311:        "@",    "A",    "B",    "C",    "D",    "E",    "F",    "G",
                    312:        "H",    "I",    "J",    "K",    "L",    "M",    "N",    "O",
                    313:        "P",    "Q",    "R",    "S",    "T",    "U",    "V",    "W",
                    314:        "X",    "Y",    "Z",    "[",    "\\",   "]",    "^",    "_",
                    315:        "`",    "a",    "b",    "c",    "d",    "e",    "f",    "g",
                    316:        "h",    "i",    "j",    "k",    "l",    "m",    "n",    "o",
                    317:        "p",    "q",    "r",    "s",    "t",    "u",    "v",    "w",
                    318:        "x",    "y",    "z",    "{",    "|",    "}",    "~",    "<DEL>",
                    319:        "<80>", "<81>", "<82>", "<83>", "<84>", "<85>", "<86>", "<87>",
                    320:        "<88>", "<89>", "<8A>", "<8B>", "<8C>", "<8D>", "<8E>", "<8F>",
                    321:        "<90>", "<91>", "<92>", "<93>", "<94>", "<95>", "<96>", "<97>",
1.58      schwarze  322:        "<98>", "<99>", "<9A>", "<9B>", "<9C>", "<9D>", "<9E>", "<9F>",
1.63      schwarze  323:        nbrsp,  "!",    "/\bc", "-\bL", "o\bx", "=\bY", "|",    "<section>",
1.37      schwarze  324:        "\"",   "(C)",  "_\ba", "<<",   "~",    "",     "(R)",  "-",
1.59      schwarze  325:        "<degree>","+-","^2",   "^3",   "'","<micro>","<paragraph>",".",
                    326:        ",",    "^1",   "_\bo", ">>",   "1/4",  "1/2",  "3/4",  "?",
1.37      schwarze  327:        "`\bA", "'\bA", "^\bA", "~\bA", "\"\bA","o\bA", "AE",   ",\bC",
                    328:        "`\bE", "'\bE", "^\bE", "\"\bE","`\bI", "'\bI", "^\bI", "\"\bI",
1.59      schwarze  329:        "Dh",   "~\bN", "`\bO", "'\bO", "^\bO", "~\bO", "\"\bO","x",
1.37      schwarze  330:        "/\bO", "`\bU", "'\bU", "^\bU", "\"\bU","'\bY", "Th",   "ss",
                    331:        "`\ba", "'\ba", "^\ba", "~\ba", "\"\ba","o\ba", "ae",   ",\bc",
                    332:        "`\be", "'\be", "^\be", "\"\be","`\bi", "'\bi", "^\bi", "\"\bi",
1.59      schwarze  333:        "dh",   "~\bn", "`\bo", "'\bo", "^\bo", "~\bo", "\"\bo","/",
1.37      schwarze  334:        "/\bo", "`\bu", "'\bu", "^\bu", "\"\bu","'\by", "th",   "\"\by",
                    335:        "A",    "a",    "A",    "a",    "A",    "a",    "'\bC", "'\bc",
                    336:        "^\bC", "^\bc", "C",    "c",    "C",    "c",    "D",    "d",
                    337:        "/\bD", "/\bd", "E",    "e",    "E",    "e",    "E",    "e",
                    338:        "E",    "e",    "E",    "e",    "^\bG", "^\bg", "G",    "g",
                    339:        "G",    "g",    ",\bG", ",\bg", "^\bH", "^\bh", "/\bH", "/\bh",
                    340:        "~\bI", "~\bi", "I",    "i",    "I",    "i",    "I",    "i",
                    341:        "I",    "i",    "IJ",   "ij",   "^\bJ", "^\bj", ",\bK", ",\bk",
                    342:        "q",    "'\bL", "'\bl", ",\bL", ",\bl", "L",    "l",    "L",
                    343:        "l",    "/\bL", "/\bl", "'\bN", "'\bn", ",\bN", ",\bn", "N",
1.36      schwarze  344:        "n",    "'n",   "Ng",   "ng",   "O",    "o",    "O",    "o",
1.37      schwarze  345:        "O",    "o",    "OE",   "oe",   "'\bR", "'\br", ",\bR", ",\br",
                    346:        "R",    "r",    "'\bS", "'\bs", "^\bS", "^\bs", ",\bS", ",\bs",
                    347:        "S",    "s",    ",\bT", ",\bt", "T",    "t",    "/\bT", "/\bt",
                    348:        "~\bU", "~\bu", "U",    "u",    "U",    "u",    "U",    "u",
                    349:        "U",    "u",    "U",    "u",    "^\bW", "^\bw", "^\bY", "^\by",
                    350:        "\"\bY","'\bZ", "'\bz", "Z",    "z",    "Z",    "z",    "s",
1.36      schwarze  351:        "b",    "B",    "B",    "b",    "6",    "6",    "O",    "C",
                    352:        "c",    "D",    "D",    "D",    "d",    "d",    "3",    "@",
1.37      schwarze  353:        "E",    "F",    ",\bf", "G",    "G",    "hv",   "I",    "/\bI",
                    354:        "K",    "k",    "/\bl", "l",    "W",    "N",    "n",    "~\bO",
1.36      schwarze  355:        "O",    "o",    "OI",   "oi",   "P",    "p",    "YR",   "2",
                    356:        "2",    "SH",   "sh",   "t",    "T",    "t",    "T",    "U",
1.37      schwarze  357:        "u",    "Y",    "V",    "Y",    "y",    "/\bZ", "/\bz", "ZH",
                    358:        "ZH",   "zh",   "zh",   "/\b2", "5",    "5",    "ts",   "w",
1.36      schwarze  359:        "|",    "||",   "|=",   "!",    "DZ",   "Dz",   "dz",   "LJ",
                    360:        "Lj",   "lj",   "NJ",   "Nj",   "nj",   "A",    "a",    "I",
                    361:        "i",    "O",    "o",    "U",    "u",    "U",    "u",    "U",
                    362:        "u",    "U",    "u",    "U",    "u",    "@",    "A",    "a",
1.37      schwarze  363:        "A",    "a",    "AE",   "ae",   "/\bG", "/\bg", "G",    "g",
1.36      schwarze  364:        "K",    "k",    "O",    "o",    "O",    "o",    "ZH",   "zh",
1.37      schwarze  365:        "j",    "DZ",   "Dz",   "dz",   "'\bG", "'\bg", "HV",   "W",
                    366:        "`\bN", "`\bn", "A",    "a",    "'\bAE","'\bae","O",    "o"};
1.36      schwarze  367:
1.39      schwarze  368:        assert(uc >= 0);
1.36      schwarze  369:        if ((size_t)uc < sizeof(tab)/sizeof(tab[0]))
1.49      schwarze  370:                return tab[uc];
                    371:        return mchars_uc2str(uc);
1.7       kristaps  372: }
                    373:
1.31      schwarze  374: #if HAVE_WCHAR
1.15      kristaps  375: static size_t
                    376: locale_width(const struct termp *p, int c)
                    377: {
                    378:        int             rc;
                    379:
1.26      schwarze  380:        if (c == ASCII_NBRSP)
                    381:                c = ' ';
                    382:        rc = wcwidth(c);
                    383:        if (rc < 0)
                    384:                rc = 0;
1.49      schwarze  385:        return rc;
1.15      kristaps  386: }
                    387:
                    388: static void
                    389: locale_advance(struct termp *p, size_t len)
                    390: {
1.25      schwarze  391:        size_t          i;
1.15      kristaps  392:
1.66    ! schwarze  393:        /*
        !           394:         * XXX We used to have "assert(len < UINT16_MAX)" here.
        !           395:         * that is not quite right because the input document
        !           396:         * can trigger that by merely providing large input.
        !           397:         * For now, simply truncate.
        !           398:         */
        !           399:        if (len > 256)
        !           400:                len = 256;
1.15      kristaps  401:        for (i = 0; i < len; i++)
                    402:                putwchar(L' ');
                    403: }
                    404:
                    405: static void
                    406: locale_endline(struct termp *p)
                    407: {
                    408:
1.47      schwarze  409:        p->line++;
1.65      schwarze  410:        if ((int)p->tcol->offset > p->ti)
                    411:                p->tcol->offset -= p->ti;
                    412:        else
                    413:                p->tcol->offset = 0;
1.56      schwarze  414:        p->ti = 0;
1.15      kristaps  415:        putwchar(L'\n');
                    416: }
                    417:
                    418: static void
                    419: locale_letter(struct termp *p, int c)
                    420: {
1.25      schwarze  421:
1.15      kristaps  422:        putwchar(c);
                    423: }
                    424: #endif

CVSweb