Addition to help to state that -f will always return OK if ssh command
[monitoring-plugins.git] / lib / extra_opts.c
blobb555417a834a4bf9cd1eb5e94042f4a846447899
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 *
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 *****************************************************************************/
28 #include "common.h"
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! */
34 int
35 is_option2 (char *str)
37 if (!str)
38 return FALSE;
39 else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
40 return TRUE;
41 else
42 return FALSE;
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;
48 char **argv_new=NULL;
49 char *argptr=NULL;
50 int i, j, optfound, argc_new, ea_num=*argc;
52 if(*argc<2) {
53 /* No arguments provided */
54 return argv;
57 for(i=1; i<*argc; i++){
58 argptr=NULL;
59 optfound=0;
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 */
64 argptr=argv[i]+13;
65 /* Delete the extra opts argument */
66 for(j=i;j<*argc;j++) argv[j]=argv[j+1];
67 i--;
68 *argc-=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 */
72 argptr=argv[i+1];
73 /* Delete the extra-opts argument/value */
74 for(j=i;j<*argc-1;j++) argv[j]=argv[j+2];
75 i-=2;
76 *argc-=2;
77 ea_num--;
78 }else{
79 /* It has no value */
80 optfound=1;
81 /* Delete the extra opts argument */
82 for(j=i;j<*argc;j++) argv[j]=argv[j+1];
83 i--;
84 *argc-=1;
88 /* If we found extra-opts, expand them and store them for later*/
89 if(argptr||optfound){
90 /* Process ini section, returning a linked list of arguments */
91 ea1=np_get_defaults(argptr, plugin_name);
92 if(ea1==NULL) {
93 /* no extra args (empty section)? */
94 ea_num--;
95 continue;
98 /* append the list to extra_args */
99 if(extra_args==NULL){
100 extra_args=ea1;
101 while(ea1=ea1->next) ea_num++;
102 }else{
103 ea_tmp=extra_args;
104 while(ea_tmp->next) {
105 ea_tmp=ea_tmp->next;
106 ea_num++;
108 ea_tmp->next=ea1;
110 ea1=ea_tmp=NULL;
112 /* lather, rince, repeat */
115 if(ea_num==*argc && extra_args==NULL){
116 /* No extra-opts */
117 return argv;
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 */
125 argv_new[0]=argv[0];
126 argc_new=1;
127 /* then parsed ini opts (frying them up in the same run) */
128 while(extra_args){
129 argv_new[argc_new++]=extra_args->arg;
130 ea1=extra_args;
131 extra_args=extra_args->next;
132 free(ea1);
134 /* finally the rest of the argv array */
135 for (i=1; i<*argc; i++) argv_new[argc_new++]=argv[i];
136 *argc=argc_new;
137 /* and terminate. */
138 argv_new[argc_new]=NULL;
140 return argv_new;