lexer: New function lex_ofs_representation().
[pspp] / src / language / utilities / title.c
index 1bdb0e9a0db6487467d51aa958452cf91ef13637..8ad6a4acb30b5723ea27e1ad0fc698f667c0b758 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2007, 2010, 2011 Free Software Foundation, Inc.
 
    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
 #include <ctype.h>
 #include <stdlib.h>
 
-#include <data/dictionary.h>
-#include <data/procedure.h>
-#include <data/variable.h>
-#include <language/command.h>
-#include <language/lexer/lexer.h>
-#include <libpspp/message.h>
-#include <libpspp/start-date.h>
-#include <libpspp/version.h>
-#include <output/output.h>
+#include "data/dataset.h"
+#include "data/dictionary.h"
+#include "data/variable.h"
+#include "language/command.h"
+#include "language/lexer/lexer.h"
+#include "language/lexer/token.h"
+#include "libpspp/message.h"
+#include "libpspp/start-date.h"
+#include "libpspp/version.h"
+#include "output/driver.h"
 
-#include "xalloc.h"
+#include "gl/c-ctype.h"
+#include "gl/xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
-static int get_title (struct lexer *, const char *cmd, char **title);
-
-int
-cmd_title (struct lexer *lexer, struct dataset *ds UNUSED)
-{
-  return get_title (lexer, "TITLE", &outp_title);
-}
-
-int
-cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED)
-{
-  return get_title (lexer, "SUBTITLE", &outp_subtitle);
-}
-
 static int
-get_title (struct lexer *lexer, const char *cmd, char **title)
+parse_title (struct lexer *lexer, void (*set_title) (const char *))
 {
-  int c;
-
-  c = lex_look_ahead (lexer);
-  if (c == '"' || c == '\'')
+  if (lex_token (lexer) == T_STRING)
     {
+      set_title (lex_tokcstr (lexer));
       lex_get (lexer);
-      if (!lex_force_string (lexer))
-       return CMD_FAILURE;
-      if (*title)
-       free (*title);
-      *title = ds_xstrdup (lex_tokstr (lexer));
-      lex_get (lexer);
-      if (lex_token (lexer) != '.')
-       {
-         msg (SE, _("%s: `.' expected after string."), cmd);
-         return CMD_FAILURE;
-       }
     }
   else
     {
-      char *cp;
-
-      if (*title)
-       free (*title);
-      *title = xstrdup (lex_rest_of_line (lexer));
-      lex_discard_line (lexer);
-      for (cp = *title; *cp; cp++)
-       *cp = toupper ((unsigned char) (*cp));
+      int start_ofs = lex_ofs (lexer);
+      while (lex_token (lexer) != T_ENDCMD)
+        lex_get (lexer);
+
+      /* Get the raw representation of all the tokens, including any space
+         between them, and use it as the title. */
+      char *title = lex_ofs_representation (lexer, start_ofs,
+                                            lex_ofs (lexer) - 1);
+      set_title (title);
+      free (title);
     }
   return CMD_SUCCESS;
 }
 
-/* Performs the FILE LABEL command. */
 int
-cmd_file_label (struct lexer *lexer, struct dataset *ds)
+cmd_title (struct lexer *lexer, struct dataset *ds UNUSED)
 {
-  const char *label;
-
-  label = lex_rest_of_line (lexer);
-  lex_discard_line (lexer);
-  while (isspace ((unsigned char) *label))
-    label++;
-
-  dict_set_label (dataset_dict (ds), label);
+  return parse_title (lexer, output_set_title);
+}
 
-  return CMD_SUCCESS;
+int
+cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED)
+{
+  return parse_title (lexer, output_set_subtitle);
 }
 
-/* Add entry date line to DICT's documents. */
-static void
-add_document_trailer (struct dictionary *dict)
+/* Performs the FILE LABEL command. */
+int
+cmd_file_label (struct lexer *lexer, struct dataset *ds)
 {
-  char buf[64];
+  if (!lex_force_string (lexer))
+    return CMD_FAILURE;
+
+  dict_set_label (dataset_dict (ds), lex_tokcstr (lexer));
+  lex_get (lexer);
 
-  sprintf (buf, _("   (Entered %s)"), get_start_date ());
-  dict_add_document_line (dict, buf);
+  return CMD_SUCCESS;
 }
 
 /* Performs the DOCUMENT command. */
@@ -114,54 +90,35 @@ int
 cmd_document (struct lexer *lexer, struct dataset *ds)
 {
   struct dictionary *dict = dataset_dict (ds);
-  struct string line = DS_EMPTY_INITIALIZER;
-  bool end_dot;
+  char *trailer;
 
-  do
+  if (!lex_force_string (lexer))
+    return CMD_FAILURE;
+
+  while (lex_is_string (lexer))
     {
-      end_dot = lex_end_dot (lexer);
-      ds_assign_string (&line, lex_entire_line_ds (lexer));
-      if (end_dot)
-        ds_put_char (&line, '.');
-      dict_add_document_line (dict, ds_cstr (&line));
-
-      lex_discard_line (lexer);
-      lex_get_line (lexer);
+      dict_add_document_line (dict, lex_tokcstr (lexer), true);
+      lex_get (lexer);
     }
-  while (!end_dot);
 
-  add_document_trailer (dict);
-  ds_destroy (&line);
+  trailer = xasprintf (_("   (Entered %s)"), get_start_date ());
+  dict_add_document_line (dict, trailer, true);
+  free (trailer);
 
   return CMD_SUCCESS;
 }
 
-/* Performs the DROP DOCUMENTS command. */
+/* Performs the ADD DOCUMENTS command. */
 int
-cmd_drop_documents (struct lexer *lexer, struct dataset *ds)
+cmd_add_documents (struct lexer *lexer, struct dataset *ds)
 {
-  dict_clear_documents (dataset_dict (ds));
-
-  return lex_end_of_command (lexer);
+  return cmd_document (lexer, ds);
 }
 
-
-/* Performs the ADD DOCUMENTS command. */
+/* Performs the DROP DOCUMENTS command. */
 int
-cmd_add_documents (struct lexer *lexer, struct dataset *ds)
+cmd_drop_documents (struct lexer *lexer UNUSED, struct dataset *ds)
 {
-  struct dictionary *dict = dataset_dict (ds);
-
-  if ( ! lex_force_string (lexer) )
-    return CMD_FAILURE;
-
-  while ( lex_is_string (lexer))
-    {
-      dict_add_document_line (dict, ds_cstr (lex_tokstr (lexer)));
-      lex_get (lexer);
-    }
-
-  add_document_trailer (dict);
-
-  return lex_end_of_command (lexer) ;
+  dict_clear_documents (dataset_dict (ds));
+  return CMD_SUCCESS;
 }