1
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
4
#if !defined(TOKUDB_COMMON_FUNCS_H)
5
#define TOKUDB_COMMON_FUNCS_H
8
COPYING CONDITIONS NOTICE:
10
This program is free software; you can redistribute it and/or modify
11
it under the terms of version 2 of the GNU General Public License as
12
published by the Free Software Foundation, and provided that the
13
following conditions are met:
15
* Redistributions of source code must retain this COPYING
16
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
17
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
18
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
21
* Redistributions in binary form must reproduce this COPYING
22
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
23
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
24
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
25
GRANT (below) in the documentation and/or other materials
26
provided with the distribution.
28
You should have received a copy of the GNU General Public License
29
along with this program; if not, write to the Free Software
30
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35
TokuDB, Tokutek Fractal Tree Indexing Library.
36
Copyright (C) 2007-2013 Tokutek, Inc.
40
This program is distributed in the hope that it will be useful, but
41
WITHOUT ANY WARRANTY; without even the implied warranty of
42
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43
General Public License for more details.
45
UNIVERSITY PATENT NOTICE:
47
The technology is licensed by the Massachusetts Institute of
48
Technology, Rutgers State University of New Jersey, and the Research
49
Foundation of State University of New York at Stony Brook under
50
United States of America Serial No. 11/760379 and to the patents
51
and/or patent applications resulting from it.
53
PATENT MARKING NOTICE:
55
This software is covered by US Patent No. 8,185,551.
56
This software is covered by US Patent No. 8,489,638.
60
"THIS IMPLEMENTATION" means the copyrightable works distributed by
61
Tokutek as part of the Fractal Tree project.
63
"PATENT CLAIMS" means the claims of patents that are owned or
64
licensable by Tokutek, both currently or in the future; and that in
65
the absence of this license would be infringed by THIS
66
IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
68
"PATENT CHALLENGE" shall mean a challenge to the validity,
69
patentability, enforceability and/or non-infringement of any of the
70
PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
72
Tokutek hereby grants to you, for the term and geographical scope of
73
the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
74
irrevocable (except as stated in this section) patent license to
75
make, have made, use, offer to sell, sell, import, transfer, and
76
otherwise run, modify, and propagate the contents of THIS
77
IMPLEMENTATION, where such license applies only to the PATENT
78
CLAIMS. This grant does not include claims that would be infringed
79
only as a consequence of further modifications of THIS
80
IMPLEMENTATION. If you or your agent or licensee institute or order
81
or agree to the institution of patent litigation against any entity
82
(including a cross-claim or counterclaim in a lawsuit) alleging that
83
THIS IMPLEMENTATION constitutes direct or contributory patent
84
infringement, or inducement of patent infringement, then any rights
85
granted to you under this License shall terminate as of the date
86
such litigation is filed. If you or your agent or exclusive
87
licensee institute or order or agree to the institution of a PATENT
88
CHALLENGE, then Tokutek may terminate any rights granted to you
92
#ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
94
#include "tokudb_common.h"
96
//DB_ENV->err disabled since it does not use db_strerror
97
#define PRINT_ERROR(retval, ...) \
99
if (0) g.dbenv->err(g.dbenv, retval, __VA_ARGS__); \
101
fprintf(stderr, "\tIn %s:%d %s()\n", __FILE__, __LINE__, __FUNCTION__); \
102
fprintf(stderr, "%s: %s:", g.progname, db_strerror(retval)); \
103
fprintf(stderr, __VA_ARGS__); \
104
fprintf(stderr, "\n"); \
109
//DB_ENV->err disabled since it does not use db_strerror, errx does not exist.
110
#define PRINT_ERRORX(...) \
112
if (0) g.dbenv->err(g.dbenv, 0, __VA_ARGS__); \
114
fprintf(stderr, "\tIn %s:%d %s()\n", __FILE__, __LINE__, __FUNCTION__); \
115
fprintf(stderr, "%s: ", g.progname); \
116
fprintf(stderr, __VA_ARGS__); \
117
fprintf(stderr, "\n"); \
122
int strtoint32 (char* str, int32_t* num, int32_t min, int32_t max, int base);
123
int strtouint32 (char* str, uint32_t* num, uint32_t min, uint32_t max, int base);
124
int strtoint64 (char* str, int64_t* num, int64_t min, int64_t max, int base);
125
int strtouint64 (char* str, uint64_t* num, uint64_t min, uint64_t max, int base);
128
* Convert a string to an integer of type "type".
131
* Sets errno and returns:
132
* EINVAL: str == NULL, num == NULL, or string not of the form [ \t]*[+-]?[0-9]+
133
* ERANGE: value out of range specified. (Range of [min, max])
135
* *num is unchanged on error.
139
#define DEF_STR_TO(name, type, bigtype, strtofunc, frmt) \
140
int name(char* str, type* num, type min, type max, int base) \
147
assert(min <= max); \
148
assert(g.dbenv || g.progname); \
149
assert(base == 0 || (base >= 2 && base <= 36)); \
152
while (isspace(*str)) str++; \
153
value = strtofunc(str, &test, base); \
154
if ((*test != '\0' && *test != '\n') || test == str) { \
155
PRINT_ERRORX("%s: Invalid numeric argument\n", str); \
160
PRINT_ERROR(errno, "%s\n", str); \
163
PRINT_ERRORX("%s: Less than minimum value (%" frmt ")\n", str, min); \
167
PRINT_ERRORX("%s: Greater than maximum value (%" frmt ")\n", str, max); \
171
return EXIT_SUCCESS; \
176
DEF_STR_TO(strtoint32, int32_t, int64_t, strtoll, PRId32)
177
DEF_STR_TO(strtouint32, uint32_t, uint64_t, strtoull, PRIu32)
178
DEF_STR_TO(strtoint64, int64_t, int64_t, strtoll, PRId64)
179
DEF_STR_TO(strtouint64, uint64_t, uint64_t, strtoull, PRIu64)
182
outputbyte(uint8_t ch)
185
if (ch == '\\') printf("\\\\");
186
else if (isprint(ch)) printf("%c", ch);
187
else printf("\\%02x", ch);
189
else printf("%02x", ch);
193
outputstring(char* str)
197
for (p = str; *p != '\0'; p++) {
198
outputbyte((uint8_t)*p);
203
outputplaintextstring(char* str)
205
bool old_plaintext = g.plaintext;
208
g.plaintext = old_plaintext;
214
if (ch >= '0' && ch <= '9') {
217
if (ch >= 'a' && ch <= 'z') {
218
return ch - 'a' + 10;
220
if (ch >= 'A' && ch <= 'Z') {
221
return ch - 'A' + 10;
227
printabletocstring(char* inputstr, char** poutputstr)
236
assert(*poutputstr == NULL);
238
cstring = (char*)toku_malloc((strlen(inputstr) + 1) * sizeof(char));
239
if (cstring == NULL) {
240
PRINT_ERROR(errno, "printabletocstring");
244
for (*poutputstr = cstring; *inputstr != '\0'; inputstr++) {
245
if (*inputstr == '\\') {
246
if ((highch = *++inputstr) == '\\') {
250
if (highch == '\0' || (lowch = *++inputstr) == '\0') {
251
PRINT_ERROR(0, "unexpected end of input data or key/data pair");
254
if (!isxdigit(highch)) {
255
PRINT_ERROR(0, "Unexpected '%c' (non-hex) input.\n", highch);
258
if (!isxdigit(lowch)) {
259
PRINT_ERROR(0, "Unexpected '%c' (non-hex) input.\n", lowch);
262
nextch = (char)((hextoint(highch) << 4) | hextoint(lowch));
263
if (nextch == '\0') {
264
/* Database names are c strings, and cannot have extra NULL terminators. */
265
PRINT_ERROR(0, "Unexpected '\\00' in input.\n");
270
else *cstring++ = *inputstr;
272
/* Terminate the string. */
277
PRINT_ERROR(0, "Quitting out due to errors.\n");
282
verify_library_version(void)
287
db_version(&major, &minor, NULL);
288
if (major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR) {
289
PRINT_ERRORX("version %d.%d doesn't match library version %d.%d\n",
290
DB_VERSION_MAJOR, DB_VERSION_MINOR, major, minor);
296
static int last_caught = 0;
298
static void catch_signal(int which_signal) {
299
last_caught = which_signal;
300
if (last_caught == 0) last_caught = SIGINT;
304
init_catch_signals(void) {
305
signal(SIGINT, catch_signal);
306
signal(SIGTERM, catch_signal);
308
signal(SIGHUP, catch_signal);
311
signal(SIGPIPE, catch_signal);
316
caught_any_signals(void) {
317
return last_caught != 0;
321
resend_signals(void) {
323
signal(last_caught, SIG_DFL);
329
static int test_main (int argc, char *const argv[]);
331
main(int argc, char *const argv[]) {
333
r = test_main(argc, argv);
337
#endif /* #if !defined(TOKUDB_COMMON_H) */