[Freeswitch-svn] [commit] r8179 - in freeswitch/trunk/src: . include mod/applications/mod_commands

Freeswitch SVN anthm at freeswitch.org
Wed Apr 23 13:38:13 EDT 2008


Author: anthm
Date: Wed Apr 23 13:38:13 2008
New Revision: 8179

Modified:
   freeswitch/trunk/src/include/switch_core.h
   freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c
   freeswitch/trunk/src/switch_console.c
   freeswitch/trunk/src/switch_core_sqldb.c

Log:
dunno, some stuff...

Modified: freeswitch/trunk/src/include/switch_core.h
==============================================================================
--- freeswitch/trunk/src/include/switch_core.h	(original)
+++ freeswitch/trunk/src/include/switch_core.h	Wed Apr 23 13:38:13 2008
@@ -1601,6 +1601,7 @@
 SWITCH_DECLARE(uint32_t) switch_core_max_dtmf_duration(uint32_t duration);
 SWITCH_DECLARE(uint32_t) switch_core_default_dtmf_duration(uint32_t duration);
 SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string);
+SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string);
 
 ///\}
 

Modified: freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c	Wed Apr 23 13:38:13 2008
@@ -1885,7 +1885,7 @@
 	return 0;
 }
 
-#define COMPLETE_SYNTAX "add <word>|del [<word>|all]"
+#define COMPLETE_SYNTAX "add <word>|del [<word>|*]"
 SWITCH_STANDARD_API(complete_function)
 {
 
@@ -1900,7 +1900,23 @@
 	return SWITCH_STATUS_SUCCESS;
 }
 
-#define SHOW_SYNTAX "codec|application|api|dialplan|file|timer|calls|channels"
+
+#define ALIAS_SYNTAX "add <alias> <command> | del [<alias>|*]"
+SWITCH_STANDARD_API(alias_function)
+{
+
+	switch_status_t status;
+
+	if ((status = switch_console_set_alias(cmd)) == SWITCH_STATUS_SUCCESS) {
+		stream->write_function(stream, "+OK\n");
+	} else {
+		stream->write_function(stream, "-USAGE: %s\n", ALIAS_SYNTAX);
+	}
+	
+	return SWITCH_STATUS_SUCCESS;
+}
+
+#define SHOW_SYNTAX "codec|application|api|dialplan|file|timer|calls|channels|aliases|complete"
 SWITCH_STANDARD_API(show_function)
 {
 	char sql[1024];
@@ -1951,6 +1967,10 @@
 		sprintf(sql, "select * from calls order by created_epoch");
 	} else if (!strcasecmp(command, "channels")) {
 		sprintf(sql, "select * from channels order by created_epoch");
+	} else if (!strcasecmp(command, "aliases")) {
+		sprintf(sql, "select * from aliases order by alias");
+	} else if (!strcasecmp(command, "complete")) {
+		sprintf(sql, "select * from complete order by a1,a2,a3,a4,a5,a6,a7,a8,a9,a10");
 	} else if (!strncasecmp(command, "help", 4)) {
 		char *cmdname = NULL;
 
@@ -2280,6 +2300,7 @@
 	SWITCH_ADD_API(commands_api_interface, "break", "Break", break_function, BREAK_SYNTAX);
 	SWITCH_ADD_API(commands_api_interface, "show", "Show", show_function, SHOW_SYNTAX);
 	SWITCH_ADD_API(commands_api_interface, "complete", "Complete", complete_function, COMPLETE_SYNTAX);
+	SWITCH_ADD_API(commands_api_interface, "alias", "Alias", alias_function, ALIAS_SYNTAX);
 	SWITCH_ADD_API(commands_api_interface, "status", "status", status_function, "");
 	SWITCH_ADD_API(commands_api_interface, "uuid_bridge", "uuid_bridge", uuid_bridge_function, "");
 	SWITCH_ADD_API(commands_api_interface, "uuid_setvar", "uuid_setvar", uuid_setvar_function, SETVAR_SYNTAX);

Modified: freeswitch/trunk/src/switch_console.c
==============================================================================
--- freeswitch/trunk/src/switch_console.c	(original)
+++ freeswitch/trunk/src/switch_console.c	Wed Apr 23 13:38:13 2008
@@ -154,9 +154,47 @@
 	return ret ? SWITCH_STATUS_FALSE : SWITCH_STATUS_SUCCESS;
 }
 
