g_request* get_request(char* command) { const char *error; int error_offset; int num = 3; int ovector[num*3]; int result; int options = 0; pcre *regex; g_request *request; regex = pcre_compile(REGEX_GIT_COMMAND, options, &error, &error_offset, 0); if (!regex) { fprintf(stderr, "PCRE failed (offset: %d), %s\n", error_offset, error); return NULL; } result = pcre_exec(regex, 0, command, strlen(command), 0, 0, ovector, num*3); if (result <= 0) { switch(result) { case PCRE_ERROR_NOMATCH : fprintf(stderr, "String did not match the pattern\n"); break; case PCRE_ERROR_NULL : fprintf(stderr, "Something was null\n"); break; case PCRE_ERROR_BADOPTION : fprintf(stderr, "A bad option was passed\n"); break; case PCRE_ERROR_BADMAGIC : fprintf(stderr, "Magic number bad (compiled re corrupt?)\n"); break; case PCRE_ERROR_UNKNOWN_NODE : fprintf(stderr, "Something kooky in the compiled re\n"); break; case PCRE_ERROR_NOMEMORY : fprintf(stderr, "Ran out of memory\n"); break; default : fprintf(stderr, "Unknown error\n"); break; } } if (result > 0) { char **results = pcre_get_substrings(command, ovector, result); request = (g_request*) malloc(sizeof(g_request)); request->command = results[0]; request->action = results[1]; request->repo = results[2]; request->is_read = 0; request->is_write = 0; request->repo_path = (char*) malloc(255); sprintf(request->repo_path, "%s/%s", GIT_ROOT, request->repo); if (strcmp(request->action, "git-upload-pack") == 0 || strcmp(request->action, "git upload-pack") == 0) { request->is_read = 1; } if (strcmp(request->action, "git-receive-pack") == 0 || strcmp(request->action, "git receive-pack") == 0) { request->is_write = 1; } } pcre_free(regex); return request; }
void f_pcre_extract(void) { pcre_t *run; array_t *ret; run = CALLOCATE(1, pcre_t, TAG_TEMPORARY, "f_pcre_extract : run"); assign_svalue_no_free(&run->pattern, sp); run->subject = (sp - 1)->u.string; run->s_length = SVALUE_STRLEN(sp - 1); run->ovector = NULL; run->ovecsize = 0; if(pcre_magic(run) < 0) { error("PCRE compilation failed at offset %d: %s\n", run->erroffset, run->error); pop_2_elems(); pcre_free_memory(run); return; } /* Pop the 2 arguments from the stack */ if (run->rc < 0) /* No match. could do handling of matching errors if wanted */ { pop_2_elems(); pcre_free_memory(run); push_refed_array(&the_null_array); return; } else if (run->rc > (run->ovecsize/3 - 1)) { pop_2_elems(); pcre_free_memory(run); error("Too many substrings.\n"); return; } ret = pcre_get_substrings(run); pop_2_elems(); push_refed_array(ret); pcre_free_memory(run); return; }
//string pcre_replace_callback(string, string, function) void f_pcre_replace_callback(void) { int num_arg = st_num_arg, i; char *ret; pcre_t *run; svalue_t *arg; array_t *arr, *r; function_to_call_t ftc; arg = sp - num_arg + 1; run = CALLOCATE(1, pcre_t, TAG_TEMPORARY, "f_pcre_replace: run"); run->ovector = NULL; run->ovecsize = 0; run->subject = arg->u.string; assign_svalue_no_free(&run->pattern, (arg + 1)); run->s_length = SVALUE_STRLEN(arg); if(pcre_magic(run) < 0) { pcre_free_memory(run); error("PCRE compilation failed at offset %d: %s\n", run->erroffset, run->error); } if (run->rc < 0) /* No match. could do handling of matching errors if wanted */ { pop_n_elems(num_arg-1); pcre_free_memory(run); return; } if (run->rc > (run->ovecsize/3-1)) { pcre_free_memory(run); error("Too many substrings.\n"); } arr = pcre_get_substrings(run); if(arg[2].type == T_FUNCTION || arg[2].type == T_STRING) process_efun_callback(2, &ftc, F_PCRE_REPLACE_CALLBACK); else {// 0 pcre_free_memory(run); error("Illegal third argument (0) to pcre_replace_callback"); } r = allocate_array(run->rc - 1); //can't use the empty variant in case we error below push_refed_array(r); push_refed_array(arr); error_context_t econ; if (!save_context(&econ)){ pcre_free_memory(run); error("context stack full!\n"); } if (SETJMP(econ.context)) { restore_context(&econ); /* condition was restored to where it was when we came in */ pcre_free_memory(run); pop_context(&econ); error("error in callback!\n"); } for (i = 0; i < run->rc - 1; i++) { svalue_t *v; push_svalue(arr->item + i); push_number(i); v = call_efun_callback(&ftc, 2); /* Mimic behaviour of map(string, function) when function pointer returns null, ie return the input. */ if (v && v->type == T_STRING && v->u.string != NULL) assign_svalue_no_free(&r->item[i], v); else assign_svalue_no_free(&r->item[i], &arr->item[i]); } pop_context(&econ); ret = pcre_get_replace(run, r); pop_n_elems(num_arg+2); //refed arrays push_malloced_string(ret); pcre_free_memory(run); return; }