Tue Aug 20 16:34:22 2013

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    <support_level>deprecated</support_level>
00030    <replacement>func_env (FILE())</replacement>
00031  ***/
00032 
00033 #include "asterisk.h"
00034 
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328446 $")
00036 
00037 #include "asterisk/file.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/app.h"
00041 #include "asterisk/module.h"
00042 
00043 /*** DOCUMENTATION
00044    <application name="ReadFile" language="en_US">
00045       <synopsis>
00046          Read the contents of a text file into a channel variable.
00047       </synopsis>
00048       <syntax argsep="=">
00049          <parameter name="varname" required="true">
00050             <para>Result stored here.</para>
00051          </parameter>
00052          <parameter name="fileparams" required="true">
00053             <argument name="file" required="true">
00054                <para>The name of the file to read.</para>
00055             </argument>
00056             <argument name="length" required="false">
00057                <para>Maximum number of characters to capture.</para>
00058                <para>If not specified defaults to max.</para>
00059             </argument>
00060          </parameter>
00061       </syntax>
00062       <description>
00063          <para>Read the contents of a text file into channel variable <replaceable>varname</replaceable></para>
00064          <warning><para>ReadFile has been deprecated in favor of Set(varname=${FILE(file,0,length)})</para></warning>
00065       </description>
00066       <see-also>
00067          <ref type="application">System</ref>
00068          <ref type="application">Read</ref>
00069       </see-also>
00070    </application>
00071  ***/
00072 
00073 static char *app_readfile = "ReadFile";
00074 
00075 static int readfile_exec(struct ast_channel *chan, const char *data)
00076 {
00077    int res=0;
00078    char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
00079    int len=0;
00080    static int deprecation_warning = 0;
00081 
00082    if (ast_strlen_zero(data)) {
00083       ast_log(LOG_WARNING, "ReadFile require an argument!\n");
00084       return -1;
00085    }
00086 
00087    s = ast_strdupa(data);
00088 
00089    varname = strsep(&s, "=");
00090    file = strsep(&s, ",");
00091    length = s;
00092 
00093    if (deprecation_warning++ % 10 == 0)
00094       ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length);
00095 
00096    if (!varname || !file) {
00097       ast_log(LOG_ERROR, "No file or variable specified!\n");
00098       return -1;
00099    }
00100 
00101    if (length) {
00102       if ((sscanf(length, "%30d", &len) != 1) || (len < 0)) {
00103          ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
00104          len = 0;
00105       }
00106    }
00107 
00108    if ((returnvar = ast_read_textfile(file))) {
00109       if (len > 0) {
00110          if (len < strlen(returnvar))
00111             returnvar[len]='\0';
00112          else
00113             ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
00114       }
00115       pbx_builtin_setvar_helper(chan, varname, returnvar);
00116       ast_free(returnvar);
00117    }
00118 
00119    return res;
00120 }
00121 
00122 
00123 static int unload_module(void)
00124 {
00125    return ast_unregister_application(app_readfile);
00126 }
00127 
00128 static int load_module(void)
00129 {
00130    return ast_register_application_xml(app_readfile, readfile_exec);
00131 }
00132 
00133 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable");

Generated on 20 Aug 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1