From e36fdb7ec1e74b93cf2956df6a47b84578c7b594 Mon Sep 17 00:00:00 2001 From: christos Date: Fri, 26 Sep 2014 01:21:07 +0000 Subject: [PATCH] add VIS_META/VIS_SHELL support to encode all shell metacharacters. XXX: /etc/rc.d/wizd fix $ --- include/vis.h | 4 +++- lib/libc/gen/vis.3 | 30 +++++++++++++++++++++++++++++- lib/libc/gen/vis.c | 24 ++++++++++++++---------- usr.bin/vis/vis.1 | 15 ++++++++++++--- usr.bin/vis/vis.c | 14 ++++++++++---- 5 files changed, 68 insertions(+), 19 deletions(-) diff --git a/include/vis.h b/include/vis.h index b4c6b5e1c6de..15e99b0e039b 100644 --- a/include/vis.h +++ b/include/vis.h @@ -1,4 +1,4 @@ -/* $NetBSD: vis.h,v 1.21 2013/02/20 17:01:15 christos Exp $ */ +/* $NetBSD: vis.h,v 1.22 2014/09/26 01:21:07 christos Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -63,6 +63,8 @@ #define VIS_NOESCAPE 0x0400 /* don't decode `\' */ #define _VIS_END 0x0800 /* for unvis */ #define VIS_GLOB 0x1000 /* encode glob(3) magic characters */ +#define VIS_SHELL 0x2000 /* encode shell special characters [not glob] */ +#define VIS_META (VIS_WHITE | VIS_GLOB | VIS_SHELL) /* * unvis return codes diff --git a/lib/libc/gen/vis.3 b/lib/libc/gen/vis.3 index 166316d1f9ae..bb30ca66d75e 100644 --- a/lib/libc/gen/vis.3 +++ b/lib/libc/gen/vis.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: vis.3,v 1.39 2013/02/20 20:05:26 christos Exp $ +.\" $NetBSD: vis.3,v 1.40 2014/09/26 01:21:07 christos Exp $ .\" .\" Copyright (c) 1989, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -244,6 +244,27 @@ and .Ql # ) recognized by .Xr glob 3 . +.It Dv VIS_SHELL +Also encode the meta characters used by shells (in addition to the glob +characters): +.Ql ( ' , +.Ql ` , +.Ql \*[q] , +.Ql \&; , +.Ql \*[Am] , +.Ql \*[Lt] , +.Ql \*[Gt] , +.Ql \&( , +.Ql \&) , +.Ql \&| , +.Ql \&] , +.Ql \&\\ , +.\" XXX: How do you print a $ +.Ql \e(Do , +.Ql \&! , +.Ql \&^ +and +.Ql ~ ). .It Dv VIS_SP Also encode space. .It Dv VIS_TAB @@ -257,6 +278,13 @@ Synonym for .Dv VIS_TAB \&| .Dv VIS_NL . +.It Dv VIS_META +Synonym for +.Dv VIS_WHITE +\&| +.Dv VIS_GLOB +\&| +.Dv VIS_SHELL . .It Dv VIS_SAFE Only encode .Dq unsafe diff --git a/lib/libc/gen/vis.c b/lib/libc/gen/vis.c index 4863bbea8596..5ebb83b2ff92 100644 --- a/lib/libc/gen/vis.c +++ b/lib/libc/gen/vis.c @@ -1,4 +1,4 @@ -/* $NetBSD: vis.c,v 1.62 2014/09/08 17:35:01 christos Exp $ */ +/* $NetBSD: vis.c,v 1.63 2014/09/26 01:21:07 christos Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -57,7 +57,7 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: vis.c,v 1.62 2014/09/08 17:35:01 christos Exp $"); +__RCSID("$NetBSD: vis.c,v 1.63 2014/09/26 01:21:07 christos Exp $"); #endif /* LIBC_SCCS and not lint */ #ifdef __FBSDID __FBSDID("$FreeBSD$"); @@ -104,7 +104,10 @@ static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *); #define xtoa(c) L"0123456789abcdef"[c] #define XTOA(c) L"0123456789ABCDEF"[c] -#define MAXEXTRAS 10 +#define MAXEXTRAS 30 + +static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~"; +static const wchar_t char_glob[] = L"*?[#"; #if !HAVE_NBTOOL_CONFIG_H #ifndef __NetBSD__ @@ -318,17 +321,18 @@ makeextralist(int flags, const char *src) if (mbstowcs(dst, src, len) == (size_t)-1) { size_t i; for (i = 0; i < len; i++) - dst[i] = (wint_t)(u_char)src[i]; + dst[i] = (wchar_t)(u_char)src[i]; d = dst + len; } else d = dst + wcslen(dst); - if (flags & VIS_GLOB) { - *d++ = L'*'; - *d++ = L'?'; - *d++ = L'['; - *d++ = L'#'; - } + if (flags & VIS_GLOB) + for (const wchar_t *s = char_glob; *s; *d++ = *s++) + continue; + + if (flags & VIS_SHELL) + for (const wchar_t *s = char_shell; *s; *d++ = *s++) + continue; if (flags & VIS_SP) *d++ = L' '; if (flags & VIS_TAB) *d++ = L'\t'; diff --git a/usr.bin/vis/vis.1 b/usr.bin/vis/vis.1 index fcaa7fbc39dd..3ea80e1e8591 100644 --- a/usr.bin/vis/vis.1 +++ b/usr.bin/vis/vis.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: vis.1,v 1.20 2013/10/29 12:27:23 njoly Exp $ +.\" $NetBSD: vis.1,v 1.21 2014/09/26 01:21:07 christos Exp $ .\" .\" Copyright (c) 1989, 1991, 1993, 1994 .\" The Regents of the University of California. All rights reserved. @@ -29,7 +29,7 @@ .\" .\" @(#)vis.1 8.4 (Berkeley) 4/19/94 .\" -.Dd February 19, 2013 +.Dd September 25, 2014 .Dt VIS 1 .Os .Sh NAME @@ -37,7 +37,7 @@ .Nd display non-printable characters in a visual format .Sh SYNOPSIS .Nm -.Op Fl bcfhlmnostw +.Op Fl bcfhlmMnosStw .Op Fl e Ar extra .Op Fl F Ar foldwidth .Op Ar file ... @@ -102,6 +102,12 @@ followed by the newline. .It Fl m Encode using the MIME Quoted-Printable encoding from RFC 2045. .Pq Dv VIS_MIMESTYLE +.It Fl M +Encode all shell meta characters (implies +.Fl S , +.Fl w , +.Fl g ) +.Pq Dv VIS_META .It Fl n Turns off any encoding, except for the fact that backslashes are still doubled and hidden newline sequences inserted if @@ -128,6 +134,9 @@ Only characters considered unsafe to send to a terminal are encoded. This flag allows backspace, bell, and carriage return in addition to the default space, tab and newline. .Pq Dv VIS_SAFE +.It Fl S +Encode shell meta-characters that are non-white space or glob. +.Pq Dv VIS_SHELL .It Fl t Tabs are also encoded. .Pq Dv VIS_TAB diff --git a/usr.bin/vis/vis.c b/usr.bin/vis/vis.c index 1509c81c4123..f359dbf962e9 100644 --- a/usr.bin/vis/vis.c +++ b/usr.bin/vis/vis.c @@ -1,4 +1,4 @@ -/* $NetBSD: vis.c,v 1.22 2013/02/20 17:04:45 christos Exp $ */ +/* $NetBSD: vis.c,v 1.23 2014/09/26 01:21:07 christos Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993\ #if 0 static char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 6/6/93"; #endif -__RCSID("$NetBSD: vis.c,v 1.22 2013/02/20 17:04:45 christos Exp $"); +__RCSID("$NetBSD: vis.c,v 1.23 2014/09/26 01:21:07 christos Exp $"); #endif /* not lint */ #include @@ -70,7 +70,7 @@ main(int argc, char *argv[]) int ch; int rval; - while ((ch = getopt(argc, argv, "bcde:F:fhlmnostw")) != -1) + while ((ch = getopt(argc, argv, "bcde:F:fhlmMnosStw")) != -1) switch((char)ch) { case 'b': eflags |= VIS_NOSLASH; @@ -107,6 +107,9 @@ main(int argc, char *argv[]) if (foldwidth == 80) foldwidth = 76; break; + case 'M': + eflags |= VIS_META; + break; case 'n': none++; break; @@ -116,6 +119,9 @@ main(int argc, char *argv[]) case 's': eflags |= VIS_SAFE; break; + case 'S': + eflags |= VIS_SHELL; + break; case 't': eflags |= VIS_TAB; break; @@ -125,7 +131,7 @@ main(int argc, char *argv[]) case '?': default: (void)fprintf(stderr, - "Usage: %s [-bcfhlmnostw] [-e extra]" + "Usage: %s [-bcfhlmMnosStw] [-e extra]" " [-F foldwidth] [file ...]\n", getprogname()); return 1; }