baab13
From 46fac7e2c0eaf98668698558ec4acbc2ade76ba7 Mon Sep 17 00:00:00 2001
baab13
From: Matej Habrnal <mhabrnal@redhat.com>
baab13
Date: Sat, 20 Sep 2014 22:50:11 +0200
baab13
Subject: [ABRT PATCH 65/66] cli: batch reporting in abrt-cli
baab13
baab13
Added option process (p) to the abrt-cli.
baab13
With option process abrt-cli goes through all problems one by one (when
baab13
parameter --since is not specified) and asks the user what action will be
baab13
executed.
baab13
baab13
Resolves #1066482
baab13
baab13
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
baab13
Signed-off-by: Jakub Filak <jfilak@redhat.com>
baab13
---
baab13
 doc/abrt-cli.txt      |   2 +
baab13
 po/POTFILES.in        |   1 +
baab13
 src/cli/Makefile.am   |   1 +
baab13
 src/cli/abrt-cli.c    |   1 +
baab13
 src/cli/builtin-cmd.h |   1 +
baab13
 src/cli/process.c     | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++
baab13
 6 files changed, 175 insertions(+)
baab13
 create mode 100644 src/cli/process.c
baab13
baab13
diff --git a/doc/abrt-cli.txt b/doc/abrt-cli.txt
baab13
index 1c95655..cd14bc9 100644
baab13
--- a/doc/abrt-cli.txt
baab13
+++ b/doc/abrt-cli.txt
baab13
@@ -15,6 +15,8 @@ SYNOPSIS
baab13
 
baab13
 'abrt-cli' info   [-vd] [-s SIZE] DIR...
baab13
 
baab13
+'abrt-cli' process [-v] DIR...
baab13
+
baab13
 OPTIONS
baab13
 -------
baab13
 -v,--verbose::
baab13
diff --git a/po/POTFILES.in b/po/POTFILES.in
baab13
index 141c73a..cbe89fa 100644
baab13
--- a/po/POTFILES.in
baab13
+++ b/po/POTFILES.in
baab13
@@ -52,6 +52,7 @@ src/cli/abrt-cli.c
baab13
 src/cli/list.c
baab13
 src/cli/status.c
baab13
 src/cli/report.c
baab13
+src/cli/process.c
baab13
 
baab13
 src/plugins/analyze_CCpp.xml.in
baab13
 src/plugins/analyze_VMcore.xml.in
baab13
diff --git a/src/cli/Makefile.am b/src/cli/Makefile.am
baab13
index 75efac5..9fff5b3 100644
baab13
--- a/src/cli/Makefile.am
baab13
+++ b/src/cli/Makefile.am
baab13
@@ -9,6 +9,7 @@ BUILTIN_C += list.c
baab13
 BUILTIN_C += rm.c
baab13
 BUILTIN_C += report.c
baab13
 BUILTIN_C += status.c
baab13
+BUILTIN_C += process.c
baab13
 
baab13
 abrt_cli_SOURCES = $(CLI_C) $(BUILTIN_C) builtin-cmd.h abrt-cli-core.h
baab13
 
baab13
diff --git a/src/cli/abrt-cli.c b/src/cli/abrt-cli.c
baab13
index c04c132..bc11c7f 100644
baab13
--- a/src/cli/abrt-cli.c
baab13
+++ b/src/cli/abrt-cli.c
baab13
@@ -150,6 +150,7 @@ int main(int argc, const char **argv)
baab13
         CMD(report, "e",_("Analyze and report problem data in DIR")),
baab13
         CMD(info, "i", _("Print information about DIR")),
baab13
         CMD(status, "st",_("Print the count of the recent crashes")),
baab13
+        CMD(process, "p",_("Process multiple problems")),
baab13
         {NULL, NULL, NULL, NULL}
baab13
     };
baab13
 
baab13
diff --git a/src/cli/builtin-cmd.h b/src/cli/builtin-cmd.h
baab13
index 18588e1..bc80479 100644
baab13
--- a/src/cli/builtin-cmd.h
baab13
+++ b/src/cli/builtin-cmd.h
baab13
@@ -25,5 +25,6 @@ extern int cmd_remove(int argc, const char **argv);
baab13
 extern int cmd_report(int argc, const char **argv);
