summaryrefslogtreecommitdiff
path: root/fonts/hershey/parse.c
blob: 2dd5430192ffc6121116b300781960580bf65f23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include	<stdio.h>
#include	<ctype.h>
#include	"parse.h"

static char	copyright[]	= "Copyright (C) Ken Yap 1988";

static char	zerostring[]	= "0";
static char	onestring[]	= "1";
charinfo	global		= {
	-1, -1,
	onestring, onestring,
	};
charinfo	perchar;

static char *skipblanks(p)
	char		*p;
{
	while (isspace(*p) || *p == ',')	/* , is also space */
		++p;
	return (p);
}

static char *skipnonblanks(p)
	char		*p;
{
	while (*p != '\0' && !(isspace(*p) || *p == ','))
		++p;
	return (p);
}

static char *skippast(p, c)
	char		*p, c;
{
	while (*p != '\0' && *p != c)
		++p;
	return (*p == c ? p + 1 : p);
}

static int match(p, word)
	char		*p, *word;
{
	register int	l;

	l = strlen(word);
	return (strncmp(p, word, l) == 0);
}

static int getparams(p, ci)
	char		*p;
	charinfo	*ci;
{
	if (*p == '\'')			/* collect ascii index */
	{
		if (*++p != '\\')
			ci->asciiindex = (int)*p;
		else if (*++p == '\\')
			ci->asciiindex = (int)*p;
		else if (sscanf(p, "%o", &ci->asciiindex) != 1)
		{
			(void)fprintf(stderr, "Bad char index spec %s", p);
			return (0);
		}
		p = skippast(p+1, ',');
		p = skipblanks(p);
		if (!isdigit(*p))
		{
			(void)fprintf(stderr, "Missing Hershey index %s", p);
			return (0);
		}
		ci->hersheyindex = atoi(p);
		p = skippast(p, ',');
	}
	else if (isdigit(*p))
	{
		if (*p == '0')
		{
			if (sscanf(p, "%o", &ci->asciiindex) != 1)
			{
				(void)fprintf(stderr, "Bad char index spec %s", p);
				return (0);
			}
		}
		else
		{
			if (sscanf(p, "%d", &ci->asciiindex) != 1)
			{
				(void)fprintf(stderr, "Bad char index spec %s", p);
				return (0);
			}
		}
		p = skippast(p+1, ',');
		p = skipblanks(p);
		if (!isdigit(*p))
		{
			(void)fprintf(stderr, "Missing Hershey index %s", p);
			return (0);
		}
		ci->hersheyindex = atoi(p);
		p = skippast(p, ',');
	}
	p = skipblanks(p);
	while (*p != '\0')
	{
		if (match(p, "hoff"))
		{
			p = skippast(p, '=');
			p = skipblanks(p);
			ci->hoff = p;
		}
		if (match(p, "voff"))
		{
			p = skippast(p, '=');
			p = skipblanks(p);
			ci->voff = p;
		}
		if (match(p, "hratio"))
		{
			p = skippast(p, '=');
			p = skipblanks(p);
			ci->hratio = p;
		}
		if (match(p, "vratio"))
		{
			p = skippast(p, '=');
			p = skipblanks(p);
			ci->vratio = p;
		}
		p = skipnonblanks(p);
		if (*p != '\0')
		{
			*p = '\0';
			p = skipblanks(p+1);
		}
	}
	return (1);
}

int parsespecs(line)
	char		*line;
{
	register int	l;
	register char	*p;
	char		*malloc();

	line = skipblanks(line);
	if (*line == '\'')
	{
		perchar = global;		/* copy globals */
		return (getparams(line, &perchar));
	}
	else
	{
		l = strlen(line) + 1;
		if ((p = malloc(l)) == NULL)
		{
			perror("malloc");
			exit(1);
		}
		(void)strcpy(p, line);
		if (!getparams(p, &global))
			(void)fprintf(stderr, "Bad global spec %s", line);
		return (0);
	}
	/*NOTREACHED*/
}

#ifdef	STANDALONE
main()
{
	char		line[256];

	while (fgets(line, sizeof(line), stdin) != NULL)
		parsespecs(line);
	exit(0);
}
#endif	STANDALONE