Fri Jul 3 20:35:39 2009

Asterisk developer's documentation


pbx_ael.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2006, Digium, Inc.
00005  *
00006  * Steve Murphy <murf@parsetree.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Compile symbolic Asterisk Extension Logic into Asterisk extensions, version 2.
00022  * 
00023  */
00024 
00025 #include "asterisk.h"
00026 
00027 #if !defined(STANDALONE_AEL)
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00029 #endif
00030 
00031 #include <sys/types.h>
00032 #include <stdlib.h>
00033 #include <unistd.h>
00034 #include <stdio.h>
00035 #include <string.h>
00036 #include <ctype.h>
00037 #include <errno.h>
00038 #include <regex.h>
00039 #include <sys/stat.h>
00040 
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/config.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/logger.h"
00045 #include "asterisk/cli.h"
00046 #include "asterisk/app.h"
00047 #include "asterisk/callerid.h"
00048 #include "asterisk/ael_structs.h"
00049 #ifdef AAL_ARGCHECK
00050 #include "asterisk/argdesc.h"
00051 #endif
00052 
00053 static char expr_output[2096];
00054 
00055 /* these functions are in ../ast_expr2.fl */
00056 
00057 #define DEBUG_READ   (1 << 0)
00058 #define DEBUG_TOKENS (1 << 1)
00059 #define DEBUG_MACROS (1 << 2)
00060 #define DEBUG_CONTEXTS (1 << 3)
00061 
00062 static char *config = "extensions.ael";
00063 static char *registrar = "pbx_ael";
00064 static int pbx_load_module(void);
00065 
00066 static int errs, warns;
00067 static int notes;
00068 
00069 #ifndef AAL_ARGCHECK
00070 /* for the time being, short circuit all the AAL related structures
00071    without permanently removing the code; after/during the AAL 
00072    development, this code can be properly re-instated 
00073 */
00074 
00075 /* null definitions for structs passed down the infrastructure */
00076 struct argapp
00077 {
00078    struct argapp *next;
00079 };
00080 
00081 #endif
00082 
00083 #ifdef AAL_ARGCHECK
00084 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app);
00085 int option_matches( struct argdesc *should, pval *is, struct argapp *app);
00086 int ael_is_funcname(char *name);
00087 #endif
00088 
00089 int check_app_args(pval *appcall, pval *arglist, struct argapp *app);
00090 void check_pval(pval *item, struct argapp *apps, int in_globals);
00091 void check_pval_item(pval *item, struct argapp *apps, int in_globals);
00092 void check_switch_expr(pval *item, struct argapp *apps);
00093 void ast_expr_register_extra_error_info(char *errmsg);
00094 void ast_expr_clear_extra_error_info(void);
00095 int  ast_expr(char *expr, char *buf, int length);
00096 struct pval *find_macro(char *name);
00097 struct pval *find_context(char *name);
00098 struct pval *find_context(char *name);
00099 struct pval *find_macro(char *name);
00100 struct ael_priority *new_prio(void);
00101 struct ael_extension *new_exten(void);
00102 void linkprio(struct ael_extension *exten, struct ael_priority *prio, struct ael_extension *mother_exten);
00103 void destroy_extensions(struct ael_extension *exten);
00104 static void linkexten(struct ael_extension *exten, struct ael_extension *add);
00105 static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *context );
00106 void set_priorities(struct ael_extension *exten);
00107 void add_extensions(struct ael_extension *exten);
00108 void ast_compile_ael2(struct ast_context **local_contexts, struct pval *root);
00109 void destroy_pval(pval *item);
00110 void destroy_pval_item(pval *item);
00111 int is_float(char *arg );
00112 int is_int(char *arg );
00113 int is_empty(char *arg);
00114 static pval *current_db=0;
00115 static pval *current_context=0;
00116 static pval *current_extension=0;
00117 
00118 static const char *match_context;
00119 static const char *match_exten;
00120 static const char *match_label;
00121 static int in_abstract_context;
00122 static int count_labels; /* true, put matcher in label counting mode */
00123 static int label_count;  /* labels are only meant to be counted in a context or exten */
00124 static int return_on_context_match;
00125 static pval *last_matched_label;
00126 struct pval *match_pval(pval *item);
00127 static void check_timerange(pval *p);
00128 static void check_dow(pval *DOW);
00129 static void check_day(pval *DAY);
00130 static void check_month(pval *MON);
00131 static void check_expr2_input(pval *expr, char *str);
00132 static int extension_matches(pval *here, const char *exten, const char *pattern);
00133 static void check_goto(pval *item);
00134 static void find_pval_goto_item(pval *item, int lev);
00135 static void find_pval_gotos(pval *item, int lev);
00136 
00137 static struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont);
00138 static struct pval *find_first_label_in_current_context(char *label, pval *curr_cont);
00139 static void print_pval_list(FILE *fin, pval *item, int depth);
00140 
00141 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext);
00142 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label);
00143 static pval *get_goto_target(pval *item);
00144 static int label_inside_case(pval *label);
00145 static void attach_exten(struct ael_extension **list, struct ael_extension *newmem);
00146 static void fix_gotos_in_extensions(struct ael_extension *exten);
00147 static pval *get_extension_or_contxt(pval *p);
00148 static pval *get_contxt(pval *p);
00149 static void remove_spaces_before_equals(char *str);
00150 static void substitute_commas(char *str);
00151 
00152 /* I am adding this code to substitute commas with vertbars in the args to apps */
00153 static void substitute_commas(char *str)
00154 {
00155    char *p = str;
00156    
00157    while (p && *p)
00158    {
00159       if (*p == ',' && ((p != str && *(p-1) != '\\')
00160             || p == str))
00161          *p = '|';
00162       if (*p == '\\' && *(p+1) == ',') { /* learning experience: the '\,' is turned into just ',' by pbx_config; So we need to do the same */
00163          char *q = p;
00164          while (*q) {  /* move the ',' and everything after it up 1 char */
00165             *q = *(q+1);
00166             q++;
00167          }
00168       }
00169       p++;
00170    }
00171 }
00172 
00173 
00174 /* PRETTY PRINTER FOR AEL:  ============================================================================= */
00175 
00176 static void print_pval(FILE *fin, pval *item, int depth)
00177 {
00178    int i;
00179    pval *lp;
00180    
00181    for (i=0; i<depth; i++) {
00182       fprintf(fin, "\t"); /* depth == indentation */
00183    }
00184    
00185    switch ( item->type ) {
00186    case PV_WORD:
00187       fprintf(fin,"%s;\n", item->u1.str); /* usually, words are encapsulated in something else */
00188       break;
00189       
00190    case PV_MACRO:
00191       fprintf(fin,"macro %s(", item->u1.str);
00192       for (lp=item->u2.arglist; lp; lp=lp->next) {
00193          if (lp != item->u2.arglist )
00194             fprintf(fin,", ");
00195          fprintf(fin,"%s", lp->u1.str);
00196       }
00197       fprintf(fin,") {\n");
00198       print_pval_list(fin,item->u3.macro_statements,depth+1);
00199       for (i=0; i<depth; i++) {
00200          fprintf(fin,"\t"); /* depth == indentation */
00201       }
00202       fprintf(fin,"};\n\n");
00203       break;
00204          
00205    case PV_CONTEXT:
00206       if ( item->u3.abstract )
00207          fprintf(fin,"abstract context %s {\n", item->u1.str);
00208       else
00209          fprintf(fin,"context %s {\n", item->u1.str);
00210       print_pval_list(fin,item->u2.statements,depth+1);
00211       for (i=0; i<depth; i++) {
00212          fprintf(fin,"\t"); /* depth == indentation */
00213       }
00214       fprintf(fin,"};\n\n");
00215       break;
00216          
00217    case PV_MACRO_CALL:
00218       fprintf(fin,"&%s(", item->u1.str);
00219       for (lp=item->u2.arglist; lp; lp=lp->next) {
00220          if ( lp != item->u2.arglist )
00221             fprintf(fin,", ");
00222          fprintf(fin,"%s", lp->u1.str);
00223       }
00224       fprintf(fin,");\n");
00225       break;
00226          
00227    case PV_APPLICATION_CALL:
00228       fprintf(fin,"%s(", item->u1.str);
00229       for (lp=item->u2.arglist; lp; lp=lp->next) {
00230          if ( lp != item->u2.arglist )
00231             fprintf(fin,",");
00232          fprintf(fin,"%s", lp->u1.str);
00233       }
00234       fprintf(fin,");\n");
00235       break;
00236          
00237    case PV_CASE:
00238       fprintf(fin,"case %s:\n", item->u1.str);
00239       print_pval_list(fin,item->u2.statements, depth+1);
00240       break;
00241          
00242    case PV_PATTERN:
00243       fprintf(fin,"pattern %s:\n", item->u1.str);
00244       print_pval_list(fin,item->u2.statements, depth+1);
00245       break;
00246          
00247    case PV_DEFAULT:
00248       fprintf(fin,"default:\n");
00249       print_pval_list(fin,item->u2.statements, depth+1);
00250       break;
00251          
00252    case PV_CATCH:
00253       fprintf(fin,"catch %s {\n", item->u1.str);
00254       print_pval_list(fin,item->u2.statements, depth+1);
00255       for (i=0; i<depth; i++) {
00256          fprintf(fin,"\t"); /* depth == indentation */
00257       }
00258       fprintf(fin,"};\n");
00259       break;
00260          
00261    case PV_SWITCHES:
00262       fprintf(fin,"switches {\n");
00263       print_pval_list(fin,item->u1.list,depth+1);
00264       for (i=0; i<depth; i++) {
00265          fprintf(fin,"\t"); /* depth == indentation */
00266       }
00267       fprintf(fin,"};\n");
00268       break;
00269          
00270    case PV_ESWITCHES:
00271       fprintf(fin,"eswitches {\n");
00272       print_pval_list(fin,item->u1.list,depth+1);
00273       for (i=0; i<depth; i++) {
00274          fprintf(fin,"\t"); /* depth == indentation */
00275       }
00276       fprintf(fin,"};\n");
00277       break;
00278          
00279    case PV_INCLUDES:
00280       fprintf(fin,"includes {\n");
00281       for (lp=item->u1.list; lp; lp=lp->next) {
00282          for (i=0; i<depth+1; i++) {
00283             fprintf(fin,"\t"); /* depth == indentation */
00284          }
00285          fprintf(fin,"%s", lp->u1.str); /* usually, words are encapsulated in something else */
00286          if ( lp->u2.arglist )
00287             fprintf(fin,"|%s|%s|%s|%s", 
00288                   lp->u2.arglist->u1.str,
00289                   lp->u2.arglist->next->u1.str,
00290                   lp->u2.arglist->next->next->u1.str,
00291                   lp->u2.arglist->next->next->next->u1.str
00292                );
00293          fprintf(fin,";\n"); /* usually, words are encapsulated in something else */
00294       }
00295       
00296       print_pval_list(fin,item->u1.list,depth+1);
00297       for (i=0; i<depth; i++) {
00298          fprintf(fin,"\t"); /* depth == indentation */
00299       }
00300       fprintf(fin,"};\n");
00301       break;
00302          
00303    case PV_STATEMENTBLOCK:
00304       fprintf(fin,"{\n");
00305       print_pval_list(fin,item->u1.list, depth+1);
00306       for (i=0; i<depth; i++) {
00307          fprintf(fin,"\t"); /* depth == indentation */
00308       }
00309       fprintf(fin,"};\n");
00310       break;
00311          
00312    case PV_VARDEC:
00313       fprintf(fin,"%s=%s;\n", item->u1.str, item->u2.val);
00314       break;
00315          
00316    case PV_GOTO:
00317       fprintf(fin,"goto %s", item->u1.list->u1.str);
00318       if ( item->u1.list->next )
00319          fprintf(fin,"|%s", item->u1.list->next->u1.str);
00320       if ( item->u1.list->next && item->u1.list->next->next )
00321          fprintf(fin,"|%s", item->u1.list->next->next->u1.str);
00322       fprintf(fin,"\n");
00323       break;
00324          
00325    case PV_LABEL:
00326       fprintf(fin,"%s:\n", item->u1.str);
00327       break;
00328          
00329    case PV_FOR:
00330       fprintf(fin,"for (%s; %s; %s)\n", item->u1.for_init, item->u2.for_test, item->u3.for_inc);
00331       print_pval_list(fin,item->u4.for_statements,depth+1);
00332       break;
00333          
00334    case PV_WHILE:
00335       fprintf(fin,"while (%s)\n", item->u1.str);
00336       print_pval_list(fin,item->u2.statements,depth+1);
00337       break;
00338          
00339    case PV_BREAK:
00340       fprintf(fin,"break;\n");
00341       break;
00342          
00343    case PV_RETURN:
00344       fprintf(fin,"return;\n");
00345       break;
00346          
00347    case PV_CONTINUE:
00348       fprintf(fin,"continue;\n");
00349       break;
00350          
00351    case PV_RANDOM:
00352    case PV_IFTIME:
00353    case PV_IF:
00354       if ( item->type == PV_IFTIME ) {
00355          
00356          fprintf(fin,"ifTime ( %s|%s|%s|%s )\n", 
00357                item->u1.list->u1.str, 
00358                item->u1.list->next->u1.str, 
00359                item->u1.list->next->next->u1.str, 
00360                item->u1.list->next->next->next->u1.str
00361                );
00362       } else if ( item->type == PV_RANDOM ) {
00363          fprintf(fin,"random ( %s )\n", item->u1.str );
00364       } else
00365          fprintf(fin,"if ( %s )\n", item->u1.str);
00366       if ( item->u2.statements && item->u2.statements->next ) {
00367          for (i=0; i<depth; i++) {
00368             fprintf(fin,"\t"); /* depth == indentation */
00369          }
00370          fprintf(fin,"{\n");
00371          print_pval_list(fin,item->u2.statements,depth+1);
00372          for (i=0; i<depth; i++) {
00373             fprintf(fin,"\t"); /* depth == indentation */
00374          }
00375          if ( item->u3.else_statements )
00376             fprintf(fin,"}\n");
00377          else
00378             fprintf(fin,"};\n");
00379       } else if (item->u2.statements ) {
00380          print_pval_list(fin,item->u2.statements,depth+1);
00381       } else {
00382          if (item->u3.else_statements )
00383             fprintf(fin, " {} ");
00384          else
00385             fprintf(fin, " {}; ");
00386       }
00387       if ( item->u3.else_statements ) {
00388          for (i=0; i<depth; i++) {
00389             fprintf(fin,"\t"); /* depth == indentation */
00390          }
00391          fprintf(fin,"else\n");
00392          print_pval_list(fin,item->u3.else_statements, depth);
00393       }
00394       break;
00395          
00396    case PV_SWITCH:
00397       fprintf(fin,"switch( %s ) {\n", item->u1.str);
00398       print_pval_list(fin,item->u2.statements,depth+1);
00399       for (i=0; i<depth; i++) {
00400          fprintf(fin,"\t"); /* depth == indentation */
00401       }
00402       fprintf(fin,"}\n");
00403       break;
00404          
00405    case PV_EXTENSION:
00406       if ( item->u4.regexten )
00407          fprintf(fin, "regexten ");
00408       if ( item->u3.hints )
00409          fprintf(fin,"hints(%s) ", item->u3.hints);
00410       
00411       fprintf(fin,"%s => \n", item->u1.str);
00412       print_pval_list(fin,item->u2.statements,depth+1);
00413       break;
00414          
00415    case PV_IGNOREPAT:
00416       fprintf(fin,"ignorepat => %s\n", item->u1.str);
00417       break;
00418          
00419    case PV_GLOBALS:
00420       fprintf(fin,"globals {\n");
00421       print_pval_list(fin,item->u1.statements,depth+1);
00422       for (i=0; i<depth; i++) {
00423          fprintf(fin,"\t"); /* depth == indentation */
00424       }
00425       fprintf(fin,"}\n");
00426       break;
00427    }
00428 }
00429 
00430 static void print_pval_list(FILE *fin, pval *item, int depth)
00431 {
00432    pval *i;
00433    
00434    for (i=item; i; i=i->next) {
00435       print_pval(fin, i, depth);
00436    }
00437 }
00438 
00439 #if 0
00440 static void ael2_print(char *fname, pval *tree)
00441 {
00442    FILE *fin = fopen(fname,"w");
00443    if ( !fin ) {
00444       ast_log(LOG_ERROR, "Couldn't open %s for writing.\n", fname);
00445       return;
00446    }
00447    print_pval_list(fin, tree, 0);
00448    fclose(fin);
00449 }
00450 #endif
00451 
00452 
00453 /* EMPTY TEMPLATE FUNCS FOR AEL TRAVERSAL:  ============================================================================= */
00454 
00455 void traverse_pval_template(pval *item, int depth);
00456 void traverse_pval_item_template(pval *item, int depth);
00457 
00458 
00459 void traverse_pval_item_template(pval *item, int depth)/* depth comes in handy for a pretty print (indentation),
00460                                             but you may not need it */
00461 {
00462    pval *lp;
00463    
00464    switch ( item->type ) {
00465    case PV_WORD:
00466       /* fields: item->u1.str == string associated with this (word). */
00467       break;
00468       
00469    case PV_MACRO:
00470       /* fields: item->u1.str     == name of macro
00471                  item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
00472                item->u2.arglist->u1.str  == argument
00473                item->u2.arglist->next   == next arg
00474 
00475                item->u3.macro_statements == pval list of statements in macro body.
00476       */
00477       for (lp=item->u2.arglist; lp; lp=lp->next) {
00478       
00479       }
00480       traverse_pval_item_template(item->u3.macro_statements,depth+1);
00481       break;
00482          
00483    case PV_CONTEXT:
00484       /* fields: item->u1.str     == name of context
00485                  item->u2.statements == pval list of statements in context body
00486                item->u3.abstract == int 1 if an abstract keyword were present
00487       */
00488       traverse_pval_item_template(item->u2.statements,depth+1);
00489       break;
00490          
00491    case PV_MACRO_CALL:
00492       /* fields: item->u1.str     == name of macro to call
00493                  item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
00494                item->u2.arglist->u1.str  == argument
00495                item->u2.arglist->next   == next arg
00496       */
00497       for (lp=item->u2.arglist; lp; lp=lp->next) {
00498       }
00499       break;
00500          
00501    case PV_APPLICATION_CALL:
00502       /* fields: item->u1.str     == name of application to call
00503                  item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
00504                item->u2.arglist->u1.str  == argument
00505                item->u2.arglist->next   == next arg
00506       */
00507       for (lp=item->u2.arglist; lp; lp=lp->next) {
00508       }
00509       break;
00510          
00511    case PV_CASE:
00512       /* fields: item->u1.str     == value of case
00513                  item->u2.statements == pval list of statements under the case
00514       */
00515       traverse_pval_item_template(item->u2.statements,depth+1);
00516       break;
00517          
00518    case PV_PATTERN:
00519       /* fields: item->u1.str     == value of case
00520                  item->u2.statements == pval list of statements under the case
00521       */
00522       traverse_pval_item_template(item->u2.statements,depth+1);
00523       break;
00524          
00525    case PV_DEFAULT:
00526       /* fields: 
00527                  item->u2.statements == pval list of statements under the case
00528       */
00529       traverse_pval_item_template(item->u2.statements,depth+1);
00530       break;
00531          
00532    case PV_CATCH:
00533       /* fields: item->u1.str     == name of extension to catch
00534                  item->u2.statements == pval list of statements in context body
00535       */
00536       traverse_pval_item_template(item->u2.statements,depth+1);
00537       break;
00538          
00539    case PV_SWITCHES:
00540       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
00541       */
00542       traverse_pval_item_template(item->u1.list,depth+1);
00543       break;
00544          
00545    case PV_ESWITCHES:
00546       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
00547       */
00548       traverse_pval_item_template(item->u1.list,depth+1);
00549       break;
00550          
00551    case PV_INCLUDES:
00552       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
00553                  item->u2.arglist  == pval list of 4 PV_WORD elements for time values
00554       */
00555       traverse_pval_item_template(item->u1.list,depth+1);
00556       traverse_pval_item_template(item->u2.arglist,depth+1);
00557       break;
00558          
00559    case PV_STATEMENTBLOCK:
00560       /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
00561       */
00562       traverse_pval_item_template(item->u1.list,depth+1);
00563       break;
00564          
00565    case PV_VARDEC:
00566       /* fields: item->u1.str     == variable name
00567                  item->u2.val     == variable value to assign
00568       */
00569       break;
00570          
00571    case PV_GOTO:
00572       /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
00573                  item->u1.list->u1.str  == where the data on a PV_WORD will always be.
00574       */
00575       
00576       if ( item->u1.list->next )
00577          ;
00578       if ( item->u1.list->next && item->u1.list->next->next )
00579          ;
00580       
00581       break;
00582          
00583    case PV_LABEL:
00584       /* fields: item->u1.str     == label name
00585       */
00586       break;
00587          
00588    case PV_FOR:
00589       /* fields: item->u1.for_init     == a string containing the initalizer
00590                  item->u2.for_test     == a string containing the loop test
00591                  item->u3.for_inc      == a string containing the loop increment
00592 
00593                item->u4.for_statements == a pval list of statements in the for ()
00594       */
00595       traverse_pval_item_template(item->u4.for_statements,depth+1);
00596       break;
00597          
00598    case PV_WHILE:
00599       /* fields: item->u1.str        == the while conditional, as supplied by user
00600 
00601                item->u2.statements == a pval list of statements in the while ()
00602       */
00603       traverse_pval_item_template(item->u2.statements,depth+1);
00604       break;
00605          
00606    case PV_BREAK:
00607       /* fields: none
00608       */
00609       break;
00610          
00611    case PV_RETURN:
00612       /* fields: none
00613       */
00614       break;
00615          
00616    case PV_CONTINUE:
00617       /* fields: none
00618       */
00619       break;
00620          
00621    case PV_IFTIME:
00622       /* fields: item->u1.list        == there are 4 linked PV_WORDs here.
00623 
00624                item->u2.statements == a pval list of statements in the if ()
00625                item->u3.else_statements == a pval list of statements in the else
00626                                     (could be zero)
00627       */
00628       traverse_pval_item_template(item->u2.statements,depth+1);
00629       if ( item->u3.else_statements ) {
00630          traverse_pval_item_template(item->u3.else_statements,depth+1);
00631       }
00632       break;
00633          
00634    case PV_RANDOM:
00635       /* fields: item->u1.str        == the random number expression, as supplied by user
00636 
00637                item->u2.statements == a pval list of statements in the if ()
00638                item->u3.else_statements == a pval list of statements in the else
00639                                     (could be zero)
00640       */
00641       traverse_pval_item_template(item->u2.statements,depth+1);
00642       if ( item->u3.else_statements ) {
00643          traverse_pval_item_template(item->u3.else_statements,depth+1);
00644       }
00645       break;
00646          
00647    case PV_IF:
00648       /* fields: item->u1.str        == the if conditional, as supplied by user
00649 
00650                item->u2.statements == a pval list of statements in the if ()
00651                item->u3.else_statements == a pval list of statements in the else
00652                                     (could be zero)
00653       */
00654       traverse_pval_item_template(item->u2.statements,depth+1);
00655       if ( item->u3.else_statements ) {
00656          traverse_pval_item_template(item->u3.else_statements,depth+1);
00657       }
00658       break;
00659          
00660    case PV_SWITCH:
00661       /* fields: item->u1.str        == the switch expression
00662 
00663                item->u2.statements == a pval list of statements in the switch, 
00664                                     (will be case statements, most likely!)
00665       */
00666       traverse_pval_item_template(item->u2.statements,depth+1);
00667       break;
00668          
00669    case PV_EXTENSION:
00670       /* fields: item->u1.str        == the extension name, label, whatever it's called
00671 
00672                item->u2.statements == a pval list of statements in the extension
00673                item->u3.hints      == a char * hint argument
00674                item->u4.regexten   == an int boolean. non-zero says that regexten was specified
00675       */
00676       traverse_pval_item_template(item->u2.statements,depth+1);
00677       break;
00678          
00679    case PV_IGNOREPAT:
00680       /* fields: item->u1.str        == the ignorepat data
00681       */
00682       break;
00683          
00684    case PV_GLOBALS:
00685       /* fields: item->u1.statements     == pval list of statements, usually vardecs
00686       */
00687       traverse_pval_item_template(item->u1.statements,depth+1);
00688       break;
00689    }
00690 }
00691 
00692 void traverse_pval_template(pval *item, int depth) /* depth comes in handy for a pretty print (indentation),
00693                                          but you may not need it */
00694 {
00695    pval *i;
00696    
00697    for (i=item; i; i=i->next) {
00698       traverse_pval_item_template(i, depth);
00699    }
00700 }
00701 
00702 
00703 /* SEMANTIC CHECKING FOR AEL:  ============================================================================= */
00704 
00705 /*   (not all that is syntactically legal is good! */
00706 
00707 
00708 
00709 static int extension_matches(pval *here, const char *exten, const char *pattern)
00710 {
00711    int err1;
00712    regex_t preg;
00713    
00714    /* simple case, they match exactly, the pattern and exten name */
00715    if( strcmp(pattern,exten) == 0 )
00716       return 1;
00717    
00718    if ( pattern[0] == '_' ) {
00719       char reg1[2000];
00720       const char *p;
00721       char *r = reg1;
00722       
00723       if ( strlen(pattern)*5 >= 2000 ) /* safety valve */ {
00724          ast_log(LOG_ERROR,"Error: The pattern %s is way too big. Pattern matching cancelled.\n",
00725                pattern);
00726          return 0;
00727       }
00728       /* form a regular expression from the pattern, and then match it against exten */
00729       *r++ = '^'; /* what if the extension is a pattern ?? */
00730       *r++ = '_'; /* what if the extension is a pattern ?? */
00731       *r++ = '?';
00732       for (p=pattern+1; *p; p++) {
00733          switch ( *p ) {
00734          case 'X':
00735             *r++ = '[';
00736             *r++ = '0';
00737             *r++ = '-';
00738             *r++ = '9';
00739             *r++ = 'X';
00740             *r++ = ']';
00741             break;
00742             
00743          case 'Z':
00744             *r++ = '[';
00745             *r++ = '1';
00746             *r++ = '-';
00747             *r++ = '9';
00748             *r++ = 'Z';
00749             *r++ = ']';
00750             break;
00751             
00752          case 'N':
00753             *r++ = '[';
00754             *r++ = '2';
00755             *r++ = '-';
00756             *r++ = '9';
00757             *r++ = 'N';
00758             *r++ = ']';
00759             break;
00760             
00761          case '[':
00762             while ( *p && *p != ']' ) {
00763                *r++ = *p++;
00764             }
00765             if ( *p != ']') {
00766                ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The extension pattern '%s' is missing a closing bracket \n",
00767                      here->filename, here->startline, here->endline, pattern);
00768             }
00769             break;
00770             
00771          case '.':
00772          case '!':
00773             *r++ = '.';
00774             *r++ = '*';
00775             break;
00776          case '*':
00777             *r++ = '\\';
00778             *r++ = '*';
00779             break;
00780          default:
00781             *r++ = *p;
00782             break;
00783             
00784          }
00785       }
00786       *r++ = '$'; /* what if the extension is a pattern ?? */
00787       *r++ = *p++; /* put in the closing null */
00788       err1 = regcomp(&preg, reg1, REG_NOSUB|REG_EXTENDED);
00789       if ( err1 ) {
00790          char errmess[500];
00791          regerror(err1,&preg,errmess,sizeof(errmess));
00792          regfree(&preg);
00793          ast_log(LOG_WARNING, "Regcomp of %s failed, error code %d\n",
00794                reg1, err1);
00795          return 0;
00796       }
00797       err1 = regexec(&preg, exten, 0, 0, 0);
00798       regfree(&preg);
00799       
00800       if ( err1 ) {
00801          /* ast_log(LOG_NOTICE,"*****************************[%d]Extension %s did not match %s(%s)\n",
00802             err1,exten, pattern, reg1); */
00803          return 0; /* no match */
00804       } else {
00805          /* ast_log(LOG_NOTICE,"*****************************Extension %s matched %s\n",
00806             exten, pattern); */
00807          return 1;
00808       }
00809       
00810       
00811    } else {
00812       if ( strcmp(exten,pattern) == 0 ) {
00813          return 1;
00814       } else
00815          return 0;
00816    }
00817 }
00818 
00819 
00820 static void check_expr2_input(pval *expr, char *str)
00821 {
00822    int spaces = strspn(str,"\t \n");
00823    if ( !strncmp(str+spaces,"$[",2) ) {
00824       ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The expression '%s' is redundantly wrapped in '$[ ]'. \n",
00825             expr->filename, expr->startline, expr->endline, str);
00826       warns++;
00827    }
00828 }
00829 
00830 static void check_includes(pval *includes)
00831 {
00832    struct pval *p4;
00833    for (p4=includes->u1.list; p4; p4=p4->next) {
00834       /* for each context pointed to, find it, then find a context/label that matches the
00835          target here! */
00836       char *incl_context = p4->u1.str;
00837       /* find a matching context name */
00838       struct pval *that_other_context = find_context(incl_context);
00839       if (!that_other_context && strcmp(incl_context, "parkedcalls") != 0) {
00840          ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The included context '%s' cannot be found.\n\
00841  (You may ignore this warning if '%s' exists in extensions.conf, or is created by another module. I cannot check for those.)\n",
00842                includes->filename, includes->startline, includes->endline, incl_context, incl_context);
00843          warns++;
00844       }
00845    }
00846 }
00847 
00848 
00849 static void check_timerange(pval *p)
00850 {
00851    char *times;
00852    char *e;
00853    int s1, s2;
00854    int e1, e2;
00855 
00856    times = ast_strdupa(p->u1.str);
00857 
00858    /* Star is all times */
00859    if (ast_strlen_zero(times) || !strcmp(times, "*")) {
00860       return;
00861    }
00862    /* Otherwise expect a range */
00863    e = strchr(times, '-');
00864    if (!e) {
00865       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",
00866             p->filename, p->startline, p->endline, times);
00867       warns++;
00868       return;
00869    }
00870    *e = '\0';
00871    e++;
00872    while (*e && !isdigit(*e)) 
00873       e++;
00874    if (!*e) {
00875       ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
00876             p->filename, p->startline, p->endline, p->u1.str);
00877       warns++;
00878    }
00879    if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
00880       ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
00881             p->filename, p->startline, p->endline, times);
00882       warns++;
00883    }
00884    if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
00885       ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
00886             p->filename, p->startline, p->endline, times);
00887       warns++;
00888    }
00889 
00890    s1 = s1 * 30 + s2/2;
00891    if ((s1 < 0) || (s1 >= 24*30)) {
00892       ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
00893             p->filename, p->startline, p->endline, times);
00894       warns++;
00895    }
00896    e1 = e1 * 30 + e2/2;
00897    if ((e1 < 0) || (e1 >= 24*30)) {
00898       ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
00899             p->filename, p->startline, p->endline, e);
00900       warns++;
00901    }
00902    return;
00903 }
00904 
00905 static char *days[] =
00906 {
00907    "sun",
00908    "mon",
00909    "tue",
00910    "wed",
00911    "thu",
00912    "fri",
00913    "sat",
00914 };
00915 
00916 /*! \brief  get_dow: Get day of week */
00917 static void check_dow(pval *DOW)
00918 {
00919    char *dow;
00920    char *c;
00921    /* The following line is coincidence, really! */
00922    int s, e;
00923    
00924    dow = ast_strdupa(DOW->u1.str);
00925 
00926    /* Check for all days */
00927    if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
00928       return;
00929    /* Get start and ending days */
00930    c = strchr(dow, '-');
00931    if (c) {
00932       *c = '\0';
00933       c++;
00934    } else
00935       c = NULL;
00936    /* Find the start */
00937    s = 0;
00938    while ((s < 7) && strcasecmp(dow, days[s])) s++;
00939    if (s >= 7) {
00940       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",
00941             DOW->filename, DOW->startline, DOW->endline, dow);
00942       warns++;
00943    }
00944    if (c) {
00945       e = 0;
00946       while ((e < 7) && strcasecmp(c, days[e])) e++;
00947       if (e >= 7) {
00948          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",
00949                DOW->filename, DOW->startline, DOW->endline, c);
00950          warns++;
00951       }
00952    } else
00953       e = s;
00954 }
00955 
00956 static void check_day(pval *DAY)
00957 {
00958    char *day;
00959    char *c;
00960    /* The following line is coincidence, really! */
00961    int s, e;
00962 
00963    day = ast_strdupa(DAY->u1.str);
00964 
00965    /* Check for all days */
00966    if (ast_strlen_zero(day) || !strcmp(day, "*")) {
00967       return;
00968    }
00969    /* Get start and ending days */
00970    c = strchr(day, '-');
00971    if (c) {
00972       *c = '\0';
00973       c++;
00974    }
00975    /* Find the start */
00976    if (sscanf(day, "%d", &s) != 1) {
00977       ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
00978             DAY->filename, DAY->startline, DAY->endline, day);
00979       warns++;
00980    }
00981    else if ((s < 1) || (s > 31)) {
00982       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",
00983             DAY->filename, DAY->startline, DAY->endline, day);
00984       warns++;
00985    }
00986    s--;
00987    if (c) {
00988       if (sscanf(c, "%d", &e) != 1) {
00989          ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
00990                DAY->filename, DAY->startline, DAY->endline, c);
00991          warns++;
00992       }
00993       else if ((e < 1) || (e > 31)) {
00994          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",
00995                DAY->filename, DAY->startline, DAY->endline, day);
00996          warns++;
00997       }
00998       e--;
00999    } else
01000       e = s;
01001 }
01002 
01003 static char *months[] =
01004 {
01005    "jan",
01006    "feb",
01007    "mar",
01008    "apr",
01009    "may",
01010    "jun",
01011    "jul",
01012    "aug",
01013    "sep",
01014    "oct",
01015    "nov",
01016    "dec",
01017 };
01018 
01019 static void check_month(pval *MON)
01020 {
01021    char *mon;
01022    char *c;
01023    /* The following line is coincidence, really! */
01024    int s, e;
01025 
01026    mon = ast_strdupa(MON->u1.str);
01027 
01028    /* Check for all days */
01029    if (ast_strlen_zero(mon) || !strcmp(mon, "*")) 
01030       return ;
01031    /* Get start and ending days */
01032    c = strchr(mon, '-');
01033    if (c) {
01034       *c = '\0';
01035       c++;
01036    }
01037    /* Find the start */
01038    s = 0;
01039    while ((s < 12) && strcasecmp(mon, months[s])) s++;
01040    if (s >= 12) {
01041       ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
01042             MON->filename, MON->startline, MON->endline, mon);
01043       warns++;
01044    }
01045    if (c) {
01046       e = 0;
01047       while ((e < 12) && strcasecmp(mon, months[e])) e++;
01048       if (e >= 12) {
01049          ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
01050                MON->filename, MON->startline, MON->endline, c);
01051          warns++;
01052       }
01053    } else
01054       e = s;
01055 }
01056 
01057 static int check_break(pval *item)
01058 {
01059    pval *p = item;
01060    
01061    while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
01062       /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
01063          no sense */
01064       if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN 
01065          || p->type == PV_WHILE || p->type == PV_FOR   ) {
01066          return 1;
01067       }
01068       p = p->dad;
01069    }
01070    ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'break' not in switch, for, or while statement!\n",
01071          item->filename, item->startline, item->endline);
01072    errs++;
01073    
01074    return 0;
01075 }
01076 
01077 static int check_continue(pval *item)
01078 {
01079    pval *p = item;
01080    
01081    while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
01082       /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
01083          no sense */
01084       if( p->type == PV_WHILE || p->type == PV_FOR   ) {
01085          return 1;
01086       }
01087       p = p->dad;
01088    }
01089    ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'continue' not in 'for' or 'while' statement!\n",
01090          item->filename, item->startline, item->endline);
01091    errs++;
01092    
01093    return 0;
01094 }
01095 
01096 
01097 /* general purpose goto finder */
01098 
01099 static void check_label(pval *item)
01100 {
01101    /* basically, ensure that a label is not repeated in a context. Period.
01102       The method:  well, for each label, find the first label in the context
01103       with the same name. If it's not the current label, then throw an error. */
01104    struct pval *curr;
01105    struct pval *x;
01106    
01107    /* printf("==== check_label:   ====\n"); */
01108    if( !current_extension )
01109       curr = current_context;
01110    else
01111       curr = current_extension;
01112    
01113    x = find_first_label_in_current_context((char *)item->u1.str, curr);
01114    /* printf("Hey, check_label found with item = %x, and x is %x, and currcont is %x, label name is %s\n", item,x, current_context, (char *)item->u1.str); */
01115    if( x && x != item )
01116    {
01117       ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Duplicate label %s! Previously defined at file %s, line %d.\n",
01118             item->filename, item->startline, item->endline, item->u1.str, x->filename, x->startline);
01119       errs++;
01120    }
01121    /* printf("<<<<< check_label:   ====\n"); */
01122 }
01123 
01124 static pval *get_goto_target(pval *item)
01125 {
01126    /* just one item-- the label should be in the current extension */
01127    pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
01128    pval *curr_cont;
01129    
01130    if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
01131       struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
01132          return x;
01133    }
01134 
01135    curr_cont = get_contxt(item);
01136 
01137    /* TWO items */
01138    if (item->u1.list->next && !item->u1.list->next->next) {
01139       if (!strstr((item->u1.list)->u1.str,"${") 
01140          && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
01141          struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
01142             return x;
01143       }
01144    }
01145    
01146    /* All 3 items! */
01147    if (item->u1.list->next && item->u1.list->next->next) {
01148       /* all three */
01149       pval *first = item->u1.list;
01150       pval *second = item->u1.list->next;
01151       pval *third = item->u1.list->next->next;
01152       
01153       if (!strstr((item->u1.list)->u1.str,"${") 
01154          && !strstr(item->u1.list->next->u1.str,"${")
01155          && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
01156          struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
01157          if (!x) {
01158 
01159             struct pval *p3;
01160             struct pval *that_context = find_context(item->u1.list->u1.str);
01161             
01162             /* the target of the goto could be in an included context!! Fancy that!! */
01163             /* look for includes in the current context */
01164             if (that_context) {
01165                for (p3=that_context->u2.statements; p3; p3=p3->next) {
01166                   if (p3->type == PV_INCLUDES) {
01167                      struct pval *p4;
01168                      for (p4=p3->u1.list; p4; p4=p4->next) {
01169                         /* for each context pointed to, find it, then find a context/label that matches the
01170                            target here! */
01171                         char *incl_context = p4->u1.str;
01172                         /* find a matching context name */
01173                         struct pval *that_other_context = find_context(incl_context);
01174                         if (that_other_context) {
01175                            struct pval *x3;
01176                            x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
01177                            if (x3) {
01178                               return x3;
01179                            }
01180                         }
01181                      }
01182                   }
01183                }
01184             }
01185          }
01186          return x;
01187       }
01188    }
01189    return 0;
01190 }
01191 
01192 static void check_goto(pval *item)
01193 {
01194    /* check for the target of the goto-- does it exist? */
01195    if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
01196       ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  empty label reference found!\n",
01197             item->filename, item->startline, item->endline);
01198       errs++;
01199    }
01200    
01201    /* just one item-- the label should be in the current extension */
01202    
01203    if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
01204       struct pval *z = get_extension_or_contxt(item);
01205       struct pval *x = 0;
01206       if (z)
01207          x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
01208       /* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
01209          (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
01210       if (!x) {
01211          ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s exists in the current extension!\n",
01212                item->filename, item->startline, item->endline, item->u1.list->u1.str);
01213          errs++;
01214       }
01215       else
01216          return;
01217    }
01218    
01219    /* TWO items */
01220    if (item->u1.list->next && !item->u1.list->next->next) {
01221       /* two items */
01222       /* printf("Calling find_label_in_current_context with args %s, %s\n",
01223          (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
01224       if (!strstr((item->u1.list)->u1.str,"${") 
01225          && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
01226          struct pval *z = get_contxt(item);
01227          struct pval *x = 0;
01228          
01229          if (z)
01230             x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
01231 
01232          if (!x) {
01233             ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s|%s exists in the current context, or any of its inclusions!\n",
01234                   item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
01235             errs++;
01236          }
01237          else
01238             return;
01239       }
01240    }
01241    
01242    /* All 3 items! */
01243    if (item->u1.list->next && item->u1.list->next->next) {
01244       /* all three */
01245       pval *first = item->u1.list;
01246       pval *second = item->u1.list->next;
01247       pval *third = item->u1.list->next->next;
01248       
01249       /* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
01250          (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
01251       if (!strstr((item->u1.list)->u1.str,"${") 
01252          && !strstr(item->u1.list->next->u1.str,"${")
01253          && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
01254          struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
01255          if (!x) {
01256             struct pval *p3;
01257             struct pval *found = 0;
01258             struct pval *that_context = find_context(item->u1.list->u1.str);
01259             
01260             /* the target of the goto could be in an included context!! Fancy that!! */
01261             /* look for includes in the current context */
01262             if (that_context) {
01263                for (p3=that_context->u2.statements; p3; p3=p3->next) {
01264                   if (p3->type == PV_INCLUDES) {
01265                      struct pval *p4;
01266                      for (p4=p3->u1.list; p4; p4=p4->next) {
01267                         /* for each context pointed to, find it, then find a context/label that matches the
01268                            target here! */
01269                         char *incl_context = p4->u1.str;
01270                         /* find a matching context name */
01271                         struct pval *that_other_context = find_context(incl_context);
01272                         if (that_other_context) {
01273                            struct pval *x3;
01274                            x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
01275                            if (x3) {
01276                               found = x3;
01277                               break;
01278                            }
01279                         }
01280                      }
01281                   }
01282                }
01283                if (!found) {
01284                   ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s|%s exists in the context %s or its inclusions!\n",
01285                         item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
01286                   errs++;
01287                }
01288             } else {
01289                /* here is where code would go to check for target existence in extensions.conf files */
01290                ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto:  no context %s could be found that matches the goto target!\n",
01291                      item->filename, item->startline, item->endline, item->u1.list->u1.str);
01292                warns++; /* this is just a warning, because this context could be in extensions.conf or somewhere */
01293             }
01294          }
01295       }
01296    }
01297 }
01298    
01299 
01300 static void find_pval_goto_item(pval *item, int lev)
01301 {
01302    struct pval *p4;
01303    
01304    if (lev>100) {
01305       ast_log(LOG_ERROR,"find_pval_goto in infinite loop! item_type: %d\n\n", item->type);
01306       return;
01307    }
01308    
01309    switch ( item->type ) {
01310    case PV_MACRO:
01311       /* fields: item->u1.str     == name of macro
01312                  item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
01313                item->u2.arglist->u1.str  == argument
01314                item->u2.arglist->next   == next arg
01315 
01316                item->u3.macro_statements == pval list of statements in macro body.
01317       */
01318          
01319       /* printf("Descending into macro %s at line %d\n", item->u1.str, item->startline); */
01320       find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
01321       
01322       break;
01323          
01324    case PV_CONTEXT:
01325       /* fields: item->u1.str     == name of context
01326                  item->u2.statements == pval list of statements in context body
01327                item->u3.abstract == int 1 if an abstract keyword were present
01328       */
01329       break;
01330 
01331    case PV_CASE:
01332       /* fields: item->u1.str     == value of case
01333                  item->u2.statements == pval list of statements under the case
01334       */
01335       /* printf("Descending into Case of %s\n", item->u1.str); */
01336       find_pval_gotos(item->u2.statements,lev+1);
01337       break;
01338          
01339    case PV_PATTERN:
01340       /* fields: item->u1.str     == value of case
01341                  item->u2.statements == pval list of statements under the case
01342       */
01343       /* printf("Descending into Pattern of %s\n", item->u1.str); */
01344       find_pval_gotos(item->u2.statements,lev+1);
01345       break;
01346          
01347    case PV_DEFAULT:
01348       /* fields: 
01349                  item->u2.statements == pval list of statements under the case
01350       */
01351       /* printf("Descending into default\n"); */
01352       find_pval_gotos(item->u2.statements,lev+1);
01353       break;
01354          
01355    case PV_CATCH:
01356       /* fields: item->u1.str     == name of extension to catch
01357                  item->u2.statements == pval list of statements in context body
01358       */
01359       /* printf("Descending into catch of %s\n", item->u1.str); */
01360       find_pval_gotos(item->u2.statements,lev+1);
01361       break;
01362          
01363    case PV_STATEMENTBLOCK:
01364       /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
01365       */
01366       /* printf("Descending into statement block\n"); */
01367       find_pval_gotos(item->u1.list,lev+1);
01368       break;
01369          
01370    case PV_GOTO:
01371       /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
01372                  item->u1.list->u1.str  == where the data on a PV_WORD will always be.
01373       */
01374       check_goto(item);  /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
01375       break;
01376          
01377    case PV_INCLUDES:
01378       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
01379       */
01380       for (p4=item->u1.list; p4; p4=p4->next) {
01381          /* for each context pointed to, find it, then find a context/label that matches the
01382             target here! */
01383          char *incl_context = p4->u1.str;
01384          /* find a matching context name */
01385          struct pval *that_context = find_context(incl_context);
01386          if (that_context && that_context->u2.statements) {
01387             /* printf("Descending into include of '%s' at line %d; that_context=%s, that_context type=%d\n", incl_context, item->startline, that_context->u1.str, that_context->type); */
01388             find_pval_gotos(that_context->u2.statements,lev+1); /* keep working up the includes */
01389          }
01390       }
01391       break;
01392       
01393    case PV_FOR:
01394       /* fields: item->u1.for_init     == a string containing the initalizer
01395                  item->u2.for_test     == a string containing the loop test
01396                  item->u3.for_inc      == a string containing the loop increment
01397 
01398                item->u4.for_statements == a pval list of statements in the for ()
01399       */
01400       /* printf("Descending into for at line %d\n", item->startline); */
01401       find_pval_gotos(item->u4.for_statements,lev+1);
01402       break;
01403          
01404    case PV_WHILE:
01405       /* fields: item->u1.str        == the while conditional, as supplied by user
01406 
01407                item->u2.statements == a pval list of statements in the while ()
01408       */
01409       /* printf("Descending into while at line %d\n", item->startline); */
01410       find_pval_gotos(item->u2.statements,lev+1);
01411       break;
01412          
01413    case PV_RANDOM:
01414       /* fields: item->u1.str        == the random number expression, as supplied by user
01415 
01416                item->u2.statements == a pval list of statements in the if ()
01417                item->u3.else_statements == a pval list of statements in the else
01418                                     (could be zero)
01419        fall thru to PV_IF */
01420       
01421    case PV_IFTIME:
01422       /* fields: item->u1.list        == the time values, 4 of them, as PV_WORD structs in a list
01423 
01424                item->u2.statements == a pval list of statements in the if ()
01425                item->u3.else_statements == a pval list of statements in the else
01426                                     (could be zero)
01427       fall thru to PV_IF*/
01428    case PV_IF:
01429       /* fields: item->u1.str        == the if conditional, as supplied by user
01430 
01431                item->u2.statements == a pval list of statements in the if ()
01432                item->u3.else_statements == a pval list of statements in the else
01433                                     (could be zero)
01434       */
01435       /* printf("Descending into random/iftime/if at line %d\n", item->startline); */
01436       find_pval_gotos(item->u2.statements,lev+1);
01437 
01438       if (item->u3.else_statements) {
01439          /* printf("Descending into random/iftime/if's ELSE at line %d\n", item->startline); */
01440          find_pval_gotos(item->u3.else_statements,lev+1);
01441       }
01442       break;
01443          
01444    case PV_SWITCH:
01445       /* fields: item->u1.str        == the switch expression
01446 
01447                item->u2.statements == a pval list of statements in the switch, 
01448                                     (will be case statements, most likely!)
01449       */
01450       /* printf("Descending into switch at line %d\n", item->startline); */
01451       find_pval_gotos(item->u3.else_statements,lev+1);
01452       break;
01453          
01454    case PV_EXTENSION:
01455       /* fields: item->u1.str        == the extension name, label, whatever it's called
01456 
01457                item->u2.statements == a pval list of statements in the extension
01458                item->u3.hints      == a char * hint argument
01459                item->u4.regexten   == an int boolean. non-zero says that regexten was specified
01460       */
01461 
01462       /* printf("Descending into extension %s at line %d\n", item->u1.str, item->startline); */
01463       find_pval_gotos(item->u2.statements,lev+1);
01464       break;
01465 
01466    default:
01467       break;
01468    }
01469 }
01470 
01471 static void find_pval_gotos(pval *item,int lev)
01472 {
01473    pval *i;
01474    
01475    for (i=item; i; i=i->next) {
01476       /* printf("About to call pval_goto_item, itemcount=%d, itemtype=%d\n", item_count, i->type); */
01477       find_pval_goto_item(i, lev);
01478    }
01479 }
01480 
01481 
01482 
01483 /* general purpose label finder */
01484 static struct pval *match_pval_item(pval *item)
01485 {
01486    pval *x;
01487    
01488    switch ( item->type ) {
01489    case PV_MACRO:
01490       /* fields: item->u1.str     == name of macro
01491                  item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
01492                item->u2.arglist->u1.str  == argument
01493                item->u2.arglist->next   == next arg
01494 
01495                item->u3.macro_statements == pval list of statements in macro body.
01496       */
01497       /* printf("    matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
01498       if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
01499          
01500          /* printf("MACRO: match context is: %s\n", match_context); */
01501          
01502          if (return_on_context_match && !strcmp(item->u1.str, match_context)) /* if we're just searching for a context, don't bother descending into them */ {
01503             /* printf("Returning on matching macro %s\n", match_context); */
01504             return item;
01505          }
01506          
01507          
01508          if (!return_on_context_match) {
01509             /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
01510             if ((x=match_pval(item->u3.macro_statements)))  {
01511                /* printf("Responded with pval match %x\n", x); */
01512                return x;
01513             }
01514          }
01515       } else {
01516          /* printf("Skipping context/macro %s\n", item->u1.str); */
01517       }
01518       
01519       break;
01520          
01521    case PV_CONTEXT:
01522       /* fields: item->u1.str     == name of context
01523                  item->u2.statements == pval list of statements in context body
01524                item->u3.abstract == int 1 if an abstract keyword were present
01525       */
01526       /* printf("    matching in CONTEXT\n"); */
01527       if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
01528          if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
01529             /* printf("Returning on matching context %s\n", match_context); */
01530             /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
01531             return item;
01532          }
01533          
01534          if (!return_on_context_match ) {
01535             /* printf("Descending into matching context %s\n", match_context); */
01536             if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
01537                /* printf("CONTEXT: Responded with pval match %x\n", x); */
01538                return x;
01539             }
01540          }
01541       } else {
01542          /* printf("Skipping context/macro %s\n", item->u1.str); */
01543       }
01544       break;
01545 
01546    case PV_CASE:
01547       /* fields: item->u1.str     == value of case
01548                  item->u2.statements == pval list of statements under the case
01549       */
01550       /* printf("    matching in CASE\n"); */
01551       if ((x=match_pval(item->u2.statements))) {
01552          /* printf("CASE: Responded with pval match %x\n", x); */
01553          return x;
01554       }
01555       break;
01556          
01557    case PV_PATTERN:
01558       /* fields: item->u1.str     == value of case
01559                  item->u2.statements == pval list of statements under the case
01560       */
01561       /* printf("    matching in PATTERN\n"); */
01562       if ((x=match_pval(item->u2.statements))) {
01563          /* printf("PATTERN: Responded with pval match %x\n", x); */
01564          return x;
01565       }
01566       break;
01567          
01568    case PV_DEFAULT:
01569       /* fields: 
01570                  item->u2.statements == pval list of statements under the case
01571       */
01572       /* printf("    matching in DEFAULT\n"); */
01573       if ((x=match_pval(item->u2.statements))) {
01574          /* printf("DEFAULT: Responded with pval match %x\n", x); */
01575          return x;
01576       }
01577       break;
01578          
01579    case PV_CATCH:
01580       /* fields: item->u1.str     == name of extension to catch
01581                  item->u2.statements == pval list of statements in context body
01582       */
01583       /* printf("    matching in CATCH\n"); */
01584       if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
01585          /* printf("Descending into matching catch %s => %s\n", match_exten, item->u1.str); */
01586          if (strcmp(match_label,"1") == 0) {
01587             if (item->u2.statements) {
01588                struct pval *p5 = item->u2.statements;
01589                while (p5 && p5->type == PV_LABEL)  /* find the first non-label statement in this context. If it exists, there's a "1" */
01590                   p5 = p5->next;
01591                if (p5)
01592                   return p5;
01593                else
01594                   return 0;
01595             }
01596             else
01597                return 0;
01598          }
01599 
01600          if ((x=match_pval(item->u2.statements))) {
01601             /* printf("CATCH: Responded with pval match %x\n", (unsigned int)x); */
01602             return x;
01603          }
01604       } else {
01605          /* printf("Skipping catch %s\n", item->u1.str); */
01606       }
01607       break;
01608          
01609    case PV_STATEMENTBLOCK:
01610       /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
01611       */
01612       /* printf("    matching in STATEMENTBLOCK\n"); */
01613       if ((x=match_pval(item->u1.list))) {
01614          /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
01615          return x;
01616       }
01617       break;
01618          
01619    case PV_LABEL:
01620       /* fields: item->u1.str     == label name
01621       */
01622       /* printf("PV_LABEL %s (cont=%s, exten=%s\n", 
01623          item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
01624       
01625       if (count_labels) {
01626          if (!strcmp(match_label, item->u1.str)) {
01627             label_count++;
01628             last_matched_label = item;
01629          }
01630          
01631       } else {
01632          if (!strcmp(match_label, item->u1.str)) {
01633             /* printf("LABEL: Responded with pval match %x\n", x); */
01634             return item;
01635          }
01636       }
01637       break;
01638          
01639    case PV_FOR:
01640       /* fields: item->u1.for_init     == a string containing the initalizer
01641                  item->u2.for_test     == a string containing the loop test
01642                  item->u3.for_inc      == a string containing the loop increment
01643 
01644                item->u4.for_statements == a pval list of statements in the for ()
01645       */
01646       /* printf("    matching in FOR\n"); */
01647       if ((x=match_pval(item->u4.for_statements))) {
01648          /* printf("FOR: Responded with pval match %x\n", x);*/
01649          return x;
01650       }
01651       break;
01652          
01653    case PV_WHILE:
01654       /* fields: item->u1.str        == the while conditional, as supplied by user
01655 
01656                item->u2.statements == a pval list of statements in the while ()
01657       */
01658       /* printf("    matching in WHILE\n"); */
01659       if ((x=match_pval(item->u2.statements))) {
01660          /* printf("WHILE: Responded with pval match %x\n", x); */
01661          return x;
01662       }
01663       break;
01664          
01665    case PV_RANDOM:
01666       /* fields: item->u1.str        == the random number expression, as supplied by user
01667 
01668                item->u2.statements == a pval list of statements in the if ()
01669                item->u3.else_statements == a pval list of statements in the else
01670                                     (could be zero)
01671        fall thru to PV_IF */
01672       
01673    case PV_IFTIME:
01674       /* fields: item->u1.list        == the time values, 4 of them, as PV_WORD structs in a list
01675 
01676                item->u2.statements == a pval list of statements in the if ()
01677                item->u3.else_statements == a pval list of statements in the else
01678                                     (could be zero)
01679       fall thru to PV_IF*/
01680    case PV_IF:
01681       /* fields: item->u1.str        == the if conditional, as supplied by user
01682 
01683                item->u2.statements == a pval list of statements in the if ()
01684                item->u3.else_statements == a pval list of statements in the else
01685                                     (could be zero)
01686       */
01687       /* printf("    matching in IF/IFTIME/RANDOM\n"); */
01688       if ((x=match_pval(item->u2.statements))) {
01689          return x;
01690       }
01691       if (item->u3.else_statements) {
01692          if ((x=match_pval(item->u3.else_statements))) {
01693             /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
01694             return x;
01695          }
01696       }
01697       break;
01698          
01699    case PV_SWITCH:
01700       /* fields: item->u1.str        == the switch expression
01701 
01702                item->u2.statements == a pval list of statements in the switch, 
01703                                     (will be case statements, most likely!)
01704       */
01705       /* printf("    matching in SWITCH\n"); */
01706       if ((x=match_pval(item->u2.statements))) {
01707          /* printf("SWITCH: Responded with pval match %x\n", x); */
01708          return x;
01709       }
01710       break;
01711          
01712    case PV_EXTENSION:
01713       /* fields: item->u1.str        == the extension name, label, whatever it's called
01714 
01715                item->u2.statements == a pval list of statements in the extension
01716                item->u3.hints      == a char * hint argument
01717                item->u4.regexten   == an int boolean. non-zero says that regexten was specified
01718       */
01719       /* printf("    matching in EXTENSION\n"); */
01720       if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
01721          /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
01722          if (strcmp(match_label,"1") == 0) {
01723             if (item->u2.statements) {
01724                struct pval *p5 = item->u2.statements;
01725                while (p5 && p5->type == PV_LABEL)  /* find the first non-label statement in this context. If it exists, there's a "1" */
01726                   p5 = p5->next;
01727                if (p5)
01728                   return p5;
01729                else
01730                   return 0;
01731             }
01732             else
01733                return 0;
01734          }
01735 
01736          if ((x=match_pval(item->u2.statements))) {
01737             /* printf("EXTENSION: Responded with pval match %x\n", x); */
01738             return x;
01739          }
01740       } else {
01741          /* printf("Skipping exten %s\n", item->u1.str); */
01742       }
01743       break;
01744    default:
01745       /* printf("    matching in default = %d\n", item->type); */
01746       break;
01747    }
01748    return 0;
01749 }
01750 
01751 struct pval *match_pval(pval *item)
01752 {
01753    pval *i;
01754 
01755    for (i=item; i; i=i->next) {
01756       pval *x;
01757       /* printf("   -- match pval: item %d\n", i->type); */
01758       
01759       if ((x = match_pval_item(i))) {
01760          /* printf("match_pval: returning x=%x\n", (int)x); */
01761          return x; /* cut the search short */
01762       }
01763    }
01764    return 0;
01765 }
01766 
01767 #if 0
01768 int count_labels_in_current_context(char *label)
01769 {
01770    label_count = 0;
01771    count_labels = 1;
01772    return_on_context_match = 0;
01773    match_pval(current_context->u2.statements);
01774    
01775    return label_count;
01776 }
01777 #endif
01778 
01779 struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
01780 {
01781    /* printf("  --- Got args %s, %s\n", exten, label); */
01782    struct pval *ret;
01783    struct pval *p3;
01784    struct pval *startpt = ((curr_cont->type==PV_MACRO)?curr_cont->u3.macro_statements: curr_cont->u2.statements);
01785    
01786    count_labels = 0;
01787    return_on_context_match = 0;
01788    match_context = "*";
01789    match_exten = "*";
01790    match_label = label;
01791    
01792    ret =  match_pval(curr_cont);
01793    if (ret)
01794       return ret;
01795                
01796    /* the target of the goto could be in an included context!! Fancy that!! */
01797    /* look for includes in the current context */
01798    for (p3=startpt; p3; p3=p3->next) {
01799       if (p3->type == PV_INCLUDES) {
01800          struct pval *p4;
01801          for (p4=p3->u1.list; p4; p4=p4->next) {
01802             /* for each context pointed to, find it, then find a context/label that matches the
01803                target here! */
01804             char *incl_context = p4->u1.str;
01805             /* find a matching context name */
01806             struct pval *that_context = find_context(incl_context);
01807             if (that_context) {
01808                struct pval *x3;
01809                x3 = find_first_label_in_current_context(label, that_context);
01810                if (x3) {
01811                   return x3;
01812                }
01813             }
01814          }
01815       }
01816    }
01817    return 0;
01818 }
01819 
01820 struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
01821 {
01822    /* printf("  --- Got args %s, %s\n", exten, label); */
01823    struct pval *ret;
01824    struct pval *p3;
01825    struct pval *startpt;
01826    
01827    count_labels = 0;
01828    return_on_context_match = 0;
01829    match_context = "*";
01830    match_exten = exten;
01831    match_label = label;
01832    if (curr_cont->type == PV_MACRO)
01833       startpt = curr_cont->u3.macro_statements;
01834    else
01835       startpt = curr_cont->u2.statements;
01836 
01837    ret =  match_pval(startpt);
01838    if (ret)
01839       return ret;
01840                
01841    /* the target of the goto could be in an included context!! Fancy that!! */
01842    /* look for includes in the current context */
01843    for (p3=startpt; p3; p3=p3->next) {
01844       if (p3->type == PV_INCLUDES) {
01845          struct pval *p4;
01846          for (p4=p3->u1.list; p4; p4=p4->next) {
01847             /* for each context pointed to, find it, then find a context/label that matches the
01848                target here! */
01849             char *incl_context = p4->u1.str;
01850             /* find a matching context name */
01851             struct pval *that_context = find_context(incl_context);
01852             if (that_context) {
01853                struct pval *x3;
01854                x3 = find_label_in_current_context(exten, label, that_context);
01855                if (x3) {
01856                   return x3;
01857                }
01858             }
01859          }
01860       }
01861    }
01862    return 0;
01863 }
01864 
01865 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
01866 {
01867    /* printf("  --- Got args %s\n", label); */
01868    count_labels = 0;
01869    return_on_context_match = 0;
01870    match_context = "*";
01871    match_exten = "*";
01872    match_label = label;
01873    return match_pval(curr_ext);
01874 }
01875 
01876 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
01877 {
01878    /* printf("  --- Got args %s, %s, %s\n", context, exten, label); */
01879    count_labels = 0;
01880    return_on_context_match = 0;
01881 
01882    match_context = context;
01883    match_exten = exten;
01884    match_label = label;
01885    
01886    return match_pval(current_db);
01887 }
01888 
01889 
01890 struct pval *find_macro(char *name)
01891 {
01892    return_on_context_match = 1;
01893    count_labels = 0;
01894    match_context = name;
01895    match_exten = "*";  /* don't really need to set these, shouldn't be reached */
01896    match_label = "*";
01897    return match_pval(current_db);
01898 }
01899 
01900 struct pval *find_context(char *name)
01901 {
01902    return_on_context_match = 1;
01903    count_labels = 0;
01904    match_context = name;
01905    match_exten = "*";  /* don't really need to set these, shouldn't be reached */
01906    match_label = "*";
01907    return match_pval(current_db);
01908 }
01909 
01910 int is_float(char *arg )
01911 {
01912    char *s;
01913    for (s=arg; *s; s++) {
01914       if (*s != '.' && (*s < '0' || *s > '9'))
01915          return 0;
01916    }
01917    return 1;
01918 }
01919 int is_int(char *arg )
01920 {
01921    char *s;
01922    for (s=arg; *s; s++) {
01923       if (*s < '0' || *s > '9')
01924          return 0;
01925    }
01926    return 1;
01927 }
01928 int is_empty(char *arg)
01929 {
01930    if (!arg)
01931       return 1;
01932    if (*arg == 0)
01933       return 1;
01934    while (*arg) {
01935       if (*arg != ' ' && *arg != '\t')
01936          return 0;
01937       arg++;
01938    }
01939    return 1;
01940 }
01941 
01942 #ifdef AAL_ARGCHECK
01943 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
01944 {
01945    struct argchoice *ac;
01946    char *opcop,*q,*p;
01947    
01948    switch (should->dtype) {
01949    case ARGD_OPTIONSET:
01950       if ( strstr(is->u1.str,"${") )
01951          return 0;  /* no checking anything if there's a var reference in there! */
01952          
01953       opcop = ast_strdupa(is->u1.str);
01954 
01955       for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
01956          if ( *q == '(' ) {
01957             p = q+1;
01958             while (*p && *p != ')' )
01959                *p++ = '+';
01960             q = p+1;
01961          }
01962       }
01963       
01964       for (ac=app->opts; ac; ac=ac->next) {
01965          if (strlen(ac->name)>1  && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
01966             return 0;
01967       }
01968       for (ac=app->opts; ac; ac=ac->next) {
01969          if (strlen(ac->name)==1  ||  strchr(ac->name,'(')) {
01970             char *p = strchr(opcop,ac->name[0]);  /* wipe out all matched options in the user-supplied string */
01971             
01972             if (p && *p == 'j') {
01973                ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
01974                      is->filename, is->startline, is->endline, app->name);
01975                errs++;
01976             }
01977             
01978             if (p) {
01979                *p = '+';
01980                if (ac->name[1] == '(') {
01981                   if (*(p+1) != '(') {
01982                      ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The %c option in the %s application call should have an (argument), but doesn't!\n",
01983                            is->filename, is->startline, is->endline, ac->name[0], app->name);
01984                      warns++;
01985                   }
01986                }
01987             }
01988          }
01989       }
01990       for (q=opcop; *q; q++) {
01991          if ( *q != '+' && *q != '(' && *q != ')') {
01992             ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The %c option in the %s application call is not available as an option!\n",
01993                   is->filename, is->startline, is->endline, *q, app->name);
01994             warns++;
01995          }
01996       }
01997       return 1;
01998       break;
01999    default:
02000       return 0;
02001    }
02002    
02003 }
02004 
02005 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
02006 {
02007    struct argchoice *ac;
02008    char *opcop;
02009    
02010    switch (should->dtype) {
02011    case ARGD_STRING:
02012       if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
02013          return 0;
02014       if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
02015          return 1;
02016       break;
02017       
02018    case ARGD_INT:
02019       if (is_int(is->u1.str))
02020          return 1;
02021       else
02022          return 0;
02023       break;
02024       
02025    case ARGD_FLOAT:
02026       if (is_float(is->u1.str))
02027          return 1;
02028       else
02029          return 0;
02030       break;
02031       
02032    case ARGD_ENUM:
02033       if( !is->u1.str || strlen(is->u1.str) == 0 )
02034          return 1; /* a null arg in the call will match an enum, I guess! */
02035       for (ac=should->choices; ac; ac=ac->next) {
02036          if (strcmp(ac->name,is->u1.str) == 0)
02037             return 1;
02038       }
02039       return 0;
02040       break;
02041       
02042    case ARGD_OPTIONSET:
02043       opcop = ast_strdupa(is->u1.str);
02044       
02045       for (ac=app->opts; ac; ac=ac->next) {
02046          if (strlen(ac->name)>1  && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
02047             return 1;
02048       }
02049       for (ac=app->opts; ac; ac=ac->next) {
02050          if (strlen(ac->name)==1  ||  strchr(ac->name,'(')) {
02051             char *p = strchr(opcop,ac->name[0]);  /* wipe out all matched options in the user-supplied string */
02052             
02053             if (p) {
02054                *p = '+';
02055                if (ac->name[1] == '(') {
02056                   if (*(p+1) == '(') {
02057                      char *q = p+1;
02058                      while (*q && *q != ')') {
02059                         *q++ = '+';
02060                      }
02061                      *q = '+';
02062                   }
02063                }
02064             }
02065          }
02066       }
02067       return 1;
02068       break;
02069    case ARGD_VARARG:
02070       return 1; /* matches anything */
02071       break;
02072    }
02073    return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
02074 }
02075 #endif
02076 
02077 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
02078 {
02079 #ifdef AAL_ARGCHECK
02080    struct argdesc *ad = app->args;
02081    pval *pa;
02082    int z;
02083    
02084    for (pa = arglist; pa; pa=pa->next) {
02085       if (!ad) {
02086          ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
02087                arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
02088          warns++;
02089          return 1;
02090       } else {
02091          /* find the first entry in the ad list that will match */
02092          do {
02093             if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
02094                break;
02095             
02096             z= option_matches( ad, pa, app);
02097             if (!z) {
02098                if ( !arglist )
02099                   arglist=appcall;
02100                
02101                if (ad->type == ARGD_REQUIRED) {
02102                   ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
02103                         arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
02104                   warns++;
02105                   return 1;
02106                }
02107             } else if (z && ad->dtype == ARGD_OPTIONSET) {
02108                option_matches_j( ad, pa, app);
02109             }
02110             ad = ad->next;
02111          } while (ad && !z);
02112       }
02113    }
02114    /* any app nodes left, that are not optional? */
02115    for ( ; ad; ad=ad->next) {
02116       if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
02117          if ( !arglist ) 
02118             arglist=appcall;
02119          ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
02120                arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
02121          warns++;
02122          return 1;
02123       }
02124    }
02125    return 0;
02126 #else
02127    return 0;
02128 #endif
02129 }
02130 
02131 void check_switch_expr(pval *item, struct argapp *apps)
02132 {
02133 #ifdef AAL_ARGCHECK
02134    /* get and clean the variable name */
02135    char *buff1, *p;
02136    struct argapp *a,*a2;
02137    struct appsetvar *v,*v2;
02138    struct argchoice *c;
02139    pval *t;
02140    
02141    p = item->u1.str;
02142    while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
02143       p++;
02144    
02145    buff1 = ast_strdupa(p);
02146 
02147    while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
02148       buff1[strlen(buff1)-1] = 0;
02149    /* buff1 now contains the variable name */
02150    v = 0;
02151    for (a=apps; a; a=a->next) {
02152       for (v=a->setvars;v;v=v->next) {
02153          if (strcmp(v->name,buff1) == 0) {
02154             break;
02155          }
02156       }
02157       if ( v )
02158          break;
02159    }
02160    if (v && v->vals) {
02161       /* we have a match, to a variable that has a set of determined values */
02162       int def= 0;
02163       int pat = 0;
02164       int f1 = 0;
02165       
02166       /* first of all, does this switch have a default case ? */
02167       for (t=item->u2.statements; t; t=t->next) {
02168          if (t->type == PV_DEFAULT) {
02169             def =1;
02170             break;
02171          }
02172          if (t->type == PV_PATTERN) {
02173             pat++;
02174          }
02175       }
02176       if (def || pat) /* nothing to check. All cases accounted for! */
02177          return;
02178       for (c=v->vals; c; c=c->next) {
02179          f1 = 0;
02180          for (t=item->u2.statements; t; t=t->next) {
02181             if (t->type == PV_CASE || t->type == PV_PATTERN) {
02182                if (!strcmp(t->u1.str,c->name)) {
02183                   f1 = 1;
02184                   break;
02185                }
02186             }
02187          }
02188          if (!f1) {
02189             ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
02190                   item->filename, item->startline, item->endline, item->u1.str, c->name);
02191             warns++;
02192          }
02193       }
02194       /* next, is there an app call in the current exten, that would set this var? */
02195       f1 = 0;
02196       t = current_extension->u2.statements;
02197       if ( t && t->type == PV_STATEMENTBLOCK )
02198          t = t->u1.statements;
02199       for (; t && t != item; t=t->next) {
02200          if (t->type == PV_APPLICATION_CALL) {
02201             /* find the application that matches the u1.str */
02202             for (a2=apps; a2; a2=a2->next) {
02203                if (strcasecmp(a2->name, t->u1.str)==0) {
02204                   for (v2=a2->setvars; v2; v2=v2->next) {
02205                      if (strcmp(v2->name, buff1) == 0) {
02206                         /* found an app that sets the var */
02207                         f1 = 1;
02208                         break;
02209                      }
02210                   }
02211                }
02212                if (f1)
02213                   break;
02214             }
02215          }
02216          if (f1)
02217             break;
02218       }
02219             
02220       /* see if it sets the var */
02221       if (!f1) {
02222          ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: Couldn't find an application call in this extension that sets the  expression (%s) value!\n",
02223                item->filename, item->startline, item->endline, item->u1.str);
02224          warns++;
02225       }
02226    }
02227 #else
02228    pval *t,*tl=0,*p2;
02229    int def= 0;
02230    
02231    /* first of all, does this switch have a default case ? */
02232    for (t=item->u2.statements; t; t=t->next) {
02233       if (t->type == PV_DEFAULT) {
02234          def =1;
02235          break;
02236       }
02237       tl = t;
02238    }
02239    if (def) /* nothing to check. All cases accounted for! */
02240       return;
02241    /* if no default, warn and insert a default case at the end */
02242    p2 = tl->next = calloc(1, sizeof(struct pval));
02243    
02244    p2->type = PV_DEFAULT;
02245    p2->startline = tl->startline;
02246    p2->endline = tl->endline;
02247    p2->startcol = tl->startcol;
02248    p2->endcol = tl->endcol;
02249    p2->filename = strdup(tl->filename);
02250    ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
02251          p2->filename, p2->startline, p2->endline);
02252    warns++;
02253 
02254 #endif
02255 }
02256 
02257 static void check_context_names(void)
02258 {
02259    pval *i,*j;
02260    for (i=current_db; i; i=i->next) {
02261       if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
02262          for (j=i->next; j; j=j->next) {
02263             if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
02264                if ( !strcmp(i->u1.str, j->u1.str) && !(i->u3.abstract&2) && !(j->u3.abstract&2) )
02265                {
02266                   ast_log(LOG_ERROR,"Error: file %s, line %d-%d: The context name (%s) is also declared in file %s, line %d-%d! (and neither is marked 'extend')\n",
02267                         i->filename, i->startline, i->endline, i->u1.str,  j->filename, j->startline, j->endline);
02268                   errs++;
02269                }
02270             }
02271          }
02272       }
02273    }
02274 }
02275 
02276 static void check_abstract_reference(pval *abstract_context)
02277 {
02278    pval *i,*j;
02279    /* find some context includes that reference this context */
02280    
02281 
02282    /* otherwise, print out a warning */
02283    for (i=current_db; i; i=i->next) {
02284       if (i->type == PV_CONTEXT) {
02285          for (j=i->u2. statements; j; j=j->next) {
02286             if ( j->type == PV_INCLUDES ) {
02287                struct pval *p4;
02288                for (p4=j->u1.list; p4; p4=p4->next) {
02289                   /* for each context pointed to, find it, then find a context/label that matches the
02290                      target here! */
02291                   if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
02292                      return; /* found a match! */
02293                }
02294             }
02295          }
02296       }
02297    }
02298    ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: Couldn't find a reference to this abstract context (%s) in any other context!\n",
02299          abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
02300    warns++;
02301 }
02302 
02303 
02304 void check_pval_item(pval *item, struct argapp *apps, int in_globals)
02305 {
02306    pval *lp;
02307 #ifdef AAL_ARGCHECK
02308    struct argapp *app, *found;
02309 #endif
02310    struct pval *macro_def;
02311    struct pval *app_def;
02312    
02313    char errmsg[4096];
02314    char *strp;
02315    
02316    switch (item->type) {
02317    case PV_WORD:
02318       /* fields: item->u1.str == string associated with this (word).
02319                  item->u2.arglist  == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
02320       break;
02321       
02322    case PV_MACRO:
02323       /* fields: item->u1.str     == name of macro
02324                  item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
02325                item->u2.arglist->u1.str  == argument
02326                item->u2.arglist->next   == next arg
02327 
02328                item->u3.macro_statements == pval list of statements in macro body.
02329       */
02330       in_abstract_context = 0;
02331       current_context = item;
02332       current_extension = 0;
02333       for (lp=item->u2.arglist; lp; lp=lp->next) {
02334       
02335       }
02336       check_pval(item->u3.macro_statements, apps,in_globals);
02337       break;
02338          
02339    case PV_CONTEXT:
02340       /* fields: item->u1.str     == name of context
02341                  item->u2.statements == pval list of statements in context body
02342                item->u3.abstract == int 1 if an abstract keyword were present
02343       */
02344       current_context = item;
02345       current_extension = 0;
02346       if ( item->u3.abstract ) {
02347          in_abstract_context = 1;
02348          check_abstract_reference(item);
02349       } else
02350          in_abstract_context = 0;
02351       check_pval(item->u2.statements, apps,in_globals);
02352       break;
02353          
02354    case PV_MACRO_CALL:
02355       /* fields: item->u1.str     == name of macro to call
02356                  item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
02357                item->u2.arglist->u1.str  == argument
02358                item->u2.arglist->next   == next arg
02359       */
02360       macro_def = find_macro(item->u1.str);
02361       if (!macro_def) {
02362          /* here is a good place to check to see if the definition is in extensions.conf! */
02363          ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s ! Hopefully it is present in extensions.conf! \n",
02364                item->filename, item->startline, item->endline, item->u1.str);
02365          warns++;
02366       } else if (macro_def->type != PV_MACRO) {
02367          ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
02368                item->filename, item->startline, item->endline, item->u1.str);
02369          errs++;
02370       } else {
02371          /* macro_def is a MACRO, so do the args match in number? */
02372          int hereargs = 0;
02373          int thereargs = 0;
02374          
02375          for (lp=item->u2.arglist; lp; lp=lp->next) {
02376             hereargs++;
02377          }
02378          for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
02379             thereargs++;
02380          }
02381          if (hereargs != thereargs ) {
02382             ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The macro call to %s has %d arguments, but the macro definition has %d arguments\n",
02383                   item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
02384             errs++;
02385          }
02386       }
02387       break;
02388          
02389    case PV_APPLICATION_CALL:
02390       /* fields: item->u1.str     == name of application to call
02391                  item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
02392                item->u2.arglist->u1.str  == argument
02393                item->u2.arglist->next   == next arg
02394       */
02395       /* Need to check to see if the application is available! */
02396       app_def = find_context(item->u1.str);
02397       if (app_def && app_def->type == PV_MACRO) {
02398          ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
02399                item->filename, item->startline, item->endline, item->u1.str);
02400          errs++;
02401       }
02402       if (strcasecmp(item->u1.str,"GotoIf") == 0
02403          || strcasecmp(item->u1.str,"GotoIfTime") == 0
02404          || strcasecmp(item->u1.str,"while") == 0
02405          || strcasecmp(item->u1.str,"endwhile") == 0
02406          || strcasecmp(item->u1.str,"random") == 0
02407          || strcasecmp(item->u1.str,"execIf") == 0 ) {
02408          ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s needs to be re-written using AEL if, while, goto, etc. keywords instead!\n",
02409                item->filename, item->startline, item->endline, item->u1.str);
02410          warns++;
02411       }
02412 #ifdef AAL_ARGCHECK
02413       found = 0;
02414       for (app=apps; app; app=app->next) {
02415          if (strcasecmp(app->name, item->u1.str) == 0) {
02416             found =app;
02417             break;
02418          }
02419       }
02420       if (!found) {
02421          ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
02422                item->filename, item->startline, item->endline, item->u1.str);
02423          warns++;
02424       } else
02425          check_app_args(item, item->u2.arglist, app);
02426 #endif
02427       break;
02428       
02429    case PV_CASE:
02430       /* fields: item->u1.str     == value of case
02431                  item->u2.statements == pval list of statements under the case
02432       */
02433       /* Make sure sequence of statements under case is terminated with  goto, return, or break */
02434       /* find the last statement */
02435       check_pval(item->u2.statements, apps,in_globals);
02436       break;
02437          
02438    case PV_PATTERN:
02439       /* fields: item->u1.str     == value of case
02440                  item->u2.statements == pval list of statements under the case
02441       */
02442       /* Make sure sequence of statements under case is terminated with  goto, return, or break */
02443       /* find the last statement */
02444       
02445       check_pval(item->u2.statements, apps,in_globals);
02446       break;
02447          
02448    case PV_DEFAULT:
02449       /* fields: 
02450                  item->u2.statements == pval list of statements under the case
02451       */
02452 
02453       check_pval(item->u2.statements, apps,in_globals);
02454       break;
02455          
02456    case PV_CATCH:
02457       /* fields: item->u1.str     == name of extension to catch
02458                  item->u2.statements == pval list of statements in context body
02459       */
02460       check_pval(item->u2.statements, apps,in_globals);
02461       break;
02462          
02463    case PV_SWITCHES:
02464       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
02465       */
02466       check_pval(item->u1.list, apps,in_globals);
02467       break;
02468          
02469    case PV_ESWITCHES:
02470       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
02471       */
02472       check_pval(item->u1.list, apps,in_globals);
02473       break;
02474          
02475    case PV_INCLUDES:
02476       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
02477       */
02478       check_pval(item->u1.list, apps,in_globals);
02479       check_includes(item);
02480       for (lp=item->u1.list; lp; lp=lp->next){
02481          char *incl_context = lp->u1.str;
02482          struct pval *that_context = find_context(incl_context);
02483 
02484          if ( lp->u2.arglist ) {
02485             check_timerange(lp->u2.arglist);
02486             check_dow(lp->u2.arglist->next);
02487             check_day(lp->u2.arglist->next->next);
02488             check_month(lp->u2.arglist->next->next->next);
02489          }
02490          
02491          if (that_context) {
02492             find_pval_gotos(that_context->u2.statements,0);
02493             
02494          }
02495       }
02496       break;
02497          
02498    case PV_STATEMENTBLOCK:
02499       /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
02500       */
02501       check_pval(item->u1.list, apps,in_globals);
02502       break;
02503          
02504    case PV_VARDEC:
02505       /* fields: item->u1.str     == variable name
02506                  item->u2.val     == variable value to assign
02507       */
02508       /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
02509       if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
02510          snprintf(errmsg,sizeof(errmsg), "file %s, line %d, columns %d-%d, variable declaration expr '%s':", config, item->startline, item->startcol, item->endcol, item->u2.val);
02511          ast_expr_register_extra_error_info(errmsg);
02512          ast_expr(item->u2.val, expr_output, sizeof(expr_output));
02513          ast_expr_clear_extra_error_info();
02514          if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
02515             ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
02516                   item->filename, item->startline, item->endline, item->u2.val);
02517             warns++;
02518          }
02519          check_expr2_input(item,item->u2.val);
02520       }
02521       break;
02522          
02523    case PV_GOTO:
02524       /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
02525                  item->u1.list->u1.str  == where the data on a PV_WORD will always be.
02526       */
02527       /* don't check goto's in abstract contexts */
02528       if ( in_abstract_context )
02529          break;
02530       
02531       check_goto(item);
02532       break;
02533          
02534    case PV_LABEL:
02535       /* fields: item->u1.str     == label name
02536       */
02537       if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
02538          ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
02539                item->filename, item->startline, item->endline, item->u1.str);
02540          warns++;
02541       }
02542 
02543       check_label(item);
02544       break;
02545          
02546    case PV_FOR:
02547       /* fields: item->u1.for_init     == a string containing the initalizer
02548                  item->u2.for_test     == a string containing the loop test
02549                  item->u3.for_inc      == a string containing the loop increment
02550 
02551                item->u4.for_statements == a pval list of statements in the for ()
02552       */
02553       snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, for test expr '%s':", config, item->startline, item->startcol, item->endcol, item->u2.for_test);
02554       ast_expr_register_extra_error_info(errmsg);
02555 
02556       strp = strchr(item->u1.for_init, '=');
02557       if (strp) {
02558          ast_expr(strp+1, expr_output, sizeof(expr_output));
02559       }
02560       ast_expr(item->u2.for_test, expr_output, sizeof(expr_output));
02561       strp = strchr(item->u3.for_inc, '=');
02562       if (strp) {
02563          ast_expr(strp+1, expr_output, sizeof(expr_output));
02564       }
02565       if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
02566          ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
02567                item->filename, item->startline, item->endline, item->u2.for_test);
02568          warns++;
02569       }
02570       if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
02571          ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
02572                item->filename, item->startline, item->endline, item->u3.for_inc);
02573          warns++;
02574       }
02575       check_expr2_input(item,item->u2.for_test);
02576       check_expr2_input(item,item->u3.for_inc);
02577       
02578       ast_expr_clear_extra_error_info();
02579       check_pval(item->u4.for_statements, apps,in_globals);
02580       break;
02581          
02582    case PV_WHILE:
02583       /* fields: item->u1.str        == the while conditional, as supplied by user
02584 
02585                item->u2.statements == a pval list of statements in the while ()
02586       */
02587       snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, while expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
02588       ast_expr_register_extra_error_info(errmsg);
02589       ast_expr(item->u1.str, expr_output, sizeof(expr_output));
02590       ast_expr_clear_extra_error_info();
02591       if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
02592          ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
02593                item->filename, item->startline, item->endline, item->u1.str);
02594          warns++;
02595       }
02596       check_expr2_input(item,item->u1.str);
02597       check_pval(item->u2.statements, apps,in_globals);
02598       break;
02599          
02600    case PV_BREAK:
02601       /* fields: none
02602       */
02603       check_break(item);
02604       break;
02605          
02606    case PV_RETURN:
02607       /* fields: none
02608       */
02609       break;
02610          
02611    case PV_CONTINUE:
02612       /* fields: none
02613       */
02614       check_continue(item);
02615       break;
02616          
02617    case PV_RANDOM:
02618       /* fields: item->u1.str        == the random number expression, as supplied by user
02619 
02620                item->u2.statements == a pval list of statements in the if ()
02621                item->u3.else_statements == a pval list of statements in the else
02622                                     (could be zero)
02623       */
02624       snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, random expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
02625       ast_expr_register_extra_error_info(errmsg);
02626       ast_expr(item->u1.str, expr_output, sizeof(expr_output));
02627       ast_expr_clear_extra_error_info();
02628       if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
02629          ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
02630                item->filename, item->startline, item->endline, item->u1.str);
02631          warns++;
02632       }
02633       check_expr2_input(item,item->u1.str);
02634       check_pval(item->u2.statements, apps,in_globals);
02635       if (item->u3.else_statements) {
02636          check_pval(item->u3.else_statements, apps,in_globals);
02637       }
02638       break;
02639 
02640    case PV_IFTIME:
02641       /* fields: item->u1.list        == the if time values, 4 of them, each in PV_WORD, linked list 
02642 
02643                item->u2.statements == a pval list of statements in the if ()
02644                item->u3.else_statements == a pval list of statements in the else
02645                                     (could be zero)
02646       */
02647       if ( item->u2.arglist ) {
02648          check_timerange(item->u1.list);
02649          check_dow(item->u1.list->next);
02650          check_day(item->u1.list->next->next);
02651          check_month(item->u1.list->next->next->next);
02652       }
02653 
02654       check_pval(item->u2.statements, apps,in_globals);
02655       if (item->u3.else_statements) {
02656          check_pval(item->u3.else_statements, apps,in_globals);
02657       }
02658       break;
02659          
02660    case PV_IF:
02661       /* fields: item->u1.str        == the if conditional, as supplied by user
02662 
02663                item->u2.statements == a pval list of statements in the if ()
02664                item->u3.else_statements == a pval list of statements in the else
02665                                     (could be zero)
02666       */
02667       snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, if expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
02668       ast_expr_register_extra_error_info(errmsg);
02669       ast_expr(item->u1.str, expr_output, sizeof(expr_output));
02670       ast_expr_clear_extra_error_info();
02671       if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
02672          ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
02673                item->filename, item->startline, item->endline, item->u1.str);
02674          warns++;
02675       }
02676       check_expr2_input(item,item->u1.str);
02677       check_pval(item->u2.statements, apps,in_globals);
02678       if (item->u3.else_statements) {
02679          check_pval(item->u3.else_statements, apps,in_globals);
02680       }
02681       break;
02682          
02683    case PV_SWITCH:
02684       /* fields: item->u1.str        == the switch expression
02685 
02686                item->u2.statements == a pval list of statements in the switch, 
02687                                     (will be case statements, most likely!)
02688       */
02689       /* we can check the switch expression, see if it matches any of the app variables...
02690            if it does, then, are all the possible cases accounted for? */
02691       check_switch_expr(item, apps);
02692       check_pval(item->u2.statements, apps,in_globals);
02693       break;
02694          
02695    case PV_EXTENSION:
02696       /* fields: item->u1.str        == the extension name, label, whatever it's called
02697 
02698                item->u2.statements == a pval list of statements in the extension
02699                item->u3.hints      == a char * hint argument
02700                item->u4.regexten   == an int boolean. non-zero says that regexten was specified
02701       */
02702       current_extension = item ;
02703       
02704       check_pval(item->u2.statements, apps,in_globals);
02705       break;
02706          
02707    case PV_IGNOREPAT:
02708       /* fields: item->u1.str        == the ignorepat data
02709       */
02710       break;
02711          
02712    case PV_GLOBALS:
02713       /* fields: item->u1.statements     == pval list of statements, usually vardecs
02714       */
02715       in_abstract_context = 0;
02716       check_pval(item->u1.statements, apps, 1);
02717       break;
02718    default:
02719       break;
02720    }
02721 }
02722 
02723 void check_pval(pval *item, struct argapp *apps, int in_globals)
02724 {
02725    pval *i;
02726 
02727    /* checks to do:
02728       1. Do goto's point to actual labels? 
02729       2. Do macro calls reference a macro?
02730       3. Does the number of macro args match the definition?
02731       4. Is a macro call missing its & at the front?
02732       5. Application calls-- we could check syntax for existing applications,
02733          but I need some some sort of universal description bnf for a general
02734         sort of method for checking arguments, in number, maybe even type, at least. 
02735         Don't want to hand code checks for hundreds of applications.
02736    */
02737    
02738    for (i=item; i; i=i->next) {
02739       check_pval_item(i,apps,in_globals);
02740    }
02741 }
02742 
02743 static void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
02744 {
02745    
02746 #ifdef AAL_ARGCHECK
02747    int argapp_errs =0;
02748    char *rfilename;
02749 #endif
02750    struct argapp *apps=0;
02751 
02752    if (!item)
02753       return; /* don't check an empty tree */
02754 #ifdef AAL_ARGCHECK
02755    rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
02756    sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
02757    
02758    apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
02759 #endif
02760    current_db = item;
02761    errs = warns = notes = 0;
02762 
02763    check_context_names();
02764    check_pval(item, apps, 0);
02765 
02766 #ifdef AAL_ARGCHECK
02767    argdesc_destroy(apps);  /* taketh away */
02768 #endif
02769    current_db = 0;
02770 
02771    *arg_errs = errs;
02772    *arg_warns = warns;
02773    *arg_notes = notes;
02774 }
02775 
02776 /* =============================================================================================== */
02777 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
02778 /* =============================================================================================== */
02779 
02780 static int control_statement_count = 0;
02781 
02782 struct ael_priority *new_prio(void)
02783 {
02784    struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
02785    return x;
02786 }
02787 
02788 struct ael_extension *new_exten(void)
02789 {
02790    struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
02791    return x;
02792 }
02793 
02794 void linkprio(struct ael_extension *exten, struct ael_priority *prio, struct ael_extension *mother_exten)
02795 {
02796    char *p1, *p2;
02797    
02798    if (!exten->plist) {
02799       exten->plist = prio;
02800       exten->plist_last = prio;
02801    } else {
02802       exten->plist_last->next = prio;
02803       exten->plist_last = prio;
02804    }
02805    if( !prio->exten )
02806       prio->exten = exten; /* don't override the switch value */
02807    /* The following code will cause all priorities within an extension 
02808       to have ${EXTEN} or ${EXTEN: replaced with ~~EXTEN~~, which is
02809       set just before the first switch in an exten. The switches
02810       will muck up the original ${EXTEN} value, so we save it away
02811       and the user accesses this copy instead. */
02812    if (prio->appargs && ((mother_exten && mother_exten->has_switch) || exten->has_switch) ) {
02813       while ((p1 = strstr(prio->appargs, "${EXTEN}"))) {
02814          p2 = malloc(strlen(prio->appargs)+5);
02815          *p1 = 0;
02816          strcpy(p2, prio->appargs);
02817          strcat(p2, "${~~EXTEN~~}");
02818          if (*(p1+8))
02819             strcat(p2, p1+8);
02820          free(prio->appargs);
02821          prio->appargs = p2;
02822       }
02823       while ((p1 = strstr(prio->appargs, "${EXTEN:"))) {
02824          p2 = malloc(strlen(prio->appargs)+5);
02825          *p1 = 0;
02826          strcpy(p2, prio->appargs);
02827          strcat(p2, "${~~EXTEN~~:");
02828          if (*(p1+8))
02829             strcat(p2, p1+8);
02830          free(prio->appargs);
02831          prio->appargs = p2;
02832       }
02833    }
02834 }
02835 
02836 void destroy_extensions(struct ael_extension *exten)
02837 {
02838    struct ael_extension *ne, *nen;
02839    for (ne=exten; ne; ne=nen) {
02840       struct ael_priority *pe, *pen;
02841       
02842       if (ne->name)
02843          free(ne->name);
02844       
02845       /* cidmatch fields are allocated with name, and freed when
02846          the name field is freed. Don't do a free for this field,
02847          unless you LIKE to see a crash! */
02848 
02849       if (ne->hints)
02850          free(ne->hints);
02851       
02852       for (pe=ne->plist; pe; pe=pen) {
02853          pen = pe->next;
02854          if (pe->app)
02855             free(pe->app);
02856          pe->app = 0;
02857          if (pe->appargs)
02858             free(pe->appargs);
02859          pe->appargs = 0;
02860          pe->origin = 0;
02861          pe->goto_true = 0;
02862          pe->goto_false = 0;
02863          free(pe);
02864       }
02865       nen = ne->next_exten;
02866       ne->next_exten = 0;
02867       ne->plist =0;
02868       ne->plist_last = 0;
02869       ne->next_exten = 0;
02870       ne->loop_break = 0;
02871       ne->loop_continue = 0;
02872       free(ne);
02873    }
02874 }
02875 
02876 static int label_inside_case(pval *label)
02877 {
02878    pval *p = label;
02879    
02880    while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
02881       if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN ) {
02882          return 1;
02883       }
02884 
02885       p = p->dad;
02886    }
02887    return 0;
02888 }
02889 
02890 static void linkexten(struct ael_extension *exten, struct ael_extension *add)
02891 {
02892    add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
02893    exten->next_exten = add;
02894 }
02895 
02896 static void remove_spaces_before_equals(char *str)
02897 {
02898    char *p;
02899    while( str && *str && *str != '=' )
02900    {
02901       if( *str == ' ' || *str == '\n' || *str == '\r' || *str == '\t' )
02902       {
02903          p = str;
02904          while( *p )
02905          {
02906             *p = *(p+1);
02907             p++;
02908          }
02909       }
02910       else
02911          str++;
02912    }
02913 }
02914 
02915 static void gen_match_to_pattern(char *pattern, char *result)
02916 {
02917    /* the result will be a string that will be matched by pattern */
02918    char *p=pattern, *t=result;
02919    while (*p) {
02920       if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
02921          *t++ = '9';
02922       else if (*p == '[') {
02923          char *z = p+1;
02924          while (*z != ']')
02925             z++;
02926          if (*(z+1)== ']')
02927             z++;
02928          *t++=*(p+1); /* use the first char in the set */
02929          p = z;
02930       } else {
02931          *t++ = *p;
02932       }
02933       p++;
02934    }
02935    *t++ = 0; /* cap it off */
02936 }
02937 
02938 /* ==== a set of routines to search for a switch statement contained in the pval description */
02939 
02940 int find_switch_item(pval *item);
02941 int contains_switch(pval *item);
02942 
02943 
02944 int find_switch_item(pval *item)
02945 {
02946    switch ( item->type ) {
02947    case PV_WORD:
02948       /* fields: item->u1.str == string associated with this (word). */
02949       break;
02950       
02951    case PV_MACRO:
02952       /* fields: item->u1.str     == name of macro
02953                  item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
02954                item->u2.arglist->u1.str  == argument
02955                item->u2.arglist->next   == next arg
02956 
02957                item->u3.macro_statements == pval list of statements in macro body.
02958       */
02959       /* had better not see this */
02960       if (contains_switch(item->u3.macro_statements))
02961          return 1;
02962       break;
02963          
02964    case PV_CONTEXT:
02965       /* fields: item->u1.str     == name of context
02966                  item->u2.statements == pval list of statements in context body
02967                item->u3.abstract == int 1 if an abstract keyword were present
02968       */
02969       /* had better not see this */
02970       if (contains_switch(item->u2.statements))
02971          return 1;
02972       break;
02973          
02974    case PV_MACRO_CALL:
02975       /* fields: item->u1.str     == name of macro to call
02976                  item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
02977                item->u2.arglist->u1.str  == argument
02978                item->u2.arglist->next   == next arg
02979       */
02980       break;
02981          
02982    case PV_APPLICATION_CALL:
02983       /* fields: item->u1.str     == name of application to call
02984                  item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
02985                item->u2.arglist->u1.str  == argument
02986                item->u2.arglist->next   == next arg
02987       */
02988       break;
02989          
02990    case PV_CASE:
02991       /* fields: item->u1.str     == value of case
02992                  item->u2.statements == pval list of statements under the case
02993       */
02994       /* had better not see this */
02995       if (contains_switch(item->u2.statements))
02996          return 1;
02997       break;
02998          
02999    case PV_PATTERN:
03000       /* fields: item->u1.str     == value of case
03001                  item->u2.statements == pval list of statements under the case
03002       */
03003       /* had better not see this */
03004       if (contains_switch(item->u2.statements))
03005          return 1;
03006       break;
03007          
03008    case PV_DEFAULT:
03009       /* fields: 
03010                  item->u2.statements == pval list of statements under the case
03011       */
03012       /* had better not see this */
03013       if (contains_switch(item->u2.statements))
03014          return 1;
03015       break;
03016          
03017    case PV_CATCH:
03018       /* fields: item->u1.str     == name of extension to catch
03019                  item->u2.statements == pval list of statements in context body
03020       */
03021       /* had better not see this */
03022       if (contains_switch(item->u2.statements))
03023          return 1;
03024       break;
03025          
03026    case PV_SWITCHES:
03027       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
03028       */
03029       break;
03030          
03031    case PV_ESWITCHES:
03032       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
03033       */
03034       break;
03035          
03036    case PV_INCLUDES:
03037       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
03038                  item->u2.arglist  == pval list of 4 PV_WORD elements for time values
03039       */
03040       break;
03041          
03042    case PV_STATEMENTBLOCK:
03043       /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
03044       */
03045       if (contains_switch(item->u1.list) )
03046          return 1;
03047       break;
03048          
03049    case PV_VARDEC:
03050       /* fields: item->u1.str     == variable name
03051                  item->u2.val     == variable value to assign
03052       */
03053       break;
03054          
03055    case PV_GOTO:
03056       /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
03057                  item->u1.list->u1.str  == where the data on a PV_WORD will always be.
03058       */
03059       break;
03060          
03061    case PV_LABEL:
03062       /* fields: item->u1.str     == label name
03063       */
03064       break;
03065          
03066    case PV_FOR:
03067       /* fields: item->u1.for_init     == a string containing the initalizer
03068                  item->u2.for_test     == a string containing the loop test
03069                  item->u3.for_inc      == a string containing the loop increment
03070 
03071                item->u4.for_statements == a pval list of statements in the for ()
03072       */
03073       if (contains_switch(item->u4.for_statements))
03074          return 1;
03075       break;
03076          
03077    case PV_WHILE:
03078       /* fields: item->u1.str        == the while conditional, as supplied by user
03079 
03080                item->u2.statements == a pval list of statements in the while ()
03081       */
03082       if (contains_switch(item->u2.statements))
03083          return 1;
03084       break;
03085          
03086    case PV_BREAK:
03087       /* fields: none
03088       */
03089       break;
03090          
03091    case PV_RETURN:
03092       /* fields: none
03093       */
03094       break;
03095          
03096    case PV_CONTINUE:
03097       /* fields: none
03098       */
03099       break;
03100          
03101    case PV_IFTIME:
03102       /* fields: item->u1.list        == there are 4 linked PV_WORDs here.
03103 
03104                item->u2.statements == a pval list of statements in the if ()
03105                item->u3.else_statements == a pval list of statements in the else
03106                                     (could be zero)
03107       */
03108       if (contains_switch(item->u2.statements))
03109          return 1;
03110       if ( item->u3.else_statements ) {
03111          if (contains_switch(item->u3.else_statements))
03112             return 1;
03113       }
03114       break;
03115          
03116    case PV_RANDOM:
03117       /* fields: item->u1.str        == the random number expression, as supplied by user
03118 
03119                item->u2.statements == a pval list of statements in the if ()
03120                item->u3.else_statements == a pval list of statements in the else
03121                                     (could be zero)
03122       */
03123       if (contains_switch(item->u2.statements))
03124          return 1;
03125       if ( item->u3.else_statements ) {
03126          if (contains_switch(item->u3.else_statements))
03127             return 1;
03128       }
03129       break;
03130          
03131    case PV_IF:
03132       /* fields: item->u1.str        == the if conditional, as supplied by user
03133 
03134                item->u2.statements == a pval list of statements in the if ()
03135                item->u3.else_statements == a pval list of statements in the else
03136                                     (could be zero)
03137       */
03138       if (contains_switch(item->u2.statements))
03139          return 1;
03140       if ( item->u3.else_statements ) {
03141          if (contains_switch(item->u3.else_statements))
03142             return 1;
03143       }
03144       break;
03145          
03146    case PV_SWITCH:
03147       /* fields: item->u1.str        == the switch expression
03148 
03149                item->u2.statements == a pval list of statements in the switch, 
03150                                     (will be case statements, most likely!)
03151       */
03152       return 1; /* JACKPOT */
03153       break;
03154          
03155    case PV_EXTENSION:
03156       /* fields: item->u1.str        == the extension name, label, whatever it's called
03157 
03158                item->u2.statements == a pval list of statements in the extension
03159                item->u3.hints      == a char * hint argument
03160                item->u4.regexten   == an int boolean. non-zero says that regexten was specified
03161       */
03162       if (contains_switch(item->u2.statements))
03163          return 1;
03164       break;
03165          
03166    case PV_IGNOREPAT:
03167       /* fields: item->u1.str        == the ignorepat data
03168       */
03169       break;
03170          
03171    case PV_GLOBALS:
03172       /* fields: item->u1.statements     == pval list of statements, usually vardecs
03173       */
03174       break;
03175    }
03176    return 0;
03177 }
03178 
03179 int contains_switch(pval *item)
03180 {
03181    pval *i;
03182    
03183    for (i=item; i; i=i->next) {
03184       if (find_switch_item(i))
03185          return 1;
03186    }
03187    return 0;
03188 }
03189 
03190 
03191 static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
03192 {
03193    pval *p,*p2,*p3;
03194    struct ael_priority *pr;
03195    struct ael_priority *for_init, *for_test, *for_inc, *for_loop, *for_end;
03196    struct ael_priority *while_test, *while_loop, *while_end;
03197    struct ael_priority *switch_set, *switch_test, *switch_end, *fall_thru, *switch_empty;
03198    struct ael_priority *if_test, *if_end, *if_skip, *if_false;
03199 #ifdef OLD_RAND_ACTION
03200    struct ael_priority *rand_test, *rand_end, *rand_skip;
03201 #endif
03202    char buf1[2000];
03203    char buf2[2000];
03204    char *strp, *strp2;
03205    char new_label[2000];
03206    int default_exists;
03207    int local_control_statement_count;
03208    struct ael_priority *loop_break_save;
03209    struct ael_priority *loop_continue_save;
03210    struct ael_extension *switch_case,*switch_null;
03211    
03212    if ((mother_exten && !mother_exten->checked_switch) || (exten && !exten->checked_switch)) {
03213       if (contains_switch(statement)) { /* only run contains_switch if you haven't checked before */
03214          if (mother_exten) {
03215             if (!mother_exten->has_switch) {
03216                switch_set = new_prio();
03217                switch_set->type = AEL_APPCALL;
03218                switch_set->app = strdup("Set");
03219                switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
03220                linkprio(exten, switch_set, mother_exten);
03221                mother_exten->has_switch = 1;
03222                mother_exten->checked_switch = 1;
03223                if (exten) {
03224                   exten->has_switch = 1;
03225                   exten->checked_switch = 1;
03226                }
03227             }
03228          } else if (exten) {
03229             if (!exten->has_switch) {
03230                switch_set = new_prio();
03231                switch_set->type = AEL_APPCALL;
03232                switch_set->app = strdup("Set");
03233                switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
03234                linkprio(exten, switch_set, mother_exten);
03235                exten->has_switch = 1;
03236                exten->checked_switch = 1;
03237                if (mother_exten) {
03238                   mother_exten->has_switch = 1;
03239                   mother_exten->checked_switch = 1;
03240                }
03241             }
03242          }
03243       } else {
03244          if (mother_exten) {
03245             mother_exten->checked_switch = 1;
03246          }
03247          if (exten) {
03248             exten->checked_switch = 1;
03249          }
03250       }
03251    }
03252    for (p=statement; p; p=p->next) {
03253       switch (p->type) {
03254       case PV_VARDEC:
03255          pr = new_prio();
03256          pr->type = AEL_APPCALL;
03257          snprintf(buf1,sizeof(buf1),"%s=$[%s]", p->u1.str, p->u2.val);
03258          pr->app = strdup("Set");
03259          remove_spaces_before_equals(buf1);
03260          pr->appargs = strdup(buf1);
03261          pr->origin = p;
03262          linkprio(exten, pr, mother_exten);
03263          break;
03264          
03265       case PV_GOTO:
03266          pr = new_prio();
03267          pr->type = AEL_APPCALL;
03268          p->u2.goto_target = get_goto_target(p);
03269          if( p->u2.goto_target ) {
03270             p->u3.goto_target_in_case = label_inside_case(p->u2.goto_target);
03271          }
03272          
03273          if (!p->u1.list->next) /* just one */ {
03274             pr->app = strdup("Goto");
03275             if (!mother_exten)
03276                pr->appargs = strdup(p->u1.list->u1.str);
03277             else {  /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */ 
03278                snprintf(buf1,sizeof(buf1),"%s|%s", mother_exten->name, p->u1.list->u1.str);
03279                pr->appargs = strdup(buf1);
03280             }
03281             
03282          } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
03283             snprintf(buf1,sizeof(buf1),"%s|%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
03284             pr->app = strdup("Goto");
03285             pr->appargs = strdup(buf1);
03286          } else if (p->u1.list->next && p->u1.list->next->next) {
03287             snprintf(buf1,sizeof(buf1),"%s|%s|%s", p->u1.list->u1.str, 
03288                   p->u1.list->next->u1.str,
03289                   p->u1.list->next->next->u1.str);
03290             pr->app = strdup("Goto");
03291             pr->appargs = strdup(buf1);
03292          }
03293          pr->origin = p;
03294          linkprio(exten, pr, mother_exten);
03295          break;
03296 
03297       case PV_LABEL:
03298          pr = new_prio();
03299          pr->type = AEL_LABEL;
03300          pr->origin = p;
03301          p->u3.compiled_label = exten;
03302          linkprio(exten, pr, mother_exten);
03303          break;
03304 
03305       case PV_FOR:
03306          control_statement_count++;
03307          loop_break_save = exten->loop_break; /* save them, then restore before leaving */
03308          loop_continue_save = exten->loop_continue;
03309          snprintf(new_label,sizeof(new_label),"for-%s-%d", label, control_statement_count);
03310          for_init = new_prio();
03311          for_inc = new_prio();
03312          for_test = new_prio();
03313          for_loop = new_prio();
03314          for_end = new_prio();
03315          for_init->type = AEL_APPCALL;
03316          for_inc->type = AEL_APPCALL;
03317          for_test->type = AEL_FOR_CONTROL;
03318          for_test->goto_false = for_end;
03319          for_loop->type = AEL_CONTROL1; /* simple goto */
03320          for_end->type = AEL_APPCALL;
03321          for_init->app = strdup("Set");
03322          
03323          strcpy(buf2,p->u1.for_init);
03324          remove_spaces_before_equals(buf2);
03325          strp = strchr(buf2, '=');
03326          if (strp) {
03327             strp2 = strchr(p->u1.for_init, '=');
03328             *(strp+1) = 0;
03329             strcat(buf2,"$[");
03330             strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
03331             strcat(buf2,"]");
03332             for_init->appargs = strdup(buf2);
03333             /* for_init->app = strdup("Set"); just set! */
03334          } else {
03335             strp2 = p->u1.for_init;
03336             while (*strp2 && isspace(*strp2))
03337                strp2++;
03338             if (*strp2 == '&') { /* itsa macro call */
03339                char *strp3 = strp2+1;
03340                while (*strp3 && isspace(*strp3))
03341                   strp3++;
03342                strcpy(buf2, strp3);
03343                strp3 = strchr(buf2,'(');
03344                if (strp3) {
03345                   *strp3 = '|';
03346                }
03347                while ((strp3=strchr(buf2,','))) {
03348                   *strp3 = '|';
03349                }
03350                strp3 = strrchr(buf2, ')');
03351                if (strp3)
03352                   *strp3 = 0; /* remove the closing paren */
03353 
03354                for_init->appargs = strdup(buf2);
03355                if (for_init->app)
03356                   free(for_init->app);
03357                for_init->app = strdup("Macro");
03358             } else {  /* must be a regular app call */
03359                char *strp3;
03360                strcpy(buf2, strp2);
03361                strp3 = strchr(buf2,'(');
03362                if (strp3) {
03363                   *strp3 = 0;
03364                   if (for_init->app)
03365                      free(for_init->app);
03366                   for_init->app = strdup(buf2);
03367                   for_init->appargs = strdup(strp3+1);
03368                   strp3 = strrchr(for_init->appargs, ')');
03369                   if (strp3)
03370                      *strp3 = 0; /* remove the closing paren */
03371                }
03372             }
03373          }
03374 
03375          strcpy(buf2,p->u3.for_inc);
03376          remove_spaces_before_equals(buf2);
03377          strp = strchr(buf2, '=');
03378          if (strp) {  /* there's an = in this part; that means an assignment. set it up */
03379             strp2 = strchr(p->u3.for_inc, '=');
03380             *(strp+1) = 0;
03381             strcat(buf2,"$[");
03382             strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
03383             strcat(buf2,"]");
03384             for_inc->appargs = strdup(buf2);
03385             for_inc->app = strdup("Set");
03386          } else {
03387             strp2 = p->u3.for_inc;
03388             while (*strp2 && isspace(*strp2))
03389                strp2++;
03390             if (*strp2 == '&') { /* itsa macro call */
03391                char *strp3 = strp2+1;
03392                while (*strp3 && isspace(*strp3))
03393                   strp3++;
03394                strcpy(buf2, strp3);
03395                strp3 = strchr(buf2,'(');
03396                if (strp3) {
03397                   *strp3 = '|';
03398                }
03399                while ((strp3=strchr(buf2,','))) {
03400                   *strp3 = '|';
03401                }
03402                strp3 = strrchr(buf2, ')');
03403                if (strp3)
03404                   *strp3 = 0; /* remove the closing paren */
03405 
03406                for_inc->appargs = strdup(buf2);
03407 
03408                for_inc->app = strdup("Macro");
03409             } else {  /* must be a regular app call */
03410                char *strp3;
03411                strcpy(buf2, strp2);
03412                strp3 = strchr(buf2,'(');
03413                if (strp3) {
03414                   *strp3 = 0;
03415                   for_inc->app = strdup(buf2);
03416                   for_inc->appargs = strdup(strp3+1);
03417                   strp3 = strrchr(for_inc->appargs, ')');
03418                   if (strp3)
03419                      *strp3 = 0; /* remove the closing paren */
03420                }
03421             }
03422          }
03423          snprintf(buf1,sizeof(buf1),"$[%s]",p->u2.for_test);
03424          for_test->app = 0;
03425          for_test->appargs = strdup(buf1);
03426          for_loop->goto_true = for_test;
03427          snprintf(buf1,sizeof(buf1),"Finish for-%s-%d", label, control_statement_count);
03428          for_end->app = strdup("NoOp");
03429          for_end->appargs = strdup(buf1);
03430          /* link & load! */
03431          linkprio(exten, for_init, mother_exten);
03432          linkprio(exten, for_test, mother_exten);
03433          
03434          /* now, put the body of the for loop here */
03435          exten->loop_break = for_end;
03436          exten->loop_continue = for_inc;
03437          
03438          gen_prios(exten, new_label, p->u4.for_statements, mother_exten, this_context); /* this will link in all the statements here */
03439          
03440          linkprio(exten, for_inc, mother_exten);
03441          linkprio(exten, for_loop, mother_exten);
03442          linkprio(exten, for_end, mother_exten);
03443          
03444          
03445          exten->loop_break = loop_break_save;
03446          exten->loop_continue = loop_continue_save;
03447          for_loop->origin = p;
03448          break;
03449 
03450       case PV_WHILE:
03451          control_statement_count++;
03452          loop_break_save = exten->loop_break; /* save them, then restore before leaving */
03453          loop_continue_save = exten->loop_continue;
03454          snprintf(new_label,sizeof(new_label),"while-%s-%d", label, control_statement_count);
03455          while_test = new_prio();
03456          while_loop = new_prio();
03457          while_end = new_prio();
03458          while_test->type = AEL_FOR_CONTROL;
03459          while_test->goto_false = while_end;
03460          while_loop->type = AEL_CONTROL1; /* simple goto */
03461          while_end->type = AEL_APPCALL;
03462          snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
03463          while_test->app = 0;
03464          while_test->appargs = strdup(buf1);
03465          while_loop->goto_true = while_test;
03466          snprintf(buf1,sizeof(buf1),"Finish while-%s-%d", label, control_statement_count);
03467          while_end->app = strdup("NoOp");
03468          while_end->appargs = strdup(buf1);
03469 
03470          linkprio(exten, while_test, mother_exten);
03471          
03472          /* now, put the body of the for loop here */
03473          exten->loop_break = while_end;
03474          exten->loop_continue = while_test;
03475          
03476          gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the while body statements here */
03477 
03478          linkprio(exten, while_loop, mother_exten);
03479          linkprio(exten, while_end, mother_exten);
03480          
03481          
03482          exten->loop_break = loop_break_save;
03483          exten->loop_continue = loop_continue_save;
03484          while_loop->origin = p;
03485          break;
03486 
03487       case PV_SWITCH:
03488          control_statement_count++;
03489          local_control_statement_count = control_statement_count;
03490          loop_break_save = exten->loop_break; /* save them, then restore before leaving */
03491          loop_continue_save = exten->loop_continue;
03492          snprintf(new_label,sizeof(new_label),"sw-%s-%d", label, control_statement_count);
03493          switch_test = new_prio();
03494          switch_end = new_prio();
03495          switch_test->type = AEL_APPCALL;
03496          switch_end->type = AEL_APPCALL;
03497          strncpy(buf2,p->u1.str,sizeof(buf2));
03498          buf2[sizeof(buf2)-1] = 0; /* just in case */
03499          substitute_commas(buf2);
03500          snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",control_statement_count, buf2);
03501          switch_test->app = strdup("Goto");
03502          switch_test->appargs = strdup(buf1);
03503          snprintf(buf1,sizeof(buf1),"Finish switch-%s-%d", label, control_statement_count);
03504          switch_end->app = strdup("NoOp");
03505          switch_end->appargs = strdup(buf1);
03506          switch_end->origin = p;
03507          switch_end->exten = exten;
03508 
03509          linkprio(exten, switch_test, mother_exten);
03510          linkprio(exten, switch_end, mother_exten);
03511          
03512          exten->loop_break = switch_end;
03513          exten->loop_continue = 0;
03514          default_exists = 0;
03515          
03516          for (p2=p->u2.statements; p2; p2=p2->next) {
03517             /* now, for each case/default put the body of the for loop here */
03518             if (p2->type == PV_CASE) {
03519                /* ok, generate a extension and link it in */
03520                switch_case = new_exten();
03521                if (mother_exten && mother_exten->checked_switch) {
03522                   switch_case->has_switch = mother_exten->has_switch;
03523                   switch_case->checked_switch = mother_exten->checked_switch;
03524                }
03525                if (exten && exten->checked_switch) {
03526                   switch_case->has_switch = exten->has_switch;
03527                   switch_case->checked_switch = exten->checked_switch;
03528                }
03529                switch_case->context = this_context;
03530                switch_case->is_switch = 1;
03531                /* the break/continue locations are inherited from parent */
03532                switch_case->loop_break = exten->loop_break;
03533                switch_case->loop_continue = exten->loop_continue;
03534                
03535                linkexten(exten,switch_case);
03536                strncpy(buf2,p2->u1.str,sizeof(buf2));
03537                buf2[sizeof(buf2)-1] = 0; /* just in case */
03538                substitute_commas(buf2);
03539                snprintf(buf1,sizeof(buf1),"sw-%d-%s", local_control_statement_count, buf2);
03540                switch_case->name = strdup(buf1);
03541                snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, buf2, local_control_statement_count);
03542                
03543                gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the case body statements here */
03544 
03545                /* here is where we write code to "fall thru" to the next case... if there is one... */
03546                for (p3=p2->u2.statements; p3; p3=p3->next) {
03547                   if (!p3->next)
03548                      break;
03549                }
03550                /* p3 now points the last statement... */
03551                if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN) ) {
03552                   /* is there a following CASE/PATTERN/DEFAULT? */
03553                   if (p2->next && p2->next->type == PV_CASE) {
03554                      fall_thru = new_prio();
03555                      fall_thru->type = AEL_APPCALL;
03556                      fall_thru->app = strdup("Goto");
03557                      strncpy(buf2,p2->next->u1.str,sizeof(buf2));
03558                      buf2[sizeof(buf2)-1] = 0; /* just in case */
03559                      substitute_commas(buf2);
03560                      snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
03561                      fall_thru->appargs = strdup(buf1);
03562                      linkprio(switch_case, fall_thru, mother_exten);
03563                   } else if (p2->next && p2->next->type == PV_PATTERN) {
03564                      fall_thru = new_prio();
03565                      fall_thru->type = AEL_APPCALL;
03566                      fall_thru->app = strdup("Goto");
03567                      gen_match_to_pattern(p2->next->u1.str, buf2);
03568                      substitute_commas(buf2);
03569                      snprintf(buf1,sizeof(buf1),"sw-%d-%s|10", local_control_statement_count, buf2);
03570                      fall_thru->appargs = strdup(buf1);
03571                      linkprio(switch_case, fall_thru, mother_exten);
03572                   } else if (p2->next && p2->next->type == PV_DEFAULT) {
03573                      fall_thru = new_prio();
03574                      fall_thru->type = AEL_APPCALL;
03575                      fall_thru->app = strdup("Goto");
03576                      snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
03577                      fall_thru->appargs = strdup(buf1);
03578                      linkprio(switch_case, fall_thru, mother_exten);
03579                   } else if (!p2->next) {
03580                      fall_thru = new_prio();
03581                      fall_thru->type = AEL_CONTROL1;
03582                      fall_thru->goto_true = switch_end;
03583                      fall_thru->app = strdup("Goto");
03584                      linkprio(switch_case, fall_thru, mother_exten);
03585                   }
03586                }
03587                if (switch_case->return_needed) {
03588                   char buf[2000];
03589                   struct ael_priority *np2 = new_prio();
03590                   np2->type = AEL_APPCALL;
03591                   np2->app = strdup("NoOp");
03592                   snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
03593                   np2->appargs = strdup(buf);
03594                   linkprio(switch_case, np2, mother_exten);
03595                   switch_case-> return_target = np2;
03596                }
03597             } else if (p2->type == PV_PATTERN) {
03598                /* ok, generate a extension and link it in */
03599                switch_case = new_exten();
03600                if (mother_exten && mother_exten->checked_switch) {
03601                   switch_case->has_switch = mother_exten->has_switch;
03602                   switch_case->checked_switch = mother_exten->checked_switch;
03603                }
03604                if (exten && exten->checked_switch) {
03605                   switch_case->has_switch = exten->has_switch;
03606                   switch_case->checked_switch = exten->checked_switch;
03607                }
03608                switch_case->context = this_context;
03609                switch_case->is_switch = 1;
03610                /* the break/continue locations are inherited from parent */
03611                switch_case->loop_break = exten->loop_break;
03612                switch_case->loop_continue = exten->loop_continue;
03613                
03614                linkexten(exten,switch_case);
03615                strncpy(buf2,p2->u1.str,sizeof(buf2));
03616                buf2[sizeof(buf2)-1] = 0; /* just in case */
03617                substitute_commas(buf2);
03618                snprintf(buf1,sizeof(buf1),"_sw-%d-%s", local_control_statement_count, buf2);
03619                switch_case->name = strdup(buf1);
03620                snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, buf2, local_control_statement_count);
03621                
03622                gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the while body statements here */
03623                /* here is where we write code to "fall thru" to the next case... if there is one... */
03624                for (p3=p2->u2.statements; p3; p3=p3->next) {
03625                   if (!p3->next)
03626                      break;
03627                }
03628                /* p3 now points the last statement... */
03629                if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN)) {
03630                   /* is there a following CASE/PATTERN/DEFAULT? */
03631                   if (p2->next && p2->next->type == PV_CASE) {
03632                      fall_thru = new_prio();
03633                      fall_thru->type = AEL_APPCALL;
03634                      fall_thru->app = strdup("Goto");
03635                      strncpy(buf2,p2->next->u1.str,sizeof(buf2));
03636                      buf2[sizeof(buf2)-1] = 0; /* just in case */
03637                      substitute_commas(buf2);
03638                      snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
03639                      fall_thru->appargs = strdup(buf1);
03640                      linkprio(switch_case, fall_thru, mother_exten);
03641                   } else if (p2->next && p2->next->type == PV_PATTERN) {
03642                      fall_thru = new_prio();
03643                      fall_thru->type = AEL_APPCALL;
03644                      fall_thru->app = strdup("Goto");
03645                      gen_match_to_pattern(p2->next->u1.str, buf2);
03646                      substitute_commas(buf2);
03647                      snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
03648                      fall_thru->appargs = strdup(buf1);
03649                      linkprio(switch_case, fall_thru, mother_exten);
03650                   } else if (p2->next && p2->next->type == PV_DEFAULT) {
03651                      fall_thru = new_prio();
03652                      fall_thru->type = AEL_APPCALL;
03653                      fall_thru->app = strdup("Goto");
03654                      snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
03655                      fall_thru->appargs = strdup(buf1);
03656                      linkprio(switch_case, fall_thru, mother_exten);
03657                   } else if (!p2->next) {
03658                      fall_thru = new_prio();
03659                      fall_thru->type = AEL_CONTROL1;
03660                      fall_thru->goto_true = switch_end;
03661                      fall_thru->app = strdup("Goto");
03662                      linkprio(switch_case, fall_thru, mother_exten);
03663                   }
03664                }
03665                if (switch_case->return_needed) {
03666                   char buf[2000];
03667                   struct ael_priority *np2 = new_prio();
03668                   np2->type = AEL_APPCALL;
03669                   np2->app = strdup("NoOp");
03670                   snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
03671                   np2->appargs = strdup(buf);
03672                   linkprio(switch_case, np2, mother_exten);
03673                   switch_case-> return_target = np2;
03674                }
03675             } else if (p2->type == PV_DEFAULT) {
03676                /* ok, generate a extension and link it in */
03677                switch_case = new_exten();
03678                if (mother_exten && mother_exten->checked_switch) {
03679                   switch_case->has_switch = mother_exten->has_switch;
03680                   switch_case->checked_switch = mother_exten->checked_switch;
03681                }
03682                if (exten && exten->checked_switch) {
03683                   switch_case->has_switch = exten->has_switch;
03684                   switch_case->checked_switch = exten->checked_switch;
03685                }
03686                switch_case->context = this_context;
03687                switch_case->is_switch = 1;
03688                
03689                /* new: the default case intros a pattern with ., which covers ALMOST everything.
03690                   but it doesn't cover a NULL pattern. So, we'll define a null extension to match
03691                   that goto's the default extension. */
03692 
03693                default_exists++;
03694                switch_null = new_exten();
03695                if (mother_exten && mother_exten->checked_switch) {
03696                   switch_null->has_switch = mother_exten->has_switch;
03697                   switch_null->checked_switch = mother_exten->checked_switch;
03698                }
03699                if (exten && exten->checked_switch) {
03700                   switch_null->has_switch = exten->has_switch;
03701                   switch_null->checked_switch = exten->checked_switch;
03702                }
03703                switch_null->context = this_context;
03704                switch_null->is_switch = 1;
03705                switch_empty = new_prio();
03706                snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
03707                switch_empty->app = strdup("Goto");
03708                switch_empty->appargs = strdup(buf1);
03709                linkprio(switch_null, switch_empty, mother_exten);
03710                snprintf(buf1,sizeof(buf1),"sw-%d-", local_control_statement_count);
03711                switch_null->name = strdup(buf1);
03712                switch_null->loop_break = exten->loop_break;
03713                switch_null->loop_continue = exten->loop_continue;
03714                linkexten(exten,switch_null);
03715 
03716                /* the break/continue locations are inherited from parent */
03717                switch_case->loop_break = exten->loop_break;
03718                switch_case->loop_continue = exten->loop_continue;
03719                linkexten(exten,switch_case);
03720                snprintf(buf1,sizeof(buf1),"_sw-%d-.", local_control_statement_count);
03721                switch_case->name = strdup(buf1);
03722                
03723                snprintf(new_label,sizeof(new_label),"sw-%s-default-%d", label, local_control_statement_count);
03724                
03725                gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the default:  body statements here */
03726                
03727                /* here is where we write code to "fall thru" to the next case... if there is one... */
03728                for (p3=p2->u2.statements; p3; p3=p3->next) {
03729                   if (!p3->next)
03730                      break;
03731                }
03732                /* p3 now points the last statement... */
03733                if (!p3 || (p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN)) {
03734                   /* is there a following CASE/PATTERN/DEFAULT? */
03735                   if (p2->next && p2->next->type == PV_CASE) {
03736                      fall_thru = new_prio();
03737                      fall_thru->type = AEL_APPCALL;
03738                      fall_thru->app = strdup("Goto");
03739                      strncpy(buf2,p2->next->u1.str,sizeof(buf2));
03740                      buf2[sizeof(buf2)-1] = 0; /* just in case */
03741                      substitute_commas(buf2);
03742                      snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
03743                      fall_thru->appargs = strdup(buf1);
03744                      linkprio(switch_case, fall_thru, mother_exten);
03745                   } else if (p2->next && p2->next->type == PV_PATTERN) {
03746                      fall_thru = new_prio();
03747                      fall_thru->type = AEL_APPCALL;
03748                      fall_thru->app = strdup("Goto");
03749                      gen_match_to_pattern(p2->next->u1.str, buf2);
03750                      substitute_commas(buf2);
03751                      snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
03752                      fall_thru->appargs = strdup(buf1);
03753                      linkprio(switch_case, fall_thru, mother_exten);
03754                   } else if (p2->next && p2->next->type == PV_DEFAULT) {
03755                      fall_thru = new_prio();
03756                      fall_thru->type = AEL_APPCALL;
03757                      fall_thru->app = strdup("Goto");
03758                      snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
03759                      fall_thru->appargs = strdup(buf1);
03760                      linkprio(switch_case, fall_thru, mother_exten);
03761                   } else if (!p2->next) {
03762                      fall_thru = new_prio();
03763                      fall_thru->type = AEL_CONTROL1;
03764                      fall_thru->goto_true = switch_end;
03765                      fall_thru->app = strdup("Goto");
03766                      linkprio(switch_case, fall_thru, mother_exten);
03767                   }
03768                }
03769                if (switch_case->return_needed) {
03770                   char buf[2000];
03771                   struct ael_priority *np2 = new_prio();
03772                   np2->type = AEL_APPCALL;
03773                   np2->app = strdup("NoOp");
03774                   snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
03775                   np2->appargs = strdup(buf);
03776                   linkprio(switch_case, np2, mother_exten);
03777                   switch_case-> return_target = np2;
03778                }
03779             } else {
03780                /* what could it be??? */
03781             }
03782          }
03783          
03784          exten->loop_break = loop_break_save;
03785          exten->loop_continue = loop_continue_save;
03786          switch_test->origin = p;
03787          switch_end->origin = p;
03788          break;
03789 
03790       case PV_MACRO_CALL:
03791          pr = new_prio();
03792          pr->type = AEL_APPCALL;
03793          snprintf(buf1,sizeof(buf1),"%s", p->u1.str);
03794          for (p2 = p->u2.arglist; p2; p2 = p2->next) {
03795             strcat(buf1,"|");
03796             strcat(buf1,p2->u1.str);
03797          }
03798          pr->app = strdup("Macro");
03799          pr->appargs = strdup(buf1);
03800          pr->origin = p;
03801          linkprio(exten, pr, mother_exten);
03802          break;
03803 
03804       case PV_APPLICATION_CALL:
03805          pr = new_prio();
03806          pr->type = AEL_APPCALL;
03807          buf1[0] = 0;
03808          for (p2 = p->u2.arglist; p2; p2 = p2->next) {
03809             if (p2 != p->u2.arglist )
03810                strcat(buf1,"|");
03811             substitute_commas(p2->u1.str);
03812             strcat(buf1,p2->u1.str);
03813          }
03814          pr->app = strdup(p->u1.str);
03815          pr->appargs = strdup(buf1);
03816          pr->origin = p;
03817          linkprio(exten, pr, mother_exten);
03818          break;
03819 
03820       case PV_BREAK:
03821          pr = new_prio();
03822          pr->type = AEL_CONTROL1; /* simple goto */
03823          pr->goto_true = exten->loop_break;
03824          pr->origin = p;
03825          linkprio(exten, pr, mother_exten);
03826          break;
03827 
03828       case PV_RETURN: /* hmmmm */
03829          pr = new_prio();
03830          pr->type = AEL_RETURN; /* simple goto */
03831          exten->return_needed++;
03832          pr->app = strdup("Goto");
03833          pr->appargs = strdup("");
03834          pr->origin = p;
03835          linkprio(exten, pr, mother_exten);
03836          break;
03837 
03838       case PV_CONTINUE:
03839          pr = new_prio();
03840          pr->type = AEL_CONTROL1; /* simple goto */
03841          pr->goto_true = exten->loop_continue;
03842          pr->origin = p;
03843          linkprio(exten, pr, mother_exten);
03844          break;
03845 
03846 #ifdef OLD_RAND_ACTION
03847       case PV_RANDOM:
03848          control_statement_count++;
03849          snprintf(new_label,sizeof(new_label),"rand-%s-%d", label, control_statement_count);
03850          rand_test = new_prio();
03851          rand_test->type = AEL_RAND_CONTROL;
03852          snprintf(buf1,sizeof(buf1),"$[%s]",
03853                 p->u1.str );
03854          rand_test->app = 0;
03855          rand_test->appargs = strdup(buf1);
03856          rand_test->origin = p;
03857          
03858          rand_end = new_prio();
03859          rand_end->type = AEL_APPCALL;
03860          snprintf(buf1,sizeof(buf1),"Finish rand-%s-%d", label, control_statement_count);
03861          rand_end->app = strdup("NoOp");
03862          rand_end->appargs = strdup(buf1);
03863          
03864          rand_skip = new_prio();
03865          rand_skip->type = AEL_CONTROL1; /* simple goto */
03866          rand_skip->goto_true = rand_end;
03867          rand_skip->origin  = p;
03868 
03869          rand_test->goto_true = rand_skip; /* +1, really */
03870 
03871          linkprio(exten, rand_test, mother_exten);
03872          
03873          if (p->u3.else_statements) {
03874             gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the else statements here */
03875          }
03876          
03877          linkprio(exten, rand_skip, mother_exten);
03878          
03879          gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the "true" statements here */
03880 
03881          linkprio(exten, rand_end, mother_exten);
03882          
03883          break;
03884 #endif         
03885 
03886       case PV_IFTIME:
03887          control_statement_count++;
03888          snprintf(new_label,sizeof(new_label),"iftime-%s-%d", label, control_statement_count);
03889          
03890          if_test = new_prio();
03891          if_test->type = AEL_IFTIME_CONTROL;
03892          snprintf(buf1,sizeof(buf1),"%s|%s|%s|%s",
03893                 p->u1.list->u1.str, 
03894                 p->u1.list->next->u1.str, 
03895                 p->u1.list->next->next->u1.str, 
03896                 p->u1.list->next->next->next->u1.str);
03897          if_test->app = 0;
03898          if_test->appargs = strdup(buf1);
03899          if_test->origin = p;
03900 
03901          if_end = new_prio();
03902          if_end->type = AEL_APPCALL;
03903          snprintf(buf1,sizeof(buf1),"Finish iftime-%s-%d", label, control_statement_count);
03904          if_end->app = strdup("NoOp");
03905          if_end->appargs = strdup(buf1);
03906 
03907          if (p->u3.else_statements) {
03908             if_skip = new_prio();
03909             if_skip->type = AEL_CONTROL1; /* simple goto */
03910             if_skip->goto_true = if_end;
03911             if_skip->origin  = p;
03912 
03913          } else {
03914             if_skip = 0;
03915 
03916             if_test->goto_false = if_end;
03917          }
03918 
03919          if_false = new_prio();
03920          if_false->type = AEL_CONTROL1;
03921          if (p->u3.else_statements) {
03922             if_false->goto_true = if_skip; /* +1 */
03923          } else {
03924             if_false->goto_true = if_end;
03925          }
03926          
03927          /* link & load! */
03928          linkprio(exten, if_test, mother_exten);
03929          linkprio(exten, if_false, mother_exten);
03930          
03931          /* now, put the body of the if here */
03932          
03933          gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
03934          
03935          if (p->u3.else_statements) {
03936             linkprio(exten, if_skip, mother_exten);
03937             gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
03938 
03939          }
03940          
03941          linkprio(exten, if_end, mother_exten);
03942          
03943          break;
03944 
03945       case PV_RANDOM:
03946       case PV_IF:
03947          control_statement_count++;
03948          snprintf(new_label,sizeof(new_label),"if-%s-%d", label, control_statement_count);
03949          
03950          if_test = new_prio();
03951          if_end = new_prio();
03952          if_test->type = AEL_IF_CONTROL;
03953          if_end->type = AEL_APPCALL;
03954          if ( p->type == PV_RANDOM )
03955             snprintf(buf1,sizeof(buf1),"$[${RAND(0|99)} < (%s)]",p->u1.str);
03956          else {
03957             char buf[8000];
03958             strcpy(buf,p->u1.str);
03959             substitute_commas(buf);
03960             snprintf(buf1,sizeof(buf1),"$[%s]",buf);
03961          }
03962          
03963          if_test->app = 0;
03964          if_test->appargs = strdup(buf1);
03965          snprintf(buf1,sizeof(buf1),"Finish if-%s-%d", label, control_statement_count);
03966          if_end->app = strdup("NoOp");
03967          if_end->appargs = strdup(buf1);
03968          if_test->origin = p;
03969          
03970          if (p->u3.else_statements) {
03971             if_skip = new_prio();
03972             if_skip->type = AEL_CONTROL1; /* simple goto */
03973             if_skip->goto_true = if_end;
03974             if_test->goto_false = if_skip;;
03975          } else {
03976             if_skip = 0;
03977             if_test->goto_false = if_end;;
03978          }
03979          
03980          /* link & load! */
03981          linkprio(exten, if_test, mother_exten);
03982          
03983          /* now, put the body of the if here */
03984          
03985          gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
03986          
03987          if (p->u3.else_statements) {
03988             linkprio(exten, if_skip, mother_exten);
03989             gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
03990 
03991          }
03992          
03993          linkprio(exten, if_end, mother_exten);
03994          
03995          break;
03996 
03997       case PV_STATEMENTBLOCK:
03998          gen_prios(exten, label, p->u1.list, mother_exten, this_context ); /* recurse into the block */
03999          break;
04000 
04001       case PV_CATCH:
04002          control_statement_count++;
04003          /* generate an extension with name of catch, put all catch stats
04004             into this exten! */
04005          switch_case = new_exten();
04006          if (mother_exten && mother_exten->checked_switch) {
04007             switch_case->has_switch = mother_exten->has_switch;
04008             switch_case->checked_switch = mother_exten->checked_switch;
04009          }
04010          if (exten && exten->checked_switch) {
04011             switch_case->has_switch = exten->has_switch;
04012             switch_case->checked_switch = exten->checked_switch;
04013          }
04014          
04015          switch_case->context = this_context;
04016          linkexten(exten,switch_case);
04017          switch_case->name = strdup(p->u1.str);
04018          snprintf(new_label,sizeof(new_label),"catch-%s-%d",p->u1.str, control_statement_count);
04019          
04020          gen_prios(switch_case, new_label, p->u2.statements,mother_exten,this_context); /* this will link in all the catch body statements here */
04021          if (switch_case->return_needed) {
04022             char buf[2000];
04023             struct ael_priority *np2 = new_prio();
04024             np2->type = AEL_APPCALL;
04025             np2->app = strdup("NoOp");
04026             snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
04027             np2->appargs = strdup(buf);
04028             linkprio(switch_case, np2, mother_exten);
04029             switch_case-> return_target = np2;
04030          }
04031 
04032          break;
04033       default:
04034          break;
04035       }
04036    }
04037 }
04038 
04039 void set_priorities(struct ael_extension *exten)
04040 {
04041    int i;
04042    struct ael_priority *pr;
04043    do {
04044       if (exten->is_switch)
04045          i = 10;
04046       else if (exten->regexten)
04047          i=2;
04048       else
04049          i=1;
04050       
04051       for (pr=exten->plist; pr; pr=pr->next) {
04052          pr->priority_num = i;
04053          
04054          if (!pr->origin || (pr->origin && pr->origin->type != PV_LABEL) ) /* Labels don't show up in the dialplan,
04055                                       but we want them to point to the right
04056                                       priority, which would be the next line
04057                                       after the label; */
04058             i++;
04059       }
04060       
04061       exten = exten->next_exten;
04062    } while ( exten );
04063 }
04064 
04065 void add_extensions(struct ael_extension *exten)
04066 {
04067    struct ael_priority *pr;
04068    char *label=0;
04069    char realext[AST_MAX_EXTENSION];
04070    if (!exten) {
04071       ast_log(LOG_WARNING, "This file is Empty!\n" );
04072       return;
04073    }
04074    do {
04075       struct ael_priority *last = 0;
04076 
04077       memset(realext, '\0', sizeof(realext));
04078       pbx_substitute_variables_helper(NULL, exten->name, realext, sizeof(realext) - 1);
04079       if (exten->hints) {
04080          if (ast_add_extension2(exten->context, 0 /*no replace*/, realext, PRIORITY_HINT, NULL, exten->cidmatch, 
04081                           exten->hints, NULL, ast_free, registrar)) {
04082             ast_log(LOG_WARNING, "Unable to add step at priority 'hint' of extension '%s'\n",
04083                   exten->name);
04084          }
04085       }
04086       
04087       for (pr=exten->plist; pr; pr=pr->next) {
04088          char app[2000];
04089          char appargs[2000];
04090 
04091          /* before we can add the extension, we need to prep the app/appargs;
04092             the CONTROL types need to be done after the priority numbers are calculated.
04093          */
04094          if (pr->type == AEL_LABEL) /* don't try to put labels in the dialplan! */ {
04095             last = pr;
04096             continue;
04097          }
04098          
04099          if (pr->app)
04100             strcpy(app, pr->app);
04101          else
04102             app[0] = 0;
04103          if (pr->appargs )
04104             strcpy(appargs, pr->appargs);
04105          else
04106             appargs[0] = 0;
04107          switch( pr->type ) {
04108          case AEL_APPCALL:
04109             /* easy case. Everything is all set up */
04110             break;
04111             
04112          case AEL_CONTROL1: /* FOR loop, WHILE loop, BREAK, CONTINUE, IF, IFTIME */
04113             /* simple, unconditional goto. */
04114             strcpy(app,"Goto");
04115             if (pr->goto_true->origin && pr->goto_true->origin->type == PV_SWITCH ) {
04116                snprintf(appargs,sizeof(appargs),"%s|%d", pr->goto_true->exten->name, pr->goto_true->priority_num);
04117             } else if (pr->goto_true->origin && pr->goto_true->origin->type == PV_IFTIME && pr->goto_true->origin->u3.else_statements ) {
04118                snprintf(appargs,sizeof(appargs),"%d", pr->goto_true->priority_num+1);
04119             } else
04120                snprintf(appargs,sizeof(appargs),"%d", pr->goto_true->priority_num);
04121             break;
04122             
04123          case AEL_FOR_CONTROL:  /* WHILE loop test, FOR loop test */
04124             strcpy(app,"GotoIf");
04125             snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num);
04126             break;
04127             
04128          case AEL_IF_CONTROL:
04129             strcpy(app,"GotoIf");
04130             if (pr->origin->u3.else_statements )
04131                snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num+1);
04132             else
04133                snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num);
04134             break;
04135 
04136          case AEL_RAND_CONTROL:
04137             strcpy(app,"Random");
04138             snprintf(appargs,sizeof(appargs),"%s:%d", pr->appargs, pr->goto_true->priority_num+1);
04139             break;
04140 
04141          case AEL_IFTIME_CONTROL:
04142             strcpy(app,"GotoIfTime");
04143             snprintf(appargs,sizeof(appargs),"%s?%d", pr->appargs, pr->priority_num+2);
04144             break;
04145 
04146          case AEL_RETURN:
04147             strcpy(app,"Goto");
04148             snprintf(appargs,sizeof(appargs), "%d", exten->return_target->priority_num);
04149             break;
04150             
04151          default:
04152             break;
04153          }
04154          if (last && last->type == AEL_LABEL ) {
04155             label = last->origin->u1.str;
04156          }
04157          else
04158             label = 0;
04159          
04160          if (ast_add_extension2(exten->context, 0 /*no replace*/, realext, pr->priority_num, (label?label:NULL), exten->cidmatch, 
04161                           app, strdup(appargs), ast_free, registrar)) {
04162             ast_log(LOG_WARNING, "Unable to add step at priority '%d' of extension '%s'\n", pr->priority_num, 
04163                   exten->name);
04164          }
04165          last = pr;
04166       }
04167       exten = exten->next_exten;
04168    } while ( exten );
04169 }
04170 
04171 static void attach_exten(struct ael_extension **list, struct ael_extension *newmem)
04172 {
04173    /* travel to the end of the list... */
04174    struct ael_extension *lptr;
04175    if( !*list ) {
04176       *list = newmem;
04177       return;
04178    }
04179    lptr = *list;
04180    
04181    while( lptr->next_exten ) {
04182       lptr = lptr->next_exten;
04183    }
04184    /* lptr should now pointing to the last element in the list; it has a null next_exten pointer */
04185    lptr->next_exten = newmem;
04186 }
04187 
04188 static pval *get_extension_or_contxt(pval *p)
04189 {
04190    while( p && p->type != PV_EXTENSION && p->type != PV_CONTEXT && p->type != PV_MACRO ) {
04191       
04192       p = p->dad;
04193    }
04194    
04195    return p;
04196 }
04197 
04198 static pval *get_contxt(pval *p)
04199 {
04200    while( p && p->type != PV_CONTEXT && p->type != PV_MACRO ) {
04201       
04202       p = p->dad;
04203    }
04204    
04205    return p;
04206 }
04207 
04208 static void fix_gotos_in_extensions(struct ael_extension *exten)
04209 {
04210    struct ael_extension *e;
04211    for(e=exten;e;e=e->next_exten) {
04212 
04213       struct ael_priority *p;
04214       for(p=e->plist;p;p=p->next) {
04215          
04216          if( p->origin && p->origin->type == PV_GOTO && p->origin->u3.goto_target_in_case ) {
04217             
04218             /* fix the extension of the goto target to the actual extension in the post-compiled dialplan */
04219 
04220             pval *target = p->origin->u2.goto_target;
04221             struct ael_extension *z = target->u3.compiled_label;
04222             pval *pv2 = p->origin;
04223             char buf1[500];
04224             char *apparg_save = p->appargs;
04225             
04226             p->appargs = 0;
04227             if (!pv2->u1.list->next) /* just one  -- it won't hurt to repeat the extension */ {
04228                snprintf(buf1,sizeof(buf1),"%s|%s", z->name, pv2->u1.list->u1.str);
04229                p->appargs = strdup(buf1);
04230                
04231             } else if (pv2->u1.list->next && !pv2->u1.list->next->next) /* two */ {
04232                snprintf(buf1,sizeof(buf1),"%s|%s", z->name, pv2->u1.list->next->u1.str);
04233                p->appargs = strdup(buf1);
04234             } else if (pv2->u1.list->next && pv2->u1.list->next->next) {
04235                snprintf(buf1,sizeof(buf1),"%s|%s|%s", pv2->u1.list->u1.str, 
04236                       z->name,
04237                       pv2->u1.list->next->next->u1.str);
04238                p->appargs = strdup(buf1);
04239             }
04240             else
04241                printf("WHAT? The goto doesn't fall into one of three cases for GOTO????\n");
04242             
04243             if( apparg_save ) {
04244                free(apparg_save);
04245             }
04246          }
04247       }
04248    }
04249 }
04250 
04251 
04252 void ast_compile_ael2(struct ast_context **local_contexts, struct pval *root)
04253 {
04254    pval *p,*p2;
04255    struct ast_context *context;
04256    char buf[2000];
04257    struct ael_extension *exten;
04258    struct ael_extension *exten_list = 0;
04259 
04260    for (p=root; p; p=p->next ) { /* do the globals first, so they'll be there
04261                             when we try to eval them */
04262       switch (p->type) {
04263       case PV_GLOBALS:
04264          /* just VARDEC elements */
04265          for (p2=p->u1.list; p2; p2=p2->next) {
04266             char buf2[2000];
04267             snprintf(buf2,sizeof(buf2),"%s=%s", p2->u1.str, p2->u2.val);
04268             pbx_builtin_setvar(NULL, buf2);
04269          }
04270          break;
04271       default:
04272          break;
04273       }
04274    }
04275    
04276    for (p=root; p; p=p->next ) {
04277       pval *lp;
04278       int argc;
04279       
04280       switch (p->type) {
04281       case PV_MACRO:
04282          strcpy(buf,"macro-");
04283          strcat(buf,p->u1.str);
04284          context = ast_context_create(local_contexts, buf, registrar);
04285          
04286          exten = new_exten();
04287          exten->context = context;
04288          exten->name = strdup("s");
04289          argc = 1;
04290          for (lp=p->u2.arglist; lp; lp=lp->next) {
04291             /* for each arg, set up a "Set" command */
04292             struct ael_priority *np2 = new_prio();
04293             np2->type = AEL_APPCALL;
04294             np2->app = strdup("Set");
04295             snprintf(buf,sizeof(buf),"%s=${ARG%d}", lp->u1.str, argc++);
04296             remove_spaces_before_equals(buf);
04297             np2->appargs = strdup(buf);
04298             linkprio(exten, np2, NULL);
04299          }
04300          /* add any includes */
04301          for (p2=p->u3.macro_statements; p2; p2=p2->next) {
04302             pval *p3;
04303             
04304             switch (p2->type) {
04305             case PV_INCLUDES:
04306                for (p3 = p2->u1.list; p3 ;p3=p3->next) {
04307                   if ( p3->u2.arglist ) {
04308                      snprintf(buf,sizeof(buf), "%s|%s|%s|%s|%s", 
04309                             p3->u1.str,
04310                             p3->u2.arglist->u1.str,
04311                             p3->u2.arglist->next->u1.str,
04312                             p3->u2.arglist->next->next->u1.str,
04313                             p3->u2.arglist->next->next->next->u1.str);
04314                      ast_context_add_include2(context, buf, registrar);
04315                   } else
04316                      ast_context_add_include2(context, p3->u1.str, registrar);
04317                }
04318                break;
04319             default:
04320                break;
04321             }
04322          }
04323          /* CONTAINS APPCALLS, CATCH, just like extensions... */
04324          gen_prios(exten, p->u1.str, p->u3.macro_statements, 0, context );
04325          if (exten->return_needed) {
04326             struct ael_priority *np2 = new_prio();
04327             np2->type = AEL_APPCALL;
04328             np2->app = strdup("NoOp");
04329             snprintf(buf,sizeof(buf),"End of Macro %s-%s",p->u1.str, exten->name);
04330             np2->appargs = strdup(buf);
04331             linkprio(exten, np2, NULL);
04332             exten-> return_target = np2;
04333          }
04334          
04335          set_priorities(exten);
04336          attach_exten(&exten_list, exten);
04337          break;
04338          
04339       case PV_GLOBALS:
04340          /* already done */
04341          break;
04342          
04343       case PV_CONTEXT:
04344          context = ast_context_find_or_create(local_contexts, p->u1.str, registrar);
04345          
04346          /* contexts contain: ignorepat, includes, switches, eswitches, extensions,  */
04347          for (p2=p->u2.statements; p2; p2=p2->next) {
04348             pval *p3;
04349             char *s3;
04350             
04351             switch (p2->type) {
04352             case PV_EXTENSION:
04353                exten = new_exten();
04354                exten->name = strdup(p2->u1.str);
04355                exten->context = context;
04356                
04357                if( (s3=strchr(exten->name, '/') ) != 0 )
04358                {
04359                   *s3 = 0;
04360                   exten->cidmatch = s3+1;
04361                }
04362                
04363                if ( p2->u3.hints )
04364                   exten->hints = strdup(p2->u3.hints);
04365                exten->regexten = p2->u4.regexten;
04366                gen_prios(exten, p->u1.str, p2->u2.statements, 0, context );
04367                if (exten->return_needed) {
04368                   struct ael_priority *np2 = new_prio();
04369                   np2->type = AEL_APPCALL;
04370                   np2->app = strdup("NoOp");
04371                   snprintf(buf,sizeof(buf),"End of Extension %s", exten->name);
04372                   np2->appargs = strdup(buf);
04373                   linkprio(exten, np2, NULL);
04374                   exten-> return_target = np2;
04375                }
04376                /* is the last priority in the extension a label? Then add a trailing no-op */
04377                if ( exten->plist_last && exten->plist_last->type == AEL_LABEL ) {
04378                   struct ael_priority *np2 = new_prio();
04379                   np2->type = AEL_APPCALL;
04380                   np2->app = strdup("NoOp");
04381                   snprintf(buf,sizeof(buf),"A NoOp to follow a trailing label %s", exten->plist_last->origin->u1.str);
04382                   np2->appargs = strdup(buf);
04383                   linkprio(exten, np2, NULL);
04384                }
04385 
04386                set_priorities(exten);
04387                attach_exten(&exten_list, exten);
04388                break;
04389                
04390             case PV_IGNOREPAT:
04391                ast_context_add_ignorepat2(context, p2->u1.str, registrar);
04392                break;
04393                
04394             case PV_INCLUDES:
04395                for (p3 = p2->u1.list; p3 ;p3=p3->next) {
04396                   if ( p3->u2.arglist ) {
04397                      snprintf(buf,sizeof(buf), "%s|%s|%s|%s|%s", 
04398                             p3->u1.str,
04399                             p3->u2.arglist->u1.str,
04400                             p3->u2.arglist->next->u1.str,
04401                             p3->u2.arglist->next->next->u1.str,
04402                             p3->u2.arglist->next->next->next->u1.str);
04403                      ast_context_add_include2(context, buf, registrar);
04404                   } else
04405                      ast_context_add_include2(context, p3->u1.str, registrar);
04406                }
04407                break;
04408                
04409             case PV_SWITCHES:
04410                for (p3 = p2->u1.list; p3 ;p3=p3->next) {
04411                   char *c = strchr(p3->u1.str, '/');
04412                   if (c) {
04413                      *c = '\0';
04414                      c++;
04415                   } else
04416                      c = "";
04417 
04418                   ast_context_add_switch2(context, p3->u1.str, c, 0, registrar);
04419                }
04420                break;
04421 
04422             case PV_ESWITCHES:
04423                for (p3 = p2->u1.list; p3 ;p3=p3->next) {
04424                   char *c = strchr(p3->u1.str, '/');
04425                   if (c) {
04426                      *c = '\0';
04427                      c++;
04428                   } else
04429                      c = "";
04430 
04431                   ast_context_add_switch2(context, p3->u1.str, c, 1, registrar);
04432                }
04433                break;
04434             default:
04435                break;
04436             }
04437          }
04438          
04439          break;
04440          
04441       default:
04442          /* huh? what? */
04443          break;
04444          
04445       }
04446    }
04447    /* moved these from being done after a macro or extension were processed,
04448       to after all processing is done, for the sake of fixing gotos to labels inside cases... */
04449    /* I guess this would be considered 2nd pass of compiler now... */
04450    fix_gotos_in_extensions(exten_list); /* find and fix extension ref in gotos to labels that are in case statements */
04451    add_extensions(exten_list);   /* actually makes calls to create priorities in ast_contexts -- feeds dialplan to asterisk */
04452    destroy_extensions(exten_list);  /* all that remains is an empty husk, discard of it as is proper */
04453    
04454 }
04455 
04456 
04457 static int aeldebug = 0;
04458 
04459 /* interface stuff */
04460 
04461 /* if all the below are static, who cares if they are present? */
04462 
04463 static int pbx_load_module(void)
04464 {
04465    int errs=0, sem_err=0, sem_warn=0, sem_note=0;
04466    char *rfilename;
04467    struct ast_context *local_contexts=NULL, *con;
04468    struct pval *parse_tree;
04469 
04470    ast_log(LOG_NOTICE, "Starting AEL load process.\n");
04471    if (config[0] == '/')
04472       rfilename = (char *)config;
04473    else {
04474       rfilename = alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2);
04475       sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, config);
04476    }
04477    ast_log(LOG_NOTICE, "AEL load process: calculated config file name '%s'.\n", rfilename);
04478 
04479    if (access(rfilename,R_OK) != 0) {
04480       ast_log(LOG_NOTICE, "File %s not found; AEL declining load\n", rfilename);
04481       return AST_MODULE_LOAD_DECLINE;
04482    }
04483    
04484    parse_tree = ael2_parse(rfilename, &errs);
04485    ast_log(LOG_NOTICE, "AEL load process: parsed config file name '%s'.\n", rfilename);
04486    ael2_semantic_check(parse_tree, &sem_err, &sem_warn, &sem_note);
04487    if (errs == 0 && sem_err == 0) {
04488       ast_log(LOG_NOTICE, "AEL load process: checked config file name '%s'.\n", rfilename);
04489       ast_compile_ael2(&local_contexts, parse_tree);
04490       ast_log(LOG_NOTICE, "AEL load process: compiled config file name '%s'.\n", rfilename);
04491       
04492       ast_merge_contexts_and_delete(&local_contexts, registrar);
04493       ast_log(LOG_NOTICE, "AEL load process: merged config file name '%s'.\n", rfilename);
04494       for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
04495          ast_context_verify_includes(con);
04496       ast_log(LOG_NOTICE, "AEL load process: verified config file name '%s'.\n", rfilename);
04497    } else {
04498       ast_log(LOG_ERROR, "Sorry, but %d syntax errors and %d semantic errors were detected. It doesn't make sense to compile.\n", errs, sem_err);
04499       destroy_pval(parse_tree); /* free up the memory */
04500       return AST_MODULE_LOAD_DECLINE;
04501    }
04502    destroy_pval(parse_tree); /* free up the memory */
04503    
04504    return AST_MODULE_LOAD_SUCCESS;
04505 }
04506 
04507 /* CLI interface */
04508 static int ael2_debug_read(int fd, int argc, char *argv[])
04509 {
04510    aeldebug |= DEBUG_READ;
04511    return 0;
04512 }
04513 
04514 static int ael2_debug_tokens(int fd, int argc, char *argv[])
04515 {
04516    aeldebug |= DEBUG_TOKENS;
04517    return 0;
04518 }
04519 
04520 static int ael2_debug_macros(int fd, int argc, char *argv[])
04521 {
04522    aeldebug |= DEBUG_MACROS;
04523    return 0;
04524 }
04525 
04526 static int ael2_debug_contexts(int fd, int argc, char *argv[])
04527 {
04528    aeldebug |= DEBUG_CONTEXTS;
04529    return 0;
04530 }
04531 
04532 static int ael2_no_debug(int fd, int argc, char *argv[])
04533 {
04534    aeldebug = 0;
04535    return 0;
04536 }
04537 
04538 static int ael2_reload(int fd, int argc, char *argv[])
04539 {
04540    return (pbx_load_module());
04541 }
04542 
04543 static struct ast_cli_entry cli_ael_no_debug = {
04544    { "ael", "no", "debug", NULL },
04545    ael2_no_debug, NULL,
04546    NULL };
04547 
04548 static struct ast_cli_entry cli_ael[] = {
04549    { { "ael", "reload", NULL },
04550    ael2_reload, "Reload AEL configuration" },
04551 
04552    { { "ael", "debug", "read", NULL },
04553    ael2_debug_read, "Enable AEL read debug (does nothing)" },
04554 
04555    { { "ael", "debug", "tokens", NULL },
04556    ael2_debug_tokens, "Enable AEL tokens debug (does nothing)" },
04557 
04558    { { "ael", "debug", "macros", NULL },
04559    ael2_debug_macros, "Enable AEL macros debug (does nothing)" },
04560 
04561    { { "ael", "debug", "contexts", NULL },
04562    ael2_debug_contexts, "Enable AEL contexts debug (does nothing)" },
04563 
04564    { { "ael", "nodebug", NULL },
04565    ael2_no_debug, "Disable AEL debug messages",
04566    NULL, NULL, &cli_ael_no_debug },
04567 };
04568 
04569 static int unload_module(void)
04570 {
04571    ast_context_destroy(NULL, registrar);
04572    ast_cli_unregister_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
04573    return 0;
04574 }
04575 
04576 static int load_module(void)
04577 {
04578    ast_cli_register_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
04579    return (pbx_load_module());
04580 }
04581 
04582 static int reload(void)
04583 {
04584    return pbx_load_module();
04585 }
04586 
04587 #ifdef STANDALONE_AEL
04588 #define AST_MODULE "ael"
04589 int ael_external_load_module(void);
04590 int ael_external_load_module(void)
04591 {
04592    pbx_load_module();
04593    return 1;
04594 }
04595 #endif
04596 
04597 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Extension Language Compiler",
04598       .load = load_module,
04599       .unload = unload_module,
04600       .reload = reload,
04601           );
04602 
04603 
04604 /* DESTROY the PVAL tree ============================================================================ */
04605 
04606 
04607 
04608 void destroy_pval_item(pval *item)
04609 {
04610    if (item == NULL) {
04611       ast_log(LOG_WARNING, "null item\n");
04612       return;
04613    }
04614 
04615    if (item->filename)
04616       free(item->filename);
04617    
04618    switch (item->type) {
04619    case PV_WORD:
04620       /* fields: item->u1.str == string associated with this (word). */
04621       if (item->u1.str )
04622          free(item->u1.str);
04623       if ( item->u2.arglist )
04624          destroy_pval(item->u2.arglist);
04625       break;
04626       
04627    case PV_MACRO:
04628       /* fields: item->u1.str     == name of macro
04629                  item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
04630                item->u2.arglist->u1.str  == argument
04631                item->u2.arglist->next   == next arg
04632 
04633                item->u3.macro_statements == pval list of statements in macro body.
04634       */
04635       destroy_pval(item->u2.arglist);
04636       if (item->u1.str )
04637          free(item->u1.str);
04638       destroy_pval(item->u3.macro_statements);
04639       break;
04640          
04641    case PV_CONTEXT:
04642       /* fields: item->u1.str     == name of context
04643                  item->u2.statements == pval list of statements in context body
04644                item->u3.abstract == int 1 if an abstract keyword were present
04645       */
04646       if (item->u1.str)
04647          free(item->u1.str);
04648       destroy_pval(item->u2.statements);
04649       break;
04650          
04651    case PV_MACRO_CALL:
04652       /* fields: item->u1.str     == name of macro to call
04653                  item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
04654                item->u2.arglist->u1.str  == argument
04655                item->u2.arglist->next   == next arg
04656       */
04657       if (item->u1.str)
04658          free(item->u1.str);
04659       destroy_pval(item->u2.arglist);
04660       break;
04661          
04662    case PV_APPLICATION_CALL:
04663       /* fields: item->u1.str     == name of application to call
04664                  item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
04665                item->u2.arglist->u1.str  == argument
04666                item->u2.arglist->next   == next arg
04667       */
04668       if (item->u1.str)
04669          free(item->u1.str);
04670       destroy_pval(item->u2.arglist);
04671       break;
04672          
04673    case PV_CASE:
04674       /* fields: item->u1.str     == value of case
04675                  item->u2.statements == pval list of statements under the case
04676       */
04677       if (item->u1.str)
04678          free(item->u1.str);
04679       destroy_pval(item->u2.statements);
04680       break;
04681          
04682    case PV_PATTERN:
04683       /* fields: item->u1.str     == value of case
04684                  item->u2.statements == pval list of statements under the case
04685       */
04686       if (item->u1.str)
04687          free(item->u1.str);
04688       destroy_pval(item->u2.statements);
04689       break;
04690          
04691    case PV_DEFAULT:
04692       /* fields: 
04693                  item->u2.statements == pval list of statements under the case
04694       */
04695       destroy_pval(item->u2.statements);
04696       break;
04697          
04698    case PV_CATCH:
04699       /* fields: item->u1.str     == name of extension to catch
04700                  item->u2.statements == pval list of statements in context body
04701       */
04702       if (item->u1.str)
04703          free(item->u1.str);
04704       destroy_pval(item->u2.statements);
04705       break;
04706          
04707    case PV_SWITCHES:
04708       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
04709       */
04710       destroy_pval(item->u1.list);
04711       break;
04712          
04713    case PV_ESWITCHES:
04714       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
04715       */
04716       destroy_pval(item->u1.list);
04717       break;
04718          
04719    case PV_INCLUDES:
04720       /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
04721                  item->u2.arglist  == pval list of 4 PV_WORD elements for time values
04722       */
04723       destroy_pval(item->u1.list);
04724       break;
04725          
04726    case PV_STATEMENTBLOCK:
04727       /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
04728       */
04729       destroy_pval(item->u1.list);
04730       break;
04731          
04732    case PV_VARDEC:
04733       /* fields: item->u1.str     == variable name
04734                  item->u2.val     == variable value to assign
04735       */
04736       if (item->u1.str)
04737          free(item->u1.str);
04738       if (item->u2.val)
04739          free(item->u2.val);
04740       break;
04741          
04742    case PV_GOTO:
04743       /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
04744                  item->u1.list->u1.str  == where the data on a PV_WORD will always be.
04745       */
04746       
04747       destroy_pval(item->u1.list);
04748       break;
04749          
04750    case PV_LABEL:
04751       /* fields: item->u1.str     == label name
04752       */
04753       if (item->u1.str)
04754          free(item->u1.str);
04755       break;
04756          
04757    case PV_FOR:
04758       /* fields: item->u1.for_init     == a string containing the initalizer
04759                  item->u2.for_test     == a string containing the loop test
04760                  item->u3.for_inc      == a string containing the loop increment
04761 
04762                item->u4.for_statements == a pval list of statements in the for ()
04763       */
04764       if (item->u1.for_init)
04765          free(item->u1.for_init);
04766       if (item->u2.for_test)
04767          free(item->u2.for_test);
04768       if (item->u3.for_inc)
04769          free(item->u3.for_inc);
04770       destroy_pval(item->u4.for_statements);
04771       break;
04772          
04773    case PV_WHILE:
04774       /* fields: item->u1.str        == the while conditional, as supplied by user
04775 
04776                item->u2.statements == a pval list of statements in the while ()
04777       */
04778       if (item->u1.str)
04779          free(item->u1.str);
04780       destroy_pval(item->u2.statements);
04781       break;
04782          
04783    case PV_BREAK:
04784       /* fields: none
04785       */
04786       break;
04787          
04788    case PV_RETURN:
04789       /* fields: none
04790       */
04791       break;
04792          
04793    case PV_CONTINUE:
04794       /* fields: none
04795       */
04796       break;
04797          
04798    case PV_IFTIME:
04799       /* fields: item->u1.list        == the 4 time values, in PV_WORD structs, linked list
04800 
04801                item->u2.statements == a pval list of statements in the if ()
04802                item->u3.else_statements == a pval list of statements in the else
04803                                     (could be zero)
04804       */
04805       destroy_pval(item->u1.list);
04806       destroy_pval(item->u2.statements);
04807       if (item->u3.else_statements) {
04808          destroy_pval(item->u3.else_statements);
04809       }
04810       break;
04811          
04812    case PV_RANDOM:
04813       /* fields: item->u1.str        == the random percentage, as supplied by user
04814 
04815                item->u2.statements == a pval list of statements in the true part ()
04816                item->u3.else_statements == a pval list of statements in the else
04817                                     (could be zero)
04818       fall thru to If */
04819    case PV_IF:
04820       /* fields: item->u1.str        == the if conditional, as supplied by user
04821 
04822                item->u2.statements == a pval list of statements in the if ()
04823                item->u3.else_statements == a pval list of statements in the else
04824                                     (could be zero)
04825       */
04826       if (item->u1.str)
04827          free(item->u1.str);
04828       destroy_pval(item->u2.statements);
04829       if (item->u3.else_statements) {
04830          destroy_pval(item->u3.else_statements);
04831       }
04832       break;
04833          
04834    case PV_SWITCH:
04835       /* fields: item->u1.str        == the switch expression
04836 
04837                item->u2.statements == a pval list of statements in the switch, 
04838                                     (will be case statements, most likely!)
04839       */
04840       if (item->u1.str)
04841          free(item->u1.str);
04842       destroy_pval(item->u2.statements);
04843       break;
04844          
04845    case PV_EXTENSION:
04846       /* fields: item->u1.str        == the extension name, label, whatever it's called
04847 
04848                item->u2.statements == a pval list of statements in the extension
04849                item->u3.hints      == a char * hint argument
04850                item->u4.regexten   == an int boolean. non-zero says that regexten was specified
04851       */
04852       if (item->u1.str)
04853          free(item->u1.str);
04854       if (item->u3.hints)
04855          free(item->u3.hints);
04856       destroy_pval(item->u2.statements);
04857       break;
04858          
04859    case PV_IGNOREPAT:
04860       /* fields: item->u1.str        == the ignorepat data
04861       */
04862       if (item->u1.str)
04863          free(item->u1.str);
04864       break;
04865          
04866    case PV_GLOBALS:
04867       /* fields: item->u1.statements     == pval list of statements, usually vardecs
04868       */
04869       destroy_pval(item->u1.statements);
04870       break;
04871    }
04872    free(item);
04873 }
04874 
04875 void destroy_pval(pval *item) 
04876 {
04877    pval *i,*nxt;
04878    
04879    for (i=item; i; i=nxt) {
04880       nxt = i->next;
04881       
04882       destroy_pval_item(i);
04883    }
04884 }
04885 
04886 #ifdef AAL_ARGCHECK
04887 static char *ael_funclist[] =
04888 {
04889    "AGENT",
04890    "ARRAY",
04891    "BASE64_DECODE",
04892    "BASE64_ENCODE",
04893    "CALLERID",
04894    "CDR",
04895    "CHANNEL",
04896    "CHECKSIPDOMAIN",
04897    "CHECK_MD5",
04898    "CURL",
04899    "CUT",
04900    "DB",
04901    "DB_EXISTS",
04902    "DUNDILOOKUP",
04903    "ENUMLOOKUP",
04904    "ENV",
04905    "EVAL",
04906    "EXISTS",
04907    "FIELDQTY",
04908    "FILTER",
04909    "GROUP",
04910    "GROUP_COUNT",
04911    "GROUP_LIST",
04912    "GROUP_MATCH_COUNT",
04913    "IAXPEER",
04914    "IF",
04915    "IFTIME",
04916    "ISNULL",
04917    "KEYPADHASH",
04918    "LANGUAGE",
04919    "LEN",
04920    "MATH",
04921    "MD5",
04922    "MUSICCLASS",
04923    "QUEUEAGENTCOUNT",
04924    "QUEUE_MEMBER_COUNT",
04925    "QUEUE_MEMBER_LIST",
04926    "QUOTE",
04927    "RAND",
04928    "REGEX",
04929    "SET",
04930    "SHA1",
04931    "SIPCHANINFO",
04932    "SIPPEER",
04933    "SIP_HEADER",
04934    "SORT",
04935    "STAT",
04936    "STRFTIME",
04937    "STRPTIME",
04938    "TIMEOUT",
04939    "TXTCIDNAME",
04940    "URIDECODE",
04941    "URIENCODE",
04942    "VMCOUNT"
04943 };
04944 
04945 
04946 int ael_is_funcname(char *name)
04947 {
04948    int s,t;
04949    t = sizeof(ael_funclist)/sizeof(char*);
04950    s = 0;
04951    while ((s < t) && strcasecmp(name, ael_funclist[s])) 
04952       s++;
04953    if ( s < t )
04954       return 1;
04955    else
04956       return 0;
04957 }
04958 #endif    

Generated on Fri Jul 3 20:35:39 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9