pbx: Create pbx_include.c for management of 'struct ast_include'.
[asterisk/asterisk.git] / main / pbx_include.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2016, CFWare, LLC
5  *
6  * Corey Farrell <git@cfware.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Dialplan context include routines.
22  *
23  * \author Corey Farrell <git@cfware.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_REGISTER_FILE()
33
34 #include "asterisk/_private.h"
35 #include "asterisk/pbx.h"
36 #include "pbx_private.h"
37
38 /*! ast_include: include= support in extensions.conf */
39 struct ast_include {
40         const char *name;
41         /*! Context to include */
42         const char *rname;
43         /*! Registrar */
44         const char *registrar;
45         /*! If time construct exists */
46         int hastime;
47         /*! time construct */
48         struct ast_timing timing;
49         char stuff[0];
50 };
51
52 const char *ast_get_include_name(const struct ast_include *inc)
53 {
54         return inc ? inc->name : NULL;
55 }
56
57 const char *include_rname(const struct ast_include *inc)
58 {
59         return inc ? inc->rname : NULL;
60 }
61
62 const char *ast_get_include_registrar(const struct ast_include *inc)
63 {
64         return inc ? inc->registrar : NULL;
65 }
66
67 int include_valid(const struct ast_include *inc)
68 {
69         if (!inc->hastime) {
70                 return 1;
71         }
72
73         return ast_check_timing(&(inc->timing));
74 }
75
76 struct ast_include *include_alloc(const char *value, const char *registrar)
77 {
78         struct ast_include *new_include;
79         char *c;
80         int valuebufsz = strlen(value) + 1;
81         char *p;
82
83         /* allocate new include structure ... */
84         new_include = ast_calloc(1, sizeof(*new_include) + (valuebufsz * 2));
85         if (!new_include) {
86                 return NULL;
87         }
88
89         /* Fill in this structure. Use 'p' for assignments, as the fields
90          * in the structure are 'const char *'
91          */
92         p = new_include->stuff;
93         new_include->name = p;
94         strcpy(p, value);
95         p += valuebufsz;
96         new_include->rname = p;
97         strcpy(p, value);
98         /* Strip off timing info, and process if it is there */
99         if ((c = strchr(p, ',')) ) {
100                 *c++ = '\0';
101                 new_include->hastime = ast_build_timing(&(new_include->timing), c);
102         }
103         new_include->registrar = registrar;
104
105         return new_include;
106 }
107
108 void include_free(struct ast_include *inc)
109 {
110         ast_destroy_timing(&(inc->timing));
111         ast_free(inc);
112 }