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