BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file objref.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 | * Synopsis: |
||
32 | * objref(list(string) target) |
||
33 | * objref_arg(list(string) target) |
||
34 | */ |
||
35 | |||
36 | #include <misc/debug.h> |
||
37 | #include <misc/balloc.h> |
||
38 | |||
39 | #include <ncd/module_common.h> |
||
40 | |||
41 | #include <generated/blog_channel_ncd_objref.h> |
||
42 | |||
43 | struct instance { |
||
44 | NCDModuleInst *i; |
||
45 | NCDObjRef ref; |
||
46 | }; |
||
47 | |||
48 | static void func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_arg) |
||
49 | { |
||
50 | ASSERT(is_arg == 0 || is_arg == 1) |
||
51 | |||
52 | struct instance *o = vo; |
||
53 | o->i = i; |
||
54 | |||
55 | NCDValRef target_arg; |
||
56 | if (!NCDVal_ListRead(params->args, 1, &target_arg)) { |
||
57 | ModuleLog(i, BLOG_ERROR, "wrong arity"); |
||
58 | goto fail0; |
||
59 | } |
||
60 | |||
61 | if (!NCDVal_IsList(target_arg)) { |
||
62 | ModuleLog(i, BLOG_ERROR, "argument is not a list"); |
||
63 | goto fail0; |
||
64 | } |
||
65 | |||
66 | size_t num_names = NCDVal_ListCount(target_arg); |
||
67 | if (num_names == 0) { |
||
68 | ModuleLog(i, BLOG_ERROR, "target list is empty"); |
||
69 | goto fail0; |
||
70 | } |
||
71 | |||
72 | size_t total_names = is_arg + num_names; |
||
73 | |||
74 | NCD_string_id_t *names = BAllocArray(total_names, sizeof(*names)); |
||
75 | if (!names) { |
||
76 | ModuleLog(i, BLOG_ERROR, "BAllocArray failed"); |
||
77 | goto fail0; |
||
78 | } |
||
79 | |||
80 | if (is_arg) { |
||
81 | names[0] = NCD_STRING_CALLER; |
||
82 | } |
||
83 | |||
84 | for (size_t j = 0; j < num_names; j++) { |
||
85 | NCDValRef name_val = NCDVal_ListGet(target_arg, j); |
||
86 | |||
87 | if (!NCDVal_IsString(name_val)) { |
||
88 | ModuleLog(i, BLOG_ERROR, "target component is not a string"); |
||
89 | goto fail1; |
||
90 | } |
||
91 | |||
92 | NCD_string_id_t name_id = ncd_get_string_id(name_val); |
||
93 | if (name_id < 0) { |
||
94 | ModuleLog(i, BLOG_ERROR, "ncd_get_string_id failed"); |
||
95 | goto fail1; |
||
96 | } |
||
97 | |||
98 | names[is_arg + j] = name_id; |
||
99 | } |
||
100 | |||
101 | NCDObject first_obj; |
||
102 | if (!NCDModuleInst_Backend_GetObj(i, names[0], &first_obj)) { |
||
103 | ModuleLog(i, BLOG_ERROR, "first target object not found"); |
||
104 | goto fail1; |
||
105 | } |
||
106 | |||
107 | NCDObject obj; |
||
108 | if (!NCDObject_ResolveObjExprCompact(&first_obj, names + 1, total_names - 1, &obj)) { |
||
109 | ModuleLog(i, BLOG_ERROR, "non-first target object not found"); |
||
110 | goto fail1; |
||
111 | } |
||
112 | |||
113 | NCDPersistentObj *pobj = NCDObject_Pobj(&obj); |
||
114 | if (!pobj) { |
||
115 | ModuleLog(i, BLOG_ERROR, "object does not support references"); |
||
116 | goto fail1; |
||
117 | } |
||
118 | |||
119 | NCDObjRef_Init(&o->ref, pobj); |
||
120 | |||
121 | BFree(names); |
||
122 | |||
123 | NCDModuleInst_Backend_Up(i); |
||
124 | return; |
||
125 | |||
126 | fail1: |
||
127 | BFree(names); |
||
128 | fail0: |
||
129 | NCDModuleInst_Backend_DeadError(i); |
||
130 | } |
||
131 | |||
132 | static void func_new_objref (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
133 | { |
||
134 | func_new_common(vo, i, params, 0); |
||
135 | } |
||
136 | |||
137 | static void func_new_objref_arg (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
138 | { |
||
139 | func_new_common(vo, i, params, 1); |
||
140 | } |
||
141 | |||
142 | static void func_die (void *vo) |
||
143 | { |
||
144 | struct instance *o = vo; |
||
145 | |||
146 | NCDObjRef_Free(&o->ref); |
||
147 | |||
148 | NCDModuleInst_Backend_Dead(o->i); |
||
149 | } |
||
150 | |||
151 | static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object) |
||
152 | { |
||
153 | struct instance *o = vo; |
||
154 | |||
155 | NCDObject obj; |
||
156 | if (!NCDObjRef_Deref(&o->ref, &obj)) { |
||
157 | ModuleLog(o->i, BLOG_ERROR, "object reference has been broken"); |
||
158 | return 0; |
||
159 | } |
||
160 | |||
161 | if (name == NCD_STRING_EMPTY) { |
||
162 | *out_object = obj; |
||
163 | return 1; |
||
164 | } |
||
165 | |||
166 | return NCDObject_GetObj(&obj, name, out_object); |
||
167 | } |
||
168 | |||
169 | static struct NCDModule modules[] = { |
||
170 | { |
||
171 | .type = "objref", |
||
172 | .func_new2 = func_new_objref, |
||
173 | .func_die = func_die, |
||
174 | .func_getobj = func_getobj, |
||
175 | .alloc_size = sizeof(struct instance) |
||
176 | }, { |
||
177 | .type = "objref_arg", |
||
178 | .func_new2 = func_new_objref_arg, |
||
179 | .func_die = func_die, |
||
180 | .func_getobj = func_getobj, |
||
181 | .alloc_size = sizeof(struct instance) |
||
182 | }, { |
||
183 | .type = NULL |
||
184 | } |
||
185 | }; |
||
186 | |||
187 | const struct NCDModuleGroup ncdmodule_objref = { |
||
188 | .modules = modules |
||
189 | }; |