int DB_find(const char *url) { bstring data = NULL; bstring line = bfromcstr(url); int res = -1; data = DB_load(DB_FILE); check(data, "Failed to load: %s", DB_FILE); if(binstr(data, 0, line) == BSTR_ERR) { res = 0; } else { res = 1; } error: // fallthrough if(data) bdestroy(data); if(line) bdestroy(line); return res; }
void concat_log_message(lua_State *L, bstring message) { int c = lua_gettop(L); bstring space = bfromcstr(" "); int i = 0; for (i = 0; i < c; i++) { const char *str = NULL; lua_pushvalue(L, 1); lua_remove(L, 1); if (lua_type(L, 1) == LUA_TSTRING) { str = lua_tostring(L, -1); } else { lua_getglobal(L, "tostring"); lua_pushvalue(L, -2); lua_remove(L, -3); int rt = lua_pcall(L, 1, 1, 1); if (rt == 0) { str = lua_tostring(L, -1); } else { str = "<Could not convert arg to str>"; } } bstring bstr = bfromcstr(str); if (i > 0) { bconcat(message, space); } bconcat(message, bstr); bdestroy(bstr); lua_pop(L, 1); } bdestroy(space); }
int test_sky_peach_message_process() { importtmp("tests/fixtures/peach_message/1/import.json"); sky_table *table = sky_table_create(); table->path = bfromcstr("tmp"); sky_table_open(table); // NOTE: The table contains two properties: foo (String) and this_is_a_really...(Int) sky_peach_message *message = sky_peach_message_create(); message->query = bfromcstr( "[Hashable(\"id\")]\n" "[Serializable]\n" "class Result {\n" " public Int id;\n" " public Int count;\n" " public Int objectTotal;\n" " public Int actionTotal;\n" "}\n" "Cursor cursor = path.events();\n" "for each (Event event in cursor) {\n" " String dynamic_prop2 = event.this_is_a_really_long_property_name_woohoo;\n" " Result item = data.get(event.actionId);\n" " item.count = item.count + 1;\n" " item.objectTotal = item.objectTotal + event.object_prop;\n" " item.actionTotal = item.actionTotal + event.action_prop;\n" "}\n" "return;" ); FILE *output = fopen("tmp/output", "w"); mu_assert(sky_peach_message_process(message, table, output) == 0, ""); fclose(output); mu_assert_file("tmp/output", "tests/fixtures/peach_message/1/output"); sky_peach_message_free(message); sky_table_free(table); return 0; }
bool do_install(CURL* curl, bstring name) { FILE* fp; CURLcode res; long httpcode = 0; struct stat buffer; bstring url = bfromcstr("http://dms.dcputoolcha.in/modules/download?name="); bstring modpath = osutil_getmodulepath(); // Append the file name. bconchar(modpath, '/'); bconcat(modpath, name); bcatcstr(modpath, ".lua"); bconcat(url, name); bcatcstr(url, ".lua"); // Check to see if the module is already installed. if (stat(modpath->data, &buffer) == 0) { if (unlink(modpath->data) == 0) printd(LEVEL_WARNING, "removed existing %s module.\n", name->data); else { printd(LEVEL_ERROR, "unable to remove existing %s module.\n", name->data); return 1; } } // Open the file and do the cURL transfer. printd(LEVEL_DEFAULT, "querying module repository...\n"); fp = fopen(modpath->data, "wb"); curl_easy_setopt(curl, CURLOPT_URL, url->data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode); if (res != 0 && httpcode != 200) { bdestroy(url); bdestroy(name); bdestroy(modpath); printd(LEVEL_ERROR, "curl failed with error code %i, HTTP error code %i.\n", res, httpcode); return 1; } fclose(fp); printd(LEVEL_DEFAULT, "module %s installed.\n", name->data); return 0; }
bstring Unixy_getcwd() { char *wd = calloc(PATH_MAX + 1, 1); bstring dir = NULL; check(getcwd(wd, PATH_MAX-1), "Could not get current working directory."); wd[PATH_MAX] = '\0'; dir = bfromcstr(wd); error: // fall through free(wd); return dir; }
bstring Node_bstr(Node *d, int follow_sibs) { int rc = 0; bstring temp = bfromcstr(""); assert_mem(temp); assert_not(d, NULL); Node_catbstr(temp, d, ' ', follow_sibs); rc = bconchar(temp, '\n'); assert(rc == BSTR_OK && "failed to append separator char"); return temp; }
char *test_blength_bdata_bchar(void) { int i = 0; bstring b = bfromcstr(test); mu_assert(b != NULL, "Failed to create bstring."); mu_assert(blength(b) == strlen(test), "Wrong string length on blength()."); mu_assert(strcmp(bdata(b), test) == 0, "Wrong on bdada()."); for (i = 0; i < blength(b); i++) { mu_assert(bchar(b, i) == test[i], "Wrong on bchar()."); } mu_assert(bdestroy(b) == BSTR_OK, "Failed to bdestroy() afetr blength()."); b = bfromcstr(test_1); mu_assert(b != NULL, "Failed to create bstring."); mu_assert(blength(b) == strlen(test_1), "Wrong string length on blength()."); mu_assert(strcmp(bdata(b), test_1) == 0, "Wrong on bdata()."); for (i = 0; i < blength(b); i++) { mu_assert(bchar(b, i) == test_1[i], "Wrong on bchar()."); } mu_assert(bdestroy(b) == BSTR_OK, "Failed to bdestroy() afetr blength()."); return NULL; }
char *test_bsplit(void) { struct bstrList *strlist = NULL; bstring b = bfromcstr("peter liu,, yahoo"); mu_assert(b != NULL, "Failed to create bstring on bsplit()."); strlist = bsplit(b, ' '); mu_assert(strlist->qty == 3, "Wrong split substring quy."); mu_assert(strcmp((const char *)strlist->entry[0]->data, "peter") == 0, "Wrong 1st split substring."); mu_assert(strcmp((const char *)strlist->entry[1]->data, "liu,,") == 0, "Wrong 2nd split substring."); mu_assert(strcmp((const char *)strlist->entry[2]->data, "yahoo") == 0, "Wrong 3nd split substring."); mu_assert(bstrListDestroy(strlist) == BSTR_OK, "Failed to destroy string list on bsplit()."); mu_assert(bdestroy(b) == BSTR_OK, "Failed to bdestroy() afetr bsplit()."); return NULL; }
/// /// Returns the directory name component of the specified path. /// /// Returns the directory component of the specified path in a /// cross-platform manner. /// /// @param path The path to retrieve the directory name of. /// @return The directory name component. /// bstring osutil_dirname(bstring path) { bstring bpath; char* cpath; char* opath; cpath = bstr2cstr(path, '0'); // needs to return in case of dirname implementation not changing parameter // in particular this doesn't work on OS X check $ man 3 dirname opath = dirname(cpath); bpath = bfromcstr(opath); bcstrfree(cpath); return bpath; }
int test_parse(const char *test, int target_count) { HandlerParser *parser = HandlerParser_create(128); bstring T1 = bfromcstr(test); int rc = HandlerParser_execute(parser, bdata(T1), blength(T1)); debug("BODY %d long at %s with rc: %d, uuid: %s, target_count: %d", (int)parser->body_length, parser->body_start, rc, bdata(parser->uuid), (int)parser->target_count); bdestroy(T1); return rc == 1 && target_count == parser->target_count; }
int DB_update(const char *url) { if(DB_find(url)) { log_info("Already recorded as installed: %s", url); } FILE *db = DB_open(DB_FILE, "a+"); check(db, "Failed to open DB file: %s", DB_FILE); bstring line = bfromcstr(url); bconchar(line, '\n'); int rc = fwrite(line->data, blength(line), 1, db); check(rc == 1, "Failed to append to the db."); return 0; error: if(db) DB_close(db); return -1; }
char *test_load() { BytecodeFile *file = BytecodeFile_new(state, bfromcstr("tests/testfile.tvm")); bstring main_name = bfromcstr("0_main"); Function *main = Hashmap_get(file->functions, main_name); bstring add_name = bfromcstr("4_add"); Function *add = Hashmap_get(file->functions, add_name); mu_assert(*main->code == PUSH, "error parsing main"); VALUE first_lit = DArray_first(main->literals); mu_assert(strcmp(VAL2STR(first_lit), "add") == 0, "error parsing literal in main"); VALUE first_num = DArray_at(main->literals, 1); mu_assert(VAL2NUM(first_num) == 1.2, "error parsing float literal in main"); mu_assert(*add->code == PUSHSELF, "error parsing add"); bdestroy(main_name); bdestroy(add_name); return NULL; }
char *test_Dir_serve_file() { int rc = 0; Request *req = NULL; Dir *test = Dir_create( bfromcstr("tests/"), bfromcstr("sample.html"), bfromcstr("test/plain"), 0); Connection conn = {.iob = NULL}; int zero_fd = open("/dev/null", O_WRONLY); conn.iob = IOBuf_create(1024, zero_fd, IOBUF_NULL); req = fake_req("GET", "/", "/sample.json"); rc = Dir_serve_file(test, req, &conn); // TODO: different platforms barf on sendfile for different reasons // mu_assert(req->response_size > -1, "Should serve the /sample.json"); req = fake_req("HEAD", "/", "/sample.json"); rc = Dir_serve_file(test, req, &conn); mu_assert(rc == 0, "Should serve the HEAD of /sample.json"); req = fake_req("POST", "/", "/sample.json"); rc = Dir_serve_file(test, req, &conn); mu_assert(rc == -1, "POST should pass through but send an error."); mu_assert(req->status_code == 405, "POST to file should 405."); req = fake_req("GET", "/tests/", "/test"); rc = Dir_serve_file(test, req, &conn); mu_assert(rc == -1, "GET of path shorter than prefix should 404"); mu_assert(req->status_code == 404, "GET shortpath should 404."); return NULL; }
int alder_vector_option_init(alder_vector_option_t *o, struct gengetopt_args_info *a) { if (a->inputs_num > 0) o->infile = bstrVectorCreate((int)a->inputs_num); for (size_t i = 0; i < a->inputs_num; i++) { bstrVectorAdd(o->infile, a->inputs[i]); } if (a->outfile_given > 0) o->outfile = bstrVectorCreate((int)a->outfile_given); for (size_t i = 0; i < a->outfile_given; i++) { bstrVectorAdd(o->outfile, a->outfile_arg[i]); } if (a->log_given) { o->logfilename = bfromcstr(a->log_arg); } return 0; }
bstring locate_config_file() { /* * Find the config file in $HOME/.kbase_config */ char *h = getenv("HOME"); if (!h) { fprintf(stderr, "home not found in env\n"); return 0; } bstring home = bfromcstr(h); bcatcstr(home, "/.kbase_config"); return home; }
static int parse_auth_cookie(allium_ptcfg *cfg, const char *path) { assert(NULL != cfg); if (NULL == path) return (0); cfg->auth_cookie_file = bfromcstr(path); if (NULL == cfg->auth_cookie_file) { fprintf(stdout, "ENV-ERROR OOM parsing Auth Cookie File\n"); return (-1); } return (0); }
int parse_blueprint_root(JSON_Object *root, blueprint bp) { bp->version = json_object_get_number(root, "Version"); bp->Name = bfromcstr(json_object_get_string(root, "Name")); JSON_Object *blueprint = json_object_get_object(root, "Blueprint"); if (blueprint == NULL) return 1; int retval; if ((retval = parse_blueprint_json(blueprint, bp)) != 0) return retval; return 0; }
static bstring mongrel2_ws_08_upgrade_headers(mongrel2_request *req){ bstring accept = bfromcstr(""); int retval; retval = mongrel2_ws_08_calculate_accept(req,&accept); if(retval != 0){ return NULL; } bstring headers = bformat(WEBSOCKET_08_UPGRADE,blength(accept),bdata(accept)); //fprintf(stdout,"upgrade headers\n=====\n%*s\n====\n",blength(headers),bdata(headers)); //fprintf(stdout,"accept\n=====\n%*s\n====\n",blength(accept),bdata(accept)); bdestroy(accept); return headers; }
//---------------------------------------------------------------------------- // sets config_path from -c // returns 0 for success, -1 for failure int parse_command_line(int argc, char **argv, bstring *config_path) { //---------------------------------------------------------------------------- int opt; while ((opt = getopt(argc, argv, optstring)) != -1) { check(opt == 'c', "unexpectged opt '%d'", opt); check(optarg != NULL, "NULL arg"); *config_path = bfromcstr(optarg); check(*config_path != NULL, "bfromcstr"); } return 0; error: return -1; }
char *test_bchar() { bstring bstr = NULL; char c = bchar(bstr, 0); mu_assert(c == '\0', "bchar(bstring b, 0) should return '\0' when b is NULL"); bstr = bfromcstr(""); c = bchar(bstr, 0); mu_assert(c == '\0', "bchar(bstring b, 0) should return '\0' when b is EMPTY"); bassigncstr(bstr, "hello"); c = bchar(bstr, 1); mu_assert(c = 'e', "bchar should return return 'e'"); bdestroy(bstr); return NULL; }
/** * This internal method checks login predicates, populates the rest of the * Player List entry, and sends the initial packet stream to spawn the player. * * @remarks Scope: private * * @param player Player List player pointer * @param username inbound username from client login packet * @param ver inbound version from client login packet */ void process_login(struct PL_entry *player, bstring username, uint32_t ver) { // TODO: Future, async check of minecraft.net for user validity // TODO: Future, check against local ACL /* Check if the client version is compatible with the craftd version */ if (ver != PROTOCOL_VERSION) { bstring dconmsg; dconmsg = bfromcstr("Client version is incompatible with this server."); send_kick(player, dconmsg); bstrFree(dconmsg); return; } /* Otherwise, finish populating their Player List entry */ pthread_rwlock_wrlock(&player->rwlock); player->username = bstrcpy(username); pthread_rwlock_unlock(&player->rwlock); send_loginresp(player); const int spawnradius = 5; #ifdef USE_CDGAME send_chunk_radius(player, Config.spawn.x, Config.spawn.z, spawnradius); send_spawnpos(player, Config.spawn.x, Config.spawn.y, Config.spawn.z); //send inv send_movelook(player, Config.spawn.x, Config.spawn.y + 6.1, Config.spawn.y + 6.2, Config.spawn.z, 0, 0, false); #endif /* Login message */ bstring loginmsg = bformat("Player %s has joined the game!", player->username->data); send_syschat(loginmsg); bstrFree(loginmsg); /* Send player MOTD */ for(int i=0; i < Config_motdsz; ++i) { send_directchat(player, Config_motd[i]); } return; }
Action *Action_create(const char *profile) { Action *action = calloc(sizeof(Action), 1); check_mem(action); action->profile_dir = bfromcstr(profile); action->name = bTail(action->profile_dir, blength(action->profile_dir) - bstrrchr(action->profile_dir, '/') - 1); action->depends = Profile_read_setting(action->profile_dir, "depends"); return action; error: return NULL; }
Object* Array_new(Object **contents, int count) { DArray *array = DArray_create(sizeof(Object*), count || 1); int i=0; for(i=0; i < count; i++) { retain((Object*)contents[i]); DArray_push(array, contents[i]); } Object *object = Object_new(); object->type = tArray; object->value.other = array; Object_define_native_method(object, bfromcstr("[]"), Primitive_Array_at, 1); return object; }
char *test_bstrcpy() { char * test_string = "hello"; bstring bstr = bfromcstr(test_string); bstring copy_of_bstr = bstrcpy(bstr); mu_assert(bstr->slen == copy_of_bstr->slen, "bstrcopy slen error."); mu_assert(bstr->mlen == copy_of_bstr->mlen, "bstrcopy mlen error."); mu_assert(bstricmp(bstr, copy_of_bstr) == 0, "bstrcopy compare failed."); int rc = bdestroy(bstr); mu_assert(rc == BSTR_OK, "destroy bstring failed"); rc = bdestroy(copy_of_bstr); mu_assert(rc == BSTR_OK, "destroy copy of bstring failed"); return NULL; }
static int parse_state_directory(allium_ptcfg *cfg, const char *path) { assert(NULL != cfg); if (NULL == path) { fprintf(stdout, "ENV-ERROR No State Directory\n"); return (-1); } cfg->state_location = bfromcstr(path); if (NULL == cfg->state_location) { fprintf(stdout, "ENV-ERROR OOM parsing State Location\n"); return (-1); } return (0); }
char *test_Server_create_destroy() { Server *server = Server_create( bfromcstr("uuid"), bfromcstr("localhost"), bfromcstr("0.0.0.0"), 8080, bfromcstr("chroot"), bfromcstr("access_log"), bfromcstr("error_log"), bfromcstr("pid_file"), 0); mu_assert(server != NULL, "Failed to make the server, something on 8090?"); Server_destroy(server); return NULL; }
bstring build_oauth_header(bKeyValues *keyvalues) { bstring buffer = bfromcstr("Authorization: OAuth "); int i = 0; int first = 1; for(i = 0; i < keyvalues->qty; i++) { if(first) { first = 0; } else { bconchar(buffer, ','); } bconcat(buffer, keyvalues->entry[i].key); bcatcstr(buffer, "=\""); bconcat(buffer, keyvalues->entry[i].value); bconchar(buffer, '\"'); } return buffer; }
bstring to_json(hgt_pop ** ps, hgt_params * params) { unsigned i; char *c; bstring b; b = bfromcstr(""); for (i = 0; i < params->replicates; i ++) { hgt_pop * pop = ps[i]; c = hgt_pop_to_json(pop, params); bformata(b, "%s", c); free(c); if (i < params->replicates-1) { bformata(b, "\n"); } } return b; }
static int chilli_sessions(bstring b) { struct dhcp_conn_t *conn = dhcp->firstusedconn; char *state = "<font color=green>Authorized</font>"; bstring s = bfromcstr(""); bcatcstr(b, "{\"service\":[{ \""); bcatcstr(b, getenv("CAP_table")); bcatcstr(b, "\":[ "); while (conn) { struct app_conn_t *appconn = (struct app_conn_t *)conn->peer; if (appconn && appconn->s_state.authenticated) { } else { state = "<font color=red>Redirect</font>"; } bassignformat(s, "{" "\"state\":\"%s\"," "\"macAddress\":\"%.2X-%.2X-%.2X-%.2X-%.2X-%.2X\"," "\"ipAddress\":\"%s\",", state, conn->hismac[0], conn->hismac[1], conn->hismac[2], conn->hismac[3], conn->hismac[4], conn->hismac[5], inet_ntoa(conn->hisip) ); conn = conn->next; if (appconn) { session_json_params(&appconn->s_state, &appconn->s_params, s, 0); bcatcstr(s, ","); session_json_acct(&appconn->s_state, &appconn->s_params, s, 0); } bcatcstr(s, "},"); bconcat(b, s); } bcatcstr(b, "]} ]}"); return 0; }
/** register command in function list * * Takes a command list, constant string and comepletely generic function * pointer to generate a cmdlet entry and store it within the list. */ constable_status cons_reg_cmd( cmdlist* list, const char* name, void* func ) { if( list->index >= CONSTABLE_LIST_MAX ) { #ifdef DEBUG Serial.println(F("CANNOT REGISTER ANOTHER FUNCTION")); #endif return CONSTABLE_GENERAL_ERROR; } cmdlet *newcmd = &(list->cmds[list->index]); newcmd->name = bfromcstr( name ); newcmd->func = func; list->index++; return CONSTABLE_GENERAL_SUCCESS; }