8 if (c
> 0 && c
<= 0x7f)
23 int utf8read(char **s
, char *d
)
25 int l
= utf8len((unsigned char) **s
);
27 for (i
= 0; i
< l
; i
++)
34 int utf8next(char *s
, int (*next
)(void))
42 for (i
= 1; i
< l
; i
++)
49 * read the next character or escape sequence (x, \x, \(xy, \[xyz], \C'xyz')
51 * character returned contents of c
59 int charnext(char *c
, int (*next
)(void), void (*back
)(int))
62 if (!utf8next(c
, next
))
65 utf8next(c
+ 1, next
);
69 utf8next(c
+ 1, next
);
71 l
= utf8next(c
, next
);
72 l
+= utf8next(c
+ l
, next
);
74 } else if (!n_cp
&& c
[1] == '[') {
77 while (n
>= 0 && n
!= '\n' && n
!= ']' && l
< GNLEN
- 1) {
83 } else if (c
[1] == 'C') {
84 argnext(c
, 'C', next
, back
);
92 /* like nextchar(), but return -1 if delim was read */
93 int charnext_delim(char *c
, int (*next
)(void), void (*back
)(int), char *delim
)
95 int t
= charnext(c
, next
, back
);
96 return strcmp(c
, delim
) ? t
: -1;
99 /* convert back the character read from nextchar() (e.g. xy -> \\(xy) */
100 void charnext_str(char *d
, char *c
)
102 int c0
= (unsigned char) c
[0];
103 if (c0
== c_ec
|| c0
== c_ni
|| !c
[1] || utf8len(c0
) == strlen(c
)) {
107 if (!c
[2] && utf8len(c0
) == 1)
108 sprintf(d
, "%c(%s", c_ec
, c
);
110 sprintf(d
, "%cC'%s'", c_ec
, c
);
113 /* like charnext() for string buffers */
114 int charread(char **s
, char *c
)
118 ret
= charnext(c
, sstr_next
, sstr_back
);
123 /* read the argument of a troff escape sequence */
124 void argnext(char *d
, int cmd
, int (*next
)(void), void (*back
)(int))
126 char delim
[GNLEN
], cs
[GNLEN
];
128 if (strchr(ESC_P
, cmd
)) {
130 if (cmd
== 's' && (c
== '-' || c
== '+')) {
137 } else if (!n_cp
&& c
== '[') {
139 while (c
> 0 && c
!= '\n' && c
!= ']') {
145 if (cmd
== 's' && c
>= '1' && c
<= '3') {
154 if (strchr(ESC_Q
, cmd
)) {
155 charnext(delim
, next
, back
);
156 while (charnext_delim(cs
, next
, back
, delim
) >= 0) {
164 /* this is called only for internal neatroff strings */
165 void argread(char **sp
, char *d
, int cmd
)
169 if (strchr(ESC_P
, cmd
)) {
170 if (cmd
== 's' && (*s
== '-' || *s
== '+'))
176 } else if (!n_cp
&& *s
== '[') {
178 while (*s
&& *s
!= ']')
184 if (cmd
== 's' && s
[-1] >= '1' && s
[-1] <= '3')
189 if (strchr(ESC_Q
, cmd
)) {
191 while (*s
&& *s
!= q
)
203 * read a glyph or an escape sequence
205 * This functions reads from s either an output troff request
206 * (only the ones emitted by wb.c) or a glyph name and updates
207 * s. The return value is the name of the troff request (the
208 * argument is copied into d) or zero for glyph names (it is
209 * copied into d). Returns -1 when the end of s is reached.
211 int escread(char **s
, char *d
)
221 utf8read(s
, d
+ strlen(d
));
222 } else if (!n_cp
&& d
[1] == '[') {
223 while (**s
&& **s
!= ']')
227 } else if (strchr("CDfhmsvXx", d
[1])) {
230 return c
== 'C' ? 0 : c
;
239 * string streams: provide next()/back() interface for string buffers
241 * Functions like charnext() require a next()/back() interface
242 * for reading input streams. In order to provide this interface
243 * for string buffers, the following functions can be used:
246 * charnext(c, sstr_next, sstr_back);
249 * The calls to sstr_push()/sstr_pop() may be nested.
251 static char *sstr_bufs
[NSSTR
]; /* buffer stack */
252 static int sstr_n
; /* numbers of items in sstr_bufs[] */
253 static char *sstr_s
; /* current buffer */
255 void sstr_push(char *s
)
257 sstr_bufs
[sstr_n
++] = sstr_s
;
264 sstr_s
= sstr_bufs
[--sstr_n
];
270 return *sstr_s
? (unsigned char) *sstr_s
++ : -1;
273 void sstr_back(int c
)