[Freeswitch-branches] [commit] r3238 - freeswitch/branches/knhor/trunk/src/mod/applications/mod_commands

Freeswitch SVN knhor at freeswitch.org
Mon Oct 30 23:39:03 EST 2006


Author: knhor
Date: Mon Oct 30 23:39:01 2006
New Revision: 3238

Modified:
   freeswitch/branches/knhor/trunk/src/mod/applications/mod_commands/mod_commands.c

Log:
make the new formatting callback work for all use caes

Modified: freeswitch/branches/knhor/trunk/src/mod/applications/mod_commands/mod_commands.c
==============================================================================
--- freeswitch/branches/knhor/trunk/src/mod/applications/mod_commands/mod_commands.c	(original)
+++ freeswitch/branches/knhor/trunk/src/mod/applications/mod_commands/mod_commands.c	Mon Oct 30 23:39:01 2006
@@ -58,8 +58,8 @@
 	switch_core_measure_time(switch_core_uptime(), &duration);
 
 	if (stream->event) {
-        http = switch_event_get_header(stream->event, "http-host");
-    }
+		http = switch_event_get_header(stream->event, "http-host");
+	}
 
 	if (http || (cmd && strstr(cmd, "html"))) {
 		html = 1;
@@ -139,23 +139,24 @@
 	
 }
 
-
 static switch_status_t load_function(char *mod, switch_core_session_t *session, switch_stream_handle_t *stream)
 {
 
 	if (session) {
 		return SWITCH_STATUS_FALSE;
 	}
+
 	if (switch_strlen_zero(mod)) {
 		stream->write_function(stream, "USAGE: %s\n", load_api_interface.syntax);
 		return SWITCH_STATUS_SUCCESS;
 	}
+
 	switch_loadable_module_load_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) mod);
 	stream->write_function(stream, "OK\n");
+
 	return SWITCH_STATUS_SUCCESS;
 }
 
-
 static switch_status_t reload_function(char *mod, switch_core_session_t *session, switch_stream_handle_t *stream)
 {
 	const char *err;
@@ -170,6 +171,7 @@
 	}
 	
 	stream->write_function(stream, "OK [%s]\n", err);
+
 	return SWITCH_STATUS_SUCCESS;
 }
 
@@ -195,7 +197,6 @@
 	return SWITCH_STATUS_SUCCESS;
 }
 
-
 static switch_status_t transfer_function(char *cmd, switch_core_session_t *isession, switch_stream_handle_t *stream)
 {
 	switch_core_session_t *session = NULL;
@@ -234,8 +235,6 @@
 	return SWITCH_STATUS_SUCCESS;
 }
 
-
-
 static switch_status_t uuid_bridge_function(char *cmd, switch_core_session_t *isession, switch_stream_handle_t *stream)
 {
 	char *argv[4] = {0};
@@ -258,8 +257,6 @@
 	return SWITCH_STATUS_SUCCESS;
 }
 
-
-
 static switch_status_t pause_function(char *cmd, switch_core_session_t *isession, switch_stream_handle_t *stream)
 {
 	switch_core_session_t *session = NULL;
@@ -385,13 +382,29 @@
 	char *http;
 	uint32_t count;
 	int print_title;
+	int format;
 };
 
+static void show_callback_format_help(switch_stream_handle_t *stream, int column, char *coldata)
+{
+	if(stream != NULL && coldata != NULL) {
+		switch(column) {
+			case 0:
+			case 2:
+				stream->write_function(stream, "%s\n", coldata);
+				break;
+			case 1:
+				stream->write_function(stream, "\t%s ", coldata);
+				break;
+		}
+	}
+}
+
 static int show_callback(void *pArg, int argc, char **argv, char **columnNames){
 	struct holder *holder = (struct holder *) pArg;
 	int x;
 
-
+	// column headers
 	if (holder->print_title && holder->count == 0) {
 		if (holder->http) {
 			holder->stream->write_function(holder->stream, "\n<tr>");
@@ -402,19 +415,20 @@
 				holder->stream->write_function(holder->stream, "<td>");
 				holder->stream->write_function(holder->stream, "<b>%s</b>%s", columnNames[x], x == (argc - 1) ? "</td></tr>\n" : "</td><td>");
 			} else {
-				switch(x) {
-					case 0:	// column 0
-					case 2:	// column 2
-						holder->stream->write_function(holder->stream, "%s\n", argv[x]);
+				switch(holder->format)
+				{
+					case 1: // help command formatting
+						show_callback_format_help(holder->stream,x,argv[x]);
 						break;
-					case 1:	// column 1
-						holder->stream->write_function(holder->stream, "\t%s ", argv[x]);
+					default: // generic formatting
+						holder->stream->write_function(holder->stream, "%s%s", columnNames[x], x == (argc - 1) ? "\n" : ",");
 						break;
 				}
 			}
 		}
 	} 
 
+	// column rows
 	if (holder->http) {
 		holder->stream->write_function(holder->stream, "<tr bgcolor=%s>", holder->count % 2 == 0 ? "eeeeee" : "ffffff");
 	}
@@ -424,21 +438,18 @@
 			holder->stream->write_function(holder->stream, "<td>");
 			holder->stream->write_function(holder->stream, "%s%s", argv[x], x == (argc - 1) ? "</td></tr>\n" : "</td><td>");
 		} else {
-			switch(x) {
-				case 0:	// column 0
-				case 2:	// column 2
-					holder->stream->write_function(holder->stream, "%s\n", argv[x]);
+			switch(holder->format)
+			{
+				case 1: // help command formatting
+					show_callback_format_help(holder->stream,x,argv[x]);
 					break;
-				case 1:	// column 1
-					holder->stream->write_function(holder->stream, "\t%s ", argv[x]);
+				default:	// generic formatting
+					holder->stream->write_function(holder->stream, "%s%s", argv[x], x == (argc - 1) ? "\n" : ",");
 					break;
 			}
 		}
 	}
