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