settings: Add parsing setting file
authorPawel Szewczyk <p.szewczyk@samsung.com>
Mon, 6 Jul 2015 14:19:45 +0000 (16:19 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 28 Jul 2015 16:06:53 +0000 (18:06 +0200)
Default setting file is installed in \etc\gt

Change-Id: I68b5dac67f899bbcb166cf52cbd1962e3fa8c15c
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
packaging/gt.spec
source/CMakeLists.txt
source/gt.conf [new file with mode: 0644]
source/main.c
source/settings/include/settings.h
source/settings/src/settings.c

index 538f4c7..0e58836 100644 (file)
@@ -33,3 +33,4 @@ make
 %license LICENSE
 %defattr(-,root,root)
 /usr/local/bin/gt
+/etc/gt/gt.conf
index 3682531..dd39b71 100644 (file)
@@ -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 (file)
index 0000000..b5bab60
--- /dev/null
@@ -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"
index 2c29eb8..39712d0 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <libgen.h>
+#include <libconfig.h>
 
 #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;
index e3feff5..fe011f8 100644 (file)
 #ifndef __GADGET_TOOL_SETTINGS_SETTINGS_H__
 #define __GADGET_TOOL_SETTINGS_SETTINGS_H__
 
+#include <libconfig.h>
+
 #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__
index fffe4c6..c36fec3 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <libconfig.h>
+#include <sys/stat.h>
 
 #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;
+}