summaryrefslogtreecommitdiffstats
path: root/lib/extra_opts.c
blob: 3a0ce045c9b40a5321d51e728c30e1d16084348f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*****************************************************************************
* 
* Nagios-plugins extra_opts library
* 
* License: GPL
* Copyright (c) 2007 Nagios Plugins Development Team
* 
* Last Modified: $Date: 2008-03-15 18:42:01 -0400 (Sat, 15 Mar 2008) $
* 
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
* $Id: parse_ini.c 1950 2008-03-15 22:42:01Z dermoth $
* 
*****************************************************************************/

#include "common.h"
#include "extra_opts.h"
#include "parse_ini.h"
#include "utils_base.h"
#include <ctype.h>

/* FIXME: copied from utils.h; we should move a bunch of libs! */
int
is_option (char *str)
{
	if (!str)
		return 0;
	else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
		return 1;
	else
		return 0;
}

/* this is the externally visible function used by plugins */
/* Shouldn't se modify directly **argv (passed as a char ***) and argc
 * (as int *) ?
 */
char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_new){
	np_arg_list *extra_args=NULL, *ea_tmp1=NULL, *ea_tmp2=NULL;
	char **argv_new=NULL;
	char *argptr=NULL;
	int i, j, optfound, ea_num=argc;

	if(argc<2) {
		/* No arguments provided */
		*argc_new=argc;
		argv_new=argv;
		return argv_new;
	}

	for(i=1; i<argc; i++){
		argptr=NULL;
		optfound=0;

		/* Do we have an extra-opts parameter? */
		if(strncmp(argv[i], "--extra-opts=", 13)==0){
			/* It is a single argument with value */
			argptr=argv[i]+13;
			/* Delete the extra opts argument */
			for(j=i;j<argc;j++) argv[j]=argv[j+1];
			i--;
			argc--;
		}else if(strcmp(argv[i], "--extra-opts")==0){
			if(!is_option(argv[i+1])){
				/* It is a argument with separate value */
				argptr=argv[i+1];
				/* Delete the extra-opts argument/value */
				for(j=i;j<argc-1;j++) argv[j]=argv[j+2];
				i-=2;
				argc-=2;
				ea_num--;
			}else{
				/* It has no value */
				optfound=1;
				/* Delete the extra opts argument */
				for(j=i;j<argc;j++) argv[j]=argv[j+1];
				i--;
				argc--;
			}
		}

		if(argptr||optfound){
			/* Process ini section, returning a linked list of arguments */
			ea_tmp1=np_get_defaults(argptr, plugin_name);
			if(ea_tmp1==NULL) {
				/* no extra args? */
				ea_num--;
				continue;
			}

			/* append the list to extra_args */
			if(extra_args==NULL){
				extra_args=ea_tmp2=ea_tmp1;
				while(ea_tmp2->next) {
					ea_tmp2=ea_tmp2->next;
					ea_num++;
				}
			}else{
				ea_tmp2=extra_args;
				while(ea_tmp2->next) {
					ea_tmp2=ea_tmp2->next;
					ea_num++;
				}
				ea_tmp2->next=ea_tmp1;
			}
			ea_tmp1=ea_tmp2=NULL;
		}
		/* lather, rince, repeat */
	}

	if(ea_num==argc && extra_args==NULL){
		/* No extra-opts */
		*argc_new=argc;
		argv_new=argv;
		return argv_new;
	}

	/* done processing arguments. now create a new argc/argv set... */
	argv_new=(char**)malloc((ea_num+1)*sizeof(char**));
	if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));

	/* starting with program name (Should we strdup or just use the poiter?) */
	argv_new[0]=strdup(argv[0]);
	*argc_new=1;
	/* then parsed ini opts (frying them up in the same run) */
	while(extra_args){
		argv_new[*argc_new]=strdup(extra_args->arg);
		*argc_new+=1;
		ea_tmp1=extra_args;
		extra_args=extra_args->next;
		free(ea_tmp1);
	}
	/* finally the rest of the argv array (Should we strdup or just use the poiter?) */
	for (i=1; i<argc; i++){
		argv_new[*argc_new]=strdup(argv[i]);
		*argc_new+=1;
	}
	/* and terminate. */
	argv_new[*argc_new]=NULL;

	return argv_new;
}