Reform string library.
[pspp-builds.git] / src / language / command.c
index 5712ddbe4b46a489c29b8041fd2ebcac4174590d..ed043d2197363fc0ef81cebae2f119f480573ad1 100644 (file)
@@ -28,6 +28,7 @@
 #include <unistd.h>
 
 #include <data/dictionary.h>
+#include <data/procedure.h>
 #include <data/settings.h>
 #include <data/variable.h>
 #include <language/lexer/lexer.h>
@@ -38,7 +39,6 @@
 #include <libpspp/str.h>
 #include <output/manager.h>
 #include <output/table.h>
-#include <procedure.h>
 
 #if HAVE_SYS_WAIT_H
 #include <sys/wait.h>
 #define _(msgid) gettext (msgid)
 #define N_(msgid) msgid
 \f
+/* Returns true if RESULT is a valid "enum cmd_result",
+   false otherwise. */
+static inline bool
+cmd_result_is_valid (enum cmd_result result) 
+{
+  return (result == CMD_SUCCESS || result == CMD_EOF || result == CMD_FINISH
+          || (result >= CMD_PRIVATE_FIRST && result <= CMD_PRIVATE_LAST)
+          || result == CMD_FAILURE || result == CMD_NOT_IMPLEMENTED
+          || result == CMD_CASCADING_FAILURE);
+}
+
 /* Returns true if RESULT indicates success,
    false otherwise. */
 bool
 cmd_result_is_success (enum cmd_result result) 
 {
-  return (result == CMD_SUCCESS || result == CMD_EOF
-          || result == CMD_QUIT || result == CMD_END_SUBLOOP);
+  assert (cmd_result_is_valid (result));
+  return result > 0;
 }
 
 /* Returns true if RESULT indicates failure,
@@ -66,7 +77,8 @@ cmd_result_is_success (enum cmd_result result)
 bool
 cmd_result_is_failure (enum cmd_result result) 
 {
-  return !cmd_result_is_success (result);
+  assert (cmd_result_is_valid (result));
+  return result < 0;
 }
 \f
 /* Command processing states. */
@@ -175,6 +187,7 @@ do_parse_command (enum cmd_state state)
   tab_set_command_name (NULL);
   msg_set_command_name (NULL);
     
+  assert (cmd_result_is_valid (result));
   return result;
 }
 
@@ -417,15 +430,15 @@ unknown_command_error (char *const words[], size_t word_cnt)
       struct string s;
       size_t i;
 
-      ds_init (&s, 0);
+      ds_init_empty (&s);
       for (i = 0; i < word_cnt; i++) 
         {
           if (i != 0)
-            ds_putc (&s, ' ');
-          ds_puts (&s, words[i]);
+            ds_put_char (&s, ' ');
+          ds_put_cstr (&s, words[i]);
         }
 
-      msg (SE, _("Unknown command %s."), ds_c_str (&s));
+      msg (SE, _("Unknown command %s."), ds_cstr (&s));
 
       ds_destroy (&s);
     }
@@ -454,7 +467,7 @@ parse_command_name (void)
       assert (word_cnt < sizeof words / sizeof *words);
       if (token == T_ID) 
         {
-          words[word_cnt] = xstrdup (ds_c_str (&tokstr));
+          words[word_cnt] = ds_xstrdup (&tokstr);
           str_uppercase (words[word_cnt]); 
         }
       else if (token == '-')
@@ -628,7 +641,7 @@ command_generator (const char *text, int state)
 int
 cmd_finish (void)
 {
-  return CMD_QUIT;
+  return CMD_FINISH;
 }
 
 /* Parses the N command. */
@@ -673,10 +686,10 @@ cmd_erase (void)
   if (!lex_force_string ())
     return CMD_FAILURE;
 
-  if (remove (ds_c_str (&tokstr)) == -1)
+  if (remove (ds_cstr (&tokstr)) == -1)
     {
       msg (SW, _("Error removing `%s': %s."),
-          ds_c_str (&tokstr), strerror (errno));
+          ds_cstr (&tokstr), strerror (errno));
       return CMD_FAILURE;
     }
 
@@ -754,7 +767,7 @@ run_command (void)
        lex_get ();
        if (!lex_force_string ())
          return CMD_FAILURE;
-       cmd = ds_c_str (&tokstr);
+       cmd = ds_cstr (&tokstr);
        string = 1;
       }
     else