summaryrefslogtreecommitdiff
path: root/openbsd-compat/vis.c
diff options
context:
space:
mode:
authorDarren Tucker <dtucker@zip.com.au>2016-06-03 16:03:44 +1000
committerDarren Tucker <dtucker@zip.com.au>2016-06-03 16:03:44 +1000
commitae9c0d4d5c581b3040d1f16b5c5f4b1cd1616743 (patch)
tree68c1f2de41a0a70cf85a7715a2da71eb278388a1 /openbsd-compat/vis.c
parente1d93705f8f48f519433d6ca9fc3d0abe92a1b77 (diff)
Update vis.h and vis.c from OpenBSD.
This will be needed for the upcoming utf8 changes.
Diffstat (limited to 'openbsd-compat/vis.c')
-rw-r--r--openbsd-compat/vis.c60
1 files changed, 46 insertions, 14 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;