1 /*****************************************************************************
3 * Nagios-plugins extra_opts library
6 * Copyright (c) 2007 Nagios Plugins Development Team
8 * Last Modified: $Date: 2008-03-15 18:42:01 -0400 (Sat, 15 Mar 2008) $
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.
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.
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/>.
24 * $Id: parse_ini.c 1950 2008-03-15 22:42:01Z dermoth $
26 *****************************************************************************/
29 #include "utils_base.h"
30 #include "parse_ini.h"
31 #include "extra_opts.h"
33 /* FIXME: copied from utils.h; we should move a bunch of libs! */
35 is_option2 (char *str
)
39 else if (strspn (str
, "-") == 1 || strspn (str
, "-") == 2)
45 /* this is the externally visible function used by plugins */
46 char **np_extra_opts(int *argc
, char **argv
, const char *plugin_name
){
47 np_arg_list
*extra_args
=NULL
, *ea1
=NULL
, *ea_tmp
=NULL
;
50 int i
, j
, optfound
, argc_new
, ea_num
=*argc
;
53 /* No arguments provided */
57 for(i
=1; i
<*argc
; i
++){
61 /* Do we have an extra-opts parameter? */
62 if(strncmp(argv
[i
], "--extra-opts=", 13)==0){
63 /* It is a single argument with value */
65 /* Delete the extra opts argument */
66 for(j
=i
;j
<*argc
;j
++) argv
[j
]=argv
[j
+1];
69 }else if(strcmp(argv
[i
], "--extra-opts")==0){
70 if((i
+1<*argc
)&&!is_option2(argv
[i
+1])){
71 /* It is a argument with separate value */
73 /* Delete the extra-opts argument/value */
74 for(j
=i
;j
<*argc
-1;j
++) argv
[j
]=argv
[j
+2];
81 /* Delete the extra opts argument */
82 for(j
=i
;j
<*argc
;j
++) argv
[j
]=argv
[j
+1];
88 /* If we found extra-opts, expand them and store them for later*/
90 /* Process ini section, returning a linked list of arguments */
91 ea1
=np_get_defaults(argptr
, plugin_name
);
93 /* no extra args (empty section)? */
98 /* append the list to extra_args */
101 while(ea1
=ea1
->next
) ea_num
++;
104 while(ea_tmp
->next
) {
112 /* lather, rince, repeat */
115 if(ea_num
==*argc
&& extra_args
==NULL
){
120 /* done processing arguments. now create a new argv array... */
121 argv_new
=(char**)malloc((ea_num
+1)*sizeof(char**));
122 if(argv_new
==NULL
) die(STATE_UNKNOWN
, _("malloc() failed!\n"));
124 /* starting with program name */
127 /* then parsed ini opts (frying them up in the same run) */
129 argv_new
[argc_new
++]=extra_args
->arg
;
131 extra_args
=extra_args
->next
;
134 /* finally the rest of the argv array */
135 for (i
=1; i
<*argc
; i
++) argv_new
[argc_new
++]=argv
[i
];
138 argv_new
[argc_new
]=NULL
;