Fix app bug, update skel example, add skel to makefile as option (bug #3869)
[asterisk/asterisk.git] / apps / app_skel.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Skeleton application
5  * 
6  * Copyright (C) <Year>, <Your Name Here>
7  *
8  * <Your Name Here> <<You Email Here>>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/module.h>
19 #include <asterisk/lock.h>
20 #include <asterisk/app.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24
25 static char *tdesc = "Trivial skeleton Application";
26 static char *app = "skel";
27 static char *synopsis = 
28 "  This is a skeleton application that shows you the basic structure to create your\n"
29 "own asterisk applications.\n";
30
31 #define OPTION_A        (1 << 0)        /* Option A */
32 #define OPTION_B        (1 << 1)        /* Option B(n) */
33 #define OPTION_C        (1 << 2)        /* Option C(str) */
34 #define OPTION_NULL     (1 << 3)        /* Dummy Termination */
35
36 AST_DECLARE_OPTIONS(app_opts,{
37         ['a'] = { OPTION_A },
38         ['b'] = { OPTION_B, 1 },
39         ['c'] = { OPTION_C, 2 }
40 });
41
42 STANDARD_LOCAL_USER;
43
44 LOCAL_USER_DECL;
45
46 static int app_exec(struct ast_channel *chan, void *data)
47 {
48         int res = 0;
49         struct ast_flags flags;
50         struct localuser *u;
51         char *options=NULL;
52         char *dummy = NULL;
53         char *args;
54         int argc = 0;
55         char *opts[2];
56         char *argv[2];
57
58         if (!(args = ast_strdupa((char *)data))) {
59                 ast_log(LOG_ERROR, "Out of memory!\n");
60                 return -1;
61         }
62
63         if (!data) {
64                 ast_log(LOG_WARNING, "%s requires an argument (dummy|[options])\n",app);
65                 return -1;
66         }
67
68         LOCAL_USER_ADD(u);
69         if ((argc = ast_separate_app_args(args, '|', argv, sizeof(argv) / sizeof(char *)))) {
70                 dummy = argv[0];
71                 options = argv[1];
72                 ast_parseoptions(app_opts, &flags, opts, options);
73         }
74
75
76         if (dummy && !ast_strlen_zero(dummy)) 
77                 ast_log(LOG_NOTICE, "Dummy value is : %s\n", dummy);
78
79         if (ast_test_flag(&flags, OPTION_A))
80                 ast_log(LOG_NOTICE, "Option A is set\n");
81
82         if (ast_test_flag(&flags, OPTION_B))
83                 ast_log(LOG_NOTICE,"Option B is set with : %s\n", opts[0] ? opts[0] : "<unspecified>");
84
85         if (ast_test_flag(&flags, OPTION_C))
86                 ast_log(LOG_NOTICE,"Option C is set with : %s\n", opts[1] ? opts[1] : "<unspecified>");
87
88         /* Do our thing here */
89         LOCAL_USER_REMOVE(u);
90         return res;
91 }
92
93 int unload_module(void)
94 {
95         STANDARD_HANGUP_LOCALUSERS;
96         return ast_unregister_application(app);
97 }
98
99 int load_module(void)
100 {
101         return ast_register_application(app, app_exec, tdesc, synopsis);
102 }
103
104 char *description(void)
105 {
106         return tdesc;
107 }
108
109 int usecount(void)
110 {
111         int res;
112         STANDARD_USECOUNT(res);
113         return res;
114 }
115
116 char *key()
117 {
118         return ASTERISK_GPL_KEY;
119 }