Port to new Weston config parser. Install Weston config in /etc/xdg/weston. 29/3529/1
authorOssama Othman <ossama.othman@intel.com>
Tue, 14 May 2013 22:30:15 +0000 (15:30 -0700)
committerOssama Othman <ossama.othman@intel.com>
Tue, 14 May 2013 22:30:15 +0000 (15:30 -0700)
Change-Id: I8fe61e12718392b25a9870e19ac317896a5ddb8d
Signed-off-by: Ossama Othman <ossama.othman@intel.com>
packaging/ico-uxf-weston-plugin.spec
src/config-parser.c
src/ico_ivi_common.c
src/ico_ivi_shell.c
src/ico_plugin_loader.c

index 44a70e7..4e37e7f 100644 (file)
@@ -44,9 +44,9 @@ rm -rf %{buildroot}
 %make_install
 
 # configurations
-%define weston_conf /root/.config
+%define weston_conf %{_sysconfdir}/xdg/weston
 mkdir -p %{buildroot}%{weston_conf}
-install -m 0644 weston.ini.ico %{buildroot}%{weston_conf}
+install -m 0644 weston.ini.ico %{buildroot}%{weston_conf}/weston.ini
 install -m 0644 weston_ivi_plugin.ini %{buildroot}%{weston_conf}
 
 %files
@@ -54,8 +54,8 @@ install -m 0644 weston_ivi_plugin.ini %{buildroot}%{weston_conf}
 %dir %{_libdir}/weston/
 %{_libdir}/weston/*.so
 %{_libdir}/libico-uxf-weston-plugin.so.*
-%{weston_conf}/weston.ini.ico
-%{weston_conf}/weston_ivi_plugin.ini
+%config(noreplace) %{weston_conf}/weston.ini
+%config(noreplace) %{weston_conf}/weston_ivi_plugin.ini
 
 %files devel
 %defattr(-,root,root,-)
index 45ad158..261c425 100644 (file)
  * OF THIS SOFTWARE.
  */
 
+#define _GNU_SOURCE   /* for stchrnul() */
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
 #include <ctype.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include <weston/config-parser.h>
 
@@ -86,7 +92,7 @@ handle_key(const struct config_key *key, const char *value)
 }
 
 int
