Fri Jun 19 12:09:45 2009

Asterisk developer's documentation


func_env.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief Environment related dialplan functions
00020  * 
00021  * \ingroup functions
00022  */
00023 
00024 #include "asterisk.h"
00025 
00026 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 182280 $")
00027 
00028 #include <sys/stat.h>
00029 
00030 #include "asterisk/module.h"
00031 #include "asterisk/channel.h"
00032 #include "asterisk/pbx.h"
00033 #include "asterisk/utils.h"
00034 #include "asterisk/app.h"
00035 
00036 static int env_read(struct ast_channel *chan, const char *cmd, char *data,
00037           char *buf, size_t len)
00038 {
00039    char *ret = NULL;
00040 
00041    *buf = '\0';
00042 
00043    if (data)
00044       ret = getenv(data);
00045 
00046    if (ret)
00047       ast_copy_string(buf, ret, len);
00048 
00049    return 0;
00050 }
00051 
00052 static int env_write(struct ast_channel *chan, const char *cmd, char *data,
00053            const char *value)
00054 {
00055    if (!ast_strlen_zero(data)) {
00056       if (!ast_strlen_zero(value)) {
00057          setenv(data, value, 1);
00058       } else {
00059          unsetenv(data);
00060       }
00061    }
00062 
00063    return 0;
00064 }
00065 
00066 static int stat_read(struct ast_channel *chan, const char *cmd, char *data,
00067            char *buf, size_t len)
00068 {
00069    char *action;
00070    struct stat s;
00071 
00072    ast_copy_string(buf, "0", len);
00073 
00074    action = strsep(&data, ",");
00075    if (stat(data, &s)) {
00076       return 0;
00077    } else {
00078       switch (*action) {
00079       case 'e':
00080          strcpy(buf, "1");
00081          break;
00082       case 's':
00083          snprintf(buf, len, "%d", (unsigned int) s.st_size);
00084          break;
00085       case 'f':
00086          snprintf(buf, len, "%d", S_ISREG(s.st_mode) ? 1 : 0);
00087          break;
00088       case 'd':
00089          snprintf(buf, len, "%d", S_ISDIR(s.st_mode) ? 1 : 0);
00090          break;
00091       case 'M':
00092          snprintf(buf, len, "%d", (int) s.st_mtime);
00093          break;
00094       case 'A':
00095          snprintf(buf, len, "%d", (int) s.st_mtime);
00096          break;
00097       case 'C':
00098          snprintf(buf, len, "%d", (int) s.st_ctime);
00099          break;
00100       case 'm':
00101          snprintf(buf, len, "%o", (int) s.st_mode);
00102          break;
00103       }
00104    }
00105 
00106    return 0;
00107 }
00108 
00109 static int file_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
00110 {
00111    AST_DECLARE_APP_ARGS(args,
00112       AST_APP_ARG(filename);
00113       AST_APP_ARG(offset);
00114       AST_APP_ARG(length);
00115    );
00116    int offset = 0, length, res = 0;
00117    char *contents;
00118    size_t contents_len;
00119 
00120    AST_STANDARD_APP_ARGS(args, data);
00121    if (args.argc > 1) {
00122       offset = atoi(args.offset);
00123    }
00124 
00125    if (args.argc > 2) {
00126       /* The +1/-1 in this code section is to accomodate for the terminating NULL. */
00127       if ((length = atoi(args.length) + 1) > len) {
00128          ast_log(LOG_WARNING, "Length %d is greater than the max (%d).  Truncating output.\n", length - 1, (int)len - 1);
00129          length = len;
00130       }
00131    } else {
00132       length = len;
00133    }
00134 
00135    if (!(contents = ast_read_textfile(args.filename))) {
00136       return -1;
00137    }
00138 
00139    do {
00140       contents_len = strlen(contents);
00141       if (offset > contents_len) {
00142          res = -1;
00143          break;
00144       }
00145 
00146       if (offset >= 0) {
00147          if (length < 0) {
00148             if (contents_len - offset + length < 0) {
00149                /* Nothing left after trimming */
00150                res = -1;
00151                break;
00152             }
00153             ast_copy_string(buf, &contents[offset], contents_len + length);
00154          } else {
00155             ast_copy_string(buf, &contents[offset], length);
00156          }
00157       } else {
00158          if (offset * -1 > contents_len) {
00159             ast_log(LOG_WARNING, "Offset is larger than the file size.\n");
00160             offset = contents_len * -1;
00161          }
00162          ast_copy_string(buf, &contents[contents_len + offset], length);
00163       }
00164    } while (0);
00165 
00166    ast_free(contents);
00167 
00168    return res;
00169 }
00170 
00171 static struct ast_custom_function env_function = {
00172    .name = "ENV",
00173    .synopsis = "Gets or sets the environment variable specified",
00174    .syntax = "ENV(<envname>)",
00175    .read = env_read,
00176    .write = env_write,
00177 };
00178 
00179 static struct ast_custom_function stat_function = {
00180    .name = "STAT",
00181    .synopsis = "Does a check on the specified file",
00182    .syntax = "STAT(<flag>,<filename>)",
00183    .read = stat_read,
00184    .desc =
00185       "flag may be one of the following:\n"
00186       "  d - Checks if the file is a directory\n"
00187       "  e - Checks if the file exists\n"
00188       "  f - Checks if the file is a regular file\n"
00189       "  m - Returns the file mode (in octal)\n"
00190       "  s - Returns the size (in bytes) of the file\n"
00191       "  A - Returns the epoch at which the file was last accessed\n"
00192       "  C - Returns the epoch at which the inode was last changed\n"
00193       "  M - Returns the epoch at which the file was last modified\n",
00194 };
00195 
00196 static struct ast_custom_function file_function = {
00197    .name = "FILE",
00198    .synopsis = "Obtains the contents of a file",
00199    .syntax = "FILE(<filename>,<offset>,<length>)",
00200    .read = file_read,
00201    /*
00202     * Some enterprising programmer could probably add write functionality
00203     * to FILE(), although I'm not sure how useful it would be.  Hence why
00204     * it's called FILE and not READFILE (like the app was).
00205     */
00206    .desc =
00207 "<offset> may be specified as any number.  If negative, <offset> specifies\n"
00208 "    the number of bytes back from the end of the file.\n"
00209 "<length>, if specified, will limit the length of the data read to that size.\n",
00210 };
00211 
00212 static int unload_module(void)
00213 {
00214    int res = 0;
00215 
00216    res |= ast_custom_function_unregister(&env_function);
00217    res |= ast_custom_function_unregister(&stat_function);
00218    res |= ast_custom_function_unregister(&file_function);
00219 
00220    return res;
00221 }
00222 
00223 static int load_module(void)
00224 {
00225    int res = 0;
00226 
00227    res |= ast_custom_function_register(&env_function);
00228    res |= ast_custom_function_register(&stat_function);
00229    res |= ast_custom_function_register(&file_function);
00230 
00231    return res;
00232 }
00233 
00234 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");

Generated on Fri Jun 19 12:09:45 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7