[Gnucash-changes] r12909 - gnucash/trunk/src - Move the loading of user and system config files from guile to C.

Chris Shoemaker chris at cvs.gnucash.org
Fri Jan 20 00:19:15 EST 2006


Author: chris
Date: 2006-01-20 00:19:15 -0500 (Fri, 20 Jan 2006)
New Revision: 12909
Trac: http://svn.gnucash.org/trac/changeset/12909

Modified:
   gnucash/trunk/src/bin/Makefile.am
   gnucash/trunk/src/bin/gnucash-bin.c
   gnucash/trunk/src/scm/command-line.scm
   gnucash/trunk/src/scm/main.scm
   gnucash/trunk/src/scm/path.scm
Log:
  Move the loading of user and system config files from guile to C.

  Remove the "load config" command line options, since they weren't doing 
  anything different than the default. 


Modified: gnucash/trunk/src/bin/Makefile.am
===================================================================
--- gnucash/trunk/src/bin/Makefile.am	2006-01-20 04:18:09 UTC (rev 12908)
+++ gnucash/trunk/src/bin/Makefile.am	2006-01-20 05:19:15 UTC (rev 12909)
@@ -2,12 +2,17 @@
 SUBDIRS = . overrides test
 
 AM_CFLAGS = -I${top_builddir} ${GLIB_CFLAGS} ${GNOME_CFLAGS} ${GTK_CFLAGS} \
-${QOF_CFLAGS} \
--I${top_srcdir}/src/gnome-utils \
--I${top_srcdir}/src/engine \
--I${top_srcdir}/src/gnome \
--I${top_builddir}/src/gnome-utils \
--I${top_srcdir}/src/gnc-module
+  ${QOF_CFLAGS} \
+  -DGNC_CONFIGDIR=\"${GNC_CONFIGDIR}\" \
+  -DGNC_SHAREDIR=\"${GNC_SHAREDIR}\" \
+  -DGNC_HELPDIR=\"${GNC_HELPDIR}\" \
+  -I${top_srcdir}/src \
+  -I${top_srcdir}/src/app-utils \
+  -I${top_srcdir}/src/gnome-utils \
+  -I${top_srcdir}/src/engine \
+  -I${top_srcdir}/src/gnome \
+  -I${top_builddir}/src/gnome-utils \
+  -I${top_srcdir}/src/gnc-module
 
 bin_PROGRAMS = gnucash-bin
 gnucash_bin_SOURCES = gnucash-bin.c
@@ -16,6 +21,7 @@
 ${top_builddir}/src/engine/libgncmod-engine.la \
 ${top_builddir}/src/gnome/libgncgnome.la \
 ${top_builddir}/src/gnome-utils/libgncmod-gnome-utils.la \
+${top_builddir}/src/app-utils/libgncmod-app-utils.la \
 ${top_builddir}/src/gnc-module/libgncmodule.la
 
 gnucash: gnucash.in ${top_builddir}/config.status Makefile

Modified: gnucash/trunk/src/bin/gnucash-bin.c
===================================================================
--- gnucash/trunk/src/bin/gnucash-bin.c	2006-01-20 04:18:09 UTC (rev 12908)
+++ gnucash/trunk/src/bin/gnucash-bin.c	2006-01-20 05:19:15 UTC (rev 12909)
@@ -35,9 +35,11 @@
 #include "i18n.h"
 #include "gnc-version.h"
 #include "gnc-engine.h"
+#include "gnc-filepath-utils.h"
 #include "gnc-file.h"
 #include "gnc-hooks.h"
 #include "top-level.h"
+#include "gfec.h"
 
 static int gnucash_show_version;
 /* GNUCASH_SVN is defined whenever we're building from an SVN tree */
@@ -60,7 +62,105 @@
            _("The next stable version will be "), "GnuCash 2.0");
 }
 
+/* Priority of paths: The default is set at build time.  It may be
+   overridden by environment variables, which may, in turn, be
+   overridden by command line options.  */
+static char *config_path = GNC_CONFIGDIR;
+static char *share_path = GNC_SHAREDIR;
+static char *help_path = GNC_HELPDIR;
 
