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
++)
59 * read the next character or escape sequence (x, \x, \(xy, \[xyz], \C'xyz')
61 * character returned contents of c
69 int charnext(char *c
, int (*next
)(void), void (*back
)(int))
72 if (!utf8next(c
, next
))
75 utf8next(c
+ 1, next
);
79 utf8next(c
+ 1, next
);
81 l
= utf8next(c
, next
);
82 l
+= utf8next(c
+ l
, next
);
84 } else if (!n_cp
&& c
[1] == '[') {
87 while (n
>= 0 && n
!= '\n' && n
!= ']' && l
< GNLEN
- 1) {
93 } else if (c
[1] == 'C') {
94 argnext(c
, 'C', next
, back
);
102 /* like nextchar(), but return -1 if delim was read */
103 int charnext_delim(char *c
, int (*next
)(void), void (*back
)(int), char *delim
)
105 int t
= charnext(c
, next
, back
);
106 return strcmp(c
, delim
) ? t
: -1;
109 /* convert back the character read from nextchar() (e.g. xy -> \\(xy) */
110 void charnext_str(char *d
, char *c
)
112 int c0
= (unsigned char) c
[0];
113 if (c0
== c_ec
|| c0
== c_ni
|| !c
[1] || utf8one(c
)) {
117 if (!c
[2] && utf8len(c0
) == 1)
118 sprintf(d
, "%c(%s", c_ec
, c
);
120 sprintf(d
, "%cC'%s'", c_ec
, c
);
123 /* like charnext() for string buffers */
124 int charread(char **s
, char *c
)
128 ret
= charnext(c
, sstr_next
, sstr_back
);
133 /* read the argument of a troff escape sequence */
134 void argnext(char *d
, int cmd
, int (*next
)(void), void (*back
)(int))
136 char delim
[GNLEN
], cs
[GNLEN
];
138 if (strchr(ESC_P
, cmd
)) {
140 if (cmd
== 's' && (c
== '-' || c
== '+')) {
147 } else if (!n_cp
&& c
== '[') {
149 while (c
> 0 && c
!= '\n' && c
!= ']') {
155 if (cmd
== 's' && c
>= '1' && c
<= '3') {
164 if (strchr(ESC_Q
, cmd
)) {
165 charnext(delim
, next
, back
);
166 while (charnext_delim(cs
, next
, back
, delim
) >= 0) {
174 /* this is called only for internal neatroff strings */
175 void argread(char **sp
, char *d
, int cmd
)
179 if (strchr(ESC_P
, cmd
)) {
180 if (cmd
== 's' && (*s
== '-' || *s
== '+'))
186 } else if (!n_cp
&& *s
== '[') {
188 while (*s
&& *s
!= ']')
194 if (cmd
== 's' && s
[-1] >= '1' && s
[-1] <= '3')
199 if (strchr(ESC_Q
, cmd
)) {
201 while (*s
&& *s
!= q
)
213 * read a glyph or an escape sequence
215 * This function reads from s either an output troff request
216 * (only the ones emitted by wb.c) or a glyph name and updates
217 * s. The return value is the name of the troff request (the
218 * argument is copied into d) or zero for glyph names (it is
219 * copied into d). Returns -1 when the end of s is reached.
221 int escread(char **s
, char *d
)
231 utf8read(s
, d
+ strlen(d
));
232 } else if (!n_cp
&& d
[1] == '[') {
233 while (**s
&& **s
!= ']')
237 } else if (strchr("CDfhmsvXx", d
[1])) {
240 return c
== 'C' ? 0 : c
;
249 * string streams: provide next()/back() interface for string buffers
251 * Functions like charnext() require a next()/back() interface
252 * for reading input streams. In order to provide this interface
253 * for string buffers, the following functions can be used:
256 * charnext(c, sstr_next, sstr_back);
259 * The calls to sstr_push()/sstr_pop() may be nested.
261 static char *sstr_bufs
[NSSTR
]; /* buffer stack */
262 static int sstr_n
; /* numbers of items in sstr_bufs[] */
263 static char *sstr_s
; /* current buffer */
265 void sstr_push(char *s
)
267 sstr_bufs
[sstr_n
++] = sstr_s
;
274 sstr_s
= sstr_bufs
[--sstr_n
];
280 return *sstr_s
? (unsigned char) *sstr_s
++ : -1;
283 void sstr_back(int c
)