dict* dict_insert(dict* d, char *key, void *val) { if (d) { #ifdef TSTC dict_entry *ent = dict_entry_new(val, key); if (d->dict) { d->dict = tstc_insert(d->dict, key, ent); } else { d->dict = tstc_insert(NULL, key, ent); } #elif defined (HASHMAP) d->dict = hash_map_insert(d->dict, key, strlen(key), val); #else if (d->dict) { tst_insert(d->dict, key, val); } else { d->dict = tst_insert(NULL, key, val); } #endif d->n += 1; } return d; }
int MIME_add_type(const char *ext, const char *type) { if(!MAX_EXT_LEN) { MAX_EXT_LEN = Setting_get_int("limits.mime_ext_len", 128); log_info("MAX limits.mime_ext_len=%d", MAX_EXT_LEN); } bstring ext_rev = bfromcstr(ext); bReverse(ext_rev); bstring type_str = bfromcstr(type); check(blength(ext_rev) > 0, "No zero length MIME extensions allowed: %s:%s", ext, type); check(blength(type_str) > 0, "No zero length MIME types allowed: %s:%s", ext, type); check(ext[0] == '.', "Extensions must start with a . '%s:%s'", ext, type); check(blength(ext_rev) < MAX_EXT_LEN, "MIME extension %s:%s is longer than %d MAX (it's %d)", ext, type, MAX_EXT_LEN, blength(ext_rev)); check(!tst_search(MIME_MAP, bdata(ext_rev), blength(ext_rev)), "MIME extension %s already exists, can't add %s:%s", ext, ext, type); MIME_MAP = tst_insert(MIME_MAP, bdata(ext_rev), blength(ext_rev), type_str); bdestroy(ext_rev); return 0; error: bdestroy(ext_rev); bdestroy(type_str); return -1; }
void library_call(Module *state, array_t *params) { ARITY(library_call, 2); REQUIRES(library, 0, STR); REQUIRES(library, 1, STR); Token *ident = ARG(0); Token *as = ARG(1); Library *lib = Library_create(state, ident); if(lib) { // came out alright, so now set the name in our list as the second parameter lib->len = Token_copy(as, lib->name, MAX_LIB_NAME, TK_STR); if(lib->len == -1) { die(state, "requested module name %.*s is too long, must be less than %d.", as->len, as->start, MAX_LIB_NAME); } // store in the global library list state->libraries = tst_insert(state->libraries, lib->name, lib->len, lib); } else { exit(1); } }
void import(Module *state, array_t *params) { size_t length = 0; CORD input = NULL; Module *target = NULL; char in_file[MAX_LIB_NAME]; ARITY(import, 2); REQUIRES(import, 0, STR); REQUIRES(import, 1, STR); if(tst_search(state->imports, ARG(1)->data)) { // already loaded so just skip it return; } if(Token_copy(ARG(0), in_file, MAX_LIB_NAME, TK_STR) == -1) { die(state, "requested module name %s is too long, must be less than %d.", in_file, MAX_LIB_NAME); } input = mmap_file(in_file, &length); assert(input && "Failed to open the file you requested."); // compile the other file using the code size specified by the parent target = Module_create(in_file, state->max_code_size); // have to add it here to prevent recursive loads from going in a loop state->imports = tst_insert(state->imports, ARG(1)->start, ARG(1)->len, target); assert(state->imports && "Error importing into the parent namespace."); if(!Module_compile(target, input, length)) { die(target, "error parsing imported module %s.", in_file); } }
char *test_tst_insert() { node = tst_insert(node, bdata(test1), blength(test1), valueA); mu_assert(node != NULL, "Failed to insert into tst."); node = tst_insert(node, bdata(test2), blength(test2), value2); mu_assert(node != NULL, "Failed to insert into tst with second name."); node = tst_insert(node, bdata(test3), blength(test3), reverse); mu_assert(node != NULL, "Failed to insert into tst with reverse name."); node = tst_insert(node, bdata(test4), blength(test4), value4); mu_assert(node != NULL, "Failed to insert into tst with second name."); return NULL; }
void co_cmd_add(char *name, co_cmd_handler_t handler, char *usage, char *description, int mask) { DEBUG("Registering command %s", name); co_cmd_t *new_cmd = malloc(sizeof(co_cmd_t)); new_cmd->exec = handler; new_cmd->mask = mask; new_cmd->name = strdup(name); new_cmd->usage = strdup(usage); new_cmd->description = strdup(description); commands = tst_insert(commands, (char *)name, strlen(name), (void *)new_cmd); if(!commands) ERROR("Failed to register command %s!", name); return; }
void taskmain(int argc, char *argv[]) { LOG_FILE = stderr; glob_t profile_glob; int rc = 0; int i = 0; Action *action = NULL; tst_t *targets = NULL; bstring pid_file = NULL; check(argc == 3, "USAGE: procer <profile_dir> <procer_pid_file>"); pid_file = bfromcstr(argv[2]); rc = Unixy_remove_dead_pidfile(pid_file); check(rc == 0, "Failed to remove %s, procer is probably already running.", bdata(pid_file)); rc = Unixy_daemonize(); check(rc == 0, "Couldn't daemonize, that's not good."); rc = chdir(argv[1]); check(rc == 0, "Couldn't change to %s profile dir.", argv[1]); rc = Unixy_pid_file(pid_file); check(rc == 0, "Failed to make the PID file: %s", bdata(pid_file)); FILE *log = fopen("error.log", "a+"); check(log, "Couldn't open error.log"); setbuf(log, NULL); LOG_FILE = log; bstring dir_glob = bformat("%s/[A-Za-z0-9]*", argv[1]); check(dir_glob, "Couldn't make the directory glob."); rc = glob(bdata(dir_glob), GLOB_ERR | GLOB_ONLYDIR, NULL, &profile_glob); check(rc == 0, "Failed to find directories in the profiles."); debug("Loading %zu actions.", profile_glob.gl_pathc); for(i = 0; i < profile_glob.gl_pathc; i++) { action = Action_create(profile_glob.gl_pathv[i]); targets = tst_insert(targets, bdata(action->name), blength(action->name), action); } // now we setup the dependencies from the settings they've got tst_traverse(targets, Action_dependency_assign, targets); tst_traverse(targets, Action_start_all, NULL); taskexit(0); error: taskexitall(1); }
int Setting_add(const char *key, const char *value) { bstring key_str = bfromcstr(key); bstring value_str = bfromcstr(value); check(!tst_search(SETTINGS_MAP, bdata(key_str), blength(value_str)), "Setting key %s already exists, can't add %s:%s", key, key, value); SETTINGS_MAP = tst_insert(SETTINGS_MAP, bdata(key_str), blength(key_str), value_str); bdestroy(key_str); return 0; error: bdestroy(key_str); bdestroy(value_str); return -1; }
/** * call-seq: * uc.register("/someuri", SampleHandler.new) -> nil * * Registers the SampleHandler (one for all requests) with the "/someuri". * When URIClassifier::resolve is called with "/someuri" it'll return * SampleHandler immediately. When called with "/someuri/iwant" it'll also * return SomeHandler immediatly, with no additional searches, but it will * return path info with "/iwant". * * You actually can reuse this class to register nearly anything and * quickly resolve it. This could be used for caching, fast mapping, etc. * The downside is it uses much more memory than a Hash, but it can be * a lot faster. It's main advantage is that it works on prefixes, which * is damn hard to get right with a Hash. */ VALUE URIClassifier_register(VALUE self, VALUE uri, VALUE handler) { int rc = 0; void *ptr = NULL; struct tst *tst = NULL; DATA_GET(self, struct tst, tst); rc = tst_insert((unsigned char *)StringValueCStr(uri), (void *)handler , tst, 0, &ptr); if(rc == TST_DUPLICATE_KEY) { rb_raise(rb_eStandardError, "Handler already registered with that name"); } else if(rc == TST_ERROR) { rb_raise(rb_eStandardError, "Memory error registering handler"); } else if(rc == TST_NULL_KEY) { rb_raise(rb_eStandardError, "URI was empty"); } rb_hash_aset(rb_ivar_get(self, id_handler_map), uri, handler); return Qnil; }
void taskmain(int argc, char *argv[]) { dbg_set_log(stderr); glob_t profile_glob; int rc = 0; int i = 0; Action *action = NULL; tst_t *targets = NULL; bstring pid_file = NULL; char *procer_error_log = NULL; m2program = procer_program; check(argc == 3, "USAGE: %s <profile_dir> <procer_pid_file>", m2program); pid_file = bfromcstr(argv[2]); srand(time(NULL)); // simple randomness rc = Unixy_remove_dead_pidfile(pid_file); check(rc == 0, "Failed to remove %s, %s is probably already running.", bdata(pid_file), m2program); rc = Unixy_daemonize(1); check(rc == 0, "Couldn't daemonize, that's not good."); rc = chdir(argv[1]); check(rc == 0, "Couldn't change to %s profile dir.", argv[1]); rc = Unixy_pid_file(pid_file); check(rc == 0, "Failed to make the PID file: %s", bdata(pid_file)); if( (procer_error_log = getenv("PROCER_ERROR_LOG")) == NULL) procer_error_log = "error.log"; FILE *log = fopen(procer_error_log, "a+"); check(log, "Couldn't open error.log"); setbuf(log, NULL); dbg_set_log(log); bstring dir_glob = bformat("%s/[A-Za-z0-9]*", argv[1]); check(dir_glob, "Couldn't make the directory glob."); rc = glob(bdata(dir_glob), GLOB_ERR, NULL, &profile_glob); check(rc == 0, "Failed to find directories in the profiles."); struct stat sb; debug("Loading %zu actions.", profile_glob.gl_pathc); start_terminator(); while(RUNNING) { for(i = 0; i < profile_glob.gl_pathc; i++) { rc = lstat(profile_glob.gl_pathv[i], &sb); check(rc == 0, "Failed to stat file or directory: %s", profile_glob.gl_pathv[i]); if (sb.st_mode & S_IFDIR) { action = Action_create(profile_glob.gl_pathv[i]); targets = tst_insert(targets, bdata(action->name), blength(action->name), action); } } // now we setup the dependencies from the settings they've got tst_traverse(targets, Action_dependency_assign, targets); tst_traverse(targets, Action_start_all, NULL); while(RUNNING) { Action_sleep(1); } } log_warn("Exiting as requested from procer."); taskexit(0); error: taskexitall(1); }
/* ** Perform a reduce action and the shift that must immediately ** follow the reduce. */ static void yy_reduce( yyParser *yypParser, /* The parser */ int yyruleno /* Number of the rule by which to reduce */ ){ int yygoto; /* The next state */ int yyact; /* The next action */ YYMINORTYPE yygotominor; /* The LHS of the rule reduced */ yyStackEntry *yymsp; /* The top of the parser's stack */ int yysize; /* Amount to pop the stack */ ParseARG_FETCH; yymsp = &yypParser->yystack[yypParser->yyidx]; #ifndef NDEBUG if( yyTraceFILE && yyruleno>=0 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt, yyRuleName[yyruleno]); } #endif /* NDEBUG */ /* Silence complaints from purify about yygotominor being uninitialized ** in some cases when it is copied into the stack after the following ** switch. yygotominor is uninitialized when a rule reduces that does ** not set the value of its left-hand side nonterminal. Leaving the ** value of the nonterminal uninitialized is utterly harmless as long ** as the value is never used. So really the only thing this code ** accomplishes is to quieten purify. ** ** 2007-01-16: The wireshark project (www.wireshark.org) reports that ** without this code, their parser segfaults. I'm not sure what there ** parser is doing to make this happen. This is the second bug report ** from wireshark this week. Clearly they are stressing Lemon in ways ** that it has not been previously stressed... (SQLite ticket #2172) */ /*memset(&yygotominor, 0, sizeof(yygotominor));*/ yygotominor = yyzerominor; switch( yyruleno ){ /* Beginning here are the reduction cases. A typical example ** follows: ** case 0: ** #line <lineno> <grammarfile> ** { ... } // User supplied code ** #line <lineno> <thisfile> ** break; */ case 0: /* config ::= vars */ #line 30 "src/parser.y" { state->settings = yymsp[0].minor.yy13; } #line 754 "src/parser.c" break; case 1: /* vars ::= vars assignment */ #line 34 "src/parser.y" { yygotominor.yy13 = tst_insert(yymsp[-1].minor.yy13, bdata(yymsp[0].minor.yy7->key->data), blength(yymsp[0].minor.yy7->key->data), yymsp[0].minor.yy7); } #line 761 "src/parser.c" break; case 2: /* vars ::= assignment */ #line 39 "src/parser.y" { yygotominor.yy13 = tst_insert(yygotominor.yy13, bdata(yymsp[0].minor.yy7->key->data), blength(yymsp[0].minor.yy7->key->data), yymsp[0].minor.yy7); } #line 768 "src/parser.c" break; case 3: /* vars ::= vars EOF */ #line 43 "src/parser.y" { yygotominor.yy13 = yymsp[-1].minor.yy13; yy_destructor(yypParser,1,&yymsp[0].minor); } #line 774 "src/parser.c" break; case 4: /* expr ::= QSTRING */ #line 47 "src/parser.y" { yygotominor.yy8 = Value_create(VAL_QSTRING, yymsp[0].minor.yy0); } #line 779 "src/parser.c" break; case 5: /* expr ::= NUMBER */ #line 48 "src/parser.y" { yygotominor.yy8 = Value_create(VAL_NUMBER, yymsp[0].minor.yy0); } #line 784 "src/parser.c" break; case 6: /* expr ::= class */ #line 49 "src/parser.y" { yygotominor.yy8 = Value_create(VAL_CLASS, yymsp[0].minor.yy11); } #line 789 "src/parser.c" break; case 7: /* expr ::= list */ #line 50 "src/parser.y" { yygotominor.yy8 = Value_create(VAL_LIST, yymsp[0].minor.yy46); } #line 794 "src/parser.c" break; case 8: /* expr ::= hash */ #line 51 "src/parser.y" { yygotominor.yy8 = Value_create(VAL_HASH, yymsp[0].minor.yy13); } #line 799 "src/parser.c" break; case 9: /* expr ::= IDENT */ #line 52 "src/parser.y" { yygotominor.yy8 = Value_create(VAL_REF, yymsp[0].minor.yy0); } #line 804 "src/parser.c" break; case 10: /* assignment ::= IDENT EQ expr */ #line 57 "src/parser.y" { yygotominor.yy7 = malloc(sizeof(Pair)); yygotominor.yy7->key = yymsp[-2].minor.yy0; yygotominor.yy7->value = yymsp[0].minor.yy8; yy_destructor(yypParser,5,&yymsp[-1].minor); } #line 812 "src/parser.c" break; case 11: /* class ::= CLASS LPAREN parameters RPAREN */ #line 64 "src/parser.y" { yygotominor.yy11 = calloc(sizeof(Class), 1); yygotominor.yy11->id = -1; yygotominor.yy11->ident = yymsp[-3].minor.yy0; yygotominor.yy11->params = yymsp[-1].minor.yy13; yy_destructor(yypParser,7,&yymsp[-2].minor); yy_destructor(yypParser,8,&yymsp[0].minor); } #line 819 "src/parser.c" break; case 12: /* parameters ::= parameters COMMA assignment */ #line 69 "src/parser.y" { yygotominor.yy13 = tst_insert(yymsp[-2].minor.yy13, bdata(yymsp[0].minor.yy7->key->data), blength(yymsp[0].minor.yy7->key->data), yymsp[0].minor.yy7); yy_destructor(yypParser,9,&yymsp[-1].minor); } #line 825 "src/parser.c" break; case 13: /* parameters ::= parameters assignment */ #line 72 "src/parser.y" { yygotominor.yy13 = tst_insert(yymsp[-1].minor.yy13, bdata(yymsp[0].minor.yy7->key->data), blength(yymsp[0].minor.yy7->key->data), yymsp[0].minor.yy7); } #line 830 "src/parser.c" break; case 14: /* parameters ::= */ case 22: /* hash_elements ::= */ yytestcase(yyruleno==22); #line 75 "src/parser.y" { yygotominor.yy13 = NULL; } #line 836 "src/parser.c" break; case 15: /* list ::= LBRACE list_elements RBRACE */ #line 79 "src/parser.y" { yygotominor.yy46 = yymsp[-1].minor.yy46; yy_destructor(yypParser,10,&yymsp[-2].minor); yy_destructor(yypParser,11,&yymsp[0].minor); } #line 843 "src/parser.c" break; case 16: /* list_elements ::= list_elements COMMA expr */ #line 83 "src/parser.y" { yygotominor.yy46 = yymsp[-2].minor.yy46; list_append(yygotominor.yy46, lnode_create(yymsp[0].minor.yy8)); yy_destructor(yypParser,9,&yymsp[-1].minor); } #line 849 "src/parser.c" break; case 17: /* list_elements ::= list_elements expr */ #line 86 "src/parser.y" { yygotominor.yy46 = yymsp[-1].minor.yy46; list_append(yygotominor.yy46, lnode_create(yymsp[0].minor.yy8)); } #line 854 "src/parser.c" break; case 18: /* list_elements ::= */ #line 89 "src/parser.y" { yygotominor.yy46 = list_create(LISTCOUNT_T_MAX); } #line 859 "src/parser.c" break; case 19: /* hash ::= LBRACKET hash_elements RBRACKET */ #line 93 "src/parser.y" { yygotominor.yy13 = yymsp[-1].minor.yy13; yy_destructor(yypParser,12,&yymsp[-2].minor); yy_destructor(yypParser,13,&yymsp[0].minor); } #line 866 "src/parser.c" break; case 20: /* hash_elements ::= hash_elements COMMA hash_pair */ #line 97 "src/parser.y" { yygotominor.yy13 = tst_insert(yymsp[-2].minor.yy13, bdata(yymsp[0].minor.yy17->key->data), blength(yymsp[0].minor.yy17->key->data), yymsp[0].minor.yy17); yy_destructor(yypParser,9,&yymsp[-1].minor); } #line 872 "src/parser.c" break; case 21: /* hash_elements ::= hash_elements hash_pair */ #line 100 "src/parser.y" { yygotominor.yy13 = tst_insert(yymsp[-1].minor.yy13, bdata(yymsp[0].minor.yy17->key->data), blength(yymsp[0].minor.yy17->key->data), yymsp[0].minor.yy17); } #line 877 "src/parser.c" break; case 23: /* hash_pair ::= QSTRING COLON expr */ #line 108 "src/parser.y" { yygotominor.yy17 = malloc(sizeof(Pair)); yygotominor.yy17->key = yymsp[-2].minor.yy0; yygotominor.yy17->value = yymsp[0].minor.yy8; yy_destructor(yypParser,14,&yymsp[-1].minor); } #line 885 "src/parser.c" break; default: break; }; yygoto = yyRuleInfo[yyruleno].lhs; yysize = yyRuleInfo[yyruleno].nrhs; yypParser->yyidx -= yysize; yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto); if( yyact < YYNSTATE ){ #ifdef NDEBUG /* If we are not debugging and the reduce action popped at least ** one element off the stack, then we can push the new element back ** onto the stack here, and skip the stack overflow test in yy_shift(). ** That gives a significant speed improvement. */ if( yysize ){ yypParser->yyidx++; yymsp -= yysize-1; yymsp->stateno = (YYACTIONTYPE)yyact; yymsp->major = (YYCODETYPE)yygoto; yymsp->minor = yygotominor; }else #endif { yy_shift(yypParser,yyact,yygoto,&yygotominor); } }else{ assert( yyact == YYNSTATE + YYNRULE + 1 ); yy_accept(yypParser); } }