A number of small build fixes found while looking at mingw support.
[awesomized/libmemcached] / libtest / cmdline.h
index ad1d61250665ba40d6aca4ceab1e2d4ec99f3db7..ca7098dd722343823e223cb932f6dbdead0615c5 100644 (file)
 
 #pragma once
 
-#include <spawn.h>
+#ifdef HAVE_SPAWN_H
+# include <spawn.h>
+#endif
+
+/*
+  http://www.gnu.org/software/automake/manual/automake.html#Tests
+  When no test protocol is in use, an exit status of 0 from a test script will denote a success, an exit status of 77 a skipped test, an exit status of 99 an hard error, and any other exit status will denote a failure.
+*/
+
+#define EXIT_SKIP 77
+#define EXIT_FATAL 99
+
+#ifndef EX_NOEXEC
+# define EX_NOEXEC 126
+#endif
+
+#ifndef EX_NOTFOUND
+# define EX_NOTFOUND 127
+#endif
 
 namespace libtest {
 
@@ -49,9 +67,43 @@ public:
   enum error_t {
     SUCCESS= EXIT_SUCCESS,
     FAILURE= EXIT_FAILURE,
-    INVALID= 127
+    UNINITIALIZED,
+    SIGTERM_KILLED,
+    UNKNOWN,
+    UNKNOWN_SIGNAL,
+    INVALID_POSIX_SPAWN= 127
   };
 
+  static const char* toString(error_t arg)
+  {
+    switch (arg)
+    {
+    case Application::SUCCESS:
+      return "EXIT_SUCCESS";
+
+    case Application::UNINITIALIZED:
+      return "UNINITIALIZED";
+
+    case Application::SIGTERM_KILLED:
+      return "Exit happened via SIGTERM";
+
+    case Application::FAILURE:
+      return "EXIT_FAILURE";
+
+    case Application::UNKNOWN_SIGNAL:
+      return "Exit happened via a signal which was not SIGTERM";
+
+    case Application::INVALID_POSIX_SPAWN:
+      return "127: Invalid call to posix_spawn()";
+
+    case Application::UNKNOWN:
+    default:
+      break;
+    }
+
+    return "EXIT_UNKNOWN";
+  }
+
   class Pipe {
   public:
     Pipe(int);
@@ -66,8 +118,7 @@ public:
 
     void reset();
     void close(const close_t& arg);
-    void dup_for_spawn(const close_t& arg,
-                       posix_spawn_file_actions_t& file_actions);
+    void dup_for_spawn(posix_spawn_file_actions_t& file_actions);
 
     void nonblock();
     void cloexec();
@@ -88,7 +139,7 @@ public:
   void add_option(const std::string&, const std::string&);
   void add_long_option(const std::string& option_name, const std::string& option_value);
   error_t run(const char *args[]= NULL);
-  error_t wait(bool nohang= true);
+  Application::error_t join();
 
   libtest::vchar_t stdout_result() const
   {
@@ -173,33 +224,18 @@ private:
   Pipe stdin_fd;
   Pipe stdout_fd;
   Pipe stderr_fd;
-  char * * built_argv;
+  libtest::vchar_ptr_t built_argv;
   pid_t _pid;
   libtest::vchar_t _stdout_buffer;
   libtest::vchar_t _stderr_buffer;
+  int _status;
+  pthread_t _thread;
+  error_t _app_exit_state;
 };
 
 static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
 {
-  switch (arg)
-  {
-    case Application::SUCCESS:
-      output << "EXIT_SUCCESS";
-      break;
-
-    case Application::FAILURE:
-      output << "EXIT_FAILURE";
-      break;
-
-    case Application::INVALID:
-      output << "127";
-      break;
-
-    default:
-      output << "EXIT_UNKNOWN";
-  }
-
-  return output;
+  return output << Application::toString(arg);
 }
 
 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);