-parse_config_file(const char *path,
+parse_config_file(int fd,
                  const struct config_section *sections, int num_sections,
                  void *data)
 {
@@ -95,12 +101,17 @@ parse_config_file(const char *path,
        const struct config_section *current = NULL;
        int i;
 
-       fp = fopen(path, "r");
+       if (fd == -1)
+               return -1;
+
+       fp = fdopen(dup(fd), "r");
        if (fp == NULL) {
-               fprintf(stderr, "couldn't open %s\n", path);
+            perror("couldn't open config file");
                return -1;
        }
 
+       rewind(fp);
+
        while (fgets(line, sizeof line, fp)) {
                if (line[0] == '#' || line[0] == '\n') {
                        continue;
@@ -151,37 +162,62 @@ parse_config_file(const char *path,
        return 0;
 }
 
-char *
-config_file_path(const char *name)
+int
+open_config_file(const char *name)
 {
-       const char dotconf[] = "/.config/";
-       const char *config_dir;
-       const char *home_dir;
-       char *path;
-       size_t size;
-
-       config_dir = getenv("XDG_CONFIG_HOME");
-       if (!config_dir) {
-               home_dir = getenv("HOME");
-               if (!home_dir) {
-                       fprintf(stderr, "HOME is not set, using cwd.\n");
-                       return strdup(name);
-               }
+       const char *config_dir  = getenv("XDG_CONFIG_HOME");
+       const char *home_dir    = getenv("HOME");
+       const char *config_dirs = getenv("XDG_CONFIG_DIRS");
+       char path[PATH_MAX];
+       const char *p, *next;
+       int fd;
+
+       /* Precedence is given to config files in the home directory,
+        * and then to directories listed in XDG_CONFIG_DIRS and
+        * finally to the current working directory. */
+
+       /* $XDG_CONFIG_HOME */
+       if (config_dir) {
+               snprintf(path, sizeof path, "%s/%s", config_dir, name);
+               fd = open(path, O_RDONLY | O_CLOEXEC);
+               if (fd >= 0)
+                       return fd;
+       }
+
+       /* $HOME/.config */
+       if (home_dir) {
+               snprintf(path, sizeof path, "%s/.config/%s", home_dir, name);
+               fd = open(path, O_RDONLY | O_CLOEXEC);
+               if (fd >= 0)
+                       return fd;
+       }
 
-               size = strlen(home_dir) + sizeof dotconf + strlen(name);
-               path = malloc(size);
-               if (!path)
-                       return NULL;
+       /* For each $XDG_CONFIG_DIRS: weston/<config_file> */
+       if (!config_dirs)
+               config_dirs = "/etc/xdg";  /* See XDG base dir spec. */
 
-               snprintf(path, size, "%s%s%s", home_dir, dotconf, name);
-               return path;
+       for (p = config_dirs; *p != '\0'; p = next) {
+               next = strchrnul(p, ':');
+               snprintf(path, sizeof path,
+                        "%.*s/weston/%s", (int)(next - p), p, name);
+               fd = open(path, O_RDONLY | O_CLOEXEC);
+               if (fd >= 0)
+                       return fd;
+
+               if (*next == ':')
+                       next++;
        }
 
-       size = strlen(config_dir) + 1 + strlen(name) + 1;
-       path = malloc(size);
-       if (!path)
-               return NULL;
+       /* Current working directory. */
+       snprintf(path, sizeof path, "./%s", name);
+       fd = open(path, O_RDONLY | O_CLOEXEC);
+
+       if (fd >= 0)
+               fprintf(stderr,
+                       "using config in current working directory: %s\n",
+                       path);
+       else
+               fprintf(stderr, "config file \"%s\" not found.\n", name);
 
-       snprintf(path, size, "%s/%s", config_dir, name);
-       return path;
+       return fd;
 }
index c1917f7..5245b04 100644 (file)
@@ -307,14 +307,14 @@ ico_ivi_dispname_2_node(const char *dispname)
 WL_EXPORT int
 module_init(struct weston_compositor *ec)
 {
-    char    *config_file;
+    int config_fd;
 
     uifw_info("ico_ivi_common: Enter(module_init)");
 
     /* Get debug level from config file         */
-    config_file = config_file_path(ICO_IVI_PLUGIN_CONFIG);
-    parse_config_file(config_file, conf_debug, ARRAY_LENGTH(conf_debug), NULL);
-    free(config_file);
+    config_fd = open_config_file(ICO_IVI_PLUGIN_CONFIG);
+    parse_config_file(config_fd, conf_debug, ARRAY_LENGTH(conf_debug), NULL);
+    close(config_fd);
 
     uifw_info("ico_ivi_common: option flag=0x%08x debug=%d",
               ico_option_flag(), ico_ivi_debuglevel());
index e245134..5e69387 100644 (file)
@@ -183,7 +183,7 @@ get_animation_type(char *animation)
 static void
 shell_configuration(struct ivi_shell *shell)
 {
-    char    *config_file;
+    int     config_fd;
     char    *win_animation = NULL;
 
     struct config_key shell_keys[] = {
@@ -195,9 +195,9 @@ shell_configuration(struct ivi_shell *shell)
         { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
     };
 
-    config_file = config_file_path(ICO_IVI_PLUGIN_CONFIG);
-    parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
-    free(config_file);
+    config_fd = open_config_file(ICO_IVI_PLUGIN_CONFIG);
+    parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), shell);
+    close(config_fd);
 
     shell->win_animation_type = get_animation_type(win_animation);
 
index dbba8f4..5f3ac61 100644 (file)
@@ -161,7 +161,7 @@ load_module(struct weston_compositor *ec, const char *path, const char *entry)
 WL_EXPORT int
 module_init(struct weston_compositor *ec)
 {
-    char    *config_file;
+    int     config_fd;
     char    *p;
     char    *end;
     char    buffer[256];
@@ -169,10 +169,10 @@ module_init(struct weston_compositor *ec)
     uifw_info("ico_plugin_loader: Enter(module_init)");
 
     /* get plugin module name from config file(weston_ivi_plugin.ini)   */
-    config_file = config_file_path(ICO_IVI_PLUGIN_CONFIG);
-    parse_config_file(config_file, conf_plugin, ARRAY_LENGTH(conf_plugin), NULL);
-    parse_config_file(config_file, conf_debug, ARRAY_LENGTH(conf_debug), NULL);
-    free(config_file);
+    config_fd = open_config_file(ICO_IVI_PLUGIN_CONFIG);
+    parse_config_file(config_fd, conf_plugin, ARRAY_LENGTH(conf_plugin), NULL);
+    parse_config_file(config_fd, conf_debug, ARRAY_LENGTH(conf_debug), NULL);
+    close(config_fd);
 
     if (modules == NULL)    {
         uifw_error("ico_plugin_loader: Leave(No Plugin in config)");