Update to support new warnings.
[m6w6/libmemcached] / libtest / cmdline.h
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
37 #pragma once
38
39 #include <spawn.h>
40
41 // http://www.gnu.org/software/automake/manual/automake.html#Using-the-TAP-test-protocol
42 #define EXIT_SKIP 77
43 #define EXIT_FATAL 77
44
45 namespace libtest {
46
47 class Application {
48 private:
49 typedef std::vector< std::pair<std::string, std::string> > Options;
50
51 public:
52
53 enum error_t {
54 SUCCESS= EXIT_SUCCESS,
55 FAILURE= EXIT_FAILURE,
56 INVALID= 127
57 };
58
59 static const char* toString(error_t arg)
60 {
61 switch (arg)
62 {
63 case Application::SUCCESS:
64 return "EXIT_SUCCESS";
65
66 case Application::FAILURE:
67 return "EXIT_FAILURE";
68
69 case Application::INVALID:
70 return "127";
71
72 default:
73 break;
74 }
75
76 return "EXIT_UNKNOWN";
77 }
78
79 class Pipe {
80 public:
81 Pipe(int);
82 ~Pipe();
83
84 int fd();
85
86 enum close_t {
87 READ= 0,
88 WRITE= 1
89 };
90
91 void reset();
92 void close(const close_t& arg);
93 void dup_for_spawn(posix_spawn_file_actions_t& file_actions);
94
95 void nonblock();
96 void cloexec();
97 bool read(libtest::vchar_t&);
98
99 private:
100 const int _std_fd;
101 int _pipe_fd[2];
102 bool _open[2];
103 };
104
105 public:
106 Application(const std::string& arg, const bool _use_libtool_arg= false);
107
108 virtual ~Application();
109
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 error_t wait(bool nohang= true);
115 Application::error_t join();
116
117 libtest::vchar_t stdout_result() const
118 {
119 return _stdout_buffer;
120 }
121
122 size_t stdout_result_length() const
123 {
124 return _stdout_buffer.size();
125 }
126
127 libtest::vchar_t stderr_result() const
128 {
129 return _stderr_buffer;
130 }
131
132 const char* stderr_c_str() const
133 {
134 return &_stderr_buffer[0];
135 }
136
137 size_t stderr_result_length() const
138 {
139 return _stderr_buffer.size();
140 }
141
142 std::string print();
143
144 void use_valgrind(bool arg= true)
145 {
146 _use_valgrind= arg;
147 }
148
149 bool check() const;
150
151 bool slurp();
152 void murder();
153
154 void use_gdb(bool arg= true)
155 {
156 _use_gdb= arg;
157 }
158
159 void use_ptrcheck(bool arg= true)
160 {
161 _use_ptrcheck= arg;
162 }
163
164 std::string arguments();
165
166 std::string gdb_filename()
167 {
168 return _gdb_filename;
169 }
170
171 pid_t pid() const
172 {
173 return _pid;
174 }
175
176 void will_fail()
177 {
178 _will_fail= true;
179 }
180
181 private:
182 void create_argv(const char *args[]);
183 void delete_argv();
184
185 private:
186 const bool _use_libtool;
187 bool _use_valgrind;
188 bool _use_gdb;
189 bool _use_ptrcheck;
190 bool _will_fail;
191 size_t _argc;
192 std::string _exectuble_name;
193 std::string _exectuble;
194 std::string _exectuble_with_path;
195 std::string _gdb_filename;
196 Options _options;
197 Pipe stdin_fd;
198 Pipe stdout_fd;
199 Pipe stderr_fd;
200 libtest::vchar_ptr_t built_argv;
201 pid_t _pid;
202 libtest::vchar_t _stdout_buffer;
203 libtest::vchar_t _stderr_buffer;
204 };
205
206 static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
207 {
208 return output << Application::toString(arg);
209 }
210
211 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
212
213 const char *gearmand_binary();
214 const char *drizzled_binary();
215
216 }