extra_opts.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /* Shouldn't se modify directly **argv (passed as a char ***) and argc
  45. * (as int *) ?
  46. */
  47. char **np_extra_opts(int argc, char **argv, const char *plugin_name, int *argc_new){
  48. np_arg_list *extra_args=NULL, *ea_tmp1=NULL, *ea_tmp2=NULL;
  49. char **argv_new=NULL;
  50. char *argptr=NULL;
  51. int i, j, optfound, ea_num=argc;
  52. if(argc<2) {
  53. /* No arguments provided */
  54. *argc_new=argc;
  55. argv_new=argv;
  56. return argv_new;
  57. }
  58. for(i=1; i<argc; i++){
  59. argptr=NULL;
  60. optfound=0;
  61. /* Do we have an extra-opts parameter? */
  62. if(strncmp(argv[i], "--extra-opts=", 13)==0){
  63. /* It is a single argument with value */
  64. argptr=argv[i]+13;
  65. /* Delete the extra opts argument */
  66. for(j=i;j<argc;j++) argv[j]=argv[j+1];
  67. i--;
  68. argc--;
  69. }else if(strcmp(argv[i], "--extra-opts")==0){
  70. if(!is_option(argv[i+1])){
  71. /* It is a argument with separate value */
  72. argptr=argv[i+1];
  73. /* Delete the extra-opts argument/value */
  74. for(j=i;j<argc-1;j++) argv[j]=argv[j+2];
  75. i-=2;
  76. argc-=2;
  77. ea_num--;
  78. }else{
  79. /* It has no value */
  80. optfound=1;
  81. /* Delete the extra opts argument */
  82. for(j=i;j<argc;j++) argv[j]=argv[j+1];
  83. i--;
  84. argc--;
  85. }
  86. }
  87. if(argptr||optfound){
  88. /* Process ini section, returning a linked list of arguments */
  89. ea_tmp1=np_get_defaults(argptr, plugin_name);
  90. if(ea_tmp1==NULL) {
  91. /* no extra args? */
  92. ea_num--;
  93. continue;
  94. }
  95. /* append the list to extra_args */
  96. if(extra_args==NULL){
  97. extra_args=ea_tmp2=ea_tmp1;
  98. while(ea_tmp2->next) {
  99. ea_tmp2=ea_tmp2->next;
  100. ea_num++;
  101. }
  102. }else{
  103. ea_tmp2=extra_args;
  104. while(ea_tmp2->next) {
  105. ea_tmp2=ea_tmp2->next;
  106. ea_num++;
  107. }
  108. ea_tmp2->next=ea_tmp1;
  109. }
  110. ea_tmp1=ea_tmp2=NULL;
  111. }
  112. /* lather, rince, repeat */
  113. }
  114. if(ea_num==argc && extra_args==NULL){
  115. /* No extra-opts */
  116. *argc_new=argc;
  117. argv_new=argv;
  118. return argv_new;
  119. }
  120. /* done processing arguments. now create a new argc/argv set... */
  121. argv_new=(char**)malloc((ea_num+1)*sizeof(char**));
  122. if(argv_new==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
  123. /* starting with program name (Should we strdup or just use the poiter?) */
  124. argv_new[0]=strdup(argv[0]);
  125. *argc_new=1;
  126. /* then parsed ini opts (frying them up in the same run) */
  127. while(extra_args){
  128. argv_new[*argc_new]=strdup(extra_args->arg);
  129. *argc_new+=1;
  130. ea_tmp1=extra_args;
  131. extra_args=extra_args->next;
  132. free(ea_tmp1);
  133. }
  134. /* finally the rest of the argv array (Should we strdup or just use the poiter?) */
  135. for (i=1; i<argc; i++){
  136. argv_new[*argc_new]=strdup(argv[i]);
  137. *argc_new+=1;
  138. }
  139. /* and terminate. */
  140. argv_new[*argc_new]=NULL;
  141. return argv_new;
  142. }