Skip to content
Snippets Groups Projects
pval.c 149 KiB
Newer Older
  • Learn to ignore specific revisions
  • 
    /*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 2006, Digium, Inc.
     *
     * Steve Murphy <murf@parsetree.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.
     */
    
    /*! \file
     *
     * \brief Compile symbolic Asterisk Extension Logic into Asterisk extensions, version 2.
     * 
     */
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    #include <sys/types.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <errno.h>
    #include <regex.h>
    #include <sys/stat.h>
    
    #include "asterisk/pbx.h"
    #include "asterisk/config.h"
    #include "asterisk/module.h"
    #include "asterisk/logger.h"
    #include "asterisk/cli.h"
    #include "asterisk/app.h"
    #include "asterisk/callerid.h"
    #include "asterisk/pval.h"
    #include "asterisk/ael_structs.h"
    #ifdef AAL_ARGCHECK
    #include "asterisk/argdesc.h"
    #endif
    
    extern int localized_pbx_load_module(void);
    
    static char expr_output[2096];
    #define AST_PBX_MAX_STACK  128
    
    /* these functions are in ../ast_expr2.fl */
    
    static int errs, warns;
    static int notes;
    #ifdef STANDALONE
    static int extensions_dot_conf_loaded = 0;
    #endif
    static char *registrar = "pbx_ael";
    
    static pval *current_db;
    static pval *current_context;
    static pval *current_extension;
    
    static const char *match_context;
    static const char *match_exten;
    static const char *match_label;
    static int in_abstract_context;
    static int count_labels; /* true, put matcher in label counting mode */
    static int label_count;  /* labels are only meant to be counted in a context or exten */
    static int return_on_context_match;
    static pval *last_matched_label;
    struct pval *match_pval(pval *item);
    static void check_timerange(pval *p);
    static void check_dow(pval *DOW);
    static void check_day(pval *DAY);
    static void check_month(pval *MON);
    static void check_expr2_input(pval *expr, char *str);
    static int extension_matches(pval *here, const char *exten, const char *pattern);
    static void check_goto(pval *item);
    static void find_pval_goto_item(pval *item, int lev);
    static void find_pval_gotos(pval *item, int lev);
    static int check_break(pval *item);
    static int check_continue(pval *item);
    static void check_label(pval *item);
    static void check_macro_returns(pval *macro);
    
    static struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont);
    static struct pval *find_first_label_in_current_context(char *label, pval *curr_cont);
    static void print_pval_list(FILE *fin, pval *item, int depth);
    
    static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext);
    static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label);
    static pval *get_goto_target(pval *item);
    static int label_inside_case(pval *label);
    static void attach_exten(struct ael_extension **list, struct ael_extension *newmem);
    static void fix_gotos_in_extensions(struct ael_extension *exten);
    static pval *get_extension_or_contxt(pval *p);
    static pval *get_contxt(pval *p);
    static void remove_spaces_before_equals(char *str);
    
    /* PRETTY PRINTER FOR AEL:  ============================================================================= */
    
    static void print_pval(FILE *fin, pval *item, int depth)
    {
    	int i;
    	pval *lp;
    	
    	for (i=0; i<depth; i++) {
    		fprintf(fin, "\t"); /* depth == indentation */
    	}
    	
    	switch ( item->type ) {
    	case PV_WORD:
    		fprintf(fin,"%s;\n", item->u1.str); /* usually, words are encapsulated in something else */
    		break;
    		
    	case PV_MACRO:
    		fprintf(fin,"macro %s(", item->u1.str);
    		for (lp=item->u2.arglist; lp; lp=lp->next) {
    			if (lp != item->u2.arglist )
    				fprintf(fin,", ");
    			fprintf(fin,"%s", lp->u1.str);
    		}
    		fprintf(fin,") {\n");
    		print_pval_list(fin,item->u3.macro_statements,depth+1);
    		for (i=0; i<depth; i++) {
    			fprintf(fin,"\t"); /* depth == indentation */
    		}
    		fprintf(fin,"};\n\n");
    		break;
    			
    	case PV_CONTEXT:
    		if ( item->u3.abstract )
    			fprintf(fin,"abstract context %s {\n", item->u1.str);
    		else
    			fprintf(fin,"context %s {\n", item->u1.str);
    		print_pval_list(fin,item->u2.statements,depth+1);
    		for (i=0; i<depth; i++) {
    			fprintf(fin,"\t"); /* depth == indentation */
    		}
    		fprintf(fin,"};\n\n");
    		break;
    			
    	case PV_MACRO_CALL:
    		fprintf(fin,"&%s(", item->u1.str);
    		for (lp=item->u2.arglist; lp; lp=lp->next) {
    			if ( lp != item->u2.arglist )
    				fprintf(fin,", ");
    			fprintf(fin,"%s", lp->u1.str);
    		}
    		fprintf(fin,");\n");
    		break;
    			
    	case PV_APPLICATION_CALL:
    		fprintf(fin,"%s(", item->u1.str);
    		for (lp=item->u2.arglist; lp; lp=lp->next) {
    			if ( lp != item->u2.arglist )
    				fprintf(fin,",");
    			fprintf(fin,"%s", lp->u1.str);
    		}
    		fprintf(fin,");\n");
    		break;
    			
    	case PV_CASE:
    		fprintf(fin,"case %s:\n", item->u1.str);
    		print_pval_list(fin,item->u2.statements, depth+1);
    		break;
    			
    	case PV_PATTERN:
    		fprintf(fin,"pattern %s:\n", item->u1.str);
    		print_pval_list(fin,item->u2.statements, depth+1);
    		break;
    			
    	case PV_DEFAULT:
    		fprintf(fin,"default:\n");
    		print_pval_list(fin,item->u2.statements, depth+1);
    		break;
    			
    	case PV_CATCH:
    		fprintf(fin,"catch %s {\n", item->u1.str);
    		print_pval_list(fin,item->u2.statements, depth+1);
    		for (i=0; i<depth; i++) {
    			fprintf(fin,"\t"); /* depth == indentation */
    		}
    		fprintf(fin,"};\n");
    		break;
    			
    	case PV_SWITCHES:
    		fprintf(fin,"switches {\n");
    		print_pval_list(fin,item->u1.list,depth+1);
    		for (i=0; i<depth; i++) {
    			fprintf(fin,"\t"); /* depth == indentation */
    		}
    		fprintf(fin,"};\n");
    		break;
    			
    	case PV_ESWITCHES:
    		fprintf(fin,"eswitches {\n");
    		print_pval_list(fin,item->u1.list,depth+1);
    		for (i=0; i<depth; i++) {
    			fprintf(fin,"\t"); /* depth == indentation */
    		}
    		fprintf(fin,"};\n");
    		break;
    			
    	case PV_INCLUDES:
    		fprintf(fin,"includes {\n");
    		for (lp=item->u1.list; lp; lp=lp->next) {
    			for (i=0; i<depth+1; i++) {
    				fprintf(fin,"\t"); /* depth == indentation */
    			}
    			fprintf(fin,"%s", lp->u1.str); /* usually, words are encapsulated in something else */
    			if (lp->u2.arglist)
    				fprintf(fin,"|%s|%s|%s|%s", 
    						lp->u2.arglist->u1.str,
    						lp->u2.arglist->next->u1.str,
    						lp->u2.arglist->next->next->u1.str,
    						lp->u2.arglist->next->next->next->u1.str
    					);
    			fprintf(fin,";\n"); /* usually, words are encapsulated in something else */
    		}
    		
    		for (i=0; i<depth; i++) {
    			fprintf(fin,"\t"); /* depth == indentation */
    		}
    		fprintf(fin,"};\n");
    		break;
    			
    	case PV_STATEMENTBLOCK:
    		fprintf(fin,"{\n");
    		print_pval_list(fin,item->u1.list, depth+1);
    		for (i=0; i<depth; i++) {
    			fprintf(fin,"\t"); /* depth == indentation */
    		}
    		fprintf(fin,"}\n");
    		break;
    			
    	case PV_VARDEC:
    		fprintf(fin,"%s=%s;\n", item->u1.str, item->u2.val);
    		break;
    			
    	case PV_LOCALVARDEC:
    		fprintf(fin,"local %s=%s;\n", item->u1.str, item->u2.val);
    		break;
    			
    	case PV_GOTO:
    		fprintf(fin,"goto %s", item->u1.list->u1.str);
    		if ( item->u1.list->next )
    			fprintf(fin,",%s", item->u1.list->next->u1.str);
    		if ( item->u1.list->next && item->u1.list->next->next )
    			fprintf(fin,",%s", item->u1.list->next->next->u1.str);
    		fprintf(fin,"\n");
    		break;
    			
    	case PV_LABEL:
    		fprintf(fin,"%s:\n", item->u1.str);
    		break;
    			
    	case PV_FOR:
    		fprintf(fin,"for (%s; %s; %s)\n", item->u1.for_init, item->u2.for_test, item->u3.for_inc);
    		print_pval_list(fin,item->u4.for_statements,depth+1);
    		break;
    			
    	case PV_WHILE:
    		fprintf(fin,"while (%s)\n", item->u1.str);
    		print_pval_list(fin,item->u2.statements,depth+1);
    		break;
    			
    	case PV_BREAK:
    		fprintf(fin,"break;\n");
    		break;
    			
    	case PV_RETURN:
    		fprintf(fin,"return;\n");
    		break;
    			
    	case PV_CONTINUE:
    		fprintf(fin,"continue;\n");
    		break;
    			
    	case PV_RANDOM:
    	case PV_IFTIME:
    	case PV_IF:
    		if ( item->type == PV_IFTIME ) {
    			
    			fprintf(fin,"ifTime ( %s|%s|%s|%s )\n", 
    					item->u1.list->u1.str, 
    					item->u1.list->next->u1.str, 
    					item->u1.list->next->next->u1.str, 
    					item->u1.list->next->next->next->u1.str
    					);
    		} else if ( item->type == PV_RANDOM ) {
    			fprintf(fin,"random ( %s )\n", item->u1.str );
    		} else
    			fprintf(fin,"if ( %s )\n", item->u1.str);
    		if ( item->u2.statements && item->u2.statements->next ) {
    			for (i=0; i<depth; i++) {
    				fprintf(fin,"\t"); /* depth == indentation */
    			}
    			fprintf(fin,"{\n");
    			print_pval_list(fin,item->u2.statements,depth+1);
    			for (i=0; i<depth; i++) {
    				fprintf(fin,"\t"); /* depth == indentation */
    			}
    			if ( item->u3.else_statements )
    				fprintf(fin,"}\n");
    			else
    				fprintf(fin,"};\n");
    		} else if (item->u2.statements ) {
    			print_pval_list(fin,item->u2.statements,depth+1);
    		} else {
    			if (item->u3.else_statements )
    				fprintf(fin, " {} ");
    			else
    				fprintf(fin, " {}; ");
    		}
    		if ( item->u3.else_statements ) {
    			for (i=0; i<depth; i++) {
    				fprintf(fin,"\t"); /* depth == indentation */
    			}
    			fprintf(fin,"else\n");
    			print_pval_list(fin,item->u3.else_statements, depth);
    		}
    		break;
    			
    	case PV_SWITCH:
    		fprintf(fin,"switch( %s ) {\n", item->u1.str);
    		print_pval_list(fin,item->u2.statements,depth+1);
    		for (i=0; i<depth; i++) {
    			fprintf(fin,"\t"); /* depth == indentation */
    		}
    		fprintf(fin,"}\n");
    		break;
    			
    	case PV_EXTENSION:
    		if ( item->u4.regexten )
    			fprintf(fin, "regexten ");
    		if ( item->u3.hints )
    
    			fprintf(fin,"hints(%s) ", item->u3.hints);
    
    347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    		
    		fprintf(fin,"%s => \n", item->u1.str);
    		print_pval_list(fin,item->u2.statements,depth+1);
    		break;
    			
    	case PV_IGNOREPAT:
    		fprintf(fin,"ignorepat => %s;\n", item->u1.str);
    		break;
    			
    	case PV_GLOBALS:
    		fprintf(fin,"globals {\n");
    		print_pval_list(fin,item->u1.statements,depth+1);
    		for (i=0; i<depth; i++) {
    			fprintf(fin,"\t"); /* depth == indentation */
    		}
    		fprintf(fin,"}\n");
    		break;
    	}
    }
    
    static void print_pval_list(FILE *fin, pval *item, int depth)
    {
    	pval *i;
    	
    	for (i=item; i; i=i->next) {
    		print_pval(fin, i, depth);
    	}
    }
    
    void ael2_print(char *fname, pval *tree)
    {
    	FILE *fin = fopen(fname,"w");
    	if ( !fin ) {
    		ast_log(LOG_ERROR, "Couldn't open %s for writing.\n", fname);
    		return;
    	}
    	print_pval_list(fin, tree, 0);
    	fclose(fin);
    }
    
    
    /* EMPTY TEMPLATE FUNCS FOR AEL TRAVERSAL:  ============================================================================= */
    
    void traverse_pval_template(pval *item, int depth);
    void traverse_pval_item_template(pval *item, int depth);
    
    
    void traverse_pval_item_template(pval *item, int depth)/* depth comes in handy for a pretty print (indentation),
    														  but you may not need it */
    {
    	pval *lp;
    	
    	switch ( item->type ) {
    	case PV_WORD:
    		/* fields: item->u1.str == string associated with this (word). */
    		break;
    		
    	case PV_MACRO:
    		/* fields: item->u1.str     == name of macro
    		           item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
    				   item->u2.arglist->u1.str  == argument
    				   item->u2.arglist->next   == next arg
    
    				   item->u3.macro_statements == pval list of statements in macro body.
    		*/
    		for (lp=item->u2.arglist; lp; lp=lp->next) {
    		
    		}
    		traverse_pval_item_template(item->u3.macro_statements,depth+1);
    		break;
    			
    	case PV_CONTEXT:
    		/* fields: item->u1.str     == name of context
    		           item->u2.statements == pval list of statements in context body
    				   item->u3.abstract == int 1 if an abstract keyword were present
    		*/
    		traverse_pval_item_template(item->u2.statements,depth+1);
    		break;
    			
    	case PV_MACRO_CALL:
    		/* fields: item->u1.str     == name of macro to call
    		           item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
    				   item->u2.arglist->u1.str  == argument
    				   item->u2.arglist->next   == next arg
    		*/
    		for (lp=item->u2.arglist; lp; lp=lp->next) {
    		}
    		break;
    			
    	case PV_APPLICATION_CALL:
    		/* fields: item->u1.str     == name of application to call
    		           item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
    				   item->u2.arglist->u1.str  == argument
    				   item->u2.arglist->next   == next arg
    		*/
    		for (lp=item->u2.arglist; lp; lp=lp->next) {
    		}
    		break;
    			
    	case PV_CASE:
    		/* fields: item->u1.str     == value of case
    		           item->u2.statements == pval list of statements under the case
    		*/
    		traverse_pval_item_template(item->u2.statements,depth+1);
    		break;
    			
    	case PV_PATTERN:
    		/* fields: item->u1.str     == value of case
    		           item->u2.statements == pval list of statements under the case
    		*/
    		traverse_pval_item_template(item->u2.statements,depth+1);
    		break;
    			
    	case PV_DEFAULT:
    		/* fields: 
    		           item->u2.statements == pval list of statements under the case
    		*/
    		traverse_pval_item_template(item->u2.statements,depth+1);
    		break;
    			
    	case PV_CATCH:
    		/* fields: item->u1.str     == name of extension to catch
    		           item->u2.statements == pval list of statements in context body
    		*/
    		traverse_pval_item_template(item->u2.statements,depth+1);
    		break;
    			
    	case PV_SWITCHES:
    		/* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
    		*/
    		traverse_pval_item_template(item->u1.list,depth+1);
    		break;
    			
    	case PV_ESWITCHES:
    		/* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
    		*/
    		traverse_pval_item_template(item->u1.list,depth+1);
    		break;
    			
    	case PV_INCLUDES:
    		/* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
    		           item->u2.arglist  == pval list of 4 PV_WORD elements for time values
    		*/
    		traverse_pval_item_template(item->u1.list,depth+1);
    		traverse_pval_item_template(item->u2.arglist,depth+1);
    		break;
    			
    	case PV_STATEMENTBLOCK:
    		/* fields: item->u1.list     == pval list of statements in block, one per entry in the list
    		*/
    		traverse_pval_item_template(item->u1.list,depth+1);
    		break;
    			
    	case PV_LOCALVARDEC:
    	case PV_VARDEC:
    		/* fields: item->u1.str     == variable name
    		           item->u2.val     == variable value to assign
    		*/
    		break;
    			
    	case PV_GOTO:
    		/* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
    		           item->u1.list->u1.str  == where the data on a PV_WORD will always be.
    		*/
    		
    		if ( item->u1.list->next )
    			;
    		if ( item->u1.list->next && item->u1.list->next->next )
    			;
    		
    		break;
    			
    	case PV_LABEL:
    		/* fields: item->u1.str     == label name
    		*/
    		break;
    			
    	case PV_FOR:
    		/* fields: item->u1.for_init     == a string containing the initalizer
    		           item->u2.for_test     == a string containing the loop test
    		           item->u3.for_inc      == a string containing the loop increment
    
    				   item->u4.for_statements == a pval list of statements in the for ()
    		*/
    		traverse_pval_item_template(item->u4.for_statements,depth+1);
    		break;
    			
    	case PV_WHILE:
    		/* fields: item->u1.str        == the while conditional, as supplied by user
    
    				   item->u2.statements == a pval list of statements in the while ()
    		*/
    		traverse_pval_item_template(item->u2.statements,depth+1);
    		break;
    			
    	case PV_BREAK:
    		/* fields: none
    		*/
    		break;
    			
    	case PV_RETURN:
    		/* fields: none
    		*/
    		break;
    			
    	case PV_CONTINUE:
    		/* fields: none
    		*/
    		break;
    			
    	case PV_IFTIME:
    		/* fields: item->u1.list        == there are 4 linked PV_WORDs here.
    
    				   item->u2.statements == a pval list of statements in the if ()
    				   item->u3.else_statements == a pval list of statements in the else
    											   (could be zero)
    		*/
    		traverse_pval_item_template(item->u2.statements,depth+1);
    		if ( item->u3.else_statements ) {
    			traverse_pval_item_template(item->u3.else_statements,depth+1);
    		}
    		break;
    			
    	case PV_RANDOM:
    		/* fields: item->u1.str        == the random number expression, as supplied by user
    
    				   item->u2.statements == a pval list of statements in the if ()
    				   item->u3.else_statements == a pval list of statements in the else
    											   (could be zero)
    		*/
    		traverse_pval_item_template(item->u2.statements,depth+1);
    		if ( item->u3.else_statements ) {
    			traverse_pval_item_template(item->u3.else_statements,depth+1);
    		}
    		break;
    			
    	case PV_IF:
    		/* fields: item->u1.str        == the if conditional, as supplied by user
    
    				   item->u2.statements == a pval list of statements in the if ()
    				   item->u3.else_statements == a pval list of statements in the else
    											   (could be zero)
    		*/
    		traverse_pval_item_template(item->u2.statements,depth+1);
    		if ( item->u3.else_statements ) {
    			traverse_pval_item_template(item->u3.else_statements,depth+1);
    		}
    		break;
    			
    	case PV_SWITCH:
    		/* fields: item->u1.str        == the switch expression
    
    				   item->u2.statements == a pval list of statements in the switch, 
    				   							(will be case statements, most likely!)
    		*/
    		traverse_pval_item_template(item->u2.statements,depth+1);
    		break;
    			
    	case PV_EXTENSION:
    		/* fields: item->u1.str        == the extension name, label, whatever it's called
    
    				   item->u2.statements == a pval list of statements in the extension
    				   item->u3.hints      == a char * hint argument
    				   item->u4.regexten   == an int boolean. non-zero says that regexten was specified
    		*/
    		traverse_pval_item_template(item->u2.statements,depth+1);
    		break;
    			
    	case PV_IGNOREPAT:
    		/* fields: item->u1.str        == the ignorepat data
    		*/
    		break;
    			
    	case PV_GLOBALS:
    		/* fields: item->u1.statements     == pval list of statements, usually vardecs
    		*/
    		traverse_pval_item_template(item->u1.statements,depth+1);
    		break;
    	}
    }
    
    void traverse_pval_template(pval *item, int depth) /* depth comes in handy for a pretty print (indentation),
    													  but you may not need it */
    {
    	pval *i;
    	
    	for (i=item; i; i=i->next) {
    		traverse_pval_item_template(i, depth);
    	}
    }
    
    
    /* SEMANTIC CHECKING FOR AEL:  ============================================================================= */
    
    /*   (not all that is syntactically legal is good! */
    
    
    static void check_macro_returns(pval *macro)
    {
    	pval *i;
    	if (!macro->u3.macro_statements)
    	{
    		pval *z = calloc(1, sizeof(struct pval));
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s is empty! I will insert a return.\n",
    				macro->filename, macro->startline, macro->endline, macro->u1.str);
    
    		z->type = PV_RETURN;
    		z->startline = macro->startline;
    		z->endline = macro->endline;
    		z->startcol = macro->startcol;
    		z->endcol = macro->endcol;
    		z->filename = strdup(macro->filename);
    
    		macro->u3.macro_statements = z;
    		return;
    	}
    	for (i=macro->u3.macro_statements; i; i=i->next) {
    		/* if the last statement in the list is not return, then insert a return there */
    		if (i->next == NULL) {
    			if (i->type != PV_RETURN) {
    				pval *z = calloc(1, sizeof(struct pval));
    				ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s does not end with a return; I will insert one.\n",
    						macro->filename, macro->startline, macro->endline, macro->u1.str);
    
    				z->type = PV_RETURN;
    				z->startline = macro->startline;
    				z->endline = macro->endline;
    				z->startcol = macro->startcol;
    				z->endcol = macro->endcol;
    				z->filename = strdup(macro->filename);
    
    				i->next = z;
    				return;
    			}
    		}
    	}
    	return;
    }
    
    
    
    static int extension_matches(pval *here, const char *exten, const char *pattern)
    {
    	int err1;
    	regex_t preg;
    	
    	/* simple case, they match exactly, the pattern and exten name */
    	if (!strcmp(pattern,exten) == 0)
    		return 1;
    	
    	if (pattern[0] == '_') {
    		char reg1[2000];
    		const char *p;
    		char *r = reg1;
    		
    		if ( strlen(pattern)*5 >= 2000 ) /* safety valve */ {
    			ast_log(LOG_ERROR,"Error: The pattern %s is way too big. Pattern matching cancelled.\n",
    					pattern);
    			return 0;
    		}
    		/* form a regular expression from the pattern, and then match it against exten */
    		*r++ = '^'; /* what if the extension is a pattern ?? */
    		*r++ = '_'; /* what if the extension is a pattern ?? */
    		*r++ = '?';
    		for (p=pattern+1; *p; p++) {
    			switch ( *p ) {
    			case 'X':
    				*r++ = '[';
    				*r++ = '0';
    				*r++ = '-';
    				*r++ = '9';
    				*r++ = 'X';
    				*r++ = ']';
    				break;
    				
    			case 'Z':
    				*r++ = '[';
    				*r++ = '1';
    				*r++ = '-';
    				*r++ = '9';
    				*r++ = 'Z';
    				*r++ = ']';
    				break;
    				
    			case 'N':
    				*r++ = '[';
    				*r++ = '2';
    				*r++ = '-';
    				*r++ = '9';
    				*r++ = 'N';
    				*r++ = ']';
    				break;
    				
    			case '[':
    				while ( *p && *p != ']' ) {
    					*r++ = *p++;
    				}
    				if ( *p != ']') {
    					ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The extension pattern '%s' is missing a closing bracket \n",
    							here->filename, here->startline, here->endline, pattern);
    				}
    				break;
    				
    			case '.':
    			case '!':
    				*r++ = '.';
    				*r++ = '*';
    				break;
    			case '*':
    				*r++ = '\\';
    				*r++ = '*';
    				break;
    			default:
    				*r++ = *p;
    				break;
    				
    			}
    		}
    		*r++ = '$'; /* what if the extension is a pattern ?? */
    		*r++ = *p++; /* put in the closing null */
    		err1 = regcomp(&preg, reg1, REG_NOSUB|REG_EXTENDED);
    		if ( err1 ) {
    			char errmess[500];
    			regerror(err1,&preg,errmess,sizeof(errmess));
    			regfree(&preg);
    			ast_log(LOG_WARNING, "Regcomp of %s failed, error code %d\n",
    					reg1, err1);
    			return 0;
    		}
    		err1 = regexec(&preg, exten, 0, 0, 0);
    		regfree(&preg);
    		
    		if ( err1 ) {
    			/* ast_log(LOG_NOTICE,"*****************************[%d]Extension %s did not match %s(%s)\n",
    			   err1,exten, pattern, reg1); */
    			return 0; /* no match */
    		} else {
    			/* ast_log(LOG_NOTICE,"*****************************Extension %s matched %s\n",
    			   exten, pattern); */
    			return 1;
    		}
    		
    		
    	} else {
    		if ( strcmp(exten,pattern) == 0 ) {
    			return 1;
    		} else
    			return 0;
    	}
    }
    
    
    static void check_expr2_input(pval *expr, char *str)
    {
    	int spaces = strspn(str,"\t \n");
    	if ( !strncmp(str+spaces,"$[",2) ) {
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The expression '%s' is redundantly wrapped in '$[ ]'. \n",
    				expr->filename, expr->startline, expr->endline, str);
    		warns++;
    	}
    }
    
    static void check_includes(pval *includes)
    {
    	struct pval *p4;
    	for (p4=includes->u1.list; p4; p4=p4->next) {
    		/* for each context pointed to, find it, then find a context/label that matches the
    		   target here! */
    		char *incl_context = p4->u1.str;
    		/* find a matching context name */
    		struct pval *that_other_context = find_context(incl_context);
    		if (!that_other_context && strcmp(incl_context, "parkedcalls") != 0) {
    			ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The included context '%s' cannot be found.\n",
    					includes->filename, includes->startline, includes->endline, incl_context);
    			warns++;
    		}
    	}
    }
    
    
    static void check_timerange(pval *p)
    {
    	char *times;
    	char *e;
    	int s1, s2;
    	int e1, e2;
    
    	times = ast_strdupa(p->u1.str);
    
    	/* Star is all times */
    	if (ast_strlen_zero(times) || !strcmp(times, "*")) {
    		return;
    	}
    	/* Otherwise expect a range */
    	e = strchr(times, '-');
    	if (!e) {
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) requires a '-' surrounded by two 24-hour times of day!\n",
    				p->filename, p->startline, p->endline, times);
    		warns++;
    		return;
    	}
    	*e = '\0';
    	e++;
    	while (*e && !isdigit(*e)) 
    		e++;
    	if (!*e) {
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
    				p->filename, p->startline, p->endline, p->u1.str);
    		warns++;
    	}
    	if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
    				p->filename, p->startline, p->endline, times);
    		warns++;
    	}
    	if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
    				p->filename, p->startline, p->endline, times);
    		warns++;
    	}
    
    	s1 = s1 * 30 + s2/2;
    	if ((s1 < 0) || (s1 >= 24*30)) {
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
    				p->filename, p->startline, p->endline, times);
    		warns++;
    	}
    	e1 = e1 * 30 + e2/2;
    	if ((e1 < 0) || (e1 >= 24*30)) {
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
    				p->filename, p->startline, p->endline, e);
    		warns++;
    	}
    	return;
    }
    
    static char *days[] =
    {
    	"sun",
    	"mon",
    	"tue",
    	"wed",
    	"thu",
    	"fri",
    	"sat",
    };
    
    /*! \brief  get_dow: Get day of week */
    static void check_dow(pval *DOW)
    {
    	char *dow;
    	char *c;
    	/* The following line is coincidence, really! */
    	int s, e;
    	
    	dow = ast_strdupa(DOW->u1.str);
    
    	/* Check for all days */
    	if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
    		return;
    	/* Get start and ending days */
    	c = strchr(dow, '-');
    	if (c) {
    		*c = '\0';
    		c++;
    	} else
    		c = NULL;
    	/* Find the start */
    	s = 0;
    	while ((s < 7) && strcasecmp(dow, days[s])) s++;
    	if (s >= 7) {
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The day (%s) must be one of 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', or 'sat'!\n",
    				DOW->filename, DOW->startline, DOW->endline, dow);
    		warns++;
    	}
    	if (c) {
    		e = 0;
    		while ((e < 7) && strcasecmp(c, days[e])) e++;
    		if (e >= 7) {
    			ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day (%s) must be one of 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', or 'sat'!\n",
    					DOW->filename, DOW->startline, DOW->endline, c);
    			warns++;
    		}
    	} else
    		e = s;
    }
    
    static void check_day(pval *DAY)
    {
    	char *day;
    	char *c;
    	/* The following line is coincidence, really! */
    	int s, e;
    
    	day = ast_strdupa(DAY->u1.str);
    
    	/* Check for all days */
    	if (ast_strlen_zero(day) || !strcmp(day, "*")) {
    		return;
    	}
    	/* Get start and ending days */
    	c = strchr(day, '-');
    	if (c) {
    		*c = '\0';
    		c++;
    	}
    	/* Find the start */
    	if (sscanf(day, "%d", &s) != 1) {
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
    				DAY->filename, DAY->startline, DAY->endline, day);
    		warns++;
    	}
    	else if ((s < 1) || (s > 31)) {
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number in the range [1-31]!\n",
    				DAY->filename, DAY->startline, DAY->endline, day);
    		warns++;
    	}
    	s--;
    	if (c) {
    		if (sscanf(c, "%d", &e) != 1) {
    			ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
    					DAY->filename, DAY->startline, DAY->endline, c);
    			warns++;
    		}
    		else if ((e < 1) || (e > 31)) {
    			ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number in the range [1-31]!\n",
    					DAY->filename, DAY->startline, DAY->endline, day);
    			warns++;
    		}
    		e--;
    	} else
    		e = s;
    }
    
    static char *months[] =
    {
    	"jan",
    	"feb",
    	"mar",
    	"apr",
    	"may",
    	"jun",
    	"jul",
    	"aug",
    	"sep",
    	"oct",
    	"nov",
    	"dec",
    };
    
    static void check_month(pval *MON)
    {
    	char *mon;
    	char *c;