Sat Mar 24 22:55:12 2007

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  * \ingroup applications
00024  */
00025 
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 #include <string.h>
00030 
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 14462 $")
00034 
00035 #include "asterisk/file.h"
00036 #include "asterisk/logger.h"
00037 #include "asterisk/options.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/app.h"
00041 #include "asterisk/module.h"
00042 
00043 static char *tdesc = "Stores output of file into a variable";
00044 
00045 static char *app_readfile = "ReadFile";
00046 
00047 static char *readfile_synopsis = "ReadFile(varname=file,length)";
00048 
00049 static char *readfile_descrip =
00050 "ReadFile(varname=file,length)\n"
00051 "  Varname - Result stored here.\n"
00052 "  File - The name of the file to read.\n"
00053 "  Length - Maximum number of characters to capture.\n";
00054 
00055 STANDARD_LOCAL_USER;
00056 
00057 LOCAL_USER_DECL;
00058 
00059 
00060 static int readfile_exec(struct ast_channel *chan, void *data)
00061 {
00062    int res=0;
00063    struct localuser *u;
00064    char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
00065    int len=0;
00066 
00067    if (ast_strlen_zero(data)) {
00068       ast_log(LOG_WARNING, "ReadFile require an argument!\n");
00069       return -1;
00070    }
00071 
00072    LOCAL_USER_ADD(u);
00073 
00074    s = ast_strdupa(data);
00075    if (!s) {
00076       ast_log(LOG_ERROR, "Out of memory\n");
00077       LOCAL_USER_REMOVE(u);
00078       return -1;
00079    }
00080 
00081    varname = strsep(&s, "=");
00082    file = strsep(&s, "|");
00083    length = s;
00084 
00085    if (!varname || !file) {
00086       ast_log(LOG_ERROR, "No file or variable specified!\n");
00087       LOCAL_USER_REMOVE(u);
00088       return -1;
00089    }
00090 
00091    if (length) {
00092       if ((sscanf(length, "%d", &len) != 1) || (len < 0)) {
00093          ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
00094          len = 0;
00095       }
00096    }
00097 
00098    if ((returnvar = ast_read_textfile(file))) {
00099       if (len > 0) {
00100          if (len < strlen(returnvar))
00101             returnvar[len]='\0';
00102          else
00103             ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
00104       }
00105       pbx_builtin_setvar_helper(chan, varname, returnvar);
00106       free(returnvar);
00107    }
00108    LOCAL_USER_REMOVE(u);
00109    return res;
00110 }
00111 
00112 
00113 int unload_module(void)
00114 {
00115    int res;
00116 
00117    res = ast_unregister_application(app_readfile);
00118    
00119    STANDARD_HANGUP_LOCALUSERS;
00120 
00121    return res; 
00122 }
00123 
00124 int load_module(void)
00125 {
00126    return ast_register_application(app_readfile, readfile_exec, readfile_synopsis, readfile_descrip);
00127 }
00128 
00129 char *description(void)
00130 {
00131    return tdesc;
00132 }
00133 
00134 int usecount(void)
00135 {
00136    int res;
00137    STANDARD_USECOUNT(res);
00138    return res;
00139 }
00140 
00141 char *key()
00142 {
00143    return ASTERISK_GPL_KEY;
00144 }

Generated on Sat Mar 24 22:55:12 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.7