Mon Mar 20 08:25:47 2006

Asterisk developer's documentation


Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

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"

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.
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

char * app_readfile = "ReadFile"
 LOCAL_USER_DECL
char * readfile_descrip
char * readfile_synopsis = "ReadFile(varname=file,length)"
 STANDARD_LOCAL_USER
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 128 of file app_readfile.c.

00129 {
00130    return tdesc;
00131 }

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 140 of file app_readfile.c.

00141 {
00142    return ASTERISK_GPL_KEY;
00143 }

int load_module void   ) 
 

Initialize the module.

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.

Definition at line 123 of file app_readfile.c.

References app_readfile, ast_register_application(), readfile_descrip, readfile_exec(), and readfile_synopsis.

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

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, "%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    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    LOCAL_USER_REMOVE(u);
00108    return res;
00109 }

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 112 of file app_readfile.c.

References app_readfile, and ast_unregister_application().

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

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 133 of file app_readfile.c.

References STANDARD_USECOUNT.

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


Variable Documentation

char* app_readfile = "ReadFile" [static]
 

Definition at line 45 of file app_readfile.c.

Referenced by load_module(), and unload_module().

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.

Referenced by load_module().

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

Definition at line 47 of file app_readfile.c.

Referenced by load_module().

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.


Generated on Mon Mar 20 08:25:47 2006 for Asterisk - the Open Source PBX by  doxygen 1.3.9.1