| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#define ASTMM_LIBC ASTMM_IGNORE |
| 24 |
#include "asterisk.h" |
| 25 |
|
| 26 |
#include "asterisk/ast_expr.h" |
| 27 |
|
| 28 |
#define AST_API_MODULE 1 |
| 29 |
#include "asterisk/inline_api.h" |
| 30 |
|
| 31 |
#define AST_API_MODULE 1 |
| 32 |
#include "asterisk/lock.h" |
| 33 |
|
| 34 |
#ifndef DEBUG_THREADS |
| 35 |
enum ast_lock_type { |
| 36 |
AST_MUTEX, |
| 37 |
AST_RDLOCK, |
| 38 |
AST_WRLOCK, |
| 39 |
}; |
| 40 |
#endif |
| 41 |
#ifdef DEBUG_THREADLOCALS |
| 42 |
#define MALLOC_FAILURE_MSG \ |
| 43 |
ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file); |
| 44 |
|
| 45 |
void * attribute_malloc __ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func); |
| 46 |
|
| 47 |
void * attribute_malloc __ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func) |
| 48 |
{ |
| 49 |
void *p; |
| 50 |
|
| 51 |
if (!(p = calloc(num, len))) |
| 52 |
MALLOC_FAILURE_MSG; |
| 53 |
|
| 54 |
return p; |
| 55 |
} |
| 56 |
#endif |
| 57 |
|
| 58 |
#ifdef DEBUG_THREADS |
| 59 |
void ast_store_lock_info(enum ast_lock_type type, const char *filename, |
| 60 |
int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt); |
| 61 |
void ast_store_lock_info(enum ast_lock_type type, const char *filename, |
| 62 |
int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt) |
| 63 |
{ |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt); |
| 68 |
void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt) |
| 69 |
{ |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
#ifdef HAVE_BKTR |
| 74 |
int __ast_bt_get_addresses(struct ast_bt *bt); |
| 75 |
int __ast_bt_get_addresses(struct ast_bt *bt) |
| 76 |
{ |
| 77 |
|
| 78 |
return 0; |
| 79 |
} |
| 80 |
struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames); |
| 81 |
struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames) |
| 82 |
{ |
| 83 |
return NULL; |
| 84 |
} |
| 85 |
#endif |
| 86 |
|
| 87 |
void ast_suspend_lock_info(void *lock_addr) |
| 88 |
{ |
| 89 |
} |
| 90 |
void ast_restore_lock_info(void *lock_addr) |
| 91 |
{ |
| 92 |
} |
| 93 |
void ast_mark_lock_acquired(void *); |
| 94 |
void ast_mark_lock_acquired(void *foo) |
| 95 |
{ |
| 96 |
|
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
|
| 101 |
static int global_lineno = 1; |
| 102 |
static int global_expr_count=0; |
| 103 |
static int global_expr_max_size=0; |
| 104 |
static int global_expr_tot_size=0; |
| 105 |
static int global_warn_count=0; |
| 106 |
static int global_OK_count=0; |
| 107 |
|
| 108 |
struct varz |
| 109 |
{ |
| 110 |
char varname[100]; |
| 111 |
char varval[1000]; |
| 112 |
struct varz *next; |
| 113 |
}; |
| 114 |
|
| 115 |
struct varz *global_varlist; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__((format(printf,5,6))); |
| 120 |
|
| 121 |
void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) |
| 122 |
{ |
| 123 |
va_list vars; |
| 124 |
va_start(vars,fmt); |
| 125 |
|
| 126 |
printf("LOG: lev:%d file:%s line:%d func: %s ", |
| 127 |
level, file, line, function); |
| 128 |
vprintf(fmt, vars); |
| 129 |
fflush(stdout); |
| 130 |
va_end(vars); |
| 131 |
} |
| 132 |
|
| 133 |
char *find_var(const char *varname); |
| 134 |
void set_var(const char *varname, const char *varval); |
| 135 |
unsigned int check_expr(char* buffer, char* error_report); |
| 136 |
int check_eval(char *buffer, char *error_report); |
| 137 |
void parse_file(const char *fname); |
| 138 |
|
| 139 |
int ast_add_profile(const char *x, uint64_t scale) { return 0;} |
| 140 |
|
| 141 |
char *find_var(const char *varname) |
| 142 |
{ |
| 143 |
struct varz *t; |
| 144 |
for (t= global_varlist; t; t = t->next) { |
| 145 |
if (!strcmp(t->varname, varname)) { |
| 146 |
return t->varval; |
| 147 |
} |
| 148 |
} |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
void set_var(const char *varname, const char *varval); |
| 153 |
|
| 154 |
void set_var(const char *varname, const char *varval) |
| 155 |
{ |
| 156 |
struct varz *t = (struct varz*)calloc(1,sizeof(struct varz)); |
| 157 |
if (!t) |
| 158 |
return; |
| 159 |
strcpy(t->varname, varname); |
| 160 |
strcpy(t->varval, varval); |
| 161 |
t->next = global_varlist; |
| 162 |
global_varlist = t; |
| 163 |
} |
| 164 |
|
| 165 |
unsigned int check_expr(char* buffer, char* error_report) |
| 166 |
{ |
| 167 |
char* cp; |
| 168 |
unsigned int warn_found = 0; |
| 169 |
|
| 170 |
error_report[0] = 0; |
| 171 |
|
| 172 |
for (cp = buffer; *cp; ++cp) |
| 173 |
{ |
| 174 |
switch (*cp) |
| 175 |
{ |
| 176 |
case '"': |
| 177 |
|
| 178 |
while (*(++cp) && *cp != '"') ; |
| 179 |
|
| 180 |
if (*cp == 0) |
| 181 |
{ |
| 182 |
fprintf(stderr, |
| 183 |
"Trouble? Unterminated double quote found at line %d\n", |
| 184 |
global_lineno); |
| 185 |
} |
| 186 |
break; |
| 187 |
|
| 188 |
case '>': |
| 189 |
case '<': |
| 190 |
case '!': |
| 191 |
if ( (*(cp + 1) == '=') |
| 192 |
&& ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) ) |
| 193 |
{ |
| 194 |
char msg[200]; |
| 195 |
snprintf(msg, |
| 196 |
sizeof(msg), |
| 197 |
"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", |
| 198 |
global_lineno, *cp, *(cp + 1)); |
| 199 |
strcat(error_report, msg); |
| 200 |
++global_warn_count; |
| 201 |
++warn_found; |
| 202 |
} |
| 203 |
break; |
| 204 |
|
| 205 |
case '|': |
| 206 |
case '&': |
| 207 |
case '=': |
| 208 |
case '+': |
| 209 |
case '-': |
| 210 |
case '*': |
| 211 |
case '/': |
| 212 |
case '%': |
| 213 |
case '?': |
| 214 |
case ':': |
| 215 |
if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') ) |
| 216 |
{ |
| 217 |
char msg[200]; |
| 218 |
snprintf(msg, |
| 219 |
sizeof(msg), |
| 220 |
"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", |
| 221 |
global_lineno, *cp ); |
| 222 |
strcat(error_report, msg); |
| 223 |
++global_warn_count; |
| 224 |
++warn_found; |
| 225 |
} |
| 226 |
break; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
return warn_found; |
| 231 |
} |
| 232 |
|
| 233 |
int check_eval(char *buffer, char *error_report); |
| 234 |
|
| 235 |
struct ast_custom_function *ast_custom_function_find(const char *name); |
| 236 |
|
| 237 |
struct ast_custom_function *ast_custom_function_find(const char *name) |
| 238 |
{ |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
int check_eval(char *buffer, char *error_report) |
| 243 |
{ |
| 244 |
char *cp, *ep; |
| 245 |
char s[4096]; |
| 246 |
char evalbuf[80000]; |
| 247 |
int result; |
| 248 |
|
| 249 |
error_report[0] = 0; |
| 250 |
ep = evalbuf; |
| 251 |
|
| 252 |
for (cp=buffer;*cp;cp++) { |
| 253 |
if (*cp == '$' && *(cp+1) == '{') { |
| 254 |
int brack_lev = 1; |
| 255 |
char *xp= cp+2; |
| 256 |
|
| 257 |
while (*xp) { |
| 258 |
if (*xp == '{') |
| 259 |
brack_lev++; |
| 260 |
else if (*xp == '}') |
| 261 |
brack_lev--; |
| 262 |
|
| 263 |
if (brack_lev == 0) |
| 264 |
break; |
| 265 |
xp++; |
| 266 |
} |
| 267 |
if (*xp == '}') { |
| 268 |
char varname[200]; |
| 269 |
char *val; |
| 270 |
|
| 271 |
strncpy(varname,cp+2, xp-cp-2); |
| 272 |
varname[xp-cp-2] = 0; |
| 273 |
cp = xp; |
| 274 |
val = find_var(varname); |
| 275 |
if (val) { |
| 276 |
char *z = val; |
| 277 |
while (*z) |
| 278 |
*ep++ = *z++; |
| 279 |
} |
| 280 |
else { |
| 281 |
*ep++ = '5'; |
| 282 |
*ep++ = '5'; |
| 283 |
*ep++ = '5'; |
| 284 |
} |
| 285 |
} |
| 286 |
else { |
| 287 |
printf("Unterminated variable reference at line %d\n", global_lineno); |
| 288 |
*ep++ = *cp; |
| 289 |
} |
| 290 |
} |
| 291 |
else if (*cp == '\\') { |
| 292 |
|
| 293 |
cp++; |
| 294 |
*ep++ = *cp; |
| 295 |
} |
| 296 |
else |
| 297 |
*ep++ = *cp; |
| 298 |
} |
| 299 |
*ep++ = 0; |
| 300 |
|
| 301 |
|
| 302 |
result = ast_expr(evalbuf, s, sizeof(s),NULL); |
| 303 |
if (result) { |
| 304 |
sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s); |
| 305 |
return 1; |
| 306 |
} else { |
| 307 |
sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf); |
| 308 |
return 1; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
void parse_file(const char *fname); |
| 314 |
|
| 315 |
void parse_file(const char *fname) |
| 316 |
{ |
| 317 |
FILE *f = fopen(fname,"r"); |
| 318 |
FILE *l = fopen("expr2_log","w"); |
| 319 |
int c1; |
| 320 |
char last_char= 0; |
| 321 |
char buffer[30000]; |
| 322 |
|
| 323 |
if (!f) { |
| 324 |
fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname); |
| 325 |
exit(20); |
| 326 |
} |
| 327 |
if (!l) { |
| 328 |
fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n"); |
| 329 |
exit(21); |
| 330 |
} |
| 331 |
|
| 332 |
global_lineno = 1; |
| 333 |
|
| 334 |
while ((c1 = fgetc(f)) != EOF) { |
| 335 |
if (c1 == '\n') |
| 336 |
global_lineno++; |
| 337 |
else if (c1 == '[') { |
| 338 |
if (last_char == '$') { |
| 339 |
|
| 340 |
int bracklev = 1; |
| 341 |
int bufcount = 0; |
| 342 |
int retval; |
| 343 |
char error_report[30000]; |
| 344 |
|
| 345 |
while ((c1 = fgetc(f)) != EOF) { |
| 346 |
if (c1 == '[') |
| 347 |
bracklev++; |
| 348 |
else if (c1 == ']') |
| 349 |
bracklev--; |
| 350 |
if (c1 == '\n') { |
| 351 |
fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno); |
| 352 |
fclose(f); |
| 353 |
fclose(l); |
| 354 |
printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno); |
| 355 |
} |
| 356 |
|
| 357 |
if (bracklev == 0) |
| 358 |
break; |
| 359 |
buffer[bufcount++] = c1; |
| 360 |
} |
| 361 |
if (c1 == EOF) { |
| 362 |
fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno); |
| 363 |
fclose(f); |
| 364 |
fclose(l); |
| 365 |
printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno); |
| 366 |
exit(22); |
| 367 |
} |
| 368 |
|
| 369 |
buffer[bufcount] = 0; |
| 370 |
|
| 371 |
global_expr_tot_size += bufcount; |
| 372 |
global_expr_count++; |
| 373 |
if (bufcount > global_expr_max_size) |
| 374 |
global_expr_max_size = bufcount; |
| 375 |
|
| 376 |
retval = check_expr(buffer, error_report); |
| 377 |
if (retval != 0) { |
| 378 |
|
| 379 |
printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n", |
| 380 |
global_lineno, buffer); |
| 381 |
fprintf(l, "%s", error_report); |
| 382 |
} |
| 383 |
else { |
| 384 |
printf("OK -- $[%s] at line %d\n", buffer, global_lineno); |
| 385 |
global_OK_count++; |
| 386 |
} |
| 387 |
error_report[0] = 0; |
| 388 |
retval = check_eval(buffer, error_report); |
| 389 |
fprintf(l, "%s", error_report); |
| 390 |
} |
| 391 |
} |
| 392 |
last_char = c1; |
| 393 |
} |
| 394 |
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", |
| 395 |
global_expr_count, |
| 396 |
global_OK_count, |
| 397 |
global_warn_count, |
| 398 |
global_expr_max_size, |
| 399 |
(global_expr_count) ? global_expr_tot_size/global_expr_count : 0); |
| 400 |
|
| 401 |
fclose(f); |
| 402 |
fclose(l); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
int main(int argc,char **argv) |
| 407 |
{ |
| 408 |
int argc1; |
| 409 |
char *eq; |
| 410 |
|
| 411 |
if (argc < 2) { |
| 412 |
printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n"); |
| 413 |
printf(" and run them thru the parser, looking for problems\n"); |
| 414 |
printf("Hey-- give me a path to an extensions.conf file!\n"); |
| 415 |
printf(" You can also follow the file path with a series of variable decls,\n"); |
| 416 |
printf(" of the form, varname=value, each separated from the next by spaces.\n"); |
| 417 |
printf(" (this might allow you to avoid division by zero messages, check that math\n"); |
| 418 |
printf(" is being done correctly, etc.)\n"); |
| 419 |
printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n"); |
| 420 |
printf(" you to possible problems where you might be expecting those operators as part of a string.\n"); |
| 421 |
printf(" (to include operators in a string, wrap with double quotes!)\n"); |
| 422 |
|
| 423 |
exit(19); |
| 424 |
} |
| 425 |
global_varlist = 0; |
| 426 |
for (argc1=2;argc1 < argc; argc1++) { |
| 427 |
if ((eq = strchr(argv[argc1],'='))) { |
| 428 |
*eq = 0; |
| 429 |
set_var(argv[argc1],eq+1); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
parse_file(argv[1]); |
| 436 |
return 0; |
| 437 |
} |