Skip to content
Snippets Groups Projects
extconf.c 181 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	
    	set_fn(fn, sizeof(fn), 0, configfile); /* just set fn to absolute ver of configfile */
    
    #ifdef __CYGWIN__	
    	if ((f = fopen(fn, "w+"))) {
    #else
    	if ((f = fopen(fn, "w"))) {
    #endif	    
    		if (option_verbose > 1)
    			ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
    
    		fclose(f);
            
    		/* from here out, we open each involved file and concat the stuff we need to add to the end and immediately close... */
    		/* since each var, cat, and associated comments can come from any file, we have to be 
    		   mobile, and open each file, print, and close it on an entry-by-entry basis */
    		
    
    			set_fn(fn, sizeof(fn), cat->file, configfile);
    			f = fopen(fn, "a");
    			if (!f)
    
    				ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno));
    				return -1;
    			}
    			
    			/* dump any includes that happen before this category header */
    			for (incl=cfg->includes; incl; incl = incl->next) {
    				if (strcmp(incl->include_location_file, cat->file) == 0){
    					if (cat->lineno > incl->include_location_lineno && !incl->output) {
    						if (incl->exec)
    							fprintf(f,"#exec \"%s\"\n", incl->exec_file);
    						else
    							fprintf(f,"#include \"%s\"\n", incl->included_file);
    						incl->output = 1;
    					}
    				}
    			}
                
    			/* Dump section with any appropriate comment */
    			for (cmt = cat->precomments; cmt; cmt=cmt->next) {
    
    				if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
    					fprintf(f,"%s", cmt->cmt);
    			}
    			if (!cat->precomments)
    				fprintf(f,"\n");
    			fprintf(f, "[%s]", cat->name);
    
    			for(cmt = cat->sameline; cmt; cmt=cmt->next) {
    
    				fprintf(f,"%s", cmt->cmt);
    			}
    			if (!cat->sameline)
    				fprintf(f,"\n");
    
    				set_fn(fn, sizeof(fn), var->file, configfile);
    				f = fopen(fn, "a");
    				if (!f)
    
    					ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno));
    					return -1;
    				}
                    
    				/* dump any includes that happen before this category header */
    				for (incl=cfg->includes; incl; incl = incl->next) {
    					if (strcmp(incl->include_location_file, var->file) == 0){
    						if (var->lineno > incl->include_location_lineno && !incl->output) {
    							if (incl->exec)
    								fprintf(f,"#exec \"%s\"\n", incl->exec_file);
    							else
    								fprintf(f,"#include \"%s\"\n", incl->included_file);
    							incl->output = 1;
    						}
    					}
    				}
                    
    				for (cmt = var->precomments; cmt; cmt=cmt->next) {
    
    					if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
    						fprintf(f,"%s", cmt->cmt);
    				}
    				if (var->sameline) 
    					fprintf(f, "%s %s %s  %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
    				else	
    					fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
    				if (var->blanklines) {
    					blanklines = var->blanklines;
    					while (blanklines--)
    						fprintf(f, "\n");
    				}
    
    				var = var->next;
    			}
    			cat = cat->next;
    		}
    		if ((option_verbose > 1) && !option_debug)
    			ast_verbose("Saved\n");
    	} else {
    		if (option_debug)
    			ast_log(LOG_DEBUG, "Unable to open for writing: %s\n", fn);
    		if (option_verbose > 1)
    			ast_verbose(VERBOSE_PREFIX_2 "Unable to write (%s)", strerror(errno));
    		return -1;
    	}
    
    
    	/* Now, for files with trailing #include/#exec statements,
    	   we have to make sure every entry is output */
    	
    	for (incl=cfg->includes; incl; incl = incl->next) {
    		if (!incl->output) {
    			/* open the respective file */
    			set_fn(fn, sizeof(fn), incl->include_location_file, configfile);
    			f = fopen(fn, "a");
    			if (!f)
    			{
    				ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno));
    				return -1;
    			}
                
    			/* output the respective include */
    			if (incl->exec)
    				fprintf(f,"#exec \"%s\"\n", incl->exec_file);
    			else
    				fprintf(f,"#include \"%s\"\n", incl->included_file);
    			fclose(f);
    			incl->output = 1;
    		}
    	}
    	
    
    	return 0;
    }
    
    /* ================ the Line ========================================
       above this line, you have what you need to load a config file,
       and below it, you have what you need to process the extensions.conf
       file into the context/exten/prio stuff. They are both in one file
       to make things simpler */
    
    static struct ast_context *local_contexts = NULL;
    static struct ast_context *contexts = NULL;
    struct ast_context;
    struct ast_app;
    #ifdef LOW_MEMORY
    #define EXT_DATA_SIZE 256
    #else
    #define EXT_DATA_SIZE 8192
    #endif
    /*!
     * When looking up extensions, we can have different requests
     * identified by the 'action' argument, as follows.
     * Note that the coding is such that the low 4 bits are the
     * third argument to extension_match_core.
     */
    enum ext_match_t {
    	E_MATCHMORE = 	0x00,	/* extension can match but only with more 'digits' */
    	E_CANMATCH =	0x01,	/* extension can match with or without more 'digits' */
    	E_MATCH =	0x02,	/* extension is an exact match */
    	E_MATCH_MASK =	0x03,	/* mask for the argument to extension_match_core() */
    	E_SPAWN =	0x12,	/* want to spawn an extension. Requires exact match */
    	E_FINDLABEL =	0x22	/* returns the priority for a given label. Requires exact match */
    };
    
    #ifdef NOT_ANYMORE
    static AST_RWLIST_HEAD_STATIC(switches, ast_switch);
    #endif
    
    #define SWITCH_DATA_LENGTH 256
    
    static const char *ast_get_extension_app(struct ast_exten *e)
    {
    	return e ? e->app : NULL;
    }
    
    static const char *ast_get_extension_name(struct ast_exten *exten)
    {
    	return exten ? exten->exten : NULL;
    }
    
    static AST_RWLIST_HEAD_STATIC(hints, ast_hint);
    
    /*! \brief  ast_change_hint: Change hint for an extension */
    static int ast_change_hint(struct ast_exten *oe, struct ast_exten *ne)
    {
    	struct ast_hint *hint;
    	int res = -1;
    
    	AST_RWLIST_TRAVERSE(&hints, hint, list) {
    		if (hint->exten == oe) {
    	    		hint->exten = ne;
    			res = 0;
    			break;
    		}
    	}
    
    	return res;
    }
    
    /*! \brief  ast_add_hint: Add hint to hint list, check initial extension state */
    static int ast_add_hint(struct ast_exten *e)
    {
    	struct ast_hint *hint;
    
    	if (!e)
    		return -1;
    
    
    	/* Search if hint exists, do nothing */
    	AST_RWLIST_TRAVERSE(&hints, hint, list) {
    		if (hint->exten == e) {
    			if (option_debug > 1)
    				ast_log(LOG_DEBUG, "HINTS: Not re-adding existing hint %s: %s\n", ast_get_extension_name(e), ast_get_extension_app(e));
    			return -1;
    		}
    	}
    
    	if (option_debug > 1)
    		ast_log(LOG_DEBUG, "HINTS: Adding hint %s: %s\n", ast_get_extension_name(e), ast_get_extension_app(e));
    
    	if (!(hint = ast_calloc(1, sizeof(*hint)))) {
    		return -1;
    	}
    	/* Initialize and insert new item at the top */
    	hint->exten = e;
    	AST_RWLIST_INSERT_HEAD(&hints, hint, list);
    
    	return 0;
    }
    
    /*! \brief add the extension in the priority chain.
     * returns 0 on success, -1 on failure
     */
    static int add_pri(struct ast_context *con, struct ast_exten *tmp,
    	struct ast_exten *el, struct ast_exten *e, int replace)
    {
    	struct ast_exten *ep;
    
    	for (ep = NULL; e ; ep = e, e = e->peer) {
    		if (e->priority >= tmp->priority)
    			break;
    	}
    	if (!e) {	/* go at the end, and ep is surely set because the list is not empty */
    		ep->peer = tmp;
    		return 0;	/* success */
    	}
    	if (e->priority == tmp->priority) {
    		/* Can't have something exactly the same.  Is this a
    		   replacement?  If so, replace, otherwise, bonk. */
    		if (!replace) {
    			ast_log(LOG_WARNING, "Unable to register extension '%s', priority %d in '%s', already in use\n", tmp->exten, tmp->priority, con->name);
    			tmp->datad(tmp->data);
    			free(tmp);
    			return -1;
    		}
    		/* we are replacing e, so copy the link fields and then update
    		 * whoever pointed to e to point to us
    		 */
    		tmp->next = e->next;	/* not meaningful if we are not first in the peer list */
    		tmp->peer = e->peer;	/* always meaningful */
    		if (ep)			/* We're in the peer list, just insert ourselves */
    			ep->peer = tmp;
    		else if (el)		/* We're the first extension. Take over e's functions */
    			el->next = tmp;
    		else			/* We're the very first extension.  */
    			con->root = tmp;
    		if (tmp->priority == PRIORITY_HINT)
    			ast_change_hint(e,tmp);
    		/* Destroy the old one */
    		e->datad(e->data);
    		free(e);
    	} else {	/* Slip ourselves in just before e */
    		tmp->peer = e;
    		tmp->next = e->next;	/* extension chain, or NULL if e is not the first extension */
    		if (ep)			/* Easy enough, we're just in the peer list */
    			ep->peer = tmp;
    		else {			/* we are the first in some peer list, so link in the ext list */
    			if (el)
    				el->next = tmp;	/* in the middle... */
    			else
    				con->root = tmp; /* ... or at the head */
    			e->next = NULL;	/* e is no more at the head, so e->next must be reset */
    		}
    		/* And immediately return success. */
    		if (tmp->priority == PRIORITY_HINT)
    			 ast_add_hint(tmp);
    	}
    	return 0;
    }
    
    /*! \brief  ast_remove_hint: Remove hint from extension */
    static int ast_remove_hint(struct ast_exten *e)
    {
    	/* Cleanup the Notifys if hint is removed */
    	struct ast_hint *hint;
    	struct ast_state_cb *cblist, *cbprev;
    	int res = -1;
    
    	if (!e)
    		return -1;
    
    	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&hints, hint, list) {
    		if (hint->exten == e) {
    			cbprev = NULL;
    			cblist = hint->callbacks;
    			while (cblist) {
    				/* Notify with -1 and remove all callbacks */
    				cbprev = cblist;
    				cblist = cblist->next;
    				free(cbprev);
    			}
    			hint->callbacks = NULL;
    			AST_RWLIST_REMOVE_CURRENT(&hints, list);
    			free(hint);
    	   		res = 0;
    			break;
    		}
    	}
    	AST_RWLIST_TRAVERSE_SAFE_END
    
    	return res;
    }
    
    static void destroy_exten(struct ast_exten *e)
    {
    	if (e->priority == PRIORITY_HINT)
    		ast_remove_hint(e);
    
    	if (e->datad)
    		e->datad(e->data);
    	free(e);
    }
    
    char *days[] =
    {
    	"sun",
    	"mon",
    	"tue",
    	"wed",
    	"thu",
    	"fri",
    	"sat",
    	NULL,
    };
    
    char *months[] =
    {
    	"jan",
    	"feb",
    	"mar",
    	"apr",
    	"may",
    	"jun",
    	"jul",
    	"aug",
    	"sep",
    	"oct",
    	"nov",
    	"dec",
    	NULL,
    };
    
    
    int ast_build_timing(struct ast_timing *i, const char *info_in);
    
    
    int ast_build_timing(struct ast_timing *i, const char *info_in)
    
    	char *info_save, *info;
    	int j, num_fields, last_sep = -1;
    
    	if (ast_strlen_zero(info_in)) {
    
    	/* make a copy just in case we were passed a static string */
    
    	info_save = info = ast_strdupa(info_in);
    
    	/* count the number of fields in the timespec */
    	for (j = 0, num_fields = 1; info[j] != '\0'; j++) {
    		if (info[j] == ',') {
    			last_sep = j;
    			num_fields++;
    		}
    	}
    
    	/* save the timezone, if it is specified */
    	if (num_fields == 5) {
    		i->timezone = ast_strdup(info + last_sep + 1);
    	} else {
    		i->timezone = NULL;
    	}
    
    
    	/* Assume everything except time */
    	i->monthmask = 0xfff;	/* 12 bits */
    	i->daymask = 0x7fffffffU; /* 31 bits */
    	i->dowmask = 0x7f; /* 7 bits */
    	/* on each call, use strsep() to move info to the next argument */
    
    	get_timerange(i, strsep(&info, "|,"));
    
    		i->dowmask = get_range(strsep(&info, "|,"), 7, days, "day of week");
    
    		i->daymask = get_range(strsep(&info, "|,"), 31, NULL, "day");
    
    		i->monthmask = get_range(strsep(&info, "|,"), 12, months, "month");
    
    	return 1;
    }
    
    /*!
     * \brief helper functions to sort extensions and patterns in the desired way,
     * so that more specific patterns appear first.
     *
     * ext_cmp1 compares individual characters (or sets of), returning
     * an int where bits 0-7 are the ASCII code of the first char in the set,
     * while bit 8-15 are the cardinality of the set minus 1.
     * This way more specific patterns (smaller cardinality) appear first.
     * Wildcards have a special value, so that we can directly compare them to
     * sets by subtracting the two values. In particular:
     * 	0x000xx		one character, xx
     * 	0x0yyxx		yy character set starting with xx
     * 	0x10000		'.' (one or more of anything)
     * 	0x20000		'!' (zero or more of anything)
     * 	0x30000		NUL (end of string)
     * 	0x40000		error in set.
     * The pointer to the string is advanced according to needs.
     * NOTES:
     *	1. the empty set is equivalent to NUL.
     *	2. given that a full set has always 0 as the first element,
     *	   we could encode the special cases as 0xffXX where XX
     *	   is 1, 2, 3, 4 as used above.
     */
    static int ext_cmp1(const char **p)
    {
    	uint32_t chars[8];
    	int c, cmin = 0xff, count = 0;
    	const char *end;
    
    	/* load, sign extend and advance pointer until we find
    	 * a valid character.
    	 */
    	while ( (c = *(*p)++) && (c == ' ' || c == '-') )
    		;	/* ignore some characters */
    
    	/* always return unless we have a set of chars */
    	switch (c) {
    	default:	/* ordinary character */
    		return 0x0000 | (c & 0xff);
    
    	case 'N':	/* 2..9 */
    		return 0x0700 | '2' ;
    
    	case 'X':	/* 0..9 */
    		return 0x0900 | '0';
    
    	case 'Z':	/* 1..9 */
    		return 0x0800 | '1';
    
    	case '.':	/* wildcard */
    		return 0x10000;
    
    	case '!':	/* earlymatch */
    		return 0x20000;	/* less specific than NULL */
    
    	case '\0':	/* empty string */
    		*p = NULL;
    		return 0x30000;
    
    	case '[':	/* pattern */
    		break;
    	}
    	/* locate end of set */
    	end = strchr(*p, ']');	
    
    	if (end == NULL) {
    		ast_log(LOG_WARNING, "Wrong usage of [] in the extension\n");
    		return 0x40000;	/* XXX make this entry go last... */
    	}
    
    
    Steve Murphy's avatar
    Steve Murphy committed
    	memset(chars, '\0', sizeof(chars));	/* clear all chars in the set */
    
    	for (; *p < end  ; (*p)++) {
    		unsigned char c1, c2;	/* first-last char in range */
    		c1 = (unsigned char)((*p)[0]);
    		if (*p + 2 < end && (*p)[1] == '-') { /* this is a range */
    			c2 = (unsigned char)((*p)[2]);
    			*p += 2;	/* skip a total of 3 chars */
    		} else			/* individual character */
    			c2 = c1;
    		if (c1 < cmin)
    			cmin = c1;
    		for (; c1 <= c2; c1++) {
    			uint32_t mask = 1 << (c1 % 32);
    			if ( (chars[ c1 / 32 ] & mask) == 0)
    				count += 0x100;
    			chars[ c1 / 32 ] |= mask;
    		}
    	}
    	(*p)++;
    	return count == 0 ? 0x30000 : (count | cmin);
    }
    
    /*!
     * \brief the full routine to compare extensions in rules.
     */
    static int ext_cmp(const char *a, const char *b)
    {
    	/* make sure non-patterns come first.
    	 * If a is not a pattern, it either comes first or
    	 * we use strcmp to compare the strings.
    	 */
    	int ret = 0;
    
    	if (a[0] != '_')
    		return (b[0] == '_') ? -1 : strcmp(a, b);
    
    	/* Now we know a is a pattern; if b is not, a comes first */
    	if (b[0] != '_')
    		return 1;
    #if 0	/* old mode for ext matching */
    	return strcmp(a, b);
    #endif
    	/* ok we need full pattern sorting routine */
    	while (!ret && a && b)
    		ret = ext_cmp1(&a) - ext_cmp1(&b);
    	if (ret == 0)
    		return 0;
    	else
    		return (ret > 0) ? 1 : -1;
    }
    
    /*! \brief copy a string skipping whitespace */
    static int ext_strncpy(char *dst, const char *src, int len)
    {
    	int count=0;
    
    	while (*src && (count < len - 1)) {
    		switch(*src) {
    		case ' ':
    			/*	otherwise exten => [a-b],1,... doesn't work */
    			/*		case '-': */
    			/* Ignore */
    			break;
    		default:
    			*dst = *src;
    			dst++;
    		}
    		src++;
    		count++;
    	}
    	*dst = '\0';
    
    	return count;
    }
    
    /*
     * Wrapper around _extension_match_core() to do performance measurement
     * using the profiling code.
     */
    
    int ast_check_timing(const struct ast_timing *i);
    
    
    int ast_check_timing(const struct ast_timing *i)
    
    	/* sorry, but this feature will NOT be available
    	   in the standalone version */
    	return 0;
    
    4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000
    }
    
    #ifdef NOT_ANYMORE
    static struct ast_switch *pbx_findswitch(const char *sw)
    {
    	struct ast_switch *asw;
    
    	AST_RWLIST_TRAVERSE(&switches, asw, list) {
    		if (!strcasecmp(asw->name, sw))
    			break;
    	}
    
    	return asw;
    }
    #endif
    
    
    static struct ast_context *ast_walk_contexts(struct ast_context *con);
    
    static struct ast_context *ast_walk_contexts(struct ast_context *con)
    {
    	return con ? con->next : contexts;
    }
    
    struct ast_context *localized_walk_contexts(struct ast_context *con);
    struct ast_context *localized_walk_contexts(struct ast_context *con)
    {
    	return ast_walk_contexts(con);
    }
    
    
    
    static struct ast_exten *ast_walk_context_extensions(struct ast_context *con,
    											  struct ast_exten *exten);
    
    static struct ast_exten *ast_walk_context_extensions(struct ast_context *con,
    	struct ast_exten *exten)
    {
    	if (!exten)
    		return con ? con->root : NULL;
    	else
    		return exten->next;
    }
    
    struct ast_exten *localized_walk_context_extensions(struct ast_context *con,
    													struct ast_exten *exten);
    struct ast_exten *localized_walk_context_extensions(struct ast_context *con,
    													struct ast_exten *exten)
    {
    	return ast_walk_context_extensions(con,exten);
    }
    
    
    static struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten,
    												struct ast_exten *priority);
    
    static struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten,
    	struct ast_exten *priority)
    {
    	return priority ? priority->peer : exten;
    }
    
    struct ast_exten *localized_walk_extension_priorities(struct ast_exten *exten,
    													  struct ast_exten *priority);
    struct ast_exten *localized_walk_extension_priorities(struct ast_exten *exten,
    													  struct ast_exten *priority)
    {
    	return ast_walk_extension_priorities(exten, priority);
    }
    
    
    
    static struct ast_include *ast_walk_context_includes(struct ast_context *con,
    											  struct ast_include *inc);
    
    static struct ast_include *ast_walk_context_includes(struct ast_context *con,
    	struct ast_include *inc)
    {
    	if (!inc)
    		return con ? con->includes : NULL;
    	else
    		return inc->next;
    }
    
    struct ast_include *localized_walk_context_includes(struct ast_context *con,
    													struct ast_include *inc);
    struct ast_include *localized_walk_context_includes(struct ast_context *con,
    													struct ast_include *inc)
    {
    	return ast_walk_context_includes(con, inc);
    }
    
    
    static struct ast_sw *ast_walk_context_switches(struct ast_context *con,
    													 struct ast_sw *sw);
    
    static struct ast_sw *ast_walk_context_switches(struct ast_context *con,
    													 struct ast_sw *sw)
    {
    	if (!sw)
    		return con ? AST_LIST_FIRST(&con->alts) : NULL;
    	else
    		return AST_LIST_NEXT(sw, list);
    }
    
    struct ast_sw *localized_walk_context_switches(struct ast_context *con,
    													struct ast_sw *sw);
    struct ast_sw *localized_walk_context_switches(struct ast_context *con,
    													struct ast_sw *sw)
    {
    	return ast_walk_context_switches(con, sw);
    }
    
    
    static struct ast_context *ast_context_find(const char *name);
    
    static struct ast_context *ast_context_find(const char *name)
    {
    	struct ast_context *tmp = NULL;
    	while ( (tmp = ast_walk_contexts(tmp)) ) {
    		if (!name || !strcasecmp(name, tmp->name))
    			break;
    	}
    	return tmp;
    }
    
    /* request and result for pbx_find_extension */
    struct pbx_find_info {
    #if 0
    	const char *context;
    	const char *exten;
    	int priority;
    #endif
    
    	char *incstack[AST_PBX_MAX_STACK];      /* filled during the search */
    	int stacklen;                   /* modified during the search */
    	int status;                     /* set on return */
    	struct ast_switch *swo;         /* set on return */
    	const char *data;               /* set on return */
    	const char *foundcontext;       /* set on return */
    };
    
    /*
     * Internal function for ast_extension_{match|close}
     * return 0 on no-match, 1 on match, 2 on early match.
     * mode is as follows:
     *	E_MATCH		success only on exact match
     *	E_MATCHMORE	success only on partial match (i.e. leftover digits in pattern)
     *	E_CANMATCH	either of the above.
     */
    
    static int _extension_match_core(const char *pattern, const char *data, enum ext_match_t mode)
    {
    	mode &= E_MATCH_MASK;	/* only consider the relevant bits */
    
    	if ( (mode == E_MATCH) && (pattern[0] == '_') && (strcasecmp(pattern,data)==0) ) /* note: if this test is left out, then _x. will not match _x. !!! */
    		return 1;
    
    	if (pattern[0] != '_') { /* not a pattern, try exact or partial match */
    		int ld = strlen(data), lp = strlen(pattern);
    
    		if (lp < ld)		/* pattern too short, cannot match */
    			return 0;
    		/* depending on the mode, accept full or partial match or both */
    		if (mode == E_MATCH)
    			return !strcmp(pattern, data); /* 1 on match, 0 on fail */
    		if (ld == 0 || !strncasecmp(pattern, data, ld)) /* partial or full match */
    			return (mode == E_MATCHMORE) ? lp > ld : 1; /* XXX should consider '!' and '/' ? */
    		else
    			return 0;
    	}
    	pattern++; /* skip leading _ */
    	/*
    	 * XXX below we stop at '/' which is a separator for the CID info. However we should
    	 * not store '/' in the pattern at all. When we insure it, we can remove the checks.
    	 */
    	while (*data && *pattern && *pattern != '/') {
    		const char *end;
    
    		if (*data == '-') { /* skip '-' in data (just a separator) */
    			data++;
    			continue;
    		}
    		switch (toupper(*pattern)) {
    		case '[':	/* a range */
    			end = strchr(pattern+1, ']'); /* XXX should deal with escapes ? */
    			if (end == NULL) {
    				ast_log(LOG_WARNING, "Wrong usage of [] in the extension\n");
    				return 0;	/* unconditional failure */
    			}
    			for (pattern++; pattern != end; pattern++) {
    				if (pattern+2 < end && pattern[1] == '-') { /* this is a range */
    					if (*data >= pattern[0] && *data <= pattern[2])
    						break;	/* match found */
    					else {
    						pattern += 2; /* skip a total of 3 chars */
    						continue;
    					}
    				} else if (*data == pattern[0])
    					break;	/* match found */
    			}
    			if (pattern == end)
    				return 0;
    			pattern = end;	/* skip and continue */
    			break;
    		case 'N':
    			if (*data < '2' || *data > '9')
    				return 0;
    			break;
    		case 'X':
    			if (*data < '0' || *data > '9')
    				return 0;
    			break;
    		case 'Z':
    			if (*data < '1' || *data > '9')
    				return 0;
    			break;
    		case '.':	/* Must match, even with more digits */
    			return 1;
    		case '!':	/* Early match */
    			return 2;
    		case ' ':
    		case '-':	/* Ignore these in patterns */
    			data--; /* compensate the final data++ */
    			break;
    		default:
    			if (*data != *pattern)
    				return 0;
    		}
    		data++;
    		pattern++;
    	}
    	if (*data)			/* data longer than pattern, no match */
    		return 0;
    	/*
    	 * match so far, but ran off the end of the data.
    	 * Depending on what is next, determine match or not.
    	 */
    	if (*pattern == '\0' || *pattern == '/')	/* exact match */
    		return (mode == E_MATCHMORE) ? 0 : 1;	/* this is a failure for E_MATCHMORE */
    	else if (*pattern == '!')			/* early match */
    		return 2;
    	else						/* partial match */
    		return (mode == E_MATCH) ? 0 : 1;	/* this is a failure for E_MATCH */
    }
    
    static int extension_match_core(const char *pattern, const char *data, enum ext_match_t mode)
    {
    	int i;
    	i = _extension_match_core(pattern, data, mode);
    	return i;
    }
    
    static int ast_extension_match(const char *pattern, const char *data);
    
    static int ast_extension_match(const char *pattern, const char *data)
    {
    	return extension_match_core(pattern, data, E_MATCH);
    }
    
    static int matchcid(const char *cidpattern, const char *callerid)
    {
    	/* If the Caller*ID pattern is empty, then we're matching NO Caller*ID, so
    	   failing to get a number should count as a match, otherwise not */
    
    	if (ast_strlen_zero(callerid))
    		return ast_strlen_zero(cidpattern) ? 1 : 0;
    
    	return ast_extension_match(cidpattern, callerid);
    }
    
    static inline int include_valid(struct ast_include *i)
    {
    	if (!i->hastime)
    		return 1;
    
    	return ast_check_timing(&(i->timing));
    }
    
    
    
    static struct ast_exten *pbx_find_extension(struct ast_channel *chan,
    											struct ast_context *bypass, 
    											struct pbx_find_info *q,
    											const char *context, 
    											const char *exten, 
    											int priority,
    											const char *label, 
    											const char *callerid, 
    											enum ext_match_t action);
    
    
    static struct ast_exten *pbx_find_extension(struct ast_channel *chan,
    											struct ast_context *bypass, 
    											struct pbx_find_info *q,
    											const char *context, 
    											const char *exten, 
    											int priority,
    											const char *label, 
    											const char *callerid, 
    											enum ext_match_t action)
    {
    	int x;
    	struct ast_context *tmp;
    	struct ast_exten *e, *eroot;
    	struct ast_include *i;
    
    	/* Initialize status if appropriate */
    	if (q->stacklen == 0) {
    		q->status = STATUS_NO_CONTEXT;
    		q->swo = NULL;
    		q->data = NULL;
    		q->foundcontext = NULL;
    	} else if (q->stacklen >= AST_PBX_MAX_STACK) {
    		ast_log(LOG_WARNING, "Maximum PBX stack exceeded\n");
    		return NULL;
    	}
    	/* Check first to see if we've already been checked */
    	for (x = 0; x < q->stacklen; x++) {
    		if (!strcasecmp(q->incstack[x], context))
    			return NULL;
    	}
    	if (bypass)	/* bypass means we only look there */
    		tmp = bypass;
    	else {	/* look in contexts */
    		tmp = NULL;
    		while ((tmp = ast_walk_contexts(tmp)) ) {
    			if (!strcmp(tmp->name, context))
    				break;
    		}
    		if (!tmp)
    			return NULL;
    	}
    	if (q->status < STATUS_NO_EXTENSION)
    		q->status = STATUS_NO_EXTENSION;
    
    	/* scan the list trying to match extension and CID */
    	eroot = NULL;
    	while ( (eroot = ast_walk_context_extensions(tmp, eroot)) ) {
    		int match = extension_match_core(eroot->exten, exten, action);
    		/* 0 on fail, 1 on match, 2 on earlymatch */
    
    		if (!match || (eroot->matchcid && !matchcid(eroot->cidmatch, callerid)))
    			continue;	/* keep trying */
    		if (match == 2 && action == E_MATCHMORE) {
    			/* We match an extension ending in '!'.
    			 * The decision in this case is final and is NULL (no match).
    			 */
    			return NULL;
    		}
    		/* found entry, now look for the right priority */
    		if (q->status < STATUS_NO_PRIORITY)
    			q->status = STATUS_NO_PRIORITY;
    		e = NULL;
    		while ( (e = ast_walk_extension_priorities(eroot, e)) ) {
    			/* Match label or priority */
    			if (action == E_FINDLABEL) {
    				if (q->status < STATUS_NO_LABEL)
    					q->status = STATUS_NO_LABEL;
    				if (label && e->label && !strcmp(label, e->label))
    					break;	/* found it */
    			} else if (e->priority == priority) {
    				break;	/* found it */
    			} /* else keep searching */
    		}
    		if (e) {	/* found a valid match */
    			q->status = STATUS_SUCCESS;
    			q->foundcontext = context;
    			return e;
    		}
    	}
    #ifdef NOT_RIGHT_NOW
    	/* Check alternative switches???  */
    	AST_LIST_TRAVERSE(&tmp->alts, sw, list) {
    		struct ast_switch *asw = pbx_findswitch(sw->name);
    		ast_switch_f *aswf = NULL;
    		char *datap;
    
    		if (!asw) {
    			ast_log(LOG_WARNING, "No such switch '%s'\n", sw->name);
    			continue;
    		}
    		/* No need to Substitute variables now; we shouldn't be here if there's any  */
    		
    		/* equivalent of extension_match_core() at the switch level */
    		if (action == E_CANMATCH)
    			aswf = asw->canmatch;
    		else if (action == E_MATCHMORE)
    			aswf = asw->matchmore;
    		else /* action == E_MATCH */
    			aswf = asw->exists;
    		datap = sw->eval ? sw->tmpdata : sw->data;
    		res = !aswf ? 0 : aswf(chan, context, exten, priority, callerid, datap);
    		if (res) {	/* Got a match */
    			q->swo = asw;
    			q->data = datap;
    			q->foundcontext = context;
    			/* XXX keep status = STATUS_NO_CONTEXT ? */
    			return NULL;
    		}
    	}
    #endif
    	q->incstack[q->stacklen++] = tmp->name;	/* Setup the stack */
    	/* Now try any includes we have in this context */
    	for (i = tmp->includes; i; i = i->next) {
    		if (include_valid(i)) {
    			if ((e = pbx_find_extension(NULL, bypass, q, i->rname, exten, priority, label, callerid, action)))
    				return e;
    			if (q->swo)
    				return NULL;
    		}
    	}
    	return NULL;
    }
    
    struct ast_exten *localized_find_extension(struct ast_context *bypass,
    										  struct pbx_find_info *q,
    										  const char *context, 
    										  const char *exten, 
    										  int priority,
    										  const char *label, 
    										  const char *callerid, 
    										  enum ext_match_t action);
    
    struct ast_exten *localized_find_extension(struct ast_context *bypass,
    										  struct pbx_find_info *q,
    										  const char *context, 
    										  const char *exten, 
    										  int priority,
    										  const char *label, 
    										  const char *callerid, 
    										   enum ext_match_t action)