Actually implement the new procedure code and adapt all of its clients
[pspp-builds.git] / src / language / command.c
index cfae2ba03e5ec4b2b5c1f56e90f2c5993ed9d7c0..49ec22fdb8904546cd10d081d89ee31fed15a19a 100644 (file)
@@ -1,6 +1,5 @@
 /* PSPP - computes sample statistics.
    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
-   Written by Ben Pfaff <blp@gnu.org>.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
 #include <errno.h>
 #include <unistd.h>
 
+#include <data/casereader.h>
 #include <data/dictionary.h>
 #include <data/procedure.h>
 #include <data/settings.h>
 #include <data/variable.h>
 #include <language/lexer/lexer.h>
-#include <language/line-buffer.h>
+#include <language/prompt.h>
 #include <libpspp/alloc.h>
 #include <libpspp/assertion.h>
 #include <libpspp/compiler.h>
@@ -137,22 +137,37 @@ static enum cmd_result do_parse_command (struct lexer *, struct dataset *, enum
    dot.  On failure, skips to the terminating dot.
    Returns the command's success or failure result. */
 enum cmd_result
-cmd_parse (struct lexer *lexer, struct dataset *ds, enum cmd_state state) 
+cmd_parse_in_state (struct lexer *lexer, struct dataset *ds,
+                   enum cmd_state state)
 {
   int result;
-  
+
   som_new_series ();
 
   result = do_parse_command (lexer, ds, state);
-  if (cmd_result_is_failure (result)) 
+  if (cmd_result_is_failure (result))
     lex_discard_rest_of_command (lexer);
 
+  assert (!proc_is_open (ds));
   unset_cmd_algorithm ();
   dict_clear_aux (dataset_dict (ds));
+  if (!dataset_end_of_command (ds))
+    result = CMD_CASCADING_FAILURE;
 
   return result;
 }
 
+enum cmd_result
+cmd_parse (struct lexer *lexer, struct dataset *ds)
+{
+  const struct dictionary *dict = dataset_dict (ds);
+  return cmd_parse_in_state (lexer, ds,
+                            proc_has_active_file (ds) &&
+                            dict_get_var_cnt (dict) > 0 ?
+                            CMD_STATE_DATA : CMD_STATE_INITIAL);
+}
+
+
 /* Parses an entire command, from command name to terminating
    dot. */
 static enum cmd_result
@@ -162,7 +177,7 @@ do_parse_command (struct lexer *lexer, struct dataset *ds, enum cmd_state state)
   enum cmd_result result;
 
   /* Read the command's first token. */
-  getl_set_prompt_style (GETL_PROMPT_FIRST);
+  prompt_set_style (PROMPT_FIRST);
   set_completion_state (state);
   lex_get (lexer);
   if (lex_token (lexer) == T_STOP)
@@ -172,7 +187,7 @@ do_parse_command (struct lexer *lexer, struct dataset *ds, enum cmd_state state)
       /* Null commands can result from extra empty lines. */
       return CMD_SUCCESS; 
     }
-  getl_set_prompt_style (GETL_PROMPT_LATER);
+  prompt_set_style (PROMPT_LATER);
 
   /* Parse the command name. */
   command = parse_command_name (lexer);
@@ -192,7 +207,7 @@ do_parse_command (struct lexer *lexer, struct dataset *ds, enum cmd_state state)
     {
       msg (SE, _("%s may be used only in enhanced syntax mode."),
            command->name);
-       return CMD_FAILURE;
+      return CMD_FAILURE;
     }
   else if (!in_correct_state (command, state)) 
     {
@@ -676,7 +691,8 @@ cmd_n_of_cases (struct lexer *lexer, struct dataset *ds)
 int
 cmd_execute (struct lexer *lexer, struct dataset *ds)
 {
-  if (!procedure (ds, NULL, NULL))
+  bool ok = casereader_destroy (proc_open (ds));
+  if (!proc_commit (ds) || !ok)
     return CMD_CASCADING_FAILURE;
   return lex_end_of_command (lexer);
 }
@@ -707,9 +723,9 @@ cmd_erase (struct lexer *lexer, struct dataset *ds UNUSED)
   return CMD_SUCCESS;
 }
 
