@@ -60,7 +60,10 @@ static struct oscap_module CPE_MATCH_MODULE = {
6060 .parent = & OSCAP_CPE_MODULE ,
6161 .summary = "Match CPE name against provided dictionary" ,
6262 .usage = "name dictionary.xml" ,
63- .help = NULL ,
63+ .help = "Options:\n"
64+ " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
65+ " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
66+ " --verbose-log-file <file> - Write verbose information into file.\n" ,
6467 .opt_parser = getopt_cpe ,
6568 .func = app_cpe_match
6669};
@@ -70,7 +73,10 @@ static struct oscap_module CPE_CHECK_MODULE = {
7073 .parent = & OSCAP_CPE_MODULE ,
7174 .summary = "Check if CPE name is valid" ,
7275 .usage = "name" ,
73- .help = NULL ,
76+ .help = "Options:\n"
77+ " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
78+ " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
79+ " --verbose-log-file <file> - Write verbose information into file.\n" ,
7480 .opt_parser = getopt_cpe ,
7581 .func = app_cpe_check
7682};
@@ -80,7 +86,10 @@ static struct oscap_module CPE_VALIDATE = {
8086 .parent = & OSCAP_CPE_MODULE ,
8187 .summary = "Validate CPE Dictionary content" ,
8288 .usage = "cpe-dict.xml" ,
83- .help = NULL ,
89+ .help = "Options:\n"
90+ " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
91+ " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
92+ " --verbose-log-file <file> - Write verbose information into file.\n" ,
8493 .opt_parser = getopt_cpe ,
8594 .func = app_cpe_validate
8695};
@@ -93,37 +102,48 @@ static struct oscap_module* CPE_SUBMODULES[CPE_SUBMODULES_NUM] = {
93102};
94103
95104bool getopt_cpe (int argc , char * * argv , struct oscap_action * action ) {
96-
97- if (action -> module == & CPE_MATCH_MODULE ) {
98- if ( argc != 5 ) {
99- oscap_module_usage (action -> module , stderr , "Wrong number of parameters.\n" );
100- return false;
105+ enum oscap_cpe_opts {
106+ OSCAP_CPE_OPT_VERBOSE = 1 ,
107+ OSCAP_CPE_OPT_VERBOSE_LOG_FILE ,
108+ };
109+ const struct option long_options [] = {
110+ // options
111+ {"verbose" , required_argument , NULL , OSCAP_CPE_OPT_VERBOSE },
112+ {"verbose-log-file" , required_argument , NULL , OSCAP_CPE_OPT_VERBOSE_LOG_FILE },
113+ // end
114+ {0 , 0 , 0 , 0 }
115+ };
116+ int c ;
117+ while ((c = getopt_long (argc , argv , "o:i:" , long_options , NULL )) != -1 ) {
118+ switch (c ) {
119+ case OSCAP_CPE_OPT_VERBOSE :
120+ action -> verbosity_level = optarg ;
121+ break ;
122+ case OSCAP_CPE_OPT_VERBOSE_LOG_FILE :
123+ action -> f_verbose_log = optarg ;
124+ break ;
125+ case 0 : break ;
126+ default : return oscap_module_usage (action -> module , stderr , NULL );
101127 }
102- action -> cpe_action = malloc (sizeof (struct cpe_action ));
103- action -> cpe_action -> name = argv [3 ];
104- action -> cpe_action -> dict = argv [4 ];
105128 }
106-
129+ if (optind >= argc ) {
130+ return oscap_module_usage (action -> module , stderr , "Wrong number of arguments!\n" );
131+ }
132+ action -> cpe_action = malloc (sizeof (struct cpe_action ));
133+ if (action -> cpe_action == NULL ) {
134+ fprintf (stderr , "Memory allocation error.\n" );
135+ return false;
136+ }
137+ if (action -> module == & CPE_MATCH_MODULE ) {
138+ action -> cpe_action -> name = argv [optind ];
139+ action -> cpe_action -> dict = argv [optind + 1 ];
140+ }
107141 if (action -> module == & CPE_CHECK_MODULE ) {
108- if ( argc != 4 ) {
109- oscap_module_usage (action -> module , stderr , "Wrong number of parameters.\n" );
110- return false;
111- }
112- action -> cpe_action = malloc (sizeof (struct cpe_action ));
113- action -> cpe_action -> name = argv [3 ];
142+ action -> cpe_action -> name = argv [optind ];
114143 }
115-
116144 if (action -> module == & CPE_VALIDATE ) {
117- if ( argc != 4 ) {
118- oscap_module_usage (action -> module , stderr , "Wrong number of parameters.\n" );
119- return false;
120- }
121-
122- action -> cpe_action = malloc (sizeof (struct cpe_action ));
123- action -> cpe_action -> dict = argv [3 ];
145+ action -> cpe_action -> dict = argv [optind ];
124146 }
125-
126-
127147 return true;
128148}
129149
0 commit comments