tr: escaped spaces inside macro arguments
[neatroff.git] / char.c
bloba13d412a1562b0580cccc767a86b751c767f942e
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;
58 /* read quoted arguments of escape sequences (ESC_Q) */
59 void quotednext(char *d, int (*next)(void), void (*back)(int))
61 char delim[GNLEN], cs[GNLEN];
62 charnext(delim, next, back);
63 while (charnext_delim(cs, next, back, delim) >= 0) {
64 charnext_str(d, cs);
65 d = strchr(d, '\0');
69 /* read unquoted arguments of escape sequences (ESC_P) */
70 void unquotednext(char *d, int cmd, int (*next)(void), void (*back)(int))
72 int c = next();
73 if (cmd == 's' && (c == '-' || c == '+')) {
74 *d++ = c;
75 c = next();
77 if (c == '(') {
78 *d++ = next();
79 *d++ = next();
80 } else if (!n_cp && c == '[') {
81 c = next();
82 while (c > 0 && c != '\n' && c != ']') {
83 *d++ = c;
84 c = next();
86 } else {
87 *d++ = c;
88 if (cmd == 's' && c >= '1' && c <= '3') {
89 c = next();
90 if (isdigit(c))
91 *d++ = c;
92 else
93 back(c);
96 *d = '\0';
100 * read the next character or escape sequence (x, \x, \(xy, \[xyz], \C'xyz')
102 * character returned contents of c
103 * x '\0' x
104 * \4x c_ni \4x
105 * \\x '\\' \\x
106 * \\(xy '(' xy
107 * \\[xyz] '[' xyz
108 * \\C'xyz' 'C' xyz
110 int charnext(char *c, int (*next)(void), void (*back)(int))
112 int l, n;
113 if (!utf8next(c, next))
114 return -1;
115 if (c[0] == c_ni) {
116 utf8next(c + 1, next);
117 return c_ni;
119 if (c[0] == c_ec) {
120 utf8next(c + 1, next);
121 if (c[1] == '(') {
122 l = utf8next(c, next);
123 l += utf8next(c + l, next);
124 return '(';
125 } else if (!n_cp && c[1] == '[') {
126 l = 0;
127 n = next();
128 while (n >= 0 && n != '\n' && n != ']' && l < GNLEN - 1) {
129 c[l++] = n;
130 n = next();
132 c[l] = '\0';
133 return '[';
134 } else if (c[1] == 'C') {
135 quotednext(c, next, back);
136 return 'C';
138 return '\\';
140 return '\0';
143 /* like nextchar(), but return -1 if delim was read */
144 int charnext_delim(char *c, int (*next)(void), void (*back)(int), char *delim)
146 int t = charnext(c, next, back);
147 return strcmp(c, delim) ? t : -1;
150 /* convert back the character read from nextchar() (e.g. xy -> \\(xy) */
151 void charnext_str(char *d, char *c)
153 int c0 = (unsigned char) c[0];
154 if (c0 == c_ec || c0 == c_ni || !c[1] || utf8one(c)) {
155 strcpy(d, c);
156 return;
158 if (!c[2] && utf8len(c0) == 1)
159 sprintf(d, "%c(%s", c_ec, c);
160 else
161 sprintf(d, "%cC'%s'", c_ec, c);
164 /* like charnext() for string buffers */
165 int charread(char **s, char *c)
167 int ret;
168 sstr_push(*s);
169 ret = charnext(c, sstr_next, sstr_back);
170 *s = sstr_pop();
171 return ret;
174 /* like charnext_delim() for string buffers */
175 int charread_delim(char **s, char *c, char *delim)
177 int ret;
178 sstr_push(*s);
179 ret = charnext_delim(c, sstr_next, sstr_back, delim);
180 *s = sstr_pop();
181 return ret;
184 /* read quoted arguments; this is called only for internal neatroff strings */
185 static void quotedread(char **sp, char *d)
187 char *s = *sp;
188 int q = *s++;
189 while (*s && *s != q)
190 *d++ = *s++;
191 if (*s == q)
192 s++;
193 *d = '\0';
194 *sp = s;
197 /* read unquoted arguments; this is called only for internal neatroff strings */
198 static void unquotedread(char **sp, char *d)
200 char *s = *sp;
201 if (*s == '(') {
202 s++;
203 *d++ = *s++;
204 *d++ = *s++;
205 } else if (!n_cp && *s == '[') {
206 s++;
207 while (*s && *s != ']')
208 *d++ = *s++;
209 if (*s == ']')
210 s++;
211 } else {
212 *d++ = *s++;
214 *d = '\0';
215 *sp = s;
219 * read a glyph or an escape sequence
221 * This function reads from s either an output troff request
222 * (only the ones emitted by wb.c) or a glyph name and updates
223 * s. The return value is the name of the troff request (the
224 * argument is copied into d) or zero for glyph names (it is
225 * copied into d). Returns -1 when the end of s is reached.
227 int escread(char **s, char *d)
229 char *r = d;
230 if (!**s)
231 return -1;
232 utf8read(s, d);
233 if (d[0] == c_ec) {
234 utf8read(s, d + 1);
235 if (d[1] == '(') {
236 utf8read(s, d);
237 utf8read(s, d + strlen(d));
238 } else if (!n_cp && d[1] == '[') {
239 while (**s && **s != ']')
240 *r++ = *(*s)++;
241 if (**s == ']')
242 (*s)++;
243 } else if (strchr("CDfhmsvXx", d[1])) {
244 int c = d[1];
245 d[0] = '\0';
246 if (strchr(ESC_P, c))
247 unquotedread(s, d);
248 if (strchr(ESC_Q, c))
249 quotedread(s, d);
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--;