Print this page
Update to 1.12.3.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mandoc/term_ascii.c
+++ new/usr/src/cmd/mandoc/term_ascii.c
1 -/* $Id: term_ascii.c,v 1.20 2011/12/04 23:10:52 schwarze Exp $ */
1 +/* $Id: term_ascii.c,v 1.21 2013/06/01 14:27:20 schwarze Exp $ */
2 2 /*
3 3 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 4 *
5 5 * Permission to use, copy, modify, and distribute this software for any
6 6 * purpose with or without fee is hereby granted, provided that the above
7 7 * copyright notice and this permission notice appear in all copies.
8 8 *
9 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 16 */
17 17 #ifdef HAVE_CONFIG_H
18 18 #include "config.h"
19 19 #endif
20 20
21 21 #include <sys/types.h>
22 22
23 23 #include <assert.h>
24 24 #ifdef USE_WCHAR
25 25 # include <locale.h>
26 26 #endif
27 27 #include <stdint.h>
28 28 #include <stdio.h>
29 29 #include <stdlib.h>
30 30 #include <unistd.h>
31 31 #ifdef USE_WCHAR
32 32 # include <wchar.h>
33 33 #endif
34 34
35 35 #include "mandoc.h"
36 36 #include "out.h"
37 37 #include "term.h"
38 38 #include "main.h"
39 39
40 40 /*
41 41 * Sadly, this doesn't seem to be defined on systems even when they
42 42 * support it. For the time being, remove it and let those compiling
43 43 * the software decide for themselves what to use.
44 44 */
45 45 #if 0
46 46 #if ! defined(__STDC_ISO_10646__)
47 47 # undef USE_WCHAR
48 48 #endif
49 49 #endif
50 50
51 51 static struct termp *ascii_init(enum termenc, char *);
52 52 static double ascii_hspan(const struct termp *,
53 53 const struct roffsu *);
54 54 static size_t ascii_width(const struct termp *, int);
55 55 static void ascii_advance(struct termp *, size_t);
56 56 static void ascii_begin(struct termp *);
57 57 static void ascii_end(struct termp *);
58 58 static void ascii_endline(struct termp *);
59 59 static void ascii_letter(struct termp *, int);
60 60
61 61 #ifdef USE_WCHAR
62 62 static void locale_advance(struct termp *, size_t);
63 63 static void locale_endline(struct termp *);
64 64 static void locale_letter(struct termp *, int);
65 65 static size_t locale_width(const struct termp *, int);
↓ open down ↓ |
54 lines elided |
↑ open up ↑ |
66 66 #endif
67 67
68 68 static struct termp *
69 69 ascii_init(enum termenc enc, char *outopts)
70 70 {
71 71 const char *toks[4];
72 72 char *v;
73 73 struct termp *p;
74 74
75 75 p = mandoc_calloc(1, sizeof(struct termp));
76 - p->enc = enc;
77 76
78 77 p->tabwidth = 5;
79 78 p->defrmargin = 78;
80 79
81 80 p->begin = ascii_begin;
82 81 p->end = ascii_end;
83 82 p->hspan = ascii_hspan;
84 83 p->type = TERMTYPE_CHAR;
85 84
86 85 p->enc = TERMENC_ASCII;
87 86 p->advance = ascii_advance;
88 87 p->endline = ascii_endline;
89 88 p->letter = ascii_letter;
90 89 p->width = ascii_width;
91 90
92 91 #ifdef USE_WCHAR
93 92 if (TERMENC_ASCII != enc) {
94 93 v = TERMENC_LOCALE == enc ?
95 94 setlocale(LC_ALL, "") :
96 - setlocale(LC_CTYPE, "UTF-8");
95 + setlocale(LC_CTYPE, "en_US.UTF-8");
97 96 if (NULL != v && MB_CUR_MAX > 1) {
98 97 p->enc = enc;
99 98 p->advance = locale_advance;
100 99 p->endline = locale_endline;
101 100 p->letter = locale_letter;
102 101 p->width = locale_width;
103 102 }
104 103 }
105 104 #endif
106 105
107 106 toks[0] = "indent";
108 107 toks[1] = "width";
109 108 toks[2] = "mdoc";
110 109 toks[3] = NULL;
111 110
112 111 while (outopts && *outopts)
113 112 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
114 113 case (0):
115 114 p->defindent = (size_t)atoi(v);
116 115 break;
117 116 case (1):
118 117 p->defrmargin = (size_t)atoi(v);
119 118 break;
120 119 case (2):
121 120 /*
122 121 * Temporary, undocumented mode
123 122 * to imitate mdoc(7) output style.
124 123 */
125 124 p->mdocstyle = 1;
126 125 p->defindent = 5;
127 126 break;
128 127 default:
129 128 break;
130 129 }
131 130
132 131 /* Enforce a lower boundary. */
133 132 if (p->defrmargin < 58)
134 133 p->defrmargin = 58;
135 134
136 135 return(p);
137 136 }
138 137
139 138 void *
140 139 ascii_alloc(char *outopts)
141 140 {
142 141
143 142 return(ascii_init(TERMENC_ASCII, outopts));
144 143 }
145 144
146 145 void *
147 146 utf8_alloc(char *outopts)
148 147 {
149 148
150 149 return(ascii_init(TERMENC_UTF8, outopts));
151 150 }
152 151
153 152
154 153 void *
155 154 locale_alloc(char *outopts)
156 155 {
157 156
158 157 return(ascii_init(TERMENC_LOCALE, outopts));
159 158 }
160 159
161 160 /* ARGSUSED */
162 161 static size_t
163 162 ascii_width(const struct termp *p, int c)
164 163 {
165 164
166 165 return(1);
167 166 }
168 167
169 168 void
170 169 ascii_free(void *arg)
171 170 {
172 171
173 172 term_free((struct termp *)arg);
174 173 }
175 174
176 175 /* ARGSUSED */
177 176 static void
178 177 ascii_letter(struct termp *p, int c)
179 178 {
180 179
181 180 putchar(c);
182 181 }
183 182
184 183 static void
185 184 ascii_begin(struct termp *p)
186 185 {
187 186
188 187 (*p->headf)(p, p->argf);
189 188 }
190 189
191 190 static void
192 191 ascii_end(struct termp *p)
193 192 {
194 193
195 194 (*p->footf)(p, p->argf);
196 195 }
197 196
198 197 /* ARGSUSED */
199 198 static void
200 199 ascii_endline(struct termp *p)
201 200 {
202 201
203 202 putchar('\n');
204 203 }
205 204
206 205 /* ARGSUSED */
207 206 static void
208 207 ascii_advance(struct termp *p, size_t len)
209 208 {
210 209 size_t i;
211 210
212 211 for (i = 0; i < len; i++)
213 212 putchar(' ');
214 213 }
215 214
216 215 /* ARGSUSED */
217 216 static double
218 217 ascii_hspan(const struct termp *p, const struct roffsu *su)
219 218 {
220 219 double r;
221 220
222 221 /*
223 222 * Approximate based on character width. These are generated
224 223 * entirely by eyeballing the screen, but appear to be correct.
225 224 */
226 225
227 226 switch (su->unit) {
228 227 case (SCALE_CM):
229 228 r = 4 * su->scale;
230 229 break;
231 230 case (SCALE_IN):
232 231 r = 10 * su->scale;
233 232 break;
234 233 case (SCALE_PC):
235 234 r = (10 * su->scale) / 6;
236 235 break;
237 236 case (SCALE_PT):
238 237 r = (10 * su->scale) / 72;
239 238 break;
240 239 case (SCALE_MM):
241 240 r = su->scale / 1000;
242 241 break;
243 242 case (SCALE_VS):
244 243 r = su->scale * 2 - 1;
245 244 break;
246 245 default:
247 246 r = su->scale;
248 247 break;
249 248 }
250 249
251 250 return(r);
252 251 }
253 252
254 253 #ifdef USE_WCHAR
255 254 /* ARGSUSED */
256 255 static size_t
257 256 locale_width(const struct termp *p, int c)
258 257 {
259 258 int rc;
260 259
261 260 return((rc = wcwidth(c)) < 0 ? 0 : rc);
262 261 }
263 262
264 263 /* ARGSUSED */
265 264 static void
266 265 locale_advance(struct termp *p, size_t len)
267 266 {
268 267 size_t i;
269 268
270 269 for (i = 0; i < len; i++)
271 270 putwchar(L' ');
272 271 }
273 272
274 273 /* ARGSUSED */
275 274 static void
276 275 locale_endline(struct termp *p)
277 276 {
278 277
279 278 putwchar(L'\n');
280 279 }
281 280
282 281 /* ARGSUSED */
283 282 static void
284 283 locale_letter(struct termp *p, int c)
285 284 {
286 285
287 286 putwchar(c);
288 287 }
289 288 #endif
↓ open down ↓ |
183 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX