chiark / gitweb /
Backgrounded commands and RELOAD.
[tripe] / client.c
index 314bb87cb0241103760aa81803aa585bde680b3e..4aece8d10462f2632a93c88796998d0ed2a9ef00 100644 (file)
--- a/client.c
+++ b/client.c
@@ -79,6 +79,7 @@ static const char *logname = 0;
 static FILE *logfp = 0;
 static unsigned f = 0;
 static int fd;
+static const char *bgtag = 0;
 
 #define f_bogus 1u
 #define f_spawn 2u
@@ -109,6 +110,21 @@ static void writelog(const char *cat, const char *msg)
   fprintf(logfp, "%s %s: %s\n", buf, cat, msg);
 }
 
+static void checkbg(char **p)
+{
+  char *q = str_getword(p);
+  if (!q)
+    die(EXIT_FAILURE, "missing background tag");
+  if (!bgtag || strcmp(bgtag, q) != 0)
+    die(EXIT_FAILURE, "unexpected background tag `%s'", q);
+}
+
+static void checkfg(void)
+{
+  if (bgtag)
+    die(EXIT_FAILURE, "unexpected foreground response");
+}
+
 static void cline(char *p, size_t len, void *b)
 {
   char *q;
@@ -141,13 +157,29 @@ static void cline(char *p, size_t len, void *b)
       writelog("error", d.buf);
       dstr_destroy(&d);
     }
-  } else if (strcmp(q, "FAIL") == 0)
+  } else if (strcmp(q, "FAIL") == 0) {
+    checkfg();
     die(EXIT_FAILURE, "%s", p);
-  else if (strcmp(q, "INFO") == 0)
+  } else if (strcmp(q, "INFO") == 0) {
+    checkfg();
     puts(p);
-  else if (strcmp(q, "OK") == 0)
+  } else if (strcmp(q, "OK") == 0) {
+    checkfg();
+    exit(0);
+  } else if (strcmp(q, "BGDETACH") == 0) {
+    if (bgtag)
+      die(EXIT_FAILURE, "repeat detach");
+    bgtag = xstrdup(p);
+  } else if (strcmp(q, "BGOK") == 0) {
+    checkbg(&p);
     exit(0);
-  else
+  } else if (strcmp(q, "BGINFO") == 0) {
+    checkbg(&p);
+    puts(p);
+  } else if (strcmp(q, "BGFAIL") == 0) {
+    checkbg(&p);
+    die(EXIT_FAILURE, "%s", p);
+  } else
     die(EXIT_FAILURE, "unexpected output `%s %s'", q, p); 
 }