arguments.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "gear_config.h"
  38. #include <cstdlib>
  39. #include <cstdio>
  40. #include <cstring>
  41. #include <string>
  42. #include <vector>
  43. #include <iostream>
  44. #include <unistd.h>
  45. #ifdef HAVE_GETOPT_H
  46. #include <getopt.h>
  47. #endif
  48. #include <libgearman/gearman.h>
  49. #include "arguments.h"
  50. namespace gearman_client
  51. {
  52. Args::Args(int p_argc, char *p_argv[]) :
  53. _host(NULL),
  54. _port(0),
  55. _count(0),
  56. _unique(NULL),
  57. _job_per_newline(false),
  58. _strip_newline(false),
  59. _worker(false),
  60. _suppress_input(false),
  61. _verbose(false),
  62. _prefix(false),
  63. _background(false),
  64. _daemon(false),
  65. _usage(false),
  66. _is_error(false),
  67. _arg_error(NULL),
  68. _priority(GEARMAN_JOB_PRIORITY_NORMAL),
  69. _timeout(-1),
  70. argv(p_argv),
  71. _error(0)
  72. {
  73. init(p_argc);
  74. }
  75. Args::~Args()
  76. {
  77. if (_arg_error)
  78. {
  79. free(_arg_error);
  80. }
  81. }
  82. void Args::init(int argc)
  83. {
  84. int c;
  85. opterr= 0;
  86. while ((c = getopt(argc, argv, "bc:f:h:HILnNp:Pst:u:vwi:d")) != -1)
  87. {
  88. switch(c)
  89. {
  90. case 'b':
  91. _background= true;
  92. break;
  93. case 'c':
  94. _count= static_cast<uint32_t>(atoi(optarg));
  95. break;
  96. case 'd':
  97. _daemon= true;
  98. break;
  99. case 'f':
  100. add(optarg);
  101. break;
  102. case 'h':
  103. _host= optarg;
  104. break;
  105. case 'i':
  106. _pid_file= optarg;
  107. break;
  108. case 'I':
  109. _priority= GEARMAN_JOB_PRIORITY_HIGH;
  110. break;
  111. case 'L':
  112. _priority= GEARMAN_JOB_PRIORITY_LOW;
  113. break;
  114. case 'n':
  115. _job_per_newline= true;
  116. break;
  117. case 'N':
  118. _job_per_newline= true;
  119. _strip_newline= true;
  120. break;
  121. case 'p':
  122. _port= static_cast<in_port_t>(atoi(optarg));
  123. break;
  124. case 'P':
  125. _prefix= true;
  126. break;
  127. case 's':
  128. _suppress_input= true;
  129. break;
  130. case 't':
  131. _timeout= atoi(optarg);
  132. break;
  133. case 'u':
  134. _unique= optarg;
  135. break;
  136. case 'w':
  137. _worker= true;
  138. break;
  139. case 'H':
  140. _usage= true;
  141. break;
  142. case 'v':
  143. _verbose= true;
  144. break;
  145. default:
  146. _is_error= true;
  147. if (optarg)
  148. {
  149. size_t length= snprintf(NULL, 0, "-%c %s", char(c), optarg);
  150. ++length; // Add in space for null
  151. _arg_error= (char*)malloc(length);
  152. if (_arg_error)
  153. {
  154. snprintf(_arg_error, length, "-%c %s", char(c), optarg);
  155. }
  156. }
  157. else
  158. {
  159. size_t length= snprintf(NULL, 0, "-%c", char(c));
  160. length++;
  161. _arg_error= (char*)malloc(length);
  162. if (_arg_error)
  163. {
  164. snprintf(_arg_error, length, "-%c", char(c));
  165. }
  166. }
  167. break;
  168. }
  169. }
  170. argv+= optind;
  171. }
  172. } // namespace gearman_client