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 *
10 *
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.
15 *
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.
20 *
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/>.
23 *
24 * $Id: parse_ini.c 1950 2008-03-15 22:42:01Z dermoth $
25 *
26 *****************************************************************************/
28 #include "common.h"
29 #include "extra_opts.h"
30 #include "parse_ini.h"
31 #include "utils_base.h"
32 #include <ctype.h>
34 /* FIXME: copied from utils.h; we should move a bunch of libs! */
35 int
36 is_option (char *str)
37 {
38 if (!str)
39 return 0;
40 else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
41 return 1;
42 else
43 return 0;
44 }
46 /* this is the externally visible function used by plugins */
47 char **np_extra_opts(int *argc, char **argv, const char *plugin_name){
48 np_arg_list *extra_args=NULL, *ea1=NULL, *ea_tmp=NULL;
49 char **argv_new=NULL;
50 char *argptr=NULL;
51 int i, j, optfound, argc_new, ea_num=*argc;
53 if(*argc<2) {
54 /* No arguments provided */
55 return argv;
56 }
58 for(i=1; i<*argc; i++){
59 argptr=NULL;
60 optfound=0;
62 /* Do we have an extra-opts parameter? */
63 if(strncmp(argv[i], "--extra-opts=", 13)==0){
64 /* It is a single argument with value */
65 argptr=argv[i]+13;
66 /* Delete the extra opts argument */
67 for(j=i;j<*argc;j++) argv[j]=argv[j+1];
68 i--;
69 *argc--;
70 }else if(strcmp(argv[i], "--extra-opts")==0){
71 if(!is_option(argv[i+1])){
72 /* It is a argument with separate value */
73 argptr=argv[i+1];
74 /* Delete the extra-opts argument/value */
75 for(j=i;j<*argc-1;j++) argv[j]=argv[j+2];
76 i-=2;
77 *argc-=2;
78 ea_num--;
79 }else{
80 /* It has no value */
81 optfound=1;
82 /* Delete the extra opts argument */
83 for(j=i;j<*argc;j++) argv[j]=argv[j+1];
84 i--;
85 *argc--;
86 }
87 }
89 /* If we found extra-opts, expand them and store them for later*/
90 if(argptr||optfound){
91 /* Process ini section, returning a linked list of arguments */
92 ea1=np_get_defaults(argptr, plugin_name);
93 if(ea1==NULL) {
94 /* no extra args (empty section)? */
95 ea_num--;
96 continue;
97 }
99 /* append the list to extra_args */
100 if(extra_args==NULL){
101 extra_args=ea1;
102 while(ea1=ea1->next) ea_num++;
103 }else{
104 ea_tmp=extra_args;
105 while(ea_tmp=ea_tmp->next) ea_num++;
106 ea_tmp->next=ea1;
107 }
108 ea1=ea_tmp=NULL;
109 }
110 /* lather, rince, repeat */
111 }
113 if(ea_num==*argc && extra_args==NULL){
114 /* No extra-opts */
115 return argv;
116 }
118 /* done processing arguments. now create a new argv array... */
119 argv_new=(char**)malloc((ea_num+1)*sizeof(char**));
120 if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
122 /* starting with program name */
123 argv_new[0]=strdup(argv[0]);
124 argc_new=1;
125 /* then parsed ini opts (frying them up in the same run) */
126 while(extra_args){
127 argv_new[argc_new++]=extra_args->arg;
128 ea1=extra_args;
129 extra_args=extra_args->next;
130 free(ea1);
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;
135 /* and terminate. */
136 argv_new[argc_new]=NULL;
138 return argv_new;
139 }