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

Annotation of mandoc/term_ascii.c, Revision 1.49

1.49    ! schwarze    1: /*     $Id: term_ascii.c,v 1.48 2015/09/26 00:54:04 schwarze Exp $ */
1.1       kristaps    2: /*
1.18      schwarze    3:  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.44      schwarze    4:  * Copyright (c) 2014, 2015 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.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"
1.44      schwarze   38: #include "manconf.h"
1.1       kristaps   39: #include "main.h"
                     40:
1.44      schwarze   41: static struct termp     *ascii_init(enum termenc, const struct mchars *,
                     42:                                const struct manoutput *);
1.45      schwarze   43: static int               ascii_hspan(const struct termp *,
1.7       kristaps   44:                                const struct roffsu *);
1.13      kristaps   45: static size_t            ascii_width(const struct termp *, int);
1.7       kristaps   46: static void              ascii_advance(struct termp *, size_t);
                     47: static void              ascii_begin(struct termp *);
                     48: static void              ascii_end(struct termp *);
1.1       kristaps   49: static void              ascii_endline(struct termp *);
1.13      kristaps   50: static void              ascii_letter(struct termp *, int);
1.45      schwarze   51: static void              ascii_setwidth(struct termp *, int, int);
1.1       kristaps   52:
1.31      schwarze   53: #if HAVE_WCHAR
1.15      kristaps   54: static void              locale_advance(struct termp *, size_t);
                     55: static void              locale_endline(struct termp *);
                     56: static void              locale_letter(struct termp *, int);
                     57: static size_t            locale_width(const struct termp *, int);
                     58: #endif
                     59:
1.25      schwarze   60:
1.14      kristaps   61: static struct termp *
1.44      schwarze   62: ascii_init(enum termenc enc, const struct mchars *mchars,
                     63:        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.14      kristaps   70:        p = mandoc_calloc(1, sizeof(struct termp));
1.1       kristaps   71:
1.38      schwarze   72:        p->symtab = mchars;
1.47      schwarze   73:        p->line = 1;
1.5       kristaps   74:        p->tabwidth = 5;
1.23      schwarze   75:        p->defrmargin = p->lastrmargin = 78;
1.41      schwarze   76:        p->fontq = mandoc_reallocarray(NULL,
                     77:             (p->fontsz = 8), sizeof(enum termfont));
                     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.17      kristaps   93:        if (TERMENC_ASCII != enc) {
                     94:                v = TERMENC_LOCALE == enc ?
1.25      schwarze   95:                    setlocale(LC_ALL, "") :
                     96:                    setlocale(LC_CTYPE, "en_US.UTF-8");
1.17      kristaps   97:                if (NULL != v && MB_CUR_MAX > 1) {
1.15      kristaps   98:                        p->enc = enc;
                     99:                        p->advance = locale_advance;
                    100:                        p->endline = locale_endline;
                    101:                        p->letter = locale_letter;
                    102:                        p->width = locale_width;
                    103:                }
1.17      kristaps  104:        }
1.15      kristaps  105: #endif
                    106:
1.44      schwarze  107:        if (outopts->mdoc) {
                    108:                p->mdocstyle = 1;
                    109:                p->defindent = 5;
                    110:        }
                    111:        if (outopts->indent)
                    112:                p->defindent = outopts->indent;
                    113:        if (outopts->width)
                    114:                p->defrmargin = outopts->width;
                    115:        if (outopts->synopsisonly)
                    116:                p->synopsisonly = 1;
1.1       kristaps  117:
1.49    ! schwarze  118:        return p;
1.5       kristaps  119: }
                    120:
1.14      kristaps  121: void *
1.44      schwarze  122: ascii_alloc(const struct mchars *mchars, const struct manoutput *outopts)
1.14      kristaps  123: {
                    124:
1.49    ! schwarze  125:        return ascii_init(TERMENC_ASCII, mchars, outopts);
1.14      kristaps  126: }
1.17      kristaps  127:
                    128: void *
1.44      schwarze  129: utf8_alloc(const struct mchars *mchars, const struct manoutput *outopts)
1.17      kristaps  130: {
                    131:
1.49    ! schwarze  132:        return ascii_init(TERMENC_UTF8, mchars, outopts);
1.17      kristaps  133: }
                    134:
1.14      kristaps  135: void *
1.44      schwarze  136: locale_alloc(const struct mchars *mchars, const struct manoutput *outopts)
1.14      kristaps  137: {
                    138:
1.49    ! schwarze  139:        return ascii_init(TERMENC_LOCALE, mchars, outopts);
1.23      schwarze  140: }
                    141:
                    142: static void
1.45      schwarze  143: ascii_setwidth(struct termp *p, int iop, int width)
1.23      schwarze  144: {
                    145:
1.45      schwarze  146:        width /= 24;
1.24      schwarze  147:        p->rmargin = p->defrmargin;
1.40      schwarze  148:        if (iop > 0)
1.24      schwarze  149:                p->defrmargin += width;
1.40      schwarze  150:        else if (iop == 0)
1.45      schwarze  151:                p->defrmargin = width ? (size_t)width : p->lastrmargin;
                    152:        else if (p->defrmargin > (size_t)width)
1.24      schwarze  153:                p->defrmargin -= width;
                    154:        else
1.40      schwarze  155:                p->defrmargin = 0;
1.24      schwarze  156:        p->lastrmargin = p->rmargin;
                    157:        p->rmargin = p->maxrmargin = p->defrmargin;
1.42      schwarze  158: }
                    159:
                    160: void
                    161: ascii_sepline(void *arg)
                    162: {
                    163:        struct termp    *p;
                    164:        size_t           i;
                    165:
                    166:        p = (struct termp *)arg;
1.47      schwarze  167:        p->line += 3;
1.42      schwarze  168:        putchar('\n');
                    169:        for (i = 0; i < p->defrmargin; i++)
                    170:                putchar('-');
                    171:        putchar('\n');
                    172:        putchar('\n');
1.34      schwarze  173: }
                    174:
1.5       kristaps  175: static size_t
1.13      kristaps  176: ascii_width(const struct termp *p, int c)
1.5       kristaps  177: {
                    178:
1.49    ! schwarze  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: static void
1.13      kristaps  190: ascii_letter(struct termp *p, int c)
1.1       kristaps  191: {
1.25      schwarze  192:
1.1       kristaps  193:        putchar(c);
                    194: }
                    195:
                    196: static void
                    197: ascii_begin(struct termp *p)
                    198: {
                    199:
                    200:        (*p->headf)(p, p->argf);
                    201: }
                    202:
                    203: static void
                    204: ascii_end(struct termp *p)
                    205: {
                    206:
                    207:        (*p->footf)(p, p->argf);
                    208: }
                    209:
                    210: static void
                    211: ascii_endline(struct termp *p)
                    212: {
                    213:
1.47      schwarze  214:        p->line++;
1.1       kristaps  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.45      schwarze  227: static int
1.7       kristaps  228: ascii_hspan(const struct termp *p, const struct roffsu *su)
                    229: {
                    230:        double           r;
                    231:
                    232:        switch (su->unit) {
1.29      kristaps  233:        case SCALE_BU:
1.45      schwarze  234:                r = su->scale;
1.29      kristaps  235:                break;
1.25      schwarze  236:        case SCALE_CM:
1.45      schwarze  237:                r = su->scale * 240.0 / 2.54;
1.29      kristaps  238:                break;
                    239:        case SCALE_FS:
1.45      schwarze  240:                r = su->scale * 65536.0;
1.7       kristaps  241:                break;
1.25      schwarze  242:        case SCALE_IN:
1.45      schwarze  243:                r = su->scale * 240.0;
1.7       kristaps  244:                break;
1.29      kristaps  245:        case SCALE_MM:
1.45      schwarze  246:                r = su->scale * 0.24;
1.29      kristaps  247:                break;
1.45      schwarze  248:        case SCALE_VS:
                    249:                /* FALLTHROUGH */