-static int switch_console_process(char *cmd)
+static int alias_callback(void *pArg, int argc, char **argv, char **columnNames)
 {
-	char *arg = NULL;
+	char **r = (char **) pArg;
+	*r = strdup(argv[0]);
+	return -1;
+}
+
+char *expand_alias(char *cmd, char *arg)
+{
+	char *errmsg = NULL;
+	char *r = NULL;
+	char *sql;
+	char *exp = NULL;
+	switch_core_db_t *db = switch_core_db_handle();
+	
+	sql = switch_mprintf("select command from aliases where alias='%s'", cmd);
+	switch_core_db_exec(db, sql, alias_callback, &r, &errmsg);
+
+	if (errmsg) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg);
+		free(errmsg);
+	}
+
+	if (r) {
+		if (arg) {
+			exp = switch_mprintf("%s %s", r, arg);
+			free(r);
+		} else {
+			exp = r;
+		}
+	} else {
+		exp = cmd;
+	}
+
+	switch_core_db_close(db);
+	return exp;
+}
+
+static int switch_console_process(char *cmd, int rec)
+{
+	char *arg = NULL, *alias = NULL;
 	switch_stream_handle_t stream = { 0 };
 
 	if (!strcmp(cmd, "shutdown") || !strcmp(cmd, "...")) {
@@ -175,6 +213,12 @@
 		*arg++ = '\0';
 	}
 
+	if (!rec && (alias = expand_alias(cmd, arg)) && alias != cmd) {
+		int r = switch_console_process(alias, ++rec);
+		free(alias);
+		return r;
+	}
+
 	SWITCH_STANDARD_STREAM(stream);
 	if (stream.data) {
 		if (switch_api_execute(cmd, arg, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
@@ -268,7 +312,7 @@
     }
 	
 	cmd = strdup(c);
-	switch_console_process(cmd);
+	switch_console_process(cmd, 0);
 	free(cmd);
 
     return CC_REDISPLAY;
@@ -353,7 +397,7 @@
 				}
 				assert(cmd != NULL);
 				history(myhistory, &ev, H_ENTER, line);
-				running = switch_console_process(cmd);
+				running = switch_console_process(cmd, 0);
 				el_deletestr(el, strlen(foo)+1);
 				memset(foo, 0, strlen(foo));
 				free(cmd);
@@ -528,7 +572,7 @@
 				for(x = 0; x < 10; x++) {
 					mystream.write_function(&mystream, "'%s'%s", switch_str_nil(argv[x+1]), x == 9 ? ")" : ", ");
 				}
-				switch_core_db_persistant_execute(db, mystream.data, 1);
+				switch_core_db_persistant_execute(db, mystream.data, 5);
 				status = SWITCH_STATUS_SUCCESS;
 			} else if (!strcasecmp(argv[0], "del")) {
 				char *what = argv[1];
@@ -555,6 +599,46 @@
 }
 
 
+SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string)
+{
+	char *mydata = NULL, *argv[3] = {0};
+	int argc;
+	switch_status_t status = SWITCH_STATUS_FALSE;
+	
+	if (string && (mydata = strdup(string))) {
+		if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) >= 2) {
+			switch_core_db_t *db = switch_core_db_handle();
+			char *sql = NULL;
+
+			if (!strcasecmp(argv[0], "add") && argc == 3) {
+				sql = switch_mprintf("delete from aliases where alias='%q'", argv[1]);
+				switch_core_db_persistant_execute(db, sql, 5);
+				switch_safe_free(sql);
+				sql = switch_mprintf("insert into aliases (alias, command) values ('%q','%q')", argv[1], argv[2]);
+				switch_core_db_persistant_execute(db, sql, 5);
+				status = SWITCH_STATUS_SUCCESS;
+			} else if (!strcasecmp(argv[0], "del") && argc == 2) {
+				char *what = argv[1];
+                if (!strcasecmp(what, "*")) {
+					switch_core_db_persistant_execute(db, "delete from aliases", 1);
+				} else {
+					sql = switch_mprintf("delete from aliases where alias='%q'", argv[1]);
+					switch_core_db_persistant_execute(db, sql, 5);
+				}
+				status = SWITCH_STATUS_SUCCESS;
+			}
+			switch_safe_free(sql);
+			switch_core_db_close(db);
+		}
+	}
+	
+	switch_safe_free(mydata);
+
+	return status;
+
+}
+
+
 SWITCH_DECLARE(void) switch_console_loop(void)
 {
 	switch_thread_t *thread;
@@ -712,7 +796,7 @@
 		}
 
 		if (cmd[0]) {
-			running = switch_console_process(cmd);
+			running = switch_console_process(cmd, 0);
 		}
 	}
 }

Modified: freeswitch/trunk/src/switch_core_sqldb.c
==============================================================================
--- freeswitch/trunk/src/switch_core_sqldb.c	(original)
+++ freeswitch/trunk/src/switch_core_sqldb.c	Wed Apr 23 13:38:13 2008
@@ -380,6 +380,12 @@
 			"   a10 VARCHAR(255)\n" 
 			");\n";
 
+		char create_alias_sql[] =
+			"CREATE TABLE aliases (\n"
+			"   alias  VARCHAR(255),\n" 
+			"   command  VARCHAR(255)\n" 
+			");\n";
+
 		char create_channels_sql[] =
 			"CREATE TABLE channels (\n"
 			"   uuid  VARCHAR(255),\n"
@@ -440,6 +446,7 @@
 		switch_core_db_exec(sql_manager.db, "PRAGMA temp_store=MEMORY;", NULL, NULL, NULL);
 
 		switch_core_db_test_reactive(sql_manager.db, "select a1 from complete", "DROP TABLE complete", create_complete_sql);
+		switch_core_db_test_reactive(sql_manager.db, "select alias from aliases", "DROP TABLE aliases", create_alias_sql);
 		switch_core_db_exec(sql_manager.db, create_channels_sql, NULL, NULL, NULL);
 		switch_core_db_exec(sql_manager.db, create_calls_sql, NULL, NULL, NULL);
 		switch_core_db_exec(sql_manager.db, create_interfaces_sql, NULL, NULL, NULL);



More information about the Freeswitch-svn mailing list