| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: thunk.c,v 1.21 2011/08/28 19:37:16 reinoud Exp $ */ | | 1 | /* $NetBSD: thunk.c,v 1.22 2011/08/28 21:19:49 jmcneill Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2011 Jared D. McNeill <jmcneill@invisible.ca> | | 4 | * Copyright (c) 2011 Jared D. McNeill <jmcneill@invisible.ca> |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -17,39 +17,40 @@ | | | @@ -17,39 +17,40 @@ |
17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * POSSIBILITY OF SUCH DAMAGE. | | 26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | */ | | 27 | */ |
28 | | | 28 | |
29 | #include <sys/cdefs.h> | | 29 | #include <sys/cdefs.h> |
30 | __RCSID("$NetBSD: thunk.c,v 1.21 2011/08/28 19:37:16 reinoud Exp $"); | | 30 | __RCSID("$NetBSD: thunk.c,v 1.22 2011/08/28 21:19:49 jmcneill Exp $"); |
31 | | | 31 | |
32 | #include <sys/types.h> | | 32 | #include <sys/types.h> |
33 | #include <sys/ansi.h> | | 33 | #include <sys/ansi.h> |
34 | | | 34 | |
35 | #include <aio.h> | | 35 | #include <aio.h> |
36 | #include <assert.h> | | 36 | #include <assert.h> |
37 | #include <fcntl.h> | | 37 | #include <fcntl.h> |
38 | #include <stdarg.h> | | 38 | #include <stdarg.h> |
39 | #include <stdint.h> | | 39 | #include <stdint.h> |
40 | #include <stdio.h> | | 40 | #include <stdio.h> |
41 | #include <stdlib.h> | | 41 | #include <stdlib.h> |
42 | #include <signal.h> | | 42 | #include <signal.h> |
| | | 43 | #include <termios.h> |
43 | #include <time.h> | | 44 | #include <time.h> |
44 | #include <ucontext.h> | | 45 | #include <ucontext.h> |
45 | #include <unistd.h> | | 46 | #include <unistd.h> |
46 | | | 47 | |
47 | #include "../include/thunk.h" | | 48 | #include "../include/thunk.h" |
48 | | | 49 | |
49 | static void | | 50 | static void |
50 | thunk_to_timeval(const struct thunk_timeval *ttv, struct timeval *tv) | | 51 | thunk_to_timeval(const struct thunk_timeval *ttv, struct timeval *tv) |
51 | { | | 52 | { |
52 | tv->tv_sec = ttv->tv_sec; | | 53 | tv->tv_sec = ttv->tv_sec; |
53 | tv->tv_usec = ttv->tv_usec; | | 54 | tv->tv_usec = ttv->tv_usec; |
54 | } | | 55 | } |
55 | | | 56 | |
| @@ -64,26 +65,56 @@ static void | | | @@ -64,26 +65,56 @@ static void |
64 | thunk_to_itimerval(const struct thunk_itimerval *tit, struct itimerval *it) | | 65 | thunk_to_itimerval(const struct thunk_itimerval *tit, struct itimerval *it) |
65 | { | | 66 | { |
66 | thunk_to_timeval(&tit->it_interval, &it->it_interval); | | 67 | thunk_to_timeval(&tit->it_interval, &it->it_interval); |
67 | thunk_to_timeval(&tit->it_value, &it->it_value); | | 68 | thunk_to_timeval(&tit->it_value, &it->it_value); |
68 | } | | 69 | } |
69 | | | 70 | |
70 | static void | | 71 | static void |
71 | thunk_from_itimerval(const struct itimerval *it, struct thunk_itimerval *tit) | | 72 | thunk_from_itimerval(const struct itimerval *it, struct thunk_itimerval *tit) |
72 | { | | 73 | { |
73 | thunk_from_timeval(&it->it_interval, &tit->it_interval); | | 74 | thunk_from_timeval(&it->it_interval, &tit->it_interval); |
74 | thunk_from_timeval(&it->it_value, &tit->it_value); | | 75 | thunk_from_timeval(&it->it_value, &tit->it_value); |
75 | } | | 76 | } |
76 | | | 77 | |
| | | 78 | static void |
| | | 79 | thunk_to_termios(const struct thunk_termios *tt, struct termios *t) |
| | | 80 | { |
| | | 81 | int i; |
| | | 82 | |
| | | 83 | t->c_iflag = tt->c_iflag; |
| | | 84 | t->c_oflag = tt->c_oflag; |
| | | 85 | t->c_cflag = tt->c_cflag; |
| | | 86 | t->c_lflag = tt->c_lflag; |
| | | 87 | for (i = 0; i < __arraycount(t->c_cc); i++) |
| | | 88 | t->c_cc[i] = tt->c_cc[i]; |
| | | 89 | t->c_ispeed = tt->c_ispeed; |
| | | 90 | t->c_ospeed= tt->c_ospeed; |
| | | 91 | } |
| | | 92 | |
| | | 93 | static void |
| | | 94 | thunk_from_termios(const struct termios *t, struct thunk_termios *tt) |
| | | 95 | { |
| | | 96 | int i; |
| | | 97 | |
| | | 98 | tt->c_iflag = t->c_iflag; |
| | | 99 | tt->c_oflag = t->c_oflag; |
| | | 100 | tt->c_cflag = t->c_cflag; |
| | | 101 | tt->c_lflag = t->c_lflag; |
| | | 102 | for (i = 0; i < __arraycount(tt->c_cc); i++) |
| | | 103 | tt->c_cc[i] = t->c_cc[i]; |
| | | 104 | tt->c_ispeed = t->c_ispeed; |
| | | 105 | tt->c_ospeed= t->c_ospeed; |
| | | 106 | } |
| | | 107 | |
77 | int | | 108 | int |
78 | thunk_setitimer(int which, const struct thunk_itimerval *value, | | 109 | thunk_setitimer(int which, const struct thunk_itimerval *value, |
79 | struct thunk_itimerval *ovalue) | | 110 | struct thunk_itimerval *ovalue) |
80 | { | | 111 | { |
81 | struct itimerval it, oit; | | 112 | struct itimerval it, oit; |
82 | int error; | | 113 | int error; |
83 | | | 114 | |
84 | thunk_to_itimerval(value, &it); | | 115 | thunk_to_itimerval(value, &it); |
85 | error = setitimer(which, &it, &oit); | | 116 | error = setitimer(which, &it, &oit); |
86 | if (error) | | 117 | if (error) |
87 | return error; | | 118 | return error; |
88 | if (ovalue) | | 119 | if (ovalue) |
89 | thunk_from_itimerval(&oit, ovalue); | | 120 | thunk_from_itimerval(&oit, ovalue); |
| @@ -176,26 +207,48 @@ thunk_makecontext(ucontext_t *ucp, void | | | @@ -176,26 +207,48 @@ thunk_makecontext(ucontext_t *ucp, void |
176 | void | | 207 | void |
177 | thunk_makecontext_trapframe2go(ucontext_t *ucp, void *func, void *trapframe) | | 208 | thunk_makecontext_trapframe2go(ucontext_t *ucp, void *func, void *trapframe) |
178 | { | | 209 | { |
179 | makecontext(ucp, func, 1, trapframe); | | 210 | makecontext(ucp, func, 1, trapframe); |
180 | } | | 211 | } |
181 | | | 212 | |
182 | int | | 213 | int |
183 | thunk_swapcontext(ucontext_t *oucp, ucontext_t *ucp) | | 214 | thunk_swapcontext(ucontext_t *oucp, ucontext_t *ucp) |
184 | { | | 215 | { |
185 | return swapcontext(oucp, ucp); | | 216 | return swapcontext(oucp, ucp); |
186 | } | | 217 | } |
187 | | | 218 | |
188 | int | | 219 | int |
| | | 220 | thunk_tcgetattr(int fd, struct thunk_termios *tt) |
| | | 221 | { |
| | | 222 | struct termios t; |
| | | 223 | int error; |
| | | 224 | |
| | | 225 | error = tcgetattr(fd, &t); |
| | | 226 | if (error) |
| | | 227 | return error; |
| | | 228 | thunk_from_termios(&t, tt); |
| | | 229 | return 0; |
| | | 230 | } |
| | | 231 | |
| | | 232 | int |
| | | 233 | thunk_tcsetattr(int fd, int action, const struct thunk_termios *tt) |
| | | 234 | { |
| | | 235 | struct termios t; |
| | | 236 | |
| | | 237 | thunk_to_termios(tt, &t); |
| | | 238 | return tcsetattr(fd, action, &t); |
| | | 239 | } |
| | | 240 | |
| | | 241 | int |
189 | thunk_getchar(void) | | 242 | thunk_getchar(void) |
190 | { | | 243 | { |
191 | return getchar(); | | 244 | return getchar(); |
192 | } | | 245 | } |
193 | | | 246 | |
194 | void | | 247 | void |
195 | thunk_putchar(int c) | | 248 | thunk_putchar(int c) |
196 | { | | 249 | { |
197 | char wc = (char) c; | | 250 | char wc = (char) c; |
198 | write(1, &wc, 1); | | 251 | write(1, &wc, 1); |
199 | } | | 252 | } |
200 | | | 253 | |
201 | int | | 254 | int |