xorp

cli.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/cli.hh,v 1.55 2008/10/02 21:58:22 bms Exp $
00021 
00022 #ifndef __RTRMGR_CLI_HH__
00023 #define __RTRMGR_CLI_HH__
00024 
00025 
00026 
00027 
00028 
00029 #include "libxipc/xrl_error.hh"
00030 
00031 #include "cli/cli_node.hh"
00032 #include "cli/cli_client.hh"
00033 
00034 #include "config_operators.hh"
00035 #include "rtrmgr_error.hh"
00036 
00037 
00038 class CommandTree;
00039 class CommandTreeNode;
00040 class ConfigTree;
00041 class ConfigTreeNode;
00042 class OpCommandList;
00043 class OpInstance;
00044 class SlaveConfigTree;
00045 class SlaveConfigTreeNode;
00046 class TemplateTree;
00047 class TemplateTreeNode;
00048 
00049 class XorpShellBase;
00050 
00051 class XrlAtomList;
00052 
00053 enum CliModeType {
00054     CLI_MODE_NONE       = 0,
00055     CLI_MODE_OPERATIONAL    = 1,
00056     CLI_MODE_CONFIGURE      = 2,
00057     CLI_MODE_TEXTENTRY      = 3
00058 };
00059 
00060 class RouterCLI : public NONCOPYABLE {
00061 public:
00062     RouterCLI(XorpShellBase& xorpsh, CliNode& cli_node,
00063           XorpFd cli_client_input_fd, XorpFd cli_client_output_fd,
00064           bool verbose) throw (InitError);
00065     virtual ~RouterCLI();
00066 
00067     bool done() const;
00068     bool is_config_mode() const;
00069     void clear_command_set();
00070     int configure_func(const string& ,
00071                const string& ,
00072                uint32_t ,
00073                const vector<string>& command_global_name,
00074                const vector<string>& argv);
00075     void enter_config_done(const XrlError& e);
00076     void got_config_users(const XrlError& e, const XrlAtomList* users);
00077     void new_config_user(uid_t user_id);
00078     void leave_config_done(const XrlError& e);
00079     void notify_user(const string& alert, bool urgent);
00080     void config_changed_by_other_user();
00081     int op_help_func(const string& ,
00082            const string& ,
00083            uint32_t ,
00084            const vector<string>& command_global_name,
00085            const vector<string>& argv);
00086     int conf_help_func(const string& ,
00087            const string& ,
00088            uint32_t ,
00089            const vector<string>& command_global_name,
00090            const vector<string>& argv);
00091     int logout_func(const string& ,
00092            const string& ,
00093            uint32_t ,
00094            const vector<string>& command_global_name,
00095            const vector<string>& argv);
00096     int exit_func(const string& ,
00097           const string& ,
00098           uint32_t ,
00099           const vector<string>& command_global_name,
00100           const vector<string>& argv);
00101     int edit_func(const string& ,
00102           const string& ,
00103           uint32_t ,
00104           const vector<string>& command_global_name,
00105           const vector<string>& argv);
00106     int extract_leaf_node_operator_and_value(const TemplateTreeNode& ttn,
00107                          const vector<string>& argv,
00108                          ConfigOperator& node_operator,
00109                          string& value,
00110                          string& error_msg);
00111     map<string, CliCommandMatch> text_entry_children_func(
00112     const vector<string>& vector_path) const;
00113     int text_entry_func(const string& ,
00114             const string& ,
00115             uint32_t ,
00116             const vector<string>& command_global_name,
00117             const vector<string>& argv);
00118     int delete_func(const string& ,
00119             const string& ,
00120             uint32_t ,
00121             const vector<string>& command_global_name,
00122             const vector<string>& argv);
00123     int commit_func(const string& ,
00124             const string& ,
00125             uint32_t ,
00126             const vector<string>& command_global_name,
00127             const vector<string>& argv);
00128     void commit_done(bool success, string error_msg);
00129 
00130     int show_func(const string& ,
00131           const string& ,
00132           uint32_t ,
00133           const vector<string>& command_global_name,
00134           const vector<string>& argv);
00135     int op_mode_func(const string& ,
00136              const string& ,
00137              uint32_t ,
00138              const vector<string>& command_global_name,
00139              const vector<string>& argv);
00140     int save_func(const string& ,
00141           const string& ,
00142           uint32_t ,
00143           const vector<string>& command_global_name,
00144           const vector<string>& argv);
00145     void save_communicated(const XrlError& e);
00146     void save_done(bool success, string error_msg);
00147 
00148     int load_func(const string& ,
00149           const string& ,
00150           uint32_t ,
00151           const vector<string>& command_global_name,
00152           const vector<string>& argv);
00153     void load_communicated(const XrlError& e);
00154     void load_done(bool success, string error_msg);
00155 
00156     map<string, CliCommandMatch> op_mode_help(const vector<string>& command_global_name) const;
00157     map<string, CliCommandMatch> configure_mode_help(const vector<string>& command_global_name) const;
00158 
00159     typedef XorpCallback1<void, const string&>::RefPtr OpModePrintCallback;
00160     typedef XorpCallback2<void, bool,const string&>::RefPtr OpModeDoneCallback;
00161 
00167     void op_mode_cmd_print(const string& result);
00168 
00175     void op_mode_cmd_done(bool success, const string& error_msg);
00176 
00189     void op_mode_cmd_interrupt(const string& server_name,
00190                    const string& cli_term_name,
00191                    uint32_t cli_session_id,
00192                    const vector<string>& command_global_name,
00193                    const vector<string>& command_args);
00194 
00200     void op_mode_cmd_tidy();
00201 
00202 private:
00203     static const string DEFAULT_XORP_PROMPT_OPERATIONAL;
00204     static const string DEFAULT_XORP_PROMPT_CONFIGURATION;
00205 
00206     CliClient* cli_client() const { return _cli_client_ptr; }
00207     void reset_path();
00208     void set_path(string path);
00209     void apply_path_change();
00210     int add_static_configure_mode_commands(string& error_msg);
00211     void set_prompt(const string& line1, 
00212             const string& line2);
00213     void config_mode_prompt();
00214     void idle_ui();
00215     void reenable_ui();
00216     void silent_reenable_ui();
00217 
00218     string pathstr() const;
00219     string pathstr2() const;
00220     void operational_mode();
00221     int add_op_mode_commands(CliCommand *root, string& error_msg);
00222     void configure_mode();
00223     void text_entry_mode();
00224     void add_command_subtree(CliCommand& current_cli_node,
00225                  const CommandTreeNode& current_ctn,
00226                  const CLI_PROCESS_CALLBACK& cli_process_callback,
00227                  vector<string> vector_path, size_t depth,
00228                  bool can_pipe, bool include_allowed_values);
00229     void add_text_entry_commands(CliCommand* com0);
00230     void add_edit_subtree();
00231     void add_create_subtree();
00232     void add_delete_subtree();
00233     void add_set_subtree();
00234     void add_show_subtree();
00235 
00236     void display_config_mode_users() const;
00237     void display_alerts();
00238 
00239     bool check_for_rtrmgr_restart();
00240     void verify_rtrmgr_restart(const XrlError& e, const uint32_t* pid);
00241 
00242     string get_help_o(const string& s) const;
00243     string get_help_c(const string& s) const;
00244 
00245     string makepath(const list<string>& parts) const;
00246     string makepath(const vector<string>& parts) const;
00247 
00248     const TemplateTree* template_tree() const;
00249     SlaveConfigTree*    config_tree();
00250     const SlaveConfigTree* config_tree() const;
00251     OpCommandList*  op_cmd_list() const;
00252     uint32_t clientid() const;
00253 
00254     void add_allowed_values_and_ranges(
00255     const TemplateTreeNode* ttn,
00256     bool is_executable, bool can_pipe,
00257     map<string, CliCommandMatch>& children) const;
00258 
00259     XorpShellBase&     _xorpsh;
00260 
00261     //    SlaveConfigTreeNode* _current_config_node;
00262 
00263     CliNode&        _cli_node;
00264     CliClient*      _cli_client_ptr;
00265     CliClient*      _removed_cli_client_ptr;
00266     bool        _verbose;   // Set to true if output is verbose
00267     string      _operational_mode_prompt;
00268     string      _configuration_mode_prompt;
00269     CliModeType     _mode;
00270     CliCommand*     _set_node;
00271     CliCommand*     _show_node;
00272     CliCommand*     _edit_node;
00273     CliCommand*     _create_node;
00274     CliCommand*     _delete_node;
00275     CliCommand*     _run_node;
00276     list<string>    _path;
00277     list<uint32_t>      _braces; // keep trace of the indent depth of
00278                  // braces in text_entry mode
00279     list<uint32_t>  _config_mode_users;
00280     list<string>    _alerts;
00281     // size_t       _nesting_depth; // for text_entry mode: number of
00282                     // brackets deep
00283     // list<size_t> _nesting_lengths; // for text_entry mode: number of
00284                     // nodes for each backet nested
00285     bool        _changes_made;  // true if there are uncommitted
00286                     // changes
00287 
00288     map<string,string> _help_o;  // short help strings for operational
00289                  // mode commands
00290     map<string,string> _help_long_o; // detailed help information for
00291                                      // operational mode commands
00292     map<string,string> _help_c;  // short help strings for configuration
00293                  // mode commands
00294     map<string,string> _help_long_c; // detailed help information for
00295                                      // configuration mode commands
00296 
00297     OpInstance *_op_mode_cmd;// Currently executing operational mode commands
00298 };
00299 
00300 #endif // __RTRMGR_CLI_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations