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