int http_parse_input(char *src, int len, int is_cookie) { bstring name = bfromcstr(""); bstring value = bfromcstr(""); int start_pos=0, eq_pos=0, end_pos=0; int i, ret = 0; char stop = (is_cookie) ? ';' : '&'; for (i=0; i <= len; i++) { if (i == len || src[i] == stop) { end_pos = i; } else if (!eq_pos && src[i] == '=') { eq_pos = i; } if (eq_pos && end_pos && start_pos != eq_pos) { bassigncstr(name, ""); bassigncstr(value, ""); if (is_cookie) { bassigncstr(name,"COOKIE_"); bcatblk(name,src+start_pos,eq_pos-start_pos); } else { bassigncstr(name,"CAP_"); bcatblk(name,src+start_pos,eq_pos-start_pos); } /*bunescape(name);*/ if ((eq_pos + 1) == end_pos || (end_pos - eq_pos) <= 1) { syslog(LOG_DEBUG, "%s = nil", name->data); } else { bassignblk(value, src+eq_pos+1, end_pos-eq_pos-1); /*bunescape(value);*/ syslog(LOG_DEBUG, "%s = %s", name->data, value->data); setenv((char *)name->data, (char *)value->data, 1); } start_pos = end_pos + 1; if (start_pos < len) while (src[start_pos] == ' ' && i < len) i++, start_pos++; end_pos = eq_pos = 0; } } ret = 1; bdestroy(name); bdestroy(value); return ret; }
/* * Return values: * 0 == nothing to write * -1 == error that closed socket * 1 == normal write */ static int redir_cli_rewrite(redir_request *req, struct conn_t *conn) { if (conn_write_remaining(conn)) { int w = net_write(req->socket_fd, conn->read_buf->data + conn->read_pos, conn->read_buf->slen - conn->read_pos); if (w < 0 && errno != EWOULDBLOCK && errno != EAGAIN) { log_err(errno, "net_write(%d)", conn->read_buf->slen - conn->read_pos); redir_conn_finish(conn, req); return -1; } else if (w > 0) { #if(_debug_) log_dbg("client (re)write: %d", w); #endif conn->read_pos += w; if (conn->read_pos == conn->read_buf->slen) { log_dbg("emptying write queue"); conn->read_pos = 0; bassigncstr(conn->read_buf, ""); } } return 1; } return 0; }
static bstring inject_fmt(redir_request *req, struct redir_conn_t *conn) { char *url = req->inject_url; if (req->ibuf->slen) return req->ibuf; if (!url || !*url) url = _options.inject; if (!url || !*url) return 0; bassigncstr(req->ibuf, ""); if (conn) { char hexchal[1+(2*REDIR_MD5LEN)]; extern void redir_wispr2_reply (struct redir_t *redir, struct redir_conn_t *conn, int res, long int timeleft, char* hexchal, char* reply, char* redirurl, bstring b); redir_chartohex(conn->s_state.redir.uamchal, hexchal, REDIR_MD5LEN); redir_wispr2_reply(req->parent, conn, REDIR_NOTYET, 0, hexchal, 0, 0, req->ibuf); } if (_options.inject_ext) { bcatcstr(req->ibuf, "<script type=\"text/javascript\">"); bcatcstr(req->ibuf, _options.inject_ext); bcatcstr(req->ibuf, "</script>"); } bcatcstr(req->ibuf, "<script src='"); bcatcstr(req->ibuf, url); bcatcstr(req->ibuf, "'></script>"); return req->ibuf; }
void parse() { bprintf("<p>"); while(yyparse()) {} bassigncstr(tag_name, ""); close_needed_tags(); bprintf("</p>"); handle_toc(); }
char *test_bassigncstr(void) { bstring b = bfromcstr("rwere"); mu_assert(bassigncstr(b, test) == BSTR_OK, "Failed to bassigncstr()."); mu_assert(strcmp((const char *)b->data, test) == 0, "Failed to string compare after bassigncstr()."); mu_assert(b->slen == (int)strlen(test), "Wrong string length after bassigncstr()."); mu_assert(bdestroy(b) == BSTR_OK, "Failed to bdestroy() afetr bassigncstr()."); return NULL; }
char *test_bfromc() { bstr = bfromcstr(test1); mu_assert(blength(bstr) == 10, "Size is wrong."); char *res = malloc(sizeof(char) * blength(bstr)); bassigncstr(bstr, res); mu_assert(strcmp(res, test1), "Strings are not equal."); free(res); bdestroy(bstr); return NULL; }
char *test_bstricmp() { bstring bstr_a = bfromcstr("a"); bstring bstr_b = bfromcstr("b"); int rc = bstricmp(bstr_a, bstr_b); mu_assert(rc == -1, "a b should bstricmp -1"); bassigncstr(bstr_a, "a"); bassigncstr(bstr_b, "A"); rc = bstricmp(bstr_a, bstr_b); mu_assert(rc == 0, "a A should bstricmp 0"); bassigncstr(bstr_a, "ab"); bassigncstr(bstr_b, "AB1"); rc = bstricmp(bstr_a, bstr_b); mu_assert(rc == -49, "ab AB1 should bstricmp -49"); bassigncstr(bstr_a, "ab"); bassigncstr(bstr_b, ""); rc = bstricmp(bstr_a, bstr_b); mu_assert(rc == 97, "ab \"\" should bstricmp 97"); bdestroy(bstr_a); bdestroy(bstr_b); return NULL; }
char *test_bassigncstr() { bstring bstr = bfromcstr(""); int rc = bassigncstr(bstr, "hello world"); mu_assert(rc == BSTR_OK, "bassigncstr failed"); bstring bstr2 = bfromcstr("hello world"); mu_assert(biseq(bstr, bstr2) == 1, "bassigncstr not equal to bfromcstr"); bdestroy(bstr2); bdestroy(bstr); return NULL; }
static void cargs_export_file_name(struct arg_file *arg_input_fn, struct arg_file *arg_output_fn, struct arg_file *arg_log_fn, bstring *input_fn, bstring *output_fn, bstring *log_fn) { if(*input_fn == NULL) { *input_fn = bfromcstr(arg_input_fn->basename[0]); } else { bassigncstr(*input_fn, arg_input_fn->basename[0]); } if(arg_output_fn->count == 1) { if(*output_fn == NULL) { *output_fn = bfromcstr(arg_output_fn->basename[0]); } else { bassigncstr(*output_fn, arg_output_fn->basename[0]); } } else if(arg_output_fn->count == 0) { if(*output_fn == NULL) { *output_fn = bfromcstr("cfeap-output.txt"); } else { bassigncstr(*output_fn, "cfeap-output.txt"); } } if(arg_log_fn->count == 1) { if(*log_fn == NULL) { *log_fn = bfromcstr(arg_log_fn->basename[0]); } else { bassigncstr(*log_fn, arg_log_fn->basename[0]); } } else if(arg_log_fn->count == 0) { if(*log_fn == NULL) { *log_fn = bfromcstr("cfeap-log.txt"); } else { bassigncstr(*log_fn, "cfeap-log.txt"); } } }
char *test_biseq() { bstring bstr_a = bfromcstr("a"); bstring bstr_b = bfromcstr("a"); int rc = biseq(bstr_a, bstr_b); mu_assert(rc == 1, "biseq a a failed"); bassigncstr(bstr_b, "b"); rc = biseq(bstr_a, bstr_b); mu_assert(rc == 0, "biseq a b failed"); bdestroy(bstr_a); bdestroy(bstr_b); return NULL; }
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; }
inline int normalize_path(bstring target) { ballocmin(target, PATH_MAX); char *path_buf = calloc(PATH_MAX+1, 1); // Some platforms (OSX!) don't allocated for you, so we have to char *normalized = realpath((const char *)target->data, path_buf); check(normalized, "Failed to normalize path: %s", bdata(target)); bassigncstr(target, normalized); free(path_buf); return 0; error: return 1; }
static void if_handle(state_t* state, match_t* match, bool* reprocess) { list_t* result = ppparam_get(state); struct expr* expr = NULL; uint16_t value; bstring output; bool stopped_at_else; // Ensure the parameter format is correct. if (list_size(result) == 1 && ((parameter_t*)list_get_at(result, 0))->type == EXPRESSION) { // Get the expression. expr = ((parameter_t*)list_get_at(result, 0))->expr; replace_state = state; value = expr_evaluate(expr, &if_define_replace, &dhalt_expression_exit_handler); replace_state = NULL; if (value) { output = skip_to_endif(state, true, &stopped_at_else); if (stopped_at_else) skip_to_endif(state, false, &stopped_at_else); } else { bassigncstr(output, ""); skip_to_endif(state, true, &stopped_at_else); if (stopped_at_else) { output = skip_to_endif(state, false, &stopped_at_else); } } // print the output to the pre processor input ppimpl_printf(state, "%s", output->data); } else dhalt(ERR_PP_ASM_IF_PARAMETERS_INCORRECT, ppimpl_get_location(state)); ppparam_free(result); }
static void macro_handle(state_t* state, match_t* match, bool* reprocess) { bstring name; bstring temp; list_t parameters; list_t arguments; bool getting_name = true; bool getting_parameters = false; bool getting_arguments = false; struct replace_info* info = match->userdata; int i = 0; int argument_brackets = 0; char c; char* start_loc; match_t* new_match; struct replace_info* new_info; // Parse the parameters out of the name. list_init(¶meters); temp = bfromcstr(""); for (i = 0; i < blength(info->full); i++) { c = info->full->data[i]; if (getting_name) { if (c == '(') { getting_name = false; getting_parameters = true; name = bstrcpy(temp); bassigncstr(temp, ""); } else bconchar(temp, c); } else if (getting_parameters) { if (c == ',' || c == ')') { btrimws(temp); list_append(¶meters, bstrcpy(temp)); bassigncstr(temp, ""); if (c == ')') { getting_parameters = false; break; } } else bconchar(temp, c); } } // Attempt to accept an open bracket. c = ppimpl_get_input(state); while (c == '\1') { // Consume macro termination. i = 0; while (i < strlen("\1MACROTERMINATE\1")) { if (c != "\1MACROTERMINATE\1"[i++]) dhalt(ERR_PP_EXPECTED_OPEN_BRACKET, ppimpl_get_location(state)); c = ppimpl_get_input(state); } ppimpl_pop_scope(state); } if (c != '(') dhalt(ERR_PP_EXPECTED_OPEN_BRACKET, ppimpl_get_location(state)); // Read arguments. getting_arguments = true; list_init(&arguments); start_loc = ppimpl_get_location(state); bassigncstr(temp, ""); while (ppimpl_has_input(state) && getting_arguments) { c = ppimpl_get_input(state); if (c == '(') { argument_brackets++; bconchar(temp, c); } else if (c == ')' && argument_brackets != 0) { argument_brackets--; bconchar(temp, c); } else if (c == ')' && argument_brackets == 0) { list_append(&arguments, bstrcpy(temp)); bassigncstr(temp, ""); getting_arguments = false; break; } else if (c == ',' && argument_brackets == 0) { list_append(&arguments, bstrcpy(temp)); bassigncstr(temp, ""); } else bconchar(temp, c); } if (getting_arguments) dhalt(ERR_PP_NO_TERMINATING_BRACKET, start_loc); // Check to see if the argument count is correct. if (list_size(&arguments) > list_size(¶meters)) dhalt(ERR_PP_TOO_MANY_PARAMETERS, start_loc); else if (list_size(&arguments) < list_size(¶meters)) dhalt(ERR_PP_NOT_ENOUGH_PARAMETERS, start_loc); free(start_loc); // Create a new scope for macro evaluation. ppimpl_push_scope(state, true); // Define the new handlers. for (i = 0; i < list_size(¶meters); i++) { new_info = malloc(sizeof(struct replace_info)); new_info->full = list_get_at(¶meters, i); new_info->replacement = list_get_at(&arguments, i); if (biseq(new_info->full, new_info->replacement)) { free(new_info); continue; } new_match = malloc(sizeof(match_t)); new_match->text = bautofree(list_get_at(¶meters, i)); new_match->handler = replace_handle; new_match->line_start_only = false; new_match->identifier_only = true; new_match->userdata = new_info; new_match->case_insensitive = false; ppimpl_register(state, new_match); } // Print out the macro evaluation and terminator. ppimpl_printf(state, "%s\1MACROTERMINATE\1", info->replacement->data); }
static void define_handle(state_t* state, match_t* match, bool* reprocess) { // We need to parse this manually because we're interested in getting // the first word and then all of the content until a line that doesn't end // with "\". bstring name = bfromcstr(""); bstring word = bfromcstr(""); bstring definition = bfromcstr(""); bool getting_word = true; bool getting_definition = true; bool is_macro = false; match_t* new_match; struct replace_info* info; // Get the first word. while (getting_word) { char c = ppimpl_get_input(state); bconchar(word, c); if (!is_macro && c != '(') bconchar(name, c); bltrimws(word); // Handle termination. if (blength(word) > 0 && (c == ' ' || c == '\t') && !is_macro) { // End of word. btrimws(word); btrimws(name); getting_word = false; } else if (blength(word) > 0 && c == '(' && !is_macro) { // Start of macro. is_macro = true; } else if (blength(word) > 0 && c == '(' && is_macro) { // Second ( in a macro; error. dhalt(ERR_PP_MACRO_MALFORMED, ppimpl_get_location(state)); } else if (blength(word) > 0 && c == ')' && is_macro) { // End of macro name. btrimws(word); btrimws(name); getting_word = false; } else if (blength(word) == 0 && c == '\n') dhalt(ERR_PP_C_DEFINE_PARAMETERS_INCORRECT, ppimpl_get_location(state)); else if (blength(word) > 0 && c == '\n') { // End of word. btrimws(word); btrimws(name); getting_word = false; getting_definition = false; ppimpl_printf(state, "\n"); } } // Get the definition. while (getting_definition) { char c = ppimpl_get_input(state); bconchar(definition, c); bltrimws(definition); if (c == '\n') { if (blength(definition) > 1 && definition->data[blength(definition) - 2] == '\\') { // Remove the new slash. bdelete(definition, blength(definition) - 2, 1); ppimpl_oprintf(state, "\n"); } else { btrimws(definition); getting_definition = false; ppimpl_printf(state, "\n"); } } else if (c == '/' || c == '*') { if (blength(definition) > 1 && definition->data[blength(definition) - 2] == '/') { // a line or block comment ppimpl_iprintf(state, "/%c", c); // remove the slashes bdelete(definition, blength(definition) - 2, 2); btrimws(definition); getting_definition = false; } } } if (blength(definition) == 0 && !is_macro) bassigncstr(definition, "1"); // Create the new replacement handler. info = malloc(sizeof(struct replace_info)); info->full = word; info->replacement = definition; if (biseq(info->full, info->replacement)) { free(info); return; } new_match = malloc(sizeof(match_t)); new_match->text = bautofree(name); if (is_macro) new_match->handler = macro_handle; else new_match->handler = replace_handle; new_match->line_start_only = false; new_match->identifier_only = true; new_match->userdata = info; new_match->case_insensitive = false; ppimpl_register(state, new_match); *reprocess = true; }
static bstring skip_to_endif(state_t* state, bool stop_at_else, bool* stopped_at_else) { char c; bool fresh_line = true; bstring temp = bfromcstr(""); bstring temp_output = bfromcstr(""); bstring output = bfromcstr(""); int if_open = 1; while (ppimpl_has_input(state)) { c = ppimpl_get_input(state); switch(c) { case '#': case '.': if (!fresh_line) { bconchar(output, c); break; } bassigncstr(temp_output, "#"); // first skip spaces while (ppimpl_has_input(state)) { c = ppimpl_get_input(state); bconchar(temp_output, c); if (c != ' ' && c != '\t') break; } // read pp directive bassigncstr(temp, ""); bconchar(temp, c); while (ppimpl_has_input(state)) { c = ppimpl_get_input(state); bconchar(temp_output, c); if (c == ' ' || c == '\t' || c == '\n') break; bconchar(temp, c); } btolower(temp); if (biseq(temp, bfromcstr("endif"))) { if_open--; if (if_open == 0) { if (c != '\n') skip_to_endln(state); *stopped_at_else = false; return output; } } else if (biseq(temp, bfromcstr("if"))) { if_open++; } else if (biseq(temp, bfromcstr("else")) && stop_at_else) { if (if_open == 1) { if (c != '\n') skip_to_endln(state); *stopped_at_else = true; return output; } } bconcat(output, temp_output); fresh_line = (c == '\n'); break; case '\n': fresh_line = true; bconchar(output, c); break; case ' ': case '\t': bconchar(output, c); break; default: fresh_line = false; bconchar(output, c); break; } } // No .ENDIF was found. dhalt(ERR_PP_ASM_NO_ENDIF_TO_IF, ppimpl_get_location(state)); // dhalt will trigger before this, but the compiler warns about // control potentially reaching the end of this function. return NULL; }
static bstring string_init_reset(bstring s) { if (!s) return bfromcstr(""); bassigncstr(s, ""); return s; }
int main(int argc, char* argv[]) { CURL* curl; bstring command; bstring name; bstring modpath; int all; // Define arguments. struct arg_lit* show_help = arg_lit0("h", "help", "Show this help."); struct arg_str* cmdopt = arg_str1(NULL, NULL, "<command>", "The command; either 'search', 'install', 'uninstall', 'enable' or 'disable'."); struct arg_str* nameopt = arg_str0(NULL, NULL, "<name>", "The name of the module to search for, install, uninstall, enable or disable."); struct arg_lit* all_flag = arg_lit0("a", "all", "Apply this command to all available / installed modules."); struct arg_lit* verbose = arg_litn("v", NULL, 0, LEVEL_EVERYTHING - LEVEL_DEFAULT, "Increase verbosity."); struct arg_lit* quiet = arg_litn("q", NULL, 0, LEVEL_DEFAULT - LEVEL_SILENT, "Decrease verbosity."); struct arg_end* end = arg_end(20); void* argtable[] = { show_help, cmdopt, all_flag, nameopt, verbose, quiet, end }; // Parse arguments. int nerrors = arg_parse(argc, argv, argtable); if (nerrors != 0 || show_help->count != 0 || (all_flag->count == 0 && nameopt->count == 0)) { if (all_flag->count == 0 && nameopt->count == 0) printd(LEVEL_ERROR, "error: must have either module name or -a."); if (show_help->count != 0) arg_print_errors(stderr, end, "mm"); fprintf(stderr, "syntax:\n dtmm"); arg_print_syntax(stderr, argtable, "\n"); fprintf(stderr, "options:\n"); arg_print_glossary(stderr, argtable, " %-25s %s\n"); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } // Set verbosity level. debug_setlevel(LEVEL_DEFAULT + verbose->count - quiet->count); // Show version information. version_print(bautofree(bfromcstr("Module Manager"))); // Set argument 0 and convert parameters. osutil_setarg0(bautofree(bfromcstr(argv[0]))); command = bfromcstr(cmdopt->sval[0]); name = bfromcstr(nameopt->sval[0]); // Initialize curl or exit. curl = curl_easy_init(); if (!curl) { printd(LEVEL_ERROR, "unable to initialize curl.\n"); return 1; } // Ensure module path exists. modpath = osutil_getmodulepath(); if (modpath == NULL) { printd(LEVEL_ERROR, "module path does not exist (searched TOOLCHAIN_MODULES and modules/).\n"); return 1; } bdestroy(modpath); // Convert all flag. all = (all_flag->count > 0); // If all is set, set the name back to "". if (all) bassigncstr(name, ""); // If the name is "all" or "*", handle this as the all // boolean flag. if (biseqcstr(name, "all") || biseqcstr(name, "*")) { bassigncstr(name, ""); all = 1; printd(LEVEL_WARNING, "treating name as -a (all) flag"); } if (biseqcstrcaseless(command, "search") || biseqcstrcaseless(command, "se")) return do_search(curl, name, all); else if (biseqcstrcaseless(command, "install") || biseqcstrcaseless(command, "in")) { if (all) return do_install_all(curl); else return do_install(curl, name); } else if (biseqcstrcaseless(command, "uninstall") || biseqcstrcaseless(command, "rm")) { if (all) return do_uninstall_all(curl); else return do_uninstall(curl, name); } else if (biseqcstrcaseless(command, "enable") || biseqcstrcaseless(command, "en")) { if (all) return do_enable_all(curl); else return do_enable(curl, name); } else if (biseqcstrcaseless(command, "disable") || biseqcstrcaseless(command, "di") || biseqcstrcaseless(command, "dis")) { if (all) return do_disable_all(curl); else return do_disable(curl, name); } else { printd(LEVEL_ERROR, "unknown command (must be search, install, uninstall, enable or disable)."); return 1; } return 0; }