Browse Source

- removed duplicate code using switch-case shortcuts

- refactoring, extracted into helper functions to improve readability
development
Andreas Romeyke 2 years ago
parent
commit
f69bc88b15
  1. 208
      src/parser/config_parser.c

208
src/parser/config_parser.c

@ -408,6 +408,108 @@ ret_t call_exec_function(ctiff_t * ctif, ret_t * retp, internal_entry_t * exep)
return ret;
}
/* ececute fc directly */
ret_t execute_function_and_set_results_for_next_call (ctiff_t * ctif, internal_entry_t * exep, ret_t * retp, int * precondition_result_p, function_t * precondition_function_p ) {
assert(NULL != ctif);
assert(NULL != exep);
assert(NULL != retp);
internal_entry_t exe = *exep;
ret_t ret = *retp;
ret = call_exec_function(ctif, &ret, &exe );
#ifdef EXE_DEBUG
printf("ret.returncode=%i, exe.is_precondition=%i is_valid=%i", ret.returncode, exe.is_precondition, is_valid);
if (exe.function == fc_tag_has_valid_type) {
printf("SSS tag %i has valid type: %i==%i (%s)\n", exe.tag, ret.returncode, is_valid, ret.value_found);
}
#endif
if (false == exe.is_precondition) { /* no precondition */
if (exe.tag >= MINTAGS) { /* exe.tag is 16bit, therefore true: && exe.tag <= (MAXTAGS-1)) */
parser_state.called_tags[exe.tag]=true; /* mark tag that it has a rule */
}
}
/* combine results */
if (
( true == exe.is_precondition ) &&
(
! (
exe.function == fc_tag_has_valid_type &&
(
ret.returncode == tagerror_unexpected_type_found ||
ret.returncode == tag_does_not_exist
)
)
)
) { /* precondition, next run depends on this result */
*precondition_result_p = ret.returncode;
if (NULL != ret.value_found) {
free(ret.value_found);
ret.value_found = NULL;
}
//if (NULL != expected_value) {
// free(expected_value);
// expected_value = NULL;
//}
} else { /* no precondition function */
/* if true, execute function */
full_res_t full;
full.tag = exe.tag;
full.function=exe.function;
full.lineno=exe.lineno;
full.returncode=ret.returncode;
full.expected_value=NULL;
full.found_value=NULL;
/*
if (NULL != expected_value) {
if (ret.returncode != is_valid) {
full.expected_value = expected_value;
} else {
free (expected_value);
expected_value = NULL;
}
}
*/
if (ret.returncode != is_valid) {
full.found_value = ret.value_found;
} else { free( ret.value_found ); ret.value_found=NULL; }
result_push( full );
*precondition_result_p=is_valid;
*precondition_function_p = exe.function;
}
#ifdef EXE_DEBUG
printf(" precondition_result=%i\n", *precondition_result_p);
#endif
return ret;
}
ret_t clean_logical_or_if_precondition_failed (internal_entry_t * exep, ret_t * retp) {
assert(NULL != exep);
assert(NULL != retp);
internal_entry_t exe = *exep;
ret_t ret = *retp;
#ifdef EXE_DEBUG
printf ( "\t\t\t\t\t\t\t####################################\n" );
printf ( "exe function: %s\n", get_parser_function_description ( exe.function ) );
#endif
if ( exe.function == fc_internal_logic_combine_open ) { /* clean logical or if precondition fails */
do {
internal_entry_t l_exe = exe_pop();
#ifdef EXE_DEBUG
printf ( "~~~ eliminating Logicals because PRE fails: %s\n", get_parser_function_description ( l_exe.function ) );
#endif
if ( l_exe.function == fc_internal_logic_combine_close ) break;
} while ( parser_state.exe_stackp > 0 );
#ifdef EXE_DEBUG
printf ( "\t\t\t\t\t\t\t# ende\n" );
#endif
}
ret.returncode = is_valid;
return ret;
}
/* executes a plan (list) of functions, checks if predicate-function calls are
* needed, too. The plan is a global variable.
*
@ -463,96 +565,14 @@ void execute_plan (ctiff_t * ctif) {
printf("should we go in fc call? %s\n", ( /* last was successful precondition */
should_we_go_in_fc_call)?"true":"false");
printf("parsing function %s (%i) (linecode=%i)\n", get_parser_function_description( exe.function), exe.function, exe.lineno);
#endif
if ( should_we_go_in_fc_call ) { /* if true, execute function */
ret = call_exec_function(ctif, &ret, &exe );
#ifdef EXE_DEBUG
printf("ret.returncode=%i, exe.is_precondition=%i is_valid=%i", ret.returncode, exe.is_precondition, is_valid);
if (exe.function == fc_tag_has_valid_type) {
printf("SSS tag %i has valid type: %i==%i (%s)\n", exe.tag, ret.returncode, is_valid, ret.value_found);
}
#endif
if (false == exe.is_precondition) {
if (exe.tag >= MINTAGS) { /* exe.tag is 16bit, therefore true: && exe.tag <= (MAXTAGS-1)) */
parser_state.called_tags[exe.tag]=true; /* mark tag that it has a rule */
}
}
/* combine results */
if (
(true == exe.is_precondition) &&
(!(
exe.function == fc_tag_has_valid_type &&
(
ret.returncode == tagerror_unexpected_type_found ||
ret.returncode == tag_does_not_exist)
))
) { /* precondition, next run depends on this result */
precondition_result = ret.returncode;
if (NULL != ret.value_found) {
free(ret.value_found);
ret.value_found = NULL;
}
//if (NULL != expected_value) {
// free(expected_value);
// expected_value = NULL;
//}
} else { /* no precondition function */
/* if true, execute function */
full_res_t full;
full.tag = exe.tag;
full.function=exe.function;
full.lineno=exe.lineno;
full.returncode=ret.returncode;
full.expected_value=NULL;
full.found_value=NULL;
/*
if (NULL != expected_value) {
if (ret.returncode != is_valid) {
full.expected_value = expected_value;
} else {
free (expected_value);
expected_value = NULL;
}
}
*/
if (ret.returncode != is_valid) {
full.found_value = ret.value_found;
} else { free( ret.value_found ); ret.value_found=NULL; }
result_push( full );
precondition_result=is_valid;
precondition_function = exe.function;
}
#ifdef EXE_DEBUG
printf(" precondition_result=%i\n", precondition_result);
#endif
} else { // if precondition fails, return precondition if they have unexpected value!
if (true == last_run_was_a_precondition) {
#ifdef EXE_DEBUG
printf("\t\t\t\t\t\t\t####################################\n");
printf("exe function: %s\n", get_parser_function_description(exe.function));
#endif
if (exe.function == fc_internal_logic_combine_open) { /* clean logical or if precondition fails */
do {
internal_entry_t l_exe = exe_pop();
#ifdef EXE_DEBUG
printf("~~~ eliminating Logicals because PRE fails: %s\n", get_parser_function_description(l_exe.function));
#endif
if (l_exe.function == fc_internal_logic_combine_close) break;
} while (parser_state.exe_stackp > 0);
#ifdef EXE_DEBUG
printf("\t\t\t\t\t\t\t# ende\n");
#endif
}
ret.returncode = is_valid;
ret = execute_function_and_set_results_for_next_call(ctif, &exe, &ret, &precondition_result, &precondition_function );
} else { // if precondition fails, return precondition if they have unexpected value!
if (last_run_was_a_precondition) {
ret = clean_logical_or_if_precondition_failed(&exe, &ret);
}
}
}
last_run_was_a_precondition=exe.is_precondition;
assert( ret.returncode != should_not_occur );
#ifdef EXE_DEBUG
@ -892,6 +912,7 @@ void evaluate_req_and_push_exe(requirements_t req, internal_entry_t e) {
exe_push(e);
switch (req) {
case ifdepends:
case optional:
{
internal_entry_t p = prepare_internal_entry();
@ -915,19 +936,6 @@ void evaluate_req_and_push_exe(requirements_t req, internal_entry_t e) {
}
break;
}
case optional:
{
internal_entry_t p = prepare_internal_entry();
if (parser_state.mode & mode_enable_type_checks) {
internal_entry_t z = e;
z.is_precondition = true;
z.function = fc_tag_has_valid_type;
exe_push(z);
}
exe_push(p);
break;
}
case optdepends:
{
internal_entry_t pp;

Loading…
Cancel
Save