Skip to content

Commit

Permalink
Split --disable-conf into --disable-conffiles & --disable-envvars
Browse files Browse the repository at this point in the history
  • Loading branch information
doegox committed Feb 22, 2013
1 parent fd6d4db commit 7e35498
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
25 changes: 18 additions & 7 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,26 @@ then
AC_DEFINE([LOG], [1], [Enable log])
fi

# Conf support (default:yes)
AC_ARG_ENABLE([conf],AS_HELP_STRING([--disable-conf],[Disable use of config files and environment variables]),[enable_conf=$enableval],[enable_conf="yes"])
AC_MSG_CHECKING(for conf flag)
AC_MSG_RESULT($enable_conf)
AM_CONDITIONAL([WITH_CONF], [test "$enable_conf" != "no"])
# Conffiles support (default:yes)
AC_ARG_ENABLE([conffiles],AS_HELP_STRING([--disable-conffiles],[Disable use of config files]),[enable_conffiles=$enableval],[enable_conffiles="yes"])
AC_MSG_CHECKING(for conffiles flag)
AC_MSG_RESULT($enable_conffiles)
AM_CONDITIONAL([WITH_CONFFILES], [test "$enable_conffiles" != "no"])

if test x"$enable_conf" = "xyes"
if test x"$enable_conffiles" = "xyes"
then
AC_DEFINE([CONF], [1], [Enable conf])
AC_DEFINE([CONFFILES], [1], [Enable conffiles])
fi

# Envvars support (default:yes)
AC_ARG_ENABLE([envvars],AS_HELP_STRING([--disable-envvars],[Disable use of environment variables]),[enable_envvars=$enableval],[enable_envvars="yes"])
AC_MSG_CHECKING(for envvars flag)
AC_MSG_RESULT($enable_envvars)
AM_CONDITIONAL([WITH_ENVVARS], [test "$enable_envvars" != "no"])

if test x"$enable_envvars" = "xyes"
then
AC_DEFINE([ENVVARS], [1], [Enable envvars])
fi

# Debug support (default:no)
Expand Down
4 changes: 2 additions & 2 deletions libnfc/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# include "config.h"
#endif // HAVE_CONFIG_H

#ifdef CONF
#ifdef CONFFILES
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
Expand Down Expand Up @@ -190,5 +190,5 @@ conf_load(nfc_context *context)
conf_devices_load(LIBNFC_DEVICECONFDIR, context);
}

#endif // CONF
#endif // CONFFILES

4 changes: 2 additions & 2 deletions libnfc/log-printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
void
log_init(const nfc_context *context)
{
#ifdef CONF
#ifdef ENVVARS
char str[32];
sprintf(str, "%"PRIu32, context->log_level);
setenv("LIBNFC_LOG_LEVEL", str, 1);
Expand All @@ -52,7 +52,7 @@ void
log_put(const uint8_t group, const char *category, const uint8_t priority, const char *format, ...)
{
char *env_log_level = NULL;
#ifdef CONF
#ifdef ENVVARS
env_log_level = getenv("LIBNFC_LOG_LEVEL");
#endif
uint32_t log_level;
Expand Down
11 changes: 8 additions & 3 deletions libnfc/nfc-internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "config.h"
#endif

#ifdef CONF
#ifdef CONFFILES
#include "conf.h"
#endif

Expand Down Expand Up @@ -88,7 +88,7 @@ nfc_context_new(void)
}
res->user_defined_device_count = 0;

#ifdef CONF
#ifdef ENVVARS
// Load user defined device from environment variable at first
char *envvar = getenv("LIBNFC_DEFAULT_DEVICE");
if (envvar) {
Expand All @@ -97,9 +97,14 @@ nfc_context_new(void)
res->user_defined_device_count++;
}

#endif // ENVVARS

#ifdef CONFFILES
// Load options from configuration file (ie. /etc/nfc/libnfc.conf)
conf_load(res);
#endif // CONFFILES

#ifdef ENVVARS
// Environment variables
// Load "intrusive scan" option
envvar = getenv("LIBNFC_INTRUSIVE_SCAN");
Expand All @@ -110,7 +115,7 @@ nfc_context_new(void)
if (envvar) {
res->log_level = atoi(envvar);
}
#endif // CONF
#endif // ENVVARS

// Initialize log before use it...
log_init(res);
Expand Down
12 changes: 10 additions & 2 deletions libnfc/nfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,15 @@ nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_
{
size_t device_found = 0;

#ifdef CONF
#ifdef CONFFILES
// Load manually configured devices (from config file and env variables)
// TODO From env var...
for (uint32_t i = 0; i < context->user_defined_device_count; i++) {
if (context->user_defined_devices[i].optional) {
// let's make sure the device exists
nfc_device *pnd = NULL;

#ifdef ENVVARS
char *env_log_level = getenv("LIBNFC_LOG_LEVEL");
char *old_env_log_level = NULL;
// do it silently
Expand All @@ -312,13 +314,19 @@ nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_
strcpy(old_env_log_level, env_log_level);
}
setenv("LIBNFC_LOG_LEVEL", "0", 1);
#endif // ENVVARS

pnd = nfc_open(context, context->user_defined_devices[i].connstring);

#ifdef ENVVARS
if (old_env_log_level) {
setenv("LIBNFC_LOG_LEVEL", old_env_log_level, 1);
free(old_env_log_level);
} else {
unsetenv("LIBNFC_LOG_LEVEL");
}
#endif // ENVVARS

if (pnd) {
nfc_close(pnd);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "User device %s found", context->user_defined_devices[i].name);
Expand All @@ -335,7 +343,7 @@ nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_
return device_found;
}
}
#endif // CONF
#endif // CONFFILES

// Device auto-detection
if (context->allow_autoscan) {
Expand Down

0 comments on commit 7e35498

Please sign in to comment.