aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/extra_opts.c83
-rw-r--r--lib/extra_opts.h10
-rw-r--r--lib/tests/test_opts.c68
3 files changed, 75 insertions, 86 deletions
diff --git a/lib/extra_opts.c b/lib/extra_opts.c
index 3a0ce045..2aeb77ac 100644
--- a/lib/extra_opts.c
+++ b/lib/extra_opts.c
@@ -44,23 +44,18 @@ is_option (char *str)
}
/* this is the externally visible function used by plugins */
-/* Shouldn't se modify directly **argv (passed as a char ***) and argc
- * (as int *) ?
- */
-char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_new){
- np_arg_list *extra_args=NULL, *ea_tmp1=NULL, *ea_tmp2=NULL;
+char **np_extra_opts(int *argc, char **argv, const char *plugin_name){
+ np_arg_list *extra_args=NULL, *ea1=NULL, *ea_tmp=NULL;
char **argv_new=NULL;
char *argptr=NULL;
- int i, j, optfound, ea_num=argc;
+ int i, j, optfound, argc_new, ea_num=*argc;
- if(argc<2) {
+ if(*argc<2) {
/* No arguments provided */
- *argc_new=argc;
- argv_new=argv;
- return argv_new;
+ return argv;
}
- for(i=1; i<argc; i++){
+ for(i=1; i<*argc; i++){
argptr=NULL;
optfound=0;
@@ -69,86 +64,76 @@ char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_n
/* It is a single argument with value */
argptr=argv[i]+13;
/* Delete the extra opts argument */
- for(j=i;j<argc;j++) argv[j]=argv[j+1];
+ for(j=i;j<*argc;j++) argv[j]=argv[j+1];
i--;
- argc--;
+ *argc--;
}else if(strcmp(argv[i], "--extra-opts")==0){
if(!is_option(argv[i+1])){
/* It is a argument with separate value */
argptr=argv[i+1];
/* Delete the extra-opts argument/value */
- for(j=i;j<argc-1;j++) argv[j]=argv[j+2];
+ for(j=i;j<*argc-1;j++) argv[j]=argv[j+2];
i-=2;
- argc-=2;
+ *argc-=2;
ea_num--;
}else{
/* It has no value */
optfound=1;
/* Delete the extra opts argument */
- for(j=i;j<argc;j++) argv[j]=argv[j+1];
+ for(j=i;j<*argc;j++) argv[j]=argv[j+1];
i--;
- argc--;
+ *argc--;
}
}
+ /* If we found extra-opts, expand them and store them for later*/
if(argptr||optfound){
/* Process ini section, returning a linked list of arguments */
- ea_tmp1=np_get_defaults(argptr, plugin_name);
- if(ea_tmp1==NULL) {
- /* no extra args? */
+ ea1=np_get_defaults(argptr, plugin_name);
+ if(ea1==NULL) {
+ /* no extra args (empty section)? */
ea_num--;
continue;
}
/* append the list to extra_args */
if(extra_args==NULL){
- extra_args=ea_tmp2=ea_tmp1;
- while(ea_tmp2->next) {
- ea_tmp2=ea_tmp2->next;
- ea_num++;
- }
+ extra_args=ea1;
+ while(ea1=ea1->next) ea_num++;
}else{
- ea_tmp2=extra_args;
- while(ea_tmp2->next) {
- ea_tmp2=ea_tmp2->next;
- ea_num++;
- }
- ea_tmp2->next=ea_tmp1;
+ ea_tmp=extra_args;
+ while(ea_tmp=ea_tmp->next) ea_num++;
+ ea_tmp->next=ea1;
}
- ea_tmp1=ea_tmp2=NULL;
+ ea1=ea_tmp=NULL;
}
/* lather, rince, repeat */
}
- if(ea_num==argc && extra_args==NULL){
+ if(ea_num==*argc && extra_args==NULL){
/* No extra-opts */
- *argc_new=argc;
- argv_new=argv;
- return argv_new;
+ return argv;
}
- /* done processing arguments. now create a new argc/argv set... */
+ /* done processing arguments. now create a new argv array... */
argv_new=(char**)malloc((ea_num+1)*sizeof(char**));
if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
- /* starting with program name (Should we strdup or just use the poiter?) */
+ /* starting with program name */
argv_new[0]=strdup(argv[0]);
- *argc_new=1;
+ argc_new=1;
/* then parsed ini opts (frying them up in the same run) */
while(extra_args){
- argv_new[*argc_new]=strdup(extra_args->arg);
- *argc_new+=1;
- ea_tmp1=extra_args;
+ argv_new[argc_new++]=extra_args->arg;
+ ea1=extra_args;
extra_args=extra_args->next;
- free(ea_tmp1);
- }
- /* finally the rest of the argv array (Should we strdup or just use the poiter?) */
- for (i=1; i<argc; i++){
- argv_new[*argc_new]=strdup(argv[i]);
- *argc_new+=1;
+ free(ea1);
}
+ /* finally the rest of the argv array */
+ for (i=1; i<*argc; i++) argv_new[argc_new++]=strdup(argv[i]);
+ *argc=argc_new;
/* and terminate. */
- argv_new[*argc_new]=NULL;
+ argv_new[argc_new]=NULL;
return argv_new;
}
diff --git a/lib/extra_opts.h b/lib/extra_opts.h
index 5f89d2b2..25bfc6db 100644
--- a/lib/extra_opts.h
+++ b/lib/extra_opts.h
@@ -7,9 +7,13 @@
*/
/* np_extra_opts: Process the --extra-opts arguments and create a new argument
- * array load the default configuration (if present) for
- * a plugin from the ini file
+ * array with ini-processed and argument-passed arguments together. The
+ * ini-procesed arguments always come first (in the ord of --extra-opts
+ * arguments). If no --extra-opts arguments are provided or returned nothing
+ * it returns **argv otherwise the new array is returned. --extra-opts are
+ * always removed from **argv and the new array and all its elements can be
+ * freed with free();
*/
-char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_new);
+char **np_extra_opts(int *argc, char **argv, const char *plugin_name);
#endif /* _EXTRA_OPTS_H_ */
diff --git a/lib/tests/test_opts.c b/lib/tests/test_opts.c
index 769a2df2..7c173f30 100644
--- a/lib/tests/test_opts.c
+++ b/lib/tests/test_opts.c
@@ -57,7 +57,7 @@ int
main (int argc, char **argv)
{
char **argv_test=NULL, **argv_known=NULL;
- int i, argc_test, argc_new;
+ int i, argc_test;
plan_tests(11);
@@ -68,9 +68,9 @@ main (int argc, char **argv)
argv_known=(char **)realloc(argv_known, 2*sizeof(char **));
argv_known[0] = "prog_name";
argv_known[1] = NULL;
- argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new);
- ok(array_diff(argc_new, argv_test, 1, argv_known), "No opts, returns correct argv/argc");
- my_free(&argc_new, argv_test);
+ argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
+ ok(array_diff(argc_test, argv_test, 1, argv_known), "No opts, returns correct argv/argc");
+ my_free(&argc_test, argv_test);
argv_test=(char **)malloc(6*sizeof(char **));
argv_test[0] = "prog_name";
@@ -87,9 +87,9 @@ main (int argc, char **argv)
argv_known[3] = "--arg3";
argv_known[4] = "val2";
argv_known[5] = NULL;
- argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new);
- ok(array_diff(argc_new, argv_test, 5, argv_known), "No extra opts, verbatim copy of argv");
- my_free(&argc_new,argv_test);
+ argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
+ ok(array_diff(argc_test, argv_test, 5, argv_known), "No extra opts, verbatim copy of argv");
+ my_free(&argc_test,argv_test);
argv_test=(char **)malloc(3*sizeof(char **));
argv_test[0] = "prog_name";
@@ -102,9 +102,9 @@ main (int argc, char **argv)
argv_known[2] = "--this=Your Mother!";
argv_known[3] = "--blank";
argv_known[4] = NULL;
- argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new);
- ok(array_diff(argc_new, argv_test, 4, argv_known), "Only extra opts using default section");
- my_free(&argc_new,argv_test);
+ argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
+ ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts using default section");
+ my_free(&argc_test,argv_test);
argv_test=(char **)malloc(5*sizeof(char **));
argv_test[0] = "prog_name";
@@ -119,9 +119,9 @@ main (int argc, char **argv)
argv_known[2] = "--something else=oops";
argv_known[3] = "--this=that";
argv_known[4] = NULL;
- argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new);
- ok(array_diff(argc_new, argv_test, 4, argv_known), "Only extra opts specified twice");
- my_free(&argc_new,argv_test);
+ argv_test=np_extra_opts(argc_test, argv_test, "check_disk");
+ ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts specified twice");
+ my_free(&argc_test,argv_test);
argv_test=(char **)malloc(7*sizeof(char **));
argv_test[0] = "prog_name";
@@ -141,9 +141,9 @@ main (int argc, char **argv)
argv_known[5] = "--arg1=val1";
argv_known[6] = "--arg2";
argv_known[7] = NULL;
- argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new);
- ok(array_diff(argc_new, argv_test, 7, argv_known), "twice extra opts using two sections");
- my_free(&argc_new,argv_test);
+ argv_test=np_extra_opts(argc_test, argv_test, "check_disk");
+ ok(array_diff(argc_test, argv_test, 7, argv_known), "twice extra opts using two sections");
+ my_free(&argc_test,argv_test);
/* Next three checks should die according to N::P - for now they're useful
* to test code is working properly (i.e. no srash or unexpected behavior)
@@ -162,9 +162,9 @@ main (int argc, char **argv)
argv_known[2] = "--arg3";
argv_known[3] = "val2";
argv_known[4] = NULL;
- argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new);
- ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 1");
- my_free(&argc_new,argv_test);
+ argv_test=np_extra_opts(argc_test, argv_test, "check_missing");
+ ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 1");
+ my_free(&argc_test,argv_test);
argv_test=(char **)malloc(7*sizeof(char **));
argv_test[0] = "prog_name";
@@ -181,9 +181,9 @@ main (int argc, char **argv)
argv_known[2] = "--arg3";
argv_known[3] = "val2";
argv_known[4] = NULL;
- argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new);
- ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 2");
- my_free(&argc_new,argv_test);
+ argv_test=np_extra_opts(argc_test, argv_test, "check_missing");
+ ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 2");
+ my_free(&argc_test,argv_test);
argv_test=(char **)malloc(6*sizeof(char **));
argv_test[0] = "prog_name";
@@ -199,9 +199,9 @@ main (int argc, char **argv)
argv_known[2] = "--arg3";
argv_known[3] = "val2";
argv_known[4] = NULL;
- argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new);
- ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 3");
- my_free(&argc_new,argv_test);
+ argv_test=np_extra_opts(argc_test, argv_test, "check_missing");
+ ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 3");
+ my_free(&argc_test,argv_test);
setenv("NAGIOS_CONFIG_PATH", ".", 1);
argv_test=(char **)malloc(6*sizeof(char **));
@@ -219,9 +219,9 @@ main (int argc, char **argv)
argv_known[3] = "--arg3";
argv_known[4] = "val2";
argv_known[5] = NULL;
- argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new);
- ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 1");
- my_free(&argc_new,argv_test);
+ argv_test=np_extra_opts(argc_test, argv_test, "check_disk");
+ ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 1");
+ my_free(&argc_test,argv_test);
argv_test=(char **)malloc(6*sizeof(char **));
argv_test[0] = "prog_name";
@@ -238,9 +238,9 @@ main (int argc, char **argv)
argv_known[3] = "--arg3";
argv_known[4] = "val2";
argv_known[5] = NULL;
- argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new);
- ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 2");
- my_free(&argc_new,argv_test);
+ argv_test=np_extra_opts(argc_test, argv_test, "check_disk");
+ ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 2");
+ my_free(&argc_test,argv_test);
argv_test=(char **)malloc(6*sizeof(char **));
argv_test[0] = "prog_name";
@@ -257,9 +257,9 @@ main (int argc, char **argv)
argv_known[3] = "--arg3";
argv_known[4] = "val2";
argv_known[5] = NULL;
- argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new);
- ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 3");
- my_free(&argc_new,argv_test);
+ argv_test=np_extra_opts(argc_test, argv_test, "check_disk");
+ ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 3");
+ my_free(&argc_test,argv_test);
return exit_status();
}