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