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; }
char *hvm_int_to_str(const hefesto_int_t value) { char *result = (char *) hefesto_mloc(1024); sprintf(result, "%d", value); return result; }
void *hvm_call_function(const char *func_call, hefesto_var_list_ctx **lo_vars, hefesto_var_list_ctx **gl_vars, hefesto_func_list_ctx *functions) { char *f_lbl, *fc, *fl; hefesto_func_list_ctx *function, *curr_exec_fp; void *result = NULL; struct stacked_function_execution_point_ctx *sv_p; if (func_call == NULL) return NULL; f_lbl = (char *) hefesto_mloc(HEFESTO_MAX_BUFFER_SIZE); memset(f_lbl, 0, HEFESTO_MAX_BUFFER_SIZE); for (fc = (char *) func_call; is_hefesto_blank(*fc); fc++); for (fl = f_lbl; *fc != 0 && *fc != '(' && !is_hefesto_blank(*fc); fc++, fl++) *fl = *fc; *fl = 0; for (fc = (char *)func_call; is_hefesto_blank(*fc); fc++); curr_exec_fp = hvm_get_current_executed_function(); if ((function = get_hefesto_func_list_ctx_scoped_name(f_lbl, (curr_exec_fp) ? curr_exec_fp->decl_at : NULL, functions))) { // INFO(Santiago): saves the execution context. sv_p = hvm_save_execution_point(function); hvm_set_current_executed_function(function); hvm_init_function_args(fc+1, lo_vars, gl_vars, functions, &function); result = hvm_exec_function(function, &function->vars, gl_vars, functions); // INFO(Santiago): restores the execution context saved above. hvm_restore_execution_point(&function, sv_p); del_stacked_function_execution_point_ctx(sv_p); } free(f_lbl); return result; }
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; }
static char *infix2postfix_function_args(const char *expr_args, const size_t esize) { char *e; char *iexpr, *pexpr; char *result = (char *) hefesto_mloc(HEFESTO_MAX_BUFFER_SIZE); char *r; size_t offset = 1; iexpr = get_arg_from_call(expr_args, &offset); r = result; *r = '('; r++; while (*iexpr) { pexpr = infix2postfix(iexpr, strlen(iexpr), 0); for (e = pexpr; *e != 0; e++, r++) *r = *e; free(iexpr); free(pexpr); iexpr = get_arg_from_call(expr_args, &offset); if (*iexpr) { *r = ','; r++; } } free(iexpr); *r = 0; return result; }
static void *hvm_str_len(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) { void *result; result = (void *) hefesto_mloc(sizeof(hefesto_int_t)); hefesto_int_t len; memset(result, 0, sizeof(hefesto_int_t)); *otype = HEFESTO_VAR_TYPE_INT; if (string_var == NULL || (*string_var) == NULL || (*string_var)->data == NULL) return result; len = strlen((char *)(*string_var)->data); *(hefesto_int_t *)result = len; return result; }
static void *hvm_str_at(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 index, offset = 0, outsz; hefesto_type_t etype = HEFESTO_VAR_TYPE_INT; char *result; void *index_p; char *arg; const char *m; if (string_var == NULL || (*string_var) == NULL || (*string_var)->data == NULL) return NULL; for (m = method; *m != '(' && *m != 0; m++); arg = get_next_call_args(m+1, &offset); index_p = expr_eval(arg, lo_vars, gl_vars, functions, &etype, &outsz); index = *(size_t *)index_p; free(index_p); free(arg); result = (char *) hefesto_mloc(sizeof(char)*2); if (strlen((char *)(*string_var)->data) > 0) { *result = *((char *)(*string_var)->data + (index % strlen((char *)(*string_var)->data))); } else { *result = 0; } *(result+1) = 0; *otype = HEFESTO_VAR_TYPE_STRING; return result; }
char *infix2postfix(const char *expr, const size_t esize, const hefesto_int_t main_call) { const char *efinis = expr + esize, *e; char *t, *term, *tt; hefesto_common_stack_ctx *sp = NULL, *dp = NULL; hefesto_common_list_ctx *lp = NULL, *l; hefesto_int_t p, tp; size_t sz; hefesto_int_t bracket = 0; char *indexing; char *iexpr; char *args; size_t offset, count; hefesto_int_t state = 0; term = (char *) hefesto_mloc(HEFESTO_MAX_BUFFER_SIZE); memset(term, 0, HEFESTO_MAX_BUFFER_SIZE); t = term; *t = 0; for (e = expr; e != efinis; e++) { if (*e == 0) break; if (*e == '\n' || *e == '\r' || *e == '\t' || *e == ' ') continue; if (*e != 0 && *e != ' ' && *e != '\n' && *e != '\r' && *e != '\t' && *e != '(' && *e != ')' && !is_op(*e) && *e != '[' && !is_hefesto_string_tok(*e)) { *t = *e; t++; *t = 0; continue; } else if (is_hefesto_string_tok(*e)) { offset = 0; iexpr = get_next_string(e, &offset); e += offset; for (indexing = iexpr; *indexing != 0; indexing++, t++) *t = *indexing; free(iexpr); *t = 0; } else if (*e == '(' && !is_hefesto_numeric_constant(term) && !is_hefesto_string(term) && *term && *term != '(' && !is_op(*term)) { bracket++; offset = 0; args = get_next_call_args(e, &offset); if (*args) { iexpr = infix2postfix_function_args(args, strlen(args)); tt = t; for (indexing = iexpr; *indexing != 0; indexing++, t++) *t = *indexing; *t = 0; free(iexpr); count = 0; for (t = tt; *t != 0; t++) { if (*t == '(') { count++; } else if (*t == ')') { count--; } else if (*t == '"') { t++; while (*t != '"' && *t != 0) { t++; if (*t == '\\') t += 2; } } } // WARN(Santiago): null buffer position skipping avoidance. while (*t == 0 && t != term) { t--; } t++; tt = ((char *)e + offset); while (is_hefesto_blank(*tt)) tt++; // can't add one more ')' because this is a function that is an // argument of another function if (*tt == ',') count--; while (count > 0) { *t = ')'; t++; count--; } *t = 0; } e += offset; free(args); if (*e == ')') e++; } else if ((*e == '-' || *e == '+') && t == term && isdigit(*(e+1)) && state == 0) { *t = *e; t++; continue; } if ((is_op(*e) || *e == '(' || *e == ')') && t == term) { *t = *e; t++; *t = 0; if (is_op(*(e+1)) && *e != '(' && *e != ')') { // composed operands *t = *(e+1); e++; t++; *t = 0; } state = 0; } else if ((e + 1) != efinis) e--; if (strcmp(term,"(") == 0) { sp = hefesto_common_stack_ctx_push(sp, (void *) term, t - term, HEFESTO_VAR_TYPE_UNTYPED); } else if(strcmp(term, ")") == 0) { t = (char *)hefesto_common_stack_ctx_data_on_top(sp); if (t && strcmp(t, "(") != 0) { while (sp && sp->data != NULL && strcmp(sp->data, "(") != 0) { lp = add_data_to_hefesto_common_list_ctx(lp, hefesto_common_stack_ctx_data_on_top(sp), hefesto_common_stack_ctx_dsize_on_top(sp)); dp = hefesto_common_stack_ctx_on_top(sp); sp = hefesto_common_stack_ctx_pop(sp); del_hefesto_common_stack_ctx(dp); } } dp = hefesto_common_stack_ctx_on_top(sp); sp = hefesto_common_stack_ctx_pop(sp); del_hefesto_common_stack_ctx(dp); } else if ((p = get_op_precedence(term)) != -1) { tp = get_op_precedence(hefesto_common_stack_ctx_data_on_top(sp)); while (tp != -1 && tp >= p) { lp = add_data_to_hefesto_common_list_ctx(lp, hefesto_common_stack_ctx_data_on_top(sp), hefesto_common_stack_ctx_dsize_on_top(sp)); dp = hefesto_common_stack_ctx_on_top(sp); sp = hefesto_common_stack_ctx_pop(sp); del_hefesto_common_stack_ctx(dp); tp = get_op_precedence(hefesto_common_stack_ctx_data_on_top(sp)); } sp = hefesto_common_stack_ctx_push(sp, (void *) term, t - term, HEFESTO_VAR_TYPE_UNTYPED); } else { lp = add_data_to_hefesto_common_list_ctx(lp, term, t - term); state = 1; } t = term; *t = 0; } if (*term) { sp = hefesto_common_stack_ctx_push(sp, (void *) term, t - term, HEFESTO_VAR_TYPE_UNTYPED); } while (!hefesto_common_stack_ctx_empty(sp)) { lp = add_data_to_hefesto_common_list_ctx(lp, hefesto_common_stack_ctx_data_on_top(sp), hefesto_common_stack_ctx_dsize_on_top(sp)); dp = hefesto_common_stack_ctx_on_top(sp); sp = hefesto_common_stack_ctx_pop(sp); del_hefesto_common_stack_ctx(dp); } free(term); for (p = 0, sz = 0, l = lp; l; l = l->next, p++) { sz += l->dsize; } sz += (size_t) p; term = (char *) hefesto_mloc(sz); *term = 0; for (l = lp; l; l = l->next) { if (*(char *)l->data == '(') continue; strncat(term, (char *) l->data, sz - 1); if (l->next && *(char *)l->next->data != '[') strncat(term, " ", sz - 1); } del_hefesto_common_list_ctx(lp); return term; }
char *infix2postfix_args(const char *arg_list, const size_t arg_list_size) { char *e = NULL; const char *ap = NULL, *ap_end = NULL; char *arg_pf = NULL; size_t offset = 0; size_t real_arg_list_size = 0; char *retval = NULL; char arg_list_cpy[HEFESTO_MAX_BUFFER_SIZE]; if (arg_list == NULL) { return NULL; } memset(arg_list_cpy, 0, sizeof(arg_list_cpy)); strncpy(arg_list_cpy, arg_list, sizeof(arg_list_cpy) - 1); real_arg_list_size = strnlen(arg_list_cpy, sizeof(arg_list_cpy)); ap_end = get_stmt_end(arg_list_cpy, real_arg_list_size); ap = &arg_list_cpy[0]; if (ap_end != NULL) { memset((char *)ap_end, 0, real_arg_list_size - (ap_end - ap)); } while (*ap != '(' && *ap != 0) { ap++; } if (*ap == 0) { return NULL; } ap++; retval = (char *) hefesto_mloc(arg_list_size * 2); memset(retval, 0, arg_list_size * 2); *retval = '('; e = get_arg_from_call(ap, &offset); while (*e) { arg_pf = infix2postfix(e, strlen(e), 1); strcat(retval, arg_pf); free(arg_pf); free(e); if (arg_list_cpy[offset] == ',') { offset++; } e = get_arg_from_call(arg_list_cpy, &offset); if (*e) { strcat(retval, ","); } else { strcat(retval, ")"); } } free(e); return retval; }
hefesto_int_t hvm_forge_project(hefesto_project_ctx *project, hefesto_var_list_ctx **gl_vars, hefesto_func_list_ctx *functions) { //hefesto_int_t build_result = 0; void *result; char *expr_pfix; char forge_invocation[HEFESTO_MAX_BUFFER_SIZE] = ""; hefesto_common_list_ctx *arg; hefesto_var_list_ctx *lo_vars_dummy = NULL; struct stacked_function_execution_point_ctx *sv_p; hefesto_project_ctx *last_project; size_t t_sz; last_project = HEFESTO_CURRENT_PROJECT; HEFESTO_CURRENT_PROJECT = project; // executes the prologue if (project->prologue && project->prologue->code) { hvm_set_current_executed_function(project->prologue); result = hvm_exec_function(project->prologue, &project->prologue->vars, gl_vars, functions); if (result != NULL) free(result); } if (project->dep_chain_expr != NULL) { expr_pfix = infix2postfix(project->dep_chain_expr, strlen(project->dep_chain_expr), 1); result = hvm_str_format(expr_pfix, &lo_vars_dummy, gl_vars, functions); free(expr_pfix); if (result != NULL) { project->dep_chain = get_dep_chain_by_user_dep_string(result); //project->dep_chain = NULL; if (result != NULL) { t_sz = strlen(result); project->dep_chain_str = (char *) hefesto_mloc(t_sz + 1); memset(project->dep_chain_str, 0, t_sz + 1); memcpy(project->dep_chain_str, result, t_sz); } free(result); HEFESTO_CURRENT_DEP_CHAIN = project->dep_chain; set_hvm_toolset_src_changes_check_flag(0); } } // executes the forge if (HEFESTO_EXIT == 0 && project->toolset && project->toolset->forge) { strncpy(forge_invocation, project->toolset->forge->name, sizeof(forge_invocation)-1); strcat(forge_invocation, "("); for (arg = project->args; arg; arg = arg->next) { strcat(forge_invocation, (char *)arg->data); if (arg->next) { strcat(forge_invocation, ", "); } } strcat(forge_invocation, ");"); HEFESTO_CURRENT_TOOLSET = project->toolset; // saves the execution context. sv_p = hvm_save_execution_point(project->toolset->forge); hvm_init_function_args(forge_invocation, gl_vars, gl_vars, functions, &project->toolset->forge); hvm_set_current_executed_function(project->toolset->forge); result = hvm_exec_function(project->toolset->forge, &project->toolset->forge->vars, gl_vars, functions); // restores the execution context saved above. hvm_restore_execution_point(&project->toolset->forge, sv_p); del_stacked_function_execution_point_ctx(sv_p); if (result != NULL) { HEFESTO_LAST_FORGE_RESULT = *(hefesto_int_t *)result; free(result); } } else { if (project->toolset == NULL) { printf(">>>> hefesto PANIC: NULL toolset!!! RUN FOREST!!\n"); } if (project->toolset->forge == NULL) { printf(">>>> hefesto PANIC: Hey Beavis Huh we got a NULL " "forge function...\nYeah Yeah Butt-Head, it's not " "good...\nShut-up Beavis, Huh Bughole Huh!\n"); } } HEFESTO_EXIT = 0; // because the epilogue needs to be executed anyway, // except when exit_forge() syscall was called. // executes the epilogue if (project && !HEFESTO_EXIT_FORGE) { if (project->epilogue && project->epilogue->code) { hvm_set_current_executed_function(project->epilogue); result = hvm_exec_function(project->epilogue, &project->epilogue->vars, gl_vars, functions); if (result != NULL) free(result); } } if (project->dep_chain) { del_hefesto_dep_chain_ctx(project->dep_chain); project->dep_chain = NULL; } HEFESTO_CURRENT_PROJECT = last_project; return HEFESTO_LAST_FORGE_RESULT; }
static void *hvm_while(hefesto_command_list_ctx *cmd, hefesto_var_list_ctx **lo_vars, hefesto_var_list_ctx **gl_vars, hefesto_func_list_ctx *functions, hefesto_int_t *should_return) { void *test; hefesto_type_t etype = HEFESTO_VAR_TYPE_INT; size_t osize; hefesto_command_list_ctx *cmd_p = NULL, *tmp_cmd_p = NULL, *lst_cmd_p = NULL; void *result = NULL; if (cmd->sub != NULL) { cmd_p = cmd->sub; } else { cmd_p = cmd->next; lst_cmd_p = get_last_cmd_to_exec(cmd_p); if (lst_cmd_p != NULL) { tmp_cmd_p = lst_cmd_p->next; lst_cmd_p->next = NULL; } } test = expr_eval(cmd->expr, lo_vars, gl_vars, functions, &etype, &osize); hvm_break_loop = 0; hvm_is_inside_a_loop++; while (test && *(hefesto_int_t *)test && !HEFESTO_EXIT) { free(test); test = NULL; result = hvm_exec_command_list(cmd_p, lo_vars, gl_vars, functions, should_return); if ((should_return && *should_return) || hvm_break_loop) { hvm_break_loop = 0; break; } else if (hvm_continue_loop) { if (result != NULL) free(result); hvm_continue_loop = 0; test = (void *) hefesto_mloc(sizeof(hefesto_int_t)); *(hefesto_int_t *)test = 1; continue; } hvm_set_current_executed_instruction(HEFESTO_WHILE); test = expr_eval(cmd->expr, lo_vars, gl_vars, functions, &etype, &osize); if (test && *(hefesto_int_t *)test) { if (result != NULL) free(result); } } //if (cmd->sub == NULL) { // cmd_p->next = tmp_cmd_p; //} if (tmp_cmd_p != NULL) { //cmd_p->next = tmp_cmd_p; lst_cmd_p->next = tmp_cmd_p; } if (test != NULL) free(test); hvm_is_inside_a_loop--; return result; }
void hvm_init_function_args(const char *args, hefesto_var_list_ctx **lo_vars, hefesto_var_list_ctx **gl_vars, hefesto_func_list_ctx *functions, hefesto_func_list_ctx **function) { char *expr; char *expr_pfix; void *expr_result; hefesto_type_t etype; size_t sz; hefesto_var_list_ctx *vp = (*function)->vars, *ap = (*function)->args, *vlp = NULL; size_t offset = 0; //hefesto_instruction_code_t c_intr = hvm_get_current_executed_instruction(); char *temp = (char *) hefesto_mloc(HEFESTO_MAX_BUFFER_SIZE); strncpy(temp, args, HEFESTO_MAX_BUFFER_SIZE); strcat(temp, ";"); //printf("FUNCTION: %s [%s]\n", (*function)->name, temp); for (expr = temp; *expr != 0 && *expr != '('; expr++); offset = expr - args + 1; expr = get_arg_from_call(args, &offset); while (vp != NULL && *expr != 0) { if (ap->contents != NULL) { del_hefesto_common_list_ctx(ap->contents); ap->contents = NULL; } ap->contents = add_data_to_hefesto_common_list_ctx(ap->contents, expr, strlen(expr)); // if (c_intr != HEFESTO_ATTRIB && c_intr != HEFESTO_RET && // c_intr != HEFESTO_IF && c_intr != HEFESTO_WHILE) { // expr_pfix = infix2postfix(expr, strlen(expr), 1); // free(expr); // } else { expr_pfix = expr; // } etype = vp->type; //printf("\tF-ARG: %s\n", expr_pfix); // INFO(Santiago): // truque sujo, aqui gl_vars precisa ser varrida primeiro, lo_vars nem // precisa pois argumentos nao serao formados por variaveis ou // expressoes com variaveis definidas no corpo dessa mesma funcao // prestes a ser executada. //expr_result = expr_eval(expr_pfix, (*lo_vars == vp) ? gl_vars : lo_vars, gl_vars, functions, &etype, &sz); /*if (hvm_get_last_executed_function() != NULL) { printf("last -> %s\n", hvm_get_last_executed_function()->name); } printf("current -> %s\n", (*function)->name);*/ expr_result = expr_eval(expr_pfix, (hvm_get_last_executed_function() == *function) ? &(*function)->vars : lo_vars, gl_vars, functions, &etype, &sz); if (*expr_pfix == '$') { vlp = get_hefesto_var_list_ctx_name(expr_pfix + 1, *lo_vars); if (vlp == NULL) { vlp = get_hefesto_var_list_ctx_name(expr_pfix + 1, *gl_vars); } } free(expr_pfix); if (vp->contents != NULL) { del_hefesto_common_list_ctx(vp->contents); vp->contents = NULL; } if (etype != HEFESTO_VAR_TYPE_LIST) { vp->contents = add_data_to_hefesto_common_list_ctx(vp->contents, expr_result, (etype == HEFESTO_VAR_TYPE_STRING) ? strlen((char*)expr_result) : sizeof(hefesto_int_t)); free(expr_result); } else { if (vlp != NULL) { vp->subtype = vlp->subtype; } else { vp->subtype = get_hsl_list_subtype((hefesto_common_list_ctx *)expr_result); /*if (((hefesto_common_list_ctx *)expr_result)->data && is_hefesto_numeric_constant(((hefesto_common_list_ctx *) expr_result)->data)) { vp->subtype = HEFESTO_VAR_TYPE_INT; } else { vp->subtype = HEFESTO_VAR_TYPE_STRING; }*/ } vp->contents = cp_hefesto_common_list_ctx((hefesto_common_list_ctx *)expr_result); del_hefesto_common_list_ctx((hefesto_common_list_ctx *)expr_result); } vp = vp->next; ap = ap->next; expr = get_arg_from_call(args, &offset); } free(expr); // we must to clear local stack initializing all with null or zero. for (; vp != NULL; vp = vp->next) { if (vp->type == HEFESTO_VAR_TYPE_FILE_DESCRIPTOR && vp->contents) { close_hefesto_file_handle((hefesto_file_handle *) vp->contents->data); } del_hefesto_common_list_ctx(vp->contents); new_hefesto_common_list_ctx(vp->contents); if (vp->type == HEFESTO_VAR_TYPE_LIST) { vp->subtype = HEFESTO_VAR_TYPE_NONE; vp->contents->is_dummy_item = 1; } } }