Add forgotten files 1.4.70b
[polyglot.git] / io.c
diff --git a/io.c b/io.c
index 88eb045..ff795d9 100644 (file)
--- a/io.c
+++ b/io.c
@@ -11,6 +11,7 @@
 #include <string.h>
 
 #include <sys/types.h>
+#include <sys/select.h>
 #include <unistd.h>
 
 #include "io.h"
@@ -21,8 +22,6 @@
 static const bool UseDebug = FALSE;
 static const bool UseCR = FALSE;
 
-static const int StringSize = 4096;
-
 static const char LF = '\n';
 static const char CR = '\r';
 
@@ -63,6 +62,27 @@ void io_init(io_t * io) {
    ASSERT(io_is_ok(io));
 }
 
+// io_peek()
+
+bool io_peek(io_t * io){
+    fd_set set[1];
+    int fd_max;
+    int ret;
+    struct timeval tv;
+    tv.tv_sec=0;
+    tv.tv_usec=0;
+  
+    FD_ZERO(set);
+    FD_SET(io->in_fd,set);
+    fd_max=io->in_fd;
+    ret=select(fd_max+1,set,NULL,NULL,&tv);
+    if(ret>0){
+       return TRUE;
+    }else{
+       return FALSE;
+    }
+}
+
 // io_close()
 
 void io_close(io_t * io) {
@@ -71,7 +91,7 @@ void io_close(io_t * io) {
 
    ASSERT(io->out_fd>=0);
 
-   my_log("Adapter>%s: EOF\n",io->name);
+   my_log("Adapter->%s: EOF\n",io->name);
 
    if (close(io->out_fd) == -1) {
       my_fatal("io_close(): close(): %s\n",strerror(errno));
@@ -97,7 +117,14 @@ void io_get_update(io_t * io) {
    pos = io->in_size;
 
    size = BufferSize - pos;
-   if (size <= 0) my_fatal("io_get_update(): buffer overflow\n");
+
+
+   if (size <= 0){
+       //  io->in_buffer[FormatBufferSize-20]='\0';
+       //  my_log("%s","io_get_update(): buffer too small; content starts with:\n");
+       //  my_log("[%s...]\n",io->in_buffer);
+       my_fatal("io_get_update(): buffer overflow\n");
+   }
 
    // read as many data as possible
    n = my_read(io->in_fd,&io->in_buffer[pos],size);
@@ -118,6 +145,7 @@ void io_get_update(io_t * io) {
 
       io->in_eof = TRUE;
    }
+
 }
 
 // io_line_ready()
@@ -196,8 +224,7 @@ bool io_get_line(io_t * io, char string[], int size) {
 
 void io_send(io_t * io, const char format[], ...) {
 
-   va_list arg_list;
-   char string[StringSize];
+   char string[FormatBufferSize];
    int len;
 
    ASSERT(io_is_ok(io));
@@ -207,9 +234,7 @@ void io_send(io_t * io, const char format[], ...) {
 
    // format
 
-   va_start(arg_list,format);
-   vsprintf(string,format,arg_list);
-   va_end(arg_list);
+   CONSTRUCT_ARG_STRING(format,string);
 
    // append string to buffer
 
@@ -245,8 +270,7 @@ void io_send(io_t * io, const char format[], ...) {
 
 void io_send_queue(io_t * io, const char format[], ...) {
 
-   va_list arg_list;
-   char string[StringSize];
+   char string[FormatBufferSize];
    int len;
 
    ASSERT(io_is_ok(io));
@@ -256,9 +280,7 @@ void io_send_queue(io_t * io, const char format[], ...) {
 
    // format
 
-   va_start(arg_list,format);
-   vsprintf(string,format,arg_list);
-   va_end(arg_list);
+   CONSTRUCT_ARG_STRING(format,string);
 
    // append string to buffer