Libecoli  0.10.1
Extensible COmmand LIne library
node.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4 
49 #pragma once
50 
51 #include <stdbool.h>
52 #include <stdio.h>
53 #include <sys/queue.h>
54 #include <sys/types.h>
55 
59 #define EC_NO_ID ""
60 
62 struct ec_node;
63 
64 struct ec_pnode;
65 struct ec_comp;
66 struct ec_strvec;
67 struct ec_dict;
68 struct ec_config;
69 struct ec_config_schema;
70 
80 #define EC_NODE_TYPE_REGISTER(t) \
81  static void ec_node_init_##t(void); \
82  static void __attribute__((constructor, used)) ec_node_init_##t(void) \
83  { \
84  if (ec_node_type_register(&t, 0) < 0) \
85  fprintf(stderr, "cannot register node type %s\n", t.name); \
86  }
87 
101 #define EC_NODE_TYPE_REGISTER_OVERRIDE(t) \
102  static void ec_node_init_##t(void); \
103  static void __attribute__((constructor, used)) ec_node_init_##t(void) \
104  { \
105  if (ec_node_type_register(&t, 1) < 0) \
106  fprintf(stderr, "cannot register node type %s\n", t.name); \
107  }
108 
130 typedef int (*ec_node_set_config_t)(struct ec_node *node, const struct ec_config *config);
131 
155 typedef int (*ec_parse_t)(
156  const struct ec_node *node,
157  struct ec_pnode *pstate,
158  const struct ec_strvec *strvec
159 );
160 
202 typedef int (*ec_complete_t)(
203  const struct ec_node *node,
204  struct ec_comp *comp,
205  const struct ec_strvec *strvec
206 );
207 
233 typedef char *(*ec_node_desc_t)(const struct ec_node *);
234 
249 typedef int (*ec_node_init_priv_t)(struct ec_node *);
250 
260 typedef void (*ec_node_free_priv_t)(struct ec_node *);
261 
272 typedef size_t (*ec_node_get_children_count_t)(const struct ec_node *);
273 
290 typedef int (*ec_node_get_child_t)(
291  const struct ec_node *,
292  size_t i,
293  struct ec_node **child,
294  unsigned int *refs
295 );
296 
304 struct ec_node_type {
305  TAILQ_ENTRY(ec_node_type) next;
306  const char *name;
309  const struct ec_config_schema *schema;
310  size_t size;
311  ec_node_set_config_t set_config;
312  ec_parse_t parse;
313  ec_complete_t complete;
314  ec_node_desc_t desc;
315  ec_node_init_priv_t init_priv;
316  ec_node_free_priv_t free_priv;
318  ec_node_get_children_count_t get_children_count;
319  ec_node_get_child_t get_child;
320 };
321 
325 TAILQ_HEAD(ec_node_type_list, ec_node_type);
326 
331 extern struct ec_node_type_list node_type_list;
332 
349 int ec_node_type_register(struct ec_node_type *type, bool override);
350 
359 const struct ec_node_type *ec_node_type_lookup(const char *name);
360 
367 void ec_node_type_dump(FILE *out);
368 
378 const struct ec_config_schema *ec_node_type_schema(const struct ec_node_type *type);
379 
388 const char *ec_node_type_name(const struct ec_node_type *type);
389 
402 struct ec_node *ec_node_from_type(const struct ec_node_type *type, const char *id);
403 
417 struct ec_node *ec_node(const char *typename, const char *id);
418 
432 struct ec_node *ec_node_clone(struct ec_node *node);
433 
440 void ec_node_free(struct ec_node *node);
441 
458 int ec_node_set_config(struct ec_node *node, struct ec_config *config);
459 
470 const struct ec_config *ec_node_get_config(const struct ec_node *node);
471 
480 size_t ec_node_get_children_count(const struct ec_node *node);
481 
494 int ec_node_get_child(const struct ec_node *node, size_t i, struct ec_node **child);
495 
504 const struct ec_node_type *ec_node_type(const struct ec_node *node);
505 
517 struct ec_dict *ec_node_attrs(const struct ec_node *node);
518 
527 const char *ec_node_id(const struct ec_node *node);
528 
538 char *ec_node_desc(const struct ec_node *node);
539 
548 void ec_node_dump(FILE *out, const struct ec_node *node);
549 
563 struct ec_node *ec_node_find(struct ec_node *node, const char *id);
564 
583 struct ec_node *ec_node_find_next(struct ec_node *root, const struct ec_node *prev, const char *id);
584 
595 int ec_node_check_type(const struct ec_node *node, const struct ec_node_type *type);
596 
605 const char *ec_node_get_type_name(const struct ec_node *node);
606 
615 void *ec_node_priv(const struct ec_node *node);
616 
625 void ec_node_schema_dump(FILE *out, const struct ec_node *node);
626 
struct ec_comp * ec_comp(void)
struct ec_dict * ec_dict(void)
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)
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:260
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)
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:290
char * ec_node_desc(const struct ec_node *node)
char *(* ec_node_desc_t)(const struct ec_node *)
Definition: node.h:233
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 * 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:130
int ec_node_type_register(struct ec_node_type *type, bool override)
int(* ec_node_init_priv_t)(struct ec_node *)
Definition: node.h:249
int(* ec_complete_t)(const struct ec_node *node, struct ec_comp *comp, const struct ec_strvec *strvec)
Definition: node.h:202
size_t(* ec_node_get_children_count_t)(const struct ec_node *)
Definition: node.h:272
struct ec_node * ec_node_find_next(struct ec_node *root, const struct ec_node *prev, const char *id)
int(* ec_parse_t)(const struct ec_node *node, struct ec_pnode *pstate, const struct ec_strvec *strvec)
Definition: node.h:155
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)
void ec_node_free(struct ec_node *node)
struct ec_pnode * ec_pnode(const struct ec_node *node)
struct ec_strvec * ec_strvec(void)