FreeCalypso > hg > freecalypso-sw
annotate loadtools/tpinterf.c @ 27:ae6294b8a015
loadtool: exit jump0 implemented
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Sat, 04 May 2013 06:22:09 +0000 |
parents | aca1948e9713 |
children | 05af070c4b60 |
rev | line source |
---|---|
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * Target program interface - this module provides some primitives |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 * for communicating programmatically with loadagent and possibly |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 * other target-utils. |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 #include <sys/types.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <sys/time.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 #include <sys/errno.h> |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
10 #include <stdio.h> |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 #include <string.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 #include <strings.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 #include <stdlib.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 extern int errno; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 extern int target_fd; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 /* definition matches ../target-utils/libcommon/cmdentry.c */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 #define MAXCMD 527 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 /* |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 * static buffer between tpinterf_make_cmd and tpinterf_send_cmd |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 * |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 * We store the command with an ending \r\n so we can use it for |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 * matching the received echo as well, hence the sizing of the |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 * buffer. |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 static char cmdbuf[MAXCMD+2]; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 static int cmdlen; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 static int |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 arg_chars_valid(arg) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 char *arg; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 char *cp; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 for (cp = arg; *cp; cp++) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 if (*cp < ' ' || *cp > '~') |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 return(0); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 return(1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
44 /* |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
45 * This function takes a command for the target in argv form and |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 * converts it to a space-separated continuous string which can be |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 * passed as tty "keyboard" input to the target, enforcing length |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 * and character validity limits in the process. The output is |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 * stored in an internal static buffer for subsequent |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 * tpinterf_send_cmd(). |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 * |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 * Return value: 0 if everything OK, or -1 if some constraint is |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 * violated. |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 tpinterf_make_cmd(argv) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 char **argv; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 int arglen; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 char **ap, *dp; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 dp = cmdbuf; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 cmdlen = 0; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 for (ap = argv; *ap; ap++) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 arglen = strlen(*ap); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 if (ap != argv) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 arglen++; /* separating space */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 if (arglen > MAXCMD - cmdlen) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 return(-1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 if (!arg_chars_valid(*ap)) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 return(-1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 if (ap != argv) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 *dp++ = ' '; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 strcpy(dp, *ap); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 dp += strlen(*ap); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 cmdlen += arglen; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 *dp++ = '\r'; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 *dp = '\n'; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 return(0); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 /* |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 * This function sends the previously-constructed command to the target, |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 * and collects the expected response. |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 * |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 * Return value: 0 if successful, -1 on errors (timeout or wrong response) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
87 */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
88 tpinterf_send_cmd() |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
89 { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
90 char echobuf[MAXCMD+2]; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
91 fd_set fds; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
92 struct timeval tv; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
93 int rcvd, cc; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
94 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
95 write(target_fd, cmdbuf, cmdlen + 1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
96 for (rcvd = 0; rcvd < cmdlen + 2; ) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
97 FD_ZERO(&fds); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
98 FD_SET(target_fd, &fds); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
99 tv.tv_sec = 1; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
100 tv.tv_usec = 0; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
101 cc = select(target_fd+1, &fds, NULL, NULL, &tv); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
102 if (cc < 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
103 if (errno == EINTR) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
104 continue; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
105 perror("select"); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
106 return(-1); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
107 } |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
108 if (cc < 1) { |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
109 fprintf(stderr, |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
110 "error: timeout waiting for command echo\n"); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
111 return(-1); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
112 } |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
113 cc = read(target_fd, echobuf + rcvd, cmdlen + 2 - rcvd); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
114 if (cc <= 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
115 perror("read after successful select"); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
116 return(-1); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
117 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
118 rcvd += cc; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
119 } |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
120 if (bcmp(echobuf, cmdbuf, cmdlen + 2)) { |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
121 fprintf(stderr, "error: command echo mismatch\n"); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
122 return(-1); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
123 } else |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
124 return(0); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
125 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
126 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
127 /* |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
128 * This functions reads the serial output from the target until a |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
129 * '=' prompt is received. All intermediate output is passed to |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
130 * stdout. |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
131 * |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
132 * Return value: 0 if '=' prompt received immediately, |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
133 * positive if some scribble came before the prompt, -1 on errors |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
134 * (timeout, read errors, etc). |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
135 */ |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
136 tpinterf_pass_output(timeout) |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
137 { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
138 char buf[512], *cp; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
139 fd_set fds; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
140 struct timeval tv; |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
141 int cc, newline = 1, totout = 0; |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
142 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
143 for (;;) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
144 FD_ZERO(&fds); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
145 FD_SET(target_fd, &fds); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
146 tv.tv_sec = timeout; |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
147 tv.tv_usec = 0; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
148 cc = select(target_fd+1, &fds, NULL, NULL, &tv); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
149 if (cc < 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
150 if (errno == EINTR) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
151 continue; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
152 perror("select"); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
153 return(-1); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
154 } |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
155 if (cc < 1) { |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
156 fprintf(stderr, |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
157 "error: timeout waiting for \'=\' prompt from target\n"); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
158 return(-1); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
159 } |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
160 cc = read(target_fd, buf, sizeof buf); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
161 if (cc <= 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
162 perror("read after successful select"); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
163 return(-1); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
164 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
165 for (cp = buf; cc; cp++) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
166 cc--; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
167 if (*cp == '=' && newline && !cc) |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
168 return(totout); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
169 putchar(*cp); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
170 totout++; |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
171 if (*cp == '\n') |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
172 newline = 1; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
173 else |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
174 newline = 0; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
175 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
176 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
177 } |