comparison loadtools/tpinterf.c @ 23:aca1948e9713

loadtool: initial version compiles and links
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 04 May 2013 04:52:05 +0000
parents 67a39d8914a8
children 05af070c4b60
comparison
equal deleted inserted replaced
22:e658a84b37df 23:aca1948e9713
5 */ 5 */
6 6
7 #include <sys/types.h> 7 #include <sys/types.h>
8 #include <sys/time.h> 8 #include <sys/time.h>
9 #include <sys/errno.h> 9 #include <sys/errno.h>
10 #include <stdio.h>
10 #include <string.h> 11 #include <string.h>
11 #include <strings.h> 12 #include <strings.h>
12 #include <stdlib.h> 13 #include <stdlib.h>
13 14
14 extern int errno; 15 extern int errno;
100 cc = select(target_fd+1, &fds, NULL, NULL, &tv); 101 cc = select(target_fd+1, &fds, NULL, NULL, &tv);
101 if (cc < 0) { 102 if (cc < 0) {
102 if (errno == EINTR) 103 if (errno == EINTR)
103 continue; 104 continue;
104 perror("select"); 105 perror("select");
105 exit(1); 106 return(-1);
106 } 107 }
107 if (cc < 1) 108 if (cc < 1) {
109 fprintf(stderr,
110 "error: timeout waiting for command echo\n");
108 return(-1); 111 return(-1);
112 }
109 cc = read(target_fd, echobuf + rcvd, cmdlen + 2 - rcvd); 113 cc = read(target_fd, echobuf + rcvd, cmdlen + 2 - rcvd);
110 if (cc <= 0) { 114 if (cc <= 0) {
111 perror("read after successful select"); 115 perror("read after successful select");
112 exit(1); 116 return(-1);
113 } 117 }
114 rcvd += cc; 118 rcvd += cc;
115 } 119 }
116 if (bcmp(echobuf, cmdbuf, cmdlen + 2)) 120 if (bcmp(echobuf, cmdbuf, cmdlen + 2)) {
121 fprintf(stderr, "error: command echo mismatch\n");
117 return(-1); 122 return(-1);
118 else 123 } else
119 return(0); 124 return(0);
120 } 125 }
121 126
122 /* 127 /*
123 * This functions reads the serial output from the target until a 128 * This functions reads the serial output from the target until a
124 * '=' prompt is received. All intermediate output is passed to 129 * '=' prompt is received. All intermediate output is passed to
125 * stdout. 130 * stdout.
126 * 131 *
127 * Return value: 0 if '=' prompt received, -1 otherwise (timeout) 132 * Return value: 0 if '=' prompt received immediately,
133 * positive if some scribble came before the prompt, -1 on errors
134 * (timeout, read errors, etc).
128 */ 135 */
129 tpinterf_pass_output() 136 tpinterf_pass_output(timeout)
130 { 137 {
131 char buf[512], *cp; 138 char buf[512], *cp;
132 fd_set fds; 139 fd_set fds;
133 struct timeval tv; 140 struct timeval tv;
134 int cc, newline = 1; 141 int cc, newline = 1, totout = 0;
135 142
136 for (;;) { 143 for (;;) {
137 FD_ZERO(&fds); 144 FD_ZERO(&fds);
138 FD_SET(target_fd, &fds); 145 FD_SET(target_fd, &fds);
139 tv.tv_sec = 1; 146 tv.tv_sec = timeout;
140 tv.tv_usec = 0; 147 tv.tv_usec = 0;
141 cc = select(target_fd+1, &fds, NULL, NULL, &tv); 148 cc = select(target_fd+1, &fds, NULL, NULL, &tv);
142 if (cc < 0) { 149 if (cc < 0) {
143 if (errno == EINTR) 150 if (errno == EINTR)
144 continue; 151 continue;
145 perror("select"); 152 perror("select");
146 exit(1); 153 return(-1);
147 } 154 }
148 if (cc < 1) 155 if (cc < 1) {
156 fprintf(stderr,
157 "error: timeout waiting for \'=\' prompt from target\n");
149 return(-1); 158 return(-1);
159 }
150 cc = read(target_fd, buf, sizeof buf); 160 cc = read(target_fd, buf, sizeof buf);
151 if (cc <= 0) { 161 if (cc <= 0) {
152 perror("read after successful select"); 162 perror("read after successful select");
153 exit(1); 163 return(-1);
154 } 164 }
155 for (cp = buf; cc; cp++) { 165 for (cp = buf; cc; cp++) {
156 cc--; 166 cc--;
157 if (*cp == '=' && newline && !cc) 167 if (*cp == '=' && newline && !cc)
158 return(0); 168 return(totout);
159 putchar(*cp); 169 putchar(*cp);
170 totout++;
160 if (*cp == '\n') 171 if (*cp == '\n')
161 newline = 1; 172 newline = 1;
162 else 173 else
163 newline = 0; 174 newline = 0;
164 } 175 }