Greatly simplify PSPP configuration.
[pspp-builds.git] / src / ui / source-init-opts.c
index 61a108e0eec65cb4686fca5d8f1932024dfd771a..c91a9fef1a8956c4bd856d58fa26fd998194df69 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2008  Free Software Foundation
+   Copyright (C) 2008, 2010  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
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <argp.h>
+
 #include "source-init-opts.h"
+
 #include <stdbool.h>
-#include <xalloc.h>
-#include <string.h>
-#include <data/file-name.h>
-#include <libpspp/getl.h>
-#include <language/syntax-file.h>
 #include <stdlib.h>
-#include <libpspp/llx.h>
-#include <data/por-file-reader.h>
-#include <data/sys-file-reader.h>
-#include <libpspp/message.h>
-#include <ui/syntax-gen.h>
-#include <language/syntax-string-source.h>
-#include <data/file-name.h>
-#include <data/settings.h>
+#include <string.h>
+
+#include "data/file-name.h"
+#include "data/por-file-reader.h"
+#include "data/settings.h"
+#include "data/sys-file-reader.h"
+#include "language/syntax-file.h"
+#include "language/syntax-string-source.h"
+#include "libpspp/assertion.h"
+#include "libpspp/argv-parser.h"
+#include "libpspp/getl.h"
+#include "libpspp/llx.h"
+#include "libpspp/message.h"
+#include "ui/syntax-gen.h"
+
+#include "gl/error.h"
+#include "gl/xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 #define N_(msgid) msgid
 
-static const struct argp_option post_init_options [] = {
-  {"algorithm", 'a', "{compatible|enhanced}", 0, N_("set to `compatible' if you want output calculated from broken algorithms"), 0},
-  {"include", 'I', "DIR", 0, N_("Append DIR to include path"), 0},
-  {"no-include", 'I', 0, 0, N_("Clear include path"), 0},
-  {"no-statrc", 'r', 0, 0, N_("Disable execution of .pspp/rc at startup"), 0},
-  {"config-dir", 'B', "DIR", 0, N_("Set configuration directory to DIR"), 0},
-  {"safer", 's', 0, 0,  N_("Don't allow some unsafe operations"), 0},
-  {"syntax", 'x', "{compatible|enhanced}", 0, N_("Set to `compatible' if you want only to accept SPSS compatible syntax"), 0},
-  { 0, 0, 0, 0, 0, 0 }
-};
-
-static error_t
-parse_post_init_opts (int key, char *arg, struct argp_state *state)
-{
-  struct source_init
+enum
   {
-    bool process_statrc;
+    OPT_ALGORITHM,
+    OPT_INCLUDE,
+    OPT_NO_INCLUDE,
+    OPT_SAFER,
+    OPT_SYNTAX,
+    N_SOURCE_INIT_OPTIONS
   };
 
-  struct source_init *sip = state->hook;
-
-  struct source_stream *ss = state->input;
+static const struct argv_option source_init_options[N_SOURCE_INIT_OPTIONS] =
+  {
+    {"algorithm", 'a', required_argument, OPT_ALGORITHM},
+    {"include", 'I', required_argument, OPT_INCLUDE},
+    {"no-include", 0, no_argument, OPT_NO_INCLUDE},
+    {"safer", 's', no_argument, OPT_SAFER},
+    {"syntax", 'x', required_argument, OPT_SYNTAX},
+  };
 
-  if ( state->input == NULL)
-    return 0;
+static void
+source_init_option_callback (int id, void *ss_)
+{
+  struct source_stream *ss = ss_;
 
-  switch (key)
+  switch (id)
     {
-    case ARGP_KEY_INIT:
-      state->hook = sip = xzalloc (sizeof (struct source_init));
-      sip->process_statrc = true;
-      break;
-    case ARGP_KEY_FINI:
-      free (sip);
-      break;
-    case  'a':
-      if ( 0 == strcmp (arg, "compatible") )
+    case OPT_ALGORITHM:
+      if (!strcmp (optarg, "compatible"))
        settings_set_algorithm (COMPATIBLE);
-      else if ( 0 == strcmp (arg, "enhanced"))
+      else if (!strcmp (optarg, "enhanced"))
        settings_set_algorithm (ENHANCED);
       else
-       {
-         argp_failure (state, 1, 0, _("Algorithm must be either \"compatible\" or \"enhanced\"."));
-       }
-      break;
-    case 'B':
-      config_path = arg;
+        error (1, 0,
+               _("Algorithm must be either \"compatible\" or \"enhanced\"."));
       break;
-    case 'I':
-      if (arg == NULL || !strcmp (arg, "-"))
+
+    case OPT_INCLUDE:
+      if (!strcmp (optarg, "-"))
        getl_clear_include_path (ss);
       else
-       getl_add_include_dir (ss, arg);
+       getl_add_include_dir (ss, optarg);
       break;
-    case 'r':
-      sip->process_statrc = false;
-      break;
-    case ARGP_KEY_SUCCESS:
-      if (sip->process_statrc)
-       {
-         char *pspprc_fn = fn_search_path ("rc", config_path);
-         if (pspprc_fn != NULL)
-           {
-             getl_append_source (ss,
-                                 create_syntax_file_source (pspprc_fn),
-                                 GETL_BATCH,
-                                 ERRMODE_CONTINUE
-                                 );
-
-             free (pspprc_fn);
-           }
-       }
+
+    case OPT_NO_INCLUDE:
+      getl_clear_include_path (ss);
       break;
-    case 's':
+
+    case OPT_SAFER:
       settings_set_safer_mode ();
       break;
-    case 'x':
-      if ( 0 == strcmp (arg, "compatible") )
+
+    case OPT_SYNTAX:
+      if (!strcmp (optarg, "compatible") )
        settings_set_syntax (COMPATIBLE);
-      else if ( 0 == strcmp (arg, "enhanced"))
+      else if (!strcmp (optarg, "enhanced"))
        settings_set_syntax (ENHANCED);
       else
-       {
-         argp_failure (state, 1, 0, _("Syntax must be either \"compatible\" or \"enhanced\"."));
-       }
+        error (1, 0,
+               _("Syntax must be either \"compatible\" or \"enhanced\"."));
       break;
+
     default:
-      return ARGP_ERR_UNKNOWN;
+      NOT_REACHED ();
     }
-
-  return 0;
 }
 
-const struct argp post_init_argp =
-  {post_init_options, parse_post_init_opts, 0, 0, 0, 0, 0};
-
+void
+source_init_register_argv_parser (struct argv_parser *ap,
+                                  struct source_stream *ss)
+{
+  argv_parser_add_options (ap, source_init_options, N_SOURCE_INIT_OPTIONS,
+                           source_init_option_callback, ss);
+}