baab13
 extern int cmd_info(int argc, const char **argv);
baab13
 extern int cmd_status(int argc, const char **argv);
baab13
+extern int cmd_process(int argc, const char **argv);
baab13
 
baab13
 #endif /* _BUILTIN-CMD_H_ */
baab13
diff --git a/src/cli/process.c b/src/cli/process.c
baab13
new file mode 100644
baab13
index 0000000..7f4fff5
baab13
--- /dev/null
baab13
+++ b/src/cli/process.c
baab13
@@ -0,0 +1,169 @@
baab13
+/*
baab13
+    Copyright (C) 2014  ABRT Team
baab13
+    Copyright (C) 2014  RedHat inc.
baab13
+
baab13
+    This program is free software; you can redistribute it and/or modify
baab13
+    it under the terms of the GNU General Public License as published by
baab13
+    the Free Software Foundation; either version 2 of the License, or
baab13
+    (at your option) any later version.
baab13
+
baab13
+    This program is distributed in the hope that it will be useful,
baab13
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
baab13
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
baab13
+    GNU General Public License for more details.
baab13
+
baab13
+    You should have received a copy of the GNU General Public License along
baab13
+    with this program; if not, write to the Free Software Foundation, Inc.,
baab13
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
baab13
+*/
baab13
+
baab13
+#include "libabrt.h"
baab13
+#include "client.h"
baab13
+
baab13
+#include "abrt-cli-core.h"
baab13
+#include "builtin-cmd.h"
baab13
+
baab13
+
baab13
+enum {
baab13
+    ACT_ERR = 0,
baab13
+    ACT_REMOVE,
baab13
+    ACT_REPORT,
baab13
+    ACT_INFO,
baab13
+    ACT_SKIP
baab13
+};
baab13
+
baab13
+static int process_one_crash(problem_data_t *problem_data)
baab13
+{
baab13
+    if (problem_data == NULL)
baab13
+        return ACT_ERR;
baab13
+
baab13
+    static const char *name_to_skip[] = {
baab13
+            FILENAME_PACKAGE   ,
baab13
+            FILENAME_UID       ,
baab13
+            FILENAME_COUNT
baab13
+    };
baab13
+
baab13
+    char *desc = make_description(problem_data,
baab13
+                        /*names_to_skip:*/ (char **)name_to_skip,
baab13
+                        /*max_text_size:*/ CD_TEXT_ATT_SIZE_BZ,
baab13
+                        MAKEDESC_SHOW_ONLY_LIST | MAKEDESC_SHOW_URLS);
baab13
+
baab13
+    fputs(desc, stdout);
baab13
+    free(desc);
baab13
+
baab13
+    const char *dir_name = problem_data_get_content_or_NULL(problem_data,
baab13
+                                                            CD_DUMPDIR);
baab13
+    char *action = NULL;
baab13
+    int ret_val = 0;
baab13
+    while (ret_val == 0)
baab13
+    {
baab13
+        const char *not_reportable = problem_data_get_content_or_NULL(problem_data, FILENAME_NOT_REPORTABLE);
baab13
+
baab13
+        /* if the problem is not-reportable then ask does not contain option report(e) */
baab13
+        if (not_reportable != NULL)
baab13
+            action = ask(_("Actions: remove(rm), info(i), skip(s):"));
baab13
+        else
baab13
+            action = ask(_("Actions: remove(rm), report(e), info(i), skip(s):"));
baab13
+
baab13
+        if(strcmp(action, "rm") == 0 || strcmp(action, "remove") == 0 )
baab13
+        {
baab13
+            log(_("Deleting '%s'"), dir_name);
baab13
+            delete_dump_dir_possibly_using_abrtd(dir_name);
baab13
+
baab13
+            ret_val = ACT_REMOVE;
baab13
+        }
baab13
+        else if (not_reportable == NULL && (strcmp(action, "e") == 0 || strcmp(action, "report") == 0))
baab13
+        {
baab13
+            log(_("Reporting '%s'"), dir_name);
baab13
+            report_problem_in_dir(dir_name,
baab13
+                                     LIBREPORT_WAIT
baab13
+                                   | LIBREPORT_RUN_CLI);
baab13
+
baab13
+            ret_val = ACT_REPORT;
baab13
+        }
baab13
+        else if (strcmp(action, "i") == 0 || strcmp(action, "info") == 0)
baab13
+        {
baab13
+            char *desc = make_description(problem_data,
baab13
+                                    /*names_to_skip:*/ NULL,
baab13
+                                    /*max_text_size:*/ CD_TEXT_ATT_SIZE_BZ,
baab13
+                                    MAKEDESC_SHOW_FILES | MAKEDESC_SHOW_MULTILINE);
baab13
+
baab13
+            fputs(desc, stdout);
baab13
+            free(desc);
baab13
+
baab13
+            ret_val = ACT_INFO;
baab13
+        }
baab13
+        else if (strcmp(action, "s") == 0 || strcmp(action, "skip") == 0)
baab13
+        {
baab13
+            ret_val = ACT_SKIP;
baab13
+        }
baab13
+
baab13
+        free(action);
baab13
+    }
baab13
+
baab13
+    return ret_val;
baab13
+}
baab13
+
baab13
+static void process_crashes(vector_of_problem_data_t *crash_list, long since)
baab13
+{
baab13
+
baab13
+    for (unsigned i = 0; i < crash_list->len; ++i)
baab13
+    {
baab13
+        problem_data_t *crash = get_problem_data(crash_list, i);
baab13
+
baab13
+        if (since != 0)
baab13
+        {
baab13
+            char *s = problem_data_get_content_or_NULL(crash, FILENAME_LAST_OCCURRENCE);
baab13
+            long val = s ? atol(s) : 0;
baab13
+            if (val < since)
baab13
+                continue;
baab13
+        }
baab13
+
baab13
+        /* do not print '\n' before first problem */
baab13
+        if(i != 0)
baab13
+            printf("\n");
baab13
+
baab13
+        int action = process_one_crash(crash);
baab13
+
baab13
+        if (i != crash_list->len - 1)
baab13
+        {
baab13
+            if (action == ACT_REMOVE || action == ACT_REPORT || action == ACT_INFO)
baab13
+            {
baab13
+                /* dummy must be free because the function ask allocate memory */
baab13
+                char *dummy = ask(_("For next problem press ENTER:"));
baab13
+                free(dummy);
baab13
+            }
baab13
+        }
baab13
+    }
baab13
+    return;
baab13
+}
baab13
+
baab13
+int cmd_process(int argc, const char **argv)
baab13
+{
baab13
+    const char *program_usage_string = _(
baab13
+        "Without --since argument, iterates over all detected problems."
baab13
+    );
baab13
+
baab13
+    int opt_since = 0;
baab13
+    struct options program_options[] = {
baab13
+        OPT__VERBOSE(&g_verbose),
baab13
+        OPT_INTEGER('s', "since" , &opt_since,  _("Selects only problems detected after timestamp")),
baab13
+        OPT_END()
baab13
+    };
baab13
+
baab13
+    parse_opts(argc, (char **)argv, program_options, program_usage_string);
baab13
+    argv += optind;
baab13
+
baab13
+    GList *D_list = get_problem_storages();
baab13
+
baab13
+    vector_of_problem_data_t *ci = fetch_crash_infos(D_list);
baab13
+
baab13
+    g_ptr_array_sort_with_data(ci, &cmp_problem_data, (char *) FILENAME_LAST_OCCURRENCE);
baab13
+
baab13
+    process_crashes(ci, opt_since);
baab13
+
baab13
+    free_vector_of_problem_data(ci);
baab13
+    list_free_with_free(D_list);
baab13
+
baab13
+    return 0;
baab13
+}
baab13
-- 
baab13
1.8.3.1
baab13