2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * Steve Murphy <murf@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * Reverse compile extensions.conf code into prototype AEL code
26 <depend>res_ael_share</depend>
27 <support_level>deprecated</support_level>
30 #define ASTMM_LIBC ASTMM_IGNORE
33 #include "asterisk/paths.h" /* CONFIG_DIR */
36 #if !defined(SOLARIS) && !defined(__CYGWIN__)
42 #include "asterisk/pbx.h"
43 #include "asterisk/ast_expr.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/chanvars.h"
46 #include "asterisk/module.h"
47 #include "asterisk/app.h"
48 #include "asterisk/config.h"
49 #include "asterisk/options.h"
50 #include "asterisk/callerid.h"
51 #include "asterisk/lock.h"
52 #include "asterisk/hashtab.h"
53 #include "asterisk/ael_structs.h"
54 #include "asterisk/devicestate.h"
55 #include "asterisk/stringfields.h"
56 #include "asterisk/pval.h"
57 #include "asterisk/extconf.h"
59 const char *ast_config_AST_CONFIG_DIR = "/etc/asterisk"; /* placeholder */
61 void get_start_stop(unsigned int *word, int bitsperword, int totalbits, int *start, int *end);
62 int all_bits_set(unsigned int *word, int bitsperword, int totalbits);
64 extern char *months[];
66 char *config = "extensions.conf";
69 static char *registrar = "conf2ael";
70 static char userscontext[AST_MAX_EXTENSION] = "default";
71 static int static_config = 0;
72 static int write_protect_config = 1;
73 static int autofallthrough_config = 0;
74 static int clearglobalvars_config = 0;
75 char ast_config_AST_SYSTEM_NAME[20] = ""; */
77 /* static AST_RWLIST_HEAD_STATIC(acf_root, ast_custom_function); */
78 //extern char ast_config_AST_CONFIG_DIR[PATH_MAX];
80 int option_verbose = 0;
82 int ast_add_profile(const char *x, uint64_t scale) { return 0;}
83 /* Our own version of ast_log, since the expr parser uses it. -- stolen from utils/check_expr.c */
84 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__((format(printf,5,6)));
86 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
91 printf("LOG: lev:%d file:%s line:%d func: %s ",
92 level, file, line, function);
98 /* stolen from pbx.c */
102 #define EXT_DATA_SIZE 256
104 #define EXT_DATA_SIZE 8192
107 #define SWITCH_DATA_LENGTH 256
109 #define VAR_BUF_SIZE 4096
112 #define VAR_SOFTTRAN 2
113 #define VAR_HARDTRAN 3
115 #define BACKGROUND_SKIP (1 << 0)
116 #define BACKGROUND_NOANSWER (1 << 1)
117 #define BACKGROUND_MATCHEXTEN (1 << 2)
118 #define BACKGROUND_PLAYBACK (1 << 3)
121 \brief ast_exten: An extension
122 The dialplan is saved as a linked list with each context
123 having it's own linked list of extensions - one item per
127 char *exten; /*!< Extension name */
128 int matchcid; /*!< Match caller id ? */
129 const char *cidmatch; /*!< Caller id to match for this extension */
130 int priority; /*!< Priority */
131 const char *label; /*!< Label */
132 struct ast_context *parent; /*!< The context this extension belongs to */
133 const char *app; /*!< Application to execute */
134 struct ast_app *cached_app; /*!< Cached location of application */
135 void *data; /*!< Data to use (arguments) */
136 void (*datad)(void *); /*!< Data destructor */
137 struct ast_exten *peer; /*!< Next higher priority with our extension */
138 const char *registrar; /*!< Registrar */
139 struct ast_exten *next; /*!< Extension with a greater ID */
144 /*! \brief ast_include: include= support in extensions.conf */
147 const char *rname; /*!< Context to include */
148 const char *registrar; /*!< Registrar */
149 int hastime; /*!< If time construct exists */
150 struct ast_timing timing; /*!< time construct */
151 struct ast_include *next; /*!< Link them together */
155 /*! \brief ast_sw: Switch statement in extensions.conf */
158 const char *registrar; /*!< Registrar */
159 char *data; /*!< Data load */
161 AST_LIST_ENTRY(ast_sw) list;
166 /*! \brief ast_ignorepat: Ignore patterns in dial plan */
167 struct ast_ignorepat {
168 const char *registrar;
169 struct ast_ignorepat *next;
170 const char pattern[0];
173 /*! \brief ast_context: An extension context */
175 ast_rwlock_t lock; /*!< A lock to prevent multiple threads from clobbering the context */
176 struct ast_exten *root; /*!< The root of the list of extensions */
177 struct ast_context *next; /*!< Link them together */
178 struct ast_include *includes; /*!< Include other contexts */
179 struct ast_ignorepat *ignorepats; /*!< Patterns for which to continue playing dialtone */
180 const char *registrar; /*!< Registrar */
181 AST_LIST_HEAD_NOLOCK(, ast_sw) alts; /*!< Alternative switches */
182 ast_mutex_t macrolock; /*!< A lock to implement "exclusive" macros - held whilst a call is executing in the macro */
183 char name[0]; /*!< Name of the context */
187 /*! \brief ast_app: A registered application */
189 int (*execute)(struct ast_channel *chan, void *data);
190 const char *synopsis; /*!< Synopsis text for 'show applications' */
191 const char *description; /*!< Description (help text) for 'show application <name>' */
192 AST_RWLIST_ENTRY(ast_app) list; /*!< Next app in list */
193 struct module *module; /*!< Module this app belongs to */
194 char name[0]; /*!< Name of the application */
197 /*! \brief ast_state_cb: An extension state notify register item */
198 struct ast_state_cb {
201 ast_state_cb_type callback;
202 struct ast_state_cb *next;
205 /*! \brief Structure for dial plan hints
207 \note Hints are pointers from an extension in the dialplan to one or
208 more devices (tech/name)
209 - See \ref AstExtState
212 struct ast_exten *exten; /*!< Extension */
213 int laststate; /*!< Last known state */
214 struct ast_state_cb *callbacks; /*!< Callback list for this extension */
215 AST_RWLIST_ENTRY(ast_hint) list;/*!< Pointer to next hint in list */
221 struct ast_state_cb *callbacks;
223 AST_LIST_ENTRY(store_hint) list;
227 AST_LIST_HEAD(store_hints, store_hint);
229 #define STATUS_NO_CONTEXT 1
230 #define STATUS_NO_EXTENSION 2
231 #define STATUS_NO_PRIORITY 3
232 #define STATUS_NO_LABEL 4
233 #define STATUS_SUCCESS 5
235 extern struct ast_context *local_contexts;
236 extern struct ast_context *contexts;
239 struct ast_custom_function *ast_custom_function_find(const char *name);
242 struct ast_custom_function *ast_custom_function_find(const char *name)
244 return 0; /* in "standalone" mode, functions are just not avail */
248 struct profile_entry {
250 uint64_t scale; /* if non-zero, values are scaled by this */
256 struct profile_data {
259 struct profile_entry e[0];
262 static int bit_at(unsigned int *word, int bitsperword, int bitnum)
264 return word[bitnum/bitsperword] & (1 << (bitnum % bitsperword));
267 void get_start_stop(unsigned int *word, int bitsperword, int totalbits, int *start, int *end)
270 int thisbit, thatbit = bit_at(word, bitsperword, totalbits-1);
272 for (i=0; i<totalbits; i++) {
273 thisbit = bit_at(word, bitsperword, i);
275 if (thisbit != thatbit ) {
286 int all_bits_set(unsigned int *word, int bitsperword, int totalbits )
289 int i, total=totalbits/bitsperword,bitmask = 0;
291 for (i=0; i<bitsperword; i++)
296 for (i=0; i<total; i++)
298 if (word[i] != bitmask)
305 int main(int argc, char **argv)
307 struct ast_context *tmp;
308 struct ast_exten *e, *eroot;
309 pval *tree, *tmptree, *sws;
310 struct ast_include *tmpi;
311 struct ast_sw *sw = 0;
312 struct ast_ignorepat *ipi;
319 /* process the command line args */
320 for (i=1; i<argc; i++)
322 if (strcmp(argv[i],"-d")==0)
326 /* 3 simple steps: */
327 /* 1. read in the extensions.conf config file
328 * 2. traverse, and build an AEL tree
329 * 3. Output the AEL tree into a file
331 printf("WARNING: This is an EXTREMELY preliminary version of a program\n");
332 printf(" that will someday hopefully do a thoughful and intelligent\n");
333 printf(" job of transforming your extensions.conf file into an\n");
334 printf(" extensions.ael file.\n");
335 printf(" This version has absolutely no intelligence, and pretty\n");
336 printf(" much just does a direct conversion\n");
337 printf(" The result will most likely need careful attention to\n");
338 printf(" finish the job!!!!!\n");
341 printf(" (You could use -d the use the extensions.conf in the current directory!)\n");
343 printf("Loading %s/%s...\n", ast_config_AST_CONFIG_DIR, config);
346 localized_use_conf_dir();
347 localized_pbx_load_module();
349 printf("... Done!\n");
352 while ((tmp = localized_walk_contexts(tmp)) ) {
353 printf("Context: %s\n", tmp->name);
355 printf("=========\n");
357 while ((tmp = localized_walk_contexts(tmp)) ) {
358 /* printf("Context: %s\n", tmp->name); */
359 tmptree = pvalCreateNode(PV_CONTEXT);
363 pvalTopLevAddObject(tree, tmptree);
365 pvalContextSetName(tmptree, ast_strdup(tmp->name));
368 incl = pvalCreateNode(PV_INCLUDES);
369 pvalContextAddStatement(tmptree, incl);
370 for (tmpi = tmp->includes; tmpi; ) { /* includes */
371 if (strchr(tmpi->name,'|')==0) {
378 int startbit=0, endbit=0;
380 if (all_bits_set(tmpi->timing.minmask, 30, 720))
381 strcpy(timerange, "*");
385 get_start_stop(tmpi->timing.minmask, 30, 720, &startbit, &endbit);
387 min = (startbit % 30) * 2;
388 sprintf(tbuf,"%02d:%02d", hr, min);
389 strcpy(timerange, tbuf);
391 min = (endbit % 30) * 2;
392 sprintf(tbuf,"%02d:%02d", hr, min);
393 strcat(timerange,"-");
394 strcat(timerange,tbuf);
397 if (all_bits_set(&tmpi->timing.dowmask, 7, 7))
398 strcpy(dowrange, "*");
400 get_start_stop(&tmpi->timing.dowmask, 7, 7, &startbit, &endbit);
401 strcpy(dowrange, days[startbit]);
402 strcat(dowrange,"-");
403 strcat(dowrange, days[endbit]);
406 if (all_bits_set(&tmpi->timing.monthmask, 12, 12))
407 strcpy(monrange, "*");
409 get_start_stop(&tmpi->timing.monthmask, 12, 12, &startbit, &endbit);
410 strcpy(monrange, months[startbit]);
411 strcat(monrange,"-");
412 strcat(monrange, months[endbit]);
415 if (all_bits_set(&tmpi->timing.daymask, 31, 31))
416 strcpy(domrange, "*");
419 get_start_stop(&tmpi->timing.daymask, 31, 31, &startbit, &endbit);
420 sprintf(tbuf,"%d", startbit);
421 strcpy(domrange, tbuf);
422 strcat(domrange,"-");
423 sprintf(tbuf,"%d", endbit);
424 strcat(domrange, tbuf);
426 /* now all 4 fields are set; what do we do? */
427 pvalIncludesAddIncludeWithTimeConstraints(incl, strdup(tmpi->name), strdup(timerange), strdup(domrange), strdup(dowrange), strdup(monrange));
430 pvalIncludesAddInclude(incl, strdup(tmpi->name));
432 } else { /* it appears the timing constraint info is tacked onto the name, carve it up and divvy it out */
434 char *all = strdup(tmpi->name);
435 char *hr = strchr(all,'|');
438 dow = strchr(hr,'|');
441 dom = strchr(dow,'|');
444 mon = strchr(dom,'|');
447 /* now all 4 fields are set; what do we do? */
448 pvalIncludesAddIncludeWithTimeConstraints(incl, strdup(all), strdup(hr), strdup(dow), strdup(dom), strdup(mon));
449 /* the original data is always best to keep (no 2-min rounding) */
451 ast_log(LOG_ERROR,"No month spec attached to include!\n");
454 ast_log(LOG_ERROR,"No day of month spec attached to include!\n");
457 ast_log(LOG_ERROR,"No day of week spec attached to include!\n");
465 for (ipi = tmp->ignorepats; ipi; ) { /* ignorepats */
466 incl = pvalCreateNode(PV_IGNOREPAT);
467 pvalIgnorePatSetPattern(incl,(char *)ipi->pattern);
468 pvalContextAddStatement(tmptree, incl);
472 while ( (eroot = localized_walk_context_extensions(tmp, eroot)) ) {
473 pval *exten = pvalCreateNode(PV_EXTENSION);
474 pvalContextAddStatement(tmptree, exten);
475 pvalExtenSetName(exten, ast_strdup(eroot->exten));
478 pval *block = pvalCreateNode(PV_STATEMENTBLOCK);
479 pvalExtenSetStatement(exten, block);
482 while ( (e = localized_walk_extension_priorities(eroot, e)) ) {
484 pval *statemnt = pvalCreateNode(PV_APPLICATION_CALL);
485 pval *args = pvalCreateNode(PV_WORD);
487 /* printf(" %s(%s)\n", e->app, (char*)e->data); */
489 pvalAppCallSetAppName(statemnt, ast_strdup(e->app));
490 pvalWordSetString(args, ast_strdup(e->data));
491 pvalAppCallAddArg(statemnt, args);
493 pvalStatementBlockAddStatement(block, statemnt);
495 } else if (eroot->priority == -1) {
497 pval *statemnt = pvalCreateNode(PV_APPLICATION_CALL);
498 pval *args = pvalCreateNode(PV_WORD);
500 /* printf("Mike, we have a hint on exten %s with data %s\n", eroot->exten, eroot->app); */
502 pvalAppCallSetAppName(statemnt, "NoOp");
503 pvalWordSetString(args, ast_strdup(eroot->app));
506 pvalExtenSetStatement(exten, statemnt);
507 pvalExtenSetHints(exten, ast_strdup(eroot->app));
510 pval *statemnt = pvalCreateNode(PV_APPLICATION_CALL);
511 pval *args = pvalCreateNode(PV_WORD);
513 /* printf(" %s (%s)\n", eroot->app, (char *)eroot->data); */
515 pvalAppCallSetAppName(statemnt, ast_strdup(eroot->app));
516 pvalWordSetString(args, ast_strdup(eroot->data));
519 pvalAppCallAddArg(statemnt, args);
520 pvalExtenSetStatement(exten, statemnt);
523 /* printf(" extension: %s\n", eroot->exten); */
525 if (AST_LIST_FIRST(&tmp->alts)) {
526 sws = pvalCreateNode(PV_SWITCHES);
527 pvalContextAddStatement(tmptree,sws);
530 while ((sw = localized_walk_context_switches(tmp,sw)) ) {
531 pvalSwitchesAddSwitch(sws, ast_strdup(sw->name));
535 printf("Generating aelout.ael file...\n");
537 ael2_print("aelout.ael", tree);
539 printf("...Done!\n");
544 /* ==================================== for linking internal stuff to external stuff */
546 int pbx_builtin_setvar(struct ast_channel *chan, const char *data)
548 return localized_pbx_builtin_setvar(chan, data);
551 void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count);
552 void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count)
555 strncpy(cp2,cp1,AST_MAX_EXTENSION); /* Right now, this routine is ONLY being called for
556 a possible var substitution on extension names,
562 int ast_add_extension2(struct ast_context *con,
563 int replace, const char *extension, int priority, const char *label, const char *callerid,
564 const char *application, void *data, void (*datad)(void *),
565 const char *registrar, const char *registrar_file, int registrar_line)
567 return localized_add_extension2(con, replace, extension, priority, label, callerid, application, data, datad, registrar);
570 int ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
573 return localized_context_add_ignorepat2(con, value, registrar);
576 int ast_context_add_switch2(struct ast_context *con, const char *value,
577 const char *data, int eval, const char *registrar)
580 return localized_context_add_switch2(con, value, data, eval, registrar);
583 int ast_context_add_include2(struct ast_context *con, const char *value,
584 const char *registrar)
587 return localized_context_add_include2(con, value,registrar);
590 struct ast_context *ast_context_find_or_create(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *name, const char *registrar)
592 printf("find/Creating context %s, registrar=%s\n", name, registrar);
594 return localized_context_find_or_create(extcontexts, exttable, name, registrar);
597 void __ast_cli_register_multiple(void);
599 void __ast_cli_register_multiple(void)
603 void ast_module_register(const struct ast_module_info *x)
607 void ast_module_unregister(const struct ast_module_info *x)
611 void ast_cli_unregister_multiple(void);
613 void ast_cli_unregister_multiple(void)
617 struct ast_context *ast_walk_contexts(struct ast_context *con);
618 struct ast_context *ast_walk_contexts(struct ast_context *con)
620 return localized_walk_contexts(con);
623 void ast_context_destroy(struct ast_context *con, const char *registrar);
625 void ast_context_destroy(struct ast_context *con, const char *registrar)
627 return localized_context_destroy(con, registrar);
630 int ast_context_verify_includes(struct ast_context *con);
632 int ast_context_verify_includes(struct ast_context *con)
634 return localized_context_verify_includes(con);
637 void ast_merge_contexts_and_delete(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *registrar);
639 void ast_merge_contexts_and_delete(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *registrar)
641 localized_merge_contexts_and_delete(extcontexts, exttable, registrar);
644 const char *ast_get_context_name(struct ast_context *con);
645 const char *ast_get_context_name(struct ast_context *con)
647 return con ? con->name : NULL;
650 struct ast_exten *ast_walk_context_extensions(struct ast_context *con, struct ast_exten *exten);
651 struct ast_exten *ast_walk_context_extensions(struct ast_context *con, struct ast_exten *exten)
656 const struct ast_include *ast_walk_context_includes(const struct ast_context *con, const struct ast_include *inc);
657 const struct ast_include *ast_walk_context_includes(const struct ast_context *con, const struct ast_include *inc)
662 struct ast_exten *pbx_find_extension(struct ast_channel *chan,
663 struct ast_context *bypass,
664 struct pbx_find_info *q,
669 const char *callerid,
670 enum ext_match_t action);
672 struct ast_exten *pbx_find_extension(struct ast_channel *chan,
673 struct ast_context *bypass,
674 struct pbx_find_info *q,
679 const char *callerid,
680 enum ext_match_t action)
682 return localized_find_extension(bypass, q, context, exten, priority, label, callerid, action);
685 int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b);
687 int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b)
692 unsigned int ast_hashtab_hash_contexts(const void *obj);
694 unsigned int ast_hashtab_hash_contexts(const void *obj)
700 void ast_mark_lock_acquired(void *lock_addr)
703 void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt)
707 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
708 int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt)
713 int __ast_bt_get_addresses(struct ast_bt *bt)
718 struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames)
722 #endif /* HAVE_BKTR */
723 void ast_suspend_lock_info(void *lock_addr)
726 void ast_restore_lock_info(void *lock_addr)
729 #endif /* DEBUG_THREADS */