13 #include <sys/types.h>
21 static const bool UseDebug = false;
22 static const bool UseCR = false;
24 static const int StringSize = 4096;
26 static const char LF = '\n';
27 static const char CR = '\r';
31 static int my_read (int fd, char string[], int size);
32 static void my_write (int fd, const char string[], int size);
38 bool io_is_ok(const io_t * io) {
40 if (io == NULL) return false;
42 if (io->name == NULL) return false;
44 if (io->in_eof != true && io->in_eof != false) return false;
46 if (io->in_size < 0 || io->in_size > BufferSize) return false;
47 if (io->out_size < 0 || io->out_size > BufferSize) return false;
54 void io_init(io_t * io) {
68 void io_close(io_t * io) {
72 ASSERT(io->out_fd>=0);
74 my_log("Adapter>Engine: EOF\n",io->name);
76 if (close(io->out_fd) == -1) {
77 my_fatal("io_close(): close(): %s\n",strerror(errno));
85 void io_get_update(io_t * io) {
99 size = BufferSize - pos;
100 if (size <= 0) my_fatal("io_get_update(): buffer overflow\n");
102 // read as many data as possible
103 n = my_read(io->in_fd,&io->in_buffer[pos],size);
104 if (UseDebug) my_log("POLYGLOT read %d byte%s from %s\n",n,(n>1)?"s":"",io->name);
106 if (n > 0) { // at least one character was read
108 // update buffer size
110 ASSERT(n>=1&&n<=size);
113 ASSERT(io->in_size>=0&&io->in_size<=BufferSize);
125 bool io_line_ready(const io_t * io) {
127 ASSERT(io_is_ok(io));
129 if (io->in_eof) return true;
131 if (memchr(io->in_buffer,LF,io->in_size) != NULL) return true; // buffer contains LF
138 bool io_get_line(io_t * io, char string[], int size) {
143 ASSERT(io_is_ok(io));
144 ASSERT(string!=NULL);
152 // test for end of buffer
154 if (src >= io->in_size) {
156 my_log("%s->Adapter: EOF\n",io->name);
159 my_fatal("io_get_line(): no EOL in buffer\n");
163 // test for end of string
165 if (dst >= size) my_fatal("io_get_line(): buffer overflow\n");
167 // copy the next character
169 c = io->in_buffer[src++];
171 if (c == LF) { // LF => line complete
174 } else if (c != CR) { // skip CRs
184 ASSERT(io->in_size>=0);
186 if (io->in_size > 0) memmove(&io->in_buffer[0],&io->in_buffer[src],io->in_size);
190 my_log("%s->Adapter: %s\n",io->name,string);
197 void io_send(io_t * io, const char format[], ...) {
200 char string[StringSize];
203 ASSERT(io_is_ok(io));
204 ASSERT(format!=NULL);
206 ASSERT(io->out_fd>=0);
210 va_start(arg_list,format);
211 vsprintf(string,format,arg_list);
214 // append string to buffer
216 len = strlen(string);
217 if (io->out_size + len > BufferSize-2) my_fatal("io_send(): buffer overflow\n");
219 memcpy(&io->out_buffer[io->out_size],string,len);
222 ASSERT(io->out_size>=0&&io->out_size<=BufferSize-2);
226 io->out_buffer[io->out_size] = '\0';
227 my_log("Adapter->%s: %s\n",io->name,io->out_buffer);
228 // my_log("> %f %s %s\n",now_real(),io->name,io->out_buffer);
229 // append EOL to buffer
231 if (UseCR) io->out_buffer[io->out_size++] = CR;
232 io->out_buffer[io->out_size++] = LF;
234 ASSERT(io->out_size>=0&&io->out_size<=BufferSize);
238 if (UseDebug) my_log("POLYGLOT writing %d byte%s to %s\n",io->out_size,(io->out_size>1)?"s":"",io->name);
239 my_write(io->out_fd,io->out_buffer,io->out_size);
246 void io_send_queue(io_t * io, const char format[], ...) {
249 char string[StringSize];
252 ASSERT(io_is_ok(io));
253 ASSERT(format!=NULL);
255 ASSERT(io->out_fd>=0);
259 va_start(arg_list,format);
260 vsprintf(string,format,arg_list);
263 // append string to buffer
265 len = strlen(string);
266 if (io->out_size + len > BufferSize-2) my_fatal("io_send_queue(): buffer overflow\n");
268 memcpy(&io->out_buffer[io->out_size],string,len);
271 ASSERT(io->out_size>=0&&io->out_size<=BufferSize-2);
276 static int my_read(int fd, char string[], int size) {
281 ASSERT(string!=NULL);
285 n = read(fd,string,size);
286 } while (n == -1 && errno == EINTR);
288 if (n == -1) my_fatal("my_read(): read(): %s\n",strerror(errno));
297 static void my_write(int fd, const char string[], int size) {
302 ASSERT(string!=NULL);
307 n = write(fd,string,size);
309 // if (n == -1 && errno != EINTR && errno != EPIPE) my_fatal("my_write(): write(): %s\n",strerror(errno));
313 } else if (errno == EINTR) {
314 n = 0; // nothing has been written
315 } else if (errno == EPIPE) {
316 n = size; // pretend everything has been written
318 my_fatal("my_write(): write(): %s\n",strerror(errno));