Wed Jan 8 2020 09:49:40

Asterisk developer's documentation


app_readfile.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Matt O'Gorman <mogorman@digium.com>
7  *
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.
13  *
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.
17  */
18 
19 /*! \file
20  *
21  * \brief ReadFile application -- Reads in a File for you.
22  *
23  * \author Matt O'Gorman <mogorman@digium.com>
24  *
25  * \ingroup applications
26  */
27 
28 /*** MODULEINFO
29  <support_level>deprecated</support_level>
30  <replacement>func_env (FILE())</replacement>
31  ***/
32 
33 #include "asterisk.h"
34 
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328446 $")
36 
37 #include "asterisk/file.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/app.h"
41 #include "asterisk/module.h"
42 
43 /*** DOCUMENTATION
44  <application name="ReadFile" language="en_US">
45  <synopsis>
46  Read the contents of a text file into a channel variable.
47  </synopsis>
48  <syntax argsep="=">
49  <parameter name="varname" required="true">
50  <para>Result stored here.</para>
51  </parameter>
52  <parameter name="fileparams" required="true">
53  <argument name="file" required="true">
54  <para>The name of the file to read.</para>
55  </argument>
56  <argument name="length" required="false">
57  <para>Maximum number of characters to capture.</para>
58  <para>If not specified defaults to max.</para>
59  </argument>
60  </parameter>
61  </syntax>
62  <description>
63  <para>Read the contents of a text file into channel variable <replaceable>varname</replaceable></para>
64  <warning><para>ReadFile has been deprecated in favor of Set(varname=${FILE(file,0,length)})</para></warning>
65  </description>
66  <see-also>
67  <ref type="application">System</ref>
68  <ref type="application">Read</ref>
69  </see-also>
70  </application>
71  ***/
72 
73 static char *app_readfile = "ReadFile";
74 
75 static int readfile_exec(struct ast_channel *chan, const char *data)
76 {
77  int res=0;
78  char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
79  int len=0;
80  static int deprecation_warning = 0;
81 
82  if (ast_strlen_zero(data)) {
83  ast_log(LOG_WARNING, "ReadFile require an argument!\n");
84  return -1;
85  }
86 
87  s = ast_strdupa(data);
88 
89  varname = strsep(&s, "=");
90  file = strsep(&s, ",");
91  length = s;
92 
93  if (deprecation_warning++ % 10 == 0)
94  ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length);
95 
96  if (!varname || !file) {
97  ast_log(LOG_ERROR, "No file or variable specified!\n");
98  return -1;
99  }
100 
101  if (length) {
102  if ((sscanf(length, "%30d", &len) != 1) || (len < 0)) {
103  ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
104  len = 0;
105  }
106  }
107 
108  if ((returnvar = ast_read_textfile(file))) {
109  if (len > 0) {
110  if (len < strlen(returnvar))
111  returnvar[len]='\0';
112  else
113  ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
114  }
115  pbx_builtin_setvar_helper(chan, varname, returnvar);
116  ast_free(returnvar);
117  }
118 
119  return res;
120 }
121 
122 
123 static int unload_module(void)
124 {
125  return ast_unregister_application(app_readfile);
126 }
127 
128 static int load_module(void)
129 {
130  return ast_register_application_xml(app_readfile, readfile_exec);
131 }
132 
133 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable");
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk main include file. File version handling, generic pbx functions.
char * strsep(char **str, const char *delims)
#define LOG_WARNING
Definition: logger.h:144
static char * app_readfile
Definition: app_readfile.c:73
static int readfile_exec(struct ast_channel *chan, const char *data)
Definition: app_readfile.c:75
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
static int unload_module(void)
Definition: app_readfile.c:123
General Asterisk PBX channel definitions.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
Core PBX routines and definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define LOG_ERROR
Definition: logger.h:155
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
Definition: pbx.c:10546
static int load_module(void)
Definition: app_readfile.c:128
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
char * ast_read_textfile(const char *file)
Read a file into asterisk.
Definition: app.c:1987
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180