Commits

jjacky committed 30a1179

fixes/changes parsing conf files; kalu updater always used /etc/pacman.conf (instead of whatever is set in Preferences), fixed

- parsing kalu.conf would report & stop on first error, now it ignores the line, continues parsing, and report all errors
- fixed memory leak when parsing config files

Comments (0)

Files changed (5)

     debug ("config: %s: %s", option, value);
 }
 
-#define set_error(fmt, ...)  g_set_error (error, KALU_ERROR, 1, \
-    "Config file %s, line %d: " fmt, file, linenum, __VA_ARGS__);
+#define add_error(fmt, ...) do {                                        \
+        if (!err_msg)                                                   \
+        {                                                               \
+            err_msg = g_string_sized_new (1024);                        \
+        }                                                               \
+        g_string_append_printf (err_msg,                                \
+                                "Config file %s, line %d: " fmt "\n",   \
+                                file, linenum, __VA_ARGS__);            \
+    } while (0)
 /** inspired from pacman's function */
 gboolean
 parse_config_file (const char       *file,
 	int         linenum         = 0;
     char       *section         = NULL;
 	int         success         = TRUE;
+    GString    *err_msg         = NULL;
 
 	debug ("config: attempting to read file %s", file);
 	fp = fopen (file, "r");
 			/* only possibility here is a line == '[]' */
 			if (line_len <= 2)
             {
-                set_error ("%s", "bad section name");
-				success = FALSE;
-				goto cleanup;
+                add_error ("%s", "bad section name");
+                free (section);
+                section = NULL;
+                continue;
 			}
 			/* new config section, skip the '[' */
+            free (section);
 			section = strdup (line + 1);
 			section[line_len - 2] = '\0';
             
 
 		if (key == NULL)
         {
-            set_error ("%s", "syntax error: missing key");
-			success = FALSE;
-			goto cleanup;
+            add_error ("%s", "syntax error: missing key");
+			continue;
 		}
         
         /* kalu.conf*/
         {
             if (value == NULL)
             {
-                set_error ("value missing for %s", key);
-                success = FALSE;
-                goto cleanup;
+                add_error ("value missing for %s", key);
+                continue;
             }
             else if (strcmp ("options", section) == 0)
             {
                         int timeout = atoi (value);
                         if (timeout < 4 || timeout > 42)
                         {
-                            set_error ("Invalid timeout delay: %s", value);
-                            success = FALSE;
-                            goto cleanup;
+                            add_error ("Invalid timeout delay: %s", value);
+                            continue;
                         }
                         config->timeout = timeout * 1000; /* from seconds to ms */
                     }
                             || end_hour < 0 || end_hour > 23
                             || end_minute < 0 || end_minute > 59)
                         {
-                            set_error ("invalid value for SkipPeriod: %s", value);
-                            success = FALSE;
-                            goto cleanup;
+                            add_error ("invalid value for SkipPeriod: %s", value);
+                            continue;
                         }
                         config->has_skip = TRUE;
                         config->skip_begin_hour   = begin_hour;
                     }
                     else
                     {
-                        set_error ("unable to parse SkipPeriod (must be HH:MM-HH:MM) : %s",
+                        add_error ("unable to parse SkipPeriod (must be HH:MM-HH:MM) : %s",
                                    value);
-                        success = FALSE;
-                        goto cleanup;
+                        continue;
                     }
                 }
                 else if (strcmp (key, "UpgradeAction") == 0)
                     }
                     else
                     {
-                        set_error ("Invalid value for UpgradeAction: %s", value);
-                        success = FALSE;
-                        goto cleanup;
+                        add_error ("Invalid value for UpgradeAction: %s", value);
+                        continue;
                     }
                     debug ("config: action: %d", config->action);
                 }
                         }
                         else
                         {
-                            set_error ("unknown value for %s: %s", key, v);
-                            success = FALSE;
-                            goto cleanup;
+                            add_error ("unknown value for %s: %s", key, v);
+                            continue;
                         }
                         
                         if (s)
                     }
                     else
                     {
-                        set_error ("unknown value for %s: %s", key, value);
-                        success = FALSE;
-                        goto cleanup;
+                        add_error ("unknown value for %s: %s", key, value);
+                        continue;
                     }
                     debug ("config: %s: %d", key, *on_click);
                 }
                 }
                 else
                 {
-                    set_error ("unknown option: %s", key);
-                    success = FALSE;
-                    goto cleanup;
+                    add_error ("unknown option: %s", key);
+                    continue;
                 }
             }
             else
                 }
                 else
                 {
-                    set_error ("unknown section: %s", section);
-                    success = FALSE;
-                    goto cleanup;
+                    add_error ("unknown section: %s", section);
+                    continue;
                 }
                 
                 /* we're in a valid template */
         {
             if (value == NULL)
             {
-                set_error ("watched package %s: version number missing", key);
-                success = FALSE;
-                goto cleanup;
+                add_error ("watched package %s: version number missing", key);
+                continue;
             }
             else
             {
         {
             if (value == NULL)
             {
-                set_error ("news data: value missing for %s", key);
-                success = FALSE;
-                goto cleanup;
+                add_error ("news data: value missing for %s", key);
+                continue;
             }
             else if (strcmp ("Last", key) == 0)
             {
     {
 		fclose(fp);
 	}
+    free (section);
     if (config->action == UPGRADE_ACTION_CMDLINE && config->cmdline == NULL)
     {
         #ifndef DISABLE_UPDATER
         #endif
         debug ("config: action: no cmdline set, reverting to %d", config->action);
     }
+    if (err_msg)
+    {
+        g_set_error (error, KALU_ERROR, 1, "%s", err_msg->str);
+        g_string_free (err_msg, TRUE);
+        err_msg = NULL;
+        success = FALSE;
+    }
 	debug ("config: finished parsing %s", file);
 	return success;
 }
-#undef set_error
+#undef add_error
     curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME "/" PACKAGE_VERSION);
     curl_easy_setopt (curl, CURLOPT_URL, url);
     curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 1);
-    curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, curl_write);
+    curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, (curl_write_callback) curl_write);
     curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) &data);
     curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, errmsg);
     
 #ifndef DISABLE_UPDATER
 #define run_updater()   do {                \
         set_kalpm_busy (TRUE);              \
