This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.c
237 lines (209 loc) · 6.1 KB
/
config.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <libconfig.h>
#include "bootstrap.h"
#include "client.h"
#include "config.h"
static char *cfgLocation = "/usr/local/etc/fidistat/config.cfg";
//get the config specified in main.h
void initConf () {
//init config Structure
config_init(&config);
//parse File and watch for Errors
if(! config_read_file(&config, cfgLocation))
{
syslog(LOG_ERR, "%s:%d - %s\n", config_error_file(&config),
config_error_line(&config), config_error_text(&config));
config_destroy(&config);
exit(1);
}
// fill in the SettingsStruct
const char *local;
//get Path to json
if (!config_lookup_string(&config, "path", &local)) {
syslog(LOG_ERR, "Can't lookup path to .json\n");
exit(1);
}
setting.path = strdup(local);
// Path must have trailing /"
if (local[strlen(local)-1] != '/') {
char fixed[strlen(local+1)];
sprintf(fixed, "%s/", local);
setting.path = strdup(fixed);
}
// Number of Stats
int nums = config_setting_length(config_lookup(&config, "settings"));
setting.statNum = nums;
//getMaxCount();
if (!config_lookup_int(&config, "maxEntrys", &setting.maxCount)) {
syslog(LOG_ERR, "Can't find maxEntries");
exit(1);
}
// Interval of CLient
if (!config_lookup_int(&config, "interval", &setting.interval)) {
syslog(LOG_ERR, "Can't find interval");
exit(1);
}
//getLocalBool();
if (!config_lookup_bool(&config, "local", &setting.local)) {
syslog(LOG_ERR, "Can't find local");
exit(1);
}
//getClientName();
if (!config_lookup_string(&config, "clientName", &local)) {
syslog(LOG_ERR, "Can't lookup name\n");
exit(1);
}
setting.clientName = strdup(local);
//getServerPort();
if (!config_lookup_string(&config, "ServerPort", &local)) {
syslog(LOG_ERR, "Can't lookup Server Port\n");
exit(1);
}
setting.serverPort = strdup(local);
//getClientServerURL();
if (!config_lookup_string(&config, "serverURL", &local)) {
syslog(LOG_ERR, "Can't lookup name\n");
exit(1);
}
setting.serverURL = strdup(local);
if (!config_lookup_string(&config, "clientAuth", &local)) {
syslog(LOG_ERR, "Can't lookup name\n");
exit(1);
}
setting.clientAuth = strdup(local);
}
// getter of Setting struct
int getStatNum() {
return setting.statNum;
}
const char* getPath() {
return setting.path;
}
const char* getClientName() {
return setting.clientName;
}
int getMaxCount() {
return setting.maxCount;
}
int getLocal() {
return setting.local;
}
int getInterval() {
return setting.interval;
}
const char* getClientAuth() {
return setting.clientAuth;
}
const char* getClientServerPort() {
return setting.serverPort;
}
const char* getClientServerURL() {
return setting.serverURL;
}
// _v: not safe after config destroyed
const char* getClientCertFile_v() {
const char *lfile;
if (!config_lookup_string(&config, "clientCertFile", &lfile)) {
syslog(LOG_ERR, "Can't lookup Client Cert\n");
exit(1);
}
return lfile;
}
const char* getServerCertFile_v() {
const char *lfile;
if (!config_lookup_string(&config, "ServerCertFile", &lfile)) {
syslog(LOG_ERR, "Can't lookup Server Cert\n");
exit(1);
}
return lfile;
}
int getServerIPv6_v() {
int v6;
if (!config_lookup_bool(&config, "ipv6", &v6)) {
syslog(LOG_ERR, "Can't find ipv6");
exit(1);
}
return v6;
}
// Functions needed for building Status
//-------------------------------------
config_setting_t* getSetting(int id) {
config_setting_t *setting = config_setting_get_elem(config_lookup(&config, "settings"), id);
if (setting == NULL) {
syslog(LOG_ERR, "Can't find %d\n", id);
exit(1);
}
return setting;
}
//Set name of *stat to the ith item of config list
void setConfName(Status *stat, int i) {
stat->id = i;
const char *lname;
if (!config_setting_lookup_string(getSetting(stat->id), "name", &lname)) {
syslog(LOG_ERR, "Can't lookup Name of %s\n", stat->name);
exit(1);
}
stat->name = strdup(lname);
}
//Check if *stat is enabled
void setConfEnable(Status *stat) {
if (!config_setting_lookup_bool(getSetting(stat->id), "enabled", &stat->enabled)) {
syslog(LOG_ERR, "Can't lookup enabled of %s\n", stat->name);
exit(1);
}
}
//Get Command of *stat
void setConfCmmd(Status *stat) {
const char *lcmmd;
if (!config_setting_lookup_string(getSetting(stat->id), "cmmd", &lcmmd)) {
syslog(LOG_ERR, "Can't lookup Command of %s\n", stat->name);
exit(1);
}
stat->cmmd = strdup(lcmmd);
}
void setConfNum(Status *stat) {
char path[25];
sprintf(path, "settings.[%d].sequencetitles" , stat->id);
stat->num = config_setting_length(getLookup(path));
}
void setCSVtitle(Status *stat) {
const char* title;
config_setting_t *setting = getSetting(stat->id);
if (!config_setting_lookup_string(config_setting_get_member(setting, "display"), "title", &title)) {
syslog(LOG_ERR, "Can't lookup Title of %s\n", stat->name);
exit(1);
}
stat->csv = title;
}
//Get Type of *stat
void setConfType(Status *stat) {
const char* type;
config_setting_t *setting = getSetting(stat->id);
if (!config_setting_lookup_string(config_setting_get_member(setting, "display"), "type", &type)) {
syslog(LOG_ERR, "Can't lookup Config Type of %s\n", stat->name);
exit(1);
}
if (!strcmp(type, "line")) {
stat->type = 0;
} else {
if (!strcmp(type, "bar")) {
stat->type = 1;
} else {
if (!strcmp(type, "csv")) {
stat->type = 2;
} else {
syslog(LOG_ERR, "Config Type of %s\n not recognized", stat->name);
exit(1);
}
}
}
}
config_setting_t* getLookup(const char *path) {
return config_lookup(&config, path);
}
void setLocation(char *loc) {
cfgLocation = loc;
}
//Destroy Config
void destroyConf() {
config_destroy(&config);
}