Whitespace changes only
[monitoring-plugins.git] / lib / extra_opts.c
blob2939c7a14e1ad80364c8a822664f4ae41b8b4ee3
1 /*****************************************************************************
2 *
3 * Nagios-plugins extra_opts library
4 *
5 * License: GPL
6 * Copyright (c) 2007 Nagios Plugins Development Team
7 *
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 *****************************************************************************/
23 #include "common.h"
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! */
29 int
30 is_option2 (char *str)
32 if (!str)
33 return FALSE;
34 else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
35 return TRUE;
36 else
37 return FALSE;
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;
43 char **argv_new=NULL;
44 char *argptr=NULL;
45 int i, j, optfound, argc_new, ea_num=*argc;
47 if(*argc<2) {
48 /* No arguments provided */
49 return argv;
52 for(i=1; i<*argc; i++){
53 argptr=NULL;
54 optfound=0;
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 */
59 argptr=argv[i]+13;
60 /* Delete the extra opts argument */
61 for(j=i;j<*argc;j++) argv[j]=argv[j+1];
62 i--;
63 *argc-=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 */
67 argptr=argv[i+1];
68 /* Delete the extra-opts argument/value */
69 for(j=i;j<*argc-1;j++) argv[j]=argv[j+2];
70 i-=2;
71 *argc-=2;
72 ea_num--;
73 }else{
74 /* It has no value */
75 optfound=1;
76 /* Delete the extra opts argument */
77 for(j=i;j<*argc;j++) argv[j]=argv[j+1];
78 i--;
79 *argc-=1;
83 /* If we found extra-opts, expand them and store them for later*/
84 if(argptr||optfound){
85 /* Process ini section, returning a linked list of arguments */
86 ea1=np_get_defaults(argptr, plugin_name);
87 if(ea1==NULL) {
88 /* no extra args (empty section)? */
89 ea_num--;
90 continue;
93 /* append the list to extra_args */
94 if(extra_args==NULL){
95 extra_args=ea1;
96 while(ea1=ea1->next) ea_num++;
97 }else{
98 ea_tmp=extra_args;
99 while(ea_tmp->next) {
100 ea_tmp=ea_tmp->next;
102 ea_tmp->next=ea1;
103 while(ea1=ea1->next) ea_num++;
105 ea1=ea_tmp=NULL;
107 } /* lather, rince, repeat */
109 if(ea_num==*argc && extra_args==NULL){
110 /* No extra-opts */
111 return argv;
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 */
119 argv_new[0]=argv[0];
120 argc_new=1;
121 /* then parsed ini opts (frying them up in the same run) */
122 while(extra_args){
123 argv_new[argc_new++]=extra_args->arg;
124 ea1=extra_args;
125 extra_args=extra_args->next;
126 free(ea1);
128 /* finally the rest of the argv array */
129 for (i=1; i<*argc; i++) argv_new[argc_new++]=argv[i];
130 *argc=argc_new;
131 /* and terminate. */
132 argv_new[argc_new]=NULL;
134 return argv_new;