60 #if defined(LIBC_SCCS) && !defined(lint)
65 #include "namespace.h"
68 #include <sys/types.h>
80 __weak_alias(strvisx,_strvisx)
83 #if !HAVE_VIS || !HAVE_SVIS
89 static char *do_svis(
char *,
size_t *,
int,
int,
int,
const char *);
95 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
97 #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
98 #define issafe(c) (c == '\b' || c == BELL || c == '\r')
99 #define xtoa(c) "0123456789abcdef"[c]
100 #define XTOA(c) "0123456789ABCDEF"[c]
104 #define MAKEEXTRALIST(flag, extra, orig_str) \
106 const char *orig = orig_str; \
107 const char *o = orig; \
111 extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \
113 for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
116 if (flag & VIS_SP) *e++ = ' '; \
117 if (flag & VIS_TAB) *e++ = '\t'; \
118 if (flag & VIS_NL) *e++ = '\n'; \
119 if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
127 do_hvis(
char *dst,
size_t *dlen,
int c,
int flag,
int nextc,
const char *
extra)
130 if ((isascii(c) && isalnum(c))
132 || c ==
'$' || c ==
'-' || c ==
'_' || c ==
'.' || c ==
'+'
134 || c ==
'!' || c ==
'*' || c ==
'\'' || c ==
'(' || c ==
')'
136 dst = do_svis(dst, dlen, c, flag, nextc, extra);
144 *dst++ = xtoa(((
unsigned int)c >> 4) & 0xf);
145 *dst++ = xtoa((
unsigned int)c & 0xf);
156 do_mvis(
char *dst,
size_t *dlen,
int c,
int flag,
int nextc,
const char *extra)
160 ((isspace(c) && (nextc ==
'\r' || nextc ==
'\n')) ||
162 (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
164 strchr(
"#$@[\\]^`{|}~", c) != NULL)) {
171 *dst++ = XTOA(((
unsigned int)c >> 4) & 0xf);
172 *dst++ = XTOA((
unsigned int)c & 0xf);
174 dst = do_svis(dst, dlen, c, flag, nextc, extra);
189 do_svis(
char *dst,
size_t *dlen,
int c,
int flag,
int nextc,
const char *extra)
192 size_t odlen = dlen ? *dlen : 0;
194 isextra = strchr(extra, c) != NULL;
203 if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||
204 ((flag & VIS_SAFE) && issafe(c)))) {
209 if (flag & VIS_CSTYLE) {
213 *dst++ =
'\\'; *dst++ =
'n';
216 *dst++ =
'\\'; *dst++ =
'r';
219 *dst++ =
'\\'; *dst++ =
'b';
222 *dst++ =
'\\'; *dst++ =
'a';
225 *dst++ =
'\\'; *dst++ =
'v';
228 *dst++ =
'\\'; *dst++ =
't';
231 *dst++ =
'\\'; *dst++ =
'f';
234 *dst++ =
'\\'; *dst++ =
's';
237 *dst++ =
'\\'; *dst++ =
'0';
238 if (isoctal(nextc)) {
246 *dst++ =
'\\'; *dst++ = c;
253 if (isextra || ((c & 0177) ==
' ') || (flag & VIS_OCTAL)) {
256 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) +
'0';
257 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) +
'0';
258 *dst++ = (c & 07) +
'0';
260 if ((flag & VIS_NOSLASH) == 0) {
267 c &= 0177; *dst++ =
'M';
279 *dst++ =
'-'; *dst++ = c;
288 typedef char *(*visfun_t)(
char *,
size_t *, int, int, int,
const char *);
296 if (flag & VIS_HTTPSTYLE)
298 if (flag & VIS_MIMESTYLE)
308 isnvis(
char *dst,
size_t *dlen,
int c,
int flag,
int nextc,
const char *extra)
313 _DIAGASSERT(dst != NULL);
314 _DIAGASSERT(extra != NULL);
315 MAKEEXTRALIST(flag, nextra, extra);
317 if (dlen && *dlen == 0) {
325 dst = (*f)(dst, dlen, c, flag, nextc, nextra);
327 if (dst == NULL || (dlen && *dlen == 0)) {
336 svis(
char *dst,
int c,
int flag,
int nextc,
const char *extra)
338 return isnvis(dst, NULL, c, flag, nextc, extra);
342 snvis(
char *dst,
size_t dlen,
int c,
int flag,
int nextc,
const char *extra)
344 return isnvis(dst, &dlen, c, flag, nextc, extra);
364 istrsnvis(
char *dst,
size_t *dlen,
const char *csrc,
int flag,
const char *extra)
369 const unsigned char *src = (
const unsigned char *)csrc;
372 _DIAGASSERT(dst != NULL);
373 _DIAGASSERT(src != NULL);
374 _DIAGASSERT(extra != NULL);
375 MAKEEXTRALIST(flag, nextra, extra);
381 for (start = dst; (c = *src++) !=
'\0'; ) {
382 dst = (*f)(dst, dlen, c, flag, *src, nextra);
389 if (dlen && *dlen == 0) {
394 return (
int)(dst - start);
398 strsvis(
char *dst,
const char *csrc,
int flag,
const char *extra)
400 return istrsnvis(dst, NULL, csrc, flag, extra);
404 strsnvis(
char *dst,
size_t dlen,
const char *csrc,
int flag,
const char *extra)
406 return istrsnvis(dst, &dlen, csrc, flag, extra);
410 istrsnvisx(
char *dst,
size_t *dlen,
const char *csrc,
size_t len,
int flag,
416 const unsigned char *src = (
const unsigned char *)csrc;
419 _DIAGASSERT(dst != NULL);
420 _DIAGASSERT(src != NULL);
421 _DIAGASSERT(extra != NULL);
422 MAKEEXTRALIST(flag, nextra, extra);
424 if (dlen && *dlen == 0) {
433 for (start = dst; len > 0; len--) {
435 dst = (*f)(dst, dlen, c, flag, len > 1 ? *src :
'\0', nextra);
442 if (dlen && *dlen == 0) {
447 return (
int)(dst - start);
451 strsvisx(
char *dst,
const char *csrc,
size_t len,
int flag,
const char *extra)
453 return istrsnvisx(dst, NULL, csrc, len, flag, extra);
457 strsnvisx(
char *dst,
size_t dlen,
const char *csrc,
size_t len,
int flag,
460 return istrsnvisx(dst, &dlen, csrc, len, flag, extra);
469 invis(
char *dst,
size_t *dlen,
int c,
int flag,
int nextc)
472 unsigned char uc = (
unsigned char)c;
475 _DIAGASSERT(dst != NULL);
477 MAKEEXTRALIST(flag, extra,
"");
479 if (dlen && *dlen == 0) {
487 dst = (*f)(dst, dlen, uc, flag, nextc, extra);
489 if (dst == NULL || (dlen && *dlen == 0)) {
498 vis(
char *dst,
int c,
int flag,
int nextc)
500 return invis(dst, NULL, c, flag, nextc);
504 nvis(
char *dst,
size_t dlen,
int c,
int flag,
int nextc)
506 return invis(dst, &dlen, c, flag, nextc);
521 istrnvis(
char *dst,
size_t *dlen,
const char *src,
int flag)
526 MAKEEXTRALIST(flag, extra,
"");
528 if (dlen && *dlen == 0) {
535 rv = istrsnvis(dst, dlen, src, flag, extra);
541 strvis(
char *dst,
const char *src,
int flag)
543 return istrnvis(dst, NULL, src, flag);
547 strnvis(
char *dst,
size_t dlen,
const char *src,
int flag)
549 return istrnvis(dst, &dlen, src, flag);
553 istrnvisx(
char *dst,
size_t *dlen,
const char *src,
size_t len,
int flag)
558 MAKEEXTRALIST(flag, extra,
"");
560 if (dlen && *dlen == 0) {
567 rv = istrsnvisx(dst, dlen, src, len, flag, extra);
573 strvisx(
char *dst,
const char *src,
size_t len,
int flag)
575 return istrnvisx(dst, NULL, src, len, flag);
579 strnvisx(
char *dst,
size_t dlen,
const char *src,
size_t len,
int flag)
581 return istrnvisx(dst, &dlen, src, len, flag);