-        updater_run (config->cmdline_post); \
+        updater_run (config->pacmanconf,    \
+                     config->cmdline_post); \
     } while (0)
 #endif
 
     snprintf (conffile, MAX_PATH - 1, "%s/.config/kalu/kalu.conf", g_get_home_dir ());
     if (!parse_config_file (conffile, CONF_FILE_KALU, &error))
     {
-        show_error ("Unable to parse configuration", error->message, NULL);
+        show_error ("Errors while parsing configuration", error->message, NULL);
         g_clear_error (&error);
     }
     /* parse watched */
 }
 
 void
-updater_run (alpm_list_t *cmdline_post)
+updater_run (const gchar *conffile, alpm_list_t *cmdline_post)
 {
     updater = calloc (1, sizeof (*updater));
     updater->cmdline_post = cmdline_post;
     /* parse pacman.conf */
     GError *error = NULL;
     pacman_config_t *pac_conf = NULL;
-    add_log (LOGTYPE_UNIMPORTANT, "Parsing pacman.conf...");
-    if (!parse_pacman_conf ("/etc/pacman.conf", NULL, 0, 0, &pac_conf, &error))
+    add_log (LOGTYPE_UNIMPORTANT, "Parsing %s ...", conffile);
+    if (!parse_pacman_conf (conffile, NULL, 0, 0, &pac_conf, &error))
     {
         add_log (LOGTYPE_UNIMPORTANT, " failed\n");
-        _show_error ("Unable to parse pacman.conf", "%s", error->message);
+        _show_error ("Unable to parse pacman.conf", "%s: %s", conffile, error->message);
         g_clear_error (&error);
         free_pacman_config (pac_conf);
         gtk_widget_destroy (window);
 #include <alpm_list.h>
 
 void
-updater_run (alpm_list_t *cmdline_post);
+updater_run (const gchar *conffile, alpm_list_t *cmdline_post);
 
 #endif /* _KALU_UPDATER_H */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.