UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2013 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 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 // TODO: error 79 return NULL; 80 } 81 82 FuncStruct *func = get_function(name); 83 if(func == NULL) { 84 // TODO: error 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 // public API 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