diff options
Diffstat (limited to 'lib/extra_opts.c')
-rw-r--r-- | lib/extra_opts.c | 149 |
1 files changed, 77 insertions, 72 deletions
diff --git a/lib/extra_opts.c b/lib/extra_opts.c index 771621d..82460e9 100644 --- a/lib/extra_opts.c +++ b/lib/extra_opts.c | |||
@@ -1,24 +1,24 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring Plugins extra_opts library | 3 | * Monitoring Plugins extra_opts library |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2007 Monitoring Plugins Development Team | 6 | * Copyright (c) 2007 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * This program is free software: you can redistribute it and/or modify | 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 | 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 | 10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. | 11 | * (at your option) any later version. |
12 | * | 12 | * |
13 | * This program is distributed in the hope that it will be useful, | 13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. | 16 | * GNU General Public License for more details. |
17 | * | 17 | * |
18 | * You should have received a copy of the GNU General Public License | 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/>. | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * | 20 | * |
21 | *****************************************************************************/ | 21 | *****************************************************************************/ |
22 | 22 | ||
23 | #include "common.h" | 23 | #include "common.h" |
24 | #include "utils_base.h" | 24 | #include "utils_base.h" |
@@ -26,110 +26,115 @@ | |||
26 | #include "extra_opts.h" | 26 | #include "extra_opts.h" |
27 | 27 | ||
28 | /* FIXME: copied from utils.h; we should move a bunch of libs! */ | 28 | /* FIXME: copied from utils.h; we should move a bunch of libs! */ |
29 | bool is_option2 (char *str) | 29 | bool is_option2(char *str) { |
30 | { | ||
31 | if (!str) | 30 | if (!str) |
32 | return false; | 31 | return false; |
33 | else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) | 32 | else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) |
34 | return true; | 33 | return true; |
35 | else | 34 | else |
36 | return false; | 35 | return false; |
37 | } | 36 | } |
38 | 37 | ||
39 | /* this is the externally visible function used by plugins */ | 38 | /* this is the externally visible function used by plugins */ |
40 | char **np_extra_opts(int *argc, char **argv, const char *plugin_name){ | 39 | char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { |
41 | np_arg_list *extra_args=NULL, *ea1=NULL, *ea_tmp=NULL; | 40 | np_arg_list *extra_args = NULL, *ea1 = NULL, *ea_tmp = NULL; |
42 | char **argv_new=NULL; | 41 | char **argv_new = NULL; |
43 | char *argptr=NULL; | 42 | char *argptr = NULL; |
44 | int i, j, optfound, argc_new, ea_num=*argc; | 43 | int i, j, optfound, argc_new, ea_num = *argc; |
45 | 44 | ||
46 | if(*argc<2) { | 45 | if (*argc < 2) { |
47 | /* No arguments provided */ | 46 | /* No arguments provided */ |
48 | return argv; | 47 | return argv; |
49 | } | 48 | } |
50 | 49 | ||
51 | for(i=1; i<*argc; i++){ | 50 | for (i = 1; i < *argc; i++) { |
52 | argptr=NULL; | 51 | argptr = NULL; |
53 | optfound=0; | 52 | optfound = 0; |
54 | 53 | ||
55 | /* Do we have an extra-opts parameter? */ | 54 | /* Do we have an extra-opts parameter? */ |
56 | if(strncmp(argv[i], "--extra-opts=", 13)==0){ | 55 | if (strncmp(argv[i], "--extra-opts=", 13) == 0) { |
57 | /* It is a single argument with value */ | 56 | /* It is a single argument with value */ |
58 | argptr=argv[i]+13; | 57 | argptr = argv[i] + 13; |
59 | /* Delete the extra opts argument */ | 58 | /* Delete the extra opts argument */ |
60 | for(j=i;j<*argc;j++) argv[j]=argv[j+1]; | 59 | for (j = i; j < *argc; j++) |
60 | argv[j] = argv[j + 1]; | ||
61 | i--; | 61 | i--; |
62 | *argc-=1; | 62 | *argc -= 1; |
63 | }else if(strcmp(argv[i], "--extra-opts")==0){ | 63 | } else if (strcmp(argv[i], "--extra-opts") == 0) { |
64 | if((i+1<*argc)&&!is_option2(argv[i+1])){ | 64 | if ((i + 1 < *argc) && !is_option2(argv[i + 1])) { |
65 | /* It is a argument with separate value */ | 65 | /* It is a argument with separate value */ |
66 | argptr=argv[i+1]; | 66 | argptr = argv[i + 1]; |
67 | /* Delete the extra-opts argument/value */ | 67 | /* Delete the extra-opts argument/value */ |
68 | for(j=i;j<*argc-1;j++) argv[j]=argv[j+2]; | 68 | for (j = i; j < *argc - 1; j++) |
69 | i-=2; | 69 | argv[j] = argv[j + 2]; |
70 | *argc-=2; | 70 | i -= 2; |
71 | *argc -= 2; | ||
71 | ea_num--; | 72 | ea_num--; |
72 | }else{ | 73 | } else { |
73 | /* It has no value */ | 74 | /* It has no value */ |
74 | optfound=1; | 75 | optfound = 1; |
75 | /* Delete the extra opts argument */ | 76 | /* Delete the extra opts argument */ |
76 | for(j=i;j<*argc;j++) argv[j]=argv[j+1]; | 77 | for (j = i; j < *argc; j++) |
78 | argv[j] = argv[j + 1]; | ||
77 | i--; | 79 | i--; |
78 | *argc-=1; | 80 | *argc -= 1; |
79 | } | 81 | } |
80 | } | 82 | } |
81 | 83 | ||
82 | /* If we found extra-opts, expand them and store them for later*/ | 84 | /* If we found extra-opts, expand them and store them for later*/ |
83 | if(argptr||optfound){ | 85 | if (argptr || optfound) { |
84 | /* Process ini section, returning a linked list of arguments */ | 86 | /* Process ini section, returning a linked list of arguments */ |
85 | ea1=np_get_defaults(argptr, plugin_name); | 87 | ea1 = np_get_defaults(argptr, plugin_name); |
86 | if(ea1==NULL) { | 88 | if (ea1 == NULL) { |
87 | /* no extra args (empty section)? */ | 89 | /* no extra args (empty section)? */ |
88 | ea_num--; | 90 | ea_num--; |
89 | continue; | 91 | continue; |
90 | } | 92 | } |
91 | 93 | ||
92 | /* append the list to extra_args */ | 94 | /* append the list to extra_args */ |
93 | if(extra_args==NULL){ | 95 | if (extra_args == NULL) { |
94 | extra_args=ea1; | 96 | extra_args = ea1; |
95 | while((ea1 = ea1->next)) ea_num++; | 97 | while ((ea1 = ea1->next)) |
96 | }else{ | 98 | ea_num++; |
97 | ea_tmp=extra_args; | 99 | } else { |
98 | while(ea_tmp->next) { | 100 | ea_tmp = extra_args; |
99 | ea_tmp=ea_tmp->next; | 101 | while (ea_tmp->next) { |
102 | ea_tmp = ea_tmp->next; | ||
100 | } | 103 | } |
101 | ea_tmp->next=ea1; | 104 | ea_tmp->next = ea1; |
102 | while((ea1 = ea1->next)) ea_num++; | 105 | while ((ea1 = ea1->next)) |
106 | ea_num++; | ||
103 | } | 107 | } |
104 | ea1=ea_tmp=NULL; | 108 | ea1 = ea_tmp = NULL; |
105 | } | 109 | } |
106 | } /* lather, rince, repeat */ | 110 | } /* lather, rince, repeat */ |
107 | 111 | ||
108 | if(ea_num==*argc && extra_args==NULL){ | 112 | if (ea_num == *argc && extra_args == NULL) { |
109 | /* No extra-opts */ | 113 | /* No extra-opts */ |
110 | return argv; | 114 | return argv; |
111 | } | 115 | } |
112 | 116 | ||
113 | /* done processing arguments. now create a new argv array... */ | 117 | /* done processing arguments. now create a new argv array... */ |
114 | argv_new=(char**)malloc((ea_num+1)*sizeof(char**)); | 118 | argv_new = (char **)malloc((ea_num + 1) * sizeof(char **)); |
115 | if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); | 119 | if (argv_new == NULL) |
120 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
116 | 121 | ||
117 | /* starting with program name */ | 122 | /* starting with program name */ |
118 | argv_new[0]=argv[0]; | 123 | argv_new[0] = argv[0]; |
119 | argc_new=1; | 124 | argc_new = 1; |
120 | /* then parsed ini opts (frying them up in the same run) */ | 125 | /* then parsed ini opts (frying them up in the same run) */ |
121 | while(extra_args){ | 126 | while (extra_args) { |
122 | argv_new[argc_new++]=extra_args->arg; | 127 | argv_new[argc_new++] = extra_args->arg; |
123 | ea1=extra_args; | 128 | ea1 = extra_args; |
124 | extra_args=extra_args->next; | 129 | extra_args = extra_args->next; |
125 | free(ea1); | 130 | free(ea1); |
126 | } | 131 | } |
127 | /* finally the rest of the argv array */ | 132 | /* finally the rest of the argv array */ |
128 | for (i=1; i<*argc; i++) argv_new[argc_new++]=argv[i]; | 133 | for (i = 1; i < *argc; i++) |
129 | *argc=argc_new; | 134 | argv_new[argc_new++] = argv[i]; |
135 | *argc = argc_new; | ||
130 | /* and terminate. */ | 136 | /* and terminate. */ |
131 | argv_new[argc_new]=NULL; | 137 | argv_new[argc_new] = NULL; |
132 | 138 | ||
133 | return argv_new; | 139 | return argv_new; |
134 | } | 140 | } |
135 | |||