-	
 
-
-
 	holder->count++;
 	return 0;
 }
@@ -456,43 +467,41 @@
 	}
 
 	if (stream->event) {
-        holder.http = switch_event_get_header(stream->event, "http-host");
-    } 
+		holder.http = switch_event_get_header(stream->event, "http-host");
+	} 
 
 	holder.print_title = 1;
 
-    if (!cmd) {
-        sprintf (sql, "select * from interfaces");
-    }
-    else if ( !strcmp(cmd,"codec") || !strcmp(cmd,"dialplan") || 
-              !strcmp(cmd,"file") || !strcmp(cmd,"timer") 
-            ) {
-        sprintf (sql, "select type, name from interfaces where type = '%s'", cmd);
-    }
-    else if (!strcmp(cmd,"application") || !strcmp(cmd,"api")) {
-        sprintf (sql, "select name, description, syntax from interfaces where type = '%s'", cmd);
-    }
-    else if ( !strcmp(cmd,"calls")) {
-        sprintf (sql, "select * from calls");
-    }
-    else if ( !strcmp(cmd,"channels")) {
-        sprintf (sql, "select * from channels");
-    }
-    else if (!strncasecmp(cmd, "help", 4)) {
+	// If you changes the field qty or order of any of these select
+	// statmements, you must also change show_callback and friends to match!
+	if (!cmd) {
+		sprintf (sql, "select * from interfaces");
+	} else if ( !strcmp(cmd,"codec") || !strcmp(cmd,"dialplan") || !strcmp(cmd,"file") || !strcmp(cmd,"timer")) {
+		sprintf (sql, "select type, name from interfaces where type = '%s'", cmd);
+	} else if (!strcmp(cmd,"application") || !strcmp(cmd,"api")) {
+		sprintf (sql, "select name, description, syntax from interfaces where type = '%s'", cmd);
+	} else if ( !strcmp(cmd,"calls")) {
+		sprintf (sql, "select * from calls");
+	} else if ( !strcmp(cmd,"channels")) {
+		sprintf (sql, "select * from channels");
+	} else if (!strncasecmp(cmd, "help", 4)) {
 		char *cmdname = NULL;
+
 		help = 1;
 		holder.print_title = 0;
+		holder.format = 1;
+
+		// If you change one, you must change the other!
 		if ((cmdname = strchr(cmd, ' ')) != 0) {
 			*cmdname++ = '\0';
-			sprintf (sql, "select name, syntax, description from interfaces where type = 'api' and name = '%s'", cmdname);
+			sprintf (sql, "select description, name, syntax from interfaces where type = 'api' and name = '%s'", cmdname);
 		} else {
 			sprintf (sql, "select description, name, syntax from interfaces where type = 'api'");
 		}
-    }
-    else {
+	} else {
 		stream->write_function(stream, "USAGE: %s\n", show_api_interface.syntax);
-        return SWITCH_STATUS_SUCCESS;
-    }
+		return SWITCH_STATUS_SUCCESS;
+	}
     
 	holder.stream = stream;
 	holder.count = 0;
@@ -533,11 +542,16 @@
 		sprintf (showcmd, "help %s", cmd);
 	}
 
-	if (all)
+	if (all) {
 		stream->write_function(stream, "\nValid Commands:\n\n");
+	}
+
 	show_function(showcmd, session, stream);
-	if (all)
+
+	if (all) {
 		stream->write_function(stream, "version\n" "shutdown - stop the program\n");
+	}
+
 	return SWITCH_STATUS_SUCCESS;
 }
 
@@ -630,7 +644,6 @@
 	/*.next */ &kill_api_interface
 };
 
-
 static const switch_loadable_module_interface_t mod_commands_module_interface = {
 	/*.module_name */ modname,
 	/*.endpoint_interface */ NULL,
@@ -641,15 +654,11 @@
 	/*.api_interface */ &originate_api_interface
 };
 
-
 SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename)
 {
-
 	/* connect my internal structure to the blank pointer passed to me */
 	*module_interface = &mod_commands_module_interface;
 
-
 	/* indicate that the module should continue to be loaded */
 	return SWITCH_STATUS_SUCCESS;
 }
-



More information about the Freeswitch-branches mailing list