summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--openbsd-compat/vis.c60
-rw-r--r--openbsd-compat/vis.h5
2 files changed, 50 insertions, 15 deletions
diff --git a/openbsd-compat/vis.c b/openbsd-compat/vis.c
index f6f5665c1..3cef6bafd 100644
--- a/openbsd-compat/vis.c
+++ b/openbsd-compat/vis.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: vis.c,v 1.19 2005/09/01 17:15:49 millert Exp $ */ 1/* $OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */
2/*- 2/*-
3 * Copyright (c) 1989, 1993 3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved. 4 * The Regents of the University of California. All rights reserved.
@@ -33,13 +33,24 @@
33#include "includes.h" 33#include "includes.h"
34#if !defined(HAVE_STRNVIS) || defined(BROKEN_STRNVIS) 34#if !defined(HAVE_STRNVIS) || defined(BROKEN_STRNVIS)
35 35
36/*
37 * We want these to override in the BROKEN_STRNVIS case. TO avoid future sync
38 * problems no-op out the weak symbol definition rather than remove it.
39 */
40#define DEF_WEAK(x)
41
42#include <sys/types.h>
43#include <errno.h>
36#include <ctype.h> 44#include <ctype.h>
45#include <limits.h>
37#include <string.h> 46#include <string.h>
47#include <stdlib.h>
38 48
39#include "vis.h" 49#include "vis.h"
40 50
41#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') 51#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
42#define isvisible(c) \ 52#define isvisible(c,flag) \
53 (((c) == '\\' || (flag & VIS_ALL) == 0) && \
43 (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \ 54 (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \
44 (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \ 55 (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \
45 (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \ 56 (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \
@@ -48,7 +59,7 @@
48 ((flag & VIS_NL) == 0 && (c) == '\n') || \ 59 ((flag & VIS_NL) == 0 && (c) == '\n') || \
49 ((flag & VIS_SAFE) && ((c) == '\b' || \ 60 ((flag & VIS_SAFE) && ((c) == '\b' || \
50 (c) == '\007' || (c) == '\r' || \ 61 (c) == '\007' || (c) == '\r' || \
51 isgraph((u_char)(c))))) 62 isgraph((u_char)(c))))))
52 63
53/* 64/*
54 * vis - visually encode characters 65 * vis - visually encode characters
@@ -56,10 +67,11 @@
56char * 67char *
57vis(char *dst, int c, int flag, int nextc) 68vis(char *dst, int c, int flag, int nextc)
58{ 69{
59 if (isvisible(c)) { 70 if (isvisible(c, flag)) {
60 *dst++ = c; 71 if ((c == '"' && (flag & VIS_DQ) != 0) ||
61 if (c == '\\' && (flag & VIS_NOSLASH) == 0) 72 (c == '\\' && (flag & VIS_NOSLASH) == 0))
62 *dst++ = '\\'; 73 *dst++ = '\\';
74 *dst++ = c;
63 *dst = '\0'; 75 *dst = '\0';
64 return (dst); 76 return (dst);
65 } 77 }
@@ -136,6 +148,7 @@ done:
136 *dst = '\0'; 148 *dst = '\0';
137 return (dst); 149 return (dst);
138} 150}
151DEF_WEAK(vis);
139 152
140/* 153/*
141 * strvis, strnvis, strvisx - visually encode characters from src into dst 154 * strvis, strnvis, strvisx - visually encode characters from src into dst
@@ -161,6 +174,7 @@ strvis(char *dst, const char *src, int flag)
161 *dst = '\0'; 174 *dst = '\0';
162 return (dst - start); 175 return (dst - start);
163} 176}
177DEF_WEAK(strvis);
164 178
165int 179int
166strnvis(char *dst, const char *src, size_t siz, int flag) 180strnvis(char *dst, const char *src, size_t siz, int flag)
@@ -171,19 +185,18 @@ strnvis(char *dst, const char *src, size_t siz, int flag)
171 185
172 i = 0; 186 i = 0;
173 for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) { 187 for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
174 if (isvisible(c)) { 188 if (isvisible(c, flag)) {
175 i = 1; 189 if ((c == '"' && (flag & VIS_DQ) != 0) ||
176 *dst++ = c; 190 (c == '\\' && (flag & VIS_NOSLASH) == 0)) {
177 if (c == '\\' && (flag & VIS_NOSLASH) == 0) {
178 /* need space for the extra '\\' */ 191 /* need space for the extra '\\' */
179 if (dst < end) 192 if (dst + 1 >= end) {
180 *dst++ = '\\';
181 else {
182 dst--;
183 i = 2; 193 i = 2;
184 break; 194 break;
185 } 195 }
196 *dst++ = '\\';
186 } 197 }
198 i = 1;
199 *dst++ = c;
187 src++; 200 src++;
188 } else { 201 } else {
189 i = vis(tbuf, c, flag, *++src) - tbuf; 202 i = vis(tbuf, c, flag, *++src) - tbuf;
@@ -207,6 +220,25 @@ strnvis(char *dst, const char *src, size_t siz, int flag)
207} 220}
208 221
209int 222int
223stravis(char **outp, const char *src, int flag)
224{
225 char *buf;
226 int len, serrno;
227
228 buf = reallocarray(NULL, 4, strlen(src) + 1);
229 if (buf == NULL)
230 return -1;
231 len = strvis(buf, src, flag);
232 serrno = errno;
233 *outp = realloc(buf, len + 1);
234 if (*outp == NULL) {
235 *outp = buf;
236 errno = serrno;
237 }
238 return (len);
239}
240
241int
210strvisx(char *dst, const char *src, size_t len, int flag) 242strvisx(char *dst, const char *src, size_t len, int flag)
211{ 243{
212 char c; 244 char c;
diff --git a/openbsd-compat/vis.h b/openbsd-compat/vis.h
index d1286c99d..2cdfd364b 100644
--- a/openbsd-compat/vis.h
+++ b/openbsd-compat/vis.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: vis.h,v 1.11 2005/08/09 19:38:31 millert Exp $ */ 1/* $OpenBSD: vis.h,v 1.15 2015/07/20 01:52:27 millert Exp $ */
2/* $NetBSD: vis.h,v 1.4 1994/10/26 00:56:41 cgd Exp $ */ 2/* $NetBSD: vis.h,v 1.4 1994/10/26 00:56:41 cgd Exp $ */
3 3
4/*- 4/*-
@@ -58,6 +58,8 @@
58#define VIS_NL 0x10 /* also encode newline */ 58#define VIS_NL 0x10 /* also encode newline */
59#define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL) 59#define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL)
60#define VIS_SAFE 0x20 /* only encode "unsafe" characters */ 60#define VIS_SAFE 0x20 /* only encode "unsafe" characters */
61#define VIS_DQ 0x200 /* backslash-escape double quotes */
62#define VIS_ALL 0x400 /* encode all characters */
61 63
62/* 64/*
63 * other 65 * other
@@ -81,6 +83,7 @@
81 83
82char *vis(char *, int, int, int); 84char *vis(char *, int, int, int);
83int strvis(char *, const char *, int); 85int strvis(char *, const char *, int);
86int stravis(char **, const char *, int);
84int strnvis(char *, const char *, size_t, int) 87int strnvis(char *, const char *, size_t, int)
85 __attribute__ ((__bounded__(__string__,1,3))); 88 __attribute__ ((__bounded__(__string__,1,3)));
86int strvisx(char *, const char *, size_t, int) 89int strvisx(char *, const char *, size_t, int)