FreeCalypso > hg > freecalypso-sw
annotate loadtools/tpinterf.c @ 21:67a39d8914a8
starting work on loadtool
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Sat, 04 May 2013 03:47:40 +0000 |
parents | |
children | aca1948e9713 |
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> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 #include <string.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 #include <strings.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 #include <stdlib.h> |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 extern int errno; |
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 target_fd; |
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 /* definition matches ../target-utils/libcommon/cmdentry.c */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 #define MAXCMD 527 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 |
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 * static buffer between tpinterf_make_cmd and tpinterf_send_cmd |
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 * 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
|
25 * 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
|
26 * buffer. |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 static char cmdbuf[MAXCMD+2]; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 static int cmdlen; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 static int |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 arg_chars_valid(arg) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 char *arg; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 char *cp; |
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 for (cp = arg; *cp; cp++) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 if (*cp < ' ' || *cp > '~') |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 return(0); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 return(1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 } |
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 * 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
|
45 * 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
|
46 * passed as tty "keyboard" input to the target, enforcing length |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 * and character validity limits in the process. The output is |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 * stored in an internal static buffer for subsequent |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 * tpinterf_send_cmd(). |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 * |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 * 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
|
52 * violated. |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 tpinterf_make_cmd(argv) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 char **argv; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 int arglen; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 char **ap, *dp; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 dp = cmdbuf; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 cmdlen = 0; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 for (ap = argv; *ap; ap++) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 arglen = strlen(*ap); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 if (ap != argv) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 arglen++; /* separating space */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 if (arglen > MAXCMD - cmdlen) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 return(-1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 if (!arg_chars_valid(*ap)) |
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 (ap != argv) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 *dp++ = ' '; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 strcpy(dp, *ap); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 dp += strlen(*ap); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 cmdlen += arglen; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 *dp++ = '\r'; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 *dp = '\n'; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 return(0); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 } |
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 * This function sends the previously-constructed command to the target, |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 * and collects the expected response. |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 * |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 * 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
|
86 */ |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
87 tpinterf_send_cmd() |
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 char echobuf[MAXCMD+2]; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
90 fd_set fds; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
91 struct timeval tv; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
92 int rcvd, cc; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
93 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
94 write(target_fd, cmdbuf, cmdlen + 1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
95 for (rcvd = 0; rcvd < cmdlen + 2; ) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
96 FD_ZERO(&fds); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
97 FD_SET(target_fd, &fds); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
98 tv.tv_sec = 1; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
99 tv.tv_usec = 0; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
100 cc = select(target_fd+1, &fds, NULL, NULL, &tv); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
101 if (cc < 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
102 if (errno == EINTR) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
103 continue; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
104 perror("select"); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
105 exit(1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
106 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
107 if (cc < 1) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
108 return(-1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
109 cc = read(target_fd, echobuf + rcvd, cmdlen + 2 - rcvd); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
110 if (cc <= 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
111 perror("read after successful select"); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
112 exit(1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
113 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
114 rcvd += cc; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
115 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
116 if (bcmp(echobuf, cmdbuf, cmdlen + 2)) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
117 return(-1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
118 else |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
119 return(0); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
120 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
121 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
122 /* |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
123 * 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
|
124 * '=' prompt is received. All intermediate output is passed to |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
125 * stdout. |
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 * Return value: 0 if '=' prompt received, -1 otherwise (timeout) |
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 tpinterf_pass_output() |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
130 { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
131 char buf[512], *cp; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
132 fd_set fds; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
133 struct timeval tv; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
134 int cc, newline = 1; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
135 |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
136 for (;;) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
137 FD_ZERO(&fds); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
138 FD_SET(target_fd, &fds); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
139 tv.tv_sec = 1; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
140 tv.tv_usec = 0; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
141 cc = select(target_fd+1, &fds, NULL, NULL, &tv); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
142 if (cc < 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
143 if (errno == EINTR) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
144 continue; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
145 perror("select"); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
146 exit(1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
147 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
148 if (cc < 1) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
149 return(-1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
150 cc = read(target_fd, buf, sizeof buf); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
151 if (cc <= 0) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
152 perror("read after successful select"); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
153 exit(1); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
154 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
155 for (cp = buf; cc; cp++) { |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
156 cc--; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
157 if (*cp == '=' && newline && !cc) |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
158 return(0); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
159 putchar(*cp); |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
160 if (*cp == '\n') |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
161 newline = 1; |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
162 else |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
163 newline = 0; |
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 } |
67a39d8914a8
starting work on loadtool
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
166 } |