1.25      schwarze  250:        case SCALE_PC:
1.45      schwarze  251:                r = su->scale * 40.0;
1.7       kristaps  252:                break;
1.25      schwarze  253:        case SCALE_PT:
1.45      schwarze  254:                r = su->scale * 10.0 / 3.0;
1.7       kristaps  255:                break;
1.29      kristaps  256:        case SCALE_EN:
1.30      schwarze  257:                /* FALLTHROUGH */
1.29      kristaps  258:        case SCALE_EM:
1.45      schwarze  259:                r = su->scale * 24.0;
1.29      kristaps  260:                break;
1.32      schwarze  261:        default:
1.29      kristaps  262:                abort();
1.7       kristaps  263:        }
1.49    ! schwarze  264:        return r > 0.0 ? r + 0.01 : r - 0.01;
1.36      schwarze  265: }
                    266:
                    267: const char *
                    268: ascii_uc2str(int uc)
                    269: {
                    270:        static const char nbrsp[2] = { ASCII_NBRSP, '\0' };
                    271:        static const char *tab[] = {
                    272:        "<NUL>","<SOH>","<STX>","<ETX>","<EOT>","<ENQ>","<ACK>","<BEL>",
                    273:        "<BS>", "\t",   "<LF>", "<VT>", "<FF>", "<CR>", "<SO>", "<SI>",
                    274:        "<DLE>","<DC1>","<DC2>","<DC3>","<DC4>","<NAK>","<SYN>","<ETB>",
                    275:        "<CAN>","<EM>", "<SUB>","<ESC>","<FS>", "<GS>", "<RS>", "<US>",
                    276:        " ",    "!",    "\"",   "#",    "$",    "%",    "&",    "'",
                    277:        "(",    ")",    "*",    "+",    ",",    "-",    ".",    "/",
                    278:        "0",    "1",    "2",    "3",    "4",    "5",    "6",    "7",
                    279:        "8",    "9",    ":",    ";",    "<",    "=",    ">",    "?",
                    280:        "@",    "A",    "B",    "C",    "D",    "E",    "F",    "G",
                    281:        "H",    "I",    "J",    "K",    "L",    "M",    "N",    "O",
                    282:        "P",    "Q",    "R",    "S",    "T",    "U",    "V",    "W",
                    283:        "X",    "Y",    "Z",    "[",    "\\",   "]",    "^",    "_",
                    284:        "`",    "a",    "b",    "c",    "d",    "e",    "f",    "g",
                    285:        "h",    "i",    "j",    "k",    "l",    "m",    "n",    "o",
                    286:        "p",    "q",    "r",    "s",    "t",    "u",    "v",    "w",
                    287:        "x",    "y",    "z",    "{",    "|",    "}",    "~",    "<DEL>",
                    288:        "<80>", "<81>", "<82>", "<83>", "<84>", "<85>", "<86>", "<87>",
                    289:        "<88>", "<89>", "<8A>", "<8B>", "<8C>", "<8D>", "<8E>", "<8F>",
                    290:        "<90>", "<91>", "<92>", "<93>", "<94>", "<95>", "<96>", "<97>",
                    291:        "<99>", "<99>", "<9A>", "<9B>", "<9C>", "<9D>", "<9E>", "<9F>",
1.37      schwarze  292:        nbrsp,  "!",    "/\bc", "GBP",  "o\bx", "=\bY", "|",    "<sec>",
                    293:        "\"",   "(C)",  "_\ba", "<<",   "~",    "",     "(R)",  "-",
                    294:        "<deg>","+-",   "2",    "3",    "'",    ",\bu", "<par>",".",
                    295:        ",",    "1",    "_\bo", ">>",   "1/4",  "1/2",  "3/4",  "?",
                    296:        "`\bA", "'\bA", "^\bA", "~\bA", "\"\bA","o\bA", "AE",   ",\bC",
                    297:        "`\bE", "'\bE", "^\bE", "\"\bE","`\bI", "'\bI", "^\bI", "\"\bI",
                    298:        "-\bD", "~\bN", "`\bO", "'\bO", "^\bO", "~\bO", "\"\bO","x",
                    299:        "/\bO", "`\bU", "'\bU", "^\bU", "\"\bU","'\bY", "Th",   "ss",
                    300:        "`\ba", "'\ba", "^\ba", "~\ba", "\"\ba","o\ba", "ae",   ",\bc",
                    301:        "`\be", "'\be", "^\be", "\"\be","`\bi", "'\bi", "^\bi", "\"\bi",
                    302:        "d",    "~\bn", "`\bo", "'\bo", "^\bo", "~\bo", "\"\bo","-:-",
                    303:        "/\bo", "`\bu", "'\bu", "^\bu", "\"\bu","'\by", "th",   "\"\by",
                    304:        "A",    "a",    "A",    "a",    "A",    "a",    "'\bC", "'\bc",
                    305:        "^\bC", "^\bc", "C",    "c",    "C",    "c",    "D",    "d",
                    306:        "/\bD", "/\bd", "E",    "e",    "E",    "e",    "E",    "e",
                    307:        "E",    "e",    "E",    "e",    "^\bG", "^\bg", "G",    "g",
                    308:        "G",    "g",    ",\bG", ",\bg", "^\bH", "^\bh", "/\bH", "/\bh",
                    309:        "~\bI", "~\bi", "I",    "i",    "I",    "i",    "I",    "i",
                    310:        "I",    "i",    "IJ",   "ij",   "^\bJ", "^\bj", ",\bK", ",\bk",
                    311:        "q",    "'\bL", "'\bl", ",\bL", ",\bl", "L",    "l",    "L",
                    312:        "l",    "/\bL", "/\bl", "'\bN", "'\bn", ",\bN", ",\bn", "N",
1.36      schwarze  313:        "n",    "'n",   "Ng",   "ng",   "O",    "o",    "O",    "o",
1.37      schwarze  314:        "O",    "o",    "OE",   "oe",   "'\bR", "'\br", ",\bR", ",\br",
                    315:        "R",    "r",    "'\bS", "'\bs", "^\bS", "^\bs", ",\bS", ",\bs",
                    316:        "S",    "s",    ",\bT", ",\bt", "T",    "t",    "/\bT", "/\bt",
                    317:        "~\bU", "~\bu", "U",    "u",    "U",    "u",    "U",    "u",
                    318:        "U",    "u",    "U",    "u",    "^\bW", "^\bw", "^\bY", "^\by",
                    319:        "\"\bY","'\bZ", "'\bz", "Z",    "z",    "Z",    "z",    "s",
1.36      schwarze  320:        "b",    "B",    "B",    "b",    "6",    "6",    "O",    "C",
                    321:        "c",    "D",    "D",    "D",    "d",    "d",    "3",    "@",
1.37      schwarze  322:        "E",    "F",    ",\bf", "G",    "G",    "hv",   "I",    "/\bI",
                    323:        "K",    "k",    "/\bl", "l",    "W",    "N",    "n",    "~\bO",
1.36      schwarze  324:        "O",    "o",    "OI",   "oi",   "P",    "p",    "YR",   "2",
                    325:        "2",    "SH",   "sh",   "t",    "T",    "t",    "T",    "U",
1.37      schwarze  326:        "u",    "Y",    "V",    "Y",    "y",    "/\bZ", "/\bz", "ZH",
                    327:        "ZH",   "zh",   "zh",   "/\b2", "5",    "5",    "ts",   "w",
1.36      schwarze  328:        "|",    "||",   "|=",   "!",    "DZ",   "Dz",   "dz",   "LJ",
                    329:        "Lj",   "lj",   "NJ",   "Nj",   "nj",   "A",    "a",    "I",
                    330:        "i",    "O",    "o",    "U",    "u",    "U",    "u",    "U",
                    331:        "u",    "U",    "u",    "U",    "u",    "@",    "A",    "a",
1.37      schwarze  332:        "A",    "a",    "AE",   "ae",   "/\bG", "/\bg", "G",    "g",
1.36      schwarze  333:        "K",    "k",    "O",    "o",    "O",    "o",    "ZH",   "zh",
1.37      schwarze  334:        "j",    "DZ",   "Dz",   "dz",   "'\bG", "'\bg", "HV",   "W",
                    335:        "`\bN", "`\bn", "A",    "a",    "'\bAE","'\bae","O",    "o"};
1.36      schwarze  336:
1.39      schwarze  337:        assert(uc >= 0);
1.36      schwarze  338:        if ((size_t)uc < sizeof(tab)/sizeof(tab[0]))
1.49    ! schwarze  339:                return tab[uc];
        !           340:        return mchars_uc2str(uc);
1.7       kristaps  341: }
                    342:
1.31      schwarze  343: #if HAVE_WCHAR
1.15      kristaps  344: static size_t
                    345: locale_width(const struct termp *p, int c)
                    346: {
                    347:        int             rc;
                    348:
1.26      schwarze  349:        if (c == ASCII_NBRSP)
                    350:                c = ' ';
                    351:        rc = wcwidth(c);
                    352:        if (rc < 0)
                    353:                rc = 0;
1.49    ! schwarze  354:        return rc;
1.15      kristaps  355: }
                    356:
                    357: static void
                    358: locale_advance(struct termp *p, size_t len)
                    359: {
1.25      schwarze  360:        size_t          i;
1.15      kristaps  361:
                    362:        for (i = 0; i < len; i++)
                    363:                putwchar(L' ');
                    364: }
                    365:
                    366: static void
                    367: locale_endline(struct termp *p)
                    368: {
                    369:
1.47      schwarze  370:        p->line++;
1.15      kristaps  371:        putwchar(L'\n');
                    372: }
                    373:
                    374: static void
                    375: locale_letter(struct termp *p, int c)
                    376: {
1.25      schwarze  377:
1.15      kristaps  378:        putwchar(c);
                    379: }
                    380: #endif

CVSweb