| 1 |
%{ |
| 2 |
/* Written by Pace Willisson ([email protected]) |
| 3 |
* and placed in the public domain. |
| 4 |
* |
| 5 |
* Largely rewritten by J.T. Conklin ([email protected]) |
| 6 |
* |
| 7 |
* And then overhauled twice by Steve Murphy ([email protected]) |
| 8 |
* to add double-quoted strings, allow mult. spaces, improve |
| 9 |
* error messages, and then to fold in a flex scanner for the |
| 10 |
* yylex operation. |
| 11 |
* |
| 12 |
* $FreeBSD: src/bin/expr/expr.y,v 1.16 2000/07/22 10:59:36 se Exp $ |
| 13 |
*/ |
| 14 |
|
| 15 |
#define ASTMM_LIBC ASTMM_REDIRECT |
| 16 |
#include "asterisk.h" |
| 17 |
|
| 18 |
#include <sys/types.h> |
| 19 |
#include <stdio.h> |
| 20 |
|
| 21 |
#if defined(STANDALONE) || defined(STANDALONE2) |
| 22 |
#ifndef __USE_ISOC99 |
| 23 |
#define __USE_ISOC99 1 |
| 24 |
#endif |
| 25 |
#endif |
| 26 |
|
| 27 |
#ifdef __USE_ISOC99 |
| 28 |
#define FP___PRINTF "%.18Lg" |
| 29 |
#define FP___TYPE long double |
| 30 |
#else |
| 31 |
#define FP___PRINTF "%.16g" |
| 32 |
#define FP___TYPE double |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifdef HAVE_COSL |
| 36 |
#define FUNC_COS cosl |
| 37 |
#elif defined(HAVE_COS) |
| 38 |
#define FUNC_COS (long double)cos |
| 39 |
#endif |
| 40 |
|
| 41 |
#ifdef HAVE_SINL |
| 42 |
#define FUNC_SIN sinl |
| 43 |
#elif defined(HAVE_SIN) |
| 44 |
#define FUNC_SIN (long double)sin |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifdef HAVE_TANL |
| 48 |
#define FUNC_TAN tanl |
| 49 |
#elif defined(HAVE_TAN) |
| 50 |
#define FUNC_TAN (long double)tan |
| 51 |
#endif |
| 52 |
|
| 53 |
#ifdef HAVE_ACOSL |
| 54 |
#define FUNC_ACOS acosl |
| 55 |
#elif defined(HAVE_ACOS) |
| 56 |
#define FUNC_ACOS (long double)acos |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifdef HAVE_ASINL |
| 60 |
#define FUNC_ASIN asinl |
| 61 |
#elif defined(HAVE_ASIN) |
| 62 |
#define FUNC_ASIN (long double)asin |
| 63 |
#endif |
| 64 |
|
| 65 |
#ifdef HAVE_ATANL |
| 66 |
#define FUNC_ATAN atanl |
| 67 |
#elif defined(HAVE_ATAN) |
| 68 |
#define FUNC_ATAN (long double)atan |
| 69 |
#endif |
| 70 |
|
| 71 |
#ifdef HAVE_ATAN2L |
| 72 |
#define FUNC_ATAN2 atan2l |
| 73 |
#elif defined(HAVE_ATAN2) |
| 74 |
#define FUNC_ATAN2 (long double)atan2 |
| 75 |
#endif |
| 76 |
|
| 77 |
#ifdef HAVE_POWL |
| 78 |
#define FUNC_POW powl |
| 79 |
#elif defined(HAVE_POW) |
| 80 |
#define FUNC_POW (long double)pow |
| 81 |
#endif |
| 82 |
|
| 83 |
#ifdef HAVE_SQRTL |
| 84 |
#define FUNC_SQRT sqrtl |
| 85 |
#elif defined(HAVE_SQRT) |
| 86 |
#define FUNC_SQRT (long double)sqrt |
| 87 |
#endif |
| 88 |
|
| 89 |
#ifdef HAVE_RINTL |
| 90 |
#define FUNC_RINT rintl |
| 91 |
#elif defined(HAVE_RINT) |
| 92 |
#define FUNC_RINT (long double)rint |
| 93 |
#endif |
| 94 |
|
| 95 |
#ifdef HAVE_EXPL |
| 96 |
#define FUNC_EXP expl |
| 97 |
#elif defined(HAVE_EXP) |
| 98 |
#define FUNC_EXP (long double)exp |
| 99 |
#endif |
| 100 |
|
| 101 |
#ifdef HAVE_LOGL |
| 102 |
#define FUNC_LOG logl |
| 103 |
#elif defined(HAVE_LOG) |
| 104 |
#define FUNC_LOG (long double)log |
| 105 |
#endif |
| 106 |
|
| 107 |
#ifdef HAVE_REMAINDERL |
| 108 |
#define FUNC_REMAINDER remainderl |
| 109 |
#elif defined(HAVE_REMAINDER) |
| 110 |
#define FUNC_REMAINDER (long double)remainder |
| 111 |
#endif |
| 112 |
|
| 113 |
#ifdef HAVE_FMODL |
| 114 |
#define FUNC_FMOD fmodl |
| 115 |
#elif defined(HAVE_FMOD) |
| 116 |
#define FUNC_FMOD (long double)fmod |
| 117 |
#endif |
| 118 |
|
| 119 |
#ifdef HAVE_STRTOLD |
| 120 |
#define FUNC_STRTOD strtold |
| 121 |
#elif defined(HAVE_STRTOD) |
| 122 |
#define FUNC_STRTOD (long double)strtod |
| 123 |
#endif |
| 124 |
|
| 125 |
#ifdef HAVE_FLOORL |
| 126 |
#define FUNC_FLOOR floorl |
| 127 |
#elif defined(HAVE_FLOOR) |
| 128 |
#define FUNC_FLOOR (long double)floor |
| 129 |
#endif |
| 130 |
|
| 131 |
#ifdef HAVE_CEILL |
| 132 |
#define FUNC_CEIL ceill |
| 133 |
#elif defined(HAVE_CEIL) |
| 134 |
#define FUNC_CEIL (long double)ceil |
| 135 |
#endif |
| 136 |
|
| 137 |
#ifdef HAVE_ROUNDL |
| 138 |
#define FUNC_ROUND roundl |
| 139 |
#elif defined(HAVE_ROUND) |
| 140 |
#define FUNC_ROUND (long double)round |
| 141 |
#endif |
| 142 |
|
| 143 |
#ifdef HAVE_TRUNCL |
| 144 |
#define FUNC_TRUNC truncl |
| 145 |
#elif defined(HAVE_TRUNC) |
| 146 |
#define FUNC_TRUNC (long double)trunc |
| 147 |
#endif |
| 148 |
|
| 149 |
/*! \note |
| 150 |
* Oddly enough, some platforms have some ISO C99 functions, but not others, so |
| 151 |
* we define the missing functions in terms of their mathematical identities. |
| 152 |
*/ |
| 153 |
#ifdef HAVE_EXP2L |
| 154 |
#define FUNC_EXP2 exp2l |
| 155 |
#elif (defined(HAVE_EXPL) && defined(HAVE_LOGL)) |
| 156 |
#define FUNC_EXP2(x) expl((x) * logl(2.0)) |
| 157 |
#elif (defined(HAVE_EXP) && defined(HAVE_LOG)) |
| 158 |
#define FUNC_EXP2(x) (long double)exp((x) * log(2.0)) |
| 159 |
#endif |
| 160 |
|
| 161 |
#ifdef HAVE_EXP10L |
| 162 |
#define FUNC_EXP10 exp10l |
| 163 |
#elif (defined(HAVE_EXPL) && defined(HAVE_LOGL)) |
| 164 |
#define FUNC_EXP10(x) expl((x) * logl(10.0)) |
| 165 |
#elif (defined(HAVE_EXP) && defined(HAVE_LOG)) |
| 166 |
#define FUNC_EXP10(x) (long double)exp((x) * log(10.0)) |
| 167 |
#endif |
| 168 |
|
| 169 |
#ifdef HAVE_LOG2L |
| 170 |
#define FUNC_LOG2 log2l |
| 171 |
#elif defined(HAVE_LOGL) |
| 172 |
#define FUNC_LOG2(x) (logl(x) / logl(2.0)) |
| 173 |
#elif defined(HAVE_LOG10L) |
| 174 |
#define FUNC_LOG2(x) (log10l(x) / log10l(2.0)) |
| 175 |
#elif defined(HAVE_LOG2) |
| 176 |
#define FUNC_LOG2 (long double)log2 |
| 177 |
#elif defined(HAVE_LOG) |
| 178 |
#define FUNC_LOG2(x) ((long double)log(x) / log(2.0)) |
| 179 |
#endif |
| 180 |
|
| 181 |
#ifdef HAVE_LOG10L |
| 182 |
#define FUNC_LOG10 log10l |
| 183 |
#elif defined(HAVE_LOGL) |
| 184 |
#define FUNC_LOG10(x) (logl(x) / logl(10.0)) |
| 185 |
#elif defined(HAVE_LOG2L) |
| 186 |
#define FUNC_LOG10(x) (log2l(x) / log2l(10.0)) |
| 187 |
#elif defined(HAVE_LOG10) |
| 188 |
#define FUNC_LOG10(x) (long double)log10(x) |
| 189 |
#elif defined(HAVE_LOG) |
| 190 |
#define FUNC_LOG10(x) ((long double)log(x) / log(10.0)) |
| 191 |
#endif |
| 192 |
|
| 193 |
|
| 194 |
#include <stdlib.h> |
| 195 |
#ifndef _GNU_SOURCE |
| 196 |
#define _GNU_SOURCE |
| 197 |
#endif |
| 198 |
#include <string.h> |
| 199 |
#include <math.h> |
| 200 |
#include <locale.h> |
| 201 |
#include <unistd.h> |
| 202 |
#include <ctype.h> |
| 203 |
#if !defined(SOLARIS) && !defined(__CYGWIN__) |
| 204 |
/* #include <err.h> */ |
| 205 |
#else |
| 206 |
#define quad_t int64_t |
| 207 |
#endif |
| 208 |
#include <errno.h> |
| 209 |
#include <regex.h> |
| 210 |
#include <limits.h> |
| 211 |
|
| 212 |
#include "asterisk/ast_expr.h" |
| 213 |
#include "asterisk/logger.h" |
| 214 |
#if !defined(STANDALONE) && !defined(STANDALONE2) |
| 215 |
#include "asterisk/pbx.h" |
| 216 |
#endif |
| 217 |
|
| 218 |
#if defined(LONG_LONG_MIN) && !defined(QUAD_MIN) |
| 219 |
#define QUAD_MIN LONG_LONG_MIN |
| 220 |
#endif |
| 221 |
#if defined(LONG_LONG_MAX) && !defined(QUAD_MAX) |
| 222 |
#define QUAD_MAX LONG_LONG_MAX |
| 223 |
#endif |
| 224 |
|
| 225 |
# if ! defined(QUAD_MIN) |
| 226 |
# define QUAD_MIN (-0x7fffffffffffffffLL-1) |
| 227 |
# endif |
| 228 |
# if ! defined(QUAD_MAX) |
| 229 |
# define QUAD_MAX (0x7fffffffffffffffLL) |
| 230 |
# endif |
| 231 |
#define YYENABLE_NLS 0 |
| 232 |
#define YYPARSE_PARAM parseio |
| 233 |
#define YYLEX_PARAM ((struct parse_io *)parseio)->scanner |
| 234 |
#define YYERROR_VERBOSE 1 |
| 235 |
extern char extra_error_message[4095]; |
| 236 |
extern int extra_error_message_supplied; |
| 237 |
|
| 238 |
enum valtype { |
| 239 |
AST_EXPR_number, AST_EXPR_numeric_string, AST_EXPR_string |
| 240 |
} ; |
| 241 |
|
| 242 |
#if defined(STANDALONE) || defined(STANDALONE2) |
| 243 |
void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6))); |
| 244 |
#endif |
| 245 |
|
| 246 |
struct val { |
| 247 |
enum valtype type; |
| 248 |
union { |
| 249 |
char *s; |
| 250 |
FP___TYPE i; /* either long double, or just double, on a bad day */ |
| 251 |
} u; |
| 252 |
} ; |
| 253 |
|
| 254 |
enum node_type { |
| 255 |
AST_EXPR_NODE_COMMA, AST_EXPR_NODE_STRING, AST_EXPR_NODE_VAL |
| 256 |
} ; |
| 257 |
|
| 258 |
struct expr_node |
| 259 |
{ |
| 260 |
enum node_type type; |
| 261 |
struct val *val; |
| 262 |
struct expr_node *left; |
| 263 |
struct expr_node *right; |
| 264 |
}; |
| 265 |
|
| 266 |
|
| 267 |
typedef void *yyscan_t; |
| 268 |
|
| 269 |
struct parse_io |
| 270 |
{ |
| 271 |
char *string; |
| 272 |
struct val *val; |
| 273 |
yyscan_t scanner; |
| 274 |
struct ast_channel *chan; |
| 275 |
}; |
| 276 |
|
| 277 |
static int chk_div __P((FP___TYPE, FP___TYPE)); |
| 278 |
static int chk_minus __P((FP___TYPE, FP___TYPE, FP___TYPE)); |
| 279 |
static int chk_plus __P((FP___TYPE, FP___TYPE, FP___TYPE)); |
| 280 |
static int chk_times __P((FP___TYPE, FP___TYPE, FP___TYPE)); |
| 281 |
static void free_value __P((struct val *)); |
| 282 |
static int is_zero_or_null __P((struct val *)); |
| 283 |
static int isstring __P((struct val *)); |
| 284 |
static struct val *make_number __P((FP___TYPE)); |
| 285 |
static struct val *make_str __P((const char *)); |
| 286 |
static struct val *op_and __P((struct val *, struct val *)); |
| 287 |
static struct val *op_colon __P((struct val *, struct val *)); |
| 288 |
static struct val *op_eqtilde __P((struct val *, struct val *)); |
| 289 |
static struct val *op_tildetilde __P((struct val *, struct val *)); |
| 290 |
static struct val *op_div __P((struct val *, struct val *)); |
| 291 |
static struct val *op_eq __P((struct val *, struct val *)); |
| 292 |
static struct val *op_ge __P((struct val *, struct val *)); |
| 293 |
static struct val *op_gt __P((struct val *, struct val *)); |
| 294 |
static struct val *op_le __P((struct val *, struct val *)); |
| 295 |
static struct val *op_lt __P((struct val *, struct val *)); |
| 296 |
static struct val *op_cond __P((struct val *, struct val *, struct val *)); |
| 297 |
static struct val *op_minus __P((struct val *, struct val *)); |
| 298 |
static struct val *op_negate __P((struct val *)); |
| 299 |
static struct val *op_compl __P((struct val *)); |
| 300 |
static struct val *op_ne __P((struct val *, struct val *)); |
| 301 |
static struct val *op_or __P((struct val *, struct val *)); |
| 302 |
static struct val *op_plus __P((struct val *, struct val *)); |
| 303 |
static struct val *op_rem __P((struct val *, struct val *)); |
| 304 |
static struct val *op_times __P((struct val *, struct val *)); |
| 305 |
static struct val *op_func(struct val *funcname, struct expr_node *arglist, struct ast_channel *chan); |
| 306 |
static int to_number __P((struct val *)); |
| 307 |
static void to_string __P((struct val *)); |
| 308 |
static struct expr_node *alloc_expr_node(enum node_type); |
| 309 |
static void destroy_arglist(struct expr_node *arglist); |
| 310 |
|
| 311 |
/* uh, if I want to predeclare yylex with a YYLTYPE, I have to predeclare the yyltype... sigh */ |
| 312 |
typedef struct yyltype |
| 313 |
{ |
| 314 |
int first_line; |
| 315 |
int first_column; |
| 316 |
|
| 317 |
int last_line; |
| 318 |
int last_column; |
| 319 |
} yyltype; |
| 320 |
|
| 321 |
# define YYLTYPE yyltype |
| 322 |
# define YYLTYPE_IS_TRIVIAL 1 |
| 323 |
|
| 324 |
/* we will get warning about no prototype for yylex! But we can't |
| 325 |
define it here, we have no definition yet for YYSTYPE. */ |
| 326 |
|
| 327 |
int ast_yyerror(const char *,YYLTYPE *, struct parse_io *); |
| 328 |
|
| 329 |
/* I wanted to add args to the yyerror routine, so I could print out |
| 330 |
some useful info about the error. Not as easy as it looks, but it |
| 331 |
is possible. */ |
| 332 |
#define ast_yyerror(x) ast_yyerror(x,&yyloc,parseio) |
| 333 |
#define DESTROY(x) {if((x)->type == AST_EXPR_numeric_string || (x)->type == AST_EXPR_string) free((x)->u.s); (x)->u.s = 0; free(x);} |
| 334 |
%} |
| 335 |
|
| 336 |
%pure-parser |
| 337 |
%locations |
| 338 |
/* %debug for when you are having big problems */ |
| 339 |
|
| 340 |
/* %name-prefix="ast_yy" */ |
| 341 |
|
| 342 |
%union |
| 343 |
{ |
| 344 |
struct val *val; |
| 345 |
struct expr_node *arglist; |
| 346 |
} |
| 347 |
|
| 348 |
%{ |
| 349 |
extern int ast_yylex __P((YYSTYPE *, YYLTYPE *, yyscan_t)); |
| 350 |
%} |
| 351 |
%left <val> TOK_COMMA |
| 352 |
%left <val> TOK_COND TOK_COLONCOLON |
| 353 |
%left <val> TOK_OR |
| 354 |
%left <val> TOK_AND |
| 355 |
%left <val> TOK_EQ TOK_GT TOK_LT TOK_GE TOK_LE TOK_NE |
| 356 |
%left <val> TOK_PLUS TOK_MINUS |
| 357 |
%left <val> TOK_MULT TOK_DIV TOK_MOD |
| 358 |
%right <val> TOK_COMPL |
| 359 |
%left <val> TOK_COLON TOK_EQTILDE TOK_TILDETILDE |
| 360 |
%left <val> TOK_RP TOK_LP |
| 361 |
|
| 362 |
%token <val> TOKEN |
| 363 |
%type <arglist> arglist |
| 364 |
%type <val> start expr |
| 365 |
|
| 366 |
%destructor { free_value($$); } expr TOKEN TOK_COND TOK_COLONCOLON TOK_OR TOK_AND TOK_EQ |
| 367 |
TOK_GT TOK_LT TOK_GE TOK_LE TOK_NE TOK_PLUS TOK_MINUS TOK_MULT TOK_DIV TOK_MOD TOK_COMPL TOK_COLON TOK_EQTILDE |
| 368 |
TOK_RP TOK_LP TOK_TILDETILDE |
| 369 |
|
| 370 |
%% |
| 371 |
|
| 372 |
start: expr { ((struct parse_io *)parseio)->val = (struct val *)calloc(sizeof(struct val),1); |
| 373 |
((struct parse_io *)parseio)->val->type = $1->type; |
| 374 |
if( $1->type == AST_EXPR_number ) |
| 375 |
((struct parse_io *)parseio)->val->u.i = $1->u.i; |
| 376 |
else |
| 377 |
((struct parse_io *)parseio)->val->u.s = $1->u.s; |
| 378 |
free($1); |
| 379 |
} |
| 380 |
| {/* nothing */ ((struct parse_io *)parseio)->val = (struct val *)calloc(sizeof(struct val),1); |
| 381 |
((struct parse_io *)parseio)->val->type = AST_EXPR_string; |
| 382 |
((struct parse_io *)parseio)->val->u.s = strdup(""); |
| 383 |
} |
| 384 |
|
| 385 |
; |
| 386 |
|
| 387 |
arglist: expr { $$ = alloc_expr_node(AST_EXPR_NODE_VAL); $$->val = $1;} |
| 388 |
| arglist TOK_COMMA expr %prec TOK_RP {struct expr_node *x = alloc_expr_node(AST_EXPR_NODE_VAL); |
| 389 |
struct expr_node *t; |
| 390 |
DESTROY($2); |
| 391 |
for (t=$1;t->right;t=t->right) |
| 392 |
; |
| 393 |
$$ = $1; t->right = x; x->val = $3;} |
| 394 |
| arglist TOK_COMMA %prec TOK_RP {struct expr_node *x = alloc_expr_node(AST_EXPR_NODE_VAL); |
| 395 |
struct expr_node *t; /* NULL args should OK */ |
| 396 |
DESTROY($2); |
| 397 |
for (t=$1;t->right;t=t->right) |
| 398 |
; |
| 399 |
$$ = $1; t->right = x; x->val = make_str("");} |
| 400 |
; |
| 401 |
|
| 402 |
expr: |
| 403 |
TOKEN TOK_LP arglist TOK_RP { $$ = op_func($1,$3, ((struct parse_io *)parseio)->chan); |
| 404 |
DESTROY($2); |
| 405 |
DESTROY($4); |
| 406 |
DESTROY($1); |
| 407 |
destroy_arglist($3); |
| 408 |
} |
| 409 |
| TOKEN {$$ = $1;} |
| 410 |
| TOK_LP expr TOK_RP { $$ = $2; |
| 411 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 412 |
@$.first_line=0; @$.last_line=0; |
| 413 |
DESTROY($1); DESTROY($3); } |
| 414 |
| expr TOK_OR expr { $$ = op_or ($1, $3); |
| 415 |
DESTROY($2); |
| 416 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 417 |
@$.first_line=0; @$.last_line=0;} |
| 418 |
| expr TOK_AND expr { $$ = op_and ($1, $3); |
| 419 |
DESTROY($2); |
| 420 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 421 |
@$.first_line=0; @$.last_line=0;} |
| 422 |
| expr TOK_EQ expr { $$ = op_eq ($1, $3); |
| 423 |
DESTROY($2); |
| 424 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 425 |
@$.first_line=0; @$.last_line=0;} |
| 426 |
| expr TOK_GT expr { $$ = op_gt ($1, $3); |
| 427 |
DESTROY($2); |
| 428 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 429 |
@$.first_line=0; @$.last_line=0;} |
| 430 |
| expr TOK_LT expr { $$ = op_lt ($1, $3); |
| 431 |
DESTROY($2); |
| 432 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 433 |
@$.first_line=0; @$.last_line=0;} |
| 434 |
| expr TOK_GE expr { $$ = op_ge ($1, $3); |
| 435 |
DESTROY($2); |
| 436 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 437 |
@$.first_line=0; @$.last_line=0;} |
| 438 |
| expr TOK_LE expr { $$ = op_le ($1, $3); |
| 439 |
DESTROY($2); |
| 440 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 441 |
@$.first_line=0; @$.last_line=0;} |
| 442 |
| expr TOK_NE expr { $$ = op_ne ($1, $3); |
| 443 |
DESTROY($2); |
| 444 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 445 |
@$.first_line=0; @$.last_line=0;} |
| 446 |
| expr TOK_PLUS expr { $$ = op_plus ($1, $3); |
| 447 |
DESTROY($2); |
| 448 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 449 |
@$.first_line=0; @$.last_line=0;} |
| 450 |
| expr TOK_MINUS expr { $$ = op_minus ($1, $3); |
| 451 |
DESTROY($2); |
| 452 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 453 |
@$.first_line=0; @$.last_line=0;} |
| 454 |
| TOK_MINUS expr %prec TOK_COMPL { $$ = op_negate ($2); |
| 455 |
DESTROY($1); |
| 456 |
@$.first_column = @1.first_column; @$.last_column = @2.last_column; |
| 457 |
@$.first_line=0; @$.last_line=0;} |
| 458 |
| TOK_COMPL expr { $$ = op_compl ($2); |
| 459 |
DESTROY($1); |
| 460 |
@$.first_column = @1.first_column; @$.last_column = @2.last_column; |
| 461 |
@$.first_line=0; @$.last_line=0;} |
| 462 |
| expr TOK_MULT expr { $$ = op_times ($1, $3); |
| 463 |
DESTROY($2); |
| 464 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 465 |
@$.first_line=0; @$.last_line=0;} |
| 466 |
| expr TOK_DIV expr { $$ = op_div ($1, $3); |
| 467 |
DESTROY($2); |
| 468 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 469 |
@$.first_line=0; @$.last_line=0;} |
| 470 |
| expr TOK_MOD expr { $$ = op_rem ($1, $3); |
| 471 |
DESTROY($2); |
| 472 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 473 |
@$.first_line=0; @$.last_line=0;} |
| 474 |
| expr TOK_COLON expr { $$ = op_colon ($1, $3); |
| 475 |
DESTROY($2); |
| 476 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 477 |
@$.first_line=0; @$.last_line=0;} |
| 478 |
| expr TOK_EQTILDE expr { $$ = op_eqtilde ($1, $3); |
| 479 |
DESTROY($2); |
| 480 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 481 |
@$.first_line=0; @$.last_line=0;} |
| 482 |
| expr TOK_COND expr TOK_COLONCOLON expr { $$ = op_cond ($1, $3, $5); |
| 483 |
DESTROY($2); |
| 484 |
DESTROY($4); |
| 485 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 486 |
@$.first_line=0; @$.last_line=0;} |
| 487 |
| expr TOK_TILDETILDE expr { $$ = op_tildetilde ($1, $3); |
| 488 |
DESTROY($2); |
| 489 |
@$.first_column = @1.first_column; @$.last_column = @3.last_column; |
| 490 |
@$.first_line=0; @$.last_line=0;} |
| 491 |
; |
| 492 |
|
| 493 |
%% |
| 494 |
|
| 495 |
static struct expr_node *alloc_expr_node(enum node_type nt) |
| 496 |
{ |
| 497 |
struct expr_node *x = calloc(1,sizeof(struct expr_node)); |
| 498 |
if (!x) { |
| 499 |
ast_log(LOG_ERROR, "Allocation for expr_node FAILED!!\n"); |
| 500 |
return 0; |
| 501 |
} |
| 502 |
x->type = nt; |
| 503 |
return x; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
static struct val * |
| 509 |
make_number (FP___TYPE i) |
| 510 |
{ |
| 511 |
struct val *vp; |
| 512 |
|
| 513 |
vp = (struct val *) malloc (sizeof (*vp)); |
| 514 |
if (vp == NULL) { |
| 515 |
ast_log(LOG_WARNING, "malloc() failed\n"); |
| 516 |
return(NULL); |
| 517 |
} |
| 518 |
|
| 519 |
vp->type = AST_EXPR_number; |
| 520 |
vp->u.i = i; |
| 521 |
return vp; |
| 522 |
} |
| 523 |
|
| 524 |
static struct val * |
| 525 |
make_str (const char *s) |
| 526 |
{ |
| 527 |
struct val *vp; |
| 528 |
size_t i; |
| 529 |
int isint; /* this started out being a test for an integer, but then ended up being a test for a float */ |
| 530 |
|
| 531 |
vp = (struct val *) malloc (sizeof (*vp)); |
| 532 |
if (vp == NULL || ((vp->u.s = strdup (s)) == NULL)) { |
| 533 |
if (vp) { |
| 534 |
free(vp); |
| 535 |
} |
| 536 |
ast_log(LOG_WARNING,"malloc() failed\n"); |
| 537 |
return(NULL); |
| 538 |
} |
| 539 |
|
| 540 |
for (i = 0, isint = (isdigit(s[0]) || s[0] == '-' || s[0]=='.'); isint && i < strlen(s); i++) |
| 541 |
{ |
| 542 |
if (!isdigit(s[i]) && s[i] != '.') { |
| 543 |
isint = 0; |
| 544 |
break; |
| 545 |
} |
| 546 |
} |
| 547 |
if (isint) |
| 548 |
vp->type = AST_EXPR_numeric_string; |
| 549 |
else |
| 550 |
vp->type = AST_EXPR_string; |
| 551 |
|
| 552 |
return vp; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
static void |
| 557 |
free_value (struct val *vp) |
| 558 |
{ |
| 559 |
if (vp==NULL) { |
| 560 |
return; |
| 561 |
} |
| 562 |
if (vp->type == AST_EXPR_string || vp->type == AST_EXPR_numeric_string) |
| 563 |
free (vp->u.s); |
| 564 |
free(vp); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
static int |
| 569 |
to_number (struct val *vp) |
| 570 |
{ |
| 571 |
FP___TYPE i; |
| 572 |
|
| 573 |
if (vp == NULL) { |
| 574 |
ast_log(LOG_WARNING,"vp==NULL in to_number()\n"); |
| 575 |
return(0); |
| 576 |
} |
| 577 |
|
| 578 |
if (vp->type == AST_EXPR_number) |
| 579 |
return 1; |
| 580 |
|
| 581 |
if (vp->type == AST_EXPR_string) |
| 582 |
return 0; |
| 583 |
|
| 584 |
/* vp->type == AST_EXPR_numeric_string, make it numeric */ |
| 585 |
errno = 0; |
| 586 |
i = FUNC_STRTOD(vp->u.s, (char**)0); /* either strtod, or strtold on a good day */ |
| 587 |
if (errno != 0) { |
| 588 |
ast_log(LOG_WARNING,"Conversion of %s to number under/overflowed!\n", vp->u.s); |
| 589 |
free(vp->u.s); |
| 590 |
vp->u.s = 0; |
| 591 |
return(0); |
| 592 |
} |
| 593 |
free (vp->u.s); |
| 594 |
vp->u.i = i; |
| 595 |
vp->type = AST_EXPR_number; |
| 596 |
return 1; |
| 597 |
} |
| 598 |
|
| 599 |
static void |
| 600 |
strip_quotes(struct val *vp) |
| 601 |
{ |
| 602 |
if (vp->type != AST_EXPR_string && vp->type != AST_EXPR_numeric_string) |
| 603 |
return; |
| 604 |
|
| 605 |
if( vp->u.s[0] == '"' && vp->u.s[strlen(vp->u.s)-1] == '"' ) |
| 606 |
{ |
| 607 |
char *f, *t; |
| 608 |
f = vp->u.s; |
| 609 |
t = vp->u.s; |
| 610 |
|
| 611 |
while( *f ) |
| 612 |
{ |
| 613 |
if( *f && *f != '"' ) |
| 614 |
*t++ = *f++; |
| 615 |
else |
| 616 |
f++; |
| 617 |
} |
| 618 |
*t = *f; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
static void |
| 623 |
to_string (struct val *vp) |
| 624 |
{ |
| 625 |
char *tmp; |
| 626 |
|
| 627 |
if (vp->type == AST_EXPR_string || vp->type == AST_EXPR_numeric_string) |
| 628 |
return; |
| 629 |
|
| 630 |
if (asprintf(&tmp, FP___PRINTF, vp->u.i) == -1) { |
| 631 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 632 |
return; |
| 633 |
} |
| 634 |
|
| 635 |
vp->type = AST_EXPR_string; |
| 636 |
vp->u.s = tmp; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
static int |
| 641 |
isstring (struct val *vp) |
| 642 |
{ |
| 643 |
/* only TRUE if this string is not a valid number */ |
| 644 |
return (vp->type == AST_EXPR_string); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
static int |
| 649 |
is_zero_or_null (struct val *vp) |
| 650 |
{ |
| 651 |
if (vp->type == AST_EXPR_number) { |
| 652 |
return (vp->u.i == 0); |
| 653 |
} else { |
| 654 |
return (*vp->u.s == 0 || (to_number(vp) && vp->u.i == 0)); |
| 655 |
} |
| 656 |
/* NOTREACHED */ |
| 657 |
} |
| 658 |
|
| 659 |
#ifdef STANDALONE2 |
| 660 |
|
| 661 |
void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) |
| 662 |
{ |
| 663 |
va_list vars; |
| 664 |
va_start(vars,fmt); |
| 665 |
|
| 666 |
printf("LOG: lev:%d file:%s line:%d func: %s ", |
| 667 |
level, file, line, function); |
| 668 |
vprintf(fmt, vars); |
| 669 |
fflush(stdout); |
| 670 |
va_end(vars); |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
int main(int argc,char **argv) { |
| 675 |
char s[4096]; |
| 676 |
char out[4096]; |
| 677 |
FILE *infile; |
| 678 |
|
| 679 |
if( !argv[1] ) |
| 680 |
exit(20); |
| 681 |
|
| 682 |
if( access(argv[1],F_OK)== 0 ) |
| 683 |
{ |
| 684 |
int ret; |
| 685 |
|
| 686 |
infile = fopen(argv[1],"r"); |
| 687 |
if( !infile ) |
| 688 |
{ |
| 689 |
printf("Sorry, couldn't open %s for reading!\n", argv[1]); |
| 690 |
exit(10); |
| 691 |
} |
| 692 |
while( fgets(s,sizeof(s),infile) ) |
| 693 |
{ |
| 694 |
if( s[strlen(s)-1] == '\n' ) |
| 695 |
s[strlen(s)-1] = 0; |
| 696 |
|
| 697 |
ret = ast_expr(s, out, sizeof(out), NULL); |
| 698 |
printf("Expression: %s Result: [%d] '%s'\n", |
| 699 |
s, ret, out); |
| 700 |
} |
| 701 |
fclose(infile); |
| 702 |
} |
| 703 |
else |
| 704 |
{ |
| 705 |
if (ast_expr(argv[1], s, sizeof(s), NULL)) |
| 706 |
printf("=====%s======\n",s); |
| 707 |
else |
| 708 |
printf("No result\n"); |
| 709 |
} |
| 710 |
return 0; |
| 711 |
} |
| 712 |
|
| 713 |
#endif |
| 714 |
|
| 715 |
#undef ast_yyerror |
| 716 |
#define ast_yyerror(x) ast_yyerror(x, YYLTYPE *yylloc, struct parse_io *parseio) |
| 717 |
|
| 718 |
/* I put the ast_yyerror func in the flex input file, |
| 719 |
because it refers to the buffer state. Best to |
| 720 |
let it access the BUFFER stuff there and not trying |
| 721 |
define all the structs, macros etc. in this file! */ |
| 722 |
|
| 723 |
static void destroy_arglist(struct expr_node *arglist) |
| 724 |
{ |
| 725 |
struct expr_node *arglist_next; |
| 726 |
|
| 727 |
while (arglist) |
| 728 |
{ |
| 729 |
arglist_next = arglist->right; |
| 730 |
if (arglist->val) |
| 731 |
free_value(arglist->val); |
| 732 |
arglist->val = 0; |
| 733 |
arglist->right = 0; |
| 734 |
free(arglist); |
| 735 |
arglist = arglist_next; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
#if !defined(STANDALONE) && !defined(STANDALONE2) |
| 740 |
static char *compose_func_args(struct expr_node *arglist) |
| 741 |
{ |
| 742 |
struct expr_node *t = arglist; |
| 743 |
char *argbuf; |
| 744 |
int total_len = 0; |
| 745 |
|
| 746 |
while (t) { |
| 747 |
if (t != arglist) |
| 748 |
total_len += 1; /* for the sep */ |
| 749 |
if (t->val) { |
| 750 |
if (t->val->type == AST_EXPR_number) |
| 751 |
total_len += 25; /* worst case */ |
| 752 |
else |
| 753 |
total_len += strlen(t->val->u.s); |
| 754 |
} |
| 755 |
|
| 756 |
t = t->right; |
| 757 |
} |
| 758 |
total_len++; /* for the null */ |
| 759 |
ast_log(LOG_NOTICE,"argbuf allocated %d bytes;\n", total_len); |
| 760 |
argbuf = malloc(total_len); |
| 761 |
argbuf[0] = 0; |
| 762 |
t = arglist; |
| 763 |
while (t) { |
| 764 |
char numbuf[30]; |
| 765 |
|
| 766 |
if (t != arglist) |
| 767 |
strcat(argbuf,","); |
| 768 |
|
| 769 |
if (t->val) { |
| 770 |
if (t->val->type == AST_EXPR_number) { |
| 771 |
sprintf(numbuf,FP___PRINTF,t->val->u.i); |
| 772 |
strcat(argbuf,numbuf); |
| 773 |
} else |
| 774 |
strcat(argbuf,t->val->u.s); |
| 775 |
} |
| 776 |
t = t->right; |
| 777 |
} |
| 778 |
ast_log(LOG_NOTICE,"argbuf uses %d bytes;\n", (int) strlen(argbuf)); |
| 779 |
return argbuf; |
| 780 |
} |
| 781 |
|
| 782 |
static int is_really_num(char *str) |
| 783 |
{ |
| 784 |
if ( strspn(str,"-0123456789. ") == strlen(str)) |
| 785 |
return 1; |
| 786 |
else |
| 787 |
return 0; |
| 788 |
} |
| 789 |
#endif |
| 790 |
|
| 791 |
static struct val *op_func(struct val *funcname, struct expr_node *arglist, struct ast_channel *chan) |
| 792 |
{ |
| 793 |
if (strspn(funcname->u.s,"ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789") == strlen(funcname->u.s)) |
| 794 |
{ |
| 795 |
struct val *result; |
| 796 |
if (0) { |
| 797 |
#ifdef FUNC_COS |
| 798 |
} else if (strcmp(funcname->u.s,"COS") == 0) { |
| 799 |
if (arglist && !arglist->right && arglist->val){ |
| 800 |
to_number(arglist->val); |
| 801 |
result = make_number(FUNC_COS(arglist->val->u.i)); |
| 802 |
return result; |
| 803 |
} else { |
| 804 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 805 |
return make_number(0.0); |
| 806 |
} |
| 807 |
#endif |
| 808 |
#ifdef FUNC_SIN |
| 809 |
} else if (strcmp(funcname->u.s,"SIN") == 0) { |
| 810 |
if (arglist && !arglist->right && arglist->val){ |
| 811 |
to_number(arglist->val); |
| 812 |
result = make_number(FUNC_SIN(arglist->val->u.i)); |
| 813 |
return result; |
| 814 |
} else { |
| 815 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 816 |
return make_number(0.0); |
| 817 |
} |
| 818 |
#endif |
| 819 |
#ifdef FUNC_TAN |
| 820 |
} else if (strcmp(funcname->u.s,"TAN") == 0) { |
| 821 |
if (arglist && !arglist->right && arglist->val){ |
| 822 |
to_number(arglist->val); |
| 823 |
result = make_number(FUNC_TAN(arglist->val->u.i)); |
| 824 |
return result; |
| 825 |
} else { |
| 826 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 827 |
return make_number(0.0); |
| 828 |
} |
| 829 |
#endif |
| 830 |
#ifdef FUNC_ACOS |
| 831 |
} else if (strcmp(funcname->u.s,"ACOS") == 0) { |
| 832 |
if (arglist && !arglist->right && arglist->val){ |
| 833 |
to_number(arglist->val); |
| 834 |
result = make_number(FUNC_ACOS(arglist->val->u.i)); |
| 835 |
return result; |
| 836 |
} else { |
| 837 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 838 |
return make_number(0.0); |
| 839 |
} |
| 840 |
#endif |
| 841 |
#ifdef FUNC_ASIN |
| 842 |
} else if (strcmp(funcname->u.s,"ASIN") == 0) { |
| 843 |
if (arglist && !arglist->right && arglist->val){ |
| 844 |
to_number(arglist->val); |
| 845 |
result = make_number(FUNC_ASIN(arglist->val->u.i)); |
| 846 |
return result; |
| 847 |
} else { |
| 848 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 849 |
return make_number(0.0); |
| 850 |
} |
| 851 |
#endif |
| 852 |
#ifdef FUNC_ATAN |
| 853 |
} else if (strcmp(funcname->u.s,"ATAN") == 0) { |
| 854 |
if (arglist && !arglist->right && arglist->val){ |
| 855 |
to_number(arglist->val); |
| 856 |
result = make_number(FUNC_ATAN(arglist->val->u.i)); |
| 857 |
return result; |
| 858 |
} else { |
| 859 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 860 |
return make_number(0.0); |
| 861 |
} |
| 862 |
#endif |
| 863 |
#ifdef FUNC_ATAN2 |
| 864 |
} else if (strcmp(funcname->u.s,"ATAN2") == 0) { |
| 865 |
if (arglist && arglist->right && !arglist->right->right && arglist->val && arglist->right->val){ |
| 866 |
to_number(arglist->val); |
| 867 |
to_number(arglist->right->val); |
| 868 |
result = make_number(FUNC_ATAN2(arglist->val->u.i, arglist->right->val->u.i)); |
| 869 |
return result; |
| 870 |
} else { |
| 871 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 872 |
return make_number(0.0); |
| 873 |
} |
| 874 |
#endif |
| 875 |
#ifdef FUNC_POW |
| 876 |
} else if (strcmp(funcname->u.s,"POW") == 0) { |
| 877 |
if (arglist && arglist->right && !arglist->right->right && arglist->val && arglist->right->val){ |
| 878 |
to_number(arglist->val); |
| 879 |
to_number(arglist->right->val); |
| 880 |
result = make_number(FUNC_POW(arglist->val->u.i, arglist->right->val->u.i)); |
| 881 |
return result; |
| 882 |
} else { |
| 883 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 884 |
return make_number(0.0); |
| 885 |
} |
| 886 |
#endif |
| 887 |
#ifdef FUNC_SQRT |
| 888 |
} else if (strcmp(funcname->u.s,"SQRT") == 0) { |
| 889 |
if (arglist && !arglist->right && arglist->val){ |
| 890 |
to_number(arglist->val); |
| 891 |
result = make_number(FUNC_SQRT(arglist->val->u.i)); |
| 892 |
return result; |
| 893 |
} else { |
| 894 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 895 |
return make_number(0.0); |
| 896 |
} |
| 897 |
#endif |
| 898 |
#ifdef FUNC_FLOOR |
| 899 |
} else if (strcmp(funcname->u.s,"FLOOR") == 0) { |
| 900 |
if (arglist && !arglist->right && arglist->val){ |
| 901 |
to_number(arglist->val); |
| 902 |
result = make_number(FUNC_FLOOR(arglist->val->u.i)); |
| 903 |
return result; |
| 904 |
} else { |
| 905 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 906 |
return make_number(0.0); |
| 907 |
} |
| 908 |
#endif |
| 909 |
#ifdef FUNC_CEIL |
| 910 |
} else if (strcmp(funcname->u.s,"CEIL") == 0) { |
| 911 |
if (arglist && !arglist->right && arglist->val){ |
| 912 |
to_number(arglist->val); |
| 913 |
result = make_number(FUNC_CEIL(arglist->val->u.i)); |
| 914 |
return result; |
| 915 |
} else { |
| 916 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 917 |
return make_number(0.0); |
| 918 |
} |
| 919 |
#endif |
| 920 |
#ifdef FUNC_ROUND |
| 921 |
} else if (strcmp(funcname->u.s,"ROUND") == 0) { |
| 922 |
if (arglist && !arglist->right && arglist->val){ |
| 923 |
to_number(arglist->val); |
| 924 |
result = make_number(FUNC_ROUND(arglist->val->u.i)); |
| 925 |
return result; |
| 926 |
} else { |
| 927 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 928 |
return make_number(0.0); |
| 929 |
} |
| 930 |
#endif /* defined(FUNC_ROUND) */ |
| 931 |
#ifdef FUNC_RINT |
| 932 |
} else if (strcmp(funcname->u.s,"RINT") == 0) { |
| 933 |
if (arglist && !arglist->right && arglist->val){ |
| 934 |
to_number(arglist->val); |
| 935 |
result = make_number(FUNC_RINT(arglist->val->u.i)); |
| 936 |
return result; |
| 937 |
} else { |
| 938 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 939 |
return make_number(0.0); |
| 940 |
} |
| 941 |
#endif |
| 942 |
#ifdef FUNC_TRUNC |
| 943 |
} else if (strcmp(funcname->u.s,"TRUNC") == 0) { |
| 944 |
if (arglist && !arglist->right && arglist->val){ |
| 945 |
to_number(arglist->val); |
| 946 |
result = make_number(FUNC_TRUNC(arglist->val->u.i)); |
| 947 |
return result; |
| 948 |
} else { |
| 949 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 950 |
return make_number(0.0); |
| 951 |
} |
| 952 |
#endif /* defined(FUNC_TRUNC) */ |
| 953 |
#ifdef FUNC_EXP |
| 954 |
} else if (strcmp(funcname->u.s,"EXP") == 0) { |
| 955 |
if (arglist && !arglist->right && arglist->val){ |
| 956 |
to_number(arglist->val); |
| 957 |
result = make_number(FUNC_EXP(arglist->val->u.i)); |
| 958 |
return result; |
| 959 |
} else { |
| 960 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 961 |
return make_number(0.0); |
| 962 |
} |
| 963 |
#endif |
| 964 |
#ifdef FUNC_EXP2 |
| 965 |
} else if (strcmp(funcname->u.s,"EXP2") == 0) { |
| 966 |
if (arglist && !arglist->right && arglist->val){ |
| 967 |
to_number(arglist->val); |
| 968 |
result = make_number(FUNC_EXP2(arglist->val->u.i)); |
| 969 |
return result; |
| 970 |
} else { |
| 971 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 972 |
return make_number(0.0); |
| 973 |
} |
| 974 |
#endif |
| 975 |
#ifdef FUNC_EXP10 |
| 976 |
} else if (strcmp(funcname->u.s,"EXP10") == 0) { |
| 977 |
if (arglist && !arglist->right && arglist->val){ |
| 978 |
to_number(arglist->val); |
| 979 |
result = make_number(FUNC_EXP10(arglist->val->u.i)); |
| 980 |
return result; |
| 981 |
} else { |
| 982 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 983 |
return make_number(0.0); |
| 984 |
} |
| 985 |
#endif |
| 986 |
#ifdef FUNC_LOG |
| 987 |
} else if (strcmp(funcname->u.s,"LOG") == 0) { |
| 988 |
if (arglist && !arglist->right && arglist->val){ |
| 989 |
to_number(arglist->val); |
| 990 |
result = make_number(FUNC_LOG(arglist->val->u.i)); |
| 991 |
return result; |
| 992 |
} else { |
| 993 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 994 |
return make_number(0.0); |
| 995 |
} |
| 996 |
#endif |
| 997 |
#ifdef FUNC_LOG2 |
| 998 |
} else if (strcmp(funcname->u.s,"LOG2") == 0) { |
| 999 |
if (arglist && !arglist->right && arglist->val){ |
| 1000 |
to_number(arglist->val); |
| 1001 |
result = make_number(FUNC_LOG2(arglist->val->u.i)); |
| 1002 |
return result; |
| 1003 |
} else { |
| 1004 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 1005 |
return make_number(0.0); |
| 1006 |
} |
| 1007 |
#endif |
| 1008 |
#ifdef FUNC_LOG10 |
| 1009 |
} else if (strcmp(funcname->u.s,"LOG10") == 0) { |
| 1010 |
if (arglist && !arglist->right && arglist->val){ |
| 1011 |
to_number(arglist->val); |
| 1012 |
result = make_number(FUNC_LOG10(arglist->val->u.i)); |
| 1013 |
return result; |
| 1014 |
} else { |
| 1015 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 1016 |
return make_number(0.0); |
| 1017 |
} |
| 1018 |
#endif |
| 1019 |
#ifdef FUNC_REMAINDER |
| 1020 |
} else if (strcmp(funcname->u.s,"REMAINDER") == 0) { |
| 1021 |
if (arglist && arglist->right && !arglist->right->right && arglist->val && arglist->right->val){ |
| 1022 |
to_number(arglist->val); |
| 1023 |
to_number(arglist->right->val); |
| 1024 |
result = make_number(FUNC_REMAINDER(arglist->val->u.i, arglist->right->val->u.i)); |
| 1025 |
return result; |
| 1026 |
} else { |
| 1027 |
ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s); |
| 1028 |
return make_number(0.0); |
| 1029 |
} |
| 1030 |
#endif |
| 1031 |
} else if (strcmp(funcname->u.s, "ABS") == 0) { |
| 1032 |
if (arglist && !arglist->right && arglist->val) { |
| 1033 |
to_number(arglist->val); |
| 1034 |
result = make_number(arglist->val->u.i < 0 ? arglist->val->u.i * -1 : arglist->val->u.i); |
| 1035 |
return result; |
| 1036 |
} else { |
| 1037 |
ast_log(LOG_WARNING, "Wrong args to %s() function\n", funcname->u.s); |
| 1038 |
return make_number(0.0); |
| 1039 |
} |
| 1040 |
} else { |
| 1041 |
/* is this a custom function we should execute and collect the results of? */ |
| 1042 |
#if !defined(STANDALONE) && !defined(STANDALONE2) |
| 1043 |
struct ast_custom_function *f = ast_custom_function_find(funcname->u.s); |
| 1044 |
if (!chan) |
| 1045 |
ast_log(LOG_WARNING,"Hey! chan is NULL.\n"); |
| 1046 |
if (!f) |
| 1047 |
ast_log(LOG_WARNING,"Hey! could not find func %s.\n", funcname->u.s); |
| 1048 |
|
| 1049 |
if (f && chan) { |
| 1050 |
if (f->read) { |
| 1051 |
char workspace[512]; |
| 1052 |
char *argbuf = compose_func_args(arglist); |
| 1053 |
f->read(chan, funcname->u.s, argbuf, workspace, sizeof(workspace)); |
| 1054 |
free(argbuf); |
| 1055 |
if (is_really_num(workspace)) |
| 1056 |
return make_number(FUNC_STRTOD(workspace,(char **)NULL)); |
| 1057 |
else |
| 1058 |
return make_str(workspace); |
| 1059 |
} else { |
| 1060 |
ast_log(LOG_ERROR,"Error! Function '%s' cannot be read!\n", funcname->u.s); |
| 1061 |
return (make_number ((FP___TYPE)0.0)); |
| 1062 |
} |
| 1063 |
|
| 1064 |
} else { |
| 1065 |
ast_log(LOG_ERROR, "Error! '%s' doesn't appear to be an available function!\n", funcname->u.s); |
| 1066 |
return (make_number ((FP___TYPE)0.0)); |
| 1067 |
} |
| 1068 |
#else |
| 1069 |
ast_log(LOG_ERROR, "Error! '%s' is not available in the standalone version!\n", funcname->u.s); |
| 1070 |
return (make_number ((FP___TYPE)0.0)); |
| 1071 |
#endif |
| 1072 |
} |
| 1073 |
} |
| 1074 |
else |
| 1075 |
{ |
| 1076 |
ast_log(LOG_ERROR, "Error! '%s' is not possibly a function name!\n", funcname->u.s); |
| 1077 |
return (make_number ((FP___TYPE)0.0)); |
| 1078 |
} |
| 1079 |
return (make_number ((FP___TYPE)0.0)); |
| 1080 |
} |
| 1081 |
|
| 1082 |
|
| 1083 |
static struct val * |
| 1084 |
op_or (struct val *a, struct val *b) |
| 1085 |
{ |
| 1086 |
if (is_zero_or_null (a)) { |
| 1087 |
free_value (a); |
| 1088 |
return (b); |
| 1089 |
} else { |
| 1090 |
free_value (b); |
| 1091 |
return (a); |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
static struct val * |
| 1096 |
op_and (struct val *a, struct val *b) |
| 1097 |
{ |
| 1098 |
if (is_zero_or_null (a) || is_zero_or_null (b)) { |
| 1099 |
free_value (a); |
| 1100 |
free_value (b); |
| 1101 |
return (make_number ((FP___TYPE)0.0)); |
| 1102 |
} else { |
| 1103 |
free_value (b); |
| 1104 |
return (a); |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
static struct val * |
| 1109 |
op_eq (struct val *a, struct val *b) |
| 1110 |
{ |
| 1111 |
struct val *r; |
| 1112 |
|
| 1113 |
if (isstring (a) || isstring (b)) { |
| 1114 |
to_string (a); |
| 1115 |
to_string (b); |
| 1116 |
r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) == 0)); |
| 1117 |
} else { |
| 1118 |
#ifdef DEBUG_FOR_CONVERSIONS |
| 1119 |
char buffer[2000]; |
| 1120 |
sprintf(buffer,"Converting '%s' and '%s' ", a->u.s, b->u.s); |
| 1121 |
#endif |
| 1122 |
(void)to_number(a); |
| 1123 |
(void)to_number(b); |
| 1124 |
#ifdef DEBUG_FOR_CONVERSIONS |
| 1125 |
ast_log(LOG_WARNING,"%s to '%lld' and '%lld'\n", buffer, a->u.i, b->u.i); |
| 1126 |
#endif |
| 1127 |
r = make_number ((FP___TYPE)(a->u.i == b->u.i)); |
| 1128 |
} |
| 1129 |
|
| 1130 |
free_value (a); |
| 1131 |
free_value (b); |
| 1132 |
return r; |
| 1133 |
} |
| 1134 |
|
| 1135 |
static struct val * |
| 1136 |
op_gt (struct val *a, struct val *b) |
| 1137 |
{ |
| 1138 |
struct val *r; |
| 1139 |
|
| 1140 |
if (isstring (a) || isstring (b)) { |
| 1141 |
to_string (a); |
| 1142 |
to_string (b); |
| 1143 |
r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) > 0)); |
| 1144 |
} else { |
| 1145 |
(void)to_number(a); |
| 1146 |
(void)to_number(b); |
| 1147 |
r = make_number ((FP___TYPE)(a->u.i > b->u.i)); |
| 1148 |
} |
| 1149 |
|
| 1150 |
free_value (a); |
| 1151 |
free_value (b); |
| 1152 |
return r; |
| 1153 |
} |
| 1154 |
|
| 1155 |
static struct val * |
| 1156 |
op_lt (struct val *a, struct val *b) |
| 1157 |
{ |
| 1158 |
struct val *r; |
| 1159 |
|
| 1160 |
if (isstring (a) || isstring (b)) { |
| 1161 |
to_string (a); |
| 1162 |
to_string (b); |
| 1163 |
r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) < 0)); |
| 1164 |
} else { |
| 1165 |
(void)to_number(a); |
| 1166 |
(void)to_number(b); |
| 1167 |
r = make_number ((FP___TYPE)(a->u.i < b->u.i)); |
| 1168 |
} |
| 1169 |
|
| 1170 |
free_value (a); |
| 1171 |
free_value (b); |
| 1172 |
return r; |
| 1173 |
} |
| 1174 |
|
| 1175 |
static struct val * |
| 1176 |
op_ge (struct val *a, struct val *b) |
| 1177 |
{ |
| 1178 |
struct val *r; |
| 1179 |
|
| 1180 |
if (isstring (a) || isstring (b)) { |
| 1181 |
to_string (a); |
| 1182 |
to_string (b); |
| 1183 |
r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) >= 0)); |
| 1184 |
} else { |
| 1185 |
(void)to_number(a); |
| 1186 |
(void)to_number(b); |
| 1187 |
r = make_number ((FP___TYPE)(a->u.i >= b->u.i)); |
| 1188 |
} |
| 1189 |
|
| 1190 |
free_value (a); |
| 1191 |
free_value (b); |
| 1192 |
return r; |
| 1193 |
} |
| 1194 |
|
| 1195 |
static struct val * |
| 1196 |
op_le (struct val *a, struct val *b) |
| 1197 |
{ |
| 1198 |
struct val *r; |
| 1199 |
|
| 1200 |
if (isstring (a) || isstring (b)) { |
| 1201 |
to_string (a); |
| 1202 |
to_string (b); |
| 1203 |
r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) <= 0)); |
| 1204 |
} else { |
| 1205 |
(void)to_number(a); |
| 1206 |
(void)to_number(b); |
| 1207 |
r = make_number ((FP___TYPE)(a->u.i <= b->u.i)); |
| 1208 |
} |
| 1209 |
|
| 1210 |
free_value (a); |
| 1211 |
free_value (b); |
| 1212 |
return r; |
| 1213 |
} |
| 1214 |
|
| 1215 |
static struct val * |
| 1216 |
op_cond (struct val *a, struct val *b, struct val *c) |
| 1217 |
{ |
| 1218 |
struct val *r; |
| 1219 |
|
| 1220 |
if( isstring(a) ) |
| 1221 |
{ |
| 1222 |
if( strlen(a->u.s) && strcmp(a->u.s, "\"\"") != 0 && strcmp(a->u.s,"0") != 0 ) |
| 1223 |
{ |
| 1224 |
free_value(a); |
| 1225 |
free_value(c); |
| 1226 |
r = b; |
| 1227 |
} |
| 1228 |
else |
| 1229 |
{ |
| 1230 |
free_value(a); |
| 1231 |
free_value(b); |
| 1232 |
r = c; |
| 1233 |
} |
| 1234 |
} |
| 1235 |
else |
| 1236 |
{ |
| 1237 |
(void)to_number(a); |
| 1238 |
if( a->u.i ) |
| 1239 |
{ |
| 1240 |
free_value(a); |
| 1241 |
free_value(c); |
| 1242 |
r = b; |
| 1243 |
} |
| 1244 |
else |
| 1245 |
{ |
| 1246 |
free_value(a); |
| 1247 |
free_value(b); |
| 1248 |
r = c; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
return r; |
| 1252 |
} |
| 1253 |
|
| 1254 |
static struct val * |
| 1255 |
op_ne (struct val *a, struct val *b) |
| 1256 |
{ |
| 1257 |
struct val *r; |
| 1258 |
|
| 1259 |
if (isstring (a) || isstring (b)) { |
| 1260 |
to_string (a); |
| 1261 |
to_string (b); |
| 1262 |
r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) != 0)); |
| 1263 |
} else { |
| 1264 |
(void)to_number(a); |
| 1265 |
(void)to_number(b); |
| 1266 |
r = make_number ((FP___TYPE)(a->u.i != b->u.i)); |
| 1267 |
} |
| 1268 |
|
| 1269 |
free_value (a); |
| 1270 |
free_value (b); |
| 1271 |
return r; |
| 1272 |
} |
| 1273 |
|
| 1274 |
static int |
| 1275 |
chk_plus (FP___TYPE a, FP___TYPE b, FP___TYPE r) |
| 1276 |
{ |
| 1277 |
/* sum of two positive numbers must be positive */ |
| 1278 |
if (a > 0 && b > 0 && r <= 0) |
| 1279 |
return 1; |
| 1280 |
/* sum of two negative numbers must be negative */ |
| 1281 |
if (a < 0 && b < 0 && r >= 0) |
| 1282 |
return 1; |
| 1283 |
/* all other cases are OK */ |
| 1284 |
return 0; |
| 1285 |
} |
| 1286 |
|
| 1287 |
static struct val * |
| 1288 |
op_plus (struct val *a, struct val *b) |
| 1289 |
{ |
| 1290 |
struct val *r; |
| 1291 |
|
| 1292 |
if (!to_number (a)) { |
| 1293 |
if( !extra_error_message_supplied ) |
| 1294 |
ast_log(LOG_WARNING,"non-numeric argument\n"); |
| 1295 |
if (!to_number (b)) { |
| 1296 |
free_value(a); |
| 1297 |
free_value(b); |
| 1298 |
return make_number(0); |
| 1299 |
} else { |
| 1300 |
free_value(a); |
| 1301 |
return (b); |
| 1302 |
} |
| 1303 |
} else if (!to_number(b)) { |
| 1304 |
free_value(b); |
| 1305 |
return (a); |
| 1306 |
} |
| 1307 |
|
| 1308 |
r = make_number (a->u.i + b->u.i); |
| 1309 |
if (chk_plus (a->u.i, b->u.i, r->u.i)) { |
| 1310 |
ast_log(LOG_WARNING,"overflow\n"); |
| 1311 |
} |
| 1312 |
free_value (a); |
| 1313 |
free_value (b); |
| 1314 |
return r; |
| 1315 |
} |
| 1316 |
|
| 1317 |
static int |
| 1318 |
chk_minus (FP___TYPE a, FP___TYPE b, FP___TYPE r) |
| 1319 |
{ |
| 1320 |
/* special case subtraction of QUAD_MIN */ |
| 1321 |
if (b == QUAD_MIN) { |
| 1322 |
if (a >= 0) |
| 1323 |
return 1; |
| 1324 |
else |
| 1325 |
return 0; |
| 1326 |
} |
| 1327 |
/* this is allowed for b != QUAD_MIN */ |
| 1328 |
return chk_plus (a, -b, r); |
| 1329 |
} |
| 1330 |
|
| 1331 |
static struct val * |
| 1332 |
op_minus (struct val *a, struct val *b) |
| 1333 |
{ |
| 1334 |
struct val *r; |
| 1335 |
|
| 1336 |
if (!to_number (a)) { |
| 1337 |
if( !extra_error_message_supplied ) |
| 1338 |
ast_log(LOG_WARNING, "non-numeric argument\n"); |
| 1339 |
if (!to_number (b)) { |
| 1340 |
free_value(a); |
| 1341 |
free_value(b); |
| 1342 |
return make_number(0); |
| 1343 |
} else { |
| 1344 |
r = make_number(0 - b->u.i); |
| 1345 |
free_value(a); |
| 1346 |
free_value(b); |
| 1347 |
return (r); |
| 1348 |
} |
| 1349 |
} else if (!to_number(b)) { |
| 1350 |
if( !extra_error_message_supplied ) |
| 1351 |
ast_log(LOG_WARNING, "non-numeric argument\n"); |
| 1352 |
free_value(b); |
| 1353 |
return (a); |
| 1354 |
} |
| 1355 |
|
| 1356 |
r = make_number (a->u.i - b->u.i); |
| 1357 |
if (chk_minus (a->u.i, b->u.i, r->u.i)) { |
| 1358 |
ast_log(LOG_WARNING, "overflow\n"); |
| 1359 |
} |
| 1360 |
free_value (a); |
| 1361 |
free_value (b); |
| 1362 |
return r; |
| 1363 |
} |
| 1364 |
|
| 1365 |
static struct val * |
| 1366 |
op_negate (struct val *a) |
| 1367 |
{ |
| 1368 |
struct val *r; |
| 1369 |
|
| 1370 |
if (!to_number (a) ) { |
| 1371 |
free_value(a); |
| 1372 |
if( !extra_error_message_supplied ) |
| 1373 |
ast_log(LOG_WARNING, "non-numeric argument\n"); |
| 1374 |
return make_number(0); |
| 1375 |
} |
| 1376 |
|
| 1377 |
r = make_number (- a->u.i); |
| 1378 |
if (chk_minus (0, a->u.i, r->u.i)) { |
| 1379 |
ast_log(LOG_WARNING, "overflow\n"); |
| 1380 |
} |
| 1381 |
free_value (a); |
| 1382 |
return r; |
| 1383 |
} |
| 1384 |
|
| 1385 |
static struct val * |
| 1386 |
op_compl (struct val *a) |
| 1387 |
{ |
| 1388 |
int v1 = 1; |
| 1389 |
struct val *r; |
| 1390 |
|
| 1391 |
if( !a ) |
| 1392 |
{ |
| 1393 |
v1 = 0; |
| 1394 |
} |
| 1395 |
else |
| 1396 |
{ |
| 1397 |
switch( a->type ) |
| 1398 |
{ |
| 1399 |
case AST_EXPR_number: |
| 1400 |
if( a->u.i == 0 ) |
| 1401 |
v1 = 0; |
| 1402 |
break; |
| 1403 |
|
| 1404 |
case AST_EXPR_string: |
| 1405 |
if( a->u.s == 0 ) |
| 1406 |
v1 = 0; |
| 1407 |
else |
| 1408 |
{ |
| 1409 |
if( a->u.s[0] == 0 ) |
| 1410 |
v1 = 0; |
| 1411 |
else if (strlen(a->u.s) == 1 && a->u.s[0] == '0' ) |
| 1412 |
v1 = 0; |
| 1413 |
else |
| 1414 |
v1 = atoi(a->u.s); |
| 1415 |
} |
| 1416 |
break; |
| 1417 |
|
| 1418 |
case AST_EXPR_numeric_string: |
| 1419 |
if( a->u.s == 0 ) |
| 1420 |
v1 = 0; |
| 1421 |
else |
| 1422 |
{ |
| 1423 |
if( a->u.s[0] == 0 ) |
| 1424 |
v1 = 0; |
| 1425 |
else if (strlen(a->u.s) == 1 && a->u.s[0] == '0' ) |
| 1426 |
v1 = 0; |
| 1427 |
else |
| 1428 |
v1 = atoi(a->u.s); |
| 1429 |
} |
| 1430 |
break; |
| 1431 |
} |
| 1432 |
} |
| 1433 |
|
| 1434 |
r = make_number (!v1); |
| 1435 |
free_value (a); |
| 1436 |
return r; |
| 1437 |
} |
| 1438 |
|
| 1439 |
static int |
| 1440 |
chk_times (FP___TYPE a, FP___TYPE b, FP___TYPE r) |
| 1441 |
{ |
| 1442 |
/* special case: first operand is 0, no overflow possible */ |
| 1443 |
if (a == 0) |
| 1444 |
return 0; |
| 1445 |
/* verify that result of division matches second operand */ |
| 1446 |
if (r / a != b) |
| 1447 |
return 1; |
| 1448 |
return 0; |
| 1449 |
} |
| 1450 |
|
| 1451 |
static struct val * |
| 1452 |
op_times (struct val *a, struct val *b) |
| 1453 |
{ |
| 1454 |
struct val *r; |
| 1455 |
|
| 1456 |
if (!to_number (a) || !to_number (b)) { |
| 1457 |
free_value(a); |
| 1458 |
free_value(b); |
| 1459 |
if( !extra_error_message_supplied ) |
| 1460 |
ast_log(LOG_WARNING, "non-numeric argument\n"); |
| 1461 |
return(make_number(0)); |
| 1462 |
} |
| 1463 |
|
| 1464 |
r = make_number (a->u.i * b->u.i); |
| 1465 |
if (chk_times (a->u.i, b->u.i, r->u.i)) { |
| 1466 |
ast_log(LOG_WARNING, "overflow\n"); |
| 1467 |
} |
| 1468 |
free_value (a); |
| 1469 |
free_value (b); |
| 1470 |
return (r); |
| 1471 |
} |
| 1472 |
|
| 1473 |
static int |
| 1474 |
chk_div (FP___TYPE a, FP___TYPE b) |
| 1475 |
{ |
| 1476 |
/* div by zero has been taken care of before */ |
| 1477 |
/* only QUAD_MIN / -1 causes overflow */ |
| 1478 |
if (a == QUAD_MIN && b == -1) |
| 1479 |
return 1; |
| 1480 |
/* everything else is OK */ |
| 1481 |
return 0; |
| 1482 |
} |
| 1483 |
|
| 1484 |
static struct val * |
| 1485 |
op_div (struct val *a, struct val *b) |
| 1486 |
{ |
| 1487 |
struct val *r; |
| 1488 |
|
| 1489 |
if (!to_number (a)) { |
| 1490 |
free_value(a); |
| 1491 |
free_value(b); |
| 1492 |
if( !extra_error_message_supplied ) |
| 1493 |
ast_log(LOG_WARNING, "non-numeric argument\n"); |
| 1494 |
return make_number(0); |
| 1495 |
} else if (!to_number (b)) { |
| 1496 |
free_value(a); |
| 1497 |
free_value(b); |
| 1498 |
if( !extra_error_message_supplied ) |
| 1499 |
ast_log(LOG_WARNING, "non-numeric argument\n"); |
| 1500 |
return make_number(INT_MAX); |
| 1501 |
} |
| 1502 |
|
| 1503 |
if (b->u.i == 0) { |
| 1504 |
ast_log(LOG_WARNING, "division by zero\n"); |
| 1505 |
free_value(a); |
| 1506 |
free_value(b); |
| 1507 |
return make_number(INT_MAX); |
| 1508 |
} |
| 1509 |
|
| 1510 |
r = make_number (a->u.i / b->u.i); |
| 1511 |
if (chk_div (a->u.i, b->u.i)) { |
| 1512 |
ast_log(LOG_WARNING, "overflow\n"); |
| 1513 |
} |
| 1514 |
free_value (a); |
| 1515 |
free_value (b); |
| 1516 |
return r; |
| 1517 |
} |
| 1518 |
|
| 1519 |
static struct val * |
| 1520 |
op_rem (struct val *a, struct val *b) |
| 1521 |
{ |
| 1522 |
struct val *r; |
| 1523 |
|
| 1524 |
if (!to_number (a) || !to_number (b)) { |
| 1525 |
if( !extra_error_message_supplied ) |
| 1526 |
ast_log(LOG_WARNING, "non-numeric argument\n"); |
| 1527 |
free_value(a); |
| 1528 |
free_value(b); |
| 1529 |
return make_number(0); |
| 1530 |
} |
| 1531 |
|
| 1532 |
if (b->u.i == 0) { |
| 1533 |
ast_log(LOG_WARNING, "div by zero\n"); |
| 1534 |
free_value(a); |
| 1535 |
return(b); |
| 1536 |
} |
| 1537 |
|
| 1538 |
r = make_number (FUNC_FMOD(a->u.i, b->u.i)); /* either fmod or fmodl if FP___TYPE is available */ |
| 1539 |
/* chk_rem necessary ??? */ |
| 1540 |
free_value (a); |
| 1541 |
free_value (b); |
| 1542 |
return r; |
| 1543 |
} |
| 1544 |
|
| 1545 |
|
| 1546 |
static struct val * |
| 1547 |
op_colon (struct val *a, struct val *b) |
| 1548 |
{ |
| 1549 |
regex_t rp; |
| 1550 |
regmatch_t rm[2]; |
| 1551 |
char errbuf[256]; |
| 1552 |
int eval; |
| 1553 |
struct val *v; |
| 1554 |
|
| 1555 |
/* coerce to both arguments to strings */ |
| 1556 |
to_string(a); |
| 1557 |
to_string(b); |
| 1558 |
/* strip double quotes from both -- they'll screw up the pattern, and the search string starting at ^ */ |
| 1559 |
strip_quotes(a); |
| 1560 |
strip_quotes(b); |
| 1561 |
/* compile regular expression */ |
| 1562 |
if ((eval = regcomp (&rp, b->u.s, REG_EXTENDED)) != 0) { |
| 1563 |
regerror (eval, &rp, errbuf, sizeof(errbuf)); |
| 1564 |
ast_log(LOG_WARNING, "regcomp() error : %s\n", errbuf); |
| 1565 |
free_value(a); |
| 1566 |
free_value(b); |
| 1567 |
return make_str(""); |
| 1568 |
} |
| 1569 |
|
| 1570 |
/* compare string against pattern */ |
| 1571 |
/* remember that patterns are anchored to the beginning of the line */ |
| 1572 |
if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0) { |
| 1573 |
if (rm[1].rm_so >= 0) { |
| 1574 |
*(a->u.s + rm[1].rm_eo) = '\0'; |
| 1575 |
v = make_str (a->u.s + rm[1].rm_so); |
| 1576 |
|
| 1577 |
} else { |
| 1578 |
v = make_number ((FP___TYPE)(rm[0].rm_eo - rm[0].rm_so)); |
| 1579 |
} |
| 1580 |
} else { |
| 1581 |
if (rp.re_nsub == 0) { |
| 1582 |
v = make_number ((FP___TYPE)0); |
| 1583 |
} else { |
| 1584 |
v = make_str (""); |
| 1585 |
} |
| 1586 |
} |
| 1587 |
|
| 1588 |
/* free arguments and pattern buffer */ |
| 1589 |
free_value (a); |
| 1590 |
free_value (b); |
| 1591 |
regfree (&rp); |
| 1592 |
|
| 1593 |
return v; |
| 1594 |
} |
| 1595 |
|
| 1596 |
|
| 1597 |
static struct val * |
| 1598 |
op_eqtilde (struct val *a, struct val *b) |
| 1599 |
{ |
| 1600 |
regex_t rp; |
| 1601 |
regmatch_t rm[2]; |
| 1602 |
char errbuf[256]; |
| 1603 |
int eval; |
| 1604 |
struct val *v; |
| 1605 |
|
| 1606 |
/* coerce to both arguments to strings */ |
| 1607 |
to_string(a); |
| 1608 |
to_string(b); |
| 1609 |
/* strip double quotes from both -- they'll screw up the pattern, and the search string starting at ^ */ |
| 1610 |
strip_quotes(a); |
| 1611 |
strip_quotes(b); |
| 1612 |
/* compile regular expression */ |
| 1613 |
if ((eval = regcomp (&rp, b->u.s, REG_EXTENDED)) != 0) { |
| 1614 |
regerror (eval, &rp, errbuf, sizeof(errbuf)); |
| 1615 |
ast_log(LOG_WARNING, "regcomp() error : %s\n", errbuf); |
| 1616 |
free_value(a); |
| 1617 |
free_value(b); |
| 1618 |
return make_str(""); |
| 1619 |
} |
| 1620 |
|
| 1621 |
/* compare string against pattern */ |
| 1622 |
/* remember that patterns are anchored to the beginning of the line */ |
| 1623 |
if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 ) { |
| 1624 |
if (rm[1].rm_so >= 0) { |
| 1625 |
*(a->u.s + rm[1].rm_eo) = '\0'; |
| 1626 |
v = make_str (a->u.s + rm[1].rm_so); |
| 1627 |
|
| 1628 |
} else { |
| 1629 |
v = make_number ((FP___TYPE)(rm[0].rm_eo - rm[0].rm_so)); |
| 1630 |
} |
| 1631 |
} else { |
| 1632 |
if (rp.re_nsub == 0) { |
| 1633 |
v = make_number ((FP___TYPE)0.0); |
| 1634 |
} else { |
| 1635 |
v = make_str (""); |
| 1636 |
} |
| 1637 |
} |
| 1638 |
|
| 1639 |
/* free arguments and pattern buffer */ |
| 1640 |
free_value (a); |
| 1641 |
free_value (b); |
| 1642 |
regfree (&rp); |
| 1643 |
|
| 1644 |
return v; |
| 1645 |
} |
| 1646 |
|
| 1647 |
static struct val * /* this is a string concat operator */ |
| 1648 |
op_tildetilde (struct val *a, struct val *b) |
| 1649 |
{ |
| 1650 |
struct val *v; |
| 1651 |
char *vs; |
| 1652 |
|
| 1653 |
/* coerce to both arguments to strings */ |
| 1654 |
to_string(a); |
| 1655 |
to_string(b); |
| 1656 |
/* strip double quotes from both -- */ |
| 1657 |
strip_quotes(a); |
| 1658 |
strip_quotes(b); |
| 1659 |
|
| 1660 |
vs = malloc(strlen(a->u.s)+strlen(b->u.s)+1); |
| 1661 |
if (vs == NULL) { |
| 1662 |
ast_log(LOG_WARNING, "malloc() failed\n"); |
| 1663 |
free_value(a); |
| 1664 |
free_value(b); |
| 1665 |
return NULL; |
| 1666 |
} |
| 1667 |
|
| 1668 |
strcpy(vs,a->u.s); |
| 1669 |
strcat(vs,b->u.s); |
| 1670 |
|
| 1671 |
v = make_str(vs); |
| 1672 |
|
| 1673 |
free(vs); |
| 1674 |
|
| 1675 |
/* free arguments */ |
| 1676 |
free_value(a); |
| 1677 |
free_value(b); |
| 1678 |
|
| 1679 |
return v; |
| 1680 |
} |