Signed-off-by: Jakub Filak jfilak@redhat.com --- src/cli/cli.c | 160 +++++++++++++++++++++++++-------------------------------- 1 file changed, 70 insertions(+), 90 deletions(-)
diff --git a/src/cli/cli.c b/src/cli/cli.c index 244dce0..eca8bda 100644 --- a/src/cli/cli.c +++ b/src/cli/cli.c @@ -22,8 +22,48 @@ #include <syslog.h> #include "internal_libreport.h" #include "cli-report.h" +#include "run_event_list.h"
-static char *do_log(char *log_line, void *param) +static enum elp_signal_ret cli_event_done(struct event_list_process *process) +{ + const char *const event_name = elp_get_current_event(process); + const enum elp_event_run_status status = elp_get_last_event_status(process); + + switch(status) + { + case ELP_ERS_SUCCESSFUL: + VERB1 log("Successfull event '%s'", event_name); + return ELPSR_CONTINUE; + + case ELP_ERS_FAILED: + error_msg("Event '%s' failed", event_name); + return ELPSR_FINISH; + + case ELP_ERS_NOT_FOUND: + error_msg("No actions are found for event '%s'", event_name); + return ELPSR_FINISH; + + case ELP_ERS_NONE: + /* fall throug */ + default: + error_msg_and_die("unexpected event return status '%d'", status); + break; + } + + return ELPSR_FINISH; +} + +enum elp_signal_ret cli_configuration_issue(const struct event_list_process *process) +{ + const struct elp_configuration_issue *last = elp_get_last_configuration_issue(process); + log("Found configuration flaws for event '%s'", last->event_name); + for(const GList *i = last->issues; i; i = g_list_next(i)) + log("* %s", (char *)i->data); + + return last->usability == EUS_UNUSABLE ? ELPSR_NEXT_EVENT : ELPSR_CONTINUE; +} + +static char* cli_logging_callback(char *log_line, void *param) { log("%s", log_line); return log_line; @@ -48,7 +88,7 @@ int main(int argc, char** argv) #endif
GList *D_list = NULL; - const char *event_name = NULL; + GList *events_list = NULL; const char *pfx = "";
/* Can't keep these strings/structs static: _() doesn't support that */ @@ -62,31 +102,23 @@ int main(int argc, char** argv) enum { OPT_list_events = 1 << 0, OPT_run_event = 1 << 1, - OPT_analyze = 1 << 2, - OPT_collect = 1 << 3, - OPT_report = 1 << 4, - OPT_version = 1 << 5, - OPT_delete = 1 << 6, - OPTMASK_op = OPT_list_events|OPT_run_event|OPT_analyze|OPT_collect|OPT_report|OPT_version, - OPTMASK_need_arg = OPT_run_event|OPT_analyze|OPT_collect|OPT_report, - OPT_y = 1 << 7, - OPT_o = 1 << 8, - OPT_v = 1 << 9, - OPT_s = 1 << 10, - OPT_p = 1 << 11, + OPT_version = 1 << 2, + OPT_delete = 1 << 3, + OPTMASK_op = OPT_list_events|OPT_run_event|OPT_version, + OPTMASK_need_arg = OPT_run_event, + OPT_y = 1 << 4, + OPT_v = 1 << 5, + OPT_s = 1 << 6, + OPT_p = 1 << 7, }; /* Keep enum above and order of options below in sync! */ struct options program_options[] = { /* short_name long_name value parameter_name help */ OPT_OPTSTRING('L', NULL , &pfx, "PREFIX", _("List possible events [which start with PREFIX]")), - OPT_STRING( 'e', NULL , &event_name, "EVENT", _("Run EVENT on DUMP_DIR")), - OPT_BOOL( 'a', "analyze", NULL, _("Run analyze event(s) on DUMP_DIR")), - OPT_BOOL( 'c', "collect", NULL, _("Run collect event(s) on DUMP_DIR")), - OPT_BOOL( 'r', "report" , NULL, _("Analyze, collect and report problem data in DUMP_DIR")), + OPT_LIST( 'e', "event" , &events_list, "EVENT", _("Run only this event")), OPT_BOOL( 'V', "version", NULL, _("Display version and exit")), - OPT_BOOL( 'd', "delete" , NULL, _("Remove DUMP_DIR after reporting")), + OPT_BOOL( 'd', "delete" , NULL, _("Remove DUMP_DIR")), OPT_BOOL( 'y', "always" , NULL, _("Noninteractive: don't ask questions, assume 'yes'")), - OPT_BOOL( 'o', "report-only" , NULL, _("With -r: do not run analyzers, run only reporters")), OPT__VERBOSE(&g_verbose), OPT_BOOL( 's', NULL , NULL, _("Log to syslog")), OPT_BOOL( 'p', NULL , NULL, _("Add program names to log")), @@ -124,8 +156,6 @@ int main(int argc, char** argv) }
char *dump_dir_name = argv[0]; - bool always = (opts & OPT_y); - bool report_only = (opts & OPT_o);
if (!D_list) { @@ -154,76 +184,26 @@ int main(int argc, char** argv) } case OPT_run_event: /* -e EVENT: run event */ { - struct run_event_state *run_state = new_run_event_state(); - run_state->logging_callback = do_log; - int r = run_event_on_dir_name(run_state, dump_dir_name, event_name); - if (r == 0 && run_state->children_count == 0) - error_msg_and_die("No actions are found for event '%s'", event_name); - free_run_event_state(run_state); - break; - } - case OPT_analyze: - { - /* Load problem_data from dump dir */ - struct dump_dir *dd = dd_opendir(dump_dir_name, DD_OPEN_READONLY); - if (!dd) - return 1; - char *analyze_events_as_lines = list_possible_events(dd, NULL, "analyze"); - dd_close(dd); - - if (analyze_events_as_lines && *analyze_events_as_lines) - { - GList *list_analyze_events = str_to_glist(analyze_events_as_lines, '\n'); - char *event = select_event_option(list_analyze_events); - list_free_with_free(list_analyze_events); - exitcode = run_analyze_event(dump_dir_name, event); - free(event); - } - free(analyze_events_as_lines); - break; - } - case OPT_collect: - { - exitcode = collect(dump_dir_name, always); + struct elp_signals_impl impl; + event_list_process_impl_init(&impl); + impl.event_done=cli_event_done;
- /* Be consistent and return 1 when opening dd failed */ - if (exitcode == -1) - return 1; + struct run_event_impl *run_impl = new_text_run_event_impl();
- break; - } - case OPT_report: - { - struct dump_dir *dd = dd_opendir(dump_dir_name, DD_OPEN_READONLY); - if (!dd) - return 1; - int readonly = !dd->locked; - dd_close(dd); - if (readonly) - { - log("'%s' is not writable", dump_dir_name); - /* D_list can't be NULL here */ - struct dump_dir *dd_copy = steal_directory((char *)D_list->data, dump_dir_name); - if (dd_copy) - { - delete_dump_dir_possibly_using_abrtd(dump_dir_name); - dump_dir_name = xstrdup(dd_copy->dd_dirname); - dd_close(dd_copy); - } - } - - exitcode = report(dump_dir_name, - (always ? CLI_REPORT_BATCH : 0) | - (report_only ? CLI_REPORT_ONLY : 0)); - if (exitcode == -1) - error_msg_and_die("Crash '%s' not found", dump_dir_name); - - if (opts & OPT_delete) - { - int r = delete_dump_dir_possibly_using_abrtd(dump_dir_name); - if (exitcode == 0) - exitcode = r; - } + struct event_list_process *process = new_event_list_process(events_list, + &impl, + run_impl, + NULL, + dump_dir_name, + NULL); + + elp_set_logging_callback(process, cli_logging_callback, NULL); + + while(elp_next_step(process)) + ; + + free_event_list_process(process); + free_text_run_event_impl(run_impl);
break; }