summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-03-30 14:02:13 (GMT)
committerThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-03-30 14:02:13 (GMT)
commit8aa5ec317af49d811b1c4b65c905cca81f845321 (patch)
tree8dbf98859c71ae6c056bf23901f0e63482d99144
parentdf93abdbaf6fe9efdf41cfa822c4d3a816830ddb (diff)
downloadmonitoring-plugins-8aa5ec317af49d811b1c4b65c905cca81f845321.tar.gz
- Remove the last argument of np_extra_opts
- Code cleanups and comments git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1967 f882894a-f735-0410-b71e-b25c423dba1c
-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 3a0ce04..2aeb77a 100644
--- a/lib/extra_opts.c
+++ b/lib/extra_opts.c
@@ -44,23 +44,18 @@ is_option (char *str)
44} 44}
45 45
46/* this is the externally visible function used by plugins */ 46/* this is the externally visible function used by plugins */
47/* Shouldn't se modify directly **argv (passed as a char ***) and argc 47char **np_extra_opts(int *argc, char **argv, const char *plugin_name){
48 * (as int *) ? 48 np_arg_list *extra_args=NULL, *ea1=NULL, *ea_tmp=NULL;
49 */
50char **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; 49 char **argv_new=NULL;
53 char *argptr=NULL; 50 char *argptr=NULL;
54 int i, j, optfound, ea_num=argc; 51 int i, j, optfound, argc_new, ea_num=*argc;
55 52
56 if(argc<2) { 53 if(*argc<2) {
57 /* No arguments provided */ 54 /* No arguments provided */
58 *argc_new=argc; 55 return argv;
59 argv_new=argv;
60 return argv_new;
61 } 56 }
62 57
63 for(i=1; i<argc; i++){ 58 for(i=1; i<*argc; i++){
64 argptr=NULL; 59 argptr=NULL;
65 optfound=0; 60 optfound=0;
66 61
@@ -69,86 +64,76 @@ char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_n
69 /* It is a single argument with value */ 64 /* It is a single argument with value */
70 argptr=argv[i]+13; 65 argptr=argv[i]+13;
71 /* Delete the extra opts argument */ 66 /* Delete the extra opts argument */
72 for(j=i;j<argc;j++) argv[j]=argv[j+1]; 67 for(j=i;j<*argc;j++) argv[j]=argv[j+1];
73 i--; 68 i--;
74 argc--; 69 *argc--;
75 }else if(strcmp(argv[i], "--extra-opts")==0){ 70 }else if(strcmp(argv[i], "--extra-opts")==0){
76 if(!is_option(argv[i+1])){ 71 if(!is_option(argv[i+1])){
77 /* It is a argument with separate value */ 72 /* It is a argument with separate value */
78 argptr=argv[i+1]; 73 argptr=argv[i+1];
79 /* Delete the extra-opts argument/value */ 74 /* Delete the extra-opts argument/value */
80 for(j=i;j<argc-1;j++) argv[j]=argv[j+2]; 75 for(j=i;j<*argc-1;j++) argv[j]=argv[j+2];
81 i-=2; 76 i-=2;
82 argc-=2; 77 *argc-=2;
83 ea_num--; 78 ea_num--;
84 }else{ 79 }else{
85 /* It has no value */ 80 /* It has no value */
86 optfound=1; 81 optfound=1;
87 /* Delete the extra opts argument */ 82 /* Delete the extra opts argument */
88 for(j=i;j<argc;j++) argv[j]=argv[j+1]; 83 for(j=i;j<*argc;j++) argv[j]=argv[j+1];
89 i--; 84 i--;
90 argc--; 85 *argc--;
91 } 86 }
92 } 87 }
93 88
89 /* If we found extra-opts, expand them and store them for later*/
94 if(argptr||optfound){ 90 if(argptr||optfound){
95 /* Process ini section, returning a linked list of arguments */ 91 /* Process ini section, returning a linked list of arguments */
96 ea_tmp1=np_get_defaults(argptr, plugin_name); 92 ea1=np_get_defaults(argptr, plugin_name);
97 if(ea_tmp1==NULL) { 93 if(ea1==NULL) {
98 /* no extra args? */ 94 /* no extra args (empty section)? */
99 ea_num--; 95 ea_num--;
100 continue; 96 continue;
101 } 97 }
102 98
103 /* append the list to extra_args */ 99 /* append the list to extra_args */
104 if(extra_args==NULL){ 100 if(extra_args==NULL){
105 extra_args=ea_tmp2=ea_tmp1; 101 extra_args=ea1;
106 while(ea_tmp2->next) { 102 while(ea1=ea1->next) ea_num++;
107 ea_tmp2=ea_tmp2->next;
108 ea_num++;
109 }
110 }else{ 103 }else{
111 ea_tmp2=extra_args; 104 ea_tmp=extra_args;
112 while(ea_tmp2->next) { 105 while(ea_tmp=ea_tmp->next) ea_num++;
113 ea_tmp2=ea_tmp2->next; 106 ea_tmp->next=ea1;
114 ea_num++;
115 }
116 ea_tmp2->next=ea_tmp1;
117 } 107 }
118 ea_tmp1=ea_tmp2=NULL; 108 ea1=ea_tmp=NULL;
119 } 109 }
120 /* lather, rince, repeat */ 110 /* lather, rince, repeat */
121 } 111 }
122 112
123 if(ea_num==argc && extra_args==NULL){ 113 if(ea_num==*argc && extra_args==NULL){
124 /* No extra-opts */ 114 /* No extra-opts */
125 *argc_new=argc; 115 return argv;
126 argv_new=argv;
127 return argv_new;
128 } 116 }
129 117
130 /* done processing arguments. now create a new argc/argv set... */ 118 /* done processing arguments. now create a new argv array... */
131 argv_new=(char**)malloc((ea_num+1)*sizeof(char**)); 119 argv_new=(char**)malloc((ea_num+1)*sizeof(char**));
132 if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); 120 if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
133 121
134 /* starting with program name (Should we strdup or just use the poiter?) */ 122 /* starting with program name */
135 argv_new[0]=strdup(argv[0]); 123 argv_new[0]=strdup(argv[0]);
136 *argc_new=1; 124 argc_new=1;
137 /* then parsed ini opts (frying them up in the same run) */ 125 /* then parsed ini opts (frying them up in the same run) */
138 while(extra_args){ 126 while(extra_args){
139 argv_new[*argc_new]=strdup(extra_args->arg); 127 argv_new[argc_new++]=extra_args->arg;
140 *argc_new+=1; 128 ea1=extra_args;
141 ea_tmp1=extra_args;
142 extra_args=extra_args->next; 129 extra_args=extra_args->next;
143 free(ea_tmp1); 130 free(ea1);
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 } 131 }
132 /* finally the rest of the argv array */
133 for (i=1; i<*argc; i++) argv_new[argc_new++]=strdup(argv[i]);
134 *argc=argc_new;
150 /* and terminate. */ 135 /* and terminate. */
151 argv_new[*argc_new]=NULL; 136 argv_new[argc_new]=NULL;
152 137
153 return argv_new; 138 return argv_new;
154} 139}
diff --git a/lib/extra_opts.h b/lib/extra_opts.h
index 5f89d2b..25bfc6d 100644
--- a/lib/extra_opts.h
+++ b/lib/extra_opts.h
@@ -7,9 +7,13 @@
7 */ 7 */
8 8
9/* np_extra_opts: Process the --extra-opts arguments and create a new argument 9/* np_extra_opts: Process the --extra-opts arguments and create a new argument
10 * array load the default configuration (if present) for 10 * array with ini-processed and argument-passed arguments together. The
11 * a plugin from the ini file 11 * ini-procesed arguments always come first (in the ord of --extra-opts
12 * arguments). If no --extra-opts arguments are provided or returned nothing
13 * it returns **argv otherwise the new array is returned. --extra-opts are
14 * always removed from **argv and the new array and all its elements can be
15 * freed with free();
12 */ 16 */
13char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_new); 17char **np_extra_opts(int *argc, char **argv, const char *plugin_name);
14 18
15#endif /* _EXTRA_OPTS_H_ */ 19#endif /* _EXTRA_OPTS_H_ */
diff --git a/lib/tests/test_opts.c b/lib/tests/test_opts.c
index 769a2df..7c173f3 100644
--- a/lib/tests/test_opts.c
+++ b/lib/tests/test_opts.c
@@ -57,7 +57,7 @@ int
57main (int argc, char **argv) 57main (int argc, char **argv)
58{ 58{
59 char **argv_test=NULL, **argv_known=NULL; 59 char **argv_test=NULL, **argv_known=NULL;
60 int i, argc_test, argc_new; 60 int i, argc_test;
61 61
62 plan_tests(11); 62 plan_tests(11);
63 63
@@ -68,9 +68,9 @@ main (int argc, char **argv)
68 argv_known=(char **)realloc(argv_known, 2*sizeof(char **)); 68 argv_known=(char **)realloc(argv_known, 2*sizeof(char **));
69 argv_known[0] = "prog_name"; 69 argv_known[0] = "prog_name";
70 argv_known[1] = NULL; 70 argv_known[1] = NULL;
71 argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); 71 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
72 ok(array_diff(argc_new, argv_test, 1, argv_known), "No opts, returns correct argv/argc"); 72 ok(array_diff(argc_test, argv_test, 1, argv_known), "No opts, returns correct argv/argc");
73 my_free(&argc_new, argv_test); 73 my_free(&argc_test, argv_test);
74 74
75 argv_test=(char **)malloc(6*sizeof(char **)); 75 argv_test=(char **)malloc(6*sizeof(char **));
76 argv_test[0] = "prog_name"; 76 argv_test[0] = "prog_name";
@@ -87,9 +87,9 @@ main (int argc, char **argv)
87 argv_known[3] = "--arg3"; 87 argv_known[3] = "--arg3";
88 argv_known[4] = "val2"; 88 argv_known[4] = "val2";
89 argv_known[5] = NULL; 89 argv_known[5] = NULL;
90 argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); 90 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
91 ok(array_diff(argc_new, argv_test, 5, argv_known), "No extra opts, verbatim copy of argv"); 91 ok(array_diff(argc_test, argv_test, 5, argv_known), "No extra opts, verbatim copy of argv");
92 my_free(&argc_new,argv_test); 92 my_free(&argc_test,argv_test);
93 93
94 argv_test=(char **)malloc(3*sizeof(char **)); 94 argv_test=(char **)malloc(3*sizeof(char **));
95 argv_test[0] = "prog_name"; 95 argv_test[0] = "prog_name";
@@ -102,9 +102,9 @@ main (int argc, char **argv)
102 argv_known[2] = "--this=Your Mother!"; 102 argv_known[2] = "--this=Your Mother!";
103 argv_known[3] = "--blank"; 103 argv_known[3] = "--blank";
104 argv_known[4] = NULL; 104 argv_known[4] = NULL;
105 argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); 105 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
106 ok(array_diff(argc_new, argv_test, 4, argv_known), "Only extra opts using default section"); 106 ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts using default section");
107 my_free(&argc_new,argv_test); 107 my_free(&argc_test,argv_test);
108 108
109 argv_test=(char **)malloc(5*sizeof(char **)); 109 argv_test=(char **)malloc(5*sizeof(char **));
110 argv_test[0] = "prog_name"; 110 argv_test[0] = "prog_name";
@@ -119,9 +119,9 @@ main (int argc, char **argv)
119 argv_known[2] = "--something else=oops"; 119 argv_known[2] = "--something else=oops";
120 argv_known[3] = "--this=that"; 120 argv_known[3] = "--this=that";
121 argv_known[4] = NULL; 121 argv_known[4] = NULL;
122 argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); 122 argv_test=np_extra_opts(argc_test, argv_test, "check_disk");
123 ok(array_diff(argc_new, argv_test, 4, argv_known), "Only extra opts specified twice"); 123 ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts specified twice");
124 my_free(&argc_new,argv_test); 124 my_free(&argc_test,argv_test);
125 125
126 argv_test=(char **)malloc(7*sizeof(char **)); 126 argv_test=(char **)malloc(7*sizeof(char **));
127 argv_test[0] = "prog_name"; 127 argv_test[0] = "prog_name";
@@ -141,9 +141,9 @@ main (int argc, char **argv)
141 argv_known[5] = "--arg1=val1"; 141 argv_known[5] = "--arg1=val1";
142 argv_known[6] = "--arg2"; 142 argv_known[6] = "--arg2";
143 argv_known[7] = NULL; 143 argv_known[7] = NULL;
144 argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); 144 argv_test=np_extra_opts(argc_test, argv_test, "check_disk");
145 ok(array_diff(argc_new, argv_test, 7, argv_known), "twice extra opts using two sections"); 145 ok(array_diff(argc_test, argv_test, 7, argv_known), "twice extra opts using two sections");
146 my_free(&argc_new,argv_test); 146 my_free(&argc_test,argv_test);
147 147
148 /* Next three checks should die according to N::P - for now they're useful 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) 149 * to test code is working properly (i.e. no srash or unexpected behavior)
@@ -162,9 +162,9 @@ main (int argc, char **argv)
162 argv_known[2] = "--arg3"; 162 argv_known[2] = "--arg3";
163 argv_known[3] = "val2"; 163 argv_known[3] = "val2";
164 argv_known[4] = NULL; 164 argv_known[4] = NULL;
165 argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new); 165 argv_test=np_extra_opts(argc_test, argv_test, "check_missing");
166 ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 1"); 166 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 1");
167 my_free(&argc_new,argv_test); 167 my_free(&argc_test,argv_test);
168 168
169 argv_test=(char **)malloc(7*sizeof(char **)); 169 argv_test=(char **)malloc(7*sizeof(char **));
170 argv_test[0] = "prog_name"; 170 argv_test[0] = "prog_name";
@@ -181,9 +181,9 @@ main (int argc, char **argv)
181 argv_known[2] = "--arg3"; 181 argv_known[2] = "--arg3";
182 argv_known[3] = "val2"; 182 argv_known[3] = "val2";
183 argv_known[4] = NULL; 183 argv_known[4] = NULL;
184 argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new); 184 argv_test=np_extra_opts(argc_test, argv_test, "check_missing");
185 ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 2"); 185 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 2");
186 my_free(&argc_new,argv_test); 186 my_free(&argc_test,argv_test);
187 187
188 argv_test=(char **)malloc(6*sizeof(char **)); 188 argv_test=(char **)malloc(6*sizeof(char **));
189 argv_test[0] = "prog_name"; 189 argv_test[0] = "prog_name";
@@ -199,9 +199,9 @@ main (int argc, char **argv)
199 argv_known[2] = "--arg3"; 199 argv_known[2] = "--arg3";
200 argv_known[3] = "val2"; 200 argv_known[3] = "val2";
201 argv_known[4] = NULL; 201 argv_known[4] = NULL;
202 argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new); 202 argv_test=np_extra_opts(argc_test, argv_test, "check_missing");
203 ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 3"); 203 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 3");
204 my_free(&argc_new,argv_test); 204 my_free(&argc_test,argv_test);
205 205
206 setenv("NAGIOS_CONFIG_PATH", ".", 1); 206 setenv("NAGIOS_CONFIG_PATH", ".", 1);
207 argv_test=(char **)malloc(6*sizeof(char **)); 207 argv_test=(char **)malloc(6*sizeof(char **));
@@ -219,9 +219,9 @@ main (int argc, char **argv)
219 argv_known[3] = "--arg3"; 219 argv_known[3] = "--arg3";
220 argv_known[4] = "val2"; 220 argv_known[4] = "val2";
221 argv_known[5] = NULL; 221 argv_known[5] = NULL;
222 argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); 222 argv_test=np_extra_opts(argc_test, argv_test, "check_disk");
223 ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 1"); 223 ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 1");
224 my_free(&argc_new,argv_test); 224 my_free(&argc_test,argv_test);
225 225
226 argv_test=(char **)malloc(6*sizeof(char **)); 226 argv_test=(char **)malloc(6*sizeof(char **));
227 argv_test[0] = "prog_name"; 227 argv_test[0] = "prog_name";
@@ -238,9 +238,9 @@ main (int argc, char **argv)
238 argv_known[3] = "--arg3"; 238 argv_known[3] = "--arg3";
239 argv_known[4] = "val2"; 239 argv_known[4] = "val2";
240 argv_known[5] = NULL; 240 argv_known[5] = NULL;
241 argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); 241 argv_test=np_extra_opts(argc_test, argv_test, "check_disk");
242 ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 2"); 242 ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 2");
243 my_free(&argc_new,argv_test); 243 my_free(&argc_test,argv_test);
244 244
245 argv_test=(char **)malloc(6*sizeof(char **)); 245 argv_test=(char **)malloc(6*sizeof(char **));
246 argv_test[0] = "prog_name"; 246 argv_test[0] = "prog_name";
@@ -257,9 +257,9 @@ main (int argc, char **argv)
257 argv_known[3] = "--arg3"; 257 argv_known[3] = "--arg3";
258 argv_known[4] = "val2"; 258 argv_known[4] = "val2";
259 argv_known[5] = NULL; 259 argv_known[5] = NULL;
260 argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); 260 argv_test=np_extra_opts(argc_test, argv_test, "check_disk");
261 ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 3"); 261 ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 3");
262 my_free(&argc_new,argv_test); 262 my_free(&argc_test,argv_test);
263 263
264 return exit_status(); 264 return exit_status();
265} 265}