Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / apps / app_readfile.c
blob8762ef860cffb38f4ce31ebd93aba9c7c212439b
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Matt O'Gorman <mogorman@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief ReadFile application -- Reads in a File for you.
23 * \author Matt O'Gorman <mogorman@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/file.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/app.h"
36 #include "asterisk/module.h"
38 static char *app_readfile = "ReadFile";
40 static char *readfile_synopsis = "Read the contents of a text file into a channel variable";
42 static char *readfile_descrip =
43 "ReadFile(varname=file,length)\n"
44 " varname - Result stored here.\n"
45 " file - The name of the file to read.\n"
46 " length - Maximum number of characters to capture.\n";
49 static int readfile_exec(struct ast_channel *chan, void *data)
51 int res=0;
52 char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
53 int len=0;
54 static int deprecation_warning = 0;
56 if (ast_strlen_zero(data)) {
57 ast_log(LOG_WARNING, "ReadFile require an argument!\n");
58 return -1;
61 s = ast_strdupa(data);
63 varname = strsep(&s, "=");
64 file = strsep(&s, ",");
65 length = s;
67 if (deprecation_warning++ % 10 == 0)
68 ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length);
70 if (!varname || !file) {
71 ast_log(LOG_ERROR, "No file or variable specified!\n");
72 return -1;
75 if (length) {
76 if ((sscanf(length, "%d", &len) != 1) || (len < 0)) {
77 ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
78 len = 0;
82 if ((returnvar = ast_read_textfile(file))) {
83 if (len > 0) {
84 if (len < strlen(returnvar))
85 returnvar[len]='\0';
86 else
87 ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
89 pbx_builtin_setvar_helper(chan, varname, returnvar);
90 ast_free(returnvar);
93 return res;
97 static int unload_module(void)
99 return ast_unregister_application(app_readfile);
102 static int load_module(void)
104 return ast_register_application(app_readfile, readfile_exec, readfile_synopsis, readfile_descrip);
107 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable");