diff options
-rw-r--r-- | apps/app_readfile.c | 4 | ||||
-rw-r--r-- | funcs/func_env.c | 61 |
2 files changed, 65 insertions, 0 deletions
diff --git a/apps/app_readfile.c b/apps/app_readfile.c index f20e028b0..d1bedcbce 100644 --- a/apps/app_readfile.c +++ b/apps/app_readfile.c @@ -58,6 +58,7 @@ static int readfile_exec(struct ast_channel *chan, void *data) int res=0; char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL; int len=0; + static int deprecation_warning = 0; if (ast_strlen_zero(data)) { ast_log(LOG_WARNING, "ReadFile require an argument!\n"); @@ -70,6 +71,9 @@ static int readfile_exec(struct ast_channel *chan, void *data) file = strsep(&s, ","); length = s; + if (deprecation_warning++ % 10 == 0) + ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length); + if (!varname || !file) { ast_log(LOG_ERROR, "No file or variable specified!\n"); return -1; diff --git a/funcs/func_env.c b/funcs/func_env.c index 3e69666ce..e53cb5309 100644 --- a/funcs/func_env.c +++ b/funcs/func_env.c @@ -111,6 +111,49 @@ static int stat_read(struct ast_channel *chan, const char *cmd, char *data, return 0; } +static int file_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) +{ + AST_DECLARE_APP_ARGS(args, + AST_APP_ARG(filename); + AST_APP_ARG(offset); + AST_APP_ARG(length); + ); + int offset = 0, length; + char *contents; + + AST_STANDARD_APP_ARGS(args, data); + if (args.argc > 1) + offset = atoi(args.offset); + + if (args.argc > 2) { + if ((length = atoi(args.length)) < 1) { + ast_log(LOG_WARNING, "Invalid length '%s'. Returning the max (%d)\n", args.length, len); + length = len; + } else if (length > len) { + ast_log(LOG_WARNING, "Length %d is greater than the max (%d). Truncating output.\n", length, len); + length = len; + } + } else + length = len; + + if (!(contents = ast_read_textfile(args.filename))) + return -1; + + if (offset >= 0) + ast_copy_string(buf, &contents[offset], length); + else { + size_t tmp = strlen(contents); + if (offset * -1 > tmp) { + ast_log(LOG_WARNING, "Offset is larger than the file size.\n"); + offset = tmp * -1; + } + ast_copy_string(buf, &contents[tmp + offset], length); + } + ast_free(contents); + + return 0; +} + static struct ast_custom_function env_function = { .name = "ENV", .synopsis = "Gets or sets the environment variable specified", @@ -136,12 +179,29 @@ static struct ast_custom_function stat_function = { " M - Returns the epoch at which the file was last modified\n", }; +static struct ast_custom_function file_function = { + .name = "FILE", + .synopsis = "Obtains the contents of a file", + .syntax = "FILE(<filename>,<offset>,<length>)", + .read = file_read, + /* + * Some enterprising programmer could probably add write functionality + * to FILE(), although I'm not sure how useful it would be. Hence why + * it's called FILE and not READFILE (like the app was). + */ + .desc = +"<offset> may be specified as any number. If negative, <offset> specifies\n" +" the number of bytes back from the end of the file.\n" +"<length>, if specified, will limit the length of the data read to that size.\n", +}; + static int unload_module(void) { int res = 0; res |= ast_custom_function_unregister(&env_function); res |= ast_custom_function_unregister(&stat_function); + res |= ast_custom_function_unregister(&file_function); return res; } @@ -152,6 +212,7 @@ static int load_module(void) res |= ast_custom_function_register(&env_function); res |= ast_custom_function_register(&stat_function); + res |= ast_custom_function_register(&file_function); return res; } |