cmdline.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. void close();
  93. enum close_t {
  94. READ= 0,
  95. WRITE= 1
  96. };
  97. void reset();
  98. void close(const close_t& arg);
  99. void dup_for_spawn(posix_spawn_file_actions_t& file_actions);
  100. void nonblock();
  101. void cloexec();
  102. bool read(libtest::vchar_t&);
  103. private:
  104. const int _std_fd;
  105. int _pipe_fd[2];
  106. bool _open[2];
  107. };
  108. public:
  109. Application(const std::string& arg, const bool _use_libtool_arg= false);
  110. virtual ~Application();
  111. void add_option(const std::string&);
  112. void add_option(const std::string&, const std::string&);
  113. void add_long_option(const std::string& option_name, const std::string& option_value);
  114. error_t run(const char *args[]= NULL);
  115. Application::error_t join();
  116. libtest::vchar_t stdout_result() const
  117. {
  118. return _stdout_buffer;
  119. }
  120. size_t stdout_result_length() const
  121. {
  122. return _stdout_buffer.size();
  123. }
  124. const char* stdout_c_str() const
  125. {
  126. return _stdout_buffer.size() ? &_stdout_buffer[0] : NULL;
  127. }
  128. libtest::vchar_t stderr_result() const
  129. {
  130. return _stderr_buffer;
  131. }
  132. const char* stderr_c_str() const
  133. {
  134. return _stderr_buffer.size() ? &_stderr_buffer[0] : NULL;
  135. }
  136. size_t stderr_result_length() const
  137. {
  138. return _stderr_buffer.size();
  139. }
  140. std::string print();
  141. void use_valgrind(bool arg)
  142. {
  143. _use_valgrind= arg;
  144. }
  145. bool check() const;
  146. bool slurp();
  147. void murder();
  148. void use_gdb(bool arg)
  149. {
  150. _use_gdb= arg;
  151. }
  152. void use_ptrcheck(bool arg)
  153. {
  154. _use_ptrcheck= arg;
  155. }
  156. std::string arguments();
  157. std::string gdb_filename()
  158. {
  159. return _gdb_filename;
  160. }
  161. pid_t pid() const
  162. {
  163. return _pid;
  164. }
  165. void will_fail()
  166. {
  167. _will_fail= true;
  168. }
  169. private:
  170. void create_argv(const char *args[]);
  171. void delete_argv();
  172. void add_to_build_argv(const char*);
  173. private:
  174. const bool _use_libtool;
  175. bool _use_valgrind;
  176. bool _use_gdb;
  177. bool _use_ptrcheck;
  178. bool _will_fail;
  179. size_t _argc;
  180. std::string _exectuble_name;
  181. std::string _exectuble;
  182. std::string _exectuble_with_path;
  183. std::string _gdb_filename;
  184. Options _options;
  185. Pipe stdin_fd;
  186. Pipe stdout_fd;
  187. Pipe stderr_fd;
  188. libtest::vchar_ptr_t built_argv;
  189. pid_t _pid;
  190. libtest::vchar_t _stdout_buffer;
  191. libtest::vchar_t _stderr_buffer;
  192. int _status;
  193. pthread_t _thread;
  194. error_t _app_exit_state;
  195. };
  196. static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
  197. {
  198. return output << Application::toString(arg);
  199. }
  200. int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
  201. }