standardize copyright
[protos/libecoli.git] / lib / ecoli_node_weakref.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <stdarg.h>
10 #include <errno.h>
11
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_test.h>
15 #include <ecoli_strvec.h>
16 #include <ecoli_node.h>
17 #include <ecoli_parsed.h>
18 #include <ecoli_completed.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_node_option.h>
21 #include <ecoli_node_weakref.h>
22
23 EC_LOG_TYPE_REGISTER(node_weakref);
24
25 struct ec_node_weakref {
26         struct ec_node gen;
27         struct ec_node *child;
28 };
29
30 static int
31 ec_node_weakref_parse(const struct ec_node *gen_node,
32                 struct ec_parsed *state,
33                 const struct ec_strvec *strvec)
34 {
35         struct ec_node_weakref *node = (struct ec_node_weakref *)gen_node;
36
37         return ec_node_parse_child(node->child, state, strvec);
38 }
39
40 static int
41 ec_node_weakref_complete(const struct ec_node *gen_node,
42                         struct ec_completed *completed,
43                         const struct ec_strvec *strvec)
44 {
45         struct ec_node_weakref *node = (struct ec_node_weakref *)gen_node;
46
47         return ec_node_complete_child(node->child, completed, strvec);
48 }
49
50 static struct ec_node_type ec_node_weakref_type = {
51         .name = "weakref",
52         .parse = ec_node_weakref_parse,
53         .complete = ec_node_weakref_complete,
54         .size = sizeof(struct ec_node_weakref),
55 };
56
57 EC_NODE_TYPE_REGISTER(ec_node_weakref_type);
58
59 int ec_node_weakref_set(struct ec_node *gen_node, struct ec_node *child)
60 {
61         struct ec_node_weakref *node = (struct ec_node_weakref *)gen_node;
62
63         assert(node != NULL);
64
65         if (child == NULL) {
66                 errno = EINVAL;
67                 goto fail;
68         }
69
70         if (ec_node_check_type(gen_node, &ec_node_weakref_type) < 0)
71                 goto fail;
72
73         node->child = child;
74
75         return 0;
76
77 fail:
78         ec_node_free(child);
79         return -1;
80 }
81
82 struct ec_node *ec_node_weakref(const char *id, struct ec_node *child)
83 {
84         struct ec_node *gen_node = NULL;
85
86         if (child == NULL)
87                 return NULL;
88
89         gen_node = __ec_node(&ec_node_weakref_type, id);
90         if (gen_node == NULL)
91                 return NULL;
92
93         ec_node_weakref_set(gen_node, child);
94
95         return gen_node;
96 }
97
98 /* LCOV_EXCL_START */
99 static int ec_node_weakref_testcase(void)
100 {
101         //XXX weakref testcase
102         return 0;
103 }
104 /* LCOV_EXCL_STOP */
105
106 static struct ec_test ec_node_weakref_test = {
107         .name = "node_weakref",
108         .test = ec_node_weakref_testcase,
109 };
110
111 EC_TEST_REGISTER(ec_node_weakref_test);