fgms 0.11.8
The
FlightGear MultiPlayer Server
project
libcli.hxx
Go to the documentation of this file.
1 //
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License as
4 // published by the Free Software Foundation; either version 2 of the
5 // License, or (at your option) any later version.
6 //
7 // This program is distributed in the hope that it will be useful, but
8 // WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // General Public License for more details.
11 //
12 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, U$
15 //
16 // derived from libcli by David Parrish (david@dparrish.com)
17 // Copyright (C) 2011 Oliver Schroeder
18 //
19 
20 #ifndef __LIBCLI_H__
21 #define __LIBCLI_H__
22 
23 #include <string>
24 #include <map>
25 #include <stdarg.h>
26 #ifndef _MSC_VER
27  #include <termios.h>
28 #endif
29 #include "cli_client.hxx"
30 #include "common.hxx"
31 #include "command.hxx"
32 
33 namespace LIBCLI
34 {
35 
36 class CLI
37 {
38 public:
39  typedef std::map<string,string> unp;
40  typedef std::map<string,string>::iterator unp_it;
41  typedef int (*c_auth_func) ( const string& , const string& );
42  typedef int (CLI::*cpp_auth_func) ( const string&, const string& );
43  typedef int (*c_enable_func) ( const string& );
44  typedef int (CLI::*cpp_enable_func) ( const string& );
45 
47  string banner;
50  bool showprompt;
51  string prompt;
52  string hostname;
53  string modestring;
54  int privilege;
55  int mode;
56  int state;
58  unp users;
60  // filter_t* filters;
61 
62  int (*regular_callback)();
67 
68  CLI ( int fd );
69  ~CLI ();
70  void destroy ();
71  void register_command ( Command<CLI>* command, Command<CLI>* parent = 0 );
72  int unregister_command ( char* command );
73  int run_command ( char* command );
74  int loop ();
75  int file ( FILE* fh, int privilege, int mode );
76  void set_auth_callback ( c_auth_func callback );
77  void set_auth_callback ( cpp_auth_func callback );
78  void set_enable_callback ( c_enable_func callback );
79  void set_enable_callback ( cpp_enable_func callback );
80  void allow_user ( const string& username, const string& password );
81  void allow_enable ( const string& password );
82  void deny_user ( const string& username );
83  void set_banner ( const string& banner );
84  void set_hostname ( const string& hostname );
85  void set_prompt ( const string& prompt );
86  void set_modestring ( const string& modestring );
87  int set_privilege ( int privilege );
88  int set_configmode ( int mode, const string& config_desc );
89  void regular ( int ( *callback ) () );
90  int print ( const char* format, ... );
91  void free_history();
92 
93 protected:
94  void free_command ( Command<CLI> *cmd );
95  int build_shortest ( Command<CLI>* commands );
96  int add_history ( char* cmd );
97  int parse_line ( char* line, char* words[], int max_words );
98  int find_command ( Command<CLI> *commands, int num_words, char* words[], int start_word, int filters[] );
99  int get_completions ( char* command, char** completions, int max_completions );
100  void clear_line ( char* cmd, int l, int cursor );
101  int pass_matches ( string pass, string tried_pass );
102  void show_prompt ();
103  int _print ( int print_mode, const char* format, va_list ap );
104  int internal_enable ( UNUSED ( char* command ), UNUSED ( char* argv[] ), UNUSED ( int argc ) );
105  int internal_disable ( UNUSED ( char* command ), UNUSED ( char* argv[] ), UNUSED ( int argc ) );
106  int internal_help ( UNUSED ( char* command ), UNUSED ( char* argv[] ), UNUSED ( int argc ) );
107  int internal_whoami ( UNUSED ( char* command ), UNUSED ( char* argv[] ), UNUSED ( int argc ) );
108  int internal_history ( UNUSED ( char* command ), UNUSED ( char* argv[] ), UNUSED ( int argc ) );
109  int internal_quit ( UNUSED ( char* command ), UNUSED ( char* argv[] ), UNUSED ( int argc ) );
110  int internal_exit ( char* command, char* argv[], int argc );
111  int internal_dump ( char* command, char* argv[], int argc );
112  int int_configure_terminal ( UNUSED ( char* command ), UNUSED ( char* argv[] ), UNUSED ( int argc ) );
113  void unregister_all ( Command<CLI> *command );
114  int pager ();
115  // line editing and handling of special chars
116  void handle_telnet_option ();
117  int get_input ( unsigned char& c );
118  void check_enable ( const char* pass );
119  void check_user_auth ( const string& username, const string& password );
120  void delete_backwards ( const unsigned char c );
121  void prompt_user ();
122  void redraw_line ();
123  void clear_line ();
124  void clear_to_eol ();
125  bool try_logout ();
126  void leave_config_mode ();
127  void list_completions ();
128  void do_history ( const unsigned char& c );
129  void cursor_left ();
130  void cursor_right ();
131  void jump_start_of_line ();
132  void jump_end_of_line ();
133  bool append ( const unsigned char& c );
134  void insert ( const unsigned char& c );
135  bool check_pager ();
136  unsigned char map_esc ();
137  // Variables
139  int my_sock;
140  int length; // length of current input line
141  int cursor; // cursor position within input line
142  char* cmd; // content of current input line
143  string username; // login name of user
145 #ifndef _MSC_VER
146  struct termios OldModes;
147 #endif
148 };
149 
150 }; // namespace LIBCLI
151 
152 #endif
cpp_enable_func cpp_enable_callback
Definition: libcli.hxx:66
int print(const char *format,...)
Definition: libcli.cxx:2187
int mode
Definition: libcli.hxx:55
struct termios OldModes
Definition: libcli.hxx:146
int cursor
Definition: libcli.hxx:141
int(CLI::* cpp_auth_func)(const string &, const string &)
Definition: libcli.hxx:42
int int_configure_terminal(UNUSED(char *command), UNUSED(char *argv[]), UNUSED(int argc))
Definition: libcli.cxx:549
unp users
Definition: libcli.hxx:58
void leave_config_mode()
Definition: libcli.cxx:1658
int(* regular_callback)()
Definition: libcli.hxx:62
void jump_start_of_line()
Definition: libcli.cxx:1812
string prompt
Definition: libcli.hxx:51
void unregister_all(Command< CLI > *command)
Definition: libcli.cxx:673
void set_auth_callback(c_auth_func callback)
Definition: libcli.cxx:69
int parse_line(char *line, char *words[], int max_words)
Definition: libcli.cxx:763
void set_prompt(const string &prompt)
Definition: libcli.cxx:175
int state
Definition: libcli.hxx:56
int completion_callback
Definition: libcli.hxx:46
string modestring
Definition: libcli.hxx:53
const int MAX_HISTORY
Definition: common.hxx:83
int internal_disable(UNUSED(char *command), UNUSED(char *argv[]), UNUSED(int argc))
Definition: libcli.cxx:437
int internal_whoami(UNUSED(char *command), UNUSED(char *argv[]), UNUSED(int argc))
Definition: libcli.cxx:477
#define UNUSED(d)
Definition: clitest.cxx:17
int pager()
Definition: libcli.cxx:2135
void jump_end_of_line()
Definition: libcli.cxx:1827
int find_command(Command< CLI > *commands, int num_words, char *words[], int start_word, int filters[])
Definition: libcli.cxx:835
void delete_backwards(const unsigned char c)
Definition: libcli.cxx:1493
int internal_enable(UNUSED(char *command), UNUSED(char *argv[]), UNUSED(int argc))
Definition: libcli.cxx:410
string username
Definition: libcli.hxx:143
int file(FILE *fh, int privilege, int mode)
Definition: libcli.cxx:2079
void clear_line()
Definition: libcli.cxx:1605
void insert(const unsigned char &c)
Definition: libcli.cxx:1859
bool check_pager()
Definition: libcli.cxx:2171
int get_input(unsigned char &c)
Definition: libcli.cxx:1370
void free_command(Command< CLI > *cmd)
Definition: libcli.cxx:352
void prompt_user()
Definition: libcli.cxx:1556
int privilege
Definition: libcli.hxx:54
int loop()
Definition: libcli.cxx:1896
int(* c_auth_func)(const string &, const string &)
Definition: libcli.hxx:41
void register_command(Command< CLI > *command, Command< CLI > *parent=0)
Definition: libcli.cxx:293
int build_shortest(Command< CLI > *commands)
Definition: libcli.cxx:185
int(* c_enable_func)(const string &)
Definition: libcli.hxx:43
void redraw_line()
Definition: libcli.cxx:1588
void show_prompt()
Definition: libcli.cxx:1306
bool try_logout()
Definition: libcli.cxx:1644
int set_configmode(int mode, const string &config_desc)
Definition: libcli.cxx:262
cpp_auth_func cpp_auth_callback
Definition: libcli.hxx:64
string enable_password
Definition: libcli.hxx:48
void set_modestring(const string &modestring)
Definition: libcli.cxx:252
bool append(const unsigned char &c)
Definition: libcli.cxx:1841
int internal_help(UNUSED(char *command), UNUSED(char *argv[]), UNUSED(int argc))
Definition: libcli.cxx:451
void set_enable_callback(c_enable_func callback)
Definition: libcli.cxx:91
void handle_telnet_option()
Definition: libcli.cxx:1353
void set_banner(const string &banner)
Definition: libcli.cxx:155
void check_enable(const char *pass)
Definition: libcli.cxx:1404
CLI(int fd)
Definition: libcli.cxx:567
int get_completions(char *command, char **completions, int max_completions)
Definition: libcli.cxx:1113
void clear_to_eol()
Definition: libcli.cxx:1620
void do_history(const unsigned char &c)
Definition: libcli.cxx:1714
string hostname
Definition: libcli.hxx:52
int add_history(char *cmd)
Definition: libcli.cxx:710
void check_user_auth(const string &username, const string &password)
Definition: libcli.cxx:1447
int internal_dump(char *command, char *argv[], int argc)
string banner
Definition: libcli.hxx:47
char * history[MAX_HISTORY]
Definition: libcli.hxx:49
int in_history
Definition: libcli.hxx:144
int internal_exit(char *command, char *argv[], int argc)
Definition: libcli.cxx:525
int run_command(char *command)
Definition: libcli.cxx:1062
int length
Definition: libcli.hxx:140
c_auth_func auth_callback
Definition: libcli.hxx:63
Command< CLI > * commands
Definition: libcli.hxx:59
void allow_enable(const string &password)
Definition: libcli.cxx:130
bool showprompt
Definition: libcli.hxx:50
c_enable_func enable_callback
Definition: libcli.hxx:65
int internal_quit(UNUSED(char *command), UNUSED(char *argv[]), UNUSED(int argc))
Definition: libcli.cxx:511
void cursor_left()
Definition: libcli.cxx:1782
void destroy()
Definition: libcli.cxx:667
int _print(int print_mode, const char *format, va_list ap)
void deny_user(const string &username)
Definition: libcli.cxx:140
int internal_history(UNUSED(char *command), UNUSED(char *argv[]), UNUSED(int argc))
Definition: libcli.cxx:490
int(CLI::* cpp_enable_func)(const string &)
Definition: libcli.hxx:44
int set_privilege(int privilege)
Definition: libcli.cxx:235
void cursor_right()
Definition: libcli.cxx:1797
int pass_matches(string pass, string tried_pass)
Definition: libcli.cxx:1284
void list_completions()
Definition: libcli.cxx:1675
char * cmd
Definition: libcli.hxx:142
void free_history()
Definition: libcli.cxx:748
bool from_socket
Definition: libcli.hxx:138
void allow_user(const string &username, const string &password)
Definition: libcli.cxx:113
unsigned char map_esc()
Definition: libcli.cxx:1326
void set_hostname(const string &hostname)
Definition: libcli.cxx:165
Client client
Definition: libcli.hxx:57
std::map< string, string >::iterator unp_it
Definition: libcli.hxx:40
int unregister_command(char *command)
Definition: libcli.cxx:374
void regular(int(*callback)())
Definition: libcli.cxx:1271
int my_sock
Definition: libcli.hxx:139
std::map< string, string > unp
Definition: libcli.hxx:39