Psppire: Move Mac OS specific code to its own file
authorJohn Darrington <john@darrington.wattle.id.au>
Sun, 13 Sep 2020 11:46:20 +0000 (13:46 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Sun, 13 Sep 2020 15:10:25 +0000 (17:10 +0200)
src/ui/gui/automake.mk
src/ui/gui/main.c
src/ui/gui/pre-initialisation.h [new file with mode: 0644]

index d3036d887df2138fe198a68457a631ceed2831b5..0fa4afb1bf54952b586e08fe95f86d0322e9696d 100644 (file)
@@ -169,6 +169,7 @@ src_ui_gui_psppire_SOURCES = \
        src/ui/gui/missing-val-dialog.h \
        src/ui/gui/options-dialog.c \
        src/ui/gui/options-dialog.h \
+       src/ui/gui/pre-initialisation.h \
        src/ui/gui/psppire.c \
        src/ui/gui/psppire-data-editor.c \
        src/ui/gui/psppire-data-editor.h \
index a7e09af8064513d130dfd57fd892dd68f4fd1ad1..a0c6c313cdeea79e26acf7620fb3d728825166e2 100644 (file)
 
 #include <config.h>
 
+#include "pre-initialisation.h"
+
 #include "ui/gui/psppire.h"
 
 #include <gtk/gtk.h>
 #include <stdlib.h>
-#if ENABLE_RELOCATABLE && defined(__APPLE__)
-#include <sys/stat.h>
-#endif
 
 #include "language/lexer/include-path.h"
 #include "libpspp/argv-parser.h"
@@ -304,7 +303,7 @@ process_pre_start_arguments (int *argc, char ***argv)
   GOptionEntry oe[] = {
     {"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
      show_version_and_exit, N_("Show version information and exit"), 0},
-    {NULL}
+    {NULL, 0, 0, 0, NULL, "", 0}
   };
 
   GOptionContext *oc = g_option_context_new ("");
@@ -315,87 +314,11 @@ process_pre_start_arguments (int *argc, char ***argv)
   g_option_context_free (oc);
 }
 
-#if ENABLE_RELOCATABLE && defined(__APPLE__)
-static void
-pspp_macos_setenv (const char * progname)
-{
-  /* helper to set environment variables for pspp to be relocatable.
-   * Due to the latest changes it is not recommended to set it in the shell
-   * wrapper anymore.
-   */
-  gchar resolved_path[PATH_MAX];
-  /* on some OSX installations open file limit is 256 and GIMP needs more */
-  struct rlimit limit;
-  limit.rlim_cur = 10000;
-  limit.rlim_max = 10000;
-  setrlimit (RLIMIT_NOFILE, &limit);
-  if (realpath (progname, resolved_path))
-    {
-      gchar  tmp[PATH_MAX];
-      gchar *app_dir;
-      gchar  res_dir[PATH_MAX];
-      struct stat sb;
-
-      app_dir = g_path_get_dirname (resolved_path);
-      g_snprintf (tmp, sizeof(tmp), "%s/../../Resources", app_dir);
-      if (realpath (tmp, res_dir) && !stat (res_dir,&sb) && S_ISDIR (sb.st_mode))
-        g_print ("pspp is started as MacOS application\n");
-      else
-        return;
-      g_free (app_dir);
-
-      g_snprintf (tmp, sizeof(tmp), "%s/lib/gtk-3.0/3.0.0", res_dir);
-      g_setenv ("GTK_PATH", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/etc/gtk-3.0/gtk.immodules", res_dir);
-      g_setenv ("GTK_IM_MODULE_FILE", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/lib/gegl-0.4", res_dir);
-      g_setenv ("GEGL_PATH", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/lib/babl-0.1", res_dir);
-      g_setenv ("BABL_PATH", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", res_dir);
-      g_setenv ("GDK_PIXBUF_MODULE_FILE", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/etc/fonts", res_dir);
-      g_setenv ("FONTCONFIG_PATH", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/lib/gio/modules", res_dir);
-      g_setenv ("GIO_MODULE_DIR", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/etc/xdg", res_dir);
-      g_setenv ("XDG_CONFIG_DIRS", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/share", res_dir);
-      g_setenv ("XDG_DATA_DIRS", tmp, TRUE);
-
-      if (g_getenv ("HOME")!=NULL)
-        {
-          g_snprintf (tmp, sizeof(tmp),
-                      "%s/Library/Application Support/pspp/1.3/cache",
-                      g_getenv("HOME"));
-          g_setenv ("XDG_CACHE_HOME", tmp, TRUE);
-        }
-    }
-}
-#endif
-
 int
 main (int argc, char *argv[])
 {
-
-#if ENABLE_RELOCATABLE && defined(__APPLE__)
-  /* remove MacOS session identifier from the command line args */
-  gint newargc = 0;
-  for (gint i = 0; i < argc; i++)
-    {
-      if (!g_str_has_prefix (argv[i], "-psn_"))
-        {
-          argv[newargc] = argv[i];
-          newargc++;
-        }
-    }
-  if (argc > newargc)
-    {
-      argv[newargc] = NULL; /* glib expects NULL terminated array */
-      argc = newargc;
-    }
-  pspp_macos_setenv (argv[0]);
-#endif
+  /* Some operating systems need to munge the arguments.  */
+  pre_initialisation (&argc, argv);
 
   set_program_name (argv[0]);
 
diff --git a/src/ui/gui/pre-initialisation.h b/src/ui/gui/pre-initialisation.h
new file mode 100644 (file)
index 0000000..de50141
--- /dev/null
@@ -0,0 +1,105 @@
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2020  Free Software Foundation
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#if !ENABLE_RELOCATABLE || !defined(__APPLE__)
+
+static inline void
+pre_initialisation (int *argc, char **argv)
+{
+}
+
+#else
+
+#include <sys/stat.h>
+#include <glib.h>
+
+static inline void
+pre_initialisation (int *argc, char **argv)
+{
+  /* remove MacOS session identifier from the command line args */
+  gint newargc = 0;
+  for (gint i = 0; i < *argc; i++)
+    {
+      if (!g_str_has_prefix (argv[i], "-psn_"))
+        {
+          argv[newargc] = argv[i];
+          newargc++;
+        }
+    }
+  if (*argc > newargc)
+    {
+      argv[newargc] = NULL; /* glib expects NULL terminated array */
+      *argc = newargc;
+    }
+
+  const char * progname = argv[0];
+
+  /* helper to set environment variables for pspp to be relocatable.
+   * Due to the latest changes it is not recommended to set it in the shell
+   * wrapper anymore.
+   */
+  gchar resolved_path[PATH_MAX];
+  /* on some OSX installations open file limit is 256 and GIMP needs more */
+  struct rlimit limit;
+  limit.rlim_cur = 10000;
+  limit.rlim_max = 10000;
+  setrlimit (RLIMIT_NOFILE, &limit);
+  if (realpath (progname, resolved_path))
+    {
+      gchar  tmp[PATH_MAX];
+      gchar *app_dir;
+      gchar  res_dir[PATH_MAX];
+      struct stat sb;
+
+      app_dir = g_path_get_dirname (resolved_path);
+      g_snprintf (tmp, sizeof(tmp), "%s/../../Resources", app_dir);
+      if (realpath (tmp, res_dir) && !stat (res_dir,&sb) && S_ISDIR (sb.st_mode))
+        g_print ("pspp is started as MacOS application\n");
+      else
+        return;
+      g_free (app_dir);
+
+      g_snprintf (tmp, sizeof(tmp), "%s/lib/gtk-3.0/3.0.0", res_dir);
+      g_setenv ("GTK_PATH", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/etc/gtk-3.0/gtk.immodules", res_dir);
+      g_setenv ("GTK_IM_MODULE_FILE", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/lib/gegl-0.4", res_dir);
+      g_setenv ("GEGL_PATH", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/lib/babl-0.1", res_dir);
+      g_setenv ("BABL_PATH", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", res_dir);
+      g_setenv ("GDK_PIXBUF_MODULE_FILE", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/etc/fonts", res_dir);
+      g_setenv ("FONTCONFIG_PATH", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/lib/gio/modules", res_dir);
+      g_setenv ("GIO_MODULE_DIR", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/etc/xdg", res_dir);
+      g_setenv ("XDG_CONFIG_DIRS", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/share", res_dir);
+      g_setenv ("XDG_DATA_DIRS", tmp, TRUE);
+
+      if (g_getenv ("HOME")!=NULL)
+        {
+          g_snprintf (tmp, sizeof(tmp),
+                      "%s/Library/Application Support/pspp/1.3/cache",
+                      g_getenv("HOME"));
+          g_setenv ("XDG_CACHE_HOME", tmp, TRUE);
+        }
+    }
+}
+#endif