Index: pbx.c
===================================================================
RCS file: /usr/cvsroot/asterisk/pbx.c,v
retrieving revision 1.205
diff -u -r1.205 pbx.c
--- pbx.c	21 Feb 2005 06:31:01 -0000	1.205
+++ pbx.c	22 Feb 2005 22:06:40 -0000
@@ -11,6 +11,8 @@
  * the GNU General Public License
  */
 
+#include <sys/types.h>
+#include <regex.h>
 #include <asterisk/lock.h>
 #include <asterisk/cli.h>
 #include <asterisk/pbx.h>
@@ -195,6 +197,8 @@
 
 static int autofallthrough = 0;
 
+static struct ast_custom_function_obj *acf_root = NULL;
+
 static struct pbx_builtin {
 	char name[AST_MAX_APP];
 	int (*execute)(struct ast_channel *chan, void *data);
@@ -1057,6 +1061,139 @@
 	}
 }
 
+static int handle_show_functions(int fd, int argc, char *argv[])
+{
+	struct ast_custom_function_obj *acfptr;
+
+	ast_cli(fd, "Installed Custom Functions:\n--------------------------------------------------------------------------------\n");
+	for (acfptr = acf_root ; acfptr ; acfptr = acfptr->next) {
+		ast_cli(fd, "%s\t(%s)\t[%s]\n", acfptr->name, acfptr->desc, acfptr->syntax);
+	}
+	ast_cli(fd, "\n");
+	return 0;
+}
+
+struct ast_custom_function_obj* ast_custom_function_find_obj(char *name) 
+{
+	struct ast_custom_function_obj *acfptr;
+
+	for (acfptr = acf_root ; acfptr ; acfptr = acfptr->next) {
+		if (!strcmp(name, acfptr->name)) {
+			break;
+		}
+	}	
+	
+	return acfptr;
+}
+
+int ast_custom_function_unregister(struct ast_custom_function_obj *acf) 
+{
+	struct ast_custom_function_obj *acfptr, *lastacf = NULL;
+
+	if (acf) {
+		for (acfptr = acf_root ; acfptr ; acfptr = acfptr->next) {
+			if (acfptr == acf) {
+				if (lastacf) {
+					lastacf->next = acf->next;
+				} else {
+					acf_root = acf->next;
+				}
+				ast_verbose(VERBOSE_PREFIX_1 "Unregistered custom function %s\n", acf->name);
+				return 0;
+			}
+			lastacf = acfptr;
+		}
+	}
+	return -1;
+}
+
+int ast_custom_function_register(struct ast_custom_function_obj *acf) 
+{
+	struct ast_custom_function_obj *acfptr;
+
+	if (acf) {
+		if((acfptr = ast_custom_function_find_obj(acf->name))) {
+			ast_log(LOG_ERROR, "Function %s already in use.\n", acf->name);
+			return -1;
+		}
+		acf->next = acf_root;
+		acf_root = acf;
+		ast_verbose(VERBOSE_PREFIX_1 "Registered custom function %s\n", acf->name);
+		return 0;
+	}
+
+	return -1;
+}
+
+static char *ast_func(char *in) {
+	char *out = NULL;
+	const char *ret_true = "1", *ret_false = "0", *ret = NULL;
+	struct ast_custom_function_obj *acfptr;
+
+	if ((out = strchr(in, ' '))) {
+		char f_isnull = 0, f_exists = 0, f_contains = 0, f_begins = 0, f_ends=0, f_regex = 0;
+		*out = '\0';
+		out++;
+		ret = ret_false; /* convince me otherwise */
+		if (! ( (f_isnull = !strcasecmp(in, "isnull"))
+				|| (f_exists = !strcasecmp(in, "exists"))
+				|| (f_contains = !strcasecmp(in, "contains"))
+				|| (f_begins = !strcasecmp(in, "begins"))
+				|| (f_ends = !strcasecmp(in, "ends"))
+				|| (f_regex = !strcasecmp(in, "regex"))
+				)) {
+			if((acfptr = ast_custom_function_find_obj(in))) {
+				/* run the custom function */
+				return acfptr->function(out);
+			} else {
+				/* why bother */
+				return strdup(ret);
+			}
+		}
+
+		if (f_isnull) {
+			ret = out ? ret_false : ret_true;
+		} else if (f_exists) {
+			ret = out ? ret_true : ret_false;
+		} else if (f_contains || f_begins || f_ends || f_regex) { /* funcs with an arg eg $(contains "foo" foo bar) */
+			char *arg, *tmp;
+			if ((tmp = ast_strdupa(out)) && (arg = strchr(tmp, '"')) && (out = strchr(arg+1, '"'))) {
+				arg++;
+				*out = '\0';
+				out++;
+			
+				if(out[0] == ' ') {
+					out++;
+				} else {
+					ast_log(LOG_WARNING, "Malformed input missing space in %s\n",in);
+					ret = ret_false;
+				}
+				if(f_contains) {
+					ret = strstr(out, arg) ? ret_true : ret_false;				
+				} else if(f_begins) {
+					ret = strncmp(out, arg, strlen(arg)) ? ret_false : ret_true;
+				} else if(f_ends) {
+					ret = (strcmp((out + strlen(out)) - strlen(arg), arg) ) ? ret_false : ret_true;
+				} else if(f_regex) {
+					regex_t regexbuf;
+					if (regcomp(&regexbuf, arg, REG_EXTENDED | REG_NOSUB)) {
+						ast_log(LOG_WARNING, "Malformed regex input %s\n",in);
+						ret = NULL;
+					}
+					ret = regexec(&regexbuf, out, 0, NULL, 0) ? ret_false : ret_true;
+					regfree(&regexbuf);
+				}
+				
+			} else {
+				ast_log(LOG_WARNING, "Malformed input %s\n",in);
+			}
+		}
+	} else {
+		ast_log(LOG_WARNING, "Malformed input missing space in %s\n", in);
+	}
+	return strdup(ret);
+}
+
 static void pbx_substitute_variables_helper_full(struct ast_channel *c, const char *cp1, char *cp2, int count, struct varshead *headp)
 {
 	char *cp4;
@@ -1064,9 +1201,9 @@
 	int length;
 	char workspace[4096];
 	char ltmp[4096], var[4096];
-	char *nextvar, *nextexp;
+	char *nextvar, *nextexp, *nextfunc;
 	char *vars, *vare;
-	int pos, brackets, needsub, len;
+	int pos, brackets, needsub, len, needfunc;
 	
 	/* Substitutes variables into cp2, based on string cp1, and assuming cp2 to be
 	   zero-filled */
@@ -1080,13 +1217,27 @@
 		
 		/* Look for an expression */
 		nextexp = strstr(whereweare, "$[");
+
+		/* Look for a function */
+		nextfunc = strstr(whereweare, "$(");
 		
 		/* Pick the first one only */
-		if (nextvar && nextexp) {
-			if (nextvar < nextexp)
-				nextexp = NULL;
-			else
-				nextvar = NULL;
+		len = 0;
+		if (nextvar)
+			len++;
+		if(nextexp)
+			len++;
+		if(nextfunc)
+			len++;
+
+		if (len > 1) {
+			if(nextfunc) {
+				nextvar = nextexp = NULL;
+			} else if (nextvar) {
+                nextexp = nextfunc = NULL;
+			} else if (nextexp) {
+                nextvar = nextfunc = NULL;
+            }
 		}
 		
 		/* If there is one, we only go that far */
@@ -1094,7 +1245,9 @@
 			pos = nextvar - whereweare;
 		else if (nextexp)
 			pos = nextexp - whereweare;
-		
+		else if (nextfunc) {
+			pos = nextfunc - whereweare;
+		}
 		/* Can't copy more than 'count' bytes */
 		if (pos > count)
 			pos = count;
@@ -1165,7 +1318,8 @@
 			vars = vare = nextexp + 2;
 			brackets = 1;
 			needsub = 0;
-			
+			needfunc = 0;
+
 			/* Find the end of it */
 			while(brackets && *vare) {
 				if ((vare[0] == '$') && (vare[1] == '[')) {
@@ -1179,6 +1333,9 @@
 				} else if ((vare[0] == '$') && (vare[1] == '{')) {
 					needsub++;
 					vare++;
+				} else if ((vare[0] == '$') && (vare[1] == '(')) {
+					needfunc++;
+					vare++;
 				}
 				vare++;
 			}
@@ -1195,7 +1352,7 @@
 			var[len] = '\0';
 			
 			/* Substitute if necessary */
-			if (needsub) {
+			if (needsub || needfunc) {
 				memset(ltmp, 0, sizeof(ltmp));
 				pbx_substitute_variables_helper(c, var, ltmp, sizeof(ltmp) - 1);
 				vars = ltmp;
@@ -1217,6 +1374,63 @@
 				cp2 += length;
 				free(cp4);
 			}
+		} else if (nextfunc) {
+			/* We have a function.  Find the start and end */
+			vars = vare = nextfunc + 2;
+			brackets = 1;
+			needsub = 0;
+			
+			/* Find the end of it */
+			while(brackets && *vare) {
+				if ((vare[0] == '$') && (vare[1] == '(')) {
+					needsub++;
+					brackets++;
+					vare++;
+				} else if (vare[0] == '(') {
+					brackets++;
+				} else if (vare[0] == ')') {
+					brackets--;
+				} else if ((vare[0] == '$') && (vare[1] == '{')) {
+					needsub++;
+					vare++;
+				}
+				vare++;
+			}
+			if (brackets)
+				ast_log(LOG_NOTICE, "Error in extension logic (missing ')')\n");
+			len = vare - vars - 1;
+			
+			/* Skip totally over variable name */
+			whereweare += ( len + 3);
+			
+			/* Store variable name (and truncate) */
+			memset(var, 0, sizeof(var));
+			strncpy(var, vars, sizeof(var) - 1);
+			var[len] = '\0';
+			
+			/* Substitute if necessary */
+			if (needsub) {
+				memset(ltmp, 0, sizeof(ltmp));
+				pbx_substitute_variables_helper(c, var, ltmp, sizeof(ltmp) - 1);
+				vars = ltmp;
+			} else {
+				vars = var;
+			}
+
+			/* Evaluate expression */			
+			cp4 = ast_func(vars);
+			
+			ast_log(LOG_DEBUG, "Expression is '%s'\n", cp4);
+			
+			if (cp4) {
+				length = strlen(cp4);
+				if (length > count)
+					length = count;
+				memcpy(cp2, cp4, length);
+				count -= length;
+				cp2 += length;
+				free(cp4);
+			}
 			
 		} else
 			break;
@@ -1238,7 +1452,7 @@
 	memset(passdata, 0, datalen);
 		
 	/* No variables or expressions in e->data, so why scan it? */
-	if (!strstr(e->data,"${") && !strstr(e->data,"$[")) {
+	if (!strstr(e->data,"${") && !strstr(e->data,"$[") && !strstr(e->data,"$(")) {
 		strncpy(passdata, e->data, datalen - 1);
 		passdata[datalen-1] = '\0';
 		return;
@@ -2565,6 +2779,10 @@
 "Usage: show application <application> [<application> [<application> [...]]]\n"
 "       Describes a particular application.\n";
 
+static char show_functions_help[] =
+" Usage: show functions\n"
+"";
+
 static char show_applications_help[] =
 "Usage: show applications [{like|describing} <text>]\n"
 "       List applications which are currently available.\n"
@@ -3149,6 +3367,11 @@
 	handle_show_applications, "Shows registered applications",
 	show_applications_help, complete_show_applications };
 
+static struct ast_cli_entry show_functions_cli = 
+	{ { "show", "functions", NULL }, 
+	handle_show_functions, "Shows registered functions",
+	show_functions_help};
+
 static struct ast_cli_entry show_application_cli =
 	{ { "show", "application", NULL }, 
 	handle_show_application, "Describe a specific application",
@@ -5416,6 +5639,7 @@
 	}
         AST_LIST_HEAD_INIT(&globals);
 	ast_cli_register(&show_applications_cli);
+	ast_cli_register(&show_functions_cli);
 	ast_cli_register(&show_application_cli);
 	ast_cli_register(&show_dialplan_cli);
 	ast_cli_register(&show_switches_cli);
