8 if (c
> 0 && c
<= 0x7f)
23 /* return nonzero if s is a single utf-8 character */
26 return !s
[utf8len((unsigned char) *s
)];
29 int utf8read(char **s
, char *d
)
31 int l
= utf8len((unsigned char) **s
);
33 for (i
= 0; i
< l
; i
++)
40 int utf8next(char *s
, int (*next
)(void))
48 for (i
= 1; i
< l
; i
++)
55 * read the next character or escape sequence (x, \x, \(xy, \[xyz], \C'xyz')
57 * character returned contents of c
65 int charnext(char *c
, int (*next
)(void), void (*back
)(int))
68 if (!utf8next(c
, next
))
71 utf8next(c
+ 1, next
);
75 utf8next(c
+ 1, next
);
77 l
= utf8next(c
, next
);
78 l
+= utf8next(c
+ l
, next
);
80 } else if (!n_cp
&& c
[1] == '[') {
83 while (n
>= 0 && n
!= '\n' && n
!= ']' && l
< GNLEN
- 1) {
89 } else if (c
[1] == 'C') {
90 argnext(c
, 'C', next
, back
);
98 /* like nextchar(), but return -1 if delim was read */
99 int charnext_delim(char *c
, int (*next
)(void), void (*back
)(int), char *delim
)
101 int t
= charnext(c
, next
, back
);
102 return strcmp(c
, delim
) ? t
: -1;
105 /* convert back the character read from nextchar() (e.g. xy -> \\(xy) */
106 void charnext_str(char *d
, char *c
)
108 int c0
= (unsigned char) c
[0];
109 if (c0
== c_ec
|| c0
== c_ni
|| !c
[1] || utf8one(c
)) {
113 if (!c
[2] && utf8len(c0
) == 1)
114 sprintf(d
, "%c(%s", c_ec
, c
);
116 sprintf(d
, "%cC'%s'", c_ec
, c
);
119 /* like charnext() for string buffers */
120 int charread(char **s
, char *c
)
124 ret
= charnext(c
, sstr_next
, sstr_back
);
129 /* read the argument of a troff escape sequence */
130 void argnext(char *d
, int cmd
, int (*next
)(void), void (*back
)(int))
132 char delim
[GNLEN
], cs
[GNLEN
];
134 if (strchr(ESC_P
, cmd
)) {
136 if (cmd
== 's' && (c
== '-' || c
== '+')) {
143 } else if (!n_cp
&& c
== '[') {
145 while (c
> 0 && c
!= '\n' && c
!= ']') {
151 if (cmd
== 's' && c
>= '1' && c
<= '3') {
160 if (strchr(ESC_Q
, cmd
)) {
161 charnext(delim
, next
, back
);
162 while (charnext_delim(cs
, next
, back
, delim
) >= 0) {
170 /* this is called only for internal neatroff strings */
171 void argread(char **sp
, char *d
, int cmd
)
175 if (strchr(ESC_P
, cmd
)) {
176 if (cmd
== 's' && (*s
== '-' || *s
== '+'))
182 } else if (!n_cp
&& *s
== '[') {
184 while (*s
&& *s
!= ']')
190 if (cmd
== 's' && s
[-1] >= '1' && s
[-1] <= '3')
195 if (strchr(ESC_Q
, cmd
)) {
197 while (*s
&& *s
!= q
)
209 * read a glyph or an escape sequence
211 * This functions reads from s either an output troff request
212 * (only the ones emitted by wb.c) or a glyph name and updates
213 * s. The return value is the name of the troff request (the
214 * argument is copied into d) or zero for glyph names (it is
215 * copied into d). Returns -1 when the end of s is reached.
217 int escread(char **s
, char *d
)
227 utf8read(s
, d
+ strlen(d
));
228 } else if (!n_cp
&& d
[1] == '[') {
229 while (**s
&& **s
!= ']')
233 } else if (strchr("CDfhmsvXx", d
[1])) {
236 return c
== 'C' ? 0 : c
;
245 * string streams: provide next()/back() interface for string buffers
247 * Functions like charnext() require a next()/back() interface
248 * for reading input streams. In order to provide this interface
249 * for string buffers, the following functions can be used:
252 * charnext(c, sstr_next, sstr_back);
255 * The calls to sstr_push()/sstr_pop() may be nested.
257 static char *sstr_bufs
[NSSTR
]; /* buffer stack */
258 static int sstr_n
; /* numbers of items in sstr_bufs[] */
259 static char *sstr_s
; /* current buffer */
261 void sstr_push(char *s
)
263 sstr_bufs
[sstr_n
++] = sstr_s
;
270 sstr_s
= sstr_bufs
[--sstr_n
];
276 return *sstr_s
? (unsigned char) *sstr_s
++ : -1;
279 void sstr_back(int c
)