arguments.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. _use_ssl(false),
  68. _arg_error(NULL),
  69. _priority(GEARMAN_JOB_PRIORITY_NORMAL),
  70. _timeout(-1),
  71. argv(p_argv),
  72. _error(0)
  73. {
  74. init(p_argc);
  75. }
  76. Args::~Args()
  77. {
  78. reset_arg_error();
  79. }
  80. void Args::init(int argc)
  81. {
  82. int c;
  83. opterr= 0;
  84. while ((c = getopt(argc, argv, "bc:f:h:HILnNp:Pst:u:vwi:dS")) != -1)
  85. {
  86. switch(c)
  87. {
  88. case 'b':
  89. _background= true;
  90. break;
  91. case 'c':
  92. _count= static_cast<uint32_t>(atoi(optarg));
  93. break;
  94. case 'd':
  95. _daemon= true;
  96. break;
  97. case 'f':
  98. add(optarg);
  99. break;
  100. case 'h':
  101. _host= optarg;
  102. break;
  103. case 'i':
  104. _pid_file= optarg;
  105. break;
  106. case 'I':
  107. _priority= GEARMAN_JOB_PRIORITY_HIGH;
  108. break;
  109. case 'L':
  110. _priority= GEARMAN_JOB_PRIORITY_LOW;
  111. break;
  112. case 'n':
  113. _job_per_newline= true;
  114. break;
  115. case 'N':
  116. _job_per_newline= true;
  117. _strip_newline= true;
  118. break;
  119. case 'p':
  120. _port= static_cast<in_port_t>(atoi(optarg));
  121. break;
  122. case 'P':
  123. _prefix= true;
  124. break;
  125. case 's':
  126. _suppress_input= true;
  127. break;
  128. case 't':
  129. _timeout= atoi(optarg);
  130. break;
  131. case 'u':
  132. _unique= optarg;
  133. break;
  134. case 'w':
  135. _worker= true;
  136. break;
  137. case 'H':
  138. _usage= true;
  139. break;
  140. case 'v':
  141. _verbose= true;
  142. break;
  143. case 'S':
  144. _use_ssl= true;
  145. break;
  146. default:
  147. _is_error= true;
  148. if (optarg)
  149. {
  150. size_t length= snprintf(NULL, 0, "-%c %s", char(c), optarg);
  151. ++length; // Add in space for null
  152. reset_arg_error();
  153. _arg_error= (char*)malloc(length);
  154. if (_arg_error)
  155. {
  156. snprintf(_arg_error, length, "-%c %s", char(c), optarg);
  157. }
  158. }
  159. else
  160. {
  161. size_t length= snprintf(NULL, 0, "-%c", char(c));
  162. length++;
  163. reset_arg_error();
  164. _arg_error= (char*)malloc(length);
  165. if (_arg_error)
  166. {
  167. snprintf(_arg_error, length, "-%c", char(c));
  168. }
  169. }
  170. break;
  171. }
  172. }
  173. argv+= optind;
  174. }
  175. } // namespace gearman_client