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