Revision
15967
Author
mrene
Date
2009-12-15 12:26:06 -0600 (Tue, 15 Dec 2009)

Log Message

add console_complete_xml api

Modified Paths

Diff

Modified: freeswitch/trunk/src/include/switch_console.h (15966 => 15967)


--- freeswitch/trunk/src/include/switch_console.h	2009-12-15 14:58:20 UTC (rev 15966)
+++ freeswitch/trunk/src/include/switch_console.h	2009-12-15 18:26:06 UTC (rev 15967)
@@ -85,7 +85,7 @@
 																 const char *last_word, switch_console_callback_match_t **matches);
 SWITCH_DECLARE(void) switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val);
 SWITCH_DECLARE(void) switch_console_free_matches(switch_console_callback_match_t **matches);
-SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const char *last_word, FILE *console_out, switch_stream_handle_t *stream);
+SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const char *cursor, FILE *console_out, switch_stream_handle_t *stream, switch_xml_t xml);
 
 SWITCH_END_EXTERN_C
 #endif

Modified: freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c (15966 => 15967)


--- freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c	2009-12-15 14:58:20 UTC (rev 15966)
+++ freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c	2009-12-15 18:26:06 UTC (rev 15967)
@@ -785,11 +785,41 @@
 		}
 	}
 
-	switch_console_complete(cmd, cursor, NULL, stream);
+	switch_console_complete(cmd, cursor, NULL, stream, NULL);
 	return SWITCH_STATUS_SUCCESS;
 }
 
+SWITCH_STANDARD_API(console_complete_xml_function)
+{
+	const char *p, *cursor = NULL;
+	int c;
+	switch_xml_t xml = switch_xml_new("complete");
+	char *sxml;
 
+	if (zstr(cmd)) {
+		cmd = " ";
+	}
+	
+	if ((p = strstr(cmd, "c="))) {
+		p += 2;
+		c = atoi(p);
+		if ((p = strchr(p, ';'))) {
+			cmd = p + 1;
+			cursor = cmd + c;
+		}
+	}
+
+	switch_console_complete(cmd, cursor, NULL, NULL, xml);
+	
+	sxml = switch_xml_toxml(xml, SWITCH_TRUE);
+	stream->write_function(stream, "%s", sxml);
+	free(sxml);
+	
+	switch_xml_free(xml);
+	
+	return SWITCH_STATUS_SUCCESS;
+}
+
 SWITCH_STANDARD_API(eval_function)
 {
 	char *expanded;
@@ -3814,6 +3844,7 @@
 	SWITCH_ADD_API(commands_api_interface, "complete", "Complete", complete_function, COMPLETE_SYNTAX);
 	SWITCH_ADD_API(commands_api_interface, "cond", "Eval a conditional", cond_function, "<expr> ? <true val> : <false val>");
 	SWITCH_ADD_API(commands_api_interface, "console_complete", "", console_complete_function, "<line>");
+	SWITCH_ADD_API(commands_api_interface, "console_complete_xml", "", console_complete_xml_function, "<line>");
 	SWITCH_ADD_API(commands_api_interface, "create_uuid", "Create a uuid", uuid_function, UUID_SYNTAX);
 	SWITCH_ADD_API(commands_api_interface, "db_cache", "db cache management", db_cache_function, "status");
 	SWITCH_ADD_API(commands_api_interface, "domain_exists", "check if a domain exists", domain_exists_function, "<domain>");

Modified: freeswitch/trunk/src/switch_console.c (15966 => 15967)


--- freeswitch/trunk/src/switch_console.c	2009-12-15 14:58:20 UTC (rev 15966)
+++ freeswitch/trunk/src/switch_console.c	2009-12-15 18:26:06 UTC (rev 15967)
@@ -395,6 +395,8 @@
 	char partial[512];
 	FILE *out;
 	switch_stream_handle_t *stream;
+	switch_xml_t xml;
+	int xml_off;
 };
 
 static int comp_callback(void *pArg, int argc, char **argv, char **columnNames)
@@ -443,6 +445,9 @@
 		if (h->stream) {
 			h->stream->write_function(h->stream, "[%20s]\t", target);
 		}
+		if (h->xml) {
+			switch_xml_set_txt_d(switch_xml_add_child_d(h->xml, "match", h->xml_off++), target);
+		}
 
 		switch_copy_string(h->last, target, sizeof(h->last));
 		h->hits++;
@@ -524,7 +529,7 @@
 }
 
 
-SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const char *cursor, FILE *console_out, switch_stream_handle_t *stream)
+SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const char *cursor, FILE *console_out, switch_stream_handle_t *stream, switch_xml_t xml)
 {
 	switch_cache_db_handle_t *db = NULL;
 	char *sql = NULL;
@@ -550,6 +555,7 @@
 	
 	h.out = console_out;
 	h.stream = stream;
+	h.xml = xml;
 
 	if (pos > 0) {
 		*(buf + pos) = '\0';
@@ -671,6 +677,20 @@
 		} else if (h.hits > 1 && !zstr(h.partial)) {
 			h.stream->write_function(h.stream, "write=%d:%s", h.len, h.partial);
 		}
+	} 
+	
+	if (h.xml) {
+		switch_xml_t x_write = switch_xml_add_child_d(h.xml, "write", h.xml_off++);
+		char buf[32];
+		
+		snprintf(buf, sizeof(buf), "%d", h.len);
+		switch_xml_set_attr_d_buf(x_write, "length", buf);
+		
+		if (h.hits == 1 && !zstr(h.last)) {
+			switch_xml_set_txt_d(x_write, h.last);
+		} else if (h.hits > 1 && !zstr(h.partial)) {
+			switch_xml_set_txt_d(x_write, h.partial);
+		}
 	}
 
 #ifdef SWITCH_HAVE_LIBEDIT
@@ -841,7 +861,7 @@
 {
 	const LineInfo *lf = el_line(el);
 
-	return switch_console_complete(lf->buffer, lf->cursor, switch_core_get_console(), NULL);
+	return switch_console_complete(lf->buffer, lf->cursor, switch_core_get_console(), NULL, NULL);
 }