extra_opts.c 3.7 KB

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