diff options
-rw-r--r-- | lib/extra_opts.c | 155 | ||||
-rw-r--r-- | lib/extra_opts.h | 15 | ||||
-rw-r--r-- | lib/tests/config-opts.ini | 14 | ||||
-rw-r--r-- | lib/tests/test_opts.c | 208 | ||||
-rwxr-xr-x | lib/tests/test_opts.t | 6 |
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 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * Nagios-plugins extra_opts library | ||
4 | * | ||
5 | * License: GPL | ||
6 | * Copyright (c) 2007 Nagios Plugins Development Team | ||
7 | * | ||
8 | * Last Modified: $Date: 2008-03-15 18:42:01 -0400 (Sat, 15 Mar 2008) $ | ||
9 | * | ||
10 | * | ||
11 | * This program is free software: you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU General Public License as published by | ||
13 | * the Free Software Foundation, either version 3 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
20 | * | ||
21 | * You should have received a copy of the GNU General Public License | ||
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
23 | * | ||
24 | * $Id: parse_ini.c 1950 2008-03-15 22:42:01Z dermoth $ | ||
25 | * | ||
26 | *****************************************************************************/ | ||
27 | |||
28 | #include "common.h" | ||
29 | #include "extra_opts.h" | ||
30 | #include "parse_ini.h" | ||
31 | #include "utils_base.h" | ||
32 | #include <ctype.h> | ||
33 | |||
34 | /* FIXME: copied from utils.h; we should move a bunch of libs! */ | ||
35 | int | ||
36 | is_option (char *str) | ||
37 | { | ||
38 | if (!str) | ||
39 | return 0; | ||
40 | else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) | ||
41 | return 1; | ||
42 | else | ||
43 | return 0; | ||
44 | } | ||
45 | |||
46 | /* this is the externally visible function used by plugins */ | ||
47 | /* Shouldn't se modify directly **argv (passed as a char ***) and argc | ||
48 | * (as int *) ? | ||
49 | */ | ||
50 | char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_new){ | ||
51 | np_arg_list *extra_args=NULL, *ea_tmp1=NULL, *ea_tmp2=NULL; | ||
52 | char **argv_new=NULL; | ||
53 | char *argptr=NULL; | ||
54 | int i, j, optfound, ea_num=argc; | ||
55 | |||
56 | if(argc<2) { | ||
57 | /* No arguments provided */ | ||
58 | *argc_new=argc; | ||
59 | argv_new=argv; | ||
60 | return argv_new; | ||
61 | } | ||
62 | |||
63 | for(i=1; i<argc; i++){ | ||
64 | argptr=NULL; | ||
65 | optfound=0; | ||
66 | |||
67 | /* Do we have an extra-opts parameter? */ | ||
68 | if(strncmp(argv[i], "--extra-opts=", 13)==0){ | ||
69 | /* It is a single argument with value */ | ||
70 | argptr=argv[i]+13; | ||
71 | /* Delete the extra opts argument */ | ||
72 | for(j=i;j<argc;j++) argv[j]=argv[j+1]; | ||
73 | i--; | ||
74 | argc--; | ||
75 | }else if(strcmp(argv[i], "--extra-opts")==0){ | ||
76 | if(!is_option(argv[i+1])){ | ||
77 | /* It is a argument with separate value */ | ||
78 | argptr=argv[i+1]; | ||
79 | /* Delete the extra-opts argument/value */ | ||
80 | for(j=i;j<argc-1;j++) argv[j]=argv[j+2]; | ||
81 | i-=2; | ||
82 | argc-=2; | ||
83 | ea_num--; | ||
84 | }else{ | ||
85 | /* It has no value */ | ||
86 | optfound=1; | ||
87 | /* Delete the extra opts argument */ | ||
88 | for(j=i;j<argc;j++) argv[j]=argv[j+1]; | ||
89 | i--; | ||
90 | argc--; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | if(argptr||optfound){ | ||
95 | /* Process ini section, returning a linked list of arguments */ | ||
96 | ea_tmp1=np_get_defaults(argptr, plugin_name); | ||
97 | if(ea_tmp1==NULL) { | ||
98 | /* no extra args? */ | ||
99 | ea_num--; | ||
100 | continue; | ||
101 | } | ||
102 | |||
103 | /* append the list to extra_args */ | ||
104 | if(extra_args==NULL){ | ||
105 | extra_args=ea_tmp2=ea_tmp1; | ||
106 | while(ea_tmp2->next) { | ||
107 | ea_tmp2=ea_tmp2->next; | ||
108 | ea_num++; | ||
109 | } | ||
110 | }else{ | ||
111 | ea_tmp2=extra_args; | ||
112 | while(ea_tmp2->next) { | ||
113 | ea_tmp2=ea_tmp2->next; | ||
114 | ea_num++; | ||
115 | } | ||
116 | ea_tmp2->next=ea_tmp1; | ||
117 | } | ||
118 | ea_tmp1=ea_tmp2=NULL; | ||
119 | } | ||
120 | /* lather, rince, repeat */ | ||
121 | } | ||
122 | |||
123 | if(ea_num==argc && extra_args==NULL){ | ||
124 | /* No extra-opts */ | ||
125 | *argc_new=argc; | ||
126 | argv_new=argv; | ||
127 | return argv_new; | ||
128 | } | ||
129 | |||
130 | /* done processing arguments. now create a new argc/argv set... */ | ||
131 | argv_new=(char**)malloc((ea_num+1)*sizeof(char**)); | ||
132 | if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
133 | |||
134 | /* starting with program name (Should we strdup or just use the poiter?) */ | ||
135 | argv_new[0]=strdup(argv[0]); | ||
136 | *argc_new=1; | ||
137 | /* then parsed ini opts (frying them up in the same run) */ | ||
138 | while(extra_args){ | ||
139 | argv_new[*argc_new]=strdup(extra_args->arg); | ||
140 | *argc_new+=1; | ||
141 | ea_tmp1=extra_args; | ||
142 | extra_args=extra_args->next; | ||
143 | free(ea_tmp1); | ||
144 | } | ||
145 | /* finally the rest of the argv array (Should we strdup or just use the poiter?) */ | ||
146 | for (i=1; i<argc; i++){ | ||
147 | argv_new[*argc_new]=strdup(argv[i]); | ||
148 | *argc_new+=1; | ||
149 | } | ||
150 | /* and terminate. */ | ||
151 | argv_new[*argc_new]=NULL; | ||
152 | |||
153 | return argv_new; | ||
154 | } | ||
155 | |||
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 @@ | |||
1 | #ifndef _EXTRA_OPTS_H_ | ||
2 | #define _EXTRA_OPTS_H_ | ||
3 | |||
4 | /* | ||
5 | * extra_opts.h: routines for loading nagios-plugin defaults from ini | ||
6 | * configuration files. | ||
7 | */ | ||
8 | |||
9 | /* np_extra_opts: Process the --extra-opts arguments and create a new argument | ||
10 | * array load the default configuration (if present) for | ||
11 | * a plugin from the ini file | ||
12 | */ | ||
13 | char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_new); | ||
14 | |||
15 | #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 @@ | |||
1 | # This config file is for testing test_opts | ||
2 | |||
3 | [sect1] | ||
4 | one=two | ||
5 | |||
6 | [check_disk] | ||
7 | foo=Bar | ||
8 | this=Your Mother! | ||
9 | blank= | ||
10 | |||
11 | [sect2] | ||
12 | something else=oops | ||
13 | this=that | ||
14 | |||
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 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 3 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | * | ||
16 | * $Id: test_ini.c 1951 2008-03-16 18:10:47Z dermoth $ | ||
17 | * | ||
18 | *****************************************************************************/ | ||
19 | |||
20 | #include "common.h" | ||
21 | #include "extra_opts.h" | ||
22 | #include "utils_base.h" | ||
23 | |||
24 | #include "tap.h" | ||
25 | |||
26 | void my_free(int *argc, char **argv) { | ||
27 | int i; | ||
28 | printf (" Arg(%i): ", *argc); | ||
29 | for (i=1; i<*argc; i++) printf ("'%s' ", argv[i]); | ||
30 | printf ("\n"); | ||
31 | free(argv); | ||
32 | *argc=0; | ||
33 | } | ||
34 | |||
35 | int array_diff(int i1, char **a1, int i2, char **a2) { | ||
36 | int i; | ||
37 | |||
38 | if (i1 != i2) { | ||
39 | printf(" Argument count doesn't match!\n"); | ||
40 | return 0; | ||
41 | } | ||
42 | for (i=0; i<=i1; i++) { | ||
43 | if (a1[i]==NULL && a2[i]==NULL) continue; | ||
44 | if (a1[i]==NULL || a2[i]==NULL) { | ||
45 | printf(" Argument # %i null in one array!\n", i); | ||
46 | return 0; | ||
47 | } | ||
48 | if (strcmp(a1[i], a2[i])) { | ||
49 | printf(" Argument # %i doesn't match!\n", i); | ||
50 | return 0; | ||
51 | } | ||
52 | } | ||
53 | return 1; | ||
54 | } | ||
55 | |||
56 | int | ||
57 | main (int argc, char **argv) | ||
58 | { | ||
59 | char **argv_test=NULL, **argv_known=NULL; | ||
60 | int i, argc_test, argc_new; | ||
61 | |||
62 | plan_tests(8); | ||
63 | |||
64 | argv_test=(char **)malloc(2*sizeof(char **)); | ||
65 | argv_test[0] = "prog_name"; | ||
66 | argv_test[1] = NULL; | ||
67 | argc_test=1; | ||
68 | argv_known=(char **)realloc(argv_known, 2*sizeof(char **)); | ||
69 | argv_known[0] = "prog_name"; | ||
70 | argv_known[1] = NULL; | ||
71 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
72 | ok(array_diff(argc_new, argv_test, 1, argv_known), "No opts, returns correct argv/argc"); | ||
73 | my_free(&argc_new, argv_test); | ||
74 | |||
75 | argv_test=(char **)malloc(6*sizeof(char **)); | ||
76 | argv_test[0] = "prog_name"; | ||
77 | argv_test[1] = "arg1"; | ||
78 | argv_test[2] = "--arg2=val1"; | ||
79 | argv_test[3] = "--arg3"; | ||
80 | argv_test[4] = "val2"; | ||
81 | argv_test[5] = NULL; | ||
82 | argc_test=5; | ||
83 | argv_known=(char **)realloc(argv_known, 6*sizeof(char **)); | ||
84 | argv_known[0] = "prog_name"; | ||
85 | argv_known[1] = "arg1"; | ||
86 | argv_known[2] = "--arg2=val1"; | ||
87 | argv_known[3] = "--arg3"; | ||
88 | argv_known[4] = "val2"; | ||
89 | argv_known[5] = NULL; | ||
90 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
91 | ok(array_diff(argc_new, argv_test, 5, argv_known), "No extra opts, verbatim copy of argv"); | ||
92 | my_free(&argc_new,argv_test); | ||
93 | |||
94 | argv_test=(char **)malloc(3*sizeof(char **)); | ||
95 | argv_test[0] = "prog_name"; | ||
96 | argv_test[1] = "--extra-opts=@./config-opts.ini"; | ||
97 | argv_test[2] = NULL; | ||
98 | argc_test=2; | ||
99 | argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); | ||
100 | argv_known[0] = "prog_name"; | ||
101 | argv_known[1] = "--foo=Bar"; | ||
102 | argv_known[2] = "--this=Your Mother!"; | ||
103 | argv_known[3] = "--blank"; | ||
104 | argv_known[4] = NULL; | ||
105 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
106 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Only extra opts using default section"); | ||
107 | my_free(&argc_new,argv_test); | ||
108 | |||
109 | argv_test=(char **)malloc(5*sizeof(char **)); | ||
110 | argv_test[0] = "prog_name"; | ||
111 | argv_test[1] = "--extra-opts=sect1@./config-opts.ini"; | ||
112 | argv_test[2] = "--extra-opts"; | ||
113 | argv_test[3] = "sect2@./config-opts.ini"; | ||
114 | argv_test[4] = NULL; | ||
115 | argc_test=4; | ||
116 | argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); | ||
117 | argv_known[0] = "prog_name"; | ||
118 | argv_known[1] = "--one=two"; | ||
119 | argv_known[2] = "--something else=oops"; | ||
120 | argv_known[3] = "--this=that"; | ||
121 | argv_known[4] = NULL; | ||
122 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
123 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Only extra opts specified twice"); | ||
124 | my_free(&argc_new,argv_test); | ||
125 | |||
126 | argv_test=(char **)malloc(7*sizeof(char **)); | ||
127 | argv_test[0] = "prog_name"; | ||
128 | argv_test[1] = "--arg1=val1"; | ||
129 | argv_test[2] = "--extra-opts=@./config-opts.ini"; | ||
130 | argv_test[3] = "--extra-opts"; | ||
131 | argv_test[4] = "sect1@./config-opts.ini"; | ||
132 | argv_test[5] = "--arg2"; | ||
133 | argv_test[6] = NULL; | ||
134 | argc_test=6; | ||
135 | argv_known=(char **)realloc(argv_known, 8*sizeof(char **)); | ||
136 | argv_known[0] = "prog_name"; | ||
137 | argv_known[1] = "--foo=Bar"; | ||
138 | argv_known[2] = "--this=Your Mother!"; | ||
139 | argv_known[3] = "--blank"; | ||
140 | argv_known[4] = "--one=two"; | ||
141 | argv_known[5] = "--arg1=val1"; | ||
142 | argv_known[6] = "--arg2"; | ||
143 | argv_known[7] = NULL; | ||
144 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
145 | ok(array_diff(argc_new, argv_test, 7, argv_known), "twice extra opts using two sections"); | ||
146 | my_free(&argc_new,argv_test); | ||
147 | |||
148 | /* Next three checks should die according to N::P - for now they're useful | ||
149 | * to test code is working properly (i.e. no srash or unexpected behavior) | ||
150 | */ | ||
151 | argv_test=(char **)malloc(6*sizeof(char **)); | ||
152 | argv_test[0] = "prog_name"; | ||
153 | argv_test[1] = "arg1"; | ||
154 | argv_test[2] = "--extra-opts=missing@./config-opts.ini"; | ||
155 | argv_test[3] = "--arg3"; | ||
156 | argv_test[4] = "val2"; | ||
157 | argv_test[5] = NULL; | ||
158 | argc_test=5; | ||
159 | argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); | ||
160 | argv_known[0] = "prog_name"; | ||
161 | argv_known[1] = "arg1"; | ||
162 | argv_known[2] = "--arg3"; | ||
163 | argv_known[3] = "val2"; | ||
164 | argv_known[4] = NULL; | ||
165 | argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new); | ||
166 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 1"); | ||
167 | my_free(&argc_new,argv_test); | ||
168 | |||
169 | argv_test=(char **)malloc(7*sizeof(char **)); | ||
170 | argv_test[0] = "prog_name"; | ||
171 | argv_test[1] = "arg1"; | ||
172 | argv_test[2] = "--extra-opts"; | ||
173 | argv_test[3] = "missing@./config-opts.ini"; | ||
174 | argv_test[4] = "--arg3"; | ||
175 | argv_test[5] = "val2"; | ||
176 | argv_test[6] = NULL; | ||
177 | argc_test=6; | ||
178 | argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); | ||
179 | argv_known[0] = "prog_name"; | ||
180 | argv_known[1] = "arg1"; | ||
181 | argv_known[2] = "--arg3"; | ||
182 | argv_known[3] = "val2"; | ||
183 | argv_known[4] = NULL; | ||
184 | argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new); | ||
185 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 2"); | ||
186 | my_free(&argc_new,argv_test); | ||
187 | |||
188 | argv_test=(char **)malloc(6*sizeof(char **)); | ||
189 | argv_test[0] = "prog_name"; | ||
190 | argv_test[1] = "arg1"; | ||
191 | argv_test[2] = "--extra-opts"; | ||
192 | argv_test[3] = "--arg3"; | ||
193 | argv_test[4] = "val2"; | ||
194 | argv_test[5] = NULL; | ||
195 | argc_test=5; | ||
196 | argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); | ||
197 | argv_known[0] = "prog_name"; | ||
198 | argv_known[1] = "arg1"; | ||
199 | argv_known[2] = "--arg3"; | ||
200 | argv_known[3] = "val2"; | ||
201 | argv_known[4] = NULL; | ||
202 | argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new); | ||
203 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 3"); | ||
204 | my_free(&argc_new,argv_test); | ||
205 | |||
206 | return exit_status(); | ||
207 | } | ||
208 | |||
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 @@ | |||
1 | #!/usr/bin/perl | ||
2 | use Test::More; | ||
3 | if (! -e "./test_opts") { | ||
4 | plan skip_all => "./test_opts not compiled - please install tap library to test"; | ||
5 | } | ||
6 | exec "./test_opts"; | ||