[asterisk-commits] rizzo: branch rizzo/astobj2 r47535 - in /team/rizzo/astobj2: include/asterisk...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Nov 13 04:04:26 MST 2006


Author: rizzo
Date: Mon Nov 13 05:04:26 2006
New Revision: 47535

URL: http://svn.digium.com/view/asterisk?view=rev&rev=47535
Log:
improve and make more consistent the user interface for the new cli api


Modified:
    team/rizzo/astobj2/include/asterisk/cli.h
    team/rizzo/astobj2/main/astobj2.c
    team/rizzo/astobj2/main/cli.c

Modified: team/rizzo/astobj2/include/asterisk/cli.h
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/include/asterisk/cli.h?view=diff&rev=47535&r1=47534&r2=47535
==============================================================================
--- team/rizzo/astobj2/include/asterisk/cli.h (original)
+++ team/rizzo/astobj2/include/asterisk/cli.h Mon Nov 13 05:04:26 2006
@@ -49,11 +49,10 @@
  * functionality of the cli handler. Called with the default arguments,
  * (argc >0), the handler simply implements the command as before.
  * A negative argc indicates one of the other functions, namely
- * generate the usage string, the full command, or implement the generator
- * (for this case, the last argument is cast to struct ast_cli_args
- * because we need more arguments to it).
- * As a trick to extend the interface without too much trouble,
- * argv[-1] points to a struct ast_cli_args.
+ * generate the usage string, the full command, or implement the generator.
+ * As a trick to extend the interface while being backward compatible,
+ * argv[-1] points to a struct ast_cli_args, and for the generator,
+ * argv[0] is really a pointer to a struct ast_cli_args.
  * The return string is obtained by casting the result to char *
  */
 enum ast_cli_fn {
@@ -106,7 +105,7 @@
 /* argument for new-style CLI handler */
 struct ast_cli_args {
 /*! \brief A command line entry */
-	struct ast_cli_entry *e;	/* pointer to the entry in use */
+	char fake[4];		/* a fake string, in the first position, for safety */
 	const char *line;	/* the current input line */
 	const char *word;	/* the word we want to complete */
 	int pos;		/* position of the word to complete */

Modified: team/rizzo/astobj2/main/astobj2.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/main/astobj2.c?view=diff&rev=47535&r1=47534&r2=47535
==============================================================================
--- team/rizzo/astobj2/main/astobj2.c (original)
+++ team/rizzo/astobj2/main/astobj2.c Mon Nov 13 05:04:26 2006
@@ -667,7 +667,7 @@
 
 static int test_new_cli(int fd, int argc, char *argv[])
 {
-	struct ast_cli_entry *e;
+	struct ast_cli_entry *e = (struct ast_cli_entry *)argv[-1];
 	struct ast_cli_args *a;
 
 	switch(argc) {
@@ -678,9 +678,9 @@
 		return (int)"astobj2 foo bar";
 
 	case CLI_GENERATE:
-		a = (struct ast_cli_args *)argv;
-		if (a->pos > a->e->args + 1)
-			return RESULT_SHOWUSAGE;
+		a = (struct ast_cli_args *)argv[0];
+		if (a->pos > e->args)	/* only extend one word */
+			return NULL;
 		if (a->n < 10) {
 			char *buf;
 			asprintf(&buf, "%d", a->n);
@@ -688,9 +688,11 @@
 		}
 		return NULL;
 	default:	/* regular handler */
-		e = (struct ast_cli_entry *)argv[-1];
+		/* we are guaranteed to be called with argc >= e->args; */
+		if (argc > e->args + 1)	/* we only accept one extra field */
+			return RESULT_SHOWUSAGE;
 	        ast_cli(fd, "this is test_new_cli %d for [%d] %s\n", argc, e->args, e->_full_cmd);
-		return 0;
+		return RESULT_SUCCESS;
 	}
 }
 static struct ast_cli_entry cli_astobj2[] = {

Modified: team/rizzo/astobj2/main/cli.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/main/cli.c?view=diff&rev=47535&r1=47534&r2=47535
==============================================================================
--- team/rizzo/astobj2/main/cli.c (original)
+++ team/rizzo/astobj2/main/cli.c Mon Nov 13 05:04:26 2006
@@ -1185,7 +1185,8 @@
 	int i, lf, ret = -1;
 
 	if (e->cmda[0] == NULL) {	/* new style entry, run the handler to init fields */
-		char *s = (char *)(e->handler(-1, CLI_CMD_STRING, NULL));
+		char *args[2] = { (char *)e, NULL };
+		char *s = (char *)(e->handler(-1, CLI_CMD_STRING, args+1));
 		char **dst = (char **)e->cmda;	/* need to cast as the entry is readonly */
 
 		s = ast_skip_blanks(s);
@@ -1199,7 +1200,7 @@
 			s = ast_skip_blanks(s);
 		}
 		*dst++ = NULL;
-		e->usage = (char *)(e->handler(-1, CLI_USAGE, NULL));
+		e->usage = (char *)(e->handler(-1, CLI_USAGE, args+1));
 		ast_verbose("running new-style CLI entry for %s\n", e->new_style_buf);
 		sleep(1);
 	}
@@ -1526,10 +1527,19 @@
 			if (e->generator)
 				ret = e->generator(matchstr, word, argindex, state);
 			else if (e->new_style_buf) {	/* new style command */
+				/* prepare fake arguments for the generator.
+				 * argv[-1] is the cli entry we use,
+				 * argv[0] is a pointer to the generator arguments,
+				 *   with a fake string '-' at the beginning so we can
+				 *   dereference it as a string with no trouble,
+				 *   and then the usual NULL terminator.
+				 */
 				struct ast_cli_args a = {
-					.e = e, .line = matchstr, .word = word,
+					.fake = "-",
+					.line = matchstr, .word = word,
 					.pos = argindex, .n = state };
-				ret = (char *)e->handler(-1, CLI_GENERATE, (char **)&a);
+				char *args[] = { (char *)e, (char *)&a, NULL };
+				ret = (char *)e->handler(-1, CLI_GENERATE, args + 1);
 			}
 			break;
 		}



More information about the asterisk-commits mailing list