Index: trunk/Ohana/src/libohana/include/ohana.h
===================================================================
--- trunk/Ohana/src/libohana/include/ohana.h	(revision 7782)
+++ trunk/Ohana/src/libohana/include/ohana.h	(revision 7917)
@@ -166,4 +166,6 @@
 int EmptyIOBuffer (IOBuffer *buffer, int Nmax, int fd);
 void FreeIOBuffer (IOBuffer *buffer);
+int PrintIOBuffer (IOBuffer *buffer, char *format, ...);
+int vPrintIOBuffer (IOBuffer *buffer, char *format, va_list argp);
 
 /* communication functions */
@@ -173,4 +175,6 @@
 int SendCommand (int device, int length, char *format, ...);
 int SendCommandV (int device, int length, char *format, va_list argp);
+
+char *CheckForMessage (IOBuffer *buffer);
 
 /*
Index: trunk/Ohana/src/libohana/src/CommOps.c
===================================================================
--- trunk/Ohana/src/libohana/src/CommOps.c	(revision 7782)
+++ trunk/Ohana/src/libohana/src/CommOps.c	(revision 7917)
@@ -113,2 +113,50 @@
   return (TRUE);
 }
+
+/* 
+
+ I need an alternative ExpectCommand function which appends to an existing buffer
+ until the complete message is ready.  
+
+ A command looks like this (pre-determined length):
+ NN bytes: XXXXX\0 
+
+ A message looks like this (preceded by NBYTES command) :
+ 16 bytes: WORD NNN\0
+ NNN bytes: message.... \0
+
+ the expect function needs to monitor the number of bytes already received
+ we need to be able to call it repeatedly until the buffer is full.
+
+*/
+
+/* check if the first entry in the buffer corresponds to a message:
+   A message looks like this (preceded by NBYTES command) :
+    16 bytes: WORD NNN
+    NNN bytes: message....
+    note that the NULL bytes are not sent
+  If a message is found, it is popped off the buffer and sent back as a
+  complete line (the length portion is dropped) 
+*/
+
+char *CheckForMessage (IOBuffer *buffer) {
+
+  int Nbytes;
+  char command[20], *line;
+
+  if (buffer[0].Nbuffer < 16) return NULL;
+  memcpy (command, buffer[0].buffer, 16);
+  command[16] = 0;
+
+  sscanf (command, "NBYTES: %d", &Nbytes);
+
+  if (buffer[0].Nbuffer < Nbytes + 16) return NULL;
+
+  ALLOCATE (line, char, Nbytes + 1);
+  memcpy (line, &buffer[0].buffer[16], Nbytes);
+  line[Nbytes] = 0;
+
+  buffer[0].Nbuffer -= Nbytes + 16;
+  memmove (buffer[0].buffer, &buffer[0].buffer[Nbytes+16], buffer[0].Nbuffer);
+  return (line);
+}
Index: trunk/Ohana/src/libohana/src/IOBufferOps.c
===================================================================
--- trunk/Ohana/src/libohana/src/IOBufferOps.c	(revision 7782)
+++ trunk/Ohana/src/libohana/src/IOBufferOps.c	(revision 7917)
@@ -84,2 +84,35 @@
   }
 }
+
+/* print to an IOBuffer (varargs form) */
+int PrintIOBuffer (IOBuffer *buffer, char *format, ...) {
+
+  int status;
+  va_list argp;  
+
+  va_start (argp, format);
+  status = vPrintIOBuffer (buffer, format, argp);
+  va_end (argp);
+  return (status);
+}
+
+/* print to an IOBuffer (va_list form) */
+int vPrintIOBuffer (IOBuffer *buffer, char *format, va_list argp) {
+
+  /* add the output line to the given IOBuffer */
+  
+  int Nbyte, status;
+  char tmp, *line;
+
+  Nbyte = vsnprintf (&tmp, 0, format, argp);
+
+  if (buffer[0].Nbuffer + Nbyte + 1 >= buffer[0].Nalloc) {
+    buffer[0].Nalloc = buffer[0].Nbuffer + Nbyte + 64;
+    REALLOCATE (buffer[0].buffer, char, buffer[0].Nalloc);
+  }
+
+  vsnprintf (&buffer[0].buffer[buffer[0].Nbuffer], Nbyte + 1, format, argp);
+  buffer[0].Nbuffer += Nbyte + 1;
+  return (TRUE);
+}
+  
