Skip to content

Commit fe9b0ff

Browse files
authored
Merge pull request #1143 from jan-cerny/make_verbose
Make verbose mode a common option for all modules
2 parents 1df38bd + bb303dc commit fe9b0ff

File tree

9 files changed

+77
-168
lines changed

9 files changed

+77
-168
lines changed

src/common/debug.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ oscap_verbosity_levels oscap_verbosity_level_from_cstr(const char *level_name)
129129
return oscap_string_to_enum(OSCAP_VERBOSITY_LEVELS, level_name);
130130
}
131131

132-
bool oscap_set_verbose(const char *verbosity_level, const char *filename, bool is_probe)
132+
bool oscap_set_verbose(const char *verbosity_level, const char *filename)
133133
{
134134
if (verbosity_level == NULL) {
135135
verbosity_level = "WARNING";

src/common/public/oscap_debug.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ OSCAP_API void __oscap_dlprintf(int level, const char *file, const char *fn, siz
5151
* @param verbosity_level Verbosity level
5252
* @param filename Name of file used as output file for store debugging
5353
* and other additional information.
54-
* @param is_probe Determines whether the function is called
55-
* from a probe (true) or from the base library (false).
5654
* @return When an error occured, returns false, otherwise true.
5755
*/
58-
OSCAP_API bool oscap_set_verbose(const char *verbosity_level, const char *filename, bool is_probe);
56+
OSCAP_API bool oscap_set_verbose(const char *verbosity_level, const char *filename);
5957

6058
/**
6159
* Parse verbosity level from a string.

utils/oscap-ds.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ static struct oscap_module DS_RDS_VALIDATE_MODULE = {
147147
.parent = &OSCAP_DS_MODULE,
148148
.summary = "Validate given ResultDataStream",
149149
.usage = "[options] result_datastream.xml",
150-
.help = "Options:\n"
151-
" --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
152-
" --verbose-log-file <file> - Write verbose informations into file.\n",
150+
.help = NULL,
153151
.opt_parser = getopt_ds,
154152
.func = app_ds_rds_validate
155153
};
@@ -168,9 +166,7 @@ static struct oscap_module* DS_SUBMODULES[DS_SUBMODULES_NUM] = {
168166
enum ds_opt {
169167
DS_OPT_DATASTREAM_ID = 1,
170168
DS_OPT_XCCDF_ID,
171-
DS_OPT_REPORT_ID,
172-
DS_OPT_VERBOSE,
173-
DS_OPT_VERBOSE_LOG_FILE,
169+
DS_OPT_REPORT_ID
174170
};
175171

176172
bool getopt_ds(int argc, char **argv, struct oscap_action *action) {
@@ -184,8 +180,6 @@ bool getopt_ds(int argc, char **argv, struct oscap_action *action) {
184180
{"xccdf-id", required_argument, NULL, DS_OPT_XCCDF_ID},
185181
{"report-id", required_argument, NULL, DS_OPT_REPORT_ID},
186182
{"fetch-remote-resources", no_argument, &action->remote_resources, 1},
187-
{"verbose", required_argument, NULL, DS_OPT_VERBOSE },
188-
{"verbose-log-file", required_argument, NULL, DS_OPT_VERBOSE_LOG_FILE },
189183
// end
190184
{0, 0, 0, 0}
191185
};
@@ -197,8 +191,6 @@ bool getopt_ds(int argc, char **argv, struct oscap_action *action) {
197191
case DS_OPT_DATASTREAM_ID: action->f_datastream_id = optarg; break;
198192
case DS_OPT_XCCDF_ID: action->f_xccdf_id = optarg; break;
199193
case DS_OPT_REPORT_ID: action->f_report_id = optarg; break;
200-
case DS_OPT_VERBOSE: action->verbosity_level = optarg; break;
201-
case DS_OPT_VERBOSE_LOG_FILE: action->f_verbose_log = optarg; break;
202194
case 0: break;
203195
default: return oscap_module_usage(action->module, stderr, NULL);
204196
}
@@ -541,10 +533,6 @@ int app_ds_rds_create(const struct oscap_action *action) {
541533
int app_ds_rds_validate(const struct oscap_action *action) {
542534
int ret = OSCAP_ERROR;
543535

544-
if (!oscap_set_verbose(action->verbosity_level, action->f_verbose_log, false)) {
545-
return OSCAP_ERROR;
546-
}
547-
548536
struct oscap_source *rds = oscap_source_new_from_file(action->ds_action->file);
549537
if (oscap_source_validate(rds, reporter, (void *) action) != 0) {
550538
oscap_source_free(rds);

utils/oscap-info.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,11 @@ static int app_info(const struct oscap_action *action);
5959
struct oscap_module OSCAP_INFO_MODULE = {
6060
.name = "info",
6161
.parent = &OSCAP_ROOT_MODULE,
62-
.summary = "info module",
62+
.summary = "Print information about a SCAP file.",
6363
.usage = "some-file.xml",
64-
.help = "Print information about a file\n"
65-
"\n"
66-
"Options:\n"
64+
.help = "Options:\n"
6765
" --fetch-remote-resources - Download remote content referenced by DataStream.\n"
68-
" --profile <id> - Show info of the profile with the given ID..\n"
66+
" --profile <id> - Show info of the profile with the given ID.\n"
6967
" --profiles - Show profiles from the input file in the <id>:<title> format, one line per profile.\n",
7068
.opt_parser = getopt_info,
7169
.func = app_info

utils/oscap-oval.c

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ static struct oscap_module OVAL_EVAL = {
112112
" --datastream-id <id> - ID of the datastream in the collection to use.\n"
113113
" (only applicable for source datastreams)\n"
114114
" --oval-id <id> - ID of the OVAL component ref in the datastream to use.\n"
115-
" (only applicable for source datastreams)\n"
116-
" --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
117-
" --verbose-log-file <file> - Write verbose information into file.\n",
115+
" (only applicable for source datastreams)\n",
118116
.opt_parser = getopt_oval_eval,
119117
.func = app_evaluate_oval
120118
};
@@ -129,9 +127,7 @@ static struct oscap_module OVAL_COLLECT = {
129127
" --id <object> - Collect system characteristics ONLY for specified OVAL Object.\n"
130128
" --syschar <file> - Write OVAL System Characteristic into file.\n"
131129
" --variables <file> - Provide external variables expected by OVAL Definitions.\n"
132-
" --skip-valid - Skip validation.\n"
133-
" --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
134-
" --verbose-log-file <file> - Write verbose information into file.\n",
130+
" --skip-valid - Skip validation.\n",
135131
.opt_parser = getopt_oval_collect,
136132
.func = app_collect_oval
137133
};
@@ -146,9 +142,7 @@ static struct oscap_module OVAL_ANALYSE = {
146142
"Options:\n"
147143
" --variables <file> - Provide external variables expected by OVAL Definitions.\n"
148144
" --directives <file> - Use OVAL Directives content to specify desired results content.\n"
149-
" --skip-valid - Skip validation.\n"
150-
" --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
151-
" --verbose-log-file <file> - Write verbose information into file.\n",
145+
" --skip-valid - Skip validation.\n",
152146
.opt_parser = getopt_oval_analyse,
153147
.func = app_analyse_oval
154148
};
@@ -210,11 +204,6 @@ int app_collect_oval(const struct oscap_action *action)
210204
struct oval_generator *generator = NULL;
211205
int ret = OSCAP_ERROR;
212206

213-
/* Turn on verbosity */
214-
if (!oscap_set_verbose(action->verbosity_level, action->f_verbose_log, false)) {
215-
goto cleanup;
216-
}
217-
218207
/* validate inputs */
219208
if (action->validate) {
220209
if (!valid_inputs(action)) {
@@ -331,11 +320,6 @@ int app_evaluate_oval(const struct oscap_action *action)
331320
oval_result_t eval_result;
332321
int ret = OSCAP_ERROR;
333322

334-
/* Turn on verbosity */
335-
if (!oscap_set_verbose(action->verbosity_level, action->f_verbose_log, false)) {
336-
goto cleanup;
337-
}
338-
339323
/* create a new OVAL session */
340324
if ((session = oval_session_new(action->f_oval)) == NULL) {
341325
oscap_print_error();
@@ -399,11 +383,6 @@ static int app_analyse_oval(const struct oscap_action *action) {
399383
struct oval_generator *generator = NULL;
400384
int ret = OSCAP_ERROR;
401385

402-
/* Turn on verbosity */
403-
if (!oscap_set_verbose(action->verbosity_level, action->f_verbose_log, false)) {
404-
goto cleanup;
405-
}
406-
407386
/* validate inputs */
408387
if (action->validate) {
409388
if (!valid_inputs(action)) {
@@ -515,9 +494,7 @@ enum oval_opt {
515494
OVAL_OPT_DIRECTIVES,
516495
OVAL_OPT_DATASTREAM_ID,
517496
OVAL_OPT_OVAL_ID,
518-
OVAL_OPT_OUTPUT = 'o',
519-
OVAL_OPT_VERBOSE,
520-
OVAL_OPT_VERBOSE_LOG_FILE
497+
OVAL_OPT_OUTPUT = 'o'
521498
};
522499

523500
#if defined(OVAL_PROBES_ENABLED)
@@ -536,8 +513,6 @@ bool getopt_oval_eval(int argc, char **argv, struct oscap_action *action)
536513
{ "datastream-id",required_argument, NULL, OVAL_OPT_DATASTREAM_ID},
537514
{ "oval-id", required_argument, NULL, OVAL_OPT_OVAL_ID},
538515
{ "skip-valid", no_argument, &action->validate, 0 },
539-
{ "verbose", required_argument, NULL, OVAL_OPT_VERBOSE },
540-
{ "verbose-log-file", required_argument, NULL, OVAL_OPT_VERBOSE_LOG_FILE },
541516
{ "fetch-remote-resources", no_argument, &action->remote_resources, 1},
542517
{ 0, 0, 0, 0 }
543518
};
@@ -552,19 +527,10 @@ bool getopt_oval_eval(int argc, char **argv, struct oscap_action *action)
552527
case OVAL_OPT_DIRECTIVES: action->f_directives = optarg; break;
553528
case OVAL_OPT_DATASTREAM_ID: action->f_datastream_id = optarg; break;
554529
case OVAL_OPT_OVAL_ID: action->f_oval_id = optarg; break;
555-
case OVAL_OPT_VERBOSE:
556-
action->verbosity_level = optarg;
557-
break;
558-
case OVAL_OPT_VERBOSE_LOG_FILE:
559-
action->f_verbose_log = optarg;
560-
break;
561530
case 0: break;
562531
default: return oscap_module_usage(action->module, stderr, NULL);
563532
}
564533
}
565-
if (!check_verbose_options(action)) {
566-
return false;
567-
}
568534

569535
/* We should have Definitions file here */
570536
if (optind >= argc)
@@ -586,8 +552,6 @@ bool getopt_oval_collect(int argc, char **argv, struct oscap_action *action)
586552
{ "variables", required_argument, NULL, OVAL_OPT_VARIABLES },
587553
{ "syschar", required_argument, NULL, OVAL_OPT_SYSCHAR },
588554
{ "skip-valid", no_argument, &action->validate, 0 },
589-
{ "verbose", required_argument, NULL, OVAL_OPT_VERBOSE },
590-
{ "verbose-log-file", required_argument, NULL, OVAL_OPT_VERBOSE_LOG_FILE },
591555
{ 0, 0, 0, 0 }
592556
};
593557

@@ -597,19 +561,10 @@ bool getopt_oval_collect(int argc, char **argv, struct oscap_action *action)
597561
case OVAL_OPT_ID: action->id = optarg; break;
598562
case OVAL_OPT_VARIABLES: action->f_variables = optarg; break;
599563
case OVAL_OPT_SYSCHAR: action->f_syschar = optarg; break;
600-
case OVAL_OPT_VERBOSE:
601-
action->verbosity_level = optarg;
602-
break;
603-
case OVAL_OPT_VERBOSE_LOG_FILE:
604-
action->f_verbose_log = optarg;
605-
break;
606564
case 0: break;
607565
default: return oscap_module_usage(action->module, stderr, NULL);
608566
}
609567
}
610-
if (!check_verbose_options(action)) {
611-
return false;
612-
}
613568

614569
/* We should have Definitions file here */
615570
if (optind >= argc)
@@ -630,8 +585,6 @@ bool getopt_oval_analyse(int argc, char **argv, struct oscap_action *action)
630585
{ "variables", required_argument, NULL, OVAL_OPT_VARIABLES },
631586
{ "directives", required_argument, NULL, OVAL_OPT_DIRECTIVES },
632587
{ "skip-valid", no_argument, &action->validate, 0 },
633-
{ "verbose", required_argument, NULL, OVAL_OPT_VERBOSE },
634-
{ "verbose-log-file", required_argument, NULL, OVAL_OPT_VERBOSE_LOG_FILE },
635588
{ 0, 0, 0, 0 }
636589
};
637590

@@ -641,19 +594,10 @@ bool getopt_oval_analyse(int argc, char **argv, struct oscap_action *action)
641594
case OVAL_OPT_RESULT_FILE: action->f_results = optarg; break;
642595
case OVAL_OPT_VARIABLES: action->f_variables = optarg; break;
643596
case OVAL_OPT_DIRECTIVES: action->f_directives = optarg; break;
644-
case OVAL_OPT_VERBOSE:
645-
action->verbosity_level = optarg;
646-
break;
647-
case OVAL_OPT_VERBOSE_LOG_FILE:
648-
action->f_verbose_log = optarg;
649-
break;
650597
case 0: break;
651598
default: return oscap_module_usage(action->module, stderr, NULL);
652599
}
653600
}
654-
if (!check_verbose_options(action)) {
655-
return false;
656-
}
657601

