xref: /openbsd/usr.bin/mandoc/out.c (revision 4175bdab)
1*4175bdabSschwarze /*	$Id: out.c,v 1.1 2009/10/21 19:13:51 schwarze Exp $ */
2*4175bdabSschwarze /*
3*4175bdabSschwarze  * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
4*4175bdabSschwarze  *
5*4175bdabSschwarze  * Permission to use, copy, modify, and distribute this software for any
6*4175bdabSschwarze  * purpose with or without fee is hereby granted, provided that the above
7*4175bdabSschwarze  * copyright notice and this permission notice appear in all copies.
8*4175bdabSschwarze  *
9*4175bdabSschwarze  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*4175bdabSschwarze  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*4175bdabSschwarze  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*4175bdabSschwarze  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*4175bdabSschwarze  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*4175bdabSschwarze  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*4175bdabSschwarze  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*4175bdabSschwarze  */
17*4175bdabSschwarze #include <sys/types.h>
18*4175bdabSschwarze 
19*4175bdabSschwarze #include <ctype.h>
20*4175bdabSschwarze #include <stdio.h>
21*4175bdabSschwarze #include <stdlib.h>
22*4175bdabSschwarze 
23*4175bdabSschwarze #include "out.h"
24*4175bdabSschwarze 
25*4175bdabSschwarze 
26*4175bdabSschwarze /*
27*4175bdabSschwarze  * Convert a `scaling unit' to a consistent form, or fail.  Scaling
28*4175bdabSschwarze  * units are documented in groff.7, mdoc.7, man.7.
29*4175bdabSschwarze  */
30*4175bdabSschwarze int
31*4175bdabSschwarze a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
32*4175bdabSschwarze {
33*4175bdabSschwarze 	char		 buf[BUFSIZ], hasd;
34*4175bdabSschwarze 	int		 i;
35*4175bdabSschwarze 	enum roffscale	 unit;
36*4175bdabSschwarze 
37*4175bdabSschwarze 	if ('\0' == *src)
38*4175bdabSschwarze 		return(0);
39*4175bdabSschwarze 
40*4175bdabSschwarze 	i = hasd = 0;
41*4175bdabSschwarze 
42*4175bdabSschwarze 	switch (*src) {
43*4175bdabSschwarze 	case ('+'):
44*4175bdabSschwarze 		src++;
45*4175bdabSschwarze 		break;
46*4175bdabSschwarze 	case ('-'):
47*4175bdabSschwarze 		buf[i++] = *src++;
48*4175bdabSschwarze 		break;
49*4175bdabSschwarze 	default:
50*4175bdabSschwarze 		break;
51*4175bdabSschwarze 	}
52*4175bdabSschwarze 
53*4175bdabSschwarze 	if ('\0' == *src)
54*4175bdabSschwarze 		return(0);
55*4175bdabSschwarze 
56*4175bdabSschwarze 	while (i < BUFSIZ) {
57*4175bdabSschwarze 		if ( ! isdigit((u_char)*src)) {
58*4175bdabSschwarze 			if ('.' != *src)
59*4175bdabSschwarze 				break;
60*4175bdabSschwarze 			else if (hasd)
61*4175bdabSschwarze 				break;
62*4175bdabSschwarze 			else
63*4175bdabSschwarze 				hasd = 1;
64*4175bdabSschwarze 		}
65*4175bdabSschwarze 		buf[i++] = *src++;
66*4175bdabSschwarze 	}
67*4175bdabSschwarze 
68*4175bdabSschwarze 	if (BUFSIZ == i || (*src && *(src + 1)))
69*4175bdabSschwarze 		return(0);
70*4175bdabSschwarze 
71*4175bdabSschwarze 	buf[i] = '\0';
72*4175bdabSschwarze 
73*4175bdabSschwarze 	switch (*src) {
74*4175bdabSschwarze 	case ('c'):
75*4175bdabSschwarze 		unit = SCALE_CM;
76*4175bdabSschwarze 		break;
77*4175bdabSschwarze 	case ('i'):
78*4175bdabSschwarze 		unit = SCALE_IN;
79*4175bdabSschwarze 		break;
80*4175bdabSschwarze 	case ('P'):
81*4175bdabSschwarze 		unit = SCALE_PC;
82*4175bdabSschwarze 		break;
83*4175bdabSschwarze 	case ('p'):
84*4175bdabSschwarze 		unit = SCALE_PT;
85*4175bdabSschwarze 		break;
86*4175bdabSschwarze 	case ('f'):
87*4175bdabSschwarze 		unit = SCALE_FS;
88*4175bdabSschwarze 		break;
89*4175bdabSschwarze 	case ('v'):
90*4175bdabSschwarze 		unit = SCALE_VS;
91*4175bdabSschwarze 		break;
92*4175bdabSschwarze 	case ('m'):
93*4175bdabSschwarze 		unit = SCALE_EM;
94*4175bdabSschwarze 		break;
95*4175bdabSschwarze 	case ('\0'):
96*4175bdabSschwarze 		if (SCALE_MAX == def)
97*4175bdabSschwarze 			return(0);
98*4175bdabSschwarze 		unit = SCALE_BU;
99*4175bdabSschwarze 		break;
100*4175bdabSschwarze 	case ('u'):
101*4175bdabSschwarze 		unit = SCALE_BU;
102*4175bdabSschwarze 		break;
103*4175bdabSschwarze 	case ('M'):
104*4175bdabSschwarze 		unit = SCALE_MM;
105*4175bdabSschwarze 		break;
106*4175bdabSschwarze 	case ('n'):
107*4175bdabSschwarze 		unit = SCALE_EN;
108*4175bdabSschwarze 		break;
109*4175bdabSschwarze 	default:
110*4175bdabSschwarze 		return(0);
111*4175bdabSschwarze 	}
112*4175bdabSschwarze 
113*4175bdabSschwarze 	if ((dst->scale = atof(buf)) < 0)
114*4175bdabSschwarze 		dst->scale = 0;
115*4175bdabSschwarze 	dst->unit = unit;
116*4175bdabSschwarze 	dst->pt = hasd;
117*4175bdabSschwarze 
118*4175bdabSschwarze 	return(1);
119*4175bdabSschwarze }
120