FreeCalypso > hg > freecalypso-sw
annotate loadtools/tpinterf.c @ 107:8b44e806b6e1
loadtools/README: documentation for loadtools should now be complete
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Fri, 13 Sep 2013 16:47:09 +0000 |
parents | 05af070c4b60 |
children |
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 |
35
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
23
diff
changeset
|
4 * other target-utils. This module will be linked by both |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
23
diff
changeset
|
5 * fc-loadtool and fc-chainload. |
21
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 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <sys/types.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 #include <sys/time.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 #include <sys/errno.h> |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
11 #include <stdio.h> |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 #include <string.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 #include <strings.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 #include <stdlib.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 extern int errno; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 extern int target_fd; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 /* definition matches ../target-utils/libcommon/cmdentry.c */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 #define MAXCMD 527 |
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 /* |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 * static buffer between tpinterf_make_cmd and tpinterf_send_cmd |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 * |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 * 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
|
27 * 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
|
28 * buffer. |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 static char cmdbuf[MAXCMD+2]; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 static int cmdlen; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 static int |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 arg_chars_valid(arg) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 char *arg; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 char *cp; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 for (cp = arg; *cp; cp++) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 if (*cp < ' ' || *cp > '~') |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 return(0); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 return(1); |
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 /* |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 * 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
|
47 * 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
|
48 * passed as tty "keyboard" input to the target, enforcing length |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 * and character validity limits in the process. The output is |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 * stored in an internal static buffer for subsequent |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 * tpinterf_send_cmd(). |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 * |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 * 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
|
54 * violated. |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 tpinterf_make_cmd(argv) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 char **argv; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 int arglen; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 char **ap, *dp; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 dp = cmdbuf; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 cmdlen = 0; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 for (ap = argv; *ap; ap++) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 arglen = strlen(*ap); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 if (ap != argv) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 arglen++; /* separating space */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 if (arglen > MAXCMD - cmdlen) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 return(-1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 if (!arg_chars_valid(*ap)) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 return(-1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 if (ap != argv) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 *dp++ = ' '; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 strcpy(dp, *ap); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 dp += strlen(*ap); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 cmdlen += arglen; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 *dp++ = '\r'; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 *dp = '\n'; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 return(0); |
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 /* |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 * This function sends the previously-constructed command to the target, |
35
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
23
diff
changeset
|
85 * and collects the expected echo. |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 * |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
87 * 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
|
88 */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
89 tpinterf_send_cmd() |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
90 { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
91 char echobuf[MAXCMD+2]; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
92 fd_set fds; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
93 struct timeval tv; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
94 int rcvd, cc; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
95 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
96 write(target_fd, cmdbuf, cmdlen + 1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
97 for (rcvd = 0; rcvd < cmdlen + 2; ) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
98 FD_ZERO(&fds); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
99 FD_SET(target_fd, &fds); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
100 tv.tv_sec = 1; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
101 tv.tv_usec = 0; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
102 cc = select(target_fd+1, &fds, NULL, NULL, &tv); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
103 if (cc < 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
104 if (errno == EINTR) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
105 continue; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
106 perror("select"); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
107 return(-1); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
108 } |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
109 if (cc < 1) { |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
110 fprintf(stderr, |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
111 "error: timeout waiting for command echo\n"); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
112 return(-1); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
113 } |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
114 cc = read(target_fd, echobuf + rcvd, cmdlen + 2 - rcvd); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
115 if (cc <= 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
116 perror("read after successful select"); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
117 return(-1); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
118 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
119 rcvd += cc; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
120 } |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
121 if (bcmp(echobuf, cmdbuf, cmdlen + 2)) { |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
122 fprintf(stderr, "error: command echo mismatch\n"); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
123 return(-1); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
124 } else |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
125 return(0); |
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 /* |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
129 * 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
|
130 * '=' prompt is received. All intermediate output is passed to |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
131 * stdout. |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
132 * |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
133 * Return value: 0 if '=' prompt received immediately, |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
134 * 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
|
135 * (timeout, read errors, etc). |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
136 */ |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
137 tpinterf_pass_output(timeout) |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
138 { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
139 char buf[512], *cp; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
140 fd_set fds; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
141 struct timeval tv; |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
142 int cc, newline = 1, totout = 0; |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
143 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
144 for (;;) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
145 FD_ZERO(&fds); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
146 FD_SET(target_fd, &fds); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
147 tv.tv_sec = timeout; |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
148 tv.tv_usec = 0; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
149 cc = select(target_fd+1, &fds, NULL, NULL, &tv); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
150 if (cc < 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
151 if (errno == EINTR) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
152 continue; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
153 perror("select"); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
154 return(-1); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
155 } |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
156 if (cc < 1) { |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
157 fprintf(stderr, |
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
158 "error: timeout waiting for \'=\' prompt from target\n"); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
159 return(-1); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
160 } |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
161 cc = read(target_fd, buf, sizeof buf); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
162 if (cc <= 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
163 perror("read after successful select"); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
164 return(-1); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
165 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
166 for (cp = buf; cc; cp++) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
167 cc--; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
168 if (*cp == '=' && newline && !cc) |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
169 return(totout); |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
170 putchar(*cp); |
23
aca1948e9713
loadtool: initial version compiles and links
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
21
diff
changeset
|
171 totout++; |
21
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
172 if (*cp == '\n') |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
173 newline = 1; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
174 else |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
175 newline = 0; |
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 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
178 } |