diff options
Diffstat (limited to 'lib/extra_opts.c')
| -rw-r--r-- | lib/extra_opts.c | 172 |
1 files changed, 96 insertions, 76 deletions
diff --git a/lib/extra_opts.c b/lib/extra_opts.c index 771621d8..3fe69014 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 - 2024 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,130 @@ | |||
| 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 | { | 30 | if (!str) { |
| 31 | if (!str) | ||
| 32 | return false; | 31 | return false; |
| 33 | else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) | 32 | } |
| 33 | |||
| 34 | if (strspn(str, "-") == 1 || strspn(str, "-") == 2) { | ||
| 34 | return true; | 35 | return true; |
| 35 | else | 36 | } |
| 36 | return false; | 37 | |
| 38 | return false; | ||
| 37 | } | 39 | } |
| 38 | 40 | ||
| 39 | /* this is the externally visible function used by plugins */ | 41 | /* this is the externally visible function used by plugins */ |
| 40 | char **np_extra_opts(int *argc, char **argv, const char *plugin_name){ | 42 | char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { |
| 41 | np_arg_list *extra_args=NULL, *ea1=NULL, *ea_tmp=NULL; | 43 | if (*argc < 2) { |
| 42 | char **argv_new=NULL; | ||
| 43 | char *argptr=NULL; | ||
| 44 | int i, j, optfound, argc_new, ea_num=*argc; | ||
| 45 | |||
| 46 | if(*argc<2) { | ||
| 47 | /* No arguments provided */ | 44 | /* No arguments provided */ |
| 48 | return argv; | 45 | return argv; |
| 49 | } | 46 | } |
| 50 | 47 | ||
| 51 | for(i=1; i<*argc; i++){ | 48 | np_arg_list *extra_args = NULL; |
| 52 | argptr=NULL; | 49 | np_arg_list *ea1 = NULL; |
| 53 | optfound=0; | 50 | np_arg_list *ea_tmp = NULL; |
| 51 | char *argptr = NULL; | ||
| 52 | int optfound; | ||
| 53 | size_t ea_num = (size_t)*argc; | ||
| 54 | |||
| 55 | for (int i = 1; i < *argc; i++) { | ||
| 56 | argptr = NULL; | ||
| 57 | optfound = 0; | ||
| 54 | 58 | ||
| 55 | /* Do we have an extra-opts parameter? */ | 59 | /* Do we have an extra-opts parameter? */ |
| 56 | if(strncmp(argv[i], "--extra-opts=", 13)==0){ | 60 | if (strncmp(argv[i], "--extra-opts=", 13) == 0) { |
| 57 | /* It is a single argument with value */ | 61 | /* It is a single argument with value */ |
| 58 | argptr=argv[i]+13; | 62 | argptr = argv[i] + 13; |
| 59 | /* Delete the extra opts argument */ | 63 | /* Delete the extra opts argument */ |
| 60 | for(j=i;j<*argc;j++) argv[j]=argv[j+1]; | 64 | for (int j = i; j < *argc; j++) { |
| 65 | argv[j] = argv[j + 1]; | ||
| 66 | } | ||
| 67 | |||
| 61 | i--; | 68 | i--; |
| 62 | *argc-=1; | 69 | *argc -= 1; |
| 63 | }else if(strcmp(argv[i], "--extra-opts")==0){ | 70 | } else if (strcmp(argv[i], "--extra-opts") == 0) { |
| 64 | if((i+1<*argc)&&!is_option2(argv[i+1])){ | 71 | if ((i + 1 < *argc) && !is_option2(argv[i + 1])) { |
| 65 | /* It is a argument with separate value */ | 72 | /* It is a argument with separate value */ |
| 66 | argptr=argv[i+1]; | 73 | argptr = argv[i + 1]; |
| 67 | /* Delete the extra-opts argument/value */ | 74 | /* Delete the extra-opts argument/value */ |
| 68 | for(j=i;j<*argc-1;j++) argv[j]=argv[j+2]; | 75 | for (int j = i; j < *argc - 1; j++) { |
| 69 | i-=2; | 76 | argv[j] = argv[j + 2]; |
| 70 | *argc-=2; | 77 | } |
| 78 | |||
| 79 | i -= 2; | ||
| 80 | *argc -= 2; | ||
| 71 | ea_num--; | 81 | ea_num--; |
| 72 | }else{ | 82 | } else { |
| 73 | /* It has no value */ | 83 | /* It has no value */ |
| 74 | optfound=1; | 84 | optfound = 1; |
| 75 | /* Delete the extra opts argument */ | 85 | /* Delete the extra opts argument */ |
| 76 | for(j=i;j<*argc;j++) argv[j]=argv[j+1]; | 86 | for (int j = i; j < *argc; j++) { |
| 87 | argv[j] = argv[j + 1]; | ||
| 88 | } | ||
| 89 | |||
| 77 | i--; | 90 | i--; |
| 78 | *argc-=1; | 91 | *argc -= 1; |
| 79 | } | 92 | } |
| 80 | } | 93 | } |
| 81 | 94 | ||
| 82 | /* If we found extra-opts, expand them and store them for later*/ | 95 | /* If we found extra-opts, expand them and store them for later*/ |
| 83 | if(argptr||optfound){ | 96 | if (argptr || optfound) { |
| 84 | /* Process ini section, returning a linked list of arguments */ | 97 | /* Process ini section, returning a linked list of arguments */ |
| 85 | ea1=np_get_defaults(argptr, plugin_name); | 98 | ea1 = np_get_defaults(argptr, plugin_name); |
| 86 | if(ea1==NULL) { | 99 | if (ea1 == NULL) { |
| 87 | /* no extra args (empty section)? */ | 100 | /* no extra args (empty section)? */ |
| 88 | ea_num--; | 101 | ea_num--; |
| 89 | continue; | 102 | continue; |
| 90 | } | 103 | } |
| 91 | 104 | ||
| 92 | /* append the list to extra_args */ | 105 | /* append the list to extra_args */ |
| 93 | if(extra_args==NULL){ | 106 | if (extra_args == NULL) { |
| 94 | extra_args=ea1; | 107 | extra_args = ea1; |
| 95 | while((ea1 = ea1->next)) ea_num++; | 108 | while ((ea1 = ea1->next)) { |
| 96 | }else{ | 109 | ea_num++; |
| 97 | ea_tmp=extra_args; | 110 | } |
| 98 | while(ea_tmp->next) { | 111 | } else { |
| 99 | ea_tmp=ea_tmp->next; | 112 | ea_tmp = extra_args; |
| 113 | while (ea_tmp->next) { | ||
| 114 | ea_tmp = ea_tmp->next; | ||
| 115 | } | ||
| 116 | ea_tmp->next = ea1; | ||
| 117 | while ((ea1 = ea1->next)) { | ||
| 118 | ea_num++; | ||
| 100 | } | 119 | } |
| 101 | ea_tmp->next=ea1; | ||
| 102 | while((ea1 = ea1->next)) ea_num++; | ||
| 103 | } | 120 | } |
| 104 | ea1=ea_tmp=NULL; | 121 | ea1 = ea_tmp = NULL; |
| 105 | } | 122 | } |
| 106 | } /* lather, rince, repeat */ | 123 | } /* lather, rince, repeat */ |
| 107 | 124 | ||
| 108 | if(ea_num==*argc && extra_args==NULL){ | 125 | if (ea_num == (size_t)*argc && extra_args == NULL) { |
| 109 | /* No extra-opts */ | 126 | /* No extra-opts */ |
| 110 | return argv; | 127 | return argv; |
| 111 | } | 128 | } |
| 112 | 129 | ||
| 113 | /* done processing arguments. now create a new argv array... */ | 130 | /* done processing arguments. now create a new argv array... */ |
| 114 | argv_new=(char**)malloc((ea_num+1)*sizeof(char**)); | 131 | char **argv_new = (char **)malloc((ea_num + 1) * sizeof(char **)); |
| 115 | if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); | 132 | if (argv_new == NULL) { |
| 133 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
| 134 | } | ||
| 116 | 135 | ||
| 117 | /* starting with program name */ | 136 | /* starting with program name */ |
| 118 | argv_new[0]=argv[0]; | 137 | argv_new[0] = argv[0]; |
| 119 | argc_new=1; | 138 | int argc_new = 1; |
| 120 | /* then parsed ini opts (frying them up in the same run) */ | 139 | /* then parsed ini opts (frying them up in the same run) */ |
| 121 | while(extra_args){ | 140 | while (extra_args) { |
| 122 | argv_new[argc_new++]=extra_args->arg; | 141 | argv_new[argc_new++] = extra_args->arg; |
| 123 | ea1=extra_args; | 142 | ea1 = extra_args; |
| 124 | extra_args=extra_args->next; | 143 | extra_args = extra_args->next; |
| 125 | free(ea1); | 144 | free(ea1); |
| 126 | } | 145 | } |
| 127 | /* finally the rest of the argv array */ | 146 | /* finally the rest of the argv array */ |
| 128 | for (i=1; i<*argc; i++) argv_new[argc_new++]=argv[i]; | 147 | for (int i = 1; i < *argc; i++) { |
| 129 | *argc=argc_new; | 148 | argv_new[argc_new++] = argv[i]; |
| 149 | } | ||
| 150 | *argc = argc_new; | ||
| 130 | /* and terminate. */ | 151 | /* and terminate. */ |
| 131 | argv_new[argc_new]=NULL; | 152 | argv_new[argc_new] = NULL; |
| 132 | 153 | ||
| 133 | return argv_new; | 154 | return argv_new; |
| 134 | } | 155 | } |
| 135 | |||
