tr: read transparent output escape sequence in tr.c
[neatroff.git] / char.c
blob3d1b2fc5a4c26a0ff7da4c69fbe292ba7840fea0
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 cmd = c;
75 *d++ = c;
76 c = next();
78 if (c == '(') {
79 *d++ = next();
80 *d++ = next();
81 } else if (!n_cp && c == '[') {
82 c = next();
83 while (c > 0 && c != '\n' && c != ']') {
84 *d++ = c;
85 c = next();
87 } else {
88 *d++ = c;
89 if (cmd == 's' && c >= '1' && c <= '3') {
90 c = next();
91 if (isdigit(c))
92 *d++ = c;
93 else
94 back(c);
97 *d = '\0';
101 * read the next character or escape sequence (x, \x, \(xy, \[xyz], \C'xyz')
103 * character returned contents of c
104 * x '\0' x
105 * \4x c_ni \4x
106 * \\x '\\' \\x
107 * \\(xy '(' xy
108 * \\[xyz] '[' xyz
109 * \\C'xyz' 'C' xyz
111 int charnext(char *c, int (*next)(void), void (*back)(int))
113 int l, n;
114 if (!utf8next(c, next))
115 return -1;
116 if (c[0] == c_ni) {
117 utf8next(c + 1, next);
118 return c_ni;
120 if (c[0] == c_ec) {
121 utf8next(c + 1, next);
122 if (c[1] == '(') {
123 l = utf8next(c, next);
124 l += utf8next(c + l, next);
125 return '(';
126 } else if (!n_cp && c[1] == '[') {
127 l = 0;
128 n = next();
129 while (n >= 0 && n != '\n' && n != ']' && l < GNLEN - 1) {
130 c[l++] = n;
131 n = next();
133 c[l] = '\0';
134 return '[';
135 } else if (c[1] == 'C') {
136 quotednext(c, next, back);
137 return 'C';
139 return '\\';
141 return '\0';
144 /* like nextchar(), but return -1 if delim was read */
145 int charnext_delim(char *c, int (*next)(void), void (*back)(int), char *delim)
147 int t = charnext(c, next, back);
148 return strcmp(c, delim) ? t : -1;
151 /* convert back the character read from nextchar() (e.g. xy -> \\(xy) */
152 void charnext_str(char *d, char *c)
154 int c0 = (unsigned char) c[0];
155 if (c0 == c_ec || c0 == c_ni || !c[1] || utf8one(c)) {
156 strcpy(d, c);
157 return;
159 if (!c[2] && utf8len(c0) == 1)
160 sprintf(d, "%c(%s", c_ec, c);
161 else
162 sprintf(d, "%cC'%s'", c_ec, c);
165 /* like charnext() for string buffers */
166 int charread(char **s, char *c)
168 int ret;
169 sstr_push(*s);
170 ret = charnext(c, sstr_next, sstr_back);
171 *s = sstr_pop();
172 return ret;
175 /* like charnext_delim() for string buffers */
176 int charread_delim(char **s, char *c, char *delim)
178 int ret;
179 sstr_push(*s);
180 ret = charnext_delim(c, sstr_next, sstr_back, delim);
181 *s = sstr_pop();
182 return ret;
185 /* read quoted arguments; this is called only for internal neatroff strings */
186 static void quotedread(char **sp, char *d)
188 char *s = *sp;
189 int q = *s++;
190 while (*s && *s != q)
191 *d++ = *s++;
192 if (*s == q)
193 s++;
194 *d = '\0';
195 *sp = s;
198 /* read unquoted arguments; this is called only for internal neatroff strings */
199 static void unquotedread(char **sp, char *d)
201 char *s = *sp;
202 if (*s == '(') {
203 s++;
204 *d++ = *s++;
205 *d++ = *s++;
206 } else if (!n_cp && *s == '[') {
207 s++;
208 while (*s && *s != ']')
209 *d++ = *s++;
210 if (*s == ']')
211 s++;
212 } else {
213 *d++ = *s++;
215 *d = '\0';
216 *sp = s;
220 * read a glyph or an escape sequence
222 * This function reads from s either an output troff request
223 * (only the ones emitted by wb.c) or a glyph name and updates
224 * s. The return value is the name of the troff request (the
225 * argument is copied into d) or zero for glyph names (it is
226 * copied into d). Returns -1 when the end of s is reached.
228 int escread(char **s, char *d)
230 char *r = d;
231 if (!**s)
232 return -1;
233 utf8read(s, d);
234 if (d[0] == c_ec) {
235 utf8read(s, d + 1);
236 if (d[1] == '(') {
237 utf8read(s, d);
238 utf8read(s, d + strlen(d));
239 } else if (!n_cp && d[1] == '[') {
240 while (**s && **s != ']')
241 *r++ = *(*s)++;
242 if (**s == ']')
243 (*s)++;
244 } else if (strchr("CDfhmsvXx", d[1])) {
245 int c = d[1];
246 d[0] = '\0';
247 if (strchr(ESC_P, c))
248 unquotedread(s, d);
249 if (strchr(ESC_Q, c))
250 quotedread(s, d);
251 return c == 'C' ? 0 : c;
254 if (d[0] == c_ni)
255 utf8read(s, d + 1);
256 return 0;
260 * string streams: provide next()/back() interface for string buffers
262 * Functions like charnext() require a next()/back() interface
263 * for reading input streams. In order to provide this interface
264 * for string buffers, the following functions can be used:
266 * sstr_push(s);
267 * charnext(c, sstr_next, sstr_back);
268 * sstr_pop();
270 * The calls to sstr_push()/sstr_pop() may be nested.
272 static char *sstr_bufs[NSSTR]; /* buffer stack */
273 static int sstr_n; /* numbers of items in sstr_bufs[] */
274 static char *sstr_s; /* current buffer */
276 void sstr_push(char *s)
278 sstr_bufs[sstr_n++] = sstr_s;
279 sstr_s = s;
282 char *sstr_pop(void)
284 char *ret = sstr_s;
285 sstr_s = sstr_bufs[--sstr_n];
286 return ret;
289 int sstr_next(void)
291 return *sstr_s ? (unsigned char) *sstr_s++ : -1;
294 void sstr_back(int c)
296 sstr_s--;