From 4986b083f2c3f1c2b62a7036d8352ee38273aff5 Mon Sep 17 00:00:00 2001
From: Pawel Szewczyk
Date: Mon, 6 Jul 2015 16:19:45 +0200
Subject: [PATCH] settings: Add parsing setting file
Default setting file is installed in \etc\gt
Change-Id: I68b5dac67f899bbcb166cf52cbd1962e3fa8c15c
Signed-off-by: Pawel Szewczyk
---
packaging/gt.spec | 1 +
source/CMakeLists.txt | 14 ++++++-
source/gt.conf | 14 +++++++
source/main.c | 11 ++++++
source/settings/include/settings.h | 21 ++++++++++
source/settings/src/settings.c | 81 +++++++++++++++++++++++++++++++++++++-
6 files changed, 140 insertions(+), 2 deletions(-)
create mode 100644 source/gt.conf
diff --git a/packaging/gt.spec b/packaging/gt.spec
index 538f4c7..0e58836 100644
--- a/packaging/gt.spec
+++ b/packaging/gt.spec
@@ -33,3 +33,4 @@ make
%license LICENSE
%defattr(-,root,root)
/usr/local/bin/gt
+/etc/gt/gt.conf
diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt
index 3682531..dd39b71 100644
--- a/source/CMakeLists.txt
+++ b/source/CMakeLists.txt
@@ -4,16 +4,21 @@ cmake_minimum_required(VERSION 2.8)
PROJECT(gt)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/base/include)
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/settings/include)
SET(PREFIX ${CMAKE_INSTALL_PREFIX})
SET(BINDIR "${PREFIX}/bin")
+SET(CONFDIR "/etc/gt/")
+SET(CONFFILE "gt.conf")
+ADD_DEFINITIONS("-DGT_SETTING_PATH=\"${CONFDIR}/${CONFFILE}\"")
ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"")
SET(PKG_MODULES
libusbg
glib-2.0
gio-2.0
+ libconfig
)
INCLUDE(FindPkgConfig)
@@ -46,5 +51,12 @@ TARGET_LINK_LIBRARIES( gt
${pkgs_LDFLAGS}
)
-INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${BINDIR})
+IF (NOT EXISTS "${CONFDIR}")
+ INSTALL(DIRECTORY DESTINATION ${CONFDIR})
+ENDIF()
+
+IF (NOT EXISTS "${CONFDIR}/${CONFFILE}")
+ INSTALL(FILES ${CONFFILE} DESTINATION ${CONFDIR})
+ENDIF()
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${BINDIR})
diff --git a/source/gt.conf b/source/gt.conf
new file mode 100644
index 0000000..b5bab60
--- /dev/null
+++ b/source/gt.conf
@@ -0,0 +1,14 @@
+# Default udc to enable gadgets on
+#default-udc="myudc"
+
+# Path where configfs is mounted
+#configfs-path="/sys/kernel/config/"
+
+# List of paths to look for gadget, functions and configs
+#lookup-path=["/etc/gt"]
+
+# Default path to store gadget templates
+#default-template-path"/etc/gt/templates"
+
+# Default gadget name
+#default-gadget="g1"
diff --git a/source/main.c b/source/main.c
index 2c29eb8..39712d0 100644
--- a/source/main.c
+++ b/source/main.c
@@ -26,11 +26,13 @@
#include
#include
#include
+#include
#include "common.h"
#include "parser.h"
#include "backend.h"
#include "executable_command.h"
+#include "settings.h"
char *program_name;
@@ -52,16 +54,25 @@ int main(int argc, char **argv)
int ret;
ExecutableCommand cmd;
char *buf = NULL;
+ config_t cfg;
program_name = program_name_get(argv[0], &buf);
ret = gt_backend_init(program_name, 0);
if (ret < 0)
goto out;
+
+ config_init(&cfg);
+ ret = gt_parse_settings(&cfg);
+ if (ret < 0)
+ goto out1;
+
gt_parse_commands(argc, argv, &cmd);
ret = executable_command_exec(&cmd);
executable_command_clean(&cmd);
+out1:
+ config_destroy(&cfg);
out:
free(buf);
return ret;
diff --git a/source/settings/include/settings.h b/source/settings/include/settings.h
index e3feff5..fe011f8 100644
--- a/source/settings/include/settings.h
+++ b/source/settings/include/settings.h
@@ -17,8 +17,23 @@
#ifndef __GADGET_TOOL_SETTINGS_SETTINGS_H__
#define __GADGET_TOOL_SETTINGS_SETTINGS_H__
+#include
+
#include "command.h"
+/* user settings file */
+#define GT_USER_SETTING_PATH "~/.gt.conf"
+
+struct gt_setting_list {
+ const char *default_udc;
+ const char *configfs_path;
+ const char **lookup_path;
+ const char *default_template_path;
+ const char *default_gadget;
+};
+
+extern struct gt_setting_list gt_settings;
+
/**
* @brief Gets the next possible commands after settings
* @param[in] cmd actual command (should be settings)
@@ -37,4 +52,10 @@ const Command *gt_settings_get_children(const Command *cmd);
*/
int gt_settings_help(void *data);
+/**
+ * @brief Parse settings
+ * @return 0 if success, -1 when error occured
+ */
+int gt_parse_settings(config_t *config);
+
#endif //__GADGET_TOOL_SETTINGS_SETTINGS_H__
diff --git a/source/settings/src/settings.c b/source/settings/src/settings.c
index fffe4c6..c36fec3 100644
--- a/source/settings/src/settings.c
+++ b/source/settings/src/settings.c
@@ -17,16 +17,28 @@
#include
#include
#include
+#include
+#include
#include "settings.h"
#include "common.h"
#include "parser.h"
+/* Some default values for settings are set here.
+ * Settings file will override them if exists. */
+struct gt_setting_list gt_settings = {
+ .default_udc = NULL,
+ .configfs_path = "/sys/kernel/config/",
+ .lookup_path = NULL,
+ .default_template_path = ".",
+ .default_gadget = "g1",
+};
+
static int gt_check_settings_var(const char *name)
{
static const char *vars[] = {
"default-udc",
- "config-fs-path",
+ "configfs-path",
"lookup-path",
"default-template-path",
"default-gadget",
@@ -265,3 +277,70 @@ const Command *gt_settings_get_children(const Command *cmd)
return commands;
}
+
+int gt_parse_settings(config_t *config)
+{
+ config_setting_t *node, *root, *elem;
+ int i, len, ret;
+ struct stat st;
+ const char *filename;
+
+ if (stat(GT_USER_SETTING_PATH, &st) == 0)
+ filename = GT_USER_SETTING_PATH;
+ else
+ filename = GT_SETTING_PATH;
+
+ ret = config_read_file(config, filename);
+ if (ret == CONFIG_FALSE)
+ return -1;
+
+ root = config_root_setting(config);
+
+#define GET_SETTING(name, field) do { \
+ node = config_setting_get_member(root, name); \
+ if (node) { \
+ if (config_setting_type(node) != CONFIG_TYPE_STRING) { \
+ fprintf(stderr, "%s:%d: Expected string\n", \
+ config_setting_source_file(node), \
+ config_setting_source_line(node)); \
+ return -1; \
+ } \
+ gt_settings.field = config_setting_get_string(node); \
+ } \
+} while (0)
+
+ GET_SETTING("default-udc", default_udc);
+ GET_SETTING("configfs-path", configfs_path);
+ GET_SETTING("default-template-path", default_template_path);
+ GET_SETTING("default-gadget", default_gadget);
+
+ node = config_setting_get_member(root, "lookup-path");
+ if (node) {
+ if (config_setting_is_aggregate(node) == CONFIG_FALSE) {
+ fprintf(stderr, "%s:%d: Expected list\n",
+ config_setting_source_file(node),
+ config_setting_source_line(node));
+ return -1;
+ }
+
+ len = config_setting_length(node);
+ gt_settings.lookup_path = calloc(len + 1, sizeof(*gt_settings.lookup_path));
+ for (i = 0; i < len; ++i) {
+ elem = config_setting_get_elem(node, i);
+ if (config_setting_type(elem) != CONFIG_TYPE_STRING) {
+ fprintf(stderr, "%s:%d: Expected string\n",
+ config_setting_source_file(elem),
+ config_setting_source_line(elem));
+ goto out;
+ }
+ gt_settings.lookup_path[i] = config_setting_get_string(elem);
+ }
+ }
+
+#undef GET_SETTING
+
+ return 0;
+out:
+ free(gt_settings.lookup_path);
+ return -1;
+}
--
2.7.4