1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.
#include <iosfwd>
#include <iostream>
#include <map>
#include "cmd.hh"
using std::istream;
using std::make_pair;
using std::map;
using std::ostream;
using std::pair;
using std::string;
using std::vector;
namespace automation {
// When this is split into multiple files, there will not be any
// guarantees about initialization order. So, use something we can
// initialize ourselves.
static map<string, automate * const> * automations;
automate::automate(string const &n, string const &p,
options::options_type const & o)
: name(n), params(p), options(o)
{
static bool first(true);
if (first)
{
first = false;
automations = new map<string, automate * const>;
}
automations->insert(make_pair(name, this));
}
automate::~automate() {}
}
automation::automate &
find_automation(utf8 const & name, string const & root_cmd_name)
{
map<string, automation::automate * const>::const_iterator
i = automation::automations->find(name());
if (i == automation::automations->end())
throw usage(root_cmd_name);
else
return *(i->second);
}
void
automate_command(utf8 cmd, vector<utf8> args,
string const & root_cmd_name,
app_state & app,
ostream & output)
{
find_automation(cmd, root_cmd_name).run(args, root_cmd_name, app, output);
}
static string const interface_version = "4.0";
// Name: interface_version
// Arguments: none
// Added in: 0.0
// Purpose: Prints version of automation interface. Major number increments
// whenever a backwards incompatible change is made; minor number increments
// whenever any change is made (but is reset when major number increments).
// Output format: "<decimal number>.<decimal number>\n". Always matches
// "[0-9]+\.[0-9]+\n".
// Error conditions: None.
AUTOMATE(interface_version, "", options::opts::none)
{
if (args.size() != 0)
throw usage(help_name);
output << interface_version << "\n";
}
// Name: stdio
// Arguments: none
// Added in: 1.0
// Purpose: Allow multiple automate commands to be run from one instance
// of monotone.
//
// Input format: The input is a series of lines of the form
// 'l'<size>':'<string>[<size>':'<string>...]'e', with characters
// after the 'e' of one command, but before the 'l' of the next ignored.
// This space is reserved, and should not contain characters other
// than '\n'.
// Example:
// l6:leavese
// l7:parents40:0e3171212f34839c2e3263e7282cdeea22fc5378e
//
// Output format: <command number>:<err code>:<last?>:<size>:<output>
// <command number> is a decimal number specifying which command
// this output is from. It is 0 for the first command, and increases
// by one each time.
// <err code> is 0 for success, 1 for a syntax error, and 2 for any
// other error.
// <last?> is 'l' if this is the last piece of output for this command,
// and 'm' if there is more output to come.
// <size> is the number of bytes in the output.
// <output> is the output of the command.
// Example:
// 0:0:l:205:0e3171212f34839c2e3263e7282cdeea22fc5378
// 1f4ef73c3e056883c6a5ff66728dd764557db5e6
// 2133c52680aa2492b18ed902bdef7e083464c0b8
// 23501f8afd1f9ee037019765309b0f8428567f8a
// 2c295fcf5fe20301557b9b3a5b4d437b5ab8ec8c
// 1:0:l:41:7706a422ccad41621c958affa999b1a1dd644e79
//
// Error conditions: Errors encountered by the commands run only set
// the error code in the output for that command. Malformed input
// results in exit with a non-zero return value and an error message.
// automate_streambuf and automate_ostream are so we can dump output at a
// set length, rather than waiting until we have all of the output.
class automate_reader
{
istream & in;
enum location {opt, cmd, none, eof};
location loc;
bool get_string(std::string & out)
{
out.clear();
if (loc == none || loc == eof)
{
return false;
}
size_t size(0);
char c;
read(&c, 1);
if (c == 'e')
{
loc = none;
return false;
}
while(c <= '9' && c >= '0')
{
size = (size*10)+(c-'0');
read(&c, 1);
}
E(c == ':', F("Bad input to automate stdio"));
char *str = new char[size];
size_t got = 0;
while(got < size)
{
int n = read(str, size-got);
got += n;
}
out = std::string(str, size);
delete[] str;
L(FL("Got string '%s'") % out);
return true;
}
static ssize_t read(void *buf, size_t nbytes, bool eof_ok = false)
{
ssize_t rv;
rv = ::read(0, buf, nbytes);
E(rv >= 0, F("read from client failed with error code: %d") % rv);
E(eof_ok || rv > 0, F("Bad input to automate stdio (unexpected EOF)"));
return rv;
}
void go_to_next_item()
{
if (loc == eof)
return;
string starters("ol");
string whitespace(" \r\n\t");
string foo;
while (loc != none)
get_string(foo);
char c('e');
do
{
if (read(&c, 1, true) == 0)
{
loc = eof;
return;
}
}
while (whitespace.find(c) != std::string::npos);
switch (c)
{
case 'o': loc = opt; break;
case 'l': loc = cmd; break;
default: E(false, F("Bad input to automate stdio"));
}
}
public:
automate_reader(istream & is) : in(is), loc(none)
{}
bool get_command(vector<pair<string, string> > & params,
vector<string> & cmdline)
{
params.clear();
cmdline.clear();
if (loc == none)
go_to_next_item();
if (loc == eof)
return false;
else if (loc == opt)
{
string key, val;
while(get_string(key) && get_string(val))
params.push_back(make_pair(key, val));
go_to_next_item();
}
E(loc == cmd, F("Bad input to automate stdio"));
string item;
while (get_string(item))
{
cmdline.push_back(item);
}
return true;
}
};
struct automate_streambuf : public std::streambuf
{
private:
size_t _bufsize;
std::ostream *out;
automate_reader *in;
int cmdnum;
int err;
public:
automate_streambuf(size_t bufsize)
: std::streambuf(), _bufsize(bufsize), out(0), in(0), cmdnum(0), err(0)
{
char *inbuf = new char[_bufsize];
setp(inbuf, inbuf + _bufsize);
}
automate_streambuf(std::ostream & o, size_t bufsize)
: std::streambuf(), _bufsize(bufsize), out(&o), in(0), cmdnum(0), err(0)
{
char *inbuf = new char[_bufsize];
setp(inbuf, inbuf + _bufsize);
}
automate_streambuf(automate_reader & i, size_t bufsize)
: std::streambuf(), _bufsize(bufsize), out(0), in(&i), cmdnum(0), err(0)
{
char *inbuf = new char[_bufsize];
setp(inbuf, inbuf + _bufsize);
}
~automate_streambuf()
{}
void set_err(int e)
{
sync();
err = e;
}
void end_cmd()
{
_M_sync(true);
++cmdnum;
err = 0;
}
virtual int sync()
{
_M_sync();
return 0;
}
void _M_sync(bool end = false)
{
if (!out)
{
setp(pbase(), pbase() + _bufsize);
return;
}
int num = pptr() - pbase();
if (num || end)
{
(*out) << cmdnum << ":"
<< err << ":"
<< (end?'l':'m') << ":"
<< num << ":" << std::string(pbase(), num);
setp(pbase(), pbase() + _bufsize);
out->flush();
}
}
int_type
overflow(int_type c = traits_type::eof())
{
sync();
sputc(c);
return 0;
}
};
struct automate_ostream : public std::ostream
{
automate_streambuf _M_autobuf;
automate_ostream(std::ostream &out, size_t blocksize)
: std::ostream(NULL),
_M_autobuf(out, blocksize)
{ this->init(&_M_autobuf); }
~automate_ostream()
{}
automate_streambuf *
rdbuf() const
{ return const_cast<automate_streambuf *>(&_M_autobuf); }
void set_err(int e)
{ _M_autobuf.set_err(e); }
void end_cmd()
{ _M_autobuf.end_cmd(); }
};
AUTOMATE(stdio, "", options::opts::automate_stdio_size)
{
if (args.size() != 0)
throw usage(help_name);
automate_ostream os(output, app.opts.automate_stdio_size);
automate_reader ar(std::cin);
vector<pair<string, string> > params;
vector<string> cmdline;
while(ar.get_command(params, cmdline))//while(!EOF)
{
utf8 cmd;
vector<utf8> args;
vector<string>::iterator i = cmdline.begin();
E(i != cmdline.end(), F("Bad input to automate stdio"));
cmd = utf8(*i);
for (++i; i != cmdline.end(); ++i)
{
args.push_back(utf8(*i));
}
try
{
options::options_type opts = options::opts::globals();
opts = opts | find_automation(cmd, help_name).options;
opts.instantiate(&app.opts).from_key_value_pairs(params);
automate_command(cmd, args, help_name, app, os);
}
catch(informative_failure & f)
{
os.set_err(2);
//Do this instead of printing f.what directly so the output
//will be split into properly-sized blocks automatically.
os<<f.what();
}
os.end_cmd();
}
}
CMD_WITH_SUBCMDS(automate, N_("automation"),
N_("automation interface"),
options::opts::none)
{
if (args.size() == 0)
throw usage(name);
vector<utf8>::const_iterator i = args.begin();
utf8 cmd = *i;
++i;
vector<utf8> cmd_args(i, args.end());
make_io_binary();
automate_command(cmd, cmd_args, name, app, std::cout);
}
std::string commands::cmd_automate::params()
{
std::string out;
map<string, automation::automate * const>::const_iterator i;
for (i = automation::automations->begin();
i != automation::automations->end(); ++i)
{
out += i->second->name + " " + i->second->params;
if (out[out.size()-1] != '\n')
out += "\n";
}
return out;
}
options::options_type
commands::cmd_automate::get_options(vector<utf8> const & args)
{
if (args.size() < 2)
return options::options_type();
return find_automation(idx(args,1), idx(args,0)()).options;
}
// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
|