]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/mdocml/term_ascii.c
Import sqlite3 3.12.1
[FreeBSD/FreeBSD.git] / contrib / mdocml / term_ascii.c
1 /*      $Id: term_ascii.c,v 1.52 2015/11/12 21:50:03 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 ascii_sepline(void *arg)
167 {
168         struct termp    *p;
169         size_t           i;
170
171         p = (struct termp *)arg;
172         p->line += 3;
173         putchar('\n');
174         for (i = 0; i < p->defrmargin; i++)
175                 putchar('-');
176         putchar('\n');
177         putchar('\n');
178 }
179
180 static size_t
181 ascii_width(const struct termp *p, int c)
182 {
183
184         return 1;
185 }
186
187 void
188 ascii_free(void *arg)
189 {
190
191         term_free((struct termp *)arg);
192 }
193
194 static void
195 ascii_letter(struct termp *p, int c)
196 {
197
198         putchar(c);
199 }
200
201 static void
202 ascii_begin(struct termp *p)
203 {
204
205         (*p->headf)(p, p->argf);
206 }
207
208 static void
209 ascii_end(struct termp *p)
210 {
211
212         (*p->footf)(p, p->argf);
213 }
214
215 static void
216 ascii_endline(struct termp *p)
217 {
218
219         p->line++;
220         putchar('\n');
221 }
222
223 static void
224 ascii_advance(struct termp *p, size_t len)
225 {
226         size_t          i;
227
228         for (i = 0; i < len; i++)
229                 putchar(' ');
230 }
231
232 static int
233 ascii_hspan(const struct termp *p, const struct roffsu *su)
234 {
235         double           r;
236
237         switch (su->unit) {
238         case SCALE_BU:
239                 r = su->scale;
240                 break;
241         case SCALE_CM:
242                 r = su->scale * 240.0 / 2.54;
243                 break;
244         case SCALE_FS:
245                 r = su->scale * 65536.0;
246                 break;
247         case SCALE_IN:
248                 r = su->scale * 240.0;
249                 break;
250         case SCALE_MM:
251                 r = su->scale * 0.24;
252                 break;
253         case SCALE_VS:
254         case SCALE_PC:
255                 r = su->scale * 40.0;
256                 break;
257         case SCALE_PT:
258                 r = su->scale * 10.0 / 3.0;
259                 break;
260         case SCALE_EN:
261         case SCALE_EM:
262                 r = su->scale * 24.0;
263                 break;
264         default:
265                 abort();
266         }
267         return r > 0.0 ? r + 0.01 : r - 0.01;
268 }
269
270 const char *
271 ascii_uc2str(int uc)
272 {
273         static const char nbrsp[2] = { ASCII_NBRSP, '\0' };
274         static const char *tab[] = {
275         "<NUL>","<SOH>","<STX>","<ETX>","<EOT>","<ENQ>","<ACK>","<BEL>",
276         "<BS>", "\t",   "<LF>", "<VT>", "<FF>", "<CR>", "<SO>", "<SI>",
277         "<DLE>","<DC1>","<DC2>","<DC3>","<DC4>","<NAK>","<SYN>","<ETB>",
278         "<CAN>","<EM>", "<SUB>","<ESC>","<FS>", "<GS>", "<RS>", "<US>",
279         " ",    "!",    "\"",   "#",    "$",    "%",    "&",    "'",
280         "(",    ")",    "*",    "+",    ",",    "-",    ".",    "/",
281         "0",    "1",    "2",    "3",    "4",    "5",    "6",    "7",
282         "8",    "9",    ":",    ";",    "<",    "=",    ">",    "?",
283         "@",    "A",    "B",    "C",    "D",    "E",    "F",    "G",
284         "H",    "I",    "J",    "K",    "L",    "M",    "N",    "O",
285         "P",    "Q",    "R",    "S",    "T",    "U",    "V",    "W",
286         "X",    "Y",    "Z",    "[",    "\\",   "]",    "^",    "_",
287         "`",    "a",    "b",    "c",    "d",    "e",    "f",    "g",
288         "h",    "i",    "j",    "k",    "l",    "m",    "n",    "o",
289         "p",    "q",    "r",    "s",    "t",    "u",    "v",    "w",
290         "x",    "y",    "z",    "{",    "|",    "}",    "~",    "<DEL>",
291         "<80>", "<81>", "<82>", "<83>", "<84>", "<85>", "<86>", "<87>",
292         "<88>", "<89>", "<8A>", "<8B>", "<8C>", "<8D>", "<8E>", "<8F>",
293         "<90>", "<91>", "<92>", "<93>", "<94>", "<95>", "<96>", "<97>",
294         "<99>", "<99>", "<9A>", "<9B>", "<9C>", "<9D>", "<9E>", "<9F>",
295         nbrsp,  "!",    "/\bc", "GBP",  "o\bx", "=\bY", "|",    "<sec>",
296         "\"",   "(C)",  "_\ba", "<<",   "~",    "",     "(R)",  "-",
297         "<deg>","+-",   "2",    "3",    "'",    ",\bu", "<par>",".",
298         ",",    "1",    "_\bo", ">>",   "1/4",  "1/2",  "3/4",  "?",
299         "`\bA", "'\bA", "^\bA", "~\bA", "\"\bA","o\bA", "AE",   ",\bC",
300         "`\bE", "'\bE", "^\bE", "\"\bE","`\bI", "'\bI", "^\bI", "\"\bI",
301         "-\bD", "~\bN", "`\bO", "'\bO", "^\bO", "~\bO", "\"\bO","x",
302         "/\bO", "`\bU", "'\bU", "^\bU", "\"\bU","'\bY", "Th",   "ss",
303         "`\ba", "'\ba", "^\ba", "~\ba", "\"\ba","o\ba", "ae",   ",\bc",
304         "`\be", "'\be", "^\be", "\"\be","`\bi", "'\bi", "^\bi", "\"\bi",
305         "d",    "~\bn", "`\bo", "'\bo", "^\bo", "~\bo", "\"\bo","-:-",
306         "/\bo", "`\bu", "'\bu", "^\bu", "\"\bu","'\by", "th",   "\"\by",
307         "A",    "a",    "A",    "a",    "A",    "a",    "'\bC", "'\bc",
308         "^\bC", "^\bc", "C",    "c",    "C",    "c",    "D",    "d",
309         "/\bD", "/\bd", "E",    "e",    "E",    "e",    "E",    "e",
310         "E",    "e",    "E",    "e",    "^\bG", "^\bg", "G",    "g",
311         "G",    "g",    ",\bG", ",\bg", "^\bH", "^\bh", "/\bH", "/\bh",
312         "~\bI", "~\bi", "I",    "i",    "I",    "i",    "I",    "i",
313         "I",    "i",    "IJ",   "ij",   "^\bJ", "^\bj", ",\bK", ",\bk",
314         "q",    "'\bL", "'\bl", ",\bL", ",\bl", "L",    "l",    "L",
315         "l",    "/\bL", "/\bl", "'\bN", "'\bn", ",\bN", ",\bn", "N",
316         "n",    "'n",   "Ng",   "ng",   "O",    "o",    "O",    "o",
317         "O",    "o",    "OE",   "oe",   "'\bR", "'\br", ",\bR", ",\br",
318         "R",    "r",    "'\bS", "'\bs", "^\bS", "^\bs", ",\bS", ",\bs",
319         "S",    "s",    ",\bT", ",\bt", "T",    "t",    "/\bT", "/\bt",
320         "~\bU", "~\bu", "U",    "u",    "U",    "u",    "U",    "u",
321         "U",    "u",    "U",    "u",    "^\bW", "^\bw", "^\bY", "^\by",
322         "\"\bY","'\bZ", "'\bz", "Z",    "z",    "Z",    "z",    "s",
323         "b",    "B",    "B",    "b",    "6",    "6",    "O",    "C",
324         "c",    "D",    "D",    "D",    "d",    "d",    "3",    "@",
325         "E",    "F",    ",\bf", "G",    "G",    "hv",   "I",    "/\bI",
326         "K",    "k",    "/\bl", "l",    "W",    "N",    "n",    "~\bO",
327         "O",    "o",    "OI",   "oi",   "P",    "p",    "YR",   "2",
328         "2",    "SH",   "sh",   "t",    "T",    "t",    "T",    "U",
329         "u",    "Y",    "V",    "Y",    "y",    "/\bZ", "/\bz", "ZH",
330         "ZH",   "zh",   "zh",   "/\b2", "5",    "5",    "ts",   "w",
331         "|",    "||",   "|=",   "!",    "DZ",   "Dz",   "dz",   "LJ",
332         "Lj",   "lj",   "NJ",   "Nj",   "nj",   "A",    "a",    "I",
333         "i",    "O",    "o",    "U",    "u",    "U",    "u",    "U",
334         "u",    "U",    "u",    "U",    "u",    "@",    "A",    "a",
335         "A",    "a",    "AE",   "ae",   "/\bG", "/\bg", "G",    "g",
336         "K",    "k",    "O",    "o",    "O",    "o",    "ZH",   "zh",
337         "j",    "DZ",   "Dz",   "dz",   "'\bG", "'\bg", "HV",   "W",
338         "`\bN", "`\bn", "A",    "a",    "'\bAE","'\bae","O",    "o"};
339
340         assert(uc >= 0);
341         if ((size_t)uc < sizeof(tab)/sizeof(tab[0]))
342                 return tab[uc];
343         return mchars_uc2str(uc);
344 }
345
346 #if HAVE_WCHAR
347 static size_t
348 locale_width(const struct termp *p, int c)
349 {
350         int             rc;
351
352         if (c == ASCII_NBRSP)
353                 c = ' ';
354         rc = wcwidth(c);
355         if (rc < 0)
356                 rc = 0;
357         return rc;
358 }
359
360 static void
361 locale_advance(struct termp *p, size_t len)
362 {
363         size_t          i;
364
365         for (i = 0; i < len; i++)
366                 putwchar(L' ');
367 }
368
369 static void
370 locale_endline(struct termp *p)
371 {
372
373         p->line++;
374         putwchar(L'\n');
375 }
376
377 static void
378 locale_letter(struct termp *p, int c)
379 {
380
381         putwchar(c);
382 }
383 #endif