diff --git a/core/src/tools/CMakeLists.txt b/core/src/tools/CMakeLists.txt index 696b24fa95f..339d7e9586f 100644 --- a/core/src/tools/CMakeLists.txt +++ b/core/src/tools/CMakeLists.txt @@ -1,6 +1,6 @@ # BAREOSĀ® - Backup Archiving REcovery Open Sourced # -# Copyright (C) 2017-2021 Bareos GmbH & Co. KG +# Copyright (C) 2017-2022 Bareos GmbH & Co. KG # # This program is Free Software; you can redistribute it and/or # modify it under the terms of version three of the GNU Affero General Public @@ -36,7 +36,7 @@ if(HAVE_WIN32) list(APPEND BREGEXSRCS ../win32/tools/bregexres.rc) endif() add_executable(bregex ${BREGEXSRCS}) -target_link_libraries(bregex bareos bareosfind) +target_link_libraries(bregex bareos bareosfind CLI11::CLI11) set(BWILDSRCS bwild.cc) if(HAVE_WIN32) diff --git a/core/src/tools/bregex.cc b/core/src/tools/bregex.cc index 7de16e924c4..a58dbf1d0dd 100644 --- a/core/src/tools/bregex.cc +++ b/core/src/tools/bregex.cc @@ -2,7 +2,7 @@ BAREOSĀ® - Backup Archiving REcovery Open Sourced Copyright (C) 2006-2006 Free Software Foundation Europe e.V. - Copyright (C) 2016-2020 Bareos GmbH & Co. KG + Copyright (C) 2016-2022 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -26,6 +26,7 @@ */ #include "include/bareos.h" +#include "lib/cli.h" #ifndef HAVE_REGEX_H # include "lib/bregex.h" @@ -33,81 +34,44 @@ # include #endif - -static void usage() -{ - fprintf(stderr, - "\n" - "Usage: bregex [-d debug_level] -f \n" - " -f specify file of data to be matched\n" - " -l suppress line numbers\n" - " -n print lines that do not match\n" - " -d set debug level to \n" - " -dt print timestamp in debug output\n" - " -? print this message.\n" - "\n\n"); - - exit(1); -} - - int main(int argc, char* const* argv) { - regex_t preg{}; - char prbuf[500]; - char* fname = NULL; - int rc, ch; - char data[1000]; - char pat[500]; - FILE* fd; - bool match_only = true; - int lineno; - bool no_linenos = false; - - setlocale(LC_ALL, ""); tzset(); bindtextdomain("bareos", LOCALEDIR); textdomain("bareos"); - while ((ch = getopt(argc, argv, "d:f:nl?")) != -1) { - switch (ch) { - case 'd': /* set debug level */ - if (*optarg == 't') { - dbg_timestamp = true; - } else { - debug_level = atoi(optarg); - if (debug_level <= 0) { debug_level = 1; } - } - break; + CLI::App bregex_app; + InitCLIApp(bregex_app, "The Bareos Regular Expression tool."); - case 'f': /* data */ - fname = optarg; - break; + AddDebugOptions(bregex_app); - case 'l': - no_linenos = true; - break; + std::string fname{}; + bregex_app + .add_option("-f,--filename", fname, "Specify file or data to be matched.") + ->required(); - case 'n': - match_only = false; - break; + bool no_linenos = false; + bregex_app.add_flag("-l,--suppress-linenumbers", no_linenos, + "Suppress line numbers."); - case '?': - default: - usage(); - } - } - argc -= optind; - argv += optind; + bool match_only = true; + bregex_app.add_flag( + "-n,--not-match", [&match_only](bool val) { match_only = false; }, + "Print line that do not match."); - if (!fname) { - printf("A data file must be specified.\n"); - usage(); - } + CLI11_PARSE(bregex_app, argc, argv); OSDependentInit(); + regex_t preg{}; + char prbuf[500]; + char data[1000]; + char pat[500]; + FILE* fd; + int rc; + int lineno; + for (;;) { printf("Enter regex pattern: "); if (fgets(pat, sizeof(pat) - 1, stdin) == NULL) { break; } @@ -119,9 +83,9 @@ int main(int argc, char* const* argv) printf("Regex compile error: %s\n", prbuf); continue; } - fd = fopen(fname, "r"); + fd = fopen(fname.c_str(), "r"); if (!fd) { - printf(_("Could not open data file: %s\n"), fname); + printf(_("Could not open data file: %s\n"), fname.c_str()); exit(1); } lineno = 0; diff --git a/docs/manuals/source/Appendix/BareosPrograms.rst b/docs/manuals/source/Appendix/BareosPrograms.rst index df4c5d2a463..e5ac8951287 100644 --- a/docs/manuals/source/Appendix/BareosPrograms.rst +++ b/docs/manuals/source/Appendix/BareosPrograms.rst @@ -1263,11 +1263,14 @@ To run it, use: :: - Usage: bregex [-d debug_level] -f - -f specify file of data to be matched - -l suppress line numbers - -n print lines that do not match - -? print this message. + Usage: bregex [OPTIONS] + + Options: + -h,--help Print this help message and exit + -d,--debug-level Set debug level to . + -f,--filename TEXT REQUIRED Specify file or data to be matched. + -l,--suppress-linenumbers Suppress line numbers. + -n,--not-match The is a filename that contains lines of data to be matched (or not) against one or more patterns. When the program is run, it will prompt you for a regular expression pattern, then apply it one line at a time against the data in the file. Each line that matches will be printed preceded by its line number. You will then be prompted again for another pattern.