From 4e34f506ba1edce5fb15a593ab78ad29d7d14759 Mon Sep 17 00:00:00 2001 From: Karel Klic Date: Mon, 21 Jun 2010 17:00:39 +0200 Subject: Split print_crash_infos to two functions, add some comments. --- src/CLI/CLI.cpp | 77 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 33 deletions(-) (limited to 'src/CLI/CLI.cpp') diff --git a/src/CLI/CLI.cpp b/src/CLI/CLI.cpp index cd530e75..12e433f8 100644 --- a/src/CLI/CLI.cpp +++ b/src/CLI/CLI.cpp @@ -1,5 +1,5 @@ /* - Copyright (C) 2009 RedHat inc. + Copyright (C) 2009, 2010 Red Hat, 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 @@ -47,43 +47,54 @@ enum OPT_DELETE }; -static void print_crash_infos(vector_map_crash_data_t& pCrashInfos, int pMode) +/** Prints basic information about a crash to stdout. */ +static void print_crash(const map_crash_data_t &crash) { - unsigned int ii; - for (ii = 0; ii < pCrashInfos.size(); ii++) - { - map_crash_data_t& info = pCrashInfos[ii]; - if (pMode == OPT_GET_LIST_FULL || get_crash_data_item_content(info, CD_REPORTED) != "1") - { - const char *timestr = get_crash_data_item_content(info, FILENAME_TIME).c_str(); - long time = strtol(timestr, NULL, 10); - if (time == 0) - error_msg_and_die("Error while converting time string."); + /* Create a localized string from crash time. */ + const char *timestr = get_crash_data_item_content(crash, FILENAME_TIME).c_str(); + long time = xatou(timestr); + char timeloc[256]; + int success = strftime(timeloc, 128, "%c", localtime(&time)); + if (!success) + error_msg_and_die("Error while converting time to string."); - char timeloc[256]; - int success = strftime(timeloc, 128, "%c", localtime(&time)); - if (!success) - error_msg_and_die("Error while converting time to string."); + printf(_("\tUID : %s\n" + "\tUUID : %s\n" + "\tPackage : %s\n" + "\tExecutable : %s\n" + "\tCrash Time : %s\n" + "\tCrash Count: %s\n"), + get_crash_data_item_content(crash, CD_UID).c_str(), + get_crash_data_item_content(crash, CD_UUID).c_str(), + get_crash_data_item_content(crash, FILENAME_PACKAGE).c_str(), + get_crash_data_item_content(crash, FILENAME_EXECUTABLE).c_str(), + timeloc, + get_crash_data_item_content(crash, CD_COUNT).c_str()); +} - printf(_("%u.\n" - "\tUID : %s\n" - "\tUUID : %s\n" - "\tPackage : %s\n" - "\tExecutable : %s\n" - "\tCrash Time : %s\n" - "\tCrash Count: %s\n"), - ii, - get_crash_data_item_content(info, CD_UID).c_str(), - get_crash_data_item_content(info, CD_UUID).c_str(), - get_crash_data_item_content(info, FILENAME_PACKAGE).c_str(), - get_crash_data_item_content(info, FILENAME_EXECUTABLE).c_str(), - timeloc, - get_crash_data_item_content(info, CD_COUNT).c_str() - ); - } +/** + * Prints a list containing "crashes" to stdout. + * @param include_reported + * Do not skip entries marked as already reported. + */ +static void print_crash_list(const vector_map_crash_data_t& crash_list, bool include_reported) +{ + for (unsigned i = 0; i < crash_list.size(); ++i) + { + const map_crash_data_t& crash = crash_list[i]; + if (get_crash_data_item_content(crash, CD_REPORTED) == "1" && !include_reported) + continue; + + printf("%u.\n", i); + print_crash(crash); } } +/** + * Converts crash reference from user's input to unique crash identification + * in form UID:UUID. + * The returned string must be released by caller. + */ static char *guess_crash_id(const char *str) { vector_map_crash_data_t ci = call_GetCrashInfos(); @@ -233,7 +244,7 @@ int main(int argc, char** argv) case OPT_GET_LIST_FULL: { vector_map_crash_data_t ci = call_GetCrashInfos(); - print_crash_infos(ci, op); + print_crash_list(ci, op == OPT_GET_LIST_FULL); break; } case OPT_REPORT: -- cgit From 8a6e878562d45c88cc4eebcd22d99548cd4470e8 Mon Sep 17 00:00:00 2001 From: Karel Klic Date: Mon, 21 Jun 2010 17:18:00 +0200 Subject: Indentation only --- src/CLI/CLI.cpp | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'src/CLI/CLI.cpp') diff --git a/src/CLI/CLI.cpp b/src/CLI/CLI.cpp index 12e433f8..e889cbd2 100644 --- a/src/CLI/CLI.cpp +++ b/src/CLI/CLI.cpp @@ -197,30 +197,30 @@ int main(int argc, char** argv) int c = getopt_long_only(argc, argv, "?V", longopts, &option_index); switch (c) { - case OPT_REPORT: - case OPT_REPORT_ALWAYS: - case OPT_DELETE: - crash_id = optarg; - /* fall through */ - case OPT_GET_LIST: - case OPT_GET_LIST_FULL: - if (op == -1) - break; - error_msg(_("You must specify exactly one operation.")); - return 1; - case -1: /* end of options */ - if (op != -1) /* if some operation was specified... */ - break; - /* fall through */ - default: - case '?': - case OPT_HELP: - usage(argv[0]); - return 1; - case 'V': - case OPT_VERSION: - printf("%s "VERSION"\n", progname(argv[0])); - return 0; + case OPT_REPORT: + case OPT_REPORT_ALWAYS: + case OPT_DELETE: + crash_id = optarg; + /* fall through */ + case OPT_GET_LIST: + case OPT_GET_LIST_FULL: + if (op == -1) + break; + error_msg(_("You must specify exactly one operation.")); + return 1; + case -1: /* end of options */ + if (op != -1) /* if some operation was specified... */ + break; + /* fall through */ + default: + case '?': + case OPT_HELP: + usage(argv[0]); + return 1; + case 'V': + case OPT_VERSION: + printf("%s "VERSION"\n", progname(argv[0])); + return 0; } if (c == -1) break; -- cgit From bef3c3ad00ed0c6d7bf1191c8c3b2daae5713095 Mon Sep 17 00:00:00 2001 From: Karel Klic Date: Mon, 21 Jun 2010 19:34:33 +0200 Subject: Options overhaul --- src/CLI/CLI.cpp | 150 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 94 insertions(+), 56 deletions(-) (limited to 'src/CLI/CLI.cpp') diff --git a/src/CLI/CLI.cpp b/src/CLI/CLI.cpp index e889cbd2..12183d99 100644 --- a/src/CLI/CLI.cpp +++ b/src/CLI/CLI.cpp @@ -35,18 +35,6 @@ # define _(S) (S) #endif -/* Program options */ -enum -{ - OPT_VERSION, - OPT_HELP, - OPT_GET_LIST, - OPT_GET_LIST_FULL, - OPT_REPORT, - OPT_REPORT_ALWAYS, - OPT_DELETE -}; - /** Prints basic information about a crash to stdout. */ static void print_crash(const map_crash_data_t &crash) { @@ -133,16 +121,32 @@ static char *guess_crash_id(const char *str) return result; } +/* Program options */ +enum +{ + OPT_GET_LIST, + OPT_REPORT, + OPT_DELETE +}; + +/** + * Long options. + * Do not use the has_arg field. Arguments are handled after parsing all options. + * The reason is that we want to use all the following combinations: + * --report ID + * --report ID --always + * --report --always ID + */ static const struct option longopts[] = { /* name, has_arg, flag, val */ - { "help" , no_argument , NULL, OPT_HELP }, - { "version" , no_argument , NULL, OPT_VERSION }, - { "get-list" , no_argument , NULL, OPT_GET_LIST }, - { "get-list-full", no_argument , NULL, OPT_GET_LIST_FULL }, - { "report" , required_argument, NULL, OPT_REPORT }, - { "report-always", required_argument, NULL, OPT_REPORT_ALWAYS }, - { "delete" , required_argument, NULL, OPT_DELETE }, + { "help" , no_argument, NULL, '?' }, + { "version" , no_argument, NULL, 'V' }, + { "get-list" , no_argument, NULL, 'l' }, + { "full" , no_argument, NULL, 'f' }, + { "always" , no_argument, NULL, 'y' }, + { "report" , no_argument, NULL, 'r' }, + { "delete" , no_argument, NULL, 'd' }, { 0, 0, 0, 0 } /* prevents crashes for unknown options*/ }; @@ -155,7 +159,10 @@ static const char *progname(const char *argv0) return argv0; } -/* Prints abrt-cli version and some help text. */ +/** + * Prints abrt-cli version and some help text. + * Then exits the program with return value 1. + */ static void usage(char *argv0) { const char *name = progname(argv0); @@ -167,23 +174,27 @@ static void usage(char *argv0) " -V, --version display the version of %s and exit\n" " -?, --help print this help\n\n" "Actions:\n" - " --get-list print list of crashes which are not reported yet\n" - " --get-list-full print list of all crashes\n" - " --report CRASH_ID create and send a report\n" - " --report-always CRASH_ID create and send a report without asking\n" - " --delete CRASH_ID remove crash\n" + " -l, --get-list print list of crashes which are not reported yet\n" + " -f, --full list all crashes, including already reported ones\n" + " -r, --report CRASH_ID create and send a report\n" + " -y, --always create and send a report without asking\n" + " -d, --delete CRASH_ID remove crash\n" "CRASH_ID can be:\n" " UID:UUID pair,\n" " unique UUID prefix - the crash with matching UUID will be acted upon\n" " @N - N'th crash (as displayed by --get-list-full) will be acted upon\n" ), name, name); + + exit(1); } int main(int argc, char** argv) { const char* crash_id = NULL; int op = -1; + bool full = false; + bool always = false; setlocale(LC_ALL, ""); #if ENABLE_NLS @@ -194,37 +205,64 @@ int main(int argc, char** argv) while (1) { int option_index; - int c = getopt_long_only(argc, argv, "?V", longopts, &option_index); + /* Do not use colons, arguments are handled after parsing all options. */ + int c = getopt_long_only(argc, argv, "?Vrdlfy", + longopts, &option_index); + +#define SET_OP(newop) \ + if (op != -1) \ + { \ + error_msg(_("You must specify exactly one operation.")); \ + return 1; \ + } \ + op = newop; + switch (c) { - case OPT_REPORT: - case OPT_REPORT_ALWAYS: - case OPT_DELETE: - crash_id = optarg; - /* fall through */ - case OPT_GET_LIST: - case OPT_GET_LIST_FULL: - if (op == -1) - break; - error_msg(_("You must specify exactly one operation.")); - return 1; - case -1: /* end of options */ - if (op != -1) /* if some operation was specified... */ - break; - /* fall through */ - default: + case 'r': SET_OP(OPT_REPORT); break; + case 'd': SET_OP(OPT_DELETE); break; + case 'l': SET_OP(OPT_GET_LIST); break; + case 'f': full = true; break; + case 'y': always = true; break; + case -1: /* end of options */ break; + default: /* some error */ case '?': - case OPT_HELP: - usage(argv[0]); - return 1; + usage(argv[0]); /* exits app */ case 'V': - case OPT_VERSION: printf("%s "VERSION"\n", progname(argv[0])); return 0; } +#undef SET_OP if (c == -1) break; - op = c; + } + + /* Handle option arguments. */ + int arg_count = argc - optind; + switch (arg_count) + { + case 0: + if (op == OPT_REPORT || op == OPT_DELETE) + usage(argv[0]); + break; + case 1: + if (op != OPT_REPORT && op != OPT_DELETE) + usage(argv[0]); + crash_id = argv[optind]; + break; + default: + usage(argv[0]); + } + + /* Check if we have an operation. + * Limit --full and --always to certain operations. + */ + if ((full && op != OPT_GET_LIST) || + (always && op != OPT_REPORT) || + op == -1) + { + usage(argv[0]); + return 1; } #ifdef ENABLE_DBUS @@ -237,27 +275,31 @@ int main(int argc, char** argv) ABRTDaemon.Connect(VAR_RUN"/abrt.socket"); #endif + /* Do the selected operation. */ int exitcode = 0; switch (op) { case OPT_GET_LIST: - case OPT_GET_LIST_FULL: { vector_map_crash_data_t ci = call_GetCrashInfos(); - print_crash_list(ci, op == OPT_GET_LIST_FULL); + print_crash_list(ci, full); break; } case OPT_REPORT: - case OPT_REPORT_ALWAYS: - exitcode = report(crash_id, (op == OPT_REPORT_ALWAYS)*CLI_REPORT_BATCH + CLI_REPORT_SILENT_IF_NOT_FOUND); + { + int flags = CLI_REPORT_SILENT_IF_NOT_FOUND; + if (always) + flags |= CLI_REPORT_BATCH; + exitcode = report(crash_id, flags); if (exitcode == -1) /* no such crash_id */ { crash_id = guess_crash_id(crash_id); - exitcode = report(crash_id, (op == OPT_REPORT_ALWAYS) * CLI_REPORT_BATCH); + exitcode = report(crash_id, always ? CLI_REPORT_BATCH : 0); if (exitcode == -1) error_msg_and_die("Crash '%s' not found", crash_id); } break; + } case OPT_DELETE: { exitcode = call_DeleteDebugDump(crash_id); @@ -266,14 +308,10 @@ int main(int argc, char** argv) crash_id = guess_crash_id(crash_id); exitcode = call_DeleteDebugDump(crash_id); if (exitcode == ENOENT) - { error_msg_and_die("Crash '%s' not found", crash_id); - } } if (exitcode != 0) - { error_msg_and_die("Can't delete debug dump '%s'", crash_id); - } break; } } -- cgit From ac304dcddd13196c8d284d363e022005edef9cb2 Mon Sep 17 00:00:00 2001 From: Karel Klic Date: Tue, 22 Jun 2010 14:11:44 +0200 Subject: Rename --get-list to --list --- src/CLI/CLI.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/CLI/CLI.cpp') diff --git a/src/CLI/CLI.cpp b/src/CLI/CLI.cpp index 12183d99..323ba006 100644 --- a/src/CLI/CLI.cpp +++ b/src/CLI/CLI.cpp @@ -140,13 +140,13 @@ enum static const struct option longopts[] = { /* name, has_arg, flag, val */ - { "help" , no_argument, NULL, '?' }, - { "version" , no_argument, NULL, 'V' }, - { "get-list" , no_argument, NULL, 'l' }, - { "full" , no_argument, NULL, 'f' }, - { "always" , no_argument, NULL, 'y' }, - { "report" , no_argument, NULL, 'r' }, - { "delete" , no_argument, NULL, 'd' }, + { "help" , no_argument, NULL, '?' }, + { "version", no_argument, NULL, 'V' }, + { "list" , no_argument, NULL, 'l' }, + { "full" , no_argument, NULL, 'f' }, + { "always" , no_argument, NULL, 'y' }, + { "report" , no_argument, NULL, 'r' }, + { "delete" , no_argument, NULL, 'd' }, { 0, 0, 0, 0 } /* prevents crashes for unknown options*/ }; @@ -174,7 +174,7 @@ static void usage(char *argv0) " -V, --version display the version of %s and exit\n" " -?, --help print this help\n\n" "Actions:\n" - " -l, --get-list print list of crashes which are not reported yet\n" + " -l, --list print list of crashes which are not reported yet\n" " -f, --full list all crashes, including already reported ones\n" " -r, --report CRASH_ID create and send a report\n" " -y, --always create and send a report without asking\n" @@ -182,7 +182,7 @@ static void usage(char *argv0) "CRASH_ID can be:\n" " UID:UUID pair,\n" " unique UUID prefix - the crash with matching UUID will be acted upon\n" - " @N - N'th crash (as displayed by --get-list-full) will be acted upon\n" + " @N - N'th crash (as displayed by --list --full) will be acted upon\n" ), name, name); -- cgit From 06241a7f480159f0c999aaaf83e5eef92860b858 Mon Sep 17 00:00:00 2001 From: Karel Klic Date: Tue, 22 Jun 2010 14:14:38 +0200 Subject: Remove dead socket code. --- src/CLI/CLI.cpp | 9 --------- 1 file changed, 9 deletions(-) (limited to 'src/CLI/CLI.cpp') diff --git a/src/CLI/CLI.cpp b/src/CLI/CLI.cpp index 323ba006..b082115c 100644 --- a/src/CLI/CLI.cpp +++ b/src/CLI/CLI.cpp @@ -265,15 +265,10 @@ int main(int argc, char** argv) return 1; } -#ifdef ENABLE_DBUS DBusError err; dbus_error_init(&err); s_dbus_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err); handle_dbus_err(s_dbus_conn == NULL, &err); -#elif ENABLE_SOCKET - CABRTSocket ABRTDaemon; - ABRTDaemon.Connect(VAR_RUN"/abrt.socket"); -#endif /* Do the selected operation. */ int exitcode = 0; @@ -316,9 +311,5 @@ int main(int argc, char** argv) } } -#if ENABLE_SOCKET - ABRTDaemon.Disconnect(); -#endif - return exitcode; } -- cgit From 213dedf33bca5b2b51457999ac79cc64140af5c8 Mon Sep 17 00:00:00 2001 From: Karel Klic Date: Tue, 29 Jun 2010 16:52:32 +0200 Subject: Allow using the same operation multiple times --- src/CLI/CLI.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/CLI/CLI.cpp') diff --git a/src/CLI/CLI.cpp b/src/CLI/CLI.cpp index b082115c..d882161a 100644 --- a/src/CLI/CLI.cpp +++ b/src/CLI/CLI.cpp @@ -210,7 +210,7 @@ int main(int argc, char** argv) longopts, &option_index); #define SET_OP(newop) \ - if (op != -1) \ + if (op != -1 && op != newop) \ { \ error_msg(_("You must specify exactly one operation.")); \ return 1; \ -- cgit