1 /* reading characters and escapes */
8 /* return the length of a utf-8 character based on its first byte */
24 /* return nonzero if s is a single utf-8 character */
27 return !s
[utf8len((unsigned char) *s
)];
30 /* read a utf-8 character from s and copy it to d */
31 int utf8read(char **s
, char *d
)
33 int l
= utf8len((unsigned char) **s
);
35 for (i
= 0; i
< l
; i
++)
42 /* read a utf-8 character with next() and copy it to s */
43 int utf8next(char *s
, int (*next
)(void))
51 for (i
= 1; i
< l
; i
++)
57 /* read quoted arguments of escape sequences (ESC_Q) */
58 char *quotednext(int (*next
)(void), void (*back
)(int))
60 char delim
[GNLEN
], cs
[GNLEN
];
63 charnext(delim
, next
, back
);
65 while (charnext_delim(cs
, next
, back
, delim
) >= 0) {
72 /* read unquoted arguments of escape sequences (ESC_P) */
73 char *unquotednext(int cmd
, int (*next
)(void), void (*back
)(int))
78 if (cmd
== 's' && (c
== '-' || c
== '+')) {
84 sbuf_add(&sb
, next());
85 sbuf_add(&sb
, next());
86 } else if (!n_cp
&& c
== '[') {
88 while (c
> 0 && c
!= '\n' && c
!= ']') {
94 if (cmd
== 's' && c
>= '1' && c
<= '3') {
102 return sbuf_out(&sb
);
106 * read the next character or escape sequence (x, \x, \(xy, \[xyz], \C'xyz')
108 * character returned contents of c
116 int charnext(char *c
, int (*next
)(void), void (*back
)(int))
119 if (!utf8next(c
, next
))
122 utf8next(c
+ 1, next
);
126 utf8next(c
+ 1, next
);
128 l
= utf8next(c
, next
);
129 l
+= utf8next(c
+ l
, next
);
131 } else if (!n_cp
&& c
[1] == '[') {
134 while (n
>= 0 && n
!= '\n' && n
!= ']' && l
< GNLEN
- 1) {
140 } else if (c
[1] == 'C') {
141 char *chr
= quotednext(next
, back
);
142 snprintf(c
, GNLEN
, "%s", chr
);
151 /* like nextchar(), but return -1 if delim was read */
152 int charnext_delim(char *c
, int (*next
)(void), void (*back
)(int), char *delim
)
154 int t
= charnext(c
, next
, back
);
155 return strcmp(c
, delim
) ? t
: -1;
158 /* convert back the character read from nextchar() (e.g. xy -> \\(xy) */
159 void charnext_str(char *d
, char *c
)
161 int c0
= (unsigned char) c
[0];
162 if (c0
== c_ec
|| c0
== c_ni
|| !c
[1] || utf8one(c
)) {
166 if (!c
[2] && utf8len(c0
) == 1)
167 sprintf(d
, "%c(%s", c_ec
, c
);
169 sprintf(d
, "%cC'%s'", c_ec
, c
);
172 /* like charnext() for string buffers */
173 int charread(char **s
, char *c
)
177 ret
= charnext(c
, sstr_next
, sstr_back
);
182 /* like charnext_delim() for string buffers */
183 int charread_delim(char **s
, char *c
, char *delim
)
187 ret
= charnext_delim(c
, sstr_next
, sstr_back
, delim
);
192 /* read quoted arguments; this is called only for internal neatroff strings */
193 static void quotedread(char **sp
, char *d
)
197 while (*s
&& *s
!= q
)
205 /* read unquoted arguments; this is called only for internal neatroff strings */
206 static void unquotedread(char **sp
, char *d
)
213 } else if (!n_cp
&& *s
== '[') {
215 while (*s
&& *s
!= ']')
227 * read a glyph or an escape sequence
229 * This function reads from s either an output troff request
230 * (only the ones emitted by wb.c) or a glyph name and updates
231 * s. The return value is the name of the troff request (the
232 * argument is copied into d) or zero for glyph names (it is
233 * copied into d). Returns -1 when the end of s is reached.
234 * Note that to d, a pointer to a static array is assigned.
236 int escread(char **s
, char **d
)
238 static char buf
[1 << 12];
249 utf8read(s
, r
+ strlen(r
));
250 } else if (!n_cp
&& r
[1] == '[') {
251 while (**s
&& **s
!= ']')
256 } else if (strchr("CDfhmsvXx", r
[1])) {
259 if (strchr(ESC_P
, c
))
261 if (strchr(ESC_Q
, c
))
263 return c
== 'C' ? 0 : c
;
265 } else if (r
[0] == c_ni
) {
272 * string streams: provide next()/back() interface for string buffers
274 * Functions like charnext() require a next()/back() interface
275 * for reading input streams. In order to provide this interface
276 * for string buffers, the following functions can be used:
279 * charnext(c, sstr_next, sstr_back);
282 * The calls to sstr_push()/sstr_pop() may be nested.
284 static char *sstr_bufs
[NSSTR
]; /* buffer stack */
285 static int sstr_n
; /* numbers of items in sstr_bufs[] */
286 static char *sstr_s
; /* current buffer */
288 void sstr_push(char *s
)
290 sstr_bufs
[sstr_n
++] = sstr_s
;
297 sstr_s
= sstr_bufs
[--sstr_n
];
303 return *sstr_s
? (unsigned char) *sstr_s
++ : -1;
306 void sstr_back(int c
)