summaryrefslogtreecommitdiff
path: root/src/sf.skel/main.c
blob: 5460454f54468236c3bdc1e4c53fe82c7f6fc215 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "../module/module.h"
#include "state.h"

char const *Usage = "usage: %1$s start        [name] [configuration]\n"
                    "       %1$s stop         [name] [configuration] [data]\n"
                    "       %1$s get-endpoint [name] [configuration] [data] [interface]\n"
                    "unsupported commands: cmd mod\n";

static configuration Configuration;
static data          Data;
static bool          ConfigurationLoaded;
static bool          DataLoaded;

// HELPERS

static void
Free_all(void)
{
	if(ConfigurationLoaded == true) Free_configuration(&Configuration);
	if(DataLoaded == true) Free_data(&Data);
}

static void
Load_configuration(ucl_object_t *root)
{
	char *Error = Parse_configuration(&Configuration, root, "");
	if(Error != NULL)
	{
		fprintf(stderr, "%s: configuration error: %s\n", Arg0, Error);
		free(Error);
		Free_all();
		exit(-1);
	}
	ConfigurationLoaded = true;
}

static void
Load_data(ucl_object_t *root)
{
	char *Error = Parse_data(&Data, root, "");
	if(Error != NULL)
	{
		fprintf(stderr, "%s: data error: %s\n", Arg0, Error);
		free(Error);
		Free_all();
		exit(-1);
	}
	DataLoaded = true;
	// TODO: check that data matches configuration
}

// COMMANDS

int
start(char const *Name, ucl_object_t *configuration)
{
	UNUSED(Name);
	Load_configuration(configuration);
	// Implement
	if((true)) goto error;
	jprint_state S;
	bzero(&S, sizeof(S));
	S.F = stdout;
	Save_data(&S, &Data);
	Free_all();
	return 0;
error:
	Free_all();
	return -1;
}

int
stop(char const *Name, ucl_object_t *configuration, ucl_object_t *data)
{
	UNUSED(Name);
	Load_configuration(configuration);
	Load_data(data);
	// Implement
	if((true)) goto error;
	Free_all();
	return 0;
error:
	Free_all();
	return -1;
}

int
get_endpoint(char const *Name, ucl_object_t *configuration, ucl_object_t *data, char const *Interface)
{
	UNUSED(Name, Interface);
	Load_configuration(configuration);
	Load_data(data);
	// Implement
	Free_all();
	return -1;
}

int
cmd(char const *Name, ucl_object_t *configuration, ucl_object_t *data, size_t ArgCount, char **Arg)
{
	UNUSED(Name, ArgCount, Arg);
	Load_configuration(configuration);
	Load_data(data);
	// Implement
	usage();
}

int
mod(char const *Name, ucl_object_t *configuration, ucl_object_t *data, size_t ArgCount, char **Arg)
{
	UNUSED(Name, ArgCount, Arg);
	Load_configuration(configuration);
	Load_data(data);
	// Implement
	usage();
}