? apps/app_readfile.c Index: app.c =================================================================== RCS file: /usr/cvsroot/asterisk/app.c,v retrieving revision 1.48 diff -u -r1.48 app.c --- app.c 17 Feb 2005 20:04:10 -0000 1.48 +++ app.c 27 Feb 2005 07:23:49 -0000 @@ -1393,3 +1393,54 @@ res = 0; return res; } + +char * ast_read_file(char *filename, char *mode) +{ + FILE *file; + char line[2048]; + char *output=NULL; + struct stat filesize; + int count=0; + if(stat(filename,&filesize)== -1){ + ast_log(LOG_ERROR,"Error can't stat %s\n",filename); + return NULL; + } + count=filesize.st_size; + output=(char *)malloc(filesize.st_size/sizeof(char)); + file = fopen(filename, mode); + + if (!file) { + ast_log(LOG_ERROR, "Cannot open file\n"); + return NULL; + } + output=(char *)malloc(filesize.st_size/sizeof(char)); + if(fgets(line, sizeof(line), file)){ + if(count >= sizeof(line)){ + count=count-sizeof(line); + sprintf(output,"%s",line); + }else{ + output=(char *) realloc(output, (strlen(output) + strlen(line) + 1) * sizeof(char)); + sprintf(output,"%s",line); + } + while (fgets(line, sizeof(line), file)) { + if (count >= sizeof(line)){ + count=count-sizeof(line); + sprintf(output,"%s%s",output,line); + } + else{ + output=(char *) realloc(output, (strlen(output) + strlen(line) + 1) * sizeof(char)); + sprintf(output,"%s%s",output,line); + } + + } + } + else{ + ast_log(LOG_ERROR,"Empty file\n"); + fclose(file); + free(output); + return NULL; + } + fclose(file); + return output; +} + Index: apps/Makefile =================================================================== RCS file: /usr/cvsroot/asterisk/apps/Makefile,v retrieving revision 1.92 diff -u -r1.92 Makefile --- apps/Makefile 18 Feb 2005 05:29:31 -0000 1.92 +++ apps/Makefile 27 Feb 2005 07:23:49 -0000 @@ -31,7 +31,7 @@ app_talkdetect.so app_alarmreceiver.so app_userevent.so app_verbose.so \ app_test.so app_forkcdr.so app_math.so app_realtime.so \ app_dumpchan.so app_waitforsilence.so app_while.so app_setrdnis.so \ - app_md5.so + app_md5.so app_readfile.so ifneq (${OSARCH},Darwin) ifneq (${OSARCH},SunOS) Index: apps/app_system.c =================================================================== RCS file: /usr/cvsroot/asterisk/apps/app_system.c,v retrieving revision 1.13 diff -u -r1.13 app_system.c --- apps/app_system.c 21 Jan 2005 07:06:24 -0000 1.13 +++ apps/app_system.c 27 Feb 2005 07:23:49 -0000 @@ -1,4 +1,4 @@ -/* +/* * Asterisk -- A telephony toolkit for Linux. * * Execute arbitrary system commands @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -30,16 +31,20 @@ static char *app2 = "TrySystem"; -static char *synopsis = "Execute a system command"; +static char *synopsis = "System(varname=command,lines)"; static char *synopsis2 = "Try executing a system command"; static char *descrip = -" System(command): Executes a command by using system(). Returns -1 on\n" +" System(varname=command,lines): Executes a command by using system(). Returns -1 on\n" "failure to execute the specified command. If the command itself executes\n" "but is in error, and if there exists a priority n + 101, where 'n' is the\n" "priority of the current instance, then the channel will be setup to\n" -"continue at that priority level. Otherwise, System returns 0.\n"; +"continue at that priority level. Otherwise, System returns 0.\n" +" Varname - Result stored here.\n" +" Command - The name of the command to run, optionally enclosed in quotes.\n" +" Charcters - Maximum chars to capture. Defaults to infinite.\n"; + static char *descrip2 = " TrySystem(command): Executes a command by using system(). Returns 0\n" @@ -56,18 +61,82 @@ { int res=0; struct localuser *u; + char *s, *varname=NULL, *program=NULL, *length=NULL, *tempvar, *returnvar=NULL; + char *path=NULL; +// char *tail="|tail -n"; + char *cat=" > "; + char *remove=NULL; + int len=0; if (!data) { ast_log(LOG_WARNING, "System requires an argument(command)\n"); return failmode; } + LOCAL_USER_ADD(u); - /* Do our thing here */ - res = ast_safe_system((char *)data); + + s = ast_strdupa(data); + if (!s) { + ast_log(LOG_ERROR, "Out of memory\n"); + return -1; + } + + varname = strsep(&s, "="); + if (s && *s == '"') { + s++; + program = strsep(&s, "\""); + if (s && *s == '|') { + s++; + } + } else { + program = strsep(&s, "|"); + } + length = s; + + if (!program) { + tempvar=varname; + } + + else { + sscanf(length, "%d", &len); + path = (char *)malloc((strlen("/var/spool/asterisk/system/")+strlen(chan->uniqueid)+1)*sizeof(char)); + sprintf(path,"/var/spool/asterisk/system/%s",chan->uniqueid); + + if (len ==0) { + tempvar=(char *)malloc((strlen(program)+strlen(cat)+strlen(path)+1)*sizeof(char)); + sprintf(tempvar,"%s%s%s",program,cat,path); + } + else{ + tempvar=(char *)malloc((strlen(program)+/*strlen(tail)+*/strlen(length)+strlen(cat)+strlen(path)+1)*sizeof(char)); + sprintf(tempvar,"%s%s%s",program,/*tail,length,*/cat,path); + } + } + ast_log(LOG_ERROR,"%s",tempvar); + res = ast_safe_system(tempvar); + if(!program){ + free(returnvar); + free(path); + } + else{ + + returnvar = ast_read_file(path,"r"); + + remove = (char *)malloc((strlen("rm -f /var/spool/asterisk/system/")+strlen(chan->uniqueid)+1)*sizeof(char)); + sprintf(remove,"rm -f /var/spool/asterisk/system/%s",chan->uniqueid); + ast_safe_system(remove); + if(len!=0){ + if(len > strlen(returnvar)) + returnvar[len]='\0'; + } + pbx_builtin_setvar_helper(chan, varname, returnvar); + free(returnvar); + free(remove); + free(path); + } if ((res < 0) && (errno != ECHILD)) { - ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data); + ast_log(LOG_WARNING, "Unable to execute1 '%s'\n", tempvar); res = failmode; } else if (res == 127) { - ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data); + ast_log(LOG_WARNING, "Unable to execute2 '%s'\n", tempvar); res = failmode; } else { if (res < 0) @@ -76,9 +145,15 @@ chan->priority+=100; res = 0; } + + if(program) + free(tempvar); + LOCAL_USER_REMOVE(u); return res; } + + static int system_exec(struct ast_channel *chan, void *data) { Index: include/asterisk/app.h =================================================================== RCS file: /usr/cvsroot/asterisk/include/asterisk/app.h,v retrieving revision 1.30 diff -u -r1.30 app.h --- include/asterisk/app.h 17 Feb 2005 20:04:10 -0000 1.30 +++ include/asterisk/app.h 27 Feb 2005 07:23:49 -0000 @@ -127,6 +127,9 @@ /* Unlock a path */ int ast_unlock_path(const char *path); +/*Read a file into asterisk*/ +char * ast_read_file(char *file, char* mode); + #define GROUP_CATEGORY_PREFIX "GROUP" /*! Split a group string into group and category, returning a default category if none is provided. */