cmdline.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #pragma once
  37. #include <spawn.h>
  38. // http://www.gnu.org/software/automake/manual/automake.html#Using-the-TAP-test-protocol
  39. #ifndef EXIT_SKIP
  40. # define EXIT_SKIP 77
  41. #endif
  42. #ifndef EXIT_FATAL
  43. # define EXIT_FATAL 99
  44. #endif
  45. #ifndef EX_NOEXEC
  46. # define EX_NOEXEC 126
  47. #endif
  48. #ifndef EX_NOTFOUND
  49. # define EX_NOTFOUND 127
  50. #endif
  51. namespace libtest {
  52. class Application {
  53. private:
  54. typedef std::vector< std::pair<std::string, std::string> > Options;
  55. public:
  56. enum error_t {
  57. SUCCESS= EXIT_SUCCESS,
  58. FAILURE= EXIT_FAILURE,
  59. UNINITIALIZED,
  60. SIGTERM_KILLED,
  61. UNKNOWN,
  62. UNKNOWN_SIGNAL,
  63. INVALID_POSIX_SPAWN= 127
  64. };
  65. static const char* toString(error_t arg)
  66. {
  67. switch (arg)
  68. {
  69. case Application::SUCCESS:
  70. return "EXIT_SUCCESS";
  71. case Application::UNINITIALIZED:
  72. return "UNINITIALIZED";
  73. case Application::SIGTERM_KILLED:
  74. return "Exit happened via SIGTERM";
  75. case Application::FAILURE:
  76. return "EXIT_FAILURE";
  77. case Application::UNKNOWN_SIGNAL:
  78. return "Exit happened via a signal which was not SIGTERM";
  79. case Application::INVALID_POSIX_SPAWN:
  80. return "127: Invalid call to posix_spawn()";
  81. case Application::UNKNOWN:
  82. default:
  83. break;
  84. }
  85. return "EXIT_UNKNOWN";
  86. }
  87. class Pipe {
  88. public:
  89. Pipe(int);
  90. ~Pipe();
  91. int fd();
  92. enum close_t {
  93. READ= 0,
  94. WRITE= 1
  95. };
  96. void reset();
  97. void close(const close_t& arg);
  98. void dup_for_spawn(posix_spawn_file_actions_t& file_actions);
  99. void nonblock();
  100. void cloexec();
  101. bool read(libtest::vchar_t&);
  102. private:
  103. const int _std_fd;
  104. int _pipe_fd[2];
  105. bool _open[2];
  106. };
  107. public:
  108. Application(const std::string& arg, const bool _use_libtool_arg= false);
  109. virtual ~Application();
  110. void add_option(const std::string&);
  111. void add_option(const std::string&, const std::string&);
  112. void add_long_option(const std::string& option_name, const std::string& option_value);
  113. error_t run(const char *args[]= NULL);
  114. Application::error_t join();
  115. libtest::vchar_t stdout_result() const
  116. {
  117. return _stdout_buffer;
  118. }
  119. size_t stdout_result_length() const
  120. {
  121. return _stdout_buffer.size();
  122. }
  123. libtest::vchar_t stderr_result() const
  124. {
  125. return _stderr_buffer;
  126. }
  127. const char* stderr_c_str() const
  128. {
  129. return &_stderr_buffer[0];
  130. }
  131. size_t stderr_result_length() const
  132. {
  133. return _stderr_buffer.size();
  134. }
  135. std::string print();
  136. void use_valgrind(bool arg)
  137. {
  138. _use_valgrind= arg;
  139. }
  140. bool check() const;
  141. bool slurp();
  142. void murder();
  143. void use_gdb(bool arg)
  144. {
  145. _use_gdb= arg;
  146. }
  147. void use_ptrcheck(bool arg)
  148. {
  149. _use_ptrcheck= arg;
  150. }
  151. std::string arguments();
  152. std::string gdb_filename()
  153. {
  154. return _gdb_filename;
  155. }
  156. pid_t pid() const
  157. {
  158. return _pid;
  159. }
  160. void will_fail()
  161. {
  162. _will_fail= true;
  163. }
  164. private:
  165. void create_argv(const char *args[]);
  166. void delete_argv();
  167. void add_to_build_argv(const char*);
  168. private:
  169. const bool _use_libtool;
  170. bool _use_valgrind;
  171. bool _use_gdb;
  172. bool _use_ptrcheck;
  173. bool _will_fail;
  174. size_t _argc;
  175. std::string _exectuble_name;
  176. std::string _exectuble;
  177. std::string _exectuble_with_path;
  178. std::string _gdb_filename;
  179. Options _options;
  180. Pipe stdin_fd;
  181. Pipe stdout_fd;
  182. Pipe stderr_fd;
  183. libtest::vchar_ptr_t built_argv;
  184. pid_t _pid;
  185. libtest::vchar_t _stdout_buffer;
  186. libtest::vchar_t _stderr_buffer;
  187. int _status;
  188. pthread_t _thread;
  189. error_t _app_exit_state;
  190. };
  191. static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
  192. {
  193. return output << Application::toString(arg);
  194. }
  195. int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
  196. const char *gearmand_binary();
  197. const char *drizzled_binary();
  198. }