158 lines
3.6 KiB
C
158 lines
3.6 KiB
C
/*-
|
|
* Copyright (c) 1989 The Regents of the University of California.
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms are permitted provided
|
|
* that: (1) source distributions retain this entire copyright notice and
|
|
* comment, and (2) distributions including binaries display the following
|
|
* acknowledgement: ``This product includes software developed by the
|
|
* University of California, Berkeley and its contributors'' in the
|
|
* documentation or other materials provided with the distribution and in
|
|
* all advertising materials mentioning features or use of this software.
|
|
* Neither the name of the University nor the names of its contributors may
|
|
* be used to endorse or promote products derived from this software without
|
|
* specific prior written permission.
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
|
|
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
*/
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
|
static char sccsid[] = "@(#)vis.c 5.3 (Berkeley) 6/26/90";
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
#define _KERNEL
|
|
#include "ixemul.h"
|
|
|
|
#include <ctype.h>
|
|
#include <vis.h>
|
|
|
|
#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
|
|
|
|
/*
|
|
* vis - visually encode characters
|
|
*/
|
|
char *
|
|
vis(char *dst, char c, int flag, char nextc)
|
|
{
|
|
if ((isascii(c) && isgraph(c)) ||
|
|
((flag & VIS_SP) == 0 && c == ' ') ||
|
|
((flag & VIS_TAB) == 0 && c == '\t') ||
|
|
((flag & VIS_NL) == 0 && c == '\n') ||
|
|
((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) {
|
|
*dst++ = c;
|
|
if (c == '\\' && (flag & VIS_NOSLASH) == 0)
|
|
*dst++ = '\\';
|
|
*dst = '\0';
|
|
return (dst);
|
|
}
|
|
|
|
if (flag & VIS_CSTYLE) {
|
|
switch(c) {
|
|
case '\n':
|
|
*dst++ = '\\';
|
|
*dst++ = 'n';
|
|
goto done;
|
|
case '\r':
|
|
*dst++ = '\\';
|
|
*dst++ = 'r';
|
|
goto done;
|
|
case '\b':
|
|
*dst++ = '\\';
|
|
*dst++ = 'b';
|
|
goto done;
|
|
case '\a':
|
|
*dst++ = '\\';
|
|
*dst++ = 'a';
|
|
goto done;
|
|
case '\v':
|
|
*dst++ = '\\';
|
|
*dst++ = 'v';
|
|
goto done;
|
|
case '\t':
|
|
*dst++ = '\\';
|
|
*dst++ = 't';
|
|
goto done;
|
|
case '\f':
|
|
*dst++ = '\\';
|
|
*dst++ = 'f';
|
|
goto done;
|
|
case ' ':
|
|
*dst++ = '\\';
|
|
*dst++ = 's';
|
|
goto done;
|
|
case '\0':
|
|
*dst++ = '\\';
|
|
*dst++ = '0';
|
|
if (isoctal(nextc)) {
|
|
*dst++ = '0';
|
|
*dst++ = '0';
|
|
}
|
|
goto done;
|
|
}
|
|
}
|
|
if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
|
|
*dst++ = '\\';
|
|
*dst++ = ((u_char)c >> 6 & 07) + '0';
|
|
*dst++ = ((u_char)c >> 3 & 07) + '0';
|
|
*dst++ = ((u_char)c & 07) + '0';
|
|
goto done;
|
|
}
|
|
if ((flag & VIS_NOSLASH) == 0)
|
|
*dst++ = '\\';
|
|
if (c & 0200) {
|
|
c &= 0177;
|
|
*dst++ = 'M';
|
|
}
|
|
if (iscntrl(c)) {
|
|
*dst++ = '^';
|
|
if (c == 0177)
|
|
*dst++ = '?';
|
|
else
|
|
*dst++ = c + '@';
|
|
} else {
|
|
*dst++ = '-';
|
|
*dst++ = c;
|
|
}
|
|
done:
|
|
*dst = '\0';
|
|
return (dst);
|
|
}
|
|
|
|
/*
|
|
* strvis, strvisx - visually encode characters from src into dst
|
|
*
|
|
* Dst must be 4 times the size of src to account for possible
|
|
* expansion. The length of dst, not including the trailing NULL,
|
|
* is returned.
|
|
*
|
|
* Strvisx encodes exactly len bytes from src into dst.
|
|
* This is useful for encoding a block of data.
|
|
*/
|
|
int
|
|
strvis(char *dst, const char *src, int flag)
|
|
{
|
|
register char c;
|
|
char *start = dst;
|
|
|
|
for (; (c = *src); src++)
|
|
dst = vis(dst, c, flag, *(src+1));
|
|
|
|
return (dst - start);
|
|
}
|
|
|
|
int
|
|
strvisx(char *dst, const char *src, size_t len, int flag)
|
|
{
|
|
char *start = dst;
|
|
|
|
while (len > 1) {
|
|
dst = vis(dst, *src, flag, *(src+1));
|
|
len--;
|
|
}
|
|
if (len)
|
|
dst = vis(dst, *src, flag, '\0');
|
|
|
|
return (dst - start);
|
|
}
|