Update for mavericks
[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 ((errno != 0) or
51 (nptr == argv[optind] and value == 0) or
52 (value == ULONG_MAX and errno == ERANGE) or
53 (value == 0 and errno == EINVAL))
54 {
55 std::cerr << "strtoul() was unable to parse given value" << std::endl;
56 return EXIT_FAILURE;
57 }
58
59 if (value < MEMCACHED_MAXIMUM_RETURN)
60 {
61 std::cout << memcached_strerror(NULL, (memcached_return_t)value) << std::endl;
62 }
63 else
64 {
65 std::cerr << memcached_strerror(NULL, MEMCACHED_MAXIMUM_RETURN) << std::endl;
66 return EXIT_FAILURE;
67 }
68
69 optind++;
70 }
71
72 return EXIT_SUCCESS;
73 }
74
75
76 void options_parse(int argc, char *argv[])
77 {
78 static struct option long_options[]=
79 {
80 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
81 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
82 {0, 0, 0, 0},
83 };
84
85 bool opt_version= false;
86 bool opt_help= false;
87 int option_index= 0;
88 while (1)
89 {
90 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
91 if (option_rv == -1)
92 {
93 break;
94 }
95
96 switch (option_rv)
97 {
98 case 0:
99 break;
100
101 case OPT_VERSION: /* --version or -V */
102 opt_version= true;
103 break;
104
105 case OPT_HELP: /* --help or -h */
106 opt_help= true;
107 break;
108
109 case '?':
110 /* getopt_long already printed an error message. */
111 exit(EXIT_FAILURE);
112
113 default:
114 exit(EXIT_FAILURE);
115 }
116 }
117
118 if (opt_version)
119 {
120 version_command(PROGRAM_NAME);
121 exit(EXIT_SUCCESS);
122 }
123
124 if (opt_help)
125 {
126 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
127 exit(EXIT_SUCCESS);
128 }
129 }