14a70c29b0ae506132506a93e29a022569702fde
[m6w6/libmemcached] / src / bin / memerror.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #include <cerrno>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cstring>
22 #include <climits>
23
24 #include <getopt.h>
25 #include <iostream>
26 #include <unistd.h>
27
28 #include "libmemcached-1.0/memcached.h"
29
30 #include "utilities.h"
31
32 #define PROGRAM_NAME "memerror"
33 #define PROGRAM_DESCRIPTION "Translate a memcached errror code into a string."
34
35 /* Prototypes */
36 void options_parse(int argc, char *argv[]);
37
38 int main(int argc, char *argv[]) {
39 options_parse(argc, argv);
40
41 if (argc < 2) {
42 return EXIT_FAILURE;
43 }
44
45 while (optind < argc) {
46 errno = 0;
47 char *nptr;
48 unsigned long value = strtoul(argv[optind], &nptr, 10);
49
50 if ((errno) or (nptr == argv[optind] and value == 0)
51 or (value == ULONG_MAX and errno == ERANGE) or (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 std::cout << memcached_strerror(NULL, (memcached_return_t) value) << std::endl;
59 } else {
60 std::cerr << memcached_strerror(NULL, MEMCACHED_MAXIMUM_RETURN) << std::endl;
61 return EXIT_FAILURE;
62 }
63
64 optind++;
65 }
66
67 return EXIT_SUCCESS;
68 }
69
70 void options_parse(int argc, char *argv[]) {
71 static struct option long_options[] = {
72 {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION},
73 {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP},
74 {0, 0, 0, 0},
75 };
76
77 bool opt_version = false;
78 bool opt_help = false;
79 int option_index = 0;
80 while (1) {
81 int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
82 if (option_rv == -1) {
83 break;
84 }
85
86 switch (option_rv) {
87 case 0:
88 break;
89
90 case OPT_VERSION: /* --version or -V */
91 opt_version = true;
92 break;
93
94 case OPT_HELP: /* --help or -h */
95 opt_help = true;
96 break;
97
98 case '?':
99 /* getopt_long already printed an error message. */
100 exit(EXIT_FAILURE);
101
102 default:
103 exit(EXIT_FAILURE);
104 }
105 }
106
107 if (opt_version) {
108 version_command(PROGRAM_NAME);
109 exit(EXIT_SUCCESS);
110 }
111
112 if (opt_help) {
113 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
114 exit(EXIT_SUCCESS);
115 }
116 }