Annotation of src/usr.bin/mandoc/term_ascii.c, Revision 1.12
1.12 ! schwarze 1: /* $Id: term_ascii.c,v 1.11 2014/03/21 22:17:01 schwarze Exp $ */
1.1 schwarze 2: /*
1.6 schwarze 3: * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.12 ! schwarze 4: * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 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: #include <sys/types.h>
19:
1.6 schwarze 20: #include <locale.h>
1.1 schwarze 21: #include <stdint.h>
22: #include <stdio.h>
23: #include <stdlib.h>
24: #include <unistd.h>
1.6 schwarze 25: #include <wchar.h>
1.1 schwarze 26:
1.4 schwarze 27: #include "mandoc.h"
1.11 schwarze 28: #include "mandoc_aux.h"
1.1 schwarze 29: #include "out.h"
30: #include "term.h"
31: #include "main.h"
32:
1.6 schwarze 33: static struct termp *ascii_init(enum termenc, char *);
1.3 schwarze 34: static double ascii_hspan(const struct termp *,
35: const struct roffsu *);
1.6 schwarze 36: static size_t ascii_width(const struct termp *, int);
1.3 schwarze 37: static void ascii_advance(struct termp *, size_t);
38: static void ascii_begin(struct termp *);
39: static void ascii_end(struct termp *);
1.1 schwarze 40: static void ascii_endline(struct termp *);
1.6 schwarze 41: static void ascii_letter(struct termp *, int);
1.12 ! schwarze 42: static void ascii_setwidth(struct termp *, size_t);
1.1 schwarze 43:
1.6 schwarze 44: static void locale_advance(struct termp *, size_t);
45: static void locale_endline(struct termp *);
46: static void locale_letter(struct termp *, int);
47: static size_t locale_width(const struct termp *, int);
1.1 schwarze 48:
1.6 schwarze 49: static struct termp *
50: ascii_init(enum termenc enc, char *outopts)
1.1 schwarze 51: {
1.8 schwarze 52: const char *toks[4];
1.1 schwarze 53: char *v;
1.6 schwarze 54: struct termp *p;
1.1 schwarze 55:
1.6 schwarze 56: p = mandoc_calloc(1, sizeof(struct termp));
1.1 schwarze 57:
1.2 schwarze 58: p->tabwidth = 5;
1.12 ! schwarze 59: p->defrmargin = p->lastrmargin = 78;
1.2 schwarze 60:
1.1 schwarze 61: p->begin = ascii_begin;
62: p->end = ascii_end;
1.6 schwarze 63: p->hspan = ascii_hspan;
64: p->type = TERMTYPE_CHAR;
65:
66: p->enc = TERMENC_ASCII;
67: p->advance = ascii_advance;
1.1 schwarze 68: p->endline = ascii_endline;
1.3 schwarze 69: p->letter = ascii_letter;
1.12 ! schwarze 70: p->setwidth = ascii_setwidth;
1.2 schwarze 71: p->width = ascii_width;
1.1 schwarze 72:
1.6 schwarze 73: if (TERMENC_ASCII != enc) {
74: v = TERMENC_LOCALE == enc ?
75: setlocale(LC_ALL, "") :
1.10 schwarze 76: setlocale(LC_CTYPE, "en_US.UTF-8");
1.6 schwarze 77: if (NULL != v && MB_CUR_MAX > 1) {
78: p->enc = enc;
79: p->advance = locale_advance;
80: p->endline = locale_endline;
81: p->letter = locale_letter;
82: p->width = locale_width;
83: }
84: }
85:
1.7 schwarze 86: toks[0] = "indent";
87: toks[1] = "width";
1.8 schwarze 88: toks[2] = "mdoc";
89: toks[3] = NULL;
1.1 schwarze 90:
91: while (outopts && *outopts)
92: switch (getsubopt(&outopts, UNCONST(toks), &v)) {
93: case (0):
1.7 schwarze 94: p->defindent = (size_t)atoi(v);
95: break;
96: case (1):
1.1 schwarze 97: p->defrmargin = (size_t)atoi(v);
1.8 schwarze 98: break;
99: case (2):
1.9 schwarze 100: /*
101: * Temporary, undocumented mode
102: * to imitate mdoc(7) output style.
103: */
1.8 schwarze 104: p->mdocstyle = 1;
105: p->defindent = 5;
1.1 schwarze 106: break;
107: default:
108: break;
109: }
110:
111: /* Enforce a lower boundary. */
112: if (p->defrmargin < 58)
113: p->defrmargin = 58;
114:
115: return(p);
1.2 schwarze 116: }
117:
1.6 schwarze 118: void *
119: ascii_alloc(char *outopts)
120: {
121:
122: return(ascii_init(TERMENC_ASCII, outopts));
123: }
124:
125: void *
126: utf8_alloc(char *outopts)
127: {
128:
129: return(ascii_init(TERMENC_UTF8, outopts));
130: }
131:
132:
133: void *
134: locale_alloc(char *outopts)
135: {
136:
137: return(ascii_init(TERMENC_LOCALE, outopts));
1.12 ! schwarze 138: }
! 139:
! 140: static void
! 141: ascii_setwidth(struct termp *p, size_t width)
! 142: {
! 143: size_t lastwidth;
! 144:
! 145: lastwidth = p->defrmargin;
! 146: p->rmargin = p->maxrmargin = p->defrmargin =
! 147: width ? width : p->lastrmargin;
! 148: p->lastrmargin = lastwidth;
1.6 schwarze 149: }
1.2 schwarze 150:
1.3 schwarze 151: /* ARGSUSED */
1.2 schwarze 152: static size_t
1.6 schwarze 153: ascii_width(const struct termp *p, int c)
1.2 schwarze 154: {
155:
156: return(1);
1.1 schwarze 157: }
158:
159: void
160: ascii_free(void *arg)
161: {
162:
163: term_free((struct termp *)arg);
164: }
165:
166: /* ARGSUSED */
167: static void
1.6 schwarze 168: ascii_letter(struct termp *p, int c)
1.1 schwarze 169: {
170:
171: putchar(c);
172: }
173:
174: static void
175: ascii_begin(struct termp *p)
176: {
177:
178: (*p->headf)(p, p->argf);
179: }
180:
181: static void
182: ascii_end(struct termp *p)
183: {
184:
185: (*p->footf)(p, p->argf);
186: }
187:
188: /* ARGSUSED */
189: static void
190: ascii_endline(struct termp *p)
191: {
192:
193: putchar('\n');
194: }
195:
196: /* ARGSUSED */
197: static void
198: ascii_advance(struct termp *p, size_t len)
199: {
200: size_t i;
201:
202: for (i = 0; i < len; i++)
203: putchar(' ');
204: }
1.3 schwarze 205:
206: /* ARGSUSED */
207: static double
208: ascii_hspan(const struct termp *p, const struct roffsu *su)
209: {
210: double r;
211:
212: /*
213: * Approximate based on character width. These are generated
214: * entirely by eyeballing the screen, but appear to be correct.
215: */
216:
217: switch (su->unit) {
218: case (SCALE_CM):
219: r = 4 * su->scale;
220: break;
221: case (SCALE_IN):
222: r = 10 * su->scale;
223: break;
224: case (SCALE_PC):
225: r = (10 * su->scale) / 6;
226: break;
227: case (SCALE_PT):
228: r = (10 * su->scale) / 72;
229: break;
230: case (SCALE_MM):
231: r = su->scale / 1000;
232: break;
233: case (SCALE_VS):
234: r = su->scale * 2 - 1;
235: break;
236: default:
237: r = su->scale;
238: break;
239: }
240:
241: return(r);
242: }
243:
1.6 schwarze 244: /* ARGSUSED */
245: static size_t
246: locale_width(const struct termp *p, int c)
247: {
248: int rc;
249:
250: return((rc = wcwidth(c)) < 0 ? 0 : rc);
251: }
252:
253: /* ARGSUSED */
254: static void
255: locale_advance(struct termp *p, size_t len)
256: {
257: size_t i;
258:
259: for (i = 0; i < len; i++)
260: putwchar(L' ');
261: }
262:
263: /* ARGSUSED */
264: static void
265: locale_endline(struct termp *p)
266: {
267:
268: putwchar(L'\n');
269: }
270:
271: /* ARGSUSED */
272: static void
273: locale_letter(struct termp *p, int c)
274: {
275:
276: putwchar(c);
277: }