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 <ucx/map.h>
32
33 #include "../public/nsapi.h"
34 #include "../util/pblock.h"
35
36 #include "func.h"
37
38 UcxMap *function_map;
39
40 void func_init() {
41 function_map = ucx_map_new(
1337);
42 }
43
44 void add_function(FuncStruct *func) {
45 struct FuncStruct *f = malloc(
sizeof(FuncStruct));
46 *f = *func;
47 char *name = strdup(func->name);
48 for(
int i=
0;name[i]!=
'\0';i++) {
49 if(name[i] ==
'_') {
50 name[i] =
'-';
51 }
52 }
53 f->name = name;
54 log_ereport(
LOG_VERBOSE,
"add_function %s", f->name);
55 ucx_map_cstr_put(function_map, (
char*)f->name, f);
56 }
57
58 void add_functions(FuncStruct *funcs) {
59 int i =
0;
60 while(funcs[i].func !=
NULL) {
61 add_function(&funcs[i]);
62 i++;
63 }
64 }
65
66 FuncStruct* get_function(
char *name) {
67 for(
int i=
0;name[i]!=
'\0';i++) {
68 if(name[i] ==
'_') {
69 name[i] =
'-';
70 }
71 }
72 return ucx_map_cstr_get(function_map, name);
73 }
74
75 FuncStruct* func_resolve(pblock *pb, Session *sn, Request *rq) {
76 char *name = pblock_findkeyval(pb_key_fn, pb);
77 if(name ==
NULL) {
78
79 return NULL;
80 }
81
82 FuncStruct *func = get_function(name);
83 if(func ==
NULL) {
84
85 }
86 return func;
87 }
88
89 int func_exec(pblock *pb, Session *sn, Request *rq) {
90 FuncStruct *func = func_resolve(pb, sn, rq);
91 if(func ==
NULL) {
92 return REQ_ABORTED;
93 }
94 return func->func(pb, sn, rq);
95 }
96
97
98
99
100
101
102 struct FuncStruct func_insert(
char *name, FuncPtr fn) {
103 struct FuncStruct fc;
104 ZERO(&fc,
sizeof(FuncStruct));
105 fc.func = fn;
106 fc.name = strdup(name);
107 add_function(&fc);
108 return *get_function(name);
109 }
110