xorp

op_commands.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2011 XORP, Inc and Others
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 // $XORP: xorp/rtrmgr/op_commands.hh,v 1.43 2008/10/02 21:58:24 bms Exp $
00021 
00022 #ifndef __RTRMGR_OP_COMMAND_HH__
00023 #define __RTRMGR_OP_COMMAND_HH__
00024 
00025 
00026 
00027 
00028 
00029 #include "libxorp/asyncio.hh"
00030 
00031 #include "cli.hh"
00032 #include "rtrmgr_error.hh"
00033 
00034 
00035 
00036 class ConfigTree;
00037 class OpCommand;
00038 class RunCommand;
00039 class TemplateTree;
00040 class SlaveModuleManager;
00041 
00042 class OpInstance :
00043     public NONCOPYABLE
00044 {
00045 public:
00046     OpInstance(EventLoop&           eventloop,
00047            OpCommand&           op_command,
00048            const string&            executable_filename,
00049            const list<string>&      command_argument_list,
00050            RouterCLI::OpModePrintCallback   print_cb,
00051            RouterCLI::OpModeDoneCallback    done_cb);
00052     ~OpInstance();
00053 
00057     void terminate();
00058 
00062     void terminate_with_prejudice();
00063 
00064 private:
00065     void stdout_cb(RunCommand* run_command, const string& output);
00066     void stderr_cb(RunCommand* run_command, const string& output);
00067     void done_cb(RunCommand* run_command, bool success,
00068          const string& error_msg);
00069     void execute_done(bool success);
00070 
00071     EventLoop&      _eventloop;
00072     OpCommand&      _op_command;
00073     string      _executable_filename;
00074     list<string>    _command_argument_list;
00075 
00076     RunCommand*     _run_command;
00077     string      _error_msg;
00078 
00079     RouterCLI::OpModePrintCallback  _print_cb;
00080     RouterCLI::OpModeDoneCallback   _done_cb;
00081 };
00082 
00083 class OpCommand {
00084 public:
00085     OpCommand(OpCommandList& ocl, const list<string>& command_parts);
00086 #ifdef XORP_USE_USTL
00087     OpCommand() { _ocl = NULL; }
00088 #endif
00089 
00090     const list<string>& command_parts() const { return _command_parts; }
00091     const string& command_name() const { return _command_name; }
00092     const string& help_string() const { return _help_string; }
00093     const string& module() const { return _module; }
00094     const string& command_action() const { return _command_action; }
00095     void set_help_string(const string& v) { _help_string = v; }
00096     void set_module(const string& v) { _module = v; }
00097     void set_command_action(const string& v) { _command_action = v; }
00098     void set_command_action_filename(const string& v) { _command_action_filename = v; }
00099     void set_command_action_argument_list(const list<string>& v) { _command_action_argument_list = v; }
00100     void set_command_executable_filename(const string& v) { _command_executable_filename = v; }
00101     bool is_executable() const { return (! _command_action.empty()); }
00102     bool can_pipe() const { return is_executable(); }
00103 
00110     bool default_nomore_mode() const { return (_default_nomore_mode); }
00111 
00118     void set_default_nomore_mode(bool v) { _default_nomore_mode = v; }
00119 
00120     void add_opt_param(const string& opt_param, const string& opt_param_help);
00121     bool has_opt_param(const string& opt_param) const;
00122     string str() const;
00123     static string command_parts2command_name(const list<string>& command_parts);
00124 
00137     static list<string> select_positional_argument(
00138     const list<string>& argument_list,
00139     const string& position,
00140     string& error_msg);
00141 
00153     OpInstance* execute(EventLoop& eventloop,
00154             const list<string>& command_line,
00155             RouterCLI::OpModePrintCallback print_cb,
00156             RouterCLI::OpModeDoneCallback done_cb);
00157 
00158     bool command_match(const list<string>& path_parts,
00159                SlaveConfigTree* sct, bool exact_match) const;
00160     void get_matches(size_t wordnum, SlaveConfigTree* sct,
00161              map<string, CliCommandMatch>& return_matches) const;
00162     bool type_match(const string& s, string& errmsg) const;
00163     void add_instance(OpInstance* instance);
00164     void remove_instance(OpInstance* instance);
00165 
00166     bool is_invalid() const { return (_is_invalid); }
00167     void set_is_invalid(bool v) { _is_invalid = v; }
00168 
00169 private:
00170     OpCommandList*  _ocl;
00171     list<string>    _command_parts;
00172     string      _command_name;
00173     string      _help_string;
00174     string      _module;
00175     string      _command_action;
00176     string      _command_action_filename;
00177     list<string>    _command_action_argument_list;
00178     string      _command_executable_filename;
00179     map<string, string> _opt_params;    // Optional parameters and the CLI help
00180     set<OpInstance*>    _instances;
00181     bool        _is_invalid;    // If true, this command is invalid
00182     bool        _default_nomore_mode; // True if "no-more" (i.e., unpaged) mode is default
00183 };
00184 
00185 class OpCommandList {
00186 public:
00187     OpCommandList(const TemplateTree* tt, SlaveModuleManager& mmgr);
00188     OpCommandList(const string& config_template_dir, const TemplateTree* tt,
00189           SlaveModuleManager& mmgr) throw (InitError);
00190     ~OpCommandList();
00191 
00192     bool done() const;
00193     void incr_running_op_instances_n();
00194     void decr_running_op_instances_n();
00195 
00196     int read_templates(const string& config_template_dir, string& errmsg);
00197     void set_slave_config_tree(SlaveConfigTree* sct) { _slave_config_tree = sct; }
00198     bool check_variable_name(const string& variable_name) const;
00199     OpCommand* find_op_command(const list<string>& command_parts);
00200     OpCommand* add_op_command(const OpCommand& op_command);
00201     bool command_match(const list<string>& command_parts,
00202                bool exact_match) const;
00203     OpInstance *execute(EventLoop& eventloop,
00204             const list<string>& command_parts,
00205             RouterCLI::OpModePrintCallback print_cb,
00206             RouterCLI::OpModeDoneCallback done_cb) const;
00207     map<string, CliCommandMatch> top_level_commands() const;
00208     map<string, CliCommandMatch> childlist(const vector<string>& vector_path) const;
00209 
00210     list<OpCommand*>& op_commands() { return _op_commands; }
00211 
00212 private:
00213     list<OpCommand*>    _op_commands;
00214     size_t      _running_op_instances_n;
00215 
00216     // Below here is temporary storage for use in parsing
00217     list<string>    _path_segments;
00218     OpCommand*      _current_command;
00219     const TemplateTree* _template_tree;
00220     SlaveConfigTree*    _slave_config_tree;
00221     SlaveModuleManager& _mmgr;
00222 };
00223 
00224 #endif // __RTRMGR_OP_COMMAND_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations