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 <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 // TODO: error 83 return NULL; 84 } 85 86 FuncStruct *func = get_function(name); 87 if(func == NULL) { 88 // TODO: error 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 // public API 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