extra_opts.c 3.6 KB

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