Ejemplo n.º 1
0
static void *hvm_str_replace(const char *method,
                             hefesto_common_list_ctx **string_var,
                             hefesto_type_t *otype,
                             hefesto_var_list_ctx **lo_vars,
                             hefesto_var_list_ctx **gl_vars,
                             hefesto_func_list_ctx *functions) {

    size_t offset = 0, outsz;
    char *regex_arg, *pattern_arg;
    char *replaced_buffer = NULL;
    const char *m;
    char errors[HEFESTO_MAX_BUFFER_SIZE];
    void *usr_regex, *usr_pattern, *result;
    hefesto_type_t etype = HEFESTO_VAR_TYPE_STRING;
    here_search_program_ctx *search_program;

    *otype = HEFESTO_VAR_TYPE_INT;
    result = hefesto_mloc(sizeof(hefesto_int_t));
    *(hefesto_int_t *)result = 0;

    if (string_var == NULL || (*string_var) == NULL ||
        (*string_var)->data == NULL) return result;

    for (m = method; *m != '(' && *m != 0; m++);

    regex_arg = get_arg_from_call(m+1, &offset);

    pattern_arg = get_arg_from_call(m+1, &offset);

    usr_regex = expr_eval(regex_arg, lo_vars, gl_vars, functions,
                          &etype, &outsz);

    if ((search_program = here_compile(usr_regex, errors)) != NULL) {
        usr_pattern = expr_eval(pattern_arg, lo_vars, gl_vars, functions,
                                &etype, &outsz);

        *(hefesto_int_t *)result = here_replace_string((*string_var)->data,
                                             search_program,
                                             usr_pattern,
                                             &replaced_buffer,
                                             &outsz);
        free((*string_var)->data);
        (*string_var)->data = (char *) hefesto_mloc(outsz+1);
        (*string_var)->dsize = outsz;
        memset((*string_var)->data, 0, outsz+1);
        strncpy((*string_var)->data, replaced_buffer, outsz+1);
        free(replaced_buffer);
        free(usr_pattern);
        del_here_search_program_ctx(search_program);
    } else {
        hlsc_info(HLSCM_MTYPE_RUNTIME, HLSCM_SYN_ERROR_INVAL_REGEX, errors);
    }

    free(usr_regex);

    return result;

}
Ejemplo n.º 2
0
static void *hvm_str_match(const char *method,
                           hefesto_common_list_ctx **string_var,
                           hefesto_type_t *otype, hefesto_var_list_ctx **lo_vars,
                           hefesto_var_list_ctx **gl_vars,
                           hefesto_func_list_ctx *functions) {

    size_t offset = 0, outsz;
    char *arg;
    const char *m;
    char errors[HEFESTO_MAX_BUFFER_SIZE];
    void *usr_regex, *result;
    here_search_program_ctx *search_program;
    here_search_result_ctx *search_result;

    hefesto_type_t etype = HEFESTO_VAR_TYPE_STRING;
    *otype = HEFESTO_VAR_TYPE_INT;
    result = hefesto_mloc(sizeof(hefesto_int_t));
    *(hefesto_int_t *)result = 0;

    if (string_var == NULL || (*string_var) == NULL ||
        (*string_var)->data == NULL) return result;

    for (m = method; *m != '(' && *m != 0; m++);

    arg = get_arg_from_call(m+1, &offset);

    usr_regex = expr_eval(arg, lo_vars, gl_vars, functions,
                          &etype, &outsz);
    if ((search_program = here_compile(usr_regex, errors)) != NULL) {
        search_result = here_match_string((*string_var)->data,
                                          search_program);
        *(hefesto_int_t *)result = here_matches(search_result);
        del_here_search_program_ctx(search_program);
        del_here_search_result_ctx(search_result);
    } else {
        hlsc_info(HLSCM_MTYPE_RUNTIME, HLSCM_SYN_ERROR_INVAL_REGEX, errors);
    }

    free(usr_regex);

    return result;

}
Ejemplo n.º 3
0
static here_search_result_ctx *match_opt(const char *buffer,
                                         const char *buffer_end,
                           here_search_program_ctx *search_program) {

    here_search_result_ctx *search_result = NULL, *aux_s_res;
    here_search_program_ctx *sub_search = NULL;
    const char *b;
    char *sub_buffer;
    const char *nb;
    size_t sb_size;
    int matches = 0;
    int should_break = 0;
    int opened_nr = 0;

    for (b = search_program->buffer; *b != 0 && !matches; b++) {

        sb_size = 0;

        should_break = 0;
        opened_nr = 0;
        while (!should_break && *b != 0) {
            if (*b == '(') {
                opened_nr++;
            } else if (*b == ')') {
                opened_nr--;
            } else if (*b == '\\') {
                b++;
            }
            sb_size++;
            b++;
            should_break = (*b == '|' && opened_nr == 0) ||
                            (*(b-1) == ')' && opened_nr == 0);
        }

        if (sb_size > 0) {
            sub_buffer = (char *) here_alloc(sb_size + 1);
            memset(sub_buffer, 0, sb_size + 1);
            memcpy(sub_buffer, (b - sb_size), sb_size);
            sub_search = NULL;
            sub_search = add_regex_to_here_search_program_ctx(sub_search,
                                                              sub_buffer);
            free(sub_buffer);

            del_here_search_result_ctx(search_result);

            search_result = here_execute_search_program(buffer,
                                                        buffer_end,
                                                        sub_search);

            matches = here_matches(search_result);
            if (matches) {
                if (sub_search->next == NULL &&
                    sub_search->ctype == HERE_CTYPE_CMATCH &&
                    sub_search->ccomp == HERE_CTYPE_NONE) {
                    matches = (search_result->start_at == buffer);
                }
            }

            if ((search_program->ccomp == HERE_CTYPE_STAR ||
                search_program->ccomp == HERE_CTYPE_QUESTION) && !matches) {
                sub_search->ccomp = HERE_CTYPE_NONE;
                matches = 1;
                if (search_result == NULL) {
                    new_here_search_result_ctx(search_result);
                }
                search_result->start_at = buffer;
                search_result->end_at = buffer;
                search_result->next_step = search_program->next;
            }

            if (matches && search_program->next != NULL) {
                aux_s_res = search_result;
                search_result = NULL;
                if (sub_search->ccomp != HERE_CTYPE_STAR &&
                    sub_search->ccomp != HERE_CTYPE_QUESTION) {
                    nb = aux_s_res->end_at + 1;
                } else {
                    nb = aux_s_res->end_at;
                }
                search_result = here_execute_search_program_step(nb,
                                                                 buffer_end,
                                                        search_program->next);
                matches = here_matches(search_result);
                del_here_search_result_ctx(search_result);
                search_result = NULL;
                if (!matches) {
                    del_here_search_result_ctx(aux_s_res);
                } else {
                    search_result = aux_s_res;
                    if (search_program->ccomp == HERE_CTYPE_STAR ||
                        search_program->ccomp == HERE_CTYPE_QUESTION) {
                        search_result->end_at += 1;
                    }
                }
            }

            if (matches) {
                if (sub_search->ccomp == HERE_CTYPE_STAR ||
                    sub_search->ccomp == HERE_CTYPE_QUESTION) {
                    search_result->end_at -= 1;
                }
            }
            del_here_search_program_ctx(sub_search);
        }
    }

    if (!here_matches(search_result)) {
        new_here_search_result_ctx(search_result);
    }

    return search_result;
}
Ejemplo n.º 4
0
/*
static here_search_result_ctx *___match_list(const char *buffer,
                                          const char *buffer_end,
                           here_search_program_ctx *search_program) {
    const char *b, *data;
    here_search_program_ctx *sub_search = NULL;
    char sub_regex[3];
    here_search_result_ctx *search_result = NULL;
    int occur_nr = 0;
    int matches = 0;
    int should_return = 0;

    b = search_program->buffer;

    for (data = buffer; !should_return &&
                        data != buffer_end;) {
        memset(sub_regex, 0, sizeof(sub_regex));
        if (*b == '\\') {
            sub_regex[0] = *b;
            b++;
            sub_regex[1] = *b;
        } else {
            sub_regex[0] = *b;
        }
        sub_search = NULL;
        sub_search = add_regex_to_here_search_program_ctx(sub_search, sub_regex);
        sub_search->ccomp = search_program->ccomp;
        sub_search->min = search_program->min;
        sub_search->max = search_program->max;
        sub_search->neg = search_program->neg;
        sub_search->next = search_program->next;
        del_here_search_result_ctx(search_result);
        search_result = here_execute_search_program_step(data,
                                                         buffer_end, sub_search);

        if (here_matches(search_result)) {
            search_result->next_step = search_program->next;
        } else {
            if (search_result == NULL) break;
            if (sub_search->ccomp == HERE_CTYPE_RANGE) {
                occur_nr += (search_result->occur_nr > 0);
                if (search_program->max == -1) {
                    matches = (occur_nr == search_program->min);
                } else if (search_program->min > -1 && search_program->max > 0) {
                    matches = (occur_nr >= search_program->min &&
                               occur_nr <= search_program->max);
                } else if (search_program->max == 0) {
                    matches = (occur_nr >= search_program->min);
                }
                if (matches) {
                    search_result->start_at = data;
                    search_result->end_at = data;
                    search_result->next_step = search_program->next;
                }
            }
            if (b != (search_program->buffer +
                      search_program->buffer_size) - 1) {
                b++;
            } else {
                b = search_program->buffer;
                if (!search_program->neg) {
                    data++;
                }
            }
        }

        if (!search_program->neg) {
            should_return = here_matches(search_result);
        } else {
            if (!here_matches(search_result)) {
                should_return = 1;
                search_result->start_at = NULL;
                search_result->end_at = NULL;
                search_result->next_step = NULL;
            } else {
                b++;
                should_return = (b == (search_program->buffer +
                                       search_program->buffer_size) - 1);

            }
        }

        sub_search->next = NULL;
        del_here_search_program_ctx(sub_search);
    }
    return search_result;
}
*/
static here_search_result_ctx *match_list(const char *buffer,
                                          const char *buffer_end,
                           here_search_program_ctx *search_program) {
    const char *b, *data;
    here_search_program_ctx *sub_search = NULL;
    char sub_regex[3];
    const char *first_match = NULL;
    here_search_result_ctx *search_result = NULL, *aux_s_res = NULL;
    int occur_nr = 0;
    int matches = 0;
    int should_return = 0;

    b = search_program->buffer;

    for (data = buffer; !should_return &&
                        data != buffer_end;) {
        memset(sub_regex, 0, sizeof(sub_regex));
        if (*b == '\\') {
            sub_regex[0] = *b;
            b++;
            sub_regex[1] = *b;
        } else {
            sub_regex[0] = *b;
        }
        sub_search = NULL;
        sub_search = add_regex_to_here_search_program_ctx(sub_search, sub_regex);
        sub_search->ccomp = search_program->ccomp;
        sub_search->min = search_program->min;
        sub_search->max = search_program->max;
        sub_search->neg = search_program->neg;
        sub_search->next = search_program->next;
        del_here_search_result_ctx(search_result);
        search_result = here_execute_search_program_step(data,
                                                         buffer_end, sub_search);

        if (here_matches(search_result) &&
            (search_program->ccomp == HERE_CTYPE_PLUS) &&
            search_program->next != NULL) {
            if (first_match == NULL) {
                first_match = search_result->start_at;
            }
            aux_s_res = search_result;
            search_result = here_execute_search_program_step(data + 1,
                                     buffer_end, search_program->next);
            if (!here_matches(search_result)) {
                del_here_search_result_ctx(search_result);
                search_result = aux_s_res;
                search_result->start_at = NULL;
                search_result->end_at = NULL;
            } else {
                del_here_search_result_ctx(search_result);
                search_result = aux_s_res;
                search_result->start_at = first_match;
            }
        }


        if (here_matches(search_result)) {
            search_result->next_step = search_program->next;
        } else {
            if (search_result == NULL) break;
            if (sub_search->ccomp == HERE_CTYPE_RANGE) {
                occur_nr += (search_result->occur_nr > 0);
                if (search_program->max == -1) {
                    matches = (occur_nr == search_program->min);
                } else if (search_program->min > -1 && search_program->max > 0) {
                    matches = (occur_nr >= search_program->min &&
                               occur_nr <= search_program->max);
                } else if (search_program->max == 0) {
                    matches = (occur_nr >= search_program->min);
                }
                if (matches) {
                    search_result->start_at = data;
                    search_result->end_at = data;
                    search_result->next_step = search_program->next;
                }
            }
            if (b != (search_program->buffer +
                      search_program->buffer_size) - 1) {
                b++;
            } else {
                b = search_program->buffer;
                if (!search_program->neg) {
                    data++;
                }
            }
        }

        if (!search_program->neg) {
            should_return = here_matches(search_result);
        } else {
            if (!here_matches(search_result)) {
                should_return = 1;
                search_result->start_at = NULL;
                search_result->end_at = NULL;
                search_result->next_step = NULL;
            } else {
                b++;
                should_return = (b == (search_program->buffer +
                                       search_program->buffer_size) - 1);

            }
        }

        sub_search->next = NULL;
        del_here_search_program_ctx(sub_search);
    }
    return search_result;
}