/* * BF for Asterisk * * Copyright (C) 2005, David Eder * * This program is free software, distributed under the terms of * the GNU General Public License */ #include <../asterisk.h> #include #include #include #include #include #include #include #define MAX_ARGS 128 static char *tdesc = "BF for the dialplan"; static char *app = "BF"; static char *synopsis = "Execute BF code"; static char *descrip = " BF(code|input_var|output_var): Executes code as BF code.\n"; STANDARD_LOCAL_USER; LOCAL_USER_DECL; static int bf_exec(struct ast_channel *chan, void *data) { int res = 0; char *argv[MAX_ARGS]; char buf[2048]=""; char *tmp = (char *)buf; int argc = 0; char *stringp; struct localuser *u; char *input; int input_pointer = 0; char output[4096]; int output_pointer = 0; char m[9999], *n[99], *r=m, *p=m+5000, **s=n, d = 0, c; if (!data || ast_strlen_zero(data)) { ast_log(LOG_WARNING, "BF requires 3 arguments (code,input_var,output_var)\n"); return -1; } strncpy(buf, data, sizeof(buf) - 1); while ((stringp = strsep(&tmp, "|"))) { argv[argc++] = stringp; } argv[argc] = NULL; if(argc != 3) { ast_log(LOG_WARNING, "BF requires 3 arguments (code,input_var,output_var)\n"); return -1; } input = (char *)pbx_builtin_getvar_helper(chan, argv[1]); LOCAL_USER_ADD(u); for(strncpy(r,argv[0],4000);(c=*r);r++) c-']'||(d>1||(r=*p?*s:(--s,r)),!d||d--),c-'['||d++||(*++s=r),d||(*p+=c=='+',*p-=c=='-',p+=c=='>', p-=c=='<',c-'.'||(output[output_pointer++]=*p),c-','||(*p = input[input_pointer++])); pbx_builtin_setvar_helper(chan, argv[2], output); LOCAL_USER_REMOVE(u); return res; } int unload_module(void) { STANDARD_HANGUP_LOCALUSERS; return ast_unregister_application(app); } int load_module(void) { return ast_register_application(app, bf_exec, synopsis, descrip); } char *description(void) { return tdesc; } int usecount(void) { int res; STANDARD_USECOUNT(res); return res; } char *key() { return ASTERISK_GPL_KEY; }