1 /* $NetBSD: sti.c,v 1.3 2005/11/10 18:04:03 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2005 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 #include <sys/cdefs.h> 39 __RCSID("$NetBSD: sti.c,v 1.3 2005/11/10 18:04:03 christos Exp $"); 40 41 #include <sys/param.h> 42 #include <sys/ioctl.h> 43 #include <ctype.h> 44 #include <stdio.h> 45 #include <string.h> 46 #include <stdlib.h> 47 #include <unistd.h> 48 #include <fcntl.h> 49 #include <err.h> 50 #include <vis.h> 51 #include <errno.h> 52 53 static int 54 unescape(const char **pp, int *state) 55 { 56 char ch, out; 57 58 while ((ch = *(*pp)++) != '\0') { 59 switch(unvis(&out, ch, state, 0)) { 60 case 0: 61 case UNVIS_NOCHAR: 62 break; 63 case UNVIS_VALID: 64 return out; 65 case UNVIS_VALIDPUSH: 66 (*pp)--; 67 return out; 68 case UNVIS_SYNBAD: 69 errno = EILSEQ; 70 return -1; 71 } 72 } 73 if (unvis(&out, '\0', state, UNVIS_END) == UNVIS_VALID) 74 return out; 75 errno = ENODATA; 76 return -1; 77 } 78 79 static void 80 sti(int fd, int c) 81 { 82 char ch = c; 83 84 if (ioctl(fd, TIOCSTI, &ch) == -1) 85 err(1, "Cannot simulate terminal input"); 86 } 87 88 int 89 main(int argc, char *argv[]) 90 { 91 const char *tty, *ptr; 92 char ttydev[MAXPATHLEN]; 93 int fd, c, state; 94 95 setprogname(*argv); 96 97 if (argc < 2) { 98 (void)fprintf(stderr, "Usage: %s tty arg ...\n", getprogname()); 99 return 1; 100 } 101 102 argc--; 103 argv++; 104 105 tty = *argv++; 106 argc--; 107 108 if (strncmp(tty, "/dev/", 5) == 0) 109 (void)snprintf(ttydev, sizeof(ttydev), "%s", tty); 110 else if (strncmp(tty, "tty", 3) == 0 || strncmp(tty, "pty", 3) == 0 || 111 strncmp(tty, "pts/", 4) == 0) 112 (void)snprintf(ttydev, sizeof(ttydev), "/dev/%s", tty); 113 else if (isdigit((unsigned char)*tty)) 114 (void)snprintf(ttydev, sizeof(ttydev), "/dev/pts/%s", tty); 115 else 116 (void)snprintf(ttydev, sizeof(ttydev), "/dev/tty%s", tty); 117 118 if ((fd = open(ttydev, O_RDWR)) == -1) 119 err(1, "Cannot open `%s'", ttydev); 120 121 for (; argc--; argv++) { 122 state = 0; 123 for (ptr = *argv; (c = unescape(&ptr, &state)) != -1;) 124 sti(fd, c); 125 if (c == -1 && errno != ENODATA) 126 warn("Cannot decode `%s'", *argv); 127 if (argc != 0) 128 sti(fd, ' '); 129 } 130 131 (void)close(fd); 132 return 0; 133 } 134