api documentation for ec_parse
[protos/libecoli.git] / src / ecoli_init.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <assert.h>
9
10 #include <ecoli_init.h>
11
12 static struct ec_init_list init_list = TAILQ_HEAD_INITIALIZER(init_list);
13
14 /* register an init function */
15 void ec_init_register(struct ec_init *init)
16 {
17         struct ec_init *cur;
18
19         if (TAILQ_EMPTY(&init_list)) {
20                 TAILQ_INSERT_HEAD(&init_list, init, next);
21                 return;
22         }
23
24
25         TAILQ_FOREACH(cur, &init_list, next) {
26                 if (init->priority > cur->priority)
27                         continue;
28
29                 TAILQ_INSERT_BEFORE(cur, init, next);
30                 return;
31         }
32
33         TAILQ_INSERT_TAIL(&init_list, init, next);
34 }
35
36 int ec_init(void)
37 {
38         struct ec_init *init;
39
40         TAILQ_FOREACH(init, &init_list, next) {
41                 if (init->init != NULL && init->init() < 0)
42                         return -1;
43         }
44
45         return 0;
46 }
47
48 void ec_exit(void)
49 {
50         struct ec_init *init;
51
52         TAILQ_FOREACH_REVERSE(init, &init_list, ec_init_list, next) {
53                 if (init->exit != NULL)
54                         init->exit();
55         }
56 }