-#ifdef unix
-/* Spawn a shell process. */
-static int
+#if HAVE_FORK && HAVE_EXECL
+/* Spawn an interactive shell process. */
+static bool
 shell (void)
 {
   int pid;
@@ -750,69 +766,48 @@ shell (void)
 
     case -1:
       msg (SE, _("Couldn't fork: %s."), strerror (errno));
-      return 0;
+      return false;
 
     default:
       assert (pid > 0);
       while (wait (NULL) != pid)
        ;
-      return 1;
+      return true;
     }
 }
-#endif /* unix */
-
-/* Parses the HOST command argument and executes the specified
-   command.  Returns a suitable command return code. */
-static int
-run_command (struct lexer *lexer)
+#else /* !(HAVE_FORK && HAVE_EXECL) */
+/* Don't know how to spawn an interactive shell. */
+static bool
+shell (void)
 {
-  const char *cmd;
-  int string;
-
-  /* Handle either a string argument or a full-line argument. */
-  {
-    int c = lex_look_ahead (lexer);
+  msg (SE, _("Interactive shell not supported on this platform."));
+  return false;
+}
+#endif
 
-    if (c == '\'' || c == '"')
-      {
-       lex_get (lexer);
-       if (!lex_force_string (lexer))
-         return CMD_FAILURE;
-       cmd = ds_cstr (lex_tokstr (lexer));
-       string = 1;
-      }
-    else
-      {
-       cmd = lex_rest_of_line (lexer, NULL);
-        lex_discard_line (lexer);
-       string = 0;
-      }
-  }
+/* Executes the specified COMMAND in a subshell.  Returns true if
+   successful, false otherwise. */
+static bool
+run_command (const char *command)
+{
+  if (system (NULL) == 0) 
+    {
+      msg (SE, _("Command shell not supported on this platform."));
+      return false;
+    }
 
   /* Execute the command. */
-  if (system (cmd) == -1)
+  if (system (command) == -1)
     msg (SE, _("Error executing command: %s."), strerror (errno));
 
-  /* Finish parsing. */
-  if (string)
-    {
-      lex_get (lexer);
-
-      if (lex_token (lexer) != '.')
-       {
-         lex_error (lexer, _("expecting end of command"));
-         return CMD_FAILURE;
-       }
-    }
-
-  return CMD_SUCCESS;
+  return true;
 }
 
 /* Parses, performs the HOST command. */
 int
 cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
 {
-  int code;
+  int look_ahead;
 
   if (get_safer_mode ()) 
     { 
@@ -820,36 +815,37 @@ cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
       return CMD_FAILURE; 
     } 
 
-#ifdef unix
-  /* Figure out whether to invoke an interactive shell or to execute a
-     single shell command. */
-  if (lex_look_ahead (lexer) == '.')
+  look_ahead = lex_look_ahead (lexer);
+  if (look_ahead == '.') 
     {
       lex_get (lexer);
-      code = shell () ? CMD_FAILURE : CMD_SUCCESS;
+      return shell () ? CMD_SUCCESS : CMD_FAILURE;
     }
-  else
-    code = run_command (lexer);
-#else /* !unix */
-  /* Make sure that the system has a command interpreter, then run a
-     command. */
-  if (system (NULL) != 0)
-    code = run_command (lexer);
-  else
+  else if (look_ahead == '\'' || look_ahead == '"')
     {
-      msg (SE, _("No operating system support for this command."));
-      code = CMD_FAILURE;
-    }
-#endif /* !unix */
+      bool ok;
+      
+      lex_get (lexer);
+      if (!lex_force_string (lexer))
+        NOT_REACHED ();
+      ok = run_command (ds_cstr (lex_tokstr (lexer)));
 
-  return code;
+      lex_get (lexer);
+      return ok ? lex_end_of_command (lexer) : CMD_FAILURE;
+    }
+  else 
+    {
+      bool ok = run_command (lex_rest_of_line (lexer));
+      lex_discard_line (lexer);
+      return ok ? CMD_SUCCESS : CMD_FAILURE;
+    }
 }
 
 /* Parses, performs the NEW FILE command. */
 int
 cmd_new_file (struct lexer *lexer, struct dataset *ds)
 {
-  discard_variables (ds);
+  proc_discard_active_file (ds);
 
   return lex_end_of_command (lexer);
 }