int init_cache(char* host, unsigned short port, char* servicename) { int err; err = !rpc_init(0); if(err) { printf("rpc_init failed\n"); return 1; } service_functions = hm_create(25L, 0.75); printf("connection to %s:%d.%s...\n",host,port,servicename); rpc = rpc_connect(host, port, servicename, 1l); if(rpc==0) { printf("rpc_connect failed\n"); return 1; } rps = rpc_offer(MY_SERVICE_NAME); if(!rps) { printf("Offer failed!\n"); return 1; } rpc_details(lhost, &lport); err = pthread_create(&serviceThread, NULL, (void*)_service_handler, NULL); if(err) { printf("could not create the thread to handle events\n"); return 1; } return 0; }
// Compiles a function declaration void compile_function(compiler_wrapper *cw, ast_node *root) { ast_func_decl_node *node = (ast_func_decl_node *)root; // We want to build a new compiler wrapper for building // the function in a new context. compiler_wrapper nw; nw.local_idx = 0; nw.saved_locals = hm_create(100, 1); nw.rnames = arr_create(10); nw.rindices = arr_create(100); nw.used_names = copy_arraylist(cw->used_names); nw.repl = 0; nw.impl_name = node->impl_name; // Deal with parameters int argc = 0; ast_value_node *v = (ast_value_node *)node->params; for(; v; v = (ast_value_node *)v->next) { char *idf = v->value.s; char *nid = malloc(strlen(idf) + 1); strcpy(nid, idf); arr_append(&nw.rnames, nid); argc++; } nw.save_val = 0; lky_object_code *code = compile_ast_ext(node->payload->next, &nw); if(node->refname) { char *refname = node->refname; code->refname = malloc(strlen(refname) + 1); strcpy(code->refname, refname); } long idx = cw->rcon.count; arr_append(&cw->rcon, code); append_op(cw, LI_LOAD_CONST, node->lineno); unsigned char buf[4]; int_to_byte_array(buf, idx); append_op(cw, buf[0], node->lineno); append_op(cw, buf[1], node->lineno); append_op(cw, buf[2], node->lineno); append_op(cw, buf[3], node->lineno); append_op(cw, LI_MAKE_FUNCTION, node->lineno); append_op(cw, argc, node->lineno); }
/****************************** LOCAL FUNCTIONS *******************************/ CH_IR_RET_E ch_ir_indexer_init( CH_IR_INDEXER_INIT_PARAMS_X *px_init_params, CH_IR_INDEXER_CTXT_X **ppx_indexer_ctxt) { CH_IR_RET_E e_ret_val = eCH_IR_RET_FAILURE; CH_IR_INDEXER_CTXT_X *px_indexer_ctxt = NULL; CH_IR_TOKENIZER_INIT_PARAMS_X x_tokenizer_init_params = {0}; CH_IR_DIR_PARSER_INIT_PARAMS_X x_dir_parser_init_params = {0}; HM_RET_E e_hm_ret = eHM_RET_FAILURE; HM_INIT_PARAMS_X x_hm_init_params = { 0 }; if ((NULL == px_init_params) || (NULL == ppx_indexer_ctxt)) { CH_IR_LOG_MED("Invalid Args"); e_ret_val = eCH_IR_RET_INVALID_ARGS; goto CLEAN_RETURN; } if ((0 == px_init_params->ui_max_filepath_len) || (0 == px_init_params->ui_max_token_len) || (0 == px_init_params->ui_token_hm_table_size) || (0 == px_init_params->ui_postings_hm_table_size)) { CH_IR_LOG_MED("Invalid Args"); e_ret_val = eCH_IR_RET_INVALID_ARGS; goto CLEAN_RETURN; } px_indexer_ctxt = pal_malloc (sizeof(CH_IR_INDEXER_CTXT_X), NULL); if (NULL == px_indexer_ctxt) { CH_IR_LOG_MED("pal_malloc failed"); e_ret_val = eCH_IR_RET_RESOURCE_FAILURE; goto CLEAN_RETURN; } (void) pal_memmove(&(px_indexer_ctxt->x_init_params), px_init_params, sizeof(px_indexer_ctxt->x_init_params)); x_tokenizer_init_params.ui_max_token_len = px_init_params->ui_max_token_len; x_tokenizer_init_params.fn_ch_ir_for_each_token_cbk = ch_ir_indexer_for_each_token_cbk; x_tokenizer_init_params.p_app_data = px_indexer_ctxt; e_ret_val = ch_ir_tokenizer_init(&x_tokenizer_init_params, &(px_indexer_ctxt->px_tokenizer_ctxt)); if (eCH_IR_RET_SUCCESS != e_ret_val) { CH_IR_LOG_MED("ch_ir_tokenizer_init failed: %d", e_ret_val); goto CLEAN_RETURN; } x_dir_parser_init_params.ui_max_filepath_len = px_init_params->ui_max_filepath_len; x_dir_parser_init_params.fn_ch_ir_for_each_file_cbk = ch_ir_indexer_for_each_file_cbk; x_dir_parser_init_params.p_app_data = px_indexer_ctxt; e_ret_val = ch_ir_dir_parser_init(&x_dir_parser_init_params, &(px_indexer_ctxt->px_dir_parser_ctxt)); if (eCH_IR_RET_SUCCESS != e_ret_val) { CH_IR_LOG_MED("ch_ir_dir_parser_init failed: %d", e_ret_val); goto CLEAN_RETURN; } if (true == px_init_params->b_ignore_stopwords) { ch_ir_indexer_create_stopwords_cache_hm (px_indexer_ctxt, px_init_params->uca_stopwords_filepath); } x_hm_init_params.e_hm_key_type = eHM_KEY_TYPE_STRING; x_hm_init_params.b_maintain_linked_list = true; x_hm_init_params.ui_linked_list_flags |= eHM_LINKED_LIST_FLAGS_BM_UNSORTED; x_hm_init_params.ui_hm_table_size = px_init_params->ui_token_hm_table_size; e_hm_ret = hm_create (&(px_indexer_ctxt->hl_token_hm), &x_hm_init_params); if (eHM_RET_SUCCESS != e_hm_ret) { CH_IR_LOG_MED("hm_create failed: %d", e_hm_ret); e_ret_val = eCH_IR_RET_RESOURCE_FAILURE; goto CLEAN_RETURN; } *ppx_indexer_ctxt = px_indexer_ctxt; e_ret_val = eCH_IR_RET_SUCCESS; CLEAN_RETURN: if (eCH_IR_RET_SUCCESS != e_ret_val) { (void) ch_ir_indexer_deinit(px_indexer_ctxt); } return e_ret_val; }
int main(int argc, char **argv) { (void) signal(SIGINT, sigint_received); settings = hm_create(10, 0.5, 7); batt_info = hm_create(10, 0.5, 7); read_settings(); strcpy(acpi, hm_get(settings, "acpi_location")); strcpy(acpi_alarm, acpi); strcat(acpi_alarm, "/alarm\0"); strcpy(acpi_info, acpi); strcat(acpi_info, "/info\0"); strcpy(acpi_state, acpi); strcat(acpi_state, "/state\0"); read_acpi(); /* Handle main arguments */ struct option opt_list[] = { {"beep", 0, NULL, 'b'}, {"help", 0, NULL, 'h'}, {"version", 0, NULL, 'v'}, {"debug", 0, NULL, 'd'}, {0,0,0,0} }; int debugging = 0; int arg = EOF; while((arg = getopt_long(argc, argv, "bhvd", opt_list, NULL)) != EOF) { switch (arg) { case 'b': beep(beep_frequency, beep_duration, beep_repetitions, beep_pause, beep_increment); exit(EXIT_SUCCESS); case 'h': print_help(); exit(EXIT_SUCCESS); case 'v': print_version(); exit(EXIT_SUCCESS); case 'd': debugging = 1; break; default: print_usage(); exit(EXIT_FAILURE); } } if (debugging) { debug_print(); } else if (!daemonize(PID_FILE, "/", 0)) { exit(EXIT_FAILURE); } unsigned int poll_count = 0; unsigned int warning_count = 0; while (1) { if ((sleep_timeout * (poll_count + 1)) % poll_timeout == 0) { read_acpi(); if (debugging) { printf("> Reading acpi\n"); } } if ((sleep_timeout * (warning_count + 1)) % warning_timeout == 0) { if (capacity_factor <= (float)warning_level / 100.0 && strcmp(bat_state, "discharging") == 0) { beep(beep_frequency, beep_duration, beep_repetitions, beep_pause, beep_increment); } if (debugging) { printf("> Capacity: %.2f%%\tState: %s\n", capacity_factor * 100, bat_state); } } poll_count++; poll_count = poll_count % (poll_timeout / sleep_timeout); warning_count++; warning_count = warning_count % (warning_timeout / sleep_timeout); SLEEP(sleep_timeout); } cleanup(); return EXIT_SUCCESS; }