BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file choose.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Multiple value selection based on boolean conditions.
32 *
33 * Synopsis:
34 * choose({{string cond1, result1}, ..., {string condN, resultN}}, default_result)
35 *
36 * Variables:
37 * (empty) - If cond1="true" then result1,
38 * else if cond2="true" then result2,
39 * ...,
40 * else default_result.
41 */
42  
43 #include <stdlib.h>
44 #include <string.h>
45  
46 #include <ncd/module_common.h>
47  
48 #include <generated/blog_channel_ncd_choose.h>
49  
50 struct instance {
51 NCDModuleInst *i;
52 NCDValRef result;
53 };
54  
55 static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
56 {
57 struct instance *o = vo;
58 o->i = i;
59  
60 // read arguments
61 NCDValRef arg_choices;
62 NCDValRef arg_default_result;
63 if (!NCDVal_ListRead(params->args, 2, &arg_choices, &arg_default_result)) {
64 ModuleLog(i, BLOG_ERROR, "wrong arity");
65 goto fail0;
66 }
67 if (!NCDVal_IsList(arg_choices)) {
68 ModuleLog(i, BLOG_ERROR, "wrong type");
69 goto fail0;
70 }
71  
72 // iterate choices
73 int have_result = 0;
74 size_t count = NCDVal_ListCount(arg_choices);
75 for (size_t j = 0; j < count; j++) {
76 NCDValRef c = NCDVal_ListGet(arg_choices, j);
77  
78 // check choice type
79 if (!NCDVal_IsList(c)) {
80 ModuleLog(i, BLOG_ERROR, "wrong choice type");
81 goto fail0;
82 }
83  
84 // read choice
85 NCDValRef c_cond;
86 NCDValRef c_result;
87 if (!NCDVal_ListRead(c, 2, &c_cond, &c_result)) {
88 ModuleLog(i, BLOG_ERROR, "wrong choice contents arity");
89 goto fail0;
90 }
91 int c_cond_val;
92 if (!ncd_read_boolean(c_cond, &c_cond_val)) {
93 ModuleLog(i, BLOG_ERROR, "wrong choice condition");
94 goto fail0;
95 }
96  
97 // update result
98 if (!have_result && c_cond_val) {
99 o->result = c_result;
100 have_result = 1;
101 }
102 }
103  
104 // default?
105 if (!have_result) {
106 o->result = arg_default_result;
107 }
108  
109 // signal up
110 NCDModuleInst_Backend_Up(o->i);
111 return;
112  
113 fail0:
114 NCDModuleInst_Backend_DeadError(i);
115 }
116  
117 static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
118 {
119 struct instance *o = vo;
120  
121 if (name == NCD_STRING_EMPTY) {
122 *out = NCDVal_NewCopy(mem, o->result);
123 return 1;
124 }
125  
126 return 0;
127 }
128  
129 static struct NCDModule modules[] = {
130 {
131 .type = "choose",
132 .func_new2 = func_new,
133 .func_getvar2 = func_getvar2,
134 .alloc_size = sizeof(struct instance)
135 }, {
136 .type = NULL
137 }
138 };
139  
140 const struct NCDModuleGroup ncdmodule_choose = {
141 .modules = modules
142 };