1 /* $NetBSD: sti.c,v 1.7 2008/04/28 20:24:17 martin Exp $ */
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: sti.c,v 1.7 2008/04/28 20:24:17 martin Exp $");
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
49 #define setprogname(a)
50 extern const char *__progname
;
51 #define getprogname() __progname
55 unescape(const char **pp
, int *state
)
60 while ((ch
= *(*pp
)++) != '\0') {
61 switch(unvis(&out
, ch
, state
, 0)) {
75 if (unvis(&out
, '\0', state
, UNVIS_END
) == UNVIS_VALID
)
78 switch ((ch
= *(*pp
)++)) {
85 switch (ch
= *(*pp
)++) {
86 case 'a': return '\a';
87 case 'b': return '\b';
88 case 'e': return '\e';
89 case 'f': return '\f';
90 case 't': return '\t';
91 case 'n': return '\n';
92 case 'r': return '\r';
93 case 'v': return '\v';
94 case '\\': return '\\';
96 case '0': case '1': case '2': case '3':
97 case '4': case '5': case '6': case '7':
99 if (ch
>= '0' && ch
< '8') {
100 out
= out
* 8 + ch
- '0';
102 if (ch
>= '0' && ch
< '8') {
103 out
= out
* 8 + ch
- '0';
105 if (ch
>= '0' && ch
< '8')
106 out
= out
* 8 + ch
- '0';
128 if (ioctl(fd
, TIOCSTI
, &ch
) == -1)
129 err(1, "Cannot simulate terminal input");
133 sendstr(int fd
, const char *str
)
136 const char *ptr
= str
;
138 while ((c
= unescape(&ptr
, &state
)) != -1)
141 if (c
== -1 && errno
!= ENODATA
)
142 warn("Cannot decode `%s'", str
);
146 main(int argc
, char *argv
[])
149 char ttydev
[MAXPATHLEN
];
155 (void)fprintf(stderr
, "Usage: %s <tty> [arg ...]\n",
166 if (strncmp(tty
, "/dev/", 5) == 0)
167 (void)snprintf(ttydev
, sizeof(ttydev
), "%s", tty
);
168 else if (strncmp(tty
, "tty", 3) == 0 || strncmp(tty
, "pty", 3) == 0 ||
169 strncmp(tty
, "pts/", 4) == 0)
170 (void)snprintf(ttydev
, sizeof(ttydev
), "/dev/%s", tty
);
171 else if (isdigit((unsigned char)*tty
))
172 (void)snprintf(ttydev
, sizeof(ttydev
), "/dev/pts/%s", tty
);
174 (void)snprintf(ttydev
, sizeof(ttydev
), "/dev/tty%s", tty
);
176 if ((fd
= open(ttydev
, O_RDWR
)) == -1)
177 err(1, "Cannot open `%s'", ttydev
);
182 line
= malloc(10240);
183 while (fgets(line
, 10240, stdin
) != NULL
) {
185 if ((p
= strrchr(line
, '\n')) != NULL
)
191 while ((line
= fparseln(stdin
, NULL
, NULL
, NULL
, 0)) != NULL
) {
197 for (; argc
--; argv
++) {