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