summaryrefslogtreecommitdiffstats
path: root/src/CLI/CLI.cpp
diff options
context:
space:
mode:
authorKarel Klic <kklic@redhat.com>2009-10-20 15:54:05 +0200
committerKarel Klic <kklic@redhat.com>2009-10-20 15:54:05 +0200
commit9e8a1ba0c9ddfa0108050f6fedc88de63b4b7c3f (patch)
tree93e418dedf9864edbdfcfbe69ba5b444d9704812 /src/CLI/CLI.cpp
parentb7f40a3f601e60fc342cad87cf40849ee4e2c153 (diff)
downloadabrt-9e8a1ba0c9ddfa0108050f6fedc88de63b4b7c3f.tar.gz
abrt-9e8a1ba0c9ddfa0108050f6fedc88de63b4b7c3f.tar.xz
abrt-9e8a1ba0c9ddfa0108050f6fedc88de63b4b7c3f.zip
Initial implementation of crash report editor. Splitted CLI.cpp into multiple files for clarity.
Diffstat (limited to 'src/CLI/CLI.cpp')
-rw-r--r--src/CLI/CLI.cpp226
1 files changed, 7 insertions, 219 deletions
diff --git a/src/CLI/CLI.cpp b/src/CLI/CLI.cpp
index d4e2bb4b..a782a59a 100644
--- a/src/CLI/CLI.cpp
+++ b/src/CLI/CLI.cpp
@@ -21,17 +21,19 @@
#include "abrtlib.h"
#include "abrt_dbus.h"
#include "DBusCommon.h"
+#include "report.h"
+#include "dbus.h"
#if HAVE_CONFIG_H
- #include <config.h>
+#include <config.h>
#endif
#if HAVE_LOCALE_H
- #include <locale.h>
+#include <locale.h>
#endif
#if ENABLE_NLS
- #include <libintl.h>
- #define _(S) gettext(S)
+#include <libintl.h>
+#define _(S) gettext(S)
#else
- #define _(S) (S)
+#define _(S) (S)
#endif
/* Program options */
@@ -46,8 +48,6 @@ enum
OPT_DELETE
};
-static DBusConnection* s_dbus_conn;
-
static void print_crash_infos(vector_crash_infos_t& pCrashInfos, int pMode)
{
unsigned int ii;
@@ -85,218 +85,6 @@ static void print_crash_infos(vector_crash_infos_t& pCrashInfos, int pMode)
}
}
-/* Saves the crash report to a file.
- * Fp must be opened before write_crash_report is called.
- * Returned Value:
- * If the report is successfully stored to the file, a zero value is returned.
- * On failure, nonzero value is returned.
- */
-static int write_crash_report(const map_crash_report_t& report, FILE *fp)
-{
- for (map_crash_report_t::const_iterator it = report.begin(); it != report.end(); it++)
- {
- if (it->second[CD_TYPE] == CD_SYS)
- continue;
-
- fprintf(fp, "\n%s\n-----\n%s\n", it->first.c_str(), it->second[CD_CONTENT].c_str());
- }
-
- return 0;
-}
-
-static void print_crash_report(const map_crash_report_t& pCrashReport)
-{
- map_crash_report_t::const_iterator it = pCrashReport.begin();
- for (; it != pCrashReport.end(); it++)
- {
- if (it->second[CD_TYPE] != CD_SYS)
- {
- printf("\n%s\n"
- "-----\n"
- "%s\n", it->first.c_str(), it->second[CD_CONTENT].c_str());
- }
- }
-}
-
-/*
- * DBus member calls
- */
-
-/* helpers */
-static DBusMessage* new_call_msg(const char* method)
-{
- DBusMessage* msg = dbus_message_new_method_call(CC_DBUS_NAME, CC_DBUS_PATH, CC_DBUS_IFACE, method);
- if (!msg)
- die_out_of_memory();
- return msg;
-}
-
-static DBusMessage* send_get_reply_and_unref(DBusMessage* msg)
-{
- DBusError err;
- dbus_error_init(&err);
- DBusMessage *reply = dbus_connection_send_with_reply_and_block(s_dbus_conn, msg, /*timeout*/ -1, &err);
- if (reply == NULL)
- {
- //TODO: analyse err
- error_msg_and_die("Error sending DBus message");
- }
- dbus_message_unref(msg);
- return reply;
-}
-
-static vector_crash_infos_t call_GetCrashInfos()
-{
- DBusMessage* msg = new_call_msg("GetCrashInfos");
- DBusMessage *reply = send_get_reply_and_unref(msg);
-
- vector_crash_infos_t argout;
- DBusMessageIter in_iter;
- dbus_message_iter_init(reply, &in_iter);
- int r = load_val(&in_iter, argout);
- if (r != ABRT_DBUS_LAST_FIELD) /* more values present, or bad type */
- error_msg_and_die("dbus call %s: return type mismatch", "GetCrashInfos");
- dbus_message_unref(reply);
- return argout;
-}
-
-static map_crash_report_t call_CreateReport(const char* uuid)
-{
- /* Yes, call name is not "CreateReport" but "GetJobResult".
- * We need to clean up the names one day. */
- DBusMessage* msg = new_call_msg("GetJobResult");
- dbus_message_append_args(msg,
- DBUS_TYPE_STRING, &uuid,
- DBUS_TYPE_INVALID);
-
- DBusMessage *reply = send_get_reply_and_unref(msg);
-
- map_crash_report_t argout;
- DBusMessageIter in_iter;
- dbus_message_iter_init(reply, &in_iter);
- int r = load_val(&in_iter, argout);
- if (r != ABRT_DBUS_LAST_FIELD) /* more values present, or bad type */
- error_msg_and_die("dbus call %s: return type mismatch", "GetJobResult");
- dbus_message_unref(reply);
- return argout;
-}
-
-static void call_Report(const map_crash_report_t& report)
-{
- DBusMessage* msg = new_call_msg("Report");
- DBusMessageIter out_iter;
- dbus_message_iter_init_append(msg, &out_iter);
- store_val(&out_iter, report);
-
- DBusMessage *reply = send_get_reply_and_unref(msg);
- //it returns a single value of report_status_t type,
- //but we don't use it (yet?)
-
- dbus_message_unref(reply);
- return;
-}
-
-static void call_DeleteDebugDump(const char* uuid)
-{
- DBusMessage* msg = new_call_msg("DeleteDebugDump");
- dbus_message_append_args(msg,
- DBUS_TYPE_STRING, &uuid,
- DBUS_TYPE_INVALID);
-
- DBusMessage *reply = send_get_reply_and_unref(msg);
- //it returns a single boolean value,
- //but we don't use it (yet?)
-
- dbus_message_unref(reply);
- return;
-}
-
-static void handle_dbus_err(bool error_flag, DBusError *err)
-{
- if (dbus_error_is_set(err))
- {
- error_msg("dbus error: %s", err->message);
- /* dbus_error_free(&err); */
- error_flag = true;
- }
- if (!error_flag)
- return;
- error_msg_and_die(
- "error requesting DBus name %s, possible reasons: "
- "abrt run by non-root; dbus config is incorrect",
- CC_DBUS_NAME);
-}
-
-int launch_editor(const char *path, const char *report, char **output)
-{
- const char *editor, *terminal;
-
- editor = getenv("VISUAL");
- if (!editor)
- editor = getenv("EDITOR");
-
- terminal = getenv("TERM");
- if (!editor && (!terminal || !strcmp(terminal, "dumb")))
- {
- error_msg(_("Terminal is dumb but no VISUAL nor EDITOR defined."));
- return 1;
- }
-
- if (!editor)
- editor = "vi";
-
- return 0;
-}
-
-/* Reports the crash with corresponding uuid over DBus. */
-int report(const char *uuid, bool always)
-{
- map_crash_report_t cr = call_CreateReport(uuid);
-
- if (always)
- {
- call_Report(cr);
- return 0;
- }
-
- print_crash_report(cr);
-
- /* Open a temporary file and write the crash report to it. */
- char filename[] = "/tmp/abrt-report.XXXXXX";
- int fd = mkstemp(filename);
- if (fd == -1)
- {
- error_msg("could not generate temporary file name");
- return 1;
- }
-
- FILE *fp = fdopen(fd, "w");
- if (!fp)
- {
- error_msg("could not open '%s' to save the crash report", filename);
- return 1;
- }
-
- write_crash_report(cr, fp);
-
- if (fclose(fp))
- {
- error_msg("could not close '%s'", filename);
- return 2;
- }
-
- printf(_("\nDo you want to send the report? [y/n]: "));
- fflush(NULL);
- char answer[16] = "n";
- fgets(answer, sizeof(answer), stdin);
- if (answer[0] == 'Y' || answer[0] == 'y')
- {
- call_Report(cr);
- }
-
- return 0;
-}
-
static const struct option longopts[] =
{
/* name, has_arg, flag, val */