oksh-noxz

[fork] Portable OpenBSD ksh, based on the Public Domain Korn Shell (pdksh).
git clone https://noxz.tech/git/oksh-noxz.git
Log | Files | Tags

vis.c
1/*	$OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */
2/*-
3 * Copyright (c) 1989, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include "portable.h"
32
33#ifndef HAVE_STRAVIS
34
35#include <sys/types.h>
36#include <errno.h>
37#include <ctype.h>
38#include <limits.h>
39#include <string.h>
40#include <stdlib.h>
41
42#include "vis.h"
43
44#define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
45#define	isvisible(c,flag)						\
46	(((c) == '\\' || (flag & VIS_ALL) == 0) &&			\
47	(((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) &&		\
48	(((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') ||	\
49		(flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) ||	\
50	((flag & VIS_SP) == 0 && (c) == ' ') ||				\
51	((flag & VIS_TAB) == 0 && (c) == '\t') ||			\
52	((flag & VIS_NL) == 0 && (c) == '\n') ||			\
53	((flag & VIS_SAFE) && ((c) == '\b' ||				\
54		(c) == '\007' || (c) == '\r' ||				\
55		isgraph((u_char)(c))))))
56
57/*
58 * vis - visually encode characters
59 */
60static char *
61evis(char *dst, int c, int flag, int nextc)
62{
63	if (isvisible(c, flag)) {
64		if ((c == '"' && (flag & VIS_DQ) != 0) ||
65		    (c == '\\' && (flag & VIS_NOSLASH) == 0))
66			*dst++ = '\\';
67		*dst++ = c;
68		*dst = '\0';
69		return (dst);
70	}
71
72	if (flag & VIS_CSTYLE) {
73		switch(c) {
74		case '\n':
75			*dst++ = '\\';
76			*dst++ = 'n';
77			goto done;
78		case '\r':
79			*dst++ = '\\';
80			*dst++ = 'r';
81			goto done;
82		case '\b':
83			*dst++ = '\\';
84			*dst++ = 'b';
85			goto done;
86		case '\a':
87			*dst++ = '\\';
88			*dst++ = 'a';
89			goto done;
90		case '\v':
91			*dst++ = '\\';
92			*dst++ = 'v';
93			goto done;
94		case '\t':
95			*dst++ = '\\';
96			*dst++ = 't';
97			goto done;
98		case '\f':
99			*dst++ = '\\';
100			*dst++ = 'f';
101			goto done;
102		case ' ':
103			*dst++ = '\\';
104			*dst++ = 's';
105			goto done;
106		case '\0':
107			*dst++ = '\\';
108			*dst++ = '0';
109			if (isoctal(nextc)) {
110				*dst++ = '0';
111				*dst++ = '0';
112			}
113			goto done;
114		}
115	}
116	if (((c & 0177) == ' ') || (flag & VIS_OCTAL) ||
117	    ((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) {
118		*dst++ = '\\';
119		*dst++ = ((u_char)c >> 6 & 07) + '0';
120		*dst++ = ((u_char)c >> 3 & 07) + '0';
121		*dst++ = ((u_char)c & 07) + '0';
122		goto done;
123	}
124	if ((flag & VIS_NOSLASH) == 0)
125		*dst++ = '\\';
126	if (c & 0200) {
127		c &= 0177;
128		*dst++ = 'M';
129	}
130	if (iscntrl((u_char)c)) {
131		*dst++ = '^';
132		if (c == 0177)
133			*dst++ = '?';
134		else
135			*dst++ = c + '@';
136	} else {
137		*dst++ = '-';
138		*dst++ = c;
139	}
140done:
141	*dst = '\0';
142	return (dst);
143}
144
145/*
146 * strvis, strnvis, strvisx - visually encode characters from src into dst
147 *	
148 *	Dst must be 4 times the size of src to account for possible
149 *	expansion.  The length of dst, not including the trailing NULL,
150 *	is returned. 
151 *
152 *	Strnvis will write no more than siz-1 bytes (and will NULL terminate).
153 *	The number of bytes needed to fully encode the string is returned.
154 *
155 *	Strvisx encodes exactly len bytes from src into dst.
156 *	This is useful for encoding a block of data.
157 */
158static int
159estrvis(char *dst, const char *src, int flag)
160{
161	char c;
162	char *start;
163
164	for (start = dst; (c = *src);)
165		dst = evis(dst, c, flag, *++src);
166	*dst = '\0';
167	return (dst - start);
168}
169
170int
171stravis(char **outp, const char *src, int flag)
172{
173	char *buf;
174	int len, serrno;
175
176	buf = reallocarray(NULL, 4, strlen(src) + 1);
177	if (buf == NULL)
178		return -1;
179	len = estrvis(buf, src, flag);
180	serrno = errno;
181	*outp = realloc(buf, len + 1);
182	if (*outp == NULL) {
183		*outp = buf;
184		errno = serrno;
185	}
186	return (len);
187}
188
189#endif /* !HAVE_STRAVIS */