]> git.cameronkatri.com Git - mandoc.git/blob - term_ascii.c
bugfix: make sure all variables are properly initialized
[mandoc.git] / term_ascii.c
1 /* $Id: term_ascii.c,v 1.24 2014/03/30 21:28:01 schwarze Exp $ */
2 /*
3 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014 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 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 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <sys/types.h>
23
24 #ifdef USE_WCHAR
25 # include <locale.h>
26 #endif
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #ifdef USE_WCHAR
32 # include <wchar.h>
33 #endif
34
35 #include "mandoc.h"
36 #include "mandoc_aux.h"
37 #include "out.h"
38 #include "term.h"
39 #include "main.h"
40
41 /*
42 * Sadly, this doesn't seem to be defined on systems even when they
43 * support it. For the time being, remove it and let those compiling
44 * the software decide for themselves what to use.
45 */
46 #if 0
47 #if ! defined(__STDC_ISO_10646__)
48 # undef USE_WCHAR
49 #endif
50 #endif
51
52 static struct termp *ascii_init(enum termenc, char *);
53 static double ascii_hspan(const struct termp *,
54 const struct roffsu *);
55 static size_t ascii_width(const struct termp *, int);
56 static void ascii_advance(struct termp *, size_t);
57 static void ascii_begin(struct termp *);
58 static void ascii_end(struct termp *);
59 static void ascii_endline(struct termp *);
60 static void ascii_letter(struct termp *, int);
61 static void ascii_setwidth(struct termp *, int, size_t);
62
63 #ifdef USE_WCHAR
64 static void locale_advance(struct termp *, size_t);
65 static void locale_endline(struct termp *);
66 static void locale_letter(struct termp *, int);
67 static size_t locale_width(const struct termp *, int);
68 #endif
69
70 static struct termp *
71 ascii_init(enum termenc enc, char *outopts)
72 {
73 const char *toks[4];
74 char *v;
75 struct termp *p;
76
77 p = mandoc_calloc(1, sizeof(struct termp));
78
79 p->tabwidth = 5;
80 p->defrmargin = p->lastrmargin = 78;
81
82 p->begin = ascii_begin;
83 p->end = ascii_end;
84 p->hspan = ascii_hspan;
85 p->type = TERMTYPE_CHAR;
86
87 p->enc = TERMENC_ASCII;
88 p->advance = ascii_advance;
89 p->endline = ascii_endline;
90 p->letter = ascii_letter;
91 p->setwidth = ascii_setwidth;
92 p->width = ascii_width;
93
94 #ifdef USE_WCHAR
95 if (TERMENC_ASCII != enc) {
96 v = TERMENC_LOCALE == enc ?
97 setlocale(LC_ALL, "") :
98 setlocale(LC_CTYPE, "en_US.UTF-8");
99 if (NULL != v && MB_CUR_MAX > 1) {
100 p->enc = enc;
101 p->advance = locale_advance;
102 p->endline = locale_endline;
103 p->letter = locale_letter;
104 p->width = locale_width;
105 }
106 }
107 #endif
108
109 toks[0] = "indent";
110 toks[1] = "width";
111 toks[2] = "mdoc";
112 toks[3] = NULL;
113
114 while (outopts && *outopts)
115 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
116 case (0):
117 p->defindent = (size_t)atoi(v);
118 break;
119 case (1):
120 p->defrmargin = (size_t)atoi(v);
121 break;
122 case (2):
123 /*
124 * Temporary, undocumented mode
125 * to imitate mdoc(7) output style.
126 */
127 p->mdocstyle = 1;
128 p->defindent = 5;
129 break;
130 default:
131 break;
132 }
133
134 /* Enforce a lower boundary. */
135 if (p->defrmargin < 58)
136 p->defrmargin = 58;
137
138 return(p);
139 }
140
141 void *
142 ascii_alloc(char *outopts)
143 {
144
145 return(ascii_init(TERMENC_ASCII, outopts));
146 }
147
148 void *
149 utf8_alloc(char *outopts)
150 {
151
152 return(ascii_init(TERMENC_UTF8, outopts));
153 }
154
155
156 void *
157 locale_alloc(char *outopts)
158 {
159
160 return(ascii_init(TERMENC_LOCALE, outopts));
161 }
162
163 static void
164 ascii_setwidth(struct termp *p, int iop, size_t width)
165 {
166
167 p->rmargin = p->defrmargin;
168 if (0 < iop)
169 p->defrmargin += width;
170 else if (0 > iop)
171 p->defrmargin -= width;
172 else
173 p->defrmargin = width ? width : p->lastrmargin;
174 p->lastrmargin = p->rmargin;
175 p->rmargin = p->maxrmargin = p->defrmargin;
176 }
177
178 /* ARGSUSED */
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 /* ARGSUSED */
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 /* ARGSUSED */
216 static void
217 ascii_endline(struct termp *p)
218 {
219
220 putchar('\n');
221 }
222
223 /* ARGSUSED */
224 static void
225 ascii_advance(struct termp *p, size_t len)
226 {
227 size_t i;
228
229 for (i = 0; i < len; i++)
230 putchar(' ');
231 }
232
233 /* ARGSUSED */
234 static double
235 ascii_hspan(const struct termp *p, const struct roffsu *su)
236 {
237 double r;
238
239 /*
240 * Approximate based on character width. These are generated
241 * entirely by eyeballing the screen, but appear to be correct.
242 */
243
244 switch (su->unit) {
245 case (SCALE_CM):
246 r = 4 * su->scale;
247 break;
248 case (SCALE_IN):
249 r = 10 * su->scale;
250 break;
251 case (SCALE_PC):
252 r = (10 * su->scale) / 6;
253 break;
254 case (SCALE_PT):
255 r = (10 * su->scale) / 72;
256 break;
257 case (SCALE_MM):
258 r = su->scale / 1000;
259 break;
260 case (SCALE_VS):
261 r = su->scale * 2 - 1;
262 break;
263 default:
264 r = su->scale;
265 break;
266 }
267
268 return(r);
269 }
270
271 #ifdef USE_WCHAR
272 /* ARGSUSED */
273 static size_t
274 locale_width(const struct termp *p, int c)
275 {
276 int rc;
277
278 return((rc = wcwidth(c)) < 0 ? 0 : rc);
279 }
280
281 /* ARGSUSED */
282 static void
283 locale_advance(struct termp *p, size_t len)
284 {
285 size_t i;
286
287 for (i = 0; i < len; i++)
288 putwchar(L' ');
289 }
290
291 /* ARGSUSED */
292 static void
293 locale_endline(struct termp *p)
294 {
295
296 putwchar(L'\n');
297 }
298
299 /* ARGSUSED */
300 static void
301 locale_letter(struct termp *p, int c)
302 {
303
304 putwchar(c);
305 }
306 #endif