Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[m6w6/libmemcached] / clients / memerror.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary:
10 *
11 */
12 #include "mem_config.h"
13
14 #include <cerrno>
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cstring>
18 #include <climits>
19
20 #include <getopt.h>
21 #include <iostream>
22 #include <unistd.h>
23
24 #include <libmemcached-1.0/memcached.h>
25
26 #include "utilities.h"
27
28 #define PROGRAM_NAME "memerror"
29 #define PROGRAM_DESCRIPTION "Translate a memcached errror code into a string."
30
31
32 /* Prototypes */
33 void options_parse(int argc, char *argv[]);
34
35 int main(int argc, char *argv[])
36 {
37 options_parse(argc, argv);
38
39 if (argc < 2)
40 {
41 return EXIT_FAILURE;
42 }
43
44 while (optind < argc)
45 {
46 errno= 0;
47 char *nptr;
48 unsigned long value= strtoul(argv[optind], &nptr, 10);
49
50 if ((nptr == argv[optind] and value == 0) or
51 (value == ULONG_MAX and errno == ERANGE) or
52 (value == 0 and errno == EINVAL))
53 {
54 std::cerr << "strtoul() was unable to parse given value" << std::endl;
55 return EXIT_FAILURE;
56 }
57
58 if (value < MEMCACHED_MAXIMUM_RETURN)
59 {
60 std::cout << memcached_strerror(NULL, (memcached_return_t)value) << std::endl;
61 }
62 else
63 {
64 std::cerr << memcached_strerror(NULL, MEMCACHED_MAXIMUM_RETURN) << std::endl;
65 return EXIT_FAILURE;
66 }
67
68 optind++;
69 }
70
71 return EXIT_SUCCESS;
72 }
73
74
75 void options_parse(int argc, char *argv[])
76 {
77 static struct option long_options[]=
78 {
79 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
80 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
81 {0, 0, 0, 0},
82 };
83
84 bool opt_version= false;
85 bool opt_help= false;
86 int option_index= 0;
87 while (1)
88 {
89 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
90 if (option_rv == -1)
91 {
92 break;
93 }
94
95 switch (option_rv)
96 {
97 case 0:
98 break;
99
100 case OPT_VERSION: /* --version or -V */
101 opt_version= true;
102 break;
103
104 case OPT_HELP: /* --help or -h */
105 opt_help= true;
106 break;
107
108 case '?':
109 /* getopt_long already printed an error message. */
110 exit(EXIT_FAILURE);
111
112 default:
113 exit(EXIT_FAILURE);
114 }
115 }
116
117 if (opt_version)
118 {
119 version_command(PROGRAM_NAME);
120 exit(EXIT_SUCCESS);
121 }
122
123 if (opt_help)
124 {
125 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
126 exit(EXIT_SUCCESS);
127 }
128 }