diff cinst/mozilla.c @ 147:fc9af77b06b9

Completed profile.ini parser. Debug main() allows to give an mozilla ini file as argument, and outputs extracted profile paths.
author Sascha Wilde <wilde@intevation.de>
date Mon, 24 Mar 2014 17:24:37 +0100
parents ffb20e76e7d0
children a46a4b443410
line wrap: on
line diff
--- a/cinst/mozilla.c	Mon Mar 24 17:23:06 2014 +0100
+++ b/cinst/mozilla.c	Mon Mar 24 17:24:37 2014 +0100
@@ -54,6 +54,7 @@
 #include <string.h>
 
 #include <errorcodes.h>
+#include <portpath.h>
 #include <strhelp.h>
 
 #ifndef _WIN32
@@ -62,6 +63,8 @@
 #define UNIX 0
 #endif
 
+#define LINEBUFLEN 1000
+
 /**
  * @brief Global Return Code
  *
@@ -86,39 +89,77 @@
 get_profile_dirs (char *inifile_name)
 {
   char **dirs = NULL;
+  char *inifile_dirname;
   FILE *inifile;
-  char line[1000];
+  char line[LINEBUFLEN];
+  char *key;
+  char *value;
+  char path[LINEBUFLEN];
+  char *fqpath;
   bool inprofile = false;
+  bool relative_path = false;
 
   if ((inifile = fopen(inifile_name, "r")) != NULL)
     {
-      while (fgets(line, 1000, inifile) != NULL)
+      inifile_dirname = port_dirname(inifile_name);
+      while (fgets(line, LINEBUFLEN, inifile) != NULL)
         {
-          if (strncmp(line, "[Profile", 8) == 0)
-            inprofile = true;
+          /* Determine if we are in an profile section */
+          if (str_starts_with(line, "[Profile"))
+            {
+              relative_path = false;
+              inprofile = true;
+            }
           else if (line[0] == '[')
             inprofile = false;
-          if (inprofile &&
-              (strncmp(line, "Path=", 5) == 0))
-            strv_append(&dirs, line, strlen(line));
+
+          /* If we are in a profile parse path related stuff */
+          if (inprofile)
+            {
+              value = line;
+              key = strsep(&value, "=");
+              str_trim(&value);
+              if (str_equal(key, "Path"))
+                {
+                  if (relative_path)
+                    snprintf(path, LINEBUFLEN, "%s/%s", inifile_dirname, value);
+                  else
+                    strncpy(path, value, LINEBUFLEN);
+                  if ((fqpath = port_realpath(path)) != NULL)
+                    {
+                      strv_append(&dirs, fqpath, strlen(fqpath));
+                      free (fqpath);
+                    }
+                  else
+                    return_code |= WARN_MOZ_PROFILE_DOES_NOT_EXIST;
+                }
+              else if (str_equal(key, "IsRelative") &&
+                       str_starts_with(value, "1"))
+                relative_path = true;
+            }
         }
     }
   else
     {
       return_code |= WARN_MOZ_FAILED_TO_OPEN_INI;
     }
-
   return dirs;
 }
 
 int
-main ()
+main (int argc, char *argv[])
 {
   int x = 0;
-  char **pdirs =
-    get_profile_dirs("/home/wilde/.mozilla/firefox/profiles.ini");
-  while (pdirs[x] != NULL)
-    puts(pdirs[x++]);
-  strv_free(pdirs);
+  if (argc == 2)
+    {
+      char **pdirs =
+        get_profile_dirs(argv[1]);
+      if (pdirs != NULL)
+        {
+          while (pdirs[x] != NULL)
+            puts(pdirs[x++]);
+          strv_free(pdirs);
+        }
+    }
   exit(return_code);
 }

http://wald.intevation.org/projects/trustbridge/