1 /*****************************************************************************
3 * Monitoring Plugins extra_opts library
6 * Copyright (c) 2007 Monitoring Plugins Development Team
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *****************************************************************************/
24 #include "utils_base.h"
25 #include "parse_ini.h"
26 #include "extra_opts.h"
28 /* FIXME: copied from utils.h; we should move a bunch of libs! */
30 is_option2 (char *str
)
34 else if (strspn (str
, "-") == 1 || strspn (str
, "-") == 2)
40 /* this is the externally visible function used by plugins */
41 char **np_extra_opts(int *argc
, char **argv
, const char *plugin_name
){
42 np_arg_list
*extra_args
=NULL
, *ea1
=NULL
, *ea_tmp
=NULL
;
45 int i
, j
, optfound
, argc_new
, ea_num
=*argc
;
48 /* No arguments provided */
52 for(i
=1; i
<*argc
; i
++){
56 /* Do we have an extra-opts parameter? */
57 if(strncmp(argv
[i
], "--extra-opts=", 13)==0){
58 /* It is a single argument with value */
60 /* Delete the extra opts argument */
61 for(j
=i
;j
<*argc
;j
++) argv
[j
]=argv
[j
+1];
64 }else if(strcmp(argv
[i
], "--extra-opts")==0){
65 if((i
+1<*argc
)&&!is_option2(argv
[i
+1])){
66 /* It is a argument with separate value */
68 /* Delete the extra-opts argument/value */
69 for(j
=i
;j
<*argc
-1;j
++) argv
[j
]=argv
[j
+2];
76 /* Delete the extra opts argument */
77 for(j
=i
;j
<*argc
;j
++) argv
[j
]=argv
[j
+1];
83 /* If we found extra-opts, expand them and store them for later*/
85 /* Process ini section, returning a linked list of arguments */
86 ea1
=np_get_defaults(argptr
, plugin_name
);
88 /* no extra args (empty section)? */
93 /* append the list to extra_args */
96 while(ea1
=ea1
->next
) ea_num
++;
103 while(ea1
=ea1
->next
) ea_num
++;
107 } /* lather, rince, repeat */
109 if(ea_num
==*argc
&& extra_args
==NULL
){
114 /* done processing arguments. now create a new argv array... */
115 argv_new
=(char**)malloc((ea_num
+1)*sizeof(char**));
116 if(argv_new
==NULL
) die(STATE_UNKNOWN
, _("malloc() failed!\n"));
118 /* starting with program name */
121 /* then parsed ini opts (frying them up in the same run) */
123 argv_new
[argc_new
++]=extra_args
->arg
;
125 extra_args
=extra_args
->next
;
128 /* finally the rest of the argv array */
129 for (i
=1; i
<*argc
; i
++) argv_new
[argc_new
++]=argv
[i
];
132 argv_new
[argc_new
]=NULL
;