Skip to content
Snippets Groups Projects
extconf.c 170 KiB
Newer Older
  • Learn to ignore specific revisions
  • 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924
    	return tmp;
    }
    
    /*! \brief
     * Main interface to add extensions to the list for out context.
     *
     * We sort extensions in order of matching preference, so that we can
     * stop the search as soon as we find a suitable match.
     * This ordering also takes care of wildcards such as '.' (meaning
     * "one or more of any character") and '!' (which is 'earlymatch',
     * meaning "zero or more of any character" but also impacts the
     * return value from CANMATCH and EARLYMATCH.
     *
     * The extension match rules defined in the devmeeting 2006.05.05 are
     * quite simple: WE SELECT THE LONGEST MATCH.
     * In detail, "longest" means the number of matched characters in
     * the extension. In case of ties (e.g. _XXX and 333) in the length
     * of a pattern, we give priority to entries with the smallest cardinality
     * (e.g, [5-9] comes before [2-8] before the former has only 5 elements,
     * while the latter has 7, etc.
     * In case of same cardinality, the first element in the range counts.
     * If we still have a tie, any final '!' will make this as a possibly
     * less specific pattern.
     *
     * EBUSY - can't lock
     * EEXIST - extension with the same priority exist and no replace is set
     *
     */
    static int ast_add_extension2(struct ast_context *con,
    	int replace, const char *extension, int priority, const char *label, const char *callerid,
    	const char *application, void *data, void (*datad)(void *),
    	const char *registrar)
    {
    	/*
    	 * Sort extensions (or patterns) according to the rules indicated above.
    	 * These are implemented by the function ext_cmp()).
    	 * All priorities for the same ext/pattern/cid are kept in a list,
    	 * using the 'peer' field  as a link field..
    	 */
    	struct ast_exten *tmp, *e, *el = NULL;
    	int res;
    	int length;
    	char *p;
    
    	/* if we are adding a hint, and there are global variables, and the hint
    	   contains variable references, then expand them --- NOT In this situation!!!
    	*/
    
    	length = sizeof(struct ast_exten);
    	length += strlen(extension) + 1;
    	length += strlen(application) + 1;
    	if (label)
    		length += strlen(label) + 1;
    	if (callerid)
    		length += strlen(callerid) + 1;
    	else
    		length ++;	/* just the '\0' */
    
    	/* Be optimistic:  Build the extension structure first */
    	if (datad == NULL)
    		datad = null_datad;
    	if (!(tmp = ast_calloc(1, length)))
    		return -1;
    
    	/* use p as dst in assignments, as the fields are const char * */
    	p = tmp->stuff;
    	if (label) {
    		tmp->label = p;
    		strcpy(p, label);
    		p += strlen(label) + 1;
    	}
    	tmp->exten = p;
    	p += ext_strncpy(p, extension, strlen(extension) + 1) + 1;
    	tmp->priority = priority;
    	tmp->cidmatch = p;	/* but use p for assignments below */
    	if (callerid) {
    		p += ext_strncpy(p, callerid, strlen(callerid) + 1) + 1;
    		tmp->matchcid = 1;
    	} else {
    		*p++ = '\0';
    		tmp->matchcid = 0;
    	}
    	tmp->app = p;
    	strcpy(p, application);
    	tmp->parent = con;
    	tmp->data = data;
    	tmp->datad = datad;
    	tmp->registrar = registrar;
    
    	res = 0; /* some compilers will think it is uninitialized otherwise */
    	for (e = con->root; e; el = e, e = e->next) {   /* scan the extension list */
    		res = ext_cmp(e->exten, extension);
    		if (res == 0) { /* extension match, now look at cidmatch */
    			if (!e->matchcid && !tmp->matchcid)
    				res = 0;
    			else if (tmp->matchcid && !e->matchcid)
    				res = 1;
    			else if (e->matchcid && !tmp->matchcid)
    				res = -1;
    			else
    				res = strcasecmp(e->cidmatch, tmp->cidmatch);
    		}
    		if (res >= 0)
    			break;
    	}
    	if (e && res == 0) { /* exact match, insert in the pri chain */
    		res = add_pri(con, tmp, el, e, replace);
    		if (res < 0) {
    			errno = EEXIST;	/* XXX do we care ? */
    			return 0; /* XXX should we return -1 maybe ? */
    		}
    	} else {
    		/*
    		 * not an exact match, this is the first entry with this pattern,
    		 * so insert in the main list right before 'e' (if any)
    		 */
    		tmp->next = e;
    		if (el)
    			el->next = tmp;
    		else
    			con->root = tmp;
    		if (tmp->priority == PRIORITY_HINT)
    			ast_add_hint(tmp);
    	}
    	if (option_debug) {
    		if (tmp->matchcid) {
    			ast_log(LOG_DEBUG, "Added extension '%s' priority %d (CID match '%s') to %s\n",
    				tmp->exten, tmp->priority, tmp->cidmatch, con->name);
    		} else {
    			ast_log(LOG_DEBUG, "Added extension '%s' priority %d to %s\n",
    				tmp->exten, tmp->priority, con->name);
    		}
    	}
    	if (option_verbose > 2) {
    		if (tmp->matchcid) {
    			ast_verbose( VERBOSE_PREFIX_3 "Added extension '%s' priority %d (CID match '%s')to %s\n",
    				tmp->exten, tmp->priority, tmp->cidmatch, con->name);
    		} else {
    			ast_verbose( VERBOSE_PREFIX_3 "Added extension '%s' priority %d to %s\n",
    				tmp->exten, tmp->priority, con->name);
    		}
    	}
    	return 0;
    }
    
    int localized_add_extension2(struct ast_context *con,
    							 int replace, const char *extension, int priority, const char *label, const char *callerid,
    							 const char *application, void *data, void (*datad)(void *),
    							 const char *registrar);
    
    int localized_add_extension2(struct ast_context *con,
    							 int replace, const char *extension, int priority, const char *label, const char *callerid,
    							 const char *application, void *data, void (*datad)(void *),
    							 const char *registrar)
    {
    	return ast_add_extension2(con, replace, extension, priority, label, callerid, application, data, datad, registrar);
    }
    
    
    
    /*! \brief The return value depends on the action:
     *
     * E_MATCH, E_CANMATCH, E_MATCHMORE require a real match,
     *	and return 0 on failure, -1 on match;
     * E_FINDLABEL maps the label to a priority, and returns
     *	the priority on success, ... XXX
     * E_SPAWN, spawn an application,
     *	and return 0 on success, -1 on failure.
     */
    static int pbx_extension_helper(struct ast_channel *c, struct ast_context *con,
    								const char *context, const char *exten, int priority,
    								const char *label, const char *callerid, enum ext_match_t action)
    {
    	struct ast_exten *e;
    	int res;
    	struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
    
    	int matching_action = (action == E_MATCH || action == E_CANMATCH || action == E_MATCHMORE);
    
    	e = pbx_find_extension(NULL, con, &q, context, exten, priority, label, callerid, action);
    	if (e) {
    		if (matching_action) {
    			return -1;	/* success, we found it */
    		} else if (action == E_FINDLABEL) { /* map the label to a priority */
    			res = e->priority;
    			return res;	/* the priority we were looking for */
    		} else {	/* spawn */
    
    			/* NOT!!!!! */
    			return 0;
    		}
    	} else if (q.swo) {	/* not found here, but in another switch */
    		if (matching_action)
    			return -1;
    		else {
    			if (!q.swo->exec) {
    				ast_log(LOG_WARNING, "No execution engine for switch %s\n", q.swo->name);
    				res = -1;
    			}
    			return q.swo->exec(c, q.foundcontext ? q.foundcontext : context, exten, priority, callerid, q.data);
    		}
    	} else {	/* not found anywhere, see what happened */
    		switch (q.status) {
    		case STATUS_NO_CONTEXT:
    			if (!matching_action)
    				ast_log(LOG_NOTICE, "Cannot find extension context '%s'\n", context);
    			break;
    		case STATUS_NO_EXTENSION:
    			if (!matching_action)
    				ast_log(LOG_NOTICE, "Cannot find extension '%s' in context '%s'\n", exten, context);
    			break;
    		case STATUS_NO_PRIORITY:
    			if (!matching_action)
    				ast_log(LOG_NOTICE, "No such priority %d in extension '%s' in context '%s'\n", priority, exten, context);
    			break;
    		case STATUS_NO_LABEL:
    			if (context)
    				ast_log(LOG_NOTICE, "No such label '%s' in extension '%s' in context '%s'\n", label, exten, context);
    			break;
    		default:
    			if (option_debug)
    				ast_log(LOG_DEBUG, "Shouldn't happen!\n");
    		}
    
    		return (matching_action) ? 0 : -1;
    	}
    }
    
    static int ast_findlabel_extension2(struct ast_channel *c, struct ast_context *con, const char *exten, const char *label, const char *callerid);
    
    static int ast_findlabel_extension2(struct ast_channel *c, struct ast_context *con, const char *exten, const char *label, const char *callerid)
    {
    	return pbx_extension_helper(c, con, NULL, exten, 0, label, callerid, E_FINDLABEL);
    }
    
    static struct ast_context *ast_context_find_or_create(struct ast_context **extcontexts, const char *name, const char *registrar)
    {
    	return __ast_context_create(extcontexts, name, registrar, 1);
    }
    
    struct ast_context *localized_context_create(struct ast_context **extcontexts, const char *name, const char *registrar);
    
    struct ast_context *localized_context_create(struct ast_context **extcontexts, const char *name, const char *registrar)
    {
    	return __ast_context_create(extcontexts, name, registrar, 0);
    }
    
    
    
    /* chopped this one off at the knees */
    static int ast_func_read(struct ast_channel *chan, const char *function, char *workspace, size_t len)
    {
    	ast_log(LOG_ERROR, "Function %s not registered\n", function);
    	return -1;
    }
    
    /*! \brief extract offset:length from variable name.
     * Returns 1 if there is a offset:length part, which is
     * trimmed off (values go into variables)
     */
    static int parse_variable_name(char *var, int *offset, int *length, int *isfunc)
    {
    	int parens=0;
    
    	*offset = 0;
    	*length = INT_MAX;
    	*isfunc = 0;
    	for (; *var; var++) {
    		if (*var == '(') {
    			(*isfunc)++;
    			parens++;
    		} else if (*var == ')') {
    			parens--;
    		} else if (*var == ':' && parens == 0) {
    			*var++ = '\0';
    			sscanf(var, "%d:%d", offset, length);
    			return 1; /* offset:length valid */
    		}
    	}
    	return 0;
    }
    
    static const char *ast_var_value(const struct ast_var_t *var)
    {
    	return (var ? var->value : NULL);
    }
    
    /*! \brief takes a substring. It is ok to call with value == workspace.
     *
     * offset < 0 means start from the end of the string and set the beginning
     *   to be that many characters back.
     * length is the length of the substring.  A value less than 0 means to leave
     * that many off the end.
     * Always return a copy in workspace.
     */
    static char *substring(const char *value, int offset, int length, char *workspace, size_t workspace_len)
    {
    	char *ret = workspace;
    	int lr;	/* length of the input string after the copy */
    
    	ast_copy_string(workspace, value, workspace_len); /* always make a copy */
    
    	lr = strlen(ret); /* compute length after copy, so we never go out of the workspace */
    
    	/* Quick check if no need to do anything */
    	if (offset == 0 && length >= lr)	/* take the whole string */
    		return ret;
    
    	if (offset < 0)	{	/* translate negative offset into positive ones */
    		offset = lr + offset;
    		if (offset < 0) /* If the negative offset was greater than the length of the string, just start at the beginning */
    			offset = 0;
    	}
    
    	/* too large offset result in empty string so we know what to return */
    	if (offset >= lr)
    		return ret + lr;	/* the final '\0' */
    
    	ret += offset;		/* move to the start position */
    	if (length >= 0 && length < lr - offset)	/* truncate if necessary */
    		ret[length] = '\0';
    	else if (length < 0) {
    		if (lr > offset - length) /* After we remove from the front and from the rear, is there anything left? */
    			ret[lr + length - offset] = '\0';
    		else
    			ret[0] = '\0';
    	}
    
    	return ret;
    }
    
    /*! \brief  Support for Asterisk built-in variables in the dialplan
    \note	See also
    	- \ref AstVar	Channel variables
    	- \ref AstCauses The HANGUPCAUSE variable
     */
    static void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, char *workspace, int workspacelen, struct varshead *headp)
    {
    	const char not_found = '\0';
    	char *tmpvar;
    	const char *s;	/* the result */
    	int offset, length;
    	int i, need_substring;
    	struct varshead *places[2] = { headp, &globals };	/* list of places where we may look */
    	
    	/*
    	 * Make a copy of var because parse_variable_name() modifies the string.
    	 * Then if called directly, we might need to run substring() on the result;
    	 * remember this for later in 'need_substring', 'offset' and 'length'
    	 */
    	tmpvar = ast_strdupa(var);	/* parse_variable_name modifies the string */
    	need_substring = parse_variable_name(tmpvar, &offset, &length, &i /* ignored */);
    	
    	/*
    	 * Look first into predefined variables, then into variable lists.
    	 * Variable 's' points to the result, according to the following rules:
    	 * s == &not_found (set at the beginning) means that we did not find a
    	 *	matching variable and need to look into more places.
    	 * If s != &not_found, s is a valid result string as follows:
    	 * s = NULL if the variable does not have a value;
    	 *	you typically do this when looking for an unset predefined variable.
    	 * s = workspace if the result has been assembled there;
    	 *	typically done when the result is built e.g. with an snprintf(),
    	 *	so we don't need to do an additional copy.
    	 * s != workspace in case we have a string, that needs to be copied
    	 *	(the ast_copy_string is done once for all at the end).
    	 *	Typically done when the result is already available in some string.
    	 */
    	s = &not_found;	/* default value */
    	if (s == &not_found) { /* look for more */
    		if (!strcmp(var, "EPOCH")) {
    			snprintf(workspace, workspacelen, "%u",(int)time(NULL));
    		}
    		
    		s = workspace;
    	}
    	/* if not found, look into chanvars or global vars */
    	for (i = 0; s == &not_found && i < (sizeof(places) / sizeof(places[0])); i++) {
    		struct ast_var_t *variables;
    		if (!places[i])
    			continue;
    		if (places[i] == &globals)
    			ast_rwlock_rdlock(&globalslock);
    		AST_LIST_TRAVERSE(places[i], variables, entries) {
    			if (strcasecmp(ast_var_name(variables), var)==0) {
    				s = ast_var_value(variables);
    				break;
    			}
    		}
    		if (places[i] == &globals)
    			ast_rwlock_unlock(&globalslock);
    	}
    	if (s == &not_found || s == NULL)
    		*ret = NULL;
    	else {
    		if (s != workspace)
    			ast_copy_string(workspace, s, workspacelen);
    		*ret = workspace;
    		if (need_substring)
    			*ret = substring(*ret, offset, length, workspace, workspacelen);
    	}
    }
    
    static void pbx_substitute_variables_helper_full(struct ast_channel *c, struct varshead *headp, const char *cp1, char *cp2, int count)
    {
    	/* Substitutes variables into cp2, based on string cp1, and assuming cp2 to be
    	   zero-filled */
    	char *cp4;
    	const char *tmp, *whereweare;
    	int length, offset, offset2, isfunction;
    	char *workspace = NULL;
    	char *ltmp = NULL, *var = NULL;
    	char *nextvar, *nextexp, *nextthing;
    	char *vars, *vare;
    	int pos, brackets, needsub, len;
    
    	whereweare=tmp=cp1;
    	while (!ast_strlen_zero(whereweare) && count) {
    		/* Assume we're copying the whole remaining string */
    		pos = strlen(whereweare);
    		nextvar = NULL;
    		nextexp = NULL;
    		nextthing = strchr(whereweare, '$');
    		if (nextthing) {
    			switch (nextthing[1]) {
    			case '{':
    				nextvar = nextthing;
    				pos = nextvar - whereweare;
    				break;
    			case '[':
    				nextexp = nextthing;
    				pos = nextexp - whereweare;
    				break;
    			}
    		}
    
    		if (pos) {
    			/* Can't copy more than 'count' bytes */
    			if (pos > count)
    				pos = count;
    
    			/* Copy that many bytes */
    			memcpy(cp2, whereweare, pos);
    
    			count -= pos;
    			cp2 += pos;
    			whereweare += pos;
    		}
    
    		if (nextvar) {
    			/* We have a variable.  Find the start and end, and determine
    			   if we are going to have to recursively call ourselves on the
    			   contents */
    			vars = vare = nextvar + 2;
    			brackets = 1;
    			needsub = 0;
    
    			/* Find the end of it */
    			while (brackets && *vare) {
    				if ((vare[0] == '$') && (vare[1] == '{')) {
    					needsub++;
    				} else if (vare[0] == '{') {
    					brackets++;
    				} else if (vare[0] == '}') {
    					brackets--;
    				} else if ((vare[0] == '$') && (vare[1] == '['))
    					needsub++;
    				vare++;
    			}
    			if (brackets)
    				ast_log(LOG_NOTICE, "Error in extension logic (missing '}' in '%s')\n", cp1);
    			len = vare - vars - 1;
    
    			/* Skip totally over variable string */
    			whereweare += (len + 3);
    
    			if (!var)
    				var = alloca(VAR_BUF_SIZE);
    
    			/* Store variable name (and truncate) */
    			ast_copy_string(var, vars, len + 1);
    
    			/* Substitute if necessary */
    			if (needsub) {
    				if (!ltmp)
    					ltmp = alloca(VAR_BUF_SIZE);
    
    				memset(ltmp, 0, VAR_BUF_SIZE);
    				pbx_substitute_variables_helper_full(c, headp, var, ltmp, VAR_BUF_SIZE - 1);
    				vars = ltmp;
    			} else {
    				vars = var;
    			}
    
    			if (!workspace)
    				workspace = alloca(VAR_BUF_SIZE);
    
    			workspace[0] = '\0';
    
    			parse_variable_name(vars, &offset, &offset2, &isfunction);
    			if (isfunction) {
    				/* Evaluate function */
    				cp4 = ast_func_read(c, vars, workspace, VAR_BUF_SIZE) ? NULL : workspace;
    				if (option_debug)
    					ast_log(LOG_DEBUG, "Function result is '%s'\n", cp4 ? cp4 : "(null)");
    			} else {
    				/* Retrieve variable value */
    				pbx_retrieve_variable(c, vars, &cp4, workspace, VAR_BUF_SIZE, headp);
    			}
    			if (cp4) {
    				cp4 = substring(cp4, offset, offset2, workspace, VAR_BUF_SIZE);
    
    				length = strlen(cp4);
    				if (length > count)
    					length = count;
    				memcpy(cp2, cp4, length);
    				count -= length;
    				cp2 += length;
    			}
    		} else if (nextexp) {
    			/* We have an expression.  Find the start and end, and determine
    			   if we are going to have to recursively call ourselves on the
    			   contents */
    			vars = vare = nextexp + 2;
    			brackets = 1;
    			needsub = 0;
    
    			/* Find the end of it */
    			while (brackets && *vare) {
    				if ((vare[0] == '$') && (vare[1] == '[')) {
    					needsub++;
    					brackets++;
    					vare++;
    				} else if (vare[0] == '[') {
    					brackets++;
    				} else if (vare[0] == ']') {
    					brackets--;
    				} else if ((vare[0] == '$') && (vare[1] == '{')) {
    					needsub++;
    					vare++;
    				}
    				vare++;
    			}
    			if (brackets)
    				ast_log(LOG_NOTICE, "Error in extension logic (missing ']')\n");
    			len = vare - vars - 1;
    
    			/* Skip totally over expression */
    			whereweare += (len + 3);
    
    			if (!var)
    				var = alloca(VAR_BUF_SIZE);
    
    			/* Store variable name (and truncate) */
    			ast_copy_string(var, vars, len + 1);
    
    			/* Substitute if necessary */
    			if (needsub) {
    				if (!ltmp)
    					ltmp = alloca(VAR_BUF_SIZE);
    
    				memset(ltmp, 0, VAR_BUF_SIZE);
    				pbx_substitute_variables_helper_full(c, headp, var, ltmp, VAR_BUF_SIZE - 1);
    				vars = ltmp;
    			} else {
    				vars = var;
    			}
    
    			length = ast_expr(vars, cp2, count, NULL);
    
    			if (length) {
    				if (option_debug)
    					ast_log(LOG_DEBUG, "Expression result is '%s'\n", cp2);
    				count -= length;
    				cp2 += length;
    			}
    		} else
    			break;
    	}
    }
    
    static void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
    {
    	pbx_substitute_variables_helper_full(c, NULL, cp1, cp2, count);
    }
    
    
    static int pbx_load_config(const char *config_file);
    
    static int pbx_load_config(const char *config_file)
    {
    	struct ast_config *cfg;
    	char *end;
    	char *label;
    	char realvalue[256];
    	int lastpri = -2;
    	struct ast_context *con;
    	struct ast_variable *v;
    	const char *cxt;
    	const char *aft;
    
    	cfg = localized_config_load(config_file);
    	if (!cfg)
    		return 0;
    
    	/* Use existing config to populate the PBX table */
    	static_config = ast_true(ast_variable_retrieve(cfg, "general", "static"));
    	write_protect_config = ast_true(ast_variable_retrieve(cfg, "general", "writeprotect"));
    	if ((aft = ast_variable_retrieve(cfg, "general", "autofallthrough")))
    		autofallthrough_config = ast_true(aft);
    	clearglobalvars_config = ast_true(ast_variable_retrieve(cfg, "general", "clearglobalvars"));
    	ast_set2_flag(&ast_options, ast_true(ast_variable_retrieve(cfg, "general", "priorityjumping")), AST_OPT_FLAG_PRIORITY_JUMPING);
    
    	if ((cxt = ast_variable_retrieve(cfg, "general", "userscontext"))) 
    		ast_copy_string(userscontext, cxt, sizeof(userscontext));
    	else
    		ast_copy_string(userscontext, "default", sizeof(userscontext));
    								    
    	for (v = ast_variable_browse(cfg, "globals"); v; v = v->next) {
    		memset(realvalue, 0, sizeof(realvalue));
    		pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
    		pbx_builtin_setvar_helper(NULL, v->name, realvalue);
    	}
    	for (cxt = NULL; (cxt = ast_category_browse(cfg, cxt)); ) {
    		/* All categories but "general" or "globals" are considered contexts */
    		if (!strcasecmp(cxt, "general") || !strcasecmp(cxt, "globals"))
    			continue;
    		con=ast_context_find_or_create(&local_contexts,cxt, registrar);
    		if (con == NULL)
    			continue;
    
    		for (v = ast_variable_browse(cfg, cxt); v; v = v->next) {
    			if (!strcasecmp(v->name, "exten")) {
    				char *tc = ast_strdup(v->value);
    				if (tc) {
    					int ipri = -2;
    					char realext[256]="";
    					char *plus, *firstp, *firstc;
    					char *pri, *appl, *data, *cidmatch;
    					char *stringp = tc;
    					char *ext = strsep(&stringp, ",");
    					if (!ext)
    						ext="";
    					pbx_substitute_variables_helper(NULL, ext, realext, sizeof(realext) - 1);
    					cidmatch = strchr(realext, '/');
    					if (cidmatch) {
    						*cidmatch++ = '\0';
    						ast_shrink_phone_number(cidmatch);
    					}
    					pri = strsep(&stringp, ",");
    					if (!pri)
    						pri="";
    					label = strchr(pri, '(');
    					if (label) {
    						*label++ = '\0';
    						end = strchr(label, ')');
    						if (end)
    							*end = '\0';
    						else
    							ast_log(LOG_WARNING, "Label missing trailing ')' at line %d\n", v->lineno);
    					}
    					plus = strchr(pri, '+');
    					if (plus)
    						*plus++ = '\0';
    					if (!strcmp(pri,"hint"))
    						ipri=PRIORITY_HINT;
    					else if (!strcmp(pri, "next") || !strcmp(pri, "n")) {
    						if (lastpri > -2)
    							ipri = lastpri + 1;
    						else
    							ast_log(LOG_WARNING, "Can't use 'next' priority on the first entry!\n");
    					} else if (!strcmp(pri, "same") || !strcmp(pri, "s")) {
    						if (lastpri > -2)
    							ipri = lastpri;
    						else
    							ast_log(LOG_WARNING, "Can't use 'same' priority on the first entry!\n");
    					} else if (sscanf(pri, "%d", &ipri) != 1 &&
    					    (ipri = ast_findlabel_extension2(NULL, con, realext, pri, cidmatch)) < 1) {
    						ast_log(LOG_WARNING, "Invalid priority/label '%s' at line %d\n", pri, v->lineno);
    						ipri = 0;
    					}
    					appl = S_OR(stringp, "");
    					/* Find the first occurrence of either '(' or ',' */
    					firstc = strchr(appl, ',');
    					firstp = strchr(appl, '(');
    					if (firstc && (!firstp || firstc < firstp)) {
    						/* comma found, no parenthesis */
    						/* or both found, but comma found first */
    						appl = strsep(&stringp, ",");
    						data = stringp;
    					} else if (!firstc && !firstp) {
    						/* Neither found */
    						data = "";
    					} else {
    						/* Final remaining case is parenthesis found first */
    						appl = strsep(&stringp, "(");
    						data = stringp;
    						end = strrchr(data, ')');
    						if ((end = strrchr(data, ')'))) {
    							*end = '\0';
    						} else {
    							ast_log(LOG_WARNING, "No closing parenthesis found? '%s(%s'\n", appl, data);
    						}
    						ast_process_quotes_and_slashes(data, ',', '|');
    					}
    
    					if (!data)
    						data="";
    					appl = ast_skip_blanks(appl);
    					if (ipri) {
    						if (plus)
    							ipri += atoi(plus);
    						lastpri = ipri;
    						if (!ast_opt_dont_warn && !strcmp(realext, "_."))
    							ast_log(LOG_WARNING, "The use of '_.' for an extension is strongly discouraged and can have unexpected behavior.  Please use '_X.' instead at line %d\n", v->lineno);
    						if (ast_add_extension2(con, 0, realext, ipri, label, cidmatch, appl, strdup(data), ast_free, registrar)) {
    							ast_log(LOG_WARNING, "Unable to register extension at line %d\n", v->lineno);
    						}
    					}
    					free(tc);
    				}
    			} else if (!strcasecmp(v->name, "include")) {
    				memset(realvalue, 0, sizeof(realvalue));
    				pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
    				if (ast_context_add_include2(con, realvalue, registrar))
    					ast_log(LOG_WARNING, "Unable to include context '%s' in context '%s'\n", v->value, cxt);
    			} else if (!strcasecmp(v->name, "ignorepat")) {
    				memset(realvalue, 0, sizeof(realvalue));
    				pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
    				if (ast_context_add_ignorepat2(con, realvalue, registrar))
    					ast_log(LOG_WARNING, "Unable to include ignorepat '%s' in context '%s'\n", v->value, cxt);
    			} else if (!strcasecmp(v->name, "switch") || !strcasecmp(v->name, "lswitch") || !strcasecmp(v->name, "eswitch")) {
    				char *stringp= realvalue;
    				char *appl, *data;
    
    				memset(realvalue, 0, sizeof(realvalue));
    				if (!strcasecmp(v->name, "switch"))
    					pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
    				else
    					ast_copy_string(realvalue, v->value, sizeof(realvalue));
    				appl = strsep(&stringp, "/");
    				data = strsep(&stringp, ""); /* XXX what for ? */
    				if (!data)
    					data = "";
    				if (ast_context_add_switch2(con, appl, data, !strcasecmp(v->name, "eswitch"), registrar))
    					ast_log(LOG_WARNING, "Unable to include switch '%s' in context '%s'\n", v->value, cxt);
    			} else {
    				ast_log(LOG_WARNING, "==!!== Unknown directive: %s at line %d -- IGNORING!!!\n", v->name, v->lineno);
    			}
    		}
    	}
    	ast_config_destroy(cfg);
    	return 1;
    }
    
    static void __ast_context_destroy(struct ast_context *con, const char *registrar)
    {
    	struct ast_context *tmp, *tmpl=NULL;
    	struct ast_include *tmpi;
    	struct ast_sw *sw;
    	struct ast_exten *e, *el, *en;
    	struct ast_ignorepat *ipi;
    
    	for (tmp = contexts; tmp; ) {
    		struct ast_context *next;	/* next starting point */
    		for (; tmp; tmpl = tmp, tmp = tmp->next) {
    			if (option_debug)
    				ast_log(LOG_DEBUG, "check ctx %s %s\n", tmp->name, tmp->registrar);
    			if ( (!registrar || !strcasecmp(registrar, tmp->registrar)) &&
    			     (!con || !strcasecmp(tmp->name, con->name)) )
    				break;	/* found it */
    		}
    		if (!tmp)	/* not found, we are done */
    			break;
    		ast_wrlock_context(tmp);
    		if (option_debug)
    			ast_log(LOG_DEBUG, "delete ctx %s %s\n", tmp->name, tmp->registrar);
    		next = tmp->next;
    		if (tmpl)
    			tmpl->next = next;
    		else
    			contexts = next;
    		/* Okay, now we're safe to let it go -- in a sense, we were
    		   ready to let it go as soon as we locked it. */
    		ast_unlock_context(tmp);
    		for (tmpi = tmp->includes; tmpi; ) { /* Free includes */
    			struct ast_include *tmpil = tmpi;
    			tmpi = tmpi->next;
    			free(tmpil);
    		}
    		for (ipi = tmp->ignorepats; ipi; ) { /* Free ignorepats */
    			struct ast_ignorepat *ipl = ipi;
    			ipi = ipi->next;
    			free(ipl);
    		}
    		while ((sw = AST_LIST_REMOVE_HEAD(&tmp->alts, list)))
    			free(sw);
    		for (e = tmp->root; e;) {
    			for (en = e->peer; en;) {
    				el = en;
    				en = en->peer;
    				destroy_exten(el);
    			}
    			el = e;
    			e = e->next;
    			destroy_exten(el);
    		}
    		ast_rwlock_destroy(&tmp->lock);
    		free(tmp);
    		/* if we have a specific match, we are done, otherwise continue */
    		tmp = con ? NULL : next;
    	}
    }
    
    void localized_context_destroy(struct ast_context *con, const char *registrar);
    
    void localized_context_destroy(struct ast_context *con, const char *registrar)
    {
    	ast_wrlock_contexts();
    	__ast_context_destroy(con,registrar);
    	ast_unlock_contexts();
    }
    
    
    static void ast_merge_contexts_and_delete(struct ast_context **extcontexts, const char *registrar)
    {
    	struct ast_context *tmp, *lasttmp = NULL;
    
    	/* it is very important that this function hold the hint list lock _and_ the conlock
    	   during its operation; not only do we need to ensure that the list of contexts
    	   and extensions does not change, but also that no hint callbacks (watchers) are
    	   added or removed during the merge/delete process
    
    	   in addition, the locks _must_ be taken in this order, because there are already
    	   other code paths that use this order
    	*/
    	ast_wrlock_contexts();
    
    	tmp = *extcontexts;
    	if (registrar) {
    		/* XXX remove previous contexts from same registrar */
    		if (option_debug)
    			ast_log(LOG_DEBUG, "must remove any reg %s\n", registrar);
    		__ast_context_destroy(NULL,registrar);
    		while (tmp) {
    			lasttmp = tmp;
    			tmp = tmp->next;
    		}
    	} else {
    		/* XXX remove contexts with the same name */
    		while (tmp) {
    			ast_log(LOG_WARNING, "must remove %s  reg %s\n", tmp->name, tmp->registrar);
    			__ast_context_destroy(tmp,tmp->registrar);
    			lasttmp = tmp;
    			tmp = tmp->next;
    		}
    	}
    	if (lasttmp) {
    		lasttmp->next = contexts;
    		contexts = *extcontexts;
    		*extcontexts = NULL;
    	} else
    		ast_log(LOG_WARNING, "Requested contexts didn't get merged\n");
    
    	ast_unlock_contexts();
    
    	return;
    }
    
    void localized_merge_contexts_and_delete(struct ast_context **extcontexts, const char *registrar);
    
    void localized_merge_contexts_and_delete(struct ast_context **extcontexts, const char *registrar)
    {
    	ast_merge_contexts_and_delete(extcontexts, registrar);
    }
    
    static int ast_context_verify_includes(struct ast_context *con)
    {
    	struct ast_include *inc = NULL;
    	int res = 0;
    
    	while ( (inc = ast_walk_context_includes(con, inc)) )
    		if (!ast_context_find(inc->rname)) {
    			res = -1;
    			if (strcasecmp(inc->rname,"parkedcalls")!=0)
    				ast_log(LOG_WARNING, "Context '%s' tries to include the nonexistent context '%s'\n",
    						ast_get_context_name(con), inc->rname);
    		}
    	return res;
    }
    
    int localized_context_verify_includes(struct ast_context *con);
    
    int localized_context_verify_includes(struct ast_context *con)
    {
    	return ast_context_verify_includes(con);
    }
    
    int localized_pbx_load_module(void);
    
    int localized_pbx_load_module(void)
    {
    	struct ast_context *con;
    
    	if(!pbx_load_config(config))
    		return -1 /* AST_MODULE_LOAD_DECLINE*/;
    
    	/* pbx_load_users(); */ /* does this affect the dialplan? */
    
    	ast_merge_contexts_and_delete(&local_contexts, registrar);
    
    	for (con = NULL; (con = ast_walk_contexts(con));)
    		ast_context_verify_includes(con);
    
    	printf("=== Loading extensions.conf ===\n");
    	con = 0;
    	while ((con = ast_walk_contexts(con)) ) {
    		printf("Context: %s\n", con->name);
    	}
    	printf("=========\n");
    	
    	return 0;
    }