1 /* reading characters and escapes */
7 /* return the length of a utf-8 character based on its first byte */
10 if (c
> 0 && c
<= 0x7f)
25 /* return nonzero if s is a single utf-8 character */
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
);
36 for (i
= 0; i
< l
; i
++)
43 /* read a utf-8 character with next() and copy it to s */
44 int utf8next(char *s
, int (*next
)(void))
52 for (i
= 1; i
< l
; i
++)
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) {
69 /* read unquoted arguments of escape sequences (ESC_P) */
70 void unquotednext(char *d
, int cmd
, int (*next
)(void), void (*back
)(int))
73 if (cmd
== 's' && (c
== '-' || c
== '+')) {
81 } else if (!n_cp
&& c
== '[') {
83 while (c
> 0 && c
!= '\n' && c
!= ']') {
89 if (cmd
== 's' && c
>= '1' && c
<= '3') {
101 * read the next character or escape sequence (x, \x, \(xy, \[xyz], \C'xyz')
103 * character returned contents of c
111 int charnext(char *c
, int (*next
)(void), void (*back
)(int))
114 if (!utf8next(c
, next
))
117 utf8next(c
+ 1, next
);
121 utf8next(c
+ 1, next
);
123 l
= utf8next(c
, next
);
124 l
+= utf8next(c
+ l
, next
);
126 } else if (!n_cp
&& c
[1] == '[') {
129 while (n
>= 0 && n
!= '\n' && n
!= ']' && l
< GNLEN
- 1) {
135 } else if (c
[1] == 'C') {
136 quotednext(c
, next
, back
);
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
)) {
159 if (!c
[2] && utf8len(c0
) == 1)
160 sprintf(d
, "%c(%s", c_ec
, c
);
162 sprintf(d
, "%cC'%s'", c_ec
, c
);
165 /* like charnext() for string buffers */
166 int charread(char **s
, char *c
)
170 ret
= charnext(c
, sstr_next
, sstr_back
);
175 /* like charnext_delim() for string buffers */
176 int charread_delim(char **s
, char *c
, char *delim
)
180 ret
= charnext_delim(c
, sstr_next
, sstr_back
, delim
);
185 /* read quoted arguments; this is called only for internal neatroff strings */
186 static void quotedread(char **sp
, char *d
)
190 while (*s
&& *s
!= q
)
198 /* read unquoted arguments; this is called only for internal neatroff strings */
199 static void unquotedread(char **sp
, char *d
)
206 } else if (!n_cp
&& *s
== '[') {
208 while (*s
&& *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
)
238 utf8read(s
, d
+ strlen(d
));
239 } else if (!n_cp
&& d
[1] == '[') {
240 while (**s
&& **s
!= ']')
244 } else if (strchr("CDfhmsvXx", d
[1])) {
247 if (strchr(ESC_P
, c
))
249 if (strchr(ESC_Q
, c
))
251 return c
== 'C' ? 0 : c
;
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:
267 * charnext(c, sstr_next, sstr_back);
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
;
285 sstr_s
= sstr_bufs
[--sstr_n
];
291 return *sstr_s
? (unsigned char) *sstr_s
++ : -1;
294 void sstr_back(int c
)