aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/extra_opts.c155
-rw-r--r--lib/extra_opts.h15
-rw-r--r--lib/tests/config-opts.ini14
-rw-r--r--lib/tests/test_opts.c208
-rwxr-xr-xlib/tests/test_opts.t6
5 files changed, 398 insertions, 0 deletions
diff --git a/lib/extra_opts.c b/lib/extra_opts.c
new file mode 100644
index 00000000..3a0ce045
--- /dev/null
+++ b/lib/extra_opts.c
@@ -0,0 +1,155 @@
+/*****************************************************************************
+*
+* Nagios-plugins extra_opts library
+*
+* License: GPL
+* Copyright (c) 2007 Nagios Plugins Development Team
+*
+* Last Modified: $Date: 2008-03-15 18:42:01 -0400 (Sat, 15 Mar 2008) $
+*
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*
+* $Id: parse_ini.c 1950 2008-03-15 22:42:01Z dermoth $
+*
+*****************************************************************************/
+
+#include "common.h"
+#include "extra_opts.h"
+#include "parse_ini.h"
+#include "utils_base.h"
+#include <ctype.h>
+
+/* FIXME: copied from utils.h; we should move a bunch of libs! */
+int
+is_option (char *str)
+{
+ if (!str)
+ return 0;
+ else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
+ return 1;
+ else
+ return 0;
+}
+
+/* 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 **argv_new=NULL;
+ char *argptr=NULL;
+ int i, j, optfound, ea_num=argc;
+
+ if(argc<2) {
+ /* No arguments provided */
+ *argc_new=argc;
+ argv_new=argv;
+ return argv_new;
+ }
+
+ for(i=1; i<argc; i++){
+ argptr=NULL;
+ optfound=0;
+
+ /* Do we have an extra-opts parameter? */
+ if(strncmp(argv[i], "--extra-opts=", 13)==0){
+ /* 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];
+ i--;
+ 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];
+ i-=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];
+ i--;
+ argc--;
+ }
+ }
+
+ 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? */
+ 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++;
+ }
+ }else{
+ ea_tmp2=extra_args;
+ while(ea_tmp2->next) {
+ ea_tmp2=ea_tmp2->next;
+ ea_num++;
+ }
+ ea_tmp2->next=ea_tmp1;
+ }
+ ea_tmp1=ea_tmp2=NULL;
+ }
+ /* lather, rince, repeat */
+ }
+
+ if(ea_num==argc && extra_args==NULL){
+ /* No extra-opts */
+ *argc_new=argc;
+ argv_new=argv;
+ return argv_new;
+ }
+
+ /* done processing arguments. now create a new argc/argv set... */
+ 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?) */
+ argv_new[0]=strdup(argv[0]);
+ *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;
+ 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;
+ }
+ /* and terminate. */
+ argv_new[*argc_new]=NULL;
+
+ return argv_new;
+}
+
diff --git a/lib/extra_opts.h b/lib/extra_opts.h
new file mode 100644
index 00000000..5f89d2b2
--- /dev/null
+++ b/lib/extra_opts.h
@@ -0,0 +1,15 @@
+#ifndef _EXTRA_OPTS_H_
+#define _EXTRA_OPTS_H_
+
+/*
+ * extra_opts.h: routines for loading nagios-plugin defaults from ini
+ * configuration files.
+ */
+
+/* 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
+ */
+char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_new);
+
+#endif /* _EXTRA_OPTS_H_ */
diff --git a/lib/tests/config-opts.ini b/lib/tests/config-opts.ini
new file mode 100644
index 00000000..5c935720
--- /dev/null
+++ b/lib/tests/config-opts.ini
@@ -0,0 +1,14 @@
+# This config file is for testing test_opts
+
+[sect1]
+one=two
+
+[check_disk]
+foo=Bar
+this=Your Mother!
+blank=
+
+[sect2]
+something else=oops
+this=that
+
diff --git a/lib/tests/test_opts.c b/lib/tests/test_opts.c
new file mode 100644
index 00000000..12726059
--- /dev/null
+++ b/lib/tests/test_opts.c
@@ -0,0 +1,208 @@
+/*****************************************************************************
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*
+* $Id: test_ini.c 1951 2008-03-16 18:10:47Z dermoth $
+*
+*****************************************************************************/
+
+#include "common.h"
+#include "extra_opts.h"
+#include "utils_base.h"
+
+#include "tap.h"
+
+void my_free(int *argc, char **argv) {
+ int i;
+ printf (" Arg(%i): ", *argc);
+ for (i=1; i<*argc; i++) printf ("'%s' ", argv[i]);
+ printf ("\n");
+ free(argv);
+ *argc=0;
+}
+
+int array_diff(int i1, char **a1, int i2, char **a2) {
+ int i;
+
+ if (i1 != i2) {
+ printf(" Argument count doesn't match!\n");
+ return 0;
+ }
+ for (i=0; i<=i1; i++) {
+ if (a1[i]==NULL && a2[i]==NULL) continue;
+ if (a1[i]==NULL || a2[i]==NULL) {
+ printf(" Argument # %i null in one array!\n", i);
+ return 0;
+ }
+ if (strcmp(a1[i], a2[i])) {
+ printf(" Argument # %i doesn't match!\n", i);
+ return 0;
+ }
+ }
+ return 1;
+}
+
+int
+main (int argc, char **argv)
+{
+ char **argv_test=NULL, **argv_known=NULL;
+ int i, argc_test, argc_new;
+
+ plan_tests(8);
+
+ argv_test=(char **)malloc(2*sizeof(char **));
+ argv_test[0] = "prog_name";
+ argv_test[1] = NULL;
+ argc_test=1;
+ 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=(char **)malloc(6*sizeof(char **));
+ argv_test[0] = "prog_name";
+ argv_test[1] = "arg1";
+ argv_test[2] = "--arg2=val1";
+ argv_test[3] = "--arg3";
+ argv_test[4] = "val2";
+ argv_test[5] = NULL;
+ argc_test=5;
+ argv_known=(char **)realloc(argv_known, 6*sizeof(char **));
+ argv_known[0] = "prog_name";
+ argv_known[1] = "arg1";
+ argv_known[2] = "--arg2=val1";
+ 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=(char **)malloc(3*sizeof(char **));
+ argv_test[0] = "prog_name";
+ argv_test[1] = "--extra-opts=@./config-opts.ini";
+ argv_test[2] = NULL;
+ argc_test=2;
+ argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
+ argv_known[0] = "prog_name";
+ argv_known[1] = "--foo=Bar";
+ 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=(char **)malloc(5*sizeof(char **));
+ argv_test[0] = "prog_name";
+ argv_test[1] = "--extra-opts=sect1@./config-opts.ini";
+ argv_test[2] = "--extra-opts";
+ argv_test[3] = "sect2@./config-opts.ini";
+ argv_test[4] = NULL;
+ argc_test=4;
+ argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
+ argv_known[0] = "prog_name";
+ argv_known[1] = "--one=two";
+ 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=(char **)malloc(7*sizeof(char **));
+ argv_test[0] = "prog_name";
+ argv_test[1] = "--arg1=val1";
+ argv_test[2] = "--extra-opts=@./config-opts.ini";
+ argv_test[3] = "--extra-opts";
+ argv_test[4] = "sect1@./config-opts.ini";
+ argv_test[5] = "--arg2";
+ argv_test[6] = NULL;
+ argc_test=6;
+ argv_known=(char **)realloc(argv_known, 8*sizeof(char **));
+ argv_known[0] = "prog_name";
+ argv_known[1] = "--foo=Bar";
+ argv_known[2] = "--this=Your Mother!";
+ argv_known[3] = "--blank";
+ argv_known[4] = "--one=two";
+ 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);
+
+ /* 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)
+ */
+ argv_test=(char **)malloc(6*sizeof(char **));
+ argv_test[0] = "prog_name";
+ argv_test[1] = "arg1";
+ argv_test[2] = "--extra-opts=missing@./config-opts.ini";
+ argv_test[3] = "--arg3";
+ argv_test[4] = "val2";
+ argv_test[5] = NULL;
+ argc_test=5;
+ argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
+ argv_known[0] = "prog_name";
+ argv_known[1] = "arg1";
+ 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=(char **)malloc(7*sizeof(char **));
+ argv_test[0] = "prog_name";
+ argv_test[1] = "arg1";
+ argv_test[2] = "--extra-opts";
+ argv_test[3] = "missing@./config-opts.ini";
+ argv_test[4] = "--arg3";
+ argv_test[5] = "val2";
+ argv_test[6] = NULL;
+ argc_test=6;
+ argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
+ argv_known[0] = "prog_name";
+ argv_known[1] = "arg1";
+ 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=(char **)malloc(6*sizeof(char **));
+ argv_test[0] = "prog_name";
+ argv_test[1] = "arg1";
+ argv_test[2] = "--extra-opts";
+ argv_test[3] = "--arg3";
+ argv_test[4] = "val2";
+ argv_test[5] = NULL;
+ argc_test=5;
+ argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
+ argv_known[0] = "prog_name";
+ argv_known[1] = "arg1";
+ 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);
+
+ return exit_status();
+}
+
diff --git a/lib/tests/test_opts.t b/lib/tests/test_opts.t
new file mode 100755
index 00000000..86a5b21a
--- /dev/null
+++ b/lib/tests/test_opts.t
@@ -0,0 +1,6 @@
+#!/usr/bin/perl
+use Test::More;
+if (! -e "./test_opts") {
+ plan skip_all => "./test_opts not compiled - please install tap library to test";
+}
+exec "./test_opts";