Libecoli  0.11.3
Extensible COmmand LIne library
node.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4 
54 #pragma once
55 
56 #include <stdbool.h>
57 #include <stdio.h>
58 #include <sys/queue.h>
59 #include <sys/types.h>
60 
64 #define EC_NO_ID ""
65 
67 struct ec_node;
68 
70 struct ec_node_iter;
71 
72 struct ec_pnode;
73 struct ec_comp;
74 struct ec_strvec;
75 struct ec_dict;
76 struct ec_config;
77 struct ec_config_schema;
78 
88 #define EC_NODE_TYPE_REGISTER(t) \
89  static void ec_node_init_##t(void); \
90  static void __attribute__((constructor, used)) ec_node_init_##t(void) \
91  { \
92  if (ec_node_type_register(&t, 0) < 0) \
93  fprintf(stderr, "cannot register node type %s\n", t.name); \
94  }
95 
109 #define EC_NODE_TYPE_REGISTER_OVERRIDE(t) \
110  static void ec_node_init_##t(void); \
111  static void __attribute__((constructor, used)) ec_node_init_##t(void) \
112  { \
113  if (ec_node_type_register(&t, 1) < 0) \
114  fprintf(stderr, "cannot register node type %s\n", t.name); \
115  }
116 
138 typedef int (*ec_node_set_config_t)(struct ec_node *node, const struct ec_config *config);
139 
163 typedef int (*ec_parse_t)(
164  const struct ec_node *node,
165  struct ec_pnode *pstate,
166  const struct ec_strvec *strvec
167 );
168 
210 typedef int (*ec_complete_t)(
211  const struct ec_node *node,
212  struct ec_comp *comp,
213  const struct ec_strvec *strvec
214 );
215 
241 typedef char *(*ec_node_desc_t)(const struct ec_node *);
242 
257 typedef int (*ec_node_init_priv_t)(struct ec_node *);
258 
268 typedef void (*ec_node_free_priv_t)(struct ec_node *);
269 
280 typedef size_t (*ec_node_get_children_count_t)(const struct ec_node *);
281 
298 typedef int (*ec_node_get_child_t)(
299  const struct ec_node *,
300  size_t i,
301  struct ec_node **child,
302  unsigned int *refs
303 );
304 
312 struct ec_node_type {
313  TAILQ_ENTRY(ec_node_type) next;
314  const char *name;
317  const struct ec_config_schema *schema;
318  size_t size;
319  ec_node_set_config_t set_config;
320  ec_parse_t parse;
321  ec_complete_t complete;
322  ec_node_desc_t desc;
323  ec_node_init_priv_t init_priv;
324  ec_node_free_priv_t free_priv;
326  ec_node_get_children_count_t get_children_count;
327  ec_node_get_child_t get_child;
328 };
329 
333 TAILQ_HEAD(ec_node_type_list, ec_node_type);
334 
340 extern struct ec_node_type_list node_type_list;
341 
358 int ec_node_type_register(struct ec_node_type *type, bool override);
359 
368 const struct ec_node_type *ec_node_type_lookup(const char *name);
369 
376 void ec_node_type_dump(FILE *out);
377 
387 const struct ec_config_schema *ec_node_type_schema(const struct ec_node_type *type);
388 
397 const char *ec_node_type_name(const struct ec_node_type *type);
398 
411 struct ec_node *ec_node_from_type(const struct ec_node_type *type, const char *id);
412 
426 struct ec_node *ec_node(const char *typename, const char *id);
427 
442 struct ec_node *ec_node_clone(struct ec_node *node);
443 
451 void ec_node_free(struct ec_node *node);
452 
469 int ec_node_set_config(struct ec_node *node, struct ec_config *config);
470 
481 const struct ec_config *ec_node_get_config(const struct ec_node *node);
482 
491 size_t ec_node_get_children_count(const struct ec_node *node);
492 
505 int ec_node_get_child(const struct ec_node *node, size_t i, struct ec_node **child);
506 
515 const struct ec_node_type *ec_node_type(const struct ec_node *node);
516 
528 struct ec_dict *ec_node_attrs(const struct ec_node *node);
529 
538 const char *ec_node_id(const struct ec_node *node);
539 
549 char *ec_node_desc(const struct ec_node *node);
550 
559 void ec_node_dump(FILE *out, const struct ec_node *node);
560 
574 struct ec_node *ec_node_find(struct ec_node *node, const char *id);
575 
614 struct ec_node_iter *ec_node_iter(struct ec_node *node);
615 
630 struct ec_node_iter *
631 ec_node_iter_next(struct ec_node_iter *root, struct ec_node_iter *iter, bool iter_children);
632 
639 void ec_node_iter_free(struct ec_node_iter *iter);
640 
650 
661 
672 int ec_node_check_type(const struct ec_node *node, const struct ec_node_type *type);
673 
682 const char *ec_node_get_type_name(const struct ec_node *node);
683 
692 void *ec_node_priv(const struct ec_node *node);
693 
702 void ec_node_schema_dump(FILE *out, const struct ec_node *node);
703 
struct ec_comp * ec_comp(void)
struct ec_dict * ec_dict(void)
struct ec_node * ec_node_iter_get_node(struct ec_node_iter *iter)
const char * ec_node_id(const struct ec_node *node)
int ec_node_get_child(const struct ec_node *node, size_t i, struct ec_node **child)
void ec_node_iter_free(struct ec_node_iter *iter)
const char * ec_node_get_type_name(const struct ec_node *node)
void ec_node_dump(FILE *out, const struct ec_node *node)
void(* ec_node_free_priv_t)(struct ec_node *)
Definition: node.h:268
struct ec_node * ec_node_from_type(const struct ec_node_type *type, const char *id)
const struct ec_config_schema * ec_node_type_schema(const struct ec_node_type *type)
void ec_node_type_dump(FILE *out)
int ec_node_check_type(const struct ec_node *node, const struct ec_node_type *type)
const struct ec_config * ec_node_get_config(const struct ec_node *node)
struct ec_node_iter * ec_node_iter_next(struct ec_node_iter *root, struct ec_node_iter *iter, bool iter_children)
int ec_node_set_config(struct ec_node *node, struct ec_config *config)
const struct ec_node_type * ec_node_type(const struct ec_node *node)
struct ec_node_type_list node_type_list
int(* ec_node_get_child_t)(const struct ec_node *, size_t i, struct ec_node **child, unsigned int *refs)
Definition: node.h:298
char * ec_node_desc(const struct ec_node *node)
char *(* ec_node_desc_t)(const struct ec_node *)
Definition: node.h:241
size_t ec_node_get_children_count(const struct ec_node *node)
const struct ec_node_type * ec_node_type_lookup(const char *name)
struct ec_node_iter * ec_node_iter(struct ec_node *node)
struct ec_node * ec_node_clone(struct ec_node *node)
void * ec_node_priv(const struct ec_node *node)
int(* ec_node_set_config_t)(struct ec_node *node, const struct ec_config *config)
Definition: node.h:138
int ec_node_type_register(struct ec_node_type *type, bool override)
int(* ec_node_init_priv_t)(struct ec_node *)
Definition: node.h:257
int(* ec_complete_t)(const struct ec_node *node, struct ec_comp *comp, const struct ec_strvec *strvec)
Definition: node.h:210
size_t(* ec_node_get_children_count_t)(const struct ec_node *)
Definition: node.h:280
int(* ec_parse_t)(const struct ec_node *node, struct ec_pnode *pstate, const struct ec_strvec *strvec)
Definition: node.h:163
void ec_node_schema_dump(FILE *out, const struct ec_node *node)
const char * ec_node_type_name(const struct ec_node_type *type)
struct ec_dict * ec_node_attrs(const struct ec_node *node)
struct ec_node * ec_node_find(struct ec_node *node, const char *id)
struct ec_node * ec_node(const char *typename, const char *id)
struct ec_node_iter * ec_node_iter_get_parent(struct ec_node_iter *iter)
void ec_node_free(struct ec_node *node)
struct ec_pnode * ec_pnode(const struct ec_node *node)
struct ec_strvec * ec_strvec(void)