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

Annotation of mandoc/term_ascii.c, Revision 1.43

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

CVSweb