+static void
+envt_override()
+{
+    char *path;
+    
+    if ((path = getenv("GNC_CONFIG_PATH")))
+        config_path = path;
+    if ((path = getenv("GNC_SHARE_PATH")))
+        share_path = path;
+    if ((path = getenv("GNC_DOC_PATH")))
+        help_path = path;
+}
+
+static int error_in_scm_eval = FALSE;
+
+static void
+error_handler(const char *msg)
+{
+    g_warning(msg);
+    error_in_scm_eval = TRUE;
+}
+
+static gboolean
+try_load(gchar *fn)
+{    
+    g_message("looking for %s", fn);
+    if (g_file_test(fn, G_FILE_TEST_EXISTS)) {
+        g_message("trying to load %s", fn);
+        error_in_scm_eval = FALSE;
+        gfec_eval_file(fn, error_handler);
+        return !error_in_scm_eval;
+    }
+    return FALSE;
+}
+
+static gboolean
+try_load_config_array(const gchar *fns[])
+{
+    gchar *filename;
+    int i;
+
+    for (i = 0; fns[i]; i++) {
+        filename = gnc_build_dotgnucash_path(fns[i]);
+        if (try_load(filename)) {
+            g_free(filename);
+            return TRUE;
+        }
+        g_free(filename);
+    }
+    return FALSE;
+}
+ 
+static void
+load_system_config(void)
+{
+    static int is_system_config_loaded = FALSE;
+    gchar *system_config;
+
+    if (is_system_config_loaded) return;
+
+    g_message("loading system configuration");
+    system_config = g_build_filename(config_path, "config", NULL);
+    is_system_config_loaded = try_load(system_config);
+    g_free(system_config);
+}
+
+static void
+load_user_config(void)
+{
+    /* Don't continue adding to this list. When 2.0 rolls around bump
+       the 1.4 (unnumbered) files off the list. */
+    static const gchar *user_config_files[] = {
+        "config-2.0.user", "config-1.8.user", "config-1.6.user", 
+        "config.user", "config-2.0.auto", "config-1.8.auto",
+        "config-1.6.auto", "config.auto", NULL};
+    static const gchar *saved_report_files[] = {
+        "saved-reports-2.0", "saved-reports-1.8", NULL};
+    static const gchar *stylesheet_files[] = { "stylesheets-2.0", NULL};
+    static int is_user_config_loaded = FALSE;
+
+    if (is_user_config_loaded) 
+        return;
+    else is_user_config_loaded = TRUE;
+
+    g_message("loading user configuration");
+    try_load_config_array(user_config_files);
+    g_message("loading saved reports");
+    try_load_config_array(saved_report_files);
+    g_message("loading stylesheets");
+    try_load_config_array(stylesheet_files);
+}
+
 /* Note: Command-line argument parsing for Gtk+ applications has
  * evolved.  Gtk+-2.4 and before use the "popt" method.  We use that
  * here for compatibility.  Gnome-2.4 has a way of wrapping the "popt"
@@ -97,21 +197,17 @@
          _("LOGLEVEL")},
         {"nofile", '\0', POPT_ARG_NONE, NULL, 0,
          _("Do not load the last file opened"), NULL},
-        {"config-path", '\0', POPT_ARG_STRING, NULL, 0,
+        {"config-path", '\0', POPT_ARG_STRING, &config_path, 0,
          _("Set configuration path"), _("CONFIGPATH")},
-        {"share-path", '\0', POPT_ARG_STRING, NULL, 0,
+        {"share-path", '\0', POPT_ARG_STRING, &share_path, 0,
          _("Set shared data file search path"), _("SHAREPATH")},
-        {"doc-path", '\0', POPT_ARG_STRING, NULL, 0,
+        {"doc-path", '\0', POPT_ARG_STRING, &help_path, 0,
          _("Set the search path for documentation files"), _("DOCPATH")},
         {"add-price-quotes", '\0', POPT_ARG_STRING, NULL, 0,
          _("Add price quotes to given FILE"), _("FILE")},
         {"namespace", '\0', POPT_ARG_STRING, NULL, 0, 
          _("Regular expression determining which namespace commodities will be retrieved"), 
          _("REGEXP")},
-        {"load-user-config", '\0', POPT_ARG_NONE, NULL, 0,
-         _("Load the user configuration"), NULL},
-        {"load-system-config", '\0', POPT_ARG_NONE, NULL, 0,
-         _("Load the system configuration"), NULL},
         POPT_TABLEEND
     };
     
@@ -180,6 +276,9 @@
     gnc_module_load("gnucash/report/report-gnome", 0);
     gnc_module_load_optional("gnucash/business-gnome", 0);
 
+    load_system_config();
+    load_user_config();
+
     scm_c_eval_string("(gnc:main)");
     shutdown(0);
     return;
@@ -198,6 +297,7 @@
 
     gtk_init (&argc, &argv);
     gnc_module_system_init();
+    envt_override();
     gnucash_command_line(argc, argv);
     gnc_print_unstable_message();
 

Modified: gnucash/trunk/src/scm/command-line.scm
===================================================================
--- gnucash/trunk/src/scm/command-line.scm	2006-01-20 04:18:09 UTC (rev 12908)
+++ gnucash/trunk/src/scm/command-line.scm	2006-01-20 05:19:15 UTC (rev 12909)
@@ -25,7 +25,6 @@
 (define gnc:*arg-no-file* #f)
 (define gnc:*loglevel* #f)
 
-(define gnc:*config-path* #f)
 (define gnc:*share-path* #f)
 (define gnc:*doc-path* #f)
 (define gnc:*namespace-regexp* #f)
@@ -112,14 +111,6 @@
          eq?
          #f))
   
-  (set! gnc:*config-path*
-        (gnc:make-path-config-var
-         (N_ "List of directories to search when looking for config files. \
-Each element must be a string representing a directory or a symbol \
-where 'default expands to the default path, and 'current expands to \
-the current value of the path.")
-         (lambda () gnc:_install-config-path_)))
-  
   (set! gnc:*share-path*
         (gnc:make-path-config-var
          (N_ "List of directories to search when looking for shared data files. \
@@ -143,11 +134,6 @@
 
   ;; Now handle any envt var overrides.
 
-  (and-let* ((envdir (getenv "GNC_CONFIG_PATH"))
-             (data (gnc:read-from-string envdir))
-             ((list? data)))
-    (gnc:config-var-value-set! gnc:*config-path* #f (gnc:flatten data)))
-             
   (and-let* ((envdir (getenv "GNC_SHARE_PATH"))
              (data (gnc:read-from-string envdir))
              ((list? data)))
@@ -194,19 +180,6 @@
          #f
          (N_ "Do not load the last file opened"))
 
-   (list "config-path"
-         'string
-         (lambda (val)
-           (gnc:debug "parsing --config-path " val)
-           (let ((path-list (gnc:read-from-string val)))
-             (if (list? path-list)
-                 (gnc:config-var-value-set! gnc:*config-path* #f path-list)
-                 (begin
-                   (gnc:error "non-list given for --config-path: " val)
-                   (gnc:shutdown 1)))))
-         "CONFIGPATH"
-         (N_ "Set configuration path"))
-
    (list "share-path"
          'string
          (lambda (val)
@@ -260,17 +233,6 @@
          #f
          (N_ "Regular expression determining which namespace commodities will be retrieved"))
 
-   (list "load-user-config"
-         'boolean
-         gnc:load-user-config-if-needed
-         #f
-         (N_ "Load the user configuration"))
-
-   (list "load-system-config"
-         'boolean
-         gnc:load-system-config-if-needed
-         #f
-         (N_ "Load the system configuration"))
 ))
 
 (define (gnc:cmd-line-get-boolean-arg args)

Modified: gnucash/trunk/src/scm/main.scm
===================================================================
--- gnucash/trunk/src/scm/main.scm	2006-01-20 04:18:09 UTC (rev 12908)
+++ gnucash/trunk/src/scm/main.scm	2006-01-20 05:19:15 UTC (rev 12909)
@@ -66,7 +66,6 @@
 (export gnc:current-saved-stylesheets)
 
 ;; from command-line.scm
-(export gnc:*config-path*)
 (export gnc:*share-path*)
 (export gnc:*doc-path*)
 (export gnc:*namespace-regexp*)
@@ -396,12 +395,7 @@
 
   ;; Load the system configs
   (gnc:update-splash-screen (_ "Loading configs..."))
-  (if (not (gnc:load-system-config-if-needed)) ;; from path.scm
-      (gnc:shutdown 1))
 
-  ;; Load the user configs
-  (gnc:load-user-config-if-needed) ;; from path.scm
-
   (gnc:report-menu-setup)
 
   ;; the Welcome to GnuCash "extravaganza" report

Modified: gnucash/trunk/src/scm/path.scm
===================================================================
--- gnucash/trunk/src/scm/path.scm	2006-01-20 04:18:09 UTC (rev 12908)
+++ gnucash/trunk/src/scm/path.scm	2006-01-20 05:19:15 UTC (rev 12909)
@@ -15,77 +15,9 @@
 ;; 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
 ;; Boston, MA  02110-1301,  USA       gnu at gnu.org
 
-(define (gnc:make-dir dir)
-  (if (access? dir X_OK)
-      #t
-      (false-if-exception (mkdir dir #o700))))
-
-(define gnc:current-config-auto
-  (gnc:build-dotgnucash-path "config-2.0.auto"))
-
 (define gnc:current-saved-reports
   (gnc:build-dotgnucash-path "saved-reports-2.0"))
 
 (define gnc:current-saved-stylesheets
   (gnc:build-dotgnucash-path "stylesheets-2.0"))
 
-(define gnc:load-user-config-if-needed
-  (let ((user-config-loaded? #f))
-
-    (define (try-load-no-set file-suffix)
-      (let ((file (gnc:build-dotgnucash-path file-suffix)))
-	(gnc:debug "trying to load " file)
-        (if (access? file F_OK)
-            (if (false-if-exception (primitive-load file))
-		#t
-                (begin
-                  (gnc:warn "failure loading " file)
-                  #f))
-            #f)))
-
-    (define (try-load file-suffix)
-      (if (try-load-no-set file-suffix)
-	  (begin
-	    (set! user-config-loaded? #t)
-	    #t)
-	  #f))
-
-    (lambda ()
-      (if (not user-config-loaded?)
-          (begin
-            (gnc:debug "loading user configuration")
-            (or-map try-load
-		    ;; Don't continue adding to this list. When 2.0
-		    ;; rolls around bump the 1.4 (unnumbered) files
-		    ;; off the list.
-                    '("config-2.0.user" "config-1.8.user"
-		      "config-1.6.user" "config.user"
-                      "config-2.0.auto" "config-1.8.auto"
-		      "config-1.6.auto" "config.auto"))
-	    (gnc:debug "loading saved reports")
-	    (or-map try-load-no-set
-		    '("saved-reports-2.0" "saved-reports-1.8"))
-	    (gnc:debug "loading stylesheets")
-	    (or-map try-load-no-set
-		    '("stylesheets-2.0"))
-	    )))))
-
-;; the system config should probably be loaded from some directory
-;; that wouldn't be a site wide mounted directory, like /usr/share
-;; However, the code below seems to zero in on /usr/share/gnucash/config
-;; ... ahh but that's OK, right ??
-(define gnc:load-system-config-if-needed
-  (let ((system-config-loaded? #f))
-    (lambda ()
-      (if (not system-config-loaded?)
-          (begin
-            (gnc:debug "loading system configuration")
-            
-            (let ((system-config (gnc:find-file
-                                  "config"
-                                  (gnc:config-var-value-get gnc:*config-path*))))
-              (if (false-if-exception (primitive-load system-config))
-                  (set! system-config-loaded? #t)        
-                  (begin
-                    (gnc:warn "failure loading " system-config)
-                    #f))))))))



More information about the gnucash-changes mailing list