Merge "res_calendar: Specialized calendars depend on symbols of general calendar."
[asterisk/asterisk.git] / main / pbx_switch.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 PBX switch 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 #include "asterisk/_private.h"
33 #include "asterisk/cli.h"
34 #include "asterisk/linkedlists.h"
35 #include "asterisk/pbx.h"
36 #include "pbx_private.h"
37
38 static AST_RWLIST_HEAD_STATIC(switches, ast_switch);
39
40 struct ast_switch *pbx_findswitch(const char *sw)
41 {
42         struct ast_switch *asw;
43
44         AST_RWLIST_RDLOCK(&switches);
45         AST_RWLIST_TRAVERSE(&switches, asw, list) {
46                 if (!strcasecmp(asw->name, sw))
47                         break;
48         }
49         AST_RWLIST_UNLOCK(&switches);
50
51         return asw;
52 }
53
54 /*
55  * Append to the list. We don't have a tail pointer because we need
56  * to scan the list anyways to check for duplicates during insertion.
57  */
58 int ast_register_switch(struct ast_switch *sw)
59 {
60         struct ast_switch *tmp;
61
62         AST_RWLIST_WRLOCK(&switches);
63         AST_RWLIST_TRAVERSE(&switches, tmp, list) {
64                 if (!strcasecmp(tmp->name, sw->name)) {
65                         AST_RWLIST_UNLOCK(&switches);
66                         ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name);
67                         return -1;
68                 }
69         }
70         AST_RWLIST_INSERT_TAIL(&switches, sw, list);
71         AST_RWLIST_UNLOCK(&switches);
72
73         return 0;
74 }
75
76 void ast_unregister_switch(struct ast_switch *sw)
77 {
78         AST_RWLIST_WRLOCK(&switches);
79         AST_RWLIST_REMOVE(&switches, sw, list);
80         AST_RWLIST_UNLOCK(&switches);
81 }
82
83 /*! \brief  handle_show_switches: CLI support for listing registered dial plan switches */
84 static char *handle_show_switches(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
85 {
86         struct ast_switch *sw;
87
88         switch (cmd) {
89         case CLI_INIT:
90                 e->command = "core show switches";
91                 e->usage =
92                         "Usage: core show switches\n"
93                         "       List registered switches\n";
94                 return NULL;
95         case CLI_GENERATE:
96                 return NULL;
97         }
98
99         AST_RWLIST_RDLOCK(&switches);
100
101         if (AST_RWLIST_EMPTY(&switches)) {
102                 AST_RWLIST_UNLOCK(&switches);
103                 ast_cli(a->fd, "There are no registered alternative switches\n");
104                 return CLI_SUCCESS;
105         }
106
107         ast_cli(a->fd, "\n    -= Registered Asterisk Alternative Switches =-\n");
108         AST_RWLIST_TRAVERSE(&switches, sw, list)
109                 ast_cli(a->fd, "%s: %s\n", sw->name, sw->description);
110
111         AST_RWLIST_UNLOCK(&switches);
112
113         return CLI_SUCCESS;
114 }
115
116 static struct ast_cli_entry sw_cli[] = {
117         AST_CLI_DEFINE(handle_show_switches, "Show alternative switches"),
118 };
119
120 static void unload_pbx_switch(void)
121 {
122         ast_cli_unregister_multiple(sw_cli, ARRAY_LEN(sw_cli));
123 }
124
125 int load_pbx_switch(void)
126 {
127         ast_cli_register_multiple(sw_cli, ARRAY_LEN(sw_cli));
128         ast_register_cleanup(unload_pbx_switch);
129
130         return 0;
131 }