a3d9cb25333fb6b33da74febfa544e44d846160b
[m6w6/libmemcached] / libtest / cmdline.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #pragma once
23
24 #include <spawn.h>
25
26 namespace libtest {
27
28 class Application {
29 private:
30 typedef std::vector< std::pair<std::string, std::string> > Options;
31
32 public:
33
34 enum error_t {
35 SUCCESS= EXIT_SUCCESS,
36 FAILURE= EXIT_FAILURE,
37 INVALID= 127
38 };
39
40 class Pipe {
41 public:
42 Pipe(int);
43 ~Pipe();
44
45 int fd();
46
47 enum close_t {
48 READ= 0,
49 WRITE= 1
50 };
51
52 void reset();
53 void close(const close_t& arg);
54 void dup_for_spawn(const close_t& arg,
55 posix_spawn_file_actions_t& file_actions);
56
57 void nonblock();
58 void cloexec();
59 bool read(libtest::vchar_t&);
60
61 private:
62 const int _std_fd;
63 int _pipe_fd[2];
64 bool _open[2];
65 };
66
67 public:
68 Application(const std::string& arg, const bool _use_libtool_arg= false);
69
70 virtual ~Application();
71
72 void add_option(const std::string&);
73 void add_option(const std::string&, const std::string&);
74 void add_long_option(const std::string& option_name, const std::string& option_value);
75 error_t run(const char *args[]= NULL);
76 error_t wait(bool nohang= true);
77
78 libtest::vchar_t stdout_result() const
79 {
80 return _stdout_buffer;
81 }
82
83 size_t stdout_result_length() const
84 {
85 return _stdout_buffer.size();
86 }
87
88 libtest::vchar_t stderr_result() const
89 {
90 return _stderr_buffer;
91 }
92
93 const char* stderr_c_str() const
94 {
95 return &_stderr_buffer[0];
96 }
97
98 size_t stderr_result_length() const
99 {
100 return _stderr_buffer.size();
101 }
102
103 std::string print();
104
105 void use_valgrind(bool arg= true)
106 {
107 _use_valgrind= arg;
108 }
109
110 bool check() const;
111
112 bool slurp();
113 void murder();
114
115 void use_gdb(bool arg= true)
116 {
117 _use_gdb= arg;
118 }
119
120 void use_ptrcheck(bool arg= true)
121 {
122 _use_ptrcheck= arg;
123 }
124
125 std::string arguments();
126
127 std::string gdb_filename()
128 {
129 return _gdb_filename;
130 }
131
132 pid_t pid() const
133 {
134 return _pid;
135 }
136
137 void will_fail()
138 {
139 _will_fail= true;
140 }
141
142 private:
143 void create_argv(const char *args[]);
144 void delete_argv();
145
146 private:
147 const bool _use_libtool;
148 bool _use_valgrind;
149 bool _use_gdb;
150 bool _use_ptrcheck;
151 bool _will_fail;
152 size_t _argc;
153 std::string _exectuble_name;
154 std::string _exectuble;
155 std::string _exectuble_with_path;
156 std::string _gdb_filename;
157 Options _options;
158 Pipe stdin_fd;
159 Pipe stdout_fd;
160 Pipe stderr_fd;
161 char * * built_argv;
162 pid_t _pid;
163 libtest::vchar_t _stdout_buffer;
164 libtest::vchar_t _stderr_buffer;
165 };
166
167 static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
168 {
169 switch (arg)
170 {
171 case Application::SUCCESS:
172 output << "EXIT_SUCCESS";
173 break;
174
175 case Application::FAILURE:
176 output << "EXIT_FAILURE";
177 break;
178
179 case Application::INVALID:
180 output << "127";
181 break;
182
183 default:
184 output << "EXIT_UNKNOWN";
185 }
186
187 return output;
188 }
189
190 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
191
192 const char *gearmand_binary();
193 const char *drizzled_binary();
194
195 }