Sat Apr 26 2014 22:01:27

Asterisk developer's documentation


app_readfile.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Matt O'Gorman <mogorman@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief ReadFile application -- Reads in a File for you.
00022  *
00023  * \author Matt O'Gorman <mogorman@digium.com>
00024  *
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <defaultenabled>yes</defaultenabled>
00030    <support_level>deprecated</support_level>
00031    <replacement>func_env (FILE())</replacement>
00032  ***/
00033 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328259 $")
00037 
00038 #include "asterisk/file.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/app.h"
00042 #include "asterisk/module.h"
00043 
00044 /*** DOCUMENTATION
00045    <application name="ReadFile" language="en_US">
00046       <synopsis>
00047          Read the contents of a text file into a channel variable.
00048       </synopsis>
00049       <syntax argsep="=">
00050          <parameter name="varname" required="true">
00051             <para>Result stored here.</para>
00052          </parameter>
00053          <parameter name="fileparams" required="true">
00054             <argument name="file" required="true">
00055                <para>The name of the file to read.</para>
00056             </argument>
00057             <argument name="length" required="false">
00058                <para>Maximum number of characters to capture.</para>
00059                <para>If not specified defaults to max.</para>
00060             </argument>
00061          </parameter>
00062       </syntax>
00063       <description>
00064          <para>Read the contents of a text file into channel variable <replaceable>varname</replaceable></para>
00065          <warning><para>ReadFile has been deprecated in favor of Set(varname=${FILE(file,0,length)})</para></warning>
00066       </description>
00067       <see-also>
00068          <ref type="application">System</ref>
00069          <ref type="application">Read</ref>
00070       </see-also>
00071    </application>
00072  ***/
00073 
00074 static char *app_readfile = "ReadFile";
00075 
00076 static int readfile_exec(struct ast_channel *chan, const char *data)
00077 {
00078    int res=0;
00079    char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
00080    int len=0;
00081    static int deprecation_warning = 0;
00082 
00083    if (ast_strlen_zero(data)) {
00084       ast_log(LOG_WARNING, "ReadFile require an argument!\n");
00085       return -1;
00086    }
00087 
00088    s = ast_strdupa(data);
00089 
00090    varname = strsep(&s, "=");
00091    file = strsep(&s, ",");
00092    length = s;
00093 
00094    if (deprecation_warning++ % 10 == 0)
00095       ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length);
00096 
00097    if (!varname || !file) {
00098       ast_log(LOG_ERROR, "No file or variable specified!\n");
00099       return -1;
00100    }
00101 
00102    if (length) {
00103       if ((sscanf(length, "%30d", &len) != 1) || (len < 0)) {
00104          ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
00105          len = 0;
00106       }
00107    }
00108 
00109    if ((returnvar = ast_read_textfile(file))) {
00110       if (len > 0) {
00111          if (len < strlen(returnvar))
00112             returnvar[len]='\0';
00113          else
00114             ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
00115       }
00116       pbx_builtin_setvar_helper(chan, varname, returnvar);
00117       ast_free(returnvar);
00118    }
00119 
00120    return res;
00121 }
00122 
00123 
00124 static int unload_module(void)
00125 {
00126    return ast_unregister_application(app_readfile);
00127 }
00128 
00129 static int load_module(void)
00130 {
00131    return ast_register_application_xml(app_readfile, readfile_exec);
00132 }
00133 
00134 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable");