comparison loadtools/ttypassthru.c @ 7:aa1f6fe16fef

loadtools building blocks started
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 30 Apr 2013 07:19:48 +0000
parents
children e2e80a09338e
comparison
equal deleted inserted replaced
6:5eaafa83be60 7:aa1f6fe16fef
1 /*
2 * This module implements the pass-thru operation mode, in which
3 * the Unix host tty is cross-connected directly to the target
4 * running some code we have just loaded.
5 */
6
7 #include <sys/types.h>
8 #include <sys/ioctl.h>
9 #include <sys/errno.h>
10 #include <termios.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <strings.h>
15
16 extern int errno;
17
18 extern int target_fd;
19
20 static struct termios saved_termios, my_termios;
21
22 static void
23 loop()
24 {
25 char buf[BUFSIZ];
26 fd_set fds, fds1;
27 register int i, cc, max;
28
29 FD_ZERO(&fds);
30 FD_SET(0, &fds);
31 FD_SET(target_fd, &fds);
32 max = target_fd + 1;
33 for (;;) {
34 bcopy(&fds, &fds1, sizeof(fd_set));
35 i = select(max, &fds1, NULL, NULL, NULL);
36 if (i < 0) {
37 if (errno == EINTR)
38 continue;
39 tcsetattr(0, TCSAFLUSH, &saved_termios);
40 perror("select");
41 exit(1);
42 }
43 if (FD_ISSET(0, &fds1)) {
44 cc = read(0, buf, sizeof buf);
45 if (cc <= 0)
46 return;
47 if (cc == 1 && buf[0] == 0x1C)
48 return;
49 write(target_fd, buf, cc);
50 }
51 if (FD_ISSET(target_fd, &fds1)) {
52 cc = read(target_fd, buf, sizeof buf);
53 if (cc <= 0) {
54 tcsetattr(0, TCSAFLUSH, &saved_termios);
55 fprintf(stderr, "EOF/error on target tty\n");
56 exit(1);
57 }
58 write(1, buf, cc);
59 }
60 }
61 }
62
63 tty_passthru()
64 {
65 static int zero = 0;
66
67 ioctl(target_fd, FIONBIO, &zero);
68
69 tcgetattr(0, &saved_termios);
70 bcopy(&saved_termios, &my_termios, sizeof(struct termios));
71 cfmakeraw(&my_termios);
72 my_termios.c_cc[VMIN] = 1;
73 my_termios.c_cc[VTIME] = 0;
74 tcsetattr(0, TCSAFLUSH, &my_termios);
75
76 printf("Entering tty pass-thru; type ^\\ to exit\r\n");
77 loop();
78 tcsetattr(0, TCSAFLUSH, &saved_termios);
79 return 0;
80 }