2005-09-14 20:46:50 +00:00
/*
* Asterisk - - An open source telephony toolkit .
*
* Copyright ( C ) 1999 - 2005 , Digium , Inc .
*
* Mark Spencer < markster @ digium . com >
*
* See http : //www.asterisk.org for more information about
* the Asterisk project . Please do not directly contact
* any of the maintainers of this project for assistance ;
* the project provides a web site , mailing lists and IRC
* channels for your use .
*
* This program is free software , distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree .
*/
2011-07-14 20:28:54 +00:00
/*** MODULEINFO
< support_level > extended < / support_level >
* * */
2018-03-14 09:27:40 +00:00
# define ASTMM_LIBC ASTMM_IGNORE
2007-11-21 00:23:49 +00:00
# include "asterisk.h"
2007-08-15 19:21:27 +00:00
2007-11-21 00:23:49 +00:00
# include "asterisk/ast_expr.h"
2007-08-22 16:27:39 +00:00
2007-08-15 19:21:27 +00:00
# define AST_API_MODULE 1
# include "asterisk/inline_api.h"
2007-08-22 16:27:39 +00:00
# define AST_API_MODULE 1
# include "asterisk/lock.h"
2007-10-01 23:33:07 +00:00
# ifndef DEBUG_THREADS
enum ast_lock_type {
AST_MUTEX ,
AST_RDLOCK ,
AST_WRLOCK ,
} ;
# endif
2008-12-17 05:04:56 +00:00
# ifdef DEBUG_THREADLOCALS
# define MALLOC_FAILURE_MSG \
ast_log ( LOG_ERROR , " Memory Allocation Failure in function %s at line %d of %s \n " , func , lineno , file ) ;
2016-08-19 19:09:45 +00:00
void * attribute_malloc __ast_calloc ( size_t num , size_t len , const char * file , int lineno , const char * func ) ;
2008-12-17 05:04:56 +00:00
2016-08-19 19:09:45 +00:00
void * attribute_malloc __ast_calloc ( size_t num , size_t len , const char * file , int lineno , const char * func )
2008-12-17 05:04:56 +00:00
{
void * p ;
if ( ! ( p = calloc ( num , len ) ) )
MALLOC_FAILURE_MSG ;
return p ;
}
# endif
2013-09-10 17:50:13 +00:00
# ifdef DEBUG_THREADS
2007-10-01 23:26:50 +00:00
void ast_store_lock_info ( enum ast_lock_type type , const char * filename ,
2008-05-23 22:35:50 +00:00
int line_num , const char * func , const char * lock_name , void * lock_addr , struct ast_bt * bt ) ;
2007-10-01 23:20:20 +00:00
void ast_store_lock_info ( enum ast_lock_type type , const char * filename ,
2008-05-23 22:35:50 +00:00
int line_num , const char * func , const char * lock_name , void * lock_addr , struct ast_bt * bt )
2007-10-01 23:20:20 +00:00
{
/* not a lot to do in a standalone w/o threading! */
}
2008-05-23 22:35:50 +00:00
void ast_remove_lock_info ( void * lock_addr , struct ast_bt * bt ) ;
void ast_remove_lock_info ( void * lock_addr , struct ast_bt * bt )
{
/* not a lot to do in a standalone w/o threading! */
}
2008-05-29 12:12:07 +00:00
2017-10-23 05:55:18 +00:00
# ifdef HAVE_BKTR
2013-06-13 21:53:45 +00:00
int __ast_bt_get_addresses ( struct ast_bt * bt ) ;
int __ast_bt_get_addresses ( struct ast_bt * bt )
2008-05-29 12:12:07 +00:00
{
/* Suck it, you stupid utils directory! */
return 0 ;
}
2018-11-08 15:53:44 +00:00
struct ast_vector_string * __ast_bt_get_symbols ( void * * addresses , size_t num_frames ) ;
struct ast_vector_string * __ast_bt_get_symbols ( void * * addresses , size_t num_frames )
2010-12-18 00:08:13 +00:00
{
2018-11-08 15:53:44 +00:00
return NULL ;
2010-12-18 00:08:13 +00:00
}
2008-05-23 22:35:50 +00:00
# endif /* HAVE_BKTR */
2013-09-10 17:26:19 +00:00
void ast_suspend_lock_info ( void * lock_addr )
{
}
void ast_restore_lock_info ( void * lock_addr )
{
}
2008-05-23 22:35:50 +00:00
void ast_mark_lock_acquired ( void * ) ;
void ast_mark_lock_acquired ( void * foo )
{
/* not a lot to do in a standalone w/o threading! */
2007-10-01 23:20:20 +00:00
}
2013-09-10 17:50:13 +00:00
# endif /* DEBUG_THREADS */
2007-10-01 23:20:20 +00:00
2013-09-10 17:26:19 +00:00
2007-01-24 18:39:49 +00:00
static int global_lineno = 1 ;
static int global_expr_count = 0 ;
static int global_expr_max_size = 0 ;
static int global_expr_tot_size = 0 ;
static int global_warn_count = 0 ;
static int global_OK_count = 0 ;
2005-06-21 20:16:51 +00:00
struct varz
{
char varname [ 100 ] ; /* a really ultra-simple, space-wasting linked list of var=val data */
char varval [ 1000 ] ; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
struct varz * next ;
} ;
struct varz * global_varlist ;
/* Our own version of ast_log, since the expr parser uses it. */
2008-11-29 17:57:39 +00:00
void ast_log ( int level , const char * file , int line , const char * function , const char * fmt , . . . ) __attribute__ ( ( format ( printf , 5 , 6 ) ) ) ;
2005-06-21 20:16:51 +00:00
void ast_log ( int level , const char * file , int line , const char * function , const char * fmt , . . . )
{
va_list vars ;
va_start ( vars , fmt ) ;
2015-05-03 09:09:57 +00:00
2005-06-21 20:16:51 +00:00
printf ( " LOG: lev:%d file:%s line:%d func: %s " ,
level , file , line , function ) ;
vprintf ( fmt , vars ) ;
fflush ( stdout ) ;
va_end ( vars ) ;
}
2007-01-24 18:39:49 +00:00
char * find_var ( const char * varname ) ;
void set_var ( const char * varname , const char * varval ) ;
unsigned int check_expr ( char * buffer , char * error_report ) ;
int check_eval ( char * buffer , char * error_report ) ;
void parse_file ( const char * fname ) ;
2015-05-03 09:09:57 +00:00
int ast_add_profile ( const char * x , uint64_t scale ) { return 0 ; }
2007-12-10 16:48:18 +00:00
2005-06-21 20:16:51 +00:00
char * find_var ( const char * varname ) /* the list should be pretty short, if there's any list at all */
{
struct varz * t ;
for ( t = global_varlist ; t ; t = t - > next ) {
if ( ! strcmp ( t - > varname , varname ) ) {
return t - > varval ;
}
}
return 0 ;
}
2007-08-15 19:21:27 +00:00
void set_var ( const char * varname , const char * varval ) ;
2005-06-21 20:16:51 +00:00
void set_var ( const char * varname , const char * varval )
{
2007-01-24 18:39:49 +00:00
struct varz * t = ( struct varz * ) calloc ( 1 , sizeof ( struct varz ) ) ;
2006-11-06 18:05:11 +00:00
if ( ! t )
return ;
2005-06-21 20:16:51 +00:00
strcpy ( t - > varname , varname ) ;
strcpy ( t - > varval , varval ) ;
t - > next = global_varlist ;
global_varlist = t ;
}
2005-06-09 20:50:48 +00:00
2007-01-24 18:39:49 +00:00
unsigned int check_expr ( char * buffer , char * error_report )
2005-06-09 20:50:48 +00:00
{
2007-01-24 18:39:49 +00:00
char * cp ;
unsigned int warn_found = 0 ;
2005-06-09 20:50:48 +00:00
error_report [ 0 ] = 0 ;
2015-05-03 09:09:57 +00:00
2007-01-24 18:39:49 +00:00
for ( cp = buffer ; * cp ; + + cp )
{
switch ( * cp )
{
case ' " ' :
2005-06-09 20:50:48 +00:00
/* skip to the other end */
2007-01-24 18:39:49 +00:00
while ( * ( + + cp ) & & * cp ! = ' " ' ) ;
if ( * cp = = 0 )
{
fprintf ( stderr ,
" Trouble? Unterminated double quote found at line %d \n " ,
global_lineno ) ;
2005-06-09 20:50:48 +00:00
}
2007-01-24 18:39:49 +00:00
break ;
2015-05-03 09:09:57 +00:00
2007-01-24 18:39:49 +00:00
case ' > ' :
case ' < ' :
case ' ! ' :
if ( ( * ( cp + 1 ) = = ' = ' )
& & ( ( ( cp > buffer ) & & ( * ( cp - 1 ) ! = ' ' ) ) | | ( * ( cp + 2 ) ! = ' ' ) ) )
{
char msg [ 200 ] ;
snprintf ( msg ,
sizeof ( msg ) ,
" WARNING: line %d: '%c%c' operator not separated by spaces. This may lead to confusion. You may wish to use double quotes to quote the grouping it is in. Please check! \n " ,
global_lineno , * cp , * ( cp + 1 ) ) ;
strcat ( error_report , msg ) ;
+ + global_warn_count ;
+ + warn_found ;
2005-06-09 20:50:48 +00:00
}
2007-01-24 18:39:49 +00:00
break ;
2015-05-03 09:09:57 +00:00
2007-01-24 18:39:49 +00:00
case ' | ' :
case ' & ' :
case ' = ' :
case ' + ' :
case ' - ' :
case ' * ' :
case ' / ' :
case ' % ' :
case ' ? ' :
case ' : ' :
if ( ( ( cp > buffer ) & & ( * ( cp - 1 ) ! = ' ' ) ) | | ( * ( cp + 1 ) ! = ' ' ) )
{
char msg [ 200 ] ;
snprintf ( msg ,
sizeof ( msg ) ,
" WARNING: line %d: '%c' operator not separated by spaces. This may lead to confusion. You may wish to use double quotes to quote the grouping it is in. Please check! \n " ,
global_lineno , * cp ) ;
strcat ( error_report , msg ) ;
+ + global_warn_count ;
+ + warn_found ;
2005-06-09 20:50:48 +00:00
}
2007-01-24 18:39:49 +00:00
break ;
2005-06-09 20:50:48 +00:00
}
}
2007-01-24 18:39:49 +00:00
2005-06-09 20:50:48 +00:00
return warn_found ;
}
2007-08-15 19:21:27 +00:00
int check_eval ( char * buffer , char * error_report ) ;
In regards to changes for 9508, expr2 system choking on floating point numbers, I'm adding this update to round out (no pun intended) and make this FP-capable version of the Expr2 stuff interoperate better with previous integer-only usage, by providing Functions syntax, with 20 builtin functions for floating pt to integer conversions, and some general floating point math routines that might commonly be used also. Along with this, I made it so if a function was not a builtin, it will try and find it in the ast_custom_function list, and if found, execute it and collect the results. Thus, you can call system functions like CDR(), CHANNEL(), etc, from within $\[..\] exprs, without having to wrap them in $\{...\} (curly brace) notation. Did a valgrind on the standalone and made sure there's no mem leaks. Looks good. Updated the docs, too.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@73449 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-07-05 18:15:22 +00:00
struct ast_custom_function * ast_custom_function_find ( const char * name ) ;
struct ast_custom_function * ast_custom_function_find ( const char * name )
{
return 0 ;
}
2005-06-21 20:16:51 +00:00
int check_eval ( char * buffer , char * error_report )
{
2007-01-24 18:39:49 +00:00
char * cp , * ep ;
2005-08-29 22:03:37 +00:00
char s [ 4096 ] ;
2005-06-21 20:16:51 +00:00
char evalbuf [ 80000 ] ;
2005-08-29 22:03:37 +00:00
int result ;
2005-06-21 20:16:51 +00:00
error_report [ 0 ] = 0 ;
ep = evalbuf ;
for ( cp = buffer ; * cp ; cp + + ) {
if ( * cp = = ' $ ' & & * ( cp + 1 ) = = ' { ' ) {
int brack_lev = 1 ;
char * xp = cp + 2 ;
2015-05-03 09:09:57 +00:00
2005-06-21 20:16:51 +00:00
while ( * xp ) {
if ( * xp = = ' { ' )
brack_lev + + ;
else if ( * xp = = ' } ' )
brack_lev - - ;
2015-05-03 09:09:57 +00:00
2005-06-21 20:16:51 +00:00
if ( brack_lev = = 0 )
break ;
xp + + ;
}
if ( * xp = = ' } ' ) {
char varname [ 200 ] ;
char * val ;
2015-05-03 09:09:57 +00:00
2005-06-21 20:16:51 +00:00
strncpy ( varname , cp + 2 , xp - cp - 2 ) ;
varname [ xp - cp - 2 ] = 0 ;
cp = xp ;
val = find_var ( varname ) ;
if ( val ) {
char * z = val ;
while ( * z )
* ep + + = * z + + ;
}
else {
* ep + + = ' 5 ' ; /* why not */
* ep + + = ' 5 ' ;
* ep + + = ' 5 ' ;
}
}
else {
printf ( " Unterminated variable reference at line %d \n " , global_lineno ) ;
* ep + + = * cp ;
}
}
else if ( * cp = = ' \\ ' ) {
/* braindead simple elim of backslash */
cp + + ;
* ep + + = * cp ;
}
else
* ep + + = * cp ;
}
* ep + + = 0 ;
/* now, run the test */
In regards to changes for 9508, expr2 system choking on floating point numbers, I'm adding this update to round out (no pun intended) and make this FP-capable version of the Expr2 stuff interoperate better with previous integer-only usage, by providing Functions syntax, with 20 builtin functions for floating pt to integer conversions, and some general floating point math routines that might commonly be used also. Along with this, I made it so if a function was not a builtin, it will try and find it in the ast_custom_function list, and if found, execute it and collect the results. Thus, you can call system functions like CDR(), CHANNEL(), etc, from within $\[..\] exprs, without having to wrap them in $\{...\} (curly brace) notation. Did a valgrind on the standalone and made sure there's no mem leaks. Looks good. Updated the docs, too.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@73449 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-07-05 18:15:22 +00:00
result = ast_expr ( evalbuf , s , sizeof ( s ) , NULL ) ;
2005-08-29 22:03:37 +00:00
if ( result ) {
2005-06-21 20:16:51 +00:00
sprintf ( error_report , " line %d, evaluation of $[ %s ] result: %s \n " , global_lineno , evalbuf , s ) ;
return 1 ;
2005-08-29 22:03:37 +00:00
} else {
2005-06-21 20:16:51 +00:00
sprintf ( error_report , " line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR**** \n " , global_lineno , evalbuf ) ;
return 1 ;
}
}
2005-06-09 20:50:48 +00:00
2007-08-15 19:21:27 +00:00
void parse_file ( const char * fname ) ;
2005-06-21 20:16:51 +00:00
void parse_file ( const char * fname )
2005-06-09 20:50:48 +00:00
{
FILE * f = fopen ( fname , " r " ) ;
FILE * l = fopen ( " expr2_log " , " w " ) ;
int c1 ;
char last_char = 0 ;
char buffer [ 30000 ] ; /* I sure hope no expr gets this big! */
2015-05-03 09:09:57 +00:00
2005-06-21 20:16:51 +00:00
if ( ! f ) {
2007-01-24 18:39:49 +00:00
fprintf ( stderr , " Couldn't open %s for reading... need an extensions.conf file to parse! \n " , fname ) ;
2005-06-09 20:50:48 +00:00
exit ( 20 ) ;
}
2005-06-21 20:16:51 +00:00
if ( ! l ) {
2005-06-09 20:50:48 +00:00
fprintf ( stderr , " Couldn't open 'expr2_log' file for writing... please fix and re-run! \n " ) ;
exit ( 21 ) ;
}
2015-05-03 09:09:57 +00:00
2005-06-21 20:16:51 +00:00
global_lineno = 1 ;
2015-05-03 09:09:57 +00:00
2005-06-21 20:16:51 +00:00
while ( ( c1 = fgetc ( f ) ) ! = EOF ) {
if ( c1 = = ' \n ' )
global_lineno + + ;
else if ( c1 = = ' [ ' ) {
if ( last_char = = ' $ ' ) {
2005-06-09 20:50:48 +00:00
/* bingo, an expr */
int bracklev = 1 ;
int bufcount = 0 ;
int retval ;
char error_report [ 30000 ] ;
2015-05-03 09:09:57 +00:00
2005-06-21 20:16:51 +00:00
while ( ( c1 = fgetc ( f ) ) ! = EOF ) {
if ( c1 = = ' [ ' )
2005-06-09 20:50:48 +00:00
bracklev + + ;
2005-06-21 20:16:51 +00:00
else if ( c1 = = ' ] ' )
2005-06-09 20:50:48 +00:00
bracklev - - ;
2005-06-21 20:16:51 +00:00
if ( c1 = = ' \n ' ) {
fprintf ( l , " ERROR-- A newline in an expression? Weird! ...at line %d \n " , global_lineno ) ;
2005-06-09 20:50:48 +00:00
fclose ( f ) ;
fclose ( l ) ;
2005-06-21 20:16:51 +00:00
printf ( " --- ERROR --- A newline in the middle of an expression at line %d! \n " , global_lineno ) ;
2005-06-09 20:50:48 +00:00
}
2015-05-03 09:09:57 +00:00
2005-06-21 20:16:51 +00:00
if ( bracklev = = 0 )
2005-06-09 20:50:48 +00:00
break ;
buffer [ bufcount + + ] = c1 ;
}
2005-06-21 20:16:51 +00:00
if ( c1 = = EOF ) {
fprintf ( l , " ERROR-- End of File Reached in the middle of an Expr at line %d \n " , global_lineno ) ;
2005-06-09 20:50:48 +00:00
fclose ( f ) ;
fclose ( l ) ;
2005-06-21 20:16:51 +00:00
printf ( " --- ERROR --- EOF reached in middle of an expression at line %d! \n " , global_lineno ) ;
2005-06-09 20:50:48 +00:00
exit ( 22 ) ;
}
2015-05-03 09:09:57 +00:00
2005-06-09 20:50:48 +00:00
buffer [ bufcount ] = 0 ;
/* update stats */
2005-06-21 20:16:51 +00:00
global_expr_tot_size + = bufcount ;
global_expr_count + + ;
if ( bufcount > global_expr_max_size )
global_expr_max_size = bufcount ;
2015-05-03 09:09:57 +00:00
2005-06-09 20:50:48 +00:00
retval = check_expr ( buffer , error_report ) ; /* check_expr should bump the warning counter */
2005-06-21 20:16:51 +00:00
if ( retval ! = 0 ) {
2005-06-09 20:50:48 +00:00
/* print error report */
2015-05-03 09:09:57 +00:00
printf ( " Warning(s) at line %d, expression: $[%s]; see expr2_log file for details \n " ,
2005-06-21 20:16:51 +00:00
global_lineno , buffer ) ;
2005-06-09 20:50:48 +00:00
fprintf ( l , " %s " , error_report ) ;
}
2005-06-21 20:16:51 +00:00
else {
printf ( " OK -- $[%s] at line %d \n " , buffer , global_lineno ) ;
global_OK_count + + ;
2005-06-09 20:50:48 +00:00
}
2005-06-21 20:16:51 +00:00
error_report [ 0 ] = 0 ;
retval = check_eval ( buffer , error_report ) ;
fprintf ( l , " %s " , error_report ) ;
2005-06-09 20:50:48 +00:00
}
}
last_char = c1 ;
}
printf ( " Summary: \n Expressions detected: %d \n Expressions OK: %d \n Total # Warnings: %d \n Longest Expr: %d chars \n Ave expr len: %d chars \n " ,
2005-06-21 20:16:51 +00:00
global_expr_count ,
global_OK_count ,
global_warn_count ,
global_expr_max_size ,
( global_expr_count ) ? global_expr_tot_size / global_expr_count : 0 ) ;
2015-05-03 09:09:57 +00:00
2005-06-09 20:50:48 +00:00
fclose ( f ) ;
fclose ( l ) ;
}
2007-01-24 18:39:49 +00:00
int main ( int argc , char * * argv )
2005-06-09 20:50:48 +00:00
{
2005-06-21 20:16:51 +00:00
int argc1 ;
char * eq ;
2015-05-03 09:09:57 +00:00
2005-06-21 20:16:51 +00:00
if ( argc < 2 ) {
2007-01-24 18:39:49 +00:00
printf ( " check_expr -- a program to look thru extensions.conf files for $[...] expressions, \n " ) ;
printf ( " and run them thru the parser, looking for problems \n " ) ;
2005-06-09 20:50:48 +00:00
printf ( " Hey-- give me a path to an extensions.conf file! \n " ) ;
2007-01-24 18:39:49 +00:00
printf ( " You can also follow the file path with a series of variable decls, \n " ) ;
printf ( " of the form, varname=value, each separated from the next by spaces. \n " ) ;
printf ( " (this might allow you to avoid division by zero messages, check that math \n " ) ;
printf ( " is being done correctly, etc.) \n " ) ;
printf ( " Note that messages about operators not being surrounded by spaces is merely to alert \n " ) ;
printf ( " you to possible problems where you might be expecting those operators as part of a string. \n " ) ;
printf ( " (to include operators in a string, wrap with double quotes!) \n " ) ;
2015-05-03 09:09:57 +00:00
2005-06-09 20:50:48 +00:00
exit ( 19 ) ;
}
2005-06-21 20:16:51 +00:00
global_varlist = 0 ;
for ( argc1 = 2 ; argc1 < argc ; argc1 + + ) {
if ( ( eq = strchr ( argv [ argc1 ] , ' = ' ) ) ) {
* eq = 0 ;
set_var ( argv [ argc1 ] , eq + 1 ) ;
}
}
/* parse command args for x=y and set varz */
2015-05-03 09:09:57 +00:00
2005-06-09 20:50:48 +00:00
parse_file ( argv [ 1 ] ) ;
2007-01-24 18:39:49 +00:00
return 0 ;
2005-06-09 20:50:48 +00:00
}