1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 #include <stdlib.h>
31 #include <cx/map.h>
32 #include <cx/hash_map.h>
33
34 #include "../public/nsapi.h"
35 #include "../util/pblock.h"
36
37 #include "func.h"
38
39 CxMap *function_map;
40
41 void func_init() {
42 function_map = cxHashMapCreate(cxDefaultAllocator,
CX_STORE_POINTERS,
256);
43 }
44
45 void add_function(FuncStruct *func) {
46 struct FuncStruct *f = malloc(
sizeof(FuncStruct));
47 *f = *func;
48 char *name = strdup(func->name);
49 for(
int i=
0;name[i]!=
'\0';i++) {
50 if(name[i] ==
'_') {
51 name[i] =
'-';
52 }
53 }
54 f->name = name;
55 log_ereport(
LOG_VERBOSE,
"add_function %s", f->name);
56 cxMapPut(function_map, cx_hash_key_str(f->name), f);
57 }
58
59 void add_functions(FuncStruct *funcs) {
60 int i =
0;
61 while(funcs[i].func !=
NULL) {
62 add_function(&funcs[i]);
63 i++;
64 }
65 }
66
67 FuncStruct* get_function(
const char *name) {
68 char *name_dup = strdup(name);
69 for(
int i=
0;name_dup[i]!=
'\0';i++) {
70 if(name_dup[i] ==
'_') {
71 name_dup[i] =
'-';
72 }
73 }
74 void *ret = cxMapGet(function_map, cx_hash_key_str(name_dup));
75 free(name_dup);
76 return ret;
77 }
78
79 FuncStruct* func_resolve(pblock *pb, Session *sn, Request *rq) {
80 char *name = pblock_findkeyval(pb_key_fn, pb);
81 if(name ==
NULL) {
82
83 return NULL;
84 }
85
86 FuncStruct *func = get_function(name);
87 if(func ==
NULL) {
88
89 }
90 return func;
91 }
92
93 int func_exec(pblock *pb, Session *sn, Request *rq) {
94 FuncStruct *func = func_resolve(pb, sn, rq);
95 if(func ==
NULL) {
96 return REQ_ABORTED;
97 }
98 return func->func(pb, sn, rq);
99 }
100
101
102
103
104
105
106 struct FuncStruct func_insert(
char *name, FuncPtr fn) {
107 struct FuncStruct fc;
108 ZERO(&fc,
sizeof(FuncStruct));
109 fc.func = fn;
110 fc.name = strdup(name);
111 add_function(&fc);
112 return *get_function(name);
113 }
114