extra_opts.c 3.7 KB

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