libtrap  1.16.1
trap_module_info.h
Go to the documentation of this file.
1 
7 /*
8  * Copyright (C) 2015 CESNET
9  *
10  * LICENSE TERMS
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  * 3. Neither the name of the Company nor the names of its contributors
22  * may be used to endorse or promote products derived from this
23  * software without specific prior written permission.
24  *
25  * ALTERNATIVELY, provided that this notice is retained in full, this
26  * product may be distributed under the terms of the GNU General Public
27  * License (GPL) version 2 or later, in which case the provisions
28  * of the GPL apply INSTEAD OF those given above.
29  *
30  * This software is provided ``as is'', and any express or implied
31  * warranties, including, but not limited to, the implied warranties of
32  * merchantability and fitness for a particular purpose are disclaimed.
33  * In no event shall the company or contributors be liable for any
34  * direct, indirect, incidental, special, exemplary, or consequential
35  * damages (including, but not limited to, procurement of substitute
36  * goods or services; loss of use, data, or profits; or business
37  * interruption) however caused and on any theory of liability, whether
38  * in contract, strict liability, or tort (including negligence or
39  * otherwise) arising in any way out of the use of this software, even
40  * if advised of the possibility of such damage.
41  *
42  */
43 
44 #ifndef _TRAP_MODULE_INFO_H_
45 #define _TRAP_MODULE_INFO_H_
46 
138  char short_opt;
139  char *long_opt;
140  char *description;
144 
150 typedef struct trap_module_info_s {
151  char *name;
152  char *description;
158 
161 #define GEN_LONG_OPT_STRUCT_LINE(p_short_opt, p_long_opt, p_description, p_required_argument, p_argument_type) {p_long_opt, p_required_argument, 0, p_short_opt},
162 
165 #define GEN_LONG_OPT_STRUCT(PARAMS) \
166  static struct option long_options[] __attribute__((used)) = { \
167  PARAMS(GEN_LONG_OPT_STRUCT_LINE) \
168  {0, 0, 0, 0} \
169  };
170 
179 #define ALLOCATE_BASIC_INFO_2(module_info, p_name, p_description, p_input, p_output) \
180  if (module_info != NULL) { \
181  if (p_name != NULL) { \
182  module_info->name = strdup(p_name); \
183  } else { \
184  module_info->name = NULL; \
185  } \
186  if (p_description != NULL) { \
187  module_info->description = strdup(p_description); \
188  } else { \
189  module_info->description = NULL; \
190  } \
191  module_info->num_ifc_in = p_input; \
192  module_info->num_ifc_out = p_output; \
193  }
194 
202 #define ALLOCATE_BASIC_INFO(p_name, p_description, p_input, p_output) \
203  ALLOCATE_BASIC_INFO_2(module_info, p_name, p_description, p_input, p_output)
204 
215 #define ALLOCATE_PARAM_ITEMS_2(m, param_id, p_short_opt, p_long_opt, p_description, p_required_argument, p_argument_type) \
216  if (m != NULL) { \
217  if (m->params[param_id] == NULL) { \
218  m->params[param_id] = (trap_module_info_parameter_t *) calloc(1, sizeof(trap_module_info_parameter_t)); \
219  } \
220  if (m->params[param_id] != NULL) { \
221  if (p_short_opt != 0) { \
222  m->params[param_id]->short_opt = p_short_opt; \
223  } else { \
224  m->params[param_id]->short_opt = 0; \
225  } \
226  if (p_long_opt != NULL) { \
227  m->params[param_id]->long_opt = strdup(p_long_opt); \
228  } else { \
229  m->params[param_id]->long_opt = strdup(""); \
230  } \
231  if (p_description != NULL) { \
232  m->params[param_id]->description = strdup(p_description); \
233  } else { \
234  m->params[param_id]->description = strdup(""); \
235  } \
236  if (p_required_argument == 1) { \
237  m->params[param_id]->param_required_argument = p_required_argument; \
238  } else { \
239  m->params[param_id]->param_required_argument = 0; \
240  } \
241  if (p_argument_type != NULL) { \
242  m->params[param_id]->argument_type = strdup(p_argument_type); \
243  } else { \
244  m->params[param_id]->argument_type = strdup(""); \
245  } \
246  } \
247  }
248 
257 #define ALLOCATE_PARAM_ITEMS(p_short_opt, p_long_opt, p_description, p_required_argument, p_argument_type) \
258  ALLOCATE_PARAM_ITEMS_2(module_info, trap_module_params_cnt, p_short_opt, p_long_opt, p_description, p_required_argument, p_argument_type) \
259  trap_module_params_cnt++;
260 
263 #define FREE_BASIC_INFO(p_name, p_description, p_input, p_output) \
264  if (module_info->name != NULL) { \
265  free(module_info->name); \
266  module_info->name = NULL; \
267  } \
268  if (module_info->description != NULL) { \
269  free(module_info->description); \
270  module_info->description = NULL; \
271  }
272 
275 #define FREE_PARAM_ITEMS(p_short_opt, p_long_opt, p_description, p_required_argument, p_argument_type) \
276  if (module_info->params[trap_module_params_cnt] != NULL) { \
277  if (module_info->params[trap_module_params_cnt]->long_opt != NULL) { \
278  free(module_info->params[trap_module_params_cnt]->long_opt); \
279  module_info->params[trap_module_params_cnt]->long_opt= NULL; \
280  } \
281  if (module_info->params[trap_module_params_cnt]->description != NULL) { \
282  free(module_info->params[trap_module_params_cnt]->description); \
283  module_info->params[trap_module_params_cnt]->description= NULL; \
284  } \
285  if (module_info->params[trap_module_params_cnt]->argument_type != NULL) { \
286  free(module_info->params[trap_module_params_cnt]->argument_type); \
287  module_info->params[trap_module_params_cnt]->argument_type= NULL; \
288  } \
289  if (module_info->params[trap_module_params_cnt] != NULL) { \
290  free(module_info->params[trap_module_params_cnt]); \
291  } \
292  trap_module_params_cnt++; \
293  }
294 
295 #define GENERATE_GETOPT_STRING(p_short_opt, p_long_opt, p_description, p_required_argument, p_argument_type) \
296  if (module_getopt_string_size <= (strlen(module_getopt_string) + 2)) { \
297  module_getopt_string_size += module_getopt_string_size/2; \
298  module_getopt_string = (char *) realloc(module_getopt_string, module_getopt_string_size * sizeof(char)); \
299  memset(module_getopt_string + (2*module_getopt_string_size)/3, 0, module_getopt_string_size/3); \
300  } \
301  module_getopt_string[strlen(module_getopt_string)] = p_short_opt; \
302  if (p_required_argument == required_argument) { \
303  module_getopt_string[strlen(module_getopt_string)] = ':'; \
304  }
305 
308 #define COUNT_MODULE_PARAMS(p_short_opt, p_long_opt, p_description, p_required_argument, p_argument_type) trap_module_params_cnt++;
309 
315 #define INIT_MODULE_INFO_STRUCT(BASIC, PARAMS) \
316  int trap_module_params_cnt = 0; \
317  size_t module_getopt_string_size = 50; \
318  char * module_getopt_string = (char *) calloc(module_getopt_string_size, sizeof(char)); \
319  module_info = (trap_module_info_t *) calloc(1, sizeof(trap_module_info_t)); \
320  GEN_LONG_OPT_STRUCT(PARAMS); \
321  BASIC(ALLOCATE_BASIC_INFO) \
322  PARAMS(COUNT_MODULE_PARAMS) \
323  if (module_info != NULL) { \
324  module_info->params = (trap_module_info_parameter_t **) calloc(trap_module_params_cnt + 1, sizeof(trap_module_info_parameter_t *)); \
325  if (module_info->params != NULL) { \
326  trap_module_params_cnt = 0; \
327  PARAMS(ALLOCATE_PARAM_ITEMS) \
328  PARAMS(GENERATE_GETOPT_STRING) \
329  } \
330  }
331 
342 trap_module_info_t *trap_create_module_info(const char *mname, const char *mdesc, int8_t i_ifcs, int8_t o_ifcs, uint16_t param_count);
343 
355 int trap_update_module_param(trap_module_info_t *m, uint16_t param_id, char shortopt, const char *longopt, const char *desc, int req_arg, const char *arg_type);
356 
361 #define FREE_MODULE_INFO_STRUCT(BASIC, PARAMS) \
362  if (module_info != NULL) { \
363  trap_module_params_cnt = 0; \
364  BASIC(FREE_BASIC_INFO) \
365  if (module_info->params != NULL) { \
366  PARAMS(FREE_PARAM_ITEMS) \
367  free(module_info->params); \
368  module_info->params = NULL; \
369  } \
370  free(module_info); \
371  module_info = NULL; \
372  if (module_getopt_string != NULL) { \
373  free(module_getopt_string); \
374  } \
375  }
376 
377 
385 #endif
char * name
Name of the module (short string)
int num_ifc_out
Number of output interfaces.
trap_module_info_t * trap_create_module_info(const char *mname, const char *mdesc, int8_t i_ifcs, int8_t o_ifcs, uint16_t param_count)
trap_module_info_parameter_t ** params
struct trap_module_info_s trap_module_info_t
struct trap_module_info_parameter_s trap_module_info_parameter_t
int trap_update_module_param(trap_module_info_t *m, uint16_t param_id, char shortopt, const char *longopt, const char *desc, int req_arg, const char *arg_type)
int num_ifc_in
Number of input interfaces.