Xenomai  3.0-rc7
smokey.h
1 /*
2  * Copyright (C) 2014 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 #ifndef _XENOMAI_SMOKEY_SMOKEY_H
19 #define _XENOMAI_SMOKEY_SMOKEY_H
20 
21 #include <boilerplate/list.h>
22 #include <boilerplate/libc.h>
23 #include <copperplate/clockobj.h>
24 #include <xenomai/init.h>
25 
26 #define SMOKEY_INT(__name) { \
27  .name = # __name, \
28  .parser = smokey_int, \
29  .matched = 0, \
30  }
31 
32 #define SMOKEY_BOOL(__name) { \
33  .name = # __name, \
34  .parser = smokey_bool, \
35  .matched = 0, \
36  }
37 
38 #define SMOKEY_STRING(__name) { \
39  .name = # __name, \
40  .parser = smokey_string, \
41  .matched = 0, \
42  }
43 
44 #define SMOKEY_ARGLIST(__args...) ((struct smokey_arg[]){ __args })
45 
46 #define SMOKEY_NOARGS (((struct smokey_arg[]){ { .name = NULL } }))
47 
48 struct smokey_arg {
49  const char *name;
50  int (*parser)(const char *s,
51  struct smokey_arg *arg);
52  union {
53  int n_val;
54  char *s_val;
55  } u;
56  int matched;
57 };
58 
59 struct smokey_test {
60  const char *name;
61  struct smokey_arg *args;
62  int nargs;
63  const char *description;
64  int (*run)(struct smokey_test *t,
65  int argc, char *const argv[]);
66  struct {
67  int id;
68  struct pvholder next;
69  } __reserved;
70 };
71 
72 #define for_each_smokey_test(__pos) \
73  pvlist_for_each_entry((__pos), &smokey_test_list, __reserved.next)
74 
75 #define __smokey_arg_count(__args) \
76  (sizeof(__args) / sizeof(__args[0]))
77 
78 #define smokey_test_plugin(__plugin, __args, __desc) \
79  static int run_ ## __plugin(struct smokey_test *t, \
80  int argc, char *const argv[]); \
81  static struct smokey_test __plugin = { \
82  .name = #__plugin, \
83  .args = (__args), \
84  .nargs = __smokey_arg_count(__args), \
85  .description = (__desc), \
86  .run = run_ ## __plugin, \
87  }; \
88  __early_ctor void smokey_plugin_ ## __plugin(void); \
89  void smokey_plugin_ ## __plugin(void) \
90  { \
91  smokey_register_plugin(&(__plugin)); \
92  }
93 
94 #define SMOKEY_ARG(__plugin, __arg) (smokey_lookup_arg(&(__plugin), # __arg))
95 #define SMOKEY_ARG_ISSET(__plugin, __arg) (SMOKEY_ARG(__plugin, __arg)->matched)
96 #define SMOKEY_ARG_INT(__plugin, __arg) (SMOKEY_ARG(__plugin, __arg)->u.n_val)
97 #define SMOKEY_ARG_BOOL(__plugin, __arg) (!!SMOKEY_ARG_INT(__plugin, __arg))
98 #define SMOKEY_ARG_STRING(__plugin, __arg) (SMOKEY_ARG(__plugin, __arg)->u.s_val)
99 
100 #define smokey_check_errno(__expr) \
101  ({ \
102  int __ret = (__expr); \
103  if (__ret < 0) { \
104  __ret = -errno; \
105  __smokey_warning(__FILE__, __LINE__, "%s: %s", \
106  #__expr, strerror(errno)); \
107  } \
108  __ret; \
109  })
110 
111 #define smokey_check_status(__expr) \
112  ({ \
113  int __ret = (__expr); \
114  if (__ret) { \
115  __smokey_warning(__FILE__, __LINE__, "%s: %s", \
116  #__expr, strerror(__ret)); \
117  __ret = -__ret; \
118  } \
119  __ret; \
120  })
121 
122 #define smokey_assert(__expr) \
123  ({ \
124  int __ret = (__expr); \
125  if (!__ret) \
126  __smokey_warning(__FILE__, __LINE__, \
127  "assertion failed: %s", #__expr); \
128  __ret; \
129  })
130 
131 #define smokey_warning(__fmt, __args...) \
132  __smokey_warning(__FILE__, __LINE__, __fmt, ##__args)
133 
134 #ifdef __cplusplus
135 extern "C" {
136 #endif
137 
138 void smokey_register_plugin(struct smokey_test *t);
139 
140 int smokey_int(const char *s, struct smokey_arg *arg);
141 
142 int smokey_bool(const char *s, struct smokey_arg *arg);
143 
144 int smokey_string(const char *s, struct smokey_arg *arg);
145 
146 struct smokey_arg *smokey_lookup_arg(struct smokey_test *t,
147  const char *arg);
148 
149 int smokey_parse_args(struct smokey_test *t,
150  int argc, char *const argv[]);
151 
152 void smokey_trace(const char *fmt, ...);
153 
154 void smokey_note(const char *fmt, ...);
155 
156 void __smokey_warning(const char *file, int lineno,
157  const char *fmt, ...);
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 extern struct pvlistobj smokey_test_list;
164 
165 extern int smokey_keep_going;
166 
167 extern int smokey_verbose_mode;
168 
169 #endif /* _XENOMAI_SMOKEY_SMOKEY_H */