From 2ac1e392b6b534b954a49904907b40edd7931755 Mon Sep 17 00:00:00 2001 From: christos Date: Thu, 10 Nov 2005 18:03:05 +0000 Subject: [PATCH] Use unvis(3) to parse the string. thanks to groo, soda, and numerous others for the hand-holding. --- usr.sbin/sti/sti.8 | 12 +++---- usr.sbin/sti/sti.c | 84 +++++++++++++++++----------------------------- 2 files changed, 37 insertions(+), 59 deletions(-) diff --git a/usr.sbin/sti/sti.8 b/usr.sbin/sti/sti.8 index 7334338276e9..ba78174db8f9 100644 --- a/usr.sbin/sti/sti.8 +++ b/usr.sbin/sti/sti.8 @@ -1,4 +1,4 @@ -.\" $NetBSD: sti.8,v 1.1 2005/11/10 16:54:05 christos Exp $ +.\" $NetBSD: sti.8,v 1.2 2005/11/10 18:03:05 christos Exp $ .\" .\" Copyright (c) 2005 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -57,13 +57,13 @@ This .Xr ioctl 2 is limited to the superuser. .Pp -Characters in the +The .Ar string -prefixed by ^ are interpreted as control characters. -Characters prefixed by \e are interpreted in the standard ``C'' Language -fashion. +is interpreted using +.Xr unvis 3 . .Sh SEE ALSO -.Xr ioctl 2 +.Xr ioctl 2 , +.Xr unvis 3 .Sh HISTORY The .Nm diff --git a/usr.sbin/sti/sti.c b/usr.sbin/sti/sti.c index a18d04785c88..d8669ef9f43d 100644 --- a/usr.sbin/sti/sti.c +++ b/usr.sbin/sti/sti.c @@ -1,4 +1,4 @@ -/* $NetBSD: sti.c,v 1.1 2005/11/10 16:54:05 christos Exp $ */ +/* $NetBSD: sti.c,v 1.2 2005/11/10 18:03:05 christos Exp $ */ /*- * Copyright (c) 2005 The NetBSD Foundation, Inc. @@ -36,7 +36,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ #include -__RCSID("$NetBSD: sti.c,v 1.1 2005/11/10 16:54:05 christos Exp $"); +__RCSID("$NetBSD: sti.c,v 1.2 2005/11/10 18:03:05 christos Exp $"); #include #include @@ -47,58 +47,33 @@ __RCSID("$NetBSD: sti.c,v 1.1 2005/11/10 16:54:05 christos Exp $"); #include #include #include - -#define isodigit(c) (isdigit(c) && ((c) < '8')) -#define nextc(a) (unsigned char)*((*a)++) - -#ifndef CTRL -#define CTRL(a) ((a)&037) -#endif +#include +#include static int -unescape(char **pp) +unescape(const char **pp, int *state) { - int c; + char ch, out; - switch (c = nextc(pp)) { - case '\\': - switch (c = nextc(pp)) { - case 'a': - return '\007'; /* Bell */ - case 'b': - return '\010'; /* Backspace */ - case 't': - return '\011'; /* Horizontal Tab */ - case 'n': - return '\012'; /* New Line */ - case 'v': - return '\013'; /* Vertical Tab */ - case 'f': - return '\014'; /* Form Feed */ - case 'r': - return '\015'; /* Carriage Return */ - case 'e': - return '\033'; /* Escape */ - default: - if (isodigit(c)) { - int x = c; - for (;;) { - c = nextc(pp); - if (!c || !isodigit(c)) { - --(*pp); - return x; - } - x <<= 3; - x |= c - '0'; - } - } - return c; + while ((ch = *(*pp)++) != '\0') { + switch(unvis(&out, ch, state, 0)) { + case 0: + case UNVIS_NOCHAR: + break; + case UNVIS_VALID: + return out; + case UNVIS_VALIDPUSH: + (*pp)--; + return out; + case UNVIS_SYNBAD: + errno = EILSEQ; + return -1; } - case '^': - return CTRL(nextc(pp)); - default: - return c; } + if (unvis(&out, '\0', state, UNVIS_END) == UNVIS_VALID) + return out; + errno = ENODATA; + return -1; } static void @@ -113,14 +88,14 @@ sti(int fd, int c) int main(int argc, char *argv[]) { - char *tty, *ptr; + const char *tty, *ptr; char ttydev[MAXPATHLEN]; - int fd, c; + int fd, c, state; setprogname(*argv); if (argc < 2) { - (void)fprintf(stderr, "Usage: %s tty arg ...", getprogname()); + (void)fprintf(stderr, "Usage: %s tty arg ...\n", getprogname()); return 1; } @@ -143,9 +118,12 @@ main(int argc, char *argv[]) if ((fd = open(ttydev, O_RDWR)) == -1) err(1, "Cannot open `%s'", ttydev); - while (argc--) { - for (ptr = *argv++; (c = unescape(&ptr)) != 0;) + for (; argc--; argv++) { + state = 0; + for (ptr = *argv; (c = unescape(&ptr, &state)) != -1;) sti(fd, c); + if (c == -1 && errno != ENODATA) + warnx("Cannot decode `%s'", *argv); if (argc != 0) sti(fd, ' '); }