Skip to content

Commit f0e7239

Browse files
committed
Make verbose mode a global common option
This adds --verbose and --verbose-log-file to the common oscap options. That means these options can be used in any module.
1 parent dfefbec commit f0e7239

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

utils/oscap-tool.c

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ bool oscap_module_usage(struct oscap_module *module, FILE *out, const char *err,
182182
return (out != stderr);
183183
}
184184

185+
static const char *common_opts_help =
186+
"Common options:\n"
187+
" --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
188+
" --verbose-log-file <file> - Write verbose informations into file.\n";
189+
185190
static void oscap_module_print_help(struct oscap_module *module, FILE *out)
186191
{
187192
assert(module != NULL);
@@ -196,7 +201,9 @@ static void oscap_module_print_help(struct oscap_module *module, FILE *out)
196201
oscap_module_print_usage(module, out, true);
197202
fprintf(out, "\n\n");
198203

199-
if (module->help) fprintf(out, "%s\n\n", module->help);
204+
fprintf(out, "%s\n", common_opts_help);
205+
if (module->help)
206+
fprintf(out, "%s\n\n", module->help);
200207

201208
if (module->submodules) {
202209
fprintf(out, "Commands:\n");
@@ -231,28 +238,48 @@ enum oscap_common_opts {
231238
OPT_LISTMODS,
232239
OPT_LISTALLMODS,
233240
OPT_MODTREE,
234-
OPT_HELP = 'h'
241+
OPT_HELP = 'h',
242+
OPT_VERBOSE,
243+
OPT_VERBOSE_LOG_FILE
235244
};
236245

237-
static enum oscap_common_opts oscap_parse_common_opts(int argc, char **argv)
246+
static enum oscap_common_opts oscap_parse_common_opts(int argc, char **argv, struct oscap_action *action)
238247
{
239248
static const struct option opts[] = {
240249
{ "help", 0, 0, OPT_HELP },
241250
{ "list-submodules", 0, 0, OPT_LISTMODS },
242251
{ "list-all-submodules", 0, 0, OPT_LISTALLMODS },
243252
{ "module-tree", 0, 0, OPT_MODTREE },
253+
{ "verbose", required_argument, NULL, OPT_VERBOSE },
254+
{ "verbose-log-file", required_argument, NULL, OPT_VERBOSE_LOG_FILE },
244255
{ 0, 0, 0, 0 }
245256
};
246257

247-
int optind_bak = optind;
248-
int opterr_bak = opterr;
249-
opterr = 0;
250-
int r = getopt_long(argc, argv, "+h", opts, NULL);
251-
opterr = opterr_bak;
252-
switch (r) {
253-
case -1: case '?': optind = optind_bak; return OPT_NONE;
254-
default: return r;
255-
}
258+
int r;
259+
int optind_bak = optind;
260+
int opterr_bak = opterr;
261+
opterr = 0;
262+
while ((r = getopt_long(argc, argv, "+h", opts, NULL)) != -1) {
263+
switch (r) {
264+
case OPT_VERBOSE:
265+
optind_bak += 2;
266+
action->verbosity_level = optarg;
267+
break;
268+
case OPT_VERBOSE_LOG_FILE:
269+
optind_bak += 2;
270+
action->f_verbose_log = optarg;
271+
break;
272+
case 0:
273+
break;
274+
case '?':
275+
optind = optind_bak;
276+
opterr = opterr_bak;
277+
return OPT_NONE;
278+
default:
279+
return r;
280+
}
281+
}
282+
return OPT_NONE;
256283
}
257284

258285
int oscap_module_call(struct oscap_action *action)
@@ -328,7 +355,7 @@ int oscap_module_process(struct oscap_module *module, int argc, char **argv)
328355

329356
getopt_parse_env(module, &argc, &argv);
330357

331-
switch (oscap_parse_common_opts(argc, argv)) {
358+
switch (oscap_parse_common_opts(argc, argv, &action)) {
332359
case OPT_HELP: oscap_module_print_help(module, stdout); goto cleanup;
333360
case OPT_LISTMODS: oscap_print_submodules(module, stdout, "\n", false); goto cleanup;
334361
case OPT_LISTALLMODS: oscap_print_submodules(module, stdout, "\n", true); goto cleanup;
@@ -345,6 +372,12 @@ int oscap_module_process(struct oscap_module *module, int argc, char **argv)
345372
}
346373

347374
if (module->func) {
375+
if (!check_verbose_options(&action)) {
376+
goto cleanup;
377+
}
378+
if (!oscap_set_verbose(action.verbosity_level, action.f_verbose_log, false)) {
379+
goto cleanup;
380+
}
348381
ret = oscap_module_call(&action);
349382
goto cleanup;
350383
}

0 commit comments

Comments
 (0)