# # # patch "cmd_automate.cc" # from [9414bb69c8ce700ae8bd446c4e5d3e74b9d01129] # to [e5c1e8e82c3ee925973289f71dace7d1d2c5cac4] # # patch "lua_hooks.cc" # from [a00266e0182e611e8c6a146a59d0d8f5dfd79509] # to [4f197236fdae655d5cf3af020f4207fc79988cf4] # ============================================================ --- cmd_automate.cc 9414bb69c8ce700ae8bd446c4e5d3e74b9d01129 +++ cmd_automate.cc e5c1e8e82c3ee925973289f71dace7d1d2c5cac4 @@ -14,6 +14,7 @@ #include "cmd.hh" #include "app_state.hh" +#include "ui.hh" #include "lua.hh" #include "lua_hooks.hh" @@ -405,79 +406,96 @@ LUAEXT(mtn_automate, ) { args_vector args; std::stringstream output; - app_state* app_p = get_app_state(L); - I(app_p != NULL); - I(app_p->lua.check_lua_state(L)); - E(app_p->mtn_automate_allowed, - F("It is illegal to call the mtn_automate() lua extension,\n" - "unless from a command function defined by register_command().")); + bool result = true; + std::stringstream & os = output; - // don't allow recursive calls - app_p->mtn_automate_allowed = false; + try + { + app_state* app_p = get_app_state(L); + I(app_p != NULL); + I(app_p->lua.check_lua_state(L)); + E(app_p->mtn_automate_allowed, + F("It is illegal to call the mtn_automate() lua extension,\n" + "unless from a command function defined by register_command().")); - // automate_ostream os(output, app_p->opts.automate_stdio_size); - std::stringstream & os = output; + // don't allow recursive calls + app_p->mtn_automate_allowed = false; + + // automate_ostream os(output, app_p->opts.automate_stdio_size); - int n = lua_gettop(L); + int n = lua_gettop(L); - E(n > 0, F("Bad input to mtn_automate() lua extension: command name is missing")); + E(n > 0, F("Bad input to mtn_automate() lua extension: command name is missing")); - app_p->db.ensure_open(); + app_p->db.ensure_open(); - for (int i=1; i<=n; i++) - args.push_back(arg_type(luaL_checkstring(L, i))); - - try - { + L(FL("Starting call to mtn_automate lua hook")); + + for (int i=1; i<=n; i++) + { + arg_type next_arg(luaL_checkstring(L, i)); + L(FL("arg: %s")%next_arg()); + args.push_back(next_arg); + } + commands::command_id id; for (args_vector::const_iterator iter = args.begin(); iter != args.end(); iter++) id.push_back(utf8((*iter)())); - if (!id.empty()) + E(!id.empty(), F("no command found")); + + set< commands::command_id > matches = + CMD_REF(automate)->complete_command(id); + + if (matches.size() == 0) { - I(!args.empty()); + N(false, F("no completions for this command")); + } + else if (matches.size() > 1) + { + N(false, F("multiple completions possible for this command")); + } - set< commands::command_id > matches = - CMD_REF(automate)->complete_command(id); + id = *matches.begin(); - if (matches.size() == 0) - { - N(false, F("no completions for this command")); - } - else if (matches.size() > 1) - { - N(false, F("multiple completions possible for this command")); - } + I(args.size() >= id.size()); + for (commands::command_id::size_type i = 0; i < id.size(); i++) + args.erase(args.begin()); - id = *matches.begin(); + commands::command const * cmd = CMD_REF(automate)->find_command(id); + I(cmd != NULL); + commands::automate const * acmd = reinterpret_cast< commands::automate const * >(cmd); - I(args.size() >= id.size()); - for (commands::command_id::size_type i = 0; i < id.size(); i++) - args.erase(args.begin()); + acmd->exec(*app_p, id, args, os); - commands::command const * cmd = CMD_REF(automate)->find_command(id); - I(cmd != NULL); - commands::automate const * acmd = reinterpret_cast< commands::automate const * >(cmd); - - acmd->exec(*app_p, id, args, os); - } + // allow further calls + app_p->mtn_automate_allowed = true; } 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<mtn_automate_allowed = true; - + lua_pushboolean(L, result); lua_pushstring(L, output.str().c_str()); - return 1; + return 2; } // Local Variables: ============================================================ --- lua_hooks.cc a00266e0182e611e8c6a146a59d0d8f5dfd79509 +++ lua_hooks.cc 4f197236fdae655d5cf3af020f4207fc79988cf4 @@ -1001,7 +1001,7 @@ void commands::cmd_lua::exec(app_state & Lua ll(st); ll.func(f_name); - for (args_vector::const_iterator it = args.begin(); it != args.end(); it++) + for (args_vector::const_iterator it = args.begin(); it != args.end(); ++it) ll.push_str((*it)()); app.mtn_automate_allowed = true;