Index: trunk/Ohana/src/opihi/pcontrol/JobOps.c
===================================================================
--- trunk/Ohana/src/opihi/pcontrol/JobOps.c	(revision 3212)
+++ trunk/Ohana/src/opihi/pcontrol/JobOps.c	(revision 4450)
@@ -35,4 +35,27 @@
 }
 
+Job *FindJobStack (IDtype JobID) {
+
+  Job *job;
+
+  job = FindJobPtr (JobID, PCONTROL_JOB_PENDING);
+  if (job != NULL) return (job);
+
+  job = FindJobPtr (JobID, PCONTROL_JOB_BUSY);
+  if (job != NULL) return (job);
+
+  job = FindJobPtr (JobID, PCONTROL_JOB_EXIT);
+  if (job != NULL) return (job);
+
+  job = FindJobPtr (JobID, PCONTROL_JOB_CRASH);
+  if (job != NULL) return (job);
+
+  job = FindJobPtr (JobID, PCONTROL_JOB_DONE);
+  if (job != NULL) return (job);
+
+  return (NULL);
+}
+
+/* add job to position in stack */
 int PutJob (Job *job, int StackID, int where) {
 
@@ -51,4 +74,5 @@
 }
   
+/* remove job from position in stack */
 Job *GetJob (int StackID, int where) {
 
@@ -63,4 +87,5 @@
 }
   
+/* return stack position of job */
 int FindJob (IDtype JobID, int StackID) {
 
@@ -81,4 +106,40 @@
 }
 
+/* return pointer to job */
+Job *FindJobPtr (IDtype JobID, int StackID) {
+
+  int i;
+  Job *job;
+  Stack *stack;
+
+  stack = GetJobStack (StackID);
+  if (stack == NULL) return (NULL);
+
+  for (i = 0; i < stack[0].Nobject; i++) {
+    job = (Job *) stack[0].object[i];
+    if (job[0].JobID == JobID) {
+      return (job);
+    }
+  }
+  return (NULL);
+}
+
+/* remove job from stack, return pointer */
+Job *PullJob (IDtype JobID, int StackID) {
+  
+  int N;
+  Job *job;
+
+  N = FindJob (JobID, StackID);
+  if (N < 0) return (NULL);
+
+  job = GetJob (StackID, N);
+  if (job == NULL) {
+    fprintf (stderr, "programming error! job missing from stack\n");
+    exit (1);
+  }
+  return (job);
+}
+
 IDtype AddJob (char *hostname, JobMode mode, int timeout, int argc, char **argv) {
 
@@ -89,5 +150,5 @@
   job[0].argc     = argc;
   job[0].argv     = argv;
-  job[0].hostname = strcreate (hostname);
+  job[0].hostname = hostname;
   job[0].mode     = mode;
   job[0].host     = NULL;
@@ -119,27 +180,5 @@
 }
 
-# if (0)
-void KillJob (Job *job) {
-
-  int status;
-  char line[64];
-  Host *host;
-
-  sprintf (line, "reset\n");
-
-  /* send command to client */
-  status = write (host[0].stdin, line, strlen(line));
-  if ((status == -1) && (errno == EPIPE)) {
-    fprintf (stderr, "host %s is down\n", host[0].hostname);
-    PutHost (host, PCONTROL_HOST_DOWN, STACK_BOTTOM);
-    PutJob (job, PCONTROL_JOB_PENDING, STACK_BOTTOM);
-    return (FALSE);
-  }
-  /* check for a response */
-
-  /** this needs to be cleaned up to handle the slow response case **/
-}
-# endif
-
+/* unlink job and host, pull host from its stack */
 Host *UnlinkJobAndHost (Job *job) {
 
@@ -147,5 +186,9 @@
   Host *host;
 
-  host = job[0].host;
+  host = (Host *) job[0].host;
+  if (host == NULL) {
+    fprintf (stderr, "programming error: job has no host\n");
+    exit (2);
+  }
 
   /* unlink host & job */
@@ -153,11 +196,9 @@
   host[0].job = NULL;
   
-  /*** need to pop host off of correct stack XXX ***/
-  N = FindHost (host[0].HostID, host[0].stack);
-  if (N < 0) {
+  /* remove host from correct stack */
+  if (PullHost (host[0].HostID, host[0].stack) == NULL) {
     fprintf (stderr, "programming error: host is not found in current stack\n");
     exit (2);
   }
-  host = GetHost (host[0].stack, N);
   return (host);
 }
@@ -166,27 +207,11 @@
   int N;
 
-  job[0].host = host;
-  host[0].job = job;
-
-# if (0)
-  /*** need to pop host off of correct stack XXX ***/
-  N = FindHost (host[0].HostID, host[0].stack);
-  if (N < 0) {
-    fprintf (stderr, "programming error: host is not found in current stack\n");
-    exit (2);
-  }
-  host = GetHost (host[0].stack, N);
-# endif
-
-  /*** need to pop job off of correct stack XXX ***/
-  N = FindJob (job[0].JobID, job[0].stack);
-  if (N < 0) {
+  job[0].host = (struct Host *) host;
+  host[0].job = (struct Job *) job;
+
+  /* remove job from correct stack */
+  if (PullJob (job[0].JobID, job[0].stack) == NULL) {
     fprintf (stderr, "programming error: job is not found in current stack\n");
     exit (2);
   }
-  job = GetJob (job[0].stack, N);
-
-  /*** this is fairly crazy : the only reason this works is cause I 
-       get the same host ptr here and in CheckIdleHost 
-  ***/
-}
+}
