[svn-commits] eliel: branch group/data_api_gsoc2009 r210560 - /team/group/data_api_gsoc2009...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Aug 5 13:22:01 CDT 2009


Author: eliel
Date: Wed Aug  5 13:21:57 2009
New Revision: 210560

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=210560
Log:
Add colors to the "data get" CLI command.
Fix a leak in the filtering.


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=210560&r1=210559&r2=210560
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Wed Aug  5 13:21:57 2009
@@ -36,6 +36,7 @@
 #include "asterisk/astobj2.h"
 #include "asterisk/xml.h"
 #include "asterisk/cli.h"
+#include "asterisk/term.h"
 
 #define NUM_DATA_NODE_BUCKETS	59
 #define NUM_DATA_RESULT_BUCKETS 59
@@ -1671,6 +1672,9 @@
 		node->filter = filter_child;
 		root_provider->handler->get(search, node);
 		ast_module_unref(root_provider->module);
+		if (filter_child) {
+			ao2_ref(filter_child, -1);
+		}
 		return node;
 	}
 
@@ -2287,6 +2291,32 @@
 	return 0;
 }
 
+struct {
+	enum ast_data_type type;
+	int color;
+} data_result_color[] = {
+	{ AST_DATA_STRING, COLOR_CYAN },
+	{ AST_DATA_INTEGER, COLOR_RED },
+	{ AST_DATA_UNSIGNED_INTEGER, COLOR_RED },
+	{ AST_DATA_DOUBLE, COLOR_RED },
+	{ AST_DATA_BOOLEAN, COLOR_BRRED },
+	{ AST_DATA_CONTAINER, COLOR_GREEN },
+	{ AST_DATA_IPADDR, COLOR_BROWN },
+	{ AST_DATA_POINTER, COLOR_YELLOW },
+};
+
+static int data_result_get_color(enum ast_data_type type)
+{
+	int i;
+	for (i = 0; i < ARRAY_LEN(data_result_color); i++) {
+		if (data_result_color[i].type == type) {
+			return data_result_color[i].color;
+		}
+	}
+
+	return COLOR_BLUE;
+}
+
 /*!
  * \internal
  * \brief Print a node to the CLI.
@@ -2297,7 +2327,7 @@
 static void data_result_print_cli_node(int fd, const struct ast_data *node, uint32_t depth)
 {
 	int i;
-	struct ast_str *tabs;
+	struct ast_str *tabs, *output;
 
 	tabs = ast_str_create(depth * 10 + 1);
 	if (!tabs) {
@@ -2305,53 +2335,71 @@
 	}
 	ast_str_reset(tabs);
 	for (i = 0; i < depth; i++) {
-		ast_str_append(&tabs, 0, "    ");
-	}
+		ast_str_append(&tabs, 0, "  ");
+	}
+
+	output = ast_str_create(20);
+	if (!output) {
+		ast_free(tabs);
+		return;
+	}
+
+	ast_str_reset(output);
+	ast_term_color_code(&output, data_result_get_color(node->type), 0);
 
 	switch (node->type) {
 	case AST_DATA_POINTER:
-		ast_cli(fd, "%s%s (pointer): %p\n", ast_str_buffer(tabs),
+		ast_str_append(&output, 0, "%s%s: %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),
+		ast_str_append(&output, 0, "%s%s: \"%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),
+		ast_str_append(&output, 0, "%s%s\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),
+		ast_str_append(&output, 0, "%s%s: %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),
+		ast_str_append(&output, 0, "%s%s: %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),
+		ast_str_append(&output, 0, "%s%s: %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),
+		ast_str_append(&output, 0, "%s%s: %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),
+		ast_str_append(&output, 0, "%s%s: %s\n", ast_str_buffer(tabs),
 				node->name,
 				ast_inet_ntoa(node->payload.ipaddr));
 		break;
 	}
 
 	ast_free(tabs);
+
+	ast_term_color_code(&output, COLOR_WHITE, 0);
+
+	ast_cli(fd, "%s", ast_str_buffer(output));
+
+	ast_free(output);
+
+	if (node->type == AST_DATA_CONTAINER) {
+		__data_result_print_cli(fd, node, depth + 1);
+	}
 }
 
 /*!
@@ -2385,7 +2433,20 @@
  */
 static void data_result_print_cli(int fd, const struct ast_data *root)
 {
-	ast_cli(fd, "%s (container)\n", root->name);
+	struct ast_str *output;
+
+	/* print the initial node. */
+	output = ast_str_create(30);
+	if (!output) {
+		return;
+	}
+
+	ast_term_color_code(&output, data_result_get_color(root->type), 0);
+	ast_str_append(&output, 0, "%s (container)\n", root->name);
+	ast_term_color_code(&output, COLOR_WHITE, 0);
+	ast_cli(fd, "%s", ast_str_buffer(output));
+	ast_free(output);
+
 	__data_result_print_cli(fd, root, 0);
 }
 




More information about the svn-commits mailing list