char *select_strategy(const char *msg) { int deep = 1; debug(deep, "Received string: '%s'", msg); info(deep, "Select strategy"); unsigned char action = msg[0]; char *result; switch (action) { case COMMAND_CREATE: append_strings("", create_file(msg), &result); break; case COMMAND_UPDATE: append_strings("", update_file(msg), &result); break; case COMMAND_DELETE: append_strings("", delete_file(msg), &result); break; case COMMAND_READ: result = read_file(msg); break; case COMMAND_LIST: result = list_files(msg); break; default: append_strings("", ANSWER_UNKOWN, &result); error(deep, "Wrong action %c", action); } info(deep, "Return value: '%s'", result); return result; }
CHAR8 *get_powerup_reason(CHAR8 *cmdline) { enum wake_sources ws; enum shutdown_sources ss; enum reset_sources rs; ss = loader_ops.get_shutdown_source(); ws = loader_ops.get_wake_source(); rs = loader_ops.get_reset_source(); cmdline = append_strings(shutdown_string[ss], cmdline); cmdline = append_strings(wake_string[ws], cmdline); cmdline = append_strings(reset_string[rs], cmdline); return cmdline; }
CHAR8 *check_vbattfreqlmt(CHAR8 *cmdline) { CHAR8 *updated_cmdline = cmdline; if (loader_ops.em_ops->is_battery_below_vbattfreqlmt()) { debug(L"Battery voltage below vbattfreqlmt add battlow in cmdline\n"); updated_cmdline = append_strings((CHAR8 *)"battlow ", cmdline); if (cmdline) FreePool(cmdline); } return updated_cmdline; }
char *list_files(const char *msg) { const int deep = 2; regex_t r; char *rv; const char *list_regex_text = "LIST[[:cntrl:]]+"; compile_regex(&r, list_regex_text); int retCode = match_regex(&r, msg, NULL, NULL, NULL); regfree(&r); if (!retCode) { error(deep, "Message '%s' does not match to regex!", msg); append_strings("", ANSWER_UNKOWN, &rv); return rv; } info(deep, "List files"); char *file_list; int file_counter = list_memory_file(&file_list); char str[15]; sprintf(str, "%d", file_counter); char *rv_wit_num; append_strings(ANSWER_SUCCESS_LIST, str, &rv_wit_num); char *rv_first_line; append_strings(rv_wit_num, "\n", &rv_first_line); free(rv_wit_num); if (file_counter > 0) { char *rv_with_list; append_strings(rv_first_line, file_list, &rv_with_list); free(rv_first_line); append_strings(rv_with_list, "\n", &rv); free(rv_with_list); } else { rv = rv_first_line; } free(file_list); return rv; }
CHAR8 *get_extra_cmdline(CHAR8 *cmdline) { CHAR8 *extra_cmdline; CHAR8 *updated_cmdline; extra_cmdline = loader_ops.get_extra_cmdline(); debug(L"Getting extra commandline: %a\n", extra_cmdline ? extra_cmdline : (CHAR8 *)""); updated_cmdline = append_strings(extra_cmdline, cmdline); updated_cmdline = get_powerup_reason(updated_cmdline); if (extra_cmdline) FreePool(extra_cmdline); if (cmdline) FreePool(cmdline); return updated_cmdline; }
void test_append_strings() { char *final_string = malloc(1); final_string[0] = '\000'; char *tmp_string; int tmp_length; char *string1 = "Bla"; char *string2 = "Test"; char *string3 = "End"; tmp_length = append_strings(final_string, string1, &tmp_string); free(final_string); final_string = malloc(tmp_length); strncpy(final_string, tmp_string, tmp_length); free(tmp_string); tmp_length = append_strings(final_string, " ", &tmp_string); free(final_string); final_string = malloc(tmp_length); strncpy(final_string, tmp_string, tmp_length); free(tmp_string); tmp_length = append_strings(final_string, string2, &tmp_string); free(final_string); final_string = malloc(tmp_length); strncpy(final_string, tmp_string, tmp_length); free(tmp_string); tmp_length = append_strings(final_string, "\n", &tmp_string); free(final_string); final_string = malloc(tmp_length); strncpy(final_string, tmp_string, tmp_length); free(tmp_string); tmp_length = append_strings(final_string, string3, &tmp_string); free(final_string); final_string = malloc(tmp_length); strncpy(final_string, tmp_string, tmp_length); free(tmp_string); tmp_length = append_strings(final_string, "\n", &tmp_string); free(final_string); final_string = malloc(tmp_length); strncpy(final_string, tmp_string, tmp_length); free(tmp_string); CU_ASSERT_STRING_EQUAL(final_string, "Bla Test\nEnd\n"); free(final_string); }
static void test_addNodeInTree_TreeIsLeaf() { before(); char *path = append_strings(testdir_path, "/cepa.jpg"); VnrFile *vnrfile = vnr_file_create_new(path, "cepa.jpg", FALSE); GNode *node = g_node_new(vnrfile); GNode *tree = get_tree(SINGLE_FILE, FALSE, FALSE); tree = get_root_node(tree); GNode *bepa = assert_forward_iteration(tree, "bepa.png"); add_node_in_tree(bepa, node); char* expected = KWHT TESTDIRNAME RESET " (3 children)\n\ ├─ bepa.png\n\ ├─ cepa.jpg\n\ └─ epa.png\n\ "; assert_equals("Add node in tree ─ Tree is leaf ─ No change", expected, print_and_free_tree(tree)); free(path); free_whole_tree(node); after(); }
char *read_file(const char *msg) { const int deep = 2; regex_t r; char filename[MAX_MESSAGE_LEN + 1]; char *returnValue; const char *read_regex_text = "READ[[:blank:]]+([[:graph:]|[:blank:]]+)[[:cntrl:]]+"; compile_regex(&r, read_regex_text); int retCode = match_regex(&r, msg, filename, NULL, NULL); regfree(&r); if (!retCode) { error(deep, "Message '%s' does not match to regex!", msg); append_strings("", ANSWER_UNKOWN, &returnValue); return returnValue; } char *content; if (read_memory_file(filename, &content)) { debug(deep, "Content of file: %s", content); int length = strlen(content) + 1; char len_string[15]; sprintf(len_string, " %d\n", length); char *rv_with_name; append_strings(ANSWER_SUCCESS_READ, filename, &rv_with_name); char *rv_with_len; append_strings(rv_with_name, len_string, &rv_with_len); free(rv_with_name); char *rv_with_content; append_strings(rv_with_len, content, &rv_with_content); free(rv_with_len); append_strings(rv_with_content, "\n", &returnValue); free(rv_with_content); free(content); } else { append_strings("", ANSWER_FAILED_READ, &returnValue); } return returnValue; }