Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
bpluginfo: replacing custom CLI parsing with CLI11
  • Loading branch information
alaaeddineelamri committed Jul 11, 2022
1 parent 581eacb commit 2a253b8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 49 deletions.
2 changes: 1 addition & 1 deletion core/src/tools/CMakeLists.txt
Expand Up @@ -53,7 +53,7 @@ if(NOT HAVE_WIN32)
target_link_libraries(btestls bareosfind bareos)

add_executable(bpluginfo bpluginfo.cc)
target_link_libraries(bpluginfo bareos ${DL_LIBRARIES})
target_link_libraries(bpluginfo bareos ${DL_LIBRARIES} CLI11::CLI11)
endif()

set(TOOLS_BIN bsmtp bwild bregex)
Expand Down
77 changes: 29 additions & 48 deletions core/src/tools/bpluginfo.cc
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2006-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2020 Bareos GmbH & Co. KG
Copyright (C) 2013-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
Expand Down Expand Up @@ -32,9 +32,11 @@
* - API version
* - Enabled functions, etc.
*/

#include "include/bareos.h"
#include "filed/fd_plugins.h"
#include "dird/dir_plugins.h"
#include "lib/cli.h"
#include "stored/sd_plugins.h"
#include "assert_macro.h"
#ifndef __WIN32__
Expand Down Expand Up @@ -173,21 +175,6 @@ void bareosFree(void* ctx, const char* file, int line, void* mem)
FREE(mem);
};

// displays a short help
void PrintHelp(int argc, char* argv[])
{
printf(
"\n"
"Usage: bpluginfo [options] <plugin_file.so>\n"
" -v verbose\n"
" -i list plugin header information only (default)\n"
" -f list plugin functions information only\n"
" -a <api> bareos api version (default %d)\n"
" -h help screen\n"
"\n",
DEFAULT_API_VERSION);
}

/* allocates and resets a main program data variable */
progdata* allocpdata(void)
{
Expand Down Expand Up @@ -225,63 +212,57 @@ void Freepdata(progdata* pdata)
*/
void ParseArgs(progdata* pdata, int argc, char* argv[])
{
int ch;
char* dirtmp;
char* progdir;
CLI::App bpluginfo_app;
InitCLIApp(bpluginfo_app, "The Bareos Plugin Info tool.");

while ((ch = getopt(argc, argv, "a:fiv")) != -1) {
switch (ch) {
case 'a':
pdata->bapiversion = atoi(optarg);
break;
bpluginfo_app
.add_option("-a,--api", pdata->bapiversion,
"bareos api version (default :"
+ std::to_string(DEFAULT_API_VERSION) + ").")
->check(CLI::NonNegativeNumber)
->type_name("<api>");

case 'f':
pdata->listfunc = true;
break;
bpluginfo_app.add_flag("-f,--list-functions", pdata->listfunc,
"list plugin functions information only.");

case 'i':
pdata->listinfo = true;
break;
bpluginfo_app.add_flag("-i,--list-headers", pdata->listinfo,
"list plugin header information only.");

case 'v':
pdata->verbose = true;
break;
bpluginfo_app.add_flag("-v,--verbose", pdata->verbose, "Verbose.");

case '?':
default:
PrintHelp(argc, argv);
exit(0);
}
}
argc -= optind;
argv += optind;
std::string plugin_file{};
bpluginfo_app.add_option("plugin_file.so", plugin_file, "Plugin file.")
->required();

if (argc < 1) {
PrintHelp(argc, argv);
exit(0);
try {
(bpluginfo_app).parse((argc), (argv));
} catch (const CLI::ParseError& e) {
exit((bpluginfo_app).exit(e));
}

char* dirtmp;
char* progdir;
if (!pdata->pluginfile) {
if (argv[0][0] != '/') {
if (plugin_file[0] != '/') {
dirtmp = MALLOC(PATH_MAX);
ASSERT_MEMORY(dirtmp);
progdir = MALLOC(PATH_MAX);
ASSERT_MEMORY(progdir);
dirtmp = getcwd(dirtmp, PATH_MAX);

strcat(dirtmp, "/");
strcat(dirtmp, argv[0]);
strcat(dirtmp, plugin_file.c_str());

if (realpath(dirtmp, progdir) == NULL) {
// Error in resolving path
FREE(progdir);
progdir = strdup(argv[0]);
progdir = strdup(plugin_file.c_str());
}
pdata->pluginfile = strdup(progdir);
FREE(dirtmp);
FREE(progdir);
} else {
pdata->pluginfile = strdup(argv[0]);
pdata->pluginfile = strdup(plugin_file.c_str());
}
}
}
Expand Down

0 comments on commit 2a253b8

Please sign in to comment.