Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:11 2009

## Asterisk developer's documentation

# app_readfile.c File Reference

ReadFile application -- Reads in a File for you. More...

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <unistd.h>`
`#include <string.h>`
`#include "asterisk.h"`
`#include "asterisk/file.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/options.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/pbx.h"`
`#include "asterisk/app.h"`
`#include "asterisk/module.h"`

Include dependency graph for app_readfile.c:

Go to the source code of this file.

## Functions

char * | description (void)
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
int | load_module (void)
| Initialize the module.
static int | readfile_exec (struct ast_channel *chan, void *data)
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

static char * | app_readfile = "ReadFile"
| LOCAL_USER_DECL
static char * | readfile_descrip
static char * | readfile_synopsis = "ReadFile(varname=file,length)"
| STANDARD_LOCAL_USER
static char * | tdesc = "Stores output of file into a variable"

## Detailed Description

ReadFile application -- Reads in a File for you.

Definition in file app_readfile.c.

## Function Documentation

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 129 of file app_readfile.c.

    00130 {
    00131    return tdesc;
    00132 }

char* key | ( | void |  | ) |

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

     char *key(void) {
             return ASTERISK_GPL_KEY;
     }

Returns:
  ASTERISK_GPL_KEY

Definition at line 141 of file app_readfile.c.

References ASTERISK_GPL_KEY.

    00142 {
    00143    return ASTERISK_GPL_KEY;
    00144 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
  int Always 0.

TE STUFF END

Definition at line 124 of file app_readfile.c.

References ast_register_application(), and readfile_exec().

    00125 {
    00126    return ast_register_application(app_readfile, readfile_exec, readfile_synopsis, readfile_descrip);
    00127 }

static int readfile_exec | ( | struct ast_channel * | chan,
|  | void * | data |
| ) |  |  | `[static]`

Definition at line 60 of file app_readfile.c.

References ast_log(), ast_read_textfile(), ast_strdupa, ast_strlen_zero(), free, LOCAL_USER_ADD, LOCAL_USER_REMOVE, LOG_ERROR, LOG_WARNING, pbx_builtin_setvar_helper(), s, and strsep().

Referenced by load_module().

    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, "%30d", &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 }

int unload_module | ( | void |  | ) |

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
  Zero on success, or non-zero on error.

Definition at line 113 of file app_readfile.c.

References ast_unregister_application(), and STANDARD_HANGUP_LOCALUSERS.

    00114 {
    00115    int res;
    00116
    00117    res = ast_unregister_application(app_readfile);
    00118
    00119    STANDARD_HANGUP_LOCALUSERS;
    00120
    00121    return res;
    00122 }

int usecount | ( | void |  | ) |

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
  The module's usecount.

Definition at line 134 of file app_readfile.c.

References STANDARD_USECOUNT.

    00135 {
    00136    int res;
    00137    STANDARD_USECOUNT(res);
    00138    return res;
    00139 }

## Variable Documentation

char* app_readfile = "ReadFile"`[static]`

Definition at line 45 of file app_readfile.c.

LOCAL_USER_DECL

Definition at line 57 of file app_readfile.c.

char* readfile_descrip`[static]`

Initial value:

    "ReadFile(varname=file,length)\n"
    "  Varname - Result stored here.\n"
    "  File - The name of the file to read.\n"
    "  Length - Maximum number of characters to capture.\n"

Definition at line 49 of file app_readfile.c.

char* readfile_synopsis = "ReadFile(varname=file,length)"`[static]`

Definition at line 47 of file app_readfile.c.

STANDARD_LOCAL_USER

Definition at line 55 of file app_readfile.c.

char* tdesc = "Stores output of file into a variable"`[static]`

Definition at line 43 of file app_readfile.c.