hyph: read the correct file for the second argument of .hpf
[neatroff.git] / char.c
blob3817b586ff85583904b66e56cb94a191e61be0ba
1 /* reading characters and escapes */
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "roff.h"
7 /* return the length of a utf-8 character based on its first byte */
8 int utf8len(int c)
10 if (c > 0 && c <= 0x7f)
11 return 1;
12 if (c >= 0xfc)
13 return 6;
14 if (c >= 0xf8)
15 return 5;
16 if (c >= 0xf0)
17 return 4;
18 if (c >= 0xe0)
19 return 3;
20 if (c >= 0xc0)
21 return 2;
22 return c != 0;
25 /* return nonzero if s is a single utf-8 character */
26 int utf8one(char *s)
28 return !s[utf8len((unsigned char) *s)];
31 /* read a utf-8 character from s and copy it to d */
32 int utf8read(char **s, char *d)
34 int l = utf8len((unsigned char) **s);
35 int i;
36 for (i = 0; i < l; i++)
37 d[i] = (*s)[i];
38 d[l] = '\0';
39 *s += l;
40 return l;
43 /* read a utf-8 character with next() and copy it to s */
44 int utf8next(char *s, int (*next)(void))
46 int c = next();
47 int l = utf8len(c);
48 int i;
49 if (c < 0)
50 return 0;
51 s[0] = c;
52 for (i = 1; i < l; i++)
53 s[i] = next();
54 s[l] = '\0';
55 return l;
59 * read the next character or escape sequence (x, \x, \(xy, \[xyz], \C'xyz')
61 * character returned contents of c
62 * x '\0' x
63 * \4x c_ni \4x
64 * \\x '\\' \\x
65 * \\(xy '(' xy
66 * \\[xyz] '[' xyz
67 * \\C'xyz' 'C' xyz
69 int charnext(char *c, int (*next)(void), void (*back)(int))
71 int l, n;
72 if (!utf8next(c, next))
73 return -1;
74 if (c[0] == c_ni) {
75 utf8next(c + 1, next);
76 return c_ni;
78 if (c[0] == c_ec) {
79 utf8next(c + 1, next);
80 if (c[1] == '(') {
81 l = utf8next(c, next);
82 l += utf8next(c + l, next);
83 return '(';
84 } else if (!n_cp && c[1] == '[') {
85 l = 0;
86 n = next();
87 while (n >= 0 && n != '\n' && n != ']' && l < GNLEN - 1) {
88 c[l++] = n;
89 n = next();
91 c[l] = '\0';
92 return '[';
93 } else if (c[1] == 'C') {
94 argnext(c, 'C', next, back);
95 return 'C';
97 return '\\';
99 return '\0';
102 /* like nextchar(), but return -1 if delim was read */
103 int charnext_delim(char *c, int (*next)(void), void (*back)(int), char *delim)
105 int t = charnext(c, next, back);
106 return strcmp(c, delim) ? t : -1;
109 /* convert back the character read from nextchar() (e.g. xy -> \\(xy) */
110 void charnext_str(char *d, char *c)
112 int c0 = (unsigned char) c[0];
113 if (c0 == c_ec || c0 == c_ni || !c[1] || utf8one(c)) {
114 strcpy(d, c);
115 return;
117 if (!c[2] && utf8len(c0) == 1)
118 sprintf(d, "%c(%s", c_ec, c);
119 else
120 sprintf(d, "%cC'%s'", c_ec, c);
123 /* like charnext() for string buffers */
124 int charread(char **s, char *c)
126 int ret;
127 sstr_push(*s);
128 ret = charnext(c, sstr_next, sstr_back);
129 *s = sstr_pop();
130 return ret;
133 /* like charnext_delim() for string buffers */
134 int charread_delim(char **s, char *c, char *delim)
136 int ret;
137 sstr_push(*s);
138 ret = charnext_delim(c, sstr_next, sstr_back, delim);
139 *s = sstr_pop();
140 return ret;
143 /* read the argument of a troff escape sequence */
144 void argnext(char *d, int cmd, int (*next)(void), void (*back)(int))
146 char delim[GNLEN], cs[GNLEN];
147 int c;
148 if (strchr(ESC_P, cmd)) {
149 c = next();
150 if (cmd == 's' && (c == '-' || c == '+')) {
151 *d++ = c;
152 c = next();
154 if (c == '(') {
155 *d++ = next();
156 *d++ = next();
157 } else if (!n_cp && c == '[') {
158 c = next();
159 while (c > 0 && c != '\n' && c != ']') {
160 *d++ = c;
161 c = next();
163 } else {
164 *d++ = c;
165 if (cmd == 's' && c >= '1' && c <= '3') {
166 c = next();
167 if (isdigit(c))
168 *d++ = c;
169 else
170 back(c);
174 if (strchr(ESC_Q, cmd)) {
175 charnext(delim, next, back);
176 while (charnext_delim(cs, next, back, delim) >= 0) {
177 charnext_str(d, cs);
178 d = strchr(d, '\0');
181 *d = '\0';
184 /* this is called only for internal neatroff strings */
185 void argread(char **sp, char *d, int cmd)
187 char *s = *sp;
188 int q;
189 if (strchr(ESC_P, cmd)) {
190 if (cmd == 's' && (*s == '-' || *s == '+'))
191 *d++ = *s++;
192 if (*s == '(') {
193 s++;
194 *d++ = *s++;
195 *d++ = *s++;
196 } else if (!n_cp && *s == '[') {
197 s++;
198 while (*s && *s != ']')
199 *d++ = *s++;
200 if (*s == ']')
201 s++;
202 } else {
203 *d++ = *s++;
204 if (cmd == 's' && s[-1] >= '1' && s[-1] <= '3')
205 if (isdigit(*s))
206 *d++ = *s++;
209 if (strchr(ESC_Q, cmd)) {
210 q = *s++;
211 while (*s && *s != q)
212 *d++ = *s++;
213 if (*s == q)
214 s++;
216 if (cmd == 'z')
217 *d++ = *s++;
218 *d = '\0';
219 *sp = s;
223 * read a glyph or an escape sequence
225 * This function reads from s either an output troff request
226 * (only the ones emitted by wb.c) or a glyph name and updates
227 * s. The return value is the name of the troff request (the
228 * argument is copied into d) or zero for glyph names (it is
229 * copied into d). Returns -1 when the end of s is reached.
231 int escread(char **s, char *d)
233 char *r = d;
234 if (!**s)
235 return -1;
236 utf8read(s, d);
237 if (d[0] == c_ec) {
238 utf8read(s, d + 1);
239 if (d[1] == '(') {
240 utf8read(s, d);
241 utf8read(s, d + strlen(d));
242 } else if (!n_cp && d[1] == '[') {
243 while (**s && **s != ']')
244 *r++ = *(*s)++;
245 if (**s == ']')
246 (*s)++;
247 } else if (strchr("CDfhmsvXx", d[1])) {
248 int c = d[1];
249 argread(s, d, d[1]);
250 return c == 'C' ? 0 : c;
253 if (d[0] == c_ni)
254 utf8read(s, d + 1);
255 return 0;
259 * string streams: provide next()/back() interface for string buffers
261 * Functions like charnext() require a next()/back() interface
262 * for reading input streams. In order to provide this interface
263 * for string buffers, the following functions can be used:
265 * sstr_push(s);
266 * charnext(c, sstr_next, sstr_back);
267 * sstr_pop();
269 * The calls to sstr_push()/sstr_pop() may be nested.
271 static char *sstr_bufs[NSSTR]; /* buffer stack */
272 static int sstr_n; /* numbers of items in sstr_bufs[] */
273 static char *sstr_s; /* current buffer */
275 void sstr_push(char *s)
277 sstr_bufs[sstr_n++] = sstr_s;
278 sstr_s = s;
281 char *sstr_pop(void)
283 char *ret = sstr_s;
284 sstr_s = sstr_bufs[--sstr_n];
285 return ret;
288 int sstr_next(void)
290 return *sstr_s ? (unsigned char) *sstr_s++ : -1;
293 void sstr_back(int c)
295 sstr_s--;