Make the expression code a little nicer and fix bugs found
[pspp-builds.git] / src / title.c
index cea04c63a966c96a46220d17b4905462eda8ad8b..cb56f84db2db7e38a57b2b7d543b83ac93b3bca7 100644 (file)
@@ -30,8 +30,6 @@
 #include "version.h"
 #include "vfm.h"
 
-#undef DEBUGGING
-/*#define DEBUGGING 1 */
 #include "debug-print.h"
 
 static int get_title (const char *cmd, char **title);
@@ -77,6 +75,7 @@ get_title (const char *cmd, char **title)
       if (*title)
        free (*title);
       *title = xstrdup (lex_rest_of_line (NULL));
+      lex_discard_line ();
       for (cp = *title; *cp; cp++)
        *cp = toupper ((unsigned char) (*cp));
       token = '.';
@@ -89,16 +88,14 @@ get_title (const char *cmd, char **title)
 int
 cmd_file_label (void)
 {
-  char *label;
+  const char *label;
 
   label = lex_rest_of_line (NULL);
+  lex_discard_line ();
   while (isspace ((unsigned char) *label))
     label++;
 
-  free (default_dict.label);
-  default_dict.label = xstrdup (label);
-  if (strlen (default_dict.label) > 60)
-    default_dict.label[60] = 0;
+  dict_set_label (default_dict, label);
   token = '.';
 
   return CMD_SUCCESS;
@@ -109,14 +106,22 @@ cmd_file_label (void)
 static void
 add_document_line (const char *line, int indent)
 {
-  char *doc;
-
-  default_dict.n_documents++;
-  default_dict.documents = xrealloc (default_dict.documents,
-                                    80 * default_dict.n_documents);
-  doc = &default_dict.documents[80 * (default_dict.n_documents - 1)];
-  memset (doc, ' ', indent);
-  st_bare_pad_copy (&doc[indent], line, 80 - indent);
+  const char *old_documents;
+  size_t old_len;
+  char *new_documents;
+
+  old_documents = dict_get_documents (default_dict);
+  old_len = old_documents != NULL ? strlen (old_documents) : 0;
+  new_documents = xmalloc (old_len + 81);
+
+  memcpy (new_documents, old_documents, old_len);
+  memset (new_documents + old_len, ' ', indent);
+  st_bare_pad_copy (new_documents + old_len + indent, line, 80 - indent);
+  new_documents[old_len + 80] = '\0';
+
+  dict_set_documents (default_dict, new_documents);
+
+  free (new_documents);
 }
 
 /* Performs the DOCUMENT command. */
@@ -128,7 +133,7 @@ cmd_document (void)
     char buf[256];
     struct tm *tmp = localtime (&last_vfm_invocation);
 
-    if (default_dict.n_documents)
+    if (dict_get_documents (default_dict) != NULL)
       add_document_line ("", 0);
 
     sprintf (buf, _("Document entered %s %02d:%02d:%02d by %s (%s):"),
@@ -140,20 +145,21 @@ cmd_document (void)
   for (;;)
     {
       int had_dot;
-      char *line;
+      const char *orig_line;
+      char *copy_line;
 
-      line = lex_rest_of_line (&had_dot);
-      while (isspace ((unsigned char) *line))
-       line++;
+      orig_line = lex_rest_of_line (&had_dot);
+      lex_discard_line ();
+      while (isspace ((unsigned char) *orig_line))
+       orig_line++;
 
+      copy_line = xmalloc (strlen (orig_line) + 2);
+      strcpy (copy_line, orig_line);
       if (had_dot)
-       {
-         char *cp = strchr (line, 0);
-         *cp++ = '.';
-         *cp = 0;
-       }
+        strcat (copy_line, ".");
 
-      add_document_line (line, 3);
+      add_document_line (copy_line, 3);
+      free (copy_line);
 
       lex_get_line ();
       if (had_dot)
@@ -168,12 +174,7 @@ cmd_document (void)
 int
 cmd_drop_documents (void)
 {
-  lex_match_id ("DROP");
-  lex_match_id ("DOCUMENTS");
-
-  free (default_dict.documents);
-  default_dict.documents = NULL;
-  default_dict.n_documents = 0;
+  dict_set_documents (default_dict, NULL);
 
   return lex_end_of_command ();
 }