45 #define LOG_CATEGORY "libnfc.config"
46 #define LOG_GROUP NFC_LOG_GROUP_CONFIG
48 #ifndef LIBNFC_SYSCONFDIR
51 #error "SYSCONFDIR is not defined but required."
53 #define LIBNFC_SYSCONFDIR SYSCONFDIR"/nfc"
56 #define LIBNFC_CONFFILE LIBNFC_SYSCONFDIR"/libnfc.conf"
57 #define LIBNFC_DEVICECONFDIR LIBNFC_SYSCONFDIR"/devices.d"
60 escaped_value(
const char line[BUFSIZ],
int i,
char **value)
65 if (line[i] == 0 || line[i] ==
'\n')
68 while (line[i] && line[i] !=
'"') {
74 *value = malloc(c + 1);
77 memset(*value, 0, c + 1);
78 memcpy(*value, &line[i - c], c);
80 while (line[i] && isspace(line[i]))
82 if (line[i] != 0 && line[i] !=
'\n')
93 non_escaped_value(
const char line[BUFSIZ],
int i,
char **value)
96 while (line[i] && !isspace(line[i])) {
100 *value = malloc(c + 1);
103 memset(*value, 0, c + 1);
104 memcpy(*value, &line[i - c], c);
106 while (line[i] && isspace(line[i]))
119 parse_line(
const char line[BUFSIZ],
char **key,
char **value)
127 while (isspace(line[i]))
129 if (line[i] == 0 || line[i] ==
'\n')
133 while (isalnum(line[i]) || line[i] ==
'_' || line[i] ==
'.') {
137 if (c == 0 || line[i] == 0 || line[i] ==
'\n')
139 *key = malloc(c + 1);
142 memset(*key, 0, c + 1);
143 memcpy(*key, &line[i - c], c);
146 while (isspace(line[i]))
151 if (line[i] == 0 || line[i] ==
'\n')
154 while (isspace(line[i]))
156 if (line[i] == 0 || line[i] ==
'\n')
158 if (escaped_value(line, i, value) == 0)
160 else if (non_escaped_value(line, i, value) == 0)
172 conf_parse_file(
const char *filename,
173 void (*conf_keyvalue)(
void *data,
const char *key,
const char *value),
176 FILE *f = fopen(filename,
"r");
178 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO,
"Unable to open file: %s", filename);
184 while (fgets(line, BUFSIZ, f) != NULL) {
193 if (parse_line(line, &key, &value) == 0) {
194 conf_keyvalue(data, key, value);
198 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG,
"Parse error on line #%d: %s", lineno, line);
208 conf_keyvalue_context(
void *data,
const char *key,
const char *value)
211 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG,
"key: [%s], value: [%s]", key, value);
212 if (strcmp(key,
"allow_autoscan") == 0) {
213 string_as_boolean(value, &(context->allow_autoscan));
214 }
else if (strcmp(key,
"allow_intrusive_scan") == 0) {
215 string_as_boolean(value, &(context->allow_intrusive_scan));
216 }
else if (strcmp(key,
"log_level") == 0) {
217 context->log_level = atoi(value);
218 }
else if (strcmp(key,
"device.name") == 0) {
219 if ((context->user_defined_device_count == 0) || strcmp(context->user_defined_devices[context->user_defined_device_count - 1].name,
"") != 0) {
220 if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) {
221 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR,
"%s",
"Configuration exceeded maximum user-defined devices.");
224 context->user_defined_device_count++;
226 strncpy(context->user_defined_devices[context->user_defined_device_count - 1].name, value, DEVICE_NAME_LENGTH - 1);
227 context->user_defined_devices[context->user_defined_device_count - 1].name[DEVICE_NAME_LENGTH - 1] =
'\0';
228 }
else if (strcmp(key,
"device.connstring") == 0) {
229 if ((context->user_defined_device_count == 0) || strcmp(context->user_defined_devices[context->user_defined_device_count - 1].connstring,
"") != 0) {
230 if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) {
231 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR,
"%s",
"Configuration exceeded maximum user-defined devices.");
234 context->user_defined_device_count++;
236 strncpy(context->user_defined_devices[context->user_defined_device_count - 1].connstring, value, NFC_BUFSIZE_CONNSTRING - 1);
237 context->user_defined_devices[context->user_defined_device_count - 1].connstring[NFC_BUFSIZE_CONNSTRING - 1] =
'\0';
238 }
else if (strcmp(key,
"device.optional") == 0) {
239 if ((context->user_defined_device_count == 0) || context->user_defined_devices[context->user_defined_device_count - 1].optional) {
240 if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) {
241 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR,
"%s",
"Configuration exceeded maximum user-defined devices.");
244 context->user_defined_device_count++;
246 if ((strcmp(value,
"true") == 0) || (strcmp(value,
"True") == 0) || (strcmp(value,
"1") == 0))
247 context->user_defined_devices[context->user_defined_device_count - 1].optional =
true;
249 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO,
"Unknown key in config line: %s = %s", key, value);
254 conf_keyvalue_device(
void *data,
const char *key,
const char *value)
257 sprintf(newkey,
"device.%s", key);
258 conf_keyvalue_context(data, newkey, value);
262 conf_devices_load(
const char *dirname,
nfc_context *context)
264 DIR *d = opendir(dirname);
266 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG,
"Unable to open directory: %s", dirname);
269 while ((de = readdir(d)) != NULL) {
271 if (de->d_name[0] !=
'.') {
272 const size_t filename_len = strlen(de->d_name);
273 const size_t extension_len = strlen(
".conf");
274 if ((filename_len > extension_len) &&
275 (strncmp(
".conf", de->d_name + (filename_len - extension_len), extension_len) == 0)) {
276 char filename[BUFSIZ] = LIBNFC_DEVICECONFDIR
"/";
277 strcat(filename, de->d_name);
279 if (stat(filename, &s) == -1) {
283 if (S_ISREG(s.st_mode)) {
284 conf_parse_file(filename, conf_keyvalue_device, context);
296 conf_parse_file(LIBNFC_CONFFILE, conf_keyvalue_context, context);
297 conf_devices_load(LIBNFC_DEVICECONFDIR, context);
Internal defines and macros.
NFC library context Struct which contains internal options, references, pointers, etc....