libctdb: test: logging enhancement
[sahlberg/ctdb.git] / libctdb / test / utils.h
1 /*
2
3 This file is taken from nfsim (http://ozlabs.org/~jk/projects/nfsim/)
4
5 Copyright (c) 2003,2004 Rusty Russell
6
7 This file is part of nfsim.
8
9 nfsim is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 nfsim is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with nfsim; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 */
23
24 #ifndef _UTILS_H
25 #define _UTILS_H
26 #include <stdbool.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <getopt.h>
31
32 /* Is A == B ? */
33 #define streq(a,b) (strcmp((a),(b)) == 0)
34
35 /* Does A start with B ? */
36 #define strstarts(a,b) (strncmp((a),(b),strlen(b)) == 0)
37
38 /* Does A end in B ? */
39 static inline bool strends(const char *a, const char *b)
40 {
41         if (strlen(a) < strlen(b))
42                 return false;
43
44         return streq(a + strlen(a) - strlen(b), b);
45 }
46
47 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
48
49 /* Paste two tokens together. */
50 #define ___cat(a,b) a ## b
51 #define __cat(a,b) ___cat(a,b)
52
53 /* Try to give a unique identifier: this comes close, iff used as static. */
54 #define __unique_id(stem) __cat(__cat(__uniq,stem),__LINE__)
55
56 enum exitcodes {
57         /* EXIT_SUCCESS, EXIT_FAILURE is in stdlib.h */
58         EXIT_SCRIPTFAIL = EXIT_FAILURE + 1,
59 };
60
61 /* init code */
62 typedef void (*initcall_t)(void);
63 #define init_call(fn) \
64         static initcall_t __initcall_##fn \
65         __attribute__((__unused__)) \
66         __attribute__((__section__("init_call"))) = &fn
67
68 /* distributed command line options */
69 struct cmdline_option
70 {
71        struct option opt;
72        void (*parse)(struct option *opt);
73 }  __attribute__((aligned(64)));        /* align it to 64 for 64bit arch,
74  * <rusty> LaF0rge: space is cheap.  A comment might be nice. */
75
76 #define cmdline_opt(_name, _has_arg, _c, _fn)                                \
77        static struct cmdline_option __cat(__cmdlnopt_,__unique_id(_fn))      \
78        __attribute__((__unused__))                                           \
79        __attribute__((__section__("cmdline")))                               \
80        = { .opt = { .name = _name, .has_arg = _has_arg, .val = _c },         \
81            .parse = _fn }
82
83 /* In generated-usage.c */
84 void print_usage(void);
85
86 #endif /* _UTILS_H */