123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
- *
- * Gearmand client and server library.
- *
- * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- *
- * * The names of its contributors may not be used to endorse or
- * promote products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- #include "gear_config.h"
- #include <cstdlib>
- #include <cstdio>
- #include <cstring>
- #include <string>
- #include <vector>
- #include <iostream>
- #include <unistd.h>
- #ifdef HAVE_GETOPT_H
- #include <getopt.h>
- #endif
- #include <libgearman/gearman.h>
- #include "arguments.h"
- namespace gearman_client
- {
- Args::Args(int p_argc, char *p_argv[]) :
- _host(NULL),
- _port(0),
- _count(0),
- _unique(NULL),
- _job_per_newline(false),
- _strip_newline(false),
- _worker(false),
- _suppress_input(false),
- _verbose(false),
- _prefix(false),
- _background(false),
- _daemon(false),
- _usage(false),
- _is_error(false),
- _use_ssl(false),
- _arg_error(NULL),
- _priority(GEARMAN_JOB_PRIORITY_NORMAL),
- _timeout(-1),
- argv(p_argv),
- _error(0)
- {
- init(p_argc);
- }
- Args::~Args()
- {
- reset_arg_error();
- }
- void Args::init(int argc)
- {
- int c;
- opterr= 0;
- while ((c = getopt(argc, argv, "bc:f:h:HILnNp:Pst:u:vwi:dS")) != -1)
- {
- switch(c)
- {
- case 'b':
- _background= true;
- break;
- case 'c':
- _count= static_cast<uint32_t>(atoi(optarg));
- break;
- case 'd':
- _daemon= true;
- break;
- case 'f':
- add(optarg);
- break;
- case 'h':
- _host= optarg;
- break;
- case 'i':
- _pid_file= optarg;
- break;
- case 'I':
- _priority= GEARMAN_JOB_PRIORITY_HIGH;
- break;
- case 'L':
- _priority= GEARMAN_JOB_PRIORITY_LOW;
- break;
- case 'n':
- _job_per_newline= true;
- break;
- case 'N':
- _job_per_newline= true;
- _strip_newline= true;
- break;
- case 'p':
- _port= static_cast<in_port_t>(atoi(optarg));
- break;
- case 'P':
- _prefix= true;
- break;
- case 's':
- _suppress_input= true;
- break;
- case 't':
- _timeout= atoi(optarg);
- break;
- case 'u':
- _unique= optarg;
- break;
- case 'w':
- _worker= true;
- break;
- case 'H':
- _usage= true;
- break;
- case 'v':
- _verbose= true;
- break;
- case 'S':
- _use_ssl= true;
- break;
- default:
- _is_error= true;
- if (optarg)
- {
- size_t length= snprintf(NULL, 0, "-%c %s", char(c), optarg);
- ++length; // Add in space for null
- reset_arg_error();
- _arg_error= (char*)malloc(length);
- if (_arg_error)
- {
- snprintf(_arg_error, length, "-%c %s", char(c), optarg);
- }
- }
- else
- {
- size_t length= snprintf(NULL, 0, "-%c", char(c));
- length++;
- reset_arg_error();
- _arg_error= (char*)malloc(length);
- if (_arg_error)
- {
- snprintf(_arg_error, length, "-%c", char(c));
- }
- }
- break;
- }
- }
- argv+= optind;
- }
- } // namespace gearman_client
|