Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add user context to cli_def.

* New void *user_context in the struct cli_def
* cli_set_context and cli_get_context functions
* Command for testing user context (in clitest)
* Added changelog entry (as well as a couple of lines to README)
* Increased version number from 1.9.5 to 1.9.6
  • Loading branch information...
commit 31bf5045781d3fee15f54b1840d5548bc89fd810 1 parent 85444cf
Teemu Karimerto authored committed
View
2  Makefile
@@ -4,7 +4,7 @@ PREFIX = /usr/local
MAJOR = 1
MINOR = 9
-REVISION = 5
+REVISION = 6
LIB = libcli.so
CC = gcc
View
3  README
@@ -94,6 +94,9 @@ cli_unregister_command(command).
You can use this to build dynamic command trees.
+It is possible to carry along a user-defined context to all command callbacks
+using cli_set_context(cli, context) and cli_get_context(cli) functions.
+
You are responsible for accepting a TCP connection, and for creating a
process or thread to run the cli. Once you are ready to process the
View
23 clitest.c
@@ -29,6 +29,11 @@
unsigned int regular_count = 0;
unsigned int debug_regular = 0;
+struct my_context {
+ int value;
+ char* message;
+};
+
#ifdef WIN32
typedef int socklen_t;
@@ -152,6 +157,13 @@ int cmd_debug_regular(struct cli_def *cli, UNUSED(const char *command), char *ar
return CLI_OK;
}
+int cmd_context(struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc))
+{
+ struct my_context *myctx = (struct my_context *)cli_get_context(cli);
+ cli_print(cli, "User context has a value of %d and message saying %s", myctx->value, myctx->message);
+ return CLI_OK;
+}
+
int check_auth(const char *username, const char *password)
{
if (strcasecmp(username, "fred") != 0)
@@ -206,6 +218,12 @@ int main()
}
#endif
+ // Prepare a small user context
+ char mymessage[] = "I contain user data!";
+ struct my_context myctx;
+ myctx.value = 5;
+ myctx.message = mymessage;
+
cli = cli_init();
cli_set_banner(cli, "libcli test environment");
cli_set_hostname(cli, "router");
@@ -244,6 +262,11 @@ int main()
cli_register_command(cli, c, "regular", cmd_debug_regular, PRIVILEGE_UNPRIVILEGED, MODE_EXEC,
"Enable cli_regular() callback debugging");
+ // Set user context and its command
+ cli_set_context(cli, (void*)&myctx);
+ cli_register_command(cli, NULL, "context", cmd_context, PRIVILEGE_UNPRIVILEGED, MODE_EXEC,
+ "Test a user-specified context");
+
cli_set_auth_callback(cli, check_auth);
cli_set_enable_callback(cli, check_enable);
// Test reading from a file
View
8 libcli.c
@@ -2328,3 +2328,11 @@ void cli_set_idle_timeout_callback(struct cli_def *cli, unsigned int seconds, in
void cli_telnet_protocol(struct cli_def *cli, int telnet_protocol) {
cli->telnet_protocol = !!telnet_protocol;
}
+
+void cli_set_context(struct cli_def *cli, void *context) {
+ cli->user_context = context;
+}
+
+void *cli_get_context(struct cli_def *cli) {
+ return cli->user_context;
+}
View
5 libcli.h
@@ -64,6 +64,7 @@ struct cli_def {
int (*idle_timeout_callback)(struct cli_def *);
time_t last_action;
int telnet_protocol;
+ void *user_context;
};
struct cli_filter {
@@ -120,6 +121,10 @@ void cli_set_idle_timeout_callback(struct cli_def *cli, unsigned int seconds, in
// Note that this is enabled by default and must be changed before cli_loop() is run.
void cli_telnet_protocol(struct cli_def *cli, int telnet_protocol);
+// Set/get user context
+void cli_set_context(struct cli_def *cli, void *context);
+void *cli_get_context(struct cli_def *cli);
+
#ifdef __cplusplus
}
#endif
View
7 libcli.spec
@@ -1,4 +1,4 @@
-Version: 1.9.5
+Version: 1.9.6
Summary: Cisco-like telnet command-line library
Name: libcli
Release: 1
@@ -36,6 +36,11 @@ rm -rf $RPM_BUILD_ROOT
%defattr(-, root, root)
%changelog
+* Tue Jun 5 2012 Teemu Karimerto <teemu.karimerto@steo.fi> 1.9.6-1
+- Added a user-definable context to struct cli_def
+- Added cli_set_context/cli_get_context for user context handling
+- Added a test for user context
+
* Mon Feb 1 2010 David Parrish <david@dparrish.com> 1.9.5-1
- Removed dependence on "quit" command
- Added cli_set_idle_timeout_callback() for custom timeout handling
Please sign in to comment.
Something went wrong with that request. Please try again.