Merging bzr://gaz.tangent.org/libmemcached/build/ to Build branch
[awesomized/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 size_t stderr_result_length() const
94 {
95 return _stderr_buffer.size();
96 }
97
98 std::string print();
99
100 void use_valgrind(bool arg= true)
101 {
102 _use_valgrind= arg;
103 }
104
105 bool check() const;
106
107 bool slurp();
108 void murder();
109
110 void use_gdb(bool arg= true)
111 {
112 _use_gdb= arg;
113 }
114
115 std::string arguments();
116
117 std::string gdb_filename()
118 {
119 return _gdb_filename;
120 }
121
122 pid_t pid() const
123 {
124 return _pid;
125 }
126
127 private:
128 void create_argv(const char *args[]);
129 void delete_argv();
130
131 private:
132 const bool _use_libtool;
133 bool _use_valgrind;
134 bool _use_gdb;
135 size_t _argc;
136 std::string _exectuble_name;
137 std::string _exectuble;
138 std::string _exectuble_with_path;
139 std::string _gdb_filename;
140 Options _options;
141 Pipe stdin_fd;
142 Pipe stdout_fd;
143 Pipe stderr_fd;
144 char * * built_argv;
145 pid_t _pid;
146 libtest::vchar_t _stdout_buffer;
147 libtest::vchar_t _stderr_buffer;
148 };
149
150 static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
151 {
152 switch (arg)
153 {
154 case Application::SUCCESS:
155 output << "EXIT_SUCCESS";
156 break;
157
158 case Application::FAILURE:
159 output << "EXIT_FAILURE";
160 break;
161
162 case Application::INVALID:
163 output << "127";
164 break;
165
166 default:
167 output << "EXIT_UNKNOWN";
168 }
169
170 return output;
171 }
172
173 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
174
175 const char *gearmand_binary();
176
177 }