Back to index

nagios-plugins  1.4.16
extra_opts.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios-plugins extra_opts library
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2007 Nagios Plugins Development Team
00007 * 
00008 * This program is free software: you can redistribute it and/or modify
00009 * it under the terms of the GNU General Public License as published by
00010 * the Free Software Foundation, either version 3 of the License, or
00011 * (at your option) any later version.
00012 * 
00013 * This program is distributed in the hope that it will be useful,
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 * GNU General Public License for more details.
00017 * 
00018 * You should have received a copy of the GNU General Public License
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 * 
00021 *****************************************************************************/
00022 
00023 #include "common.h"
00024 #include "utils_base.h"
00025 #include "parse_ini.h"
00026 #include "extra_opts.h"
00027 
00028 /* FIXME: copied from utils.h; we should move a bunch of libs! */
00029 int
00030 is_option2 (char *str)
00031 {
00032        if (!str)
00033               return FALSE;
00034        else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
00035               return TRUE;
00036        else
00037               return FALSE;
00038 }
00039 
00040 /* this is the externally visible function used by plugins */
00041 char **np_extra_opts(int *argc, char **argv, const char *plugin_name){
00042        np_arg_list *extra_args=NULL, *ea1=NULL, *ea_tmp=NULL;
00043        char **argv_new=NULL;
00044        char *argptr=NULL;
00045        int i, j, optfound, argc_new, ea_num=*argc;
00046 
00047        if(*argc<2) {
00048               /* No arguments provided */
00049               return argv;
00050        }
00051 
00052        for(i=1; i<*argc; i++){
00053               argptr=NULL;
00054               optfound=0;
00055 
00056               /* Do we have an extra-opts parameter? */
00057               if(strncmp(argv[i], "--extra-opts=", 13)==0){
00058                      /* It is a single argument with value */
00059                      argptr=argv[i]+13;
00060                      /* Delete the extra opts argument */
00061                      for(j=i;j<*argc;j++) argv[j]=argv[j+1];
00062                      i--;
00063                      *argc-=1;
00064               }else if(strcmp(argv[i], "--extra-opts")==0){
00065                      if((i+1<*argc)&&!is_option2(argv[i+1])){
00066                             /* It is a argument with separate value */
00067                             argptr=argv[i+1];
00068                             /* Delete the extra-opts argument/value */
00069                             for(j=i;j<*argc-1;j++) argv[j]=argv[j+2];
00070                             i-=2;
00071                             *argc-=2;
00072                             ea_num--;
00073                      }else{
00074                             /* It has no value */
00075                             optfound=1;
00076                             /* Delete the extra opts argument */
00077                             for(j=i;j<*argc;j++) argv[j]=argv[j+1];
00078                             i--;
00079                             *argc-=1;
00080                      }
00081               }
00082 
00083               /* If we found extra-opts, expand them and store them for later*/
00084               if(argptr||optfound){
00085                      /* Process ini section, returning a linked list of arguments */
00086                      ea1=np_get_defaults(argptr, plugin_name);
00087                      if(ea1==NULL) {
00088                             /* no extra args (empty section)? */
00089                             ea_num--;
00090                             continue;
00091                      }
00092 
00093                      /* append the list to extra_args */
00094                      if(extra_args==NULL){
00095                             extra_args=ea1;
00096                             while(ea1=ea1->next) ea_num++;
00097                      }else{
00098                             ea_tmp=extra_args;
00099                             while(ea_tmp->next) {
00100                                    ea_tmp=ea_tmp->next;
00101                             }
00102                             ea_tmp->next=ea1;
00103                             while(ea1=ea1->next) ea_num++;
00104                      }
00105                      ea1=ea_tmp=NULL;
00106               }
00107        } /* lather, rince, repeat */
00108 
00109        if(ea_num==*argc && extra_args==NULL){
00110               /* No extra-opts */
00111               return argv;
00112        }
00113 
00114        /* done processing arguments. now create a new argv array... */
00115        argv_new=(char**)malloc((ea_num+1)*sizeof(char**));
00116        if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
00117 
00118        /* starting with program name */
00119        argv_new[0]=argv[0];
00120        argc_new=1;
00121        /* then parsed ini opts (frying them up in the same run) */
00122        while(extra_args){
00123               argv_new[argc_new++]=extra_args->arg;
00124               ea1=extra_args;
00125               extra_args=extra_args->next;
00126               free(ea1);
00127        }
00128        /* finally the rest of the argv array */
00129        for (i=1; i<*argc; i++)     argv_new[argc_new++]=argv[i];
00130        *argc=argc_new;
00131        /* and terminate. */
00132        argv_new[argc_new]=NULL;
00133 
00134        return argv_new;
00135 }
00136