From 46fac7e2c0eaf98668698558ec4acbc2ade76ba7 Mon Sep 17 00:00:00 2001
From: Matej Habrnal <mhabrnal@redhat.com>
Date: Sat, 20 Sep 2014 22:50:11 +0200
Subject: [ABRT PATCH 65/66] cli: batch reporting in abrt-cli

Added option process (p) to the abrt-cli.
With option process abrt-cli goes through all problems one by one (when
parameter --since is not specified) and asks the user what action will be
executed.

Resolves #1066482

Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
 doc/abrt-cli.txt      |   2 +
 po/POTFILES.in        |   1 +
 src/cli/Makefile.am   |   1 +
 src/cli/abrt-cli.c    |   1 +
 src/cli/builtin-cmd.h |   1 +
 src/cli/process.c     | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 175 insertions(+)
 create mode 100644 src/cli/process.c

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

