[asterisk-commits] eliel: branch group/data_api_gsoc2009 r210557 - /team/group/data_api_gsoc2009...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Aug 5 11:29:28 CDT 2009


Author: eliel
Date: Wed Aug  5 11:29:24 2009
New Revision: 210557

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=210557
Log:
CLI command 'data get'.


Modified:
    team/group/data_api_gsoc2009/main/data.c

Modified: team/group/data_api_gsoc2009/main/data.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/main/data.c?view=diff&rev=210557&r1=210556&r2=210557
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Wed Aug  5 11:29:24 2009
@@ -35,7 +35,7 @@
 #include "asterisk/data.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/xml.h"
-#include "asterisk/linkedlists.h"
+#include "asterisk/cli.h"
 
 #define NUM_DATA_NODE_BUCKETS	59
 #define NUM_DATA_RESULT_BUCKETS 59
@@ -147,6 +147,7 @@
 } root_data;
 
 static void __ast_data_print_debug(const struct ast_data *root, uint32_t depth);
+static void __data_result_print_cli(int fd, const struct ast_data *root, uint32_t depth);
 
 /*!
  * \internal
@@ -2286,6 +2287,165 @@
 	return 0;
 }
 
+/*!
+ * \internal
+ * \brief Print a node to the CLI.
+ * \param[in] fd The CLI file descriptor.
+ * \param[in] node The node to print.
+ * \param[in] depth The actual node depth in the tree.
+ */
+static void data_result_print_cli_node(int fd, const struct ast_data *node, uint32_t depth)
+{
+	int i;
+	struct ast_str *tabs;
+
+	tabs = ast_str_create(depth * 10 + 1);
+	if (!tabs) {
+		return;
+	}
+	ast_str_reset(tabs);
+	for (i = 0; i < depth; i++) {
+		ast_str_append(&tabs, 0, "    ");
+	}
+
+	switch (node->type) {
+	case AST_DATA_POINTER:
+		ast_cli(fd, "%s%s (pointer): %p\n", ast_str_buffer(tabs),
+				node->name, node->payload.ptr);
+		break;
+	case AST_DATA_STRING:
+		ast_cli(fd, "%s%s (string)[%u]: \"%s\"\n", ast_str_buffer(tabs),
+				node->name,
+				(unsigned int) strlen(node->payload.str),
+				node->payload.str);
+		break;
+	case AST_DATA_CONTAINER:
+		ast_cli(fd, "%s%s (container)\n", ast_str_buffer(tabs),
+				node->name);
+		__data_result_print_cli(fd, node, depth + 1);
+		break;
+	case AST_DATA_INTEGER:
+		ast_cli(fd, "%s%s (integer): %d\n", ast_str_buffer(tabs),
+				node->name,
+				node->payload.sint);
+		break;
+	case AST_DATA_UNSIGNED_INTEGER:
+		ast_cli(fd, "%s%s (unsigned integer): %u\n", ast_str_buffer(tabs),
+				node->name,
+				node->payload.uint);
+		break;
+	case AST_DATA_DOUBLE:
+		ast_cli(fd, "%s%s (double): %lf\n", ast_str_buffer(tabs),
+				node->name,
+				node->payload.dbl);
+		break;
+	case AST_DATA_BOOLEAN:
+		ast_cli(fd, "%s%s (boolean): %s\n", ast_str_buffer(tabs),
+				node->name,
+				((node->payload.boolean) ? "True" : "False"));
+		break;
+	case AST_DATA_IPADDR:
+		ast_cli(fd, "%s%s (ipaddr): %s\n", ast_str_buffer(tabs),
+				node->name,
+				ast_inet_ntoa(node->payload.ipaddr));
+		break;
+	}
+
+	ast_free(tabs);
+}
+
+/*!
+ * \internal
+ * \brief Print out an ast_data tree to the CLI.
+ * \param[in] fd The CLI file descriptor.
+ * \param[in] root The root node of the tree.
+ * \param[in] depth Actual depth.
+ */
+static void __data_result_print_cli(int fd, const struct ast_data *root, uint32_t depth)
+{
+	struct ao2_iterator iter;
+	struct ast_data *node;
+
+	if (root->type == AST_DATA_CONTAINER) {
+		iter = ao2_iterator_init(root->children, 0);
+		while ((node = ao2_iterator_next(&iter))) {
+			data_result_print_cli_node(fd, node, depth + 1);
+			ao2_ref(node, -1);
+		}
+	} else {
+		data_result_print_cli_node(fd, root, depth);
+	}
+}
+
+/*!
+ * \internal
+ * \brief
+ * \param[in] fd The CLI file descriptor.
+ * \param[in] root The root node of the tree.
+ */
+static void data_result_print_cli(int fd, const struct ast_data *root)
+{
+	ast_cli(fd, "%s (container)\n", root->name);
+	__data_result_print_cli(fd, root, 0);
+}
+
+/*!
+ * \internal
+ * \brief Handle the CLI command "data get".
+ */
+static char *handle_cli_data_get(struct ast_cli_entry *e, int cmd,
+		struct ast_cli_args *a)
+{
+	struct ast_data_query query = {
+		.version = AST_DATA_QUERY_VERSION
+	};
+	struct ast_data *tree;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "data get";
+		e->usage = ""
+			"Usage: data get <path> [<search> [<filter>]]\n"
+			"   get the tree based on a path.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc < e->args + 1) {
+		return CLI_SHOWUSAGE;
+	}
+
+	query.path = (char *) a->argv[e->args];
+
+	if (a->argc > e->args + 1) {
+		query.search = (char *) a->argv[e->args + 1];
+	}
+
+	if (a->argc > e->args + 2) {
+		query.filter = (char *) a->argv[e->args + 2];
+	}
+
+	tree = ast_data_get(&query);
+	if (!tree) {
+		return CLI_FAILURE;
+	}
+
+	data_result_print_cli(a->fd, tree);
+
+	ast_data_free(tree);
+
+	return CLI_SUCCESS;
+}
+
+/*!
+ * \internal
+ * \brief Data API CLI commands.
+ */
+static struct ast_cli_entry cli_data[] = {
+	AST_CLI_DEFINE(handle_cli_data_get, "Data API get"),
+};
+
 int ast_data_init(void)
 {
 	ast_rwlock_init(&root_data.lock);
@@ -2295,5 +2455,7 @@
 		return -1;
 	}
 
+	ast_cli_register_multiple(cli_data, ARRAY_LEN(cli_data));
+
 	return 0;
 }




More information about the asterisk-commits mailing list