sti.c revision 1.4 1 /* $NetBSD: sti.c,v 1.4 2005/11/10 19:07: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.4 2005/11/10 19:07: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 < 1) {
98 (void)fprintf(stderr, "Usage: %s <tty> [arg ...]\n",
99 getprogname());
100 return 1;
101 }
102
103 argc--;
104 argv++;
105
106 tty = *argv++;
107 argc--;
108
109 if (strncmp(tty, "/dev/", 5) == 0)
110 (void)snprintf(ttydev, sizeof(ttydev), "%s", tty);
111 else if (strncmp(tty, "tty", 3) == 0 || strncmp(tty, "pty", 3) == 0 ||
112 strncmp(tty, "pts/", 4) == 0)
113 (void)snprintf(ttydev, sizeof(ttydev), "/dev/%s", tty);
114 else if (isdigit((unsigned char)*tty))
115 (void)snprintf(ttydev, sizeof(ttydev), "/dev/pts/%s", tty);
116 else
117 (void)snprintf(ttydev, sizeof(ttydev), "/dev/tty%s", tty);
118
119 if ((fd = open(ttydev, O_RDWR)) == -1)
120 err(1, "Cannot open `%s'", ttydev);
121
122 if (argc == 0) {
123 char *line;
124 while ((line = fparseln(stdin, NULL, NULL, NULL, 0)) != NULL) {
125 state = 0;
126 for (ptr = line; (c = unescape(&ptr, &state)) != -1;)
127 sti(fd, c);
128 if (c == -1 && errno != ENODATA)
129 warn("Cannot decode `%s'", line);
130 free(line);
131 }
132 return 0;
133 }
134 for (; argc--; argv++) {
135 state = 0;
136 for (ptr = *argv; (c = unescape(&ptr, &state)) != -1;)
137 sti(fd, c);
138 if (c == -1 && errno != ENODATA)
139 warn("Cannot decode `%s'", *argv);
140 if (argc != 0)
141 sti(fd, ' ');
142 }
143
144 (void)close(fd);
145 return 0;
146 }
147