Index: trunk/Ohana/src/opihi/pcontrol/GetJobOutput.c
===================================================================
--- trunk/Ohana/src/opihi/pcontrol/GetJobOutput.c	(revision 3212)
+++ trunk/Ohana/src/opihi/pcontrol/GetJobOutput.c	(revision 4450)
@@ -1,46 +1,56 @@
 # include "pcontrol.h"
+# define PCLIENT_TIMEOUT 20
 
-int GetJobOutput (Job *job, char *channel) {
+/* we read Nbytes from the host, then watch for the prompt */ 
+int GetJobOutput (char *command, Host *host, IOBuffer *buffer, int Nbytes) {
   
-  int      status;
-  IOBuffer *buffer;
-  Host     *host;
+  int i, status, Nstart;
+  char *line;
 
-  buffer = NULL;
-  if (!strcasecmp (channel, "stdout")) buffer = &job[0].stdout;
-  if (!strcasecmp (channel, "stderr")) buffer = &job[0].stderr;
-  if (buffer == NULL) {
-    fprintf (stderr, "invalid output channel : programming error\n");
-    exit (1);
+  /* flush any earlier messages */
+  ReadtoIOBuffer (buffer, host[0].stdout);
+  FlushIOBuffer (buffer);
+  Nstart = buffer[0].Nbuffer;
+
+  /* send command (stdout / stderr) */
+  ALLOCATE (line, char, MAX (1, strlen(command) + 1));
+  sprintf (line, "%s\n", command);
+  status = write (host[0].stdin, line, strlen(line));
+  free (line);
+
+  /* is pipe still open? */
+  if ((status == -1) && (errno == EPIPE)) return (PCLIENT_DOWN);
+
+  /* read at least Nbytes, then watch for PCLIENT_PROMPT */
+  line = NULL;
+  status = -1;
+  for (i = 0; (i < PCLIENT_TIMEOUT) && (status == -1) && (line == NULL); i++) {
+    status = ReadtoIOBuffer (buffer, host[0].stdout);
+    if ((buffer[0].Nbuffer - Nstart) >= Nbytes) {
+      line = memstr (buffer[0].buffer, PCLIENT_PROMPT, buffer[0].Nbuffer);
+    }
+    if (status == -1) usleep (10000);
   }
-
-  /* If we already have the output, just return, don't retry */
-  if (buffer[0].Nbuffer) return (TRUE);
-
-  /** must have a valid host : if not, move to pending? **/
-  host = job[0].host;
-  status = PclientCommand (host, channel, PCLIENT_PROMPT, buffer);
+  if (status ==  0) return (PCLIENT_DOWN);
+  if (status == -1) return (PCLIENT_HUNG);
 
   /* check on success of pclient command */
   switch (status) {
-    case PCLIENT_DOWN:
-      /*** different behavior for ANYHOST, WANTHOST, NEEDHOST ***/
-      buffer[0].Nbuffer = 0;
+    case -1:
+      fprintf (stderr, "host %s is not responding\n", host[0].hostname);
+      return (FALSE);
+
+    case 0:
       fprintf (stderr, "host %s is down\n", host[0].hostname);
       return (FALSE);
 
-    case PCLIENT_HUNG:
-      /*** should we consider a HUNG host DOWN? ***/
-      buffer[0].Nbuffer = 0;
-      fprintf (stderr, "host %s is not responding\n", host[0].hostname);
-      return (FALSE);
-
-    case PCLIENT_GOOD:
-      fprintf (stderr, "message received (GetJobOutput : %s)\n", channel);  
+    default:
+      fprintf (stderr, "message received (GetJobOutput : %s)\n", command);  
+      /* drop extra bytes from pclient (not pclient:job) */
+      buffer[0].Nbuffer = Nstart + Nbytes;
+      if (buffer[0].Nalloc > buffer[0].Nbuffer) {
+	bzero (buffer[0].buffer + buffer[0].Nbuffer, buffer[0].Nalloc - buffer[0].Nbuffer);
+      }
       return (TRUE);
-
-    default:
-      fprintf (stderr, "unknown status for pclient command: programming error\n");  
-      exit (1);
   }
 