658602
/* We should have Definitions file here */
659603
if (optind >= argc)

utils/oscap-tool.c

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ static void oscap_action_release(struct oscap_action *action)
6767
cvss_impact_free(action->cvss_impact);
6868
}
6969

70-
71-
static bool oscap_action_postprocess(struct oscap_action *action)
72-
{
73-
return true;
74-
}
75-
7670
static size_t paramlist_size(const char **p) { size_t s = 0; if (!p) return s; while (p[s]) s += 2; return s; }
7771

7872
static size_t paramlist_cpy(const char **to, const char **p) {
@@ -188,6 +182,12 @@ bool oscap_module_usage(struct oscap_module *module, FILE *out, const char *err,
188182
return (out != stderr);
189183
}
190184

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+
" Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
189+
" --verbose-log-file <file> - Write verbose informations into file.\n";
190+
191191
static void oscap_module_print_help(struct oscap_module *module, FILE *out)
192192
{
193193
assert(module != NULL);
@@ -202,7 +202,9 @@ static void oscap_module_print_help(struct oscap_module *module, FILE *out)
202202
oscap_module_print_usage(module, out, true);
203203
fprintf(out, "\n\n");
204204

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

207209
if (module->submodules) {
208210
fprintf(out, "Commands:\n");
@@ -237,28 +239,48 @@ enum oscap_common_opts {
237239
OPT_LISTMODS,
238240
OPT_LISTALLMODS,
239241
OPT_MODTREE,
240-
OPT_HELP = 'h'
242+
OPT_HELP = 'h',
243+
OPT_VERBOSE,
244+
OPT_VERBOSE_LOG_FILE
241245
};
242246

243-
static enum oscap_common_opts oscap_parse_common_opts(int argc, char **argv)
247+
static enum oscap_common_opts oscap_parse_common_opts(int argc, char **argv, struct oscap_action *action)
244248
{
245249
static const struct option opts[] = {
246250
{ "help", 0, 0, OPT_HELP },
247251
{ "list-submodules", 0, 0, OPT_LISTMODS },
248252
{ "list-all-submodules", 0, 0, OPT_LISTALLMODS },
249253
{ "module-tree", 0, 0, OPT_MODTREE },
254+
{ "verbose", required_argument, NULL, OPT_VERBOSE },
255+
{ "verbose-log-file", required_argument, NULL, OPT_VERBOSE_LOG_FILE },
250256
{ 0, 0, 0, 0 }
251257
};
252258

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

264286
int oscap_module_call(struct oscap_action *action)
@@ -267,9 +289,7 @@ int oscap_module_call(struct oscap_action *action)
267289
assert(action->module != NULL);
268290

269291
if (action->module->func) {
270-
if (oscap_action_postprocess(action))
271-
return action->module->func(action);
272-
else return OSCAP_ERR_FETCH;
292+
return action->module->func(action);
273293
}
274294
return OSCAP_UNIMPL_MOD;
275295
}
@@ -336,7 +356,7 @@ int oscap_module_process(struct oscap_module *module, int argc, char **argv)
336356

337357
getopt_parse_env(module, &argc, &argv);
338358

339-
switch (oscap_parse_common_opts(argc, argv)) {
359+
switch (oscap_parse_common_opts(argc, argv, &action)) {
340360
case OPT_HELP: oscap_module_print_help(module, stdout); goto cleanup;
341361
case OPT_LISTMODS: oscap_print_submodules(module, stdout, "\n", false); goto cleanup;
342362
case OPT_LISTALLMODS: oscap_print_submodules(module, stdout, "\n", true); goto cleanup;
@@ -353,6 +373,12 @@ int oscap_module_process(struct oscap_module *module, int argc, char **argv)
353373
}
354374

355375
if (module->func) {
376+
if (!check_verbose_options(&action)) {
377+
goto cleanup;
378+
}
379+
if (!oscap_set_verbose(action.verbosity_level, action.f_verbose_log)) {
380+
goto cleanup;
381+
}
356382
ret = oscap_module_call(&action);
357383
goto cleanup;
358384
}

0 commit comments

Comments
 (0)