Use the msg function to report errors wherever possible.
[pspp] / src / output / driver.c
index 203644584f162eade6f30663c26ffaa81625d58d..0a6afba98a5d7eeec20b1060217a1ea65e643296 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2007, 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012 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
@@ -38,7 +38,6 @@
 #include "output/output-item.h"
 #include "output/text-item.h"
 
-#include "gl/error.h"
 #include "gl/xalloc.h"
 #include "gl/xmemdup0.h"
 
@@ -50,8 +49,8 @@ static const struct output_driver_factory *factories[];
 /* Drivers currently registered with output_driver_register(). */
 static struct llx_list drivers = LLX_INITIALIZER (drivers);
 
-static struct output_item *deferred_syntax;
-static bool in_command;
+/* TEXT_ITEM_SYNTAX being accumulated until another kind of output arrives. */
+static struct string deferred_syntax = DS_EMPTY_INITIALIZER;
 
 void
 output_close (void)
@@ -108,47 +107,34 @@ output_submit__ (struct output_item *item)
 static void
 flush_deferred_syntax (void)
 {
-  if (deferred_syntax != NULL)
+  if (!ds_is_empty (&deferred_syntax))
     {
-      output_submit__ (deferred_syntax);
-      deferred_syntax = NULL;
+      char *syntax = ds_steal_cstr (&deferred_syntax);
+      output_submit__ (text_item_super (
+                         text_item_create_nocopy (TEXT_ITEM_SYNTAX, syntax)));
     }
 }
 
+static bool
+is_syntax_item (const struct output_item *item)
+{
+  return (is_text_item (item)
+          && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
+}
+
 /* Submits ITEM to the configured output drivers, and transfers ownership to
    the output subsystem. */
 void
 output_submit (struct output_item *item)
 {
-  if (is_text_item (item))
+  if (is_syntax_item (item))
     {
-      struct text_item *text = to_text_item (item);
-      switch (text_item_get_type (text))
-        {
-        case TEXT_ITEM_SYNTAX:
-          if (!in_command)
-            {
-              flush_deferred_syntax ();
-              deferred_syntax = item;
-              return;
-            }
-          break;
-
-        case TEXT_ITEM_COMMAND_OPEN:
-          output_submit__ (item);
-          flush_deferred_syntax ();
-          in_command = true;
-          return;
-
-        case TEXT_ITEM_COMMAND_CLOSE:
-          in_command = false;
-          break;
-
-        default:
-          break;
-        }
+      ds_put_cstr (&deferred_syntax, text_item_get_text (to_text_item (item)));
+      output_item_unref (item);
+      return;
     }
 
+  flush_deferred_syntax ();
   output_submit__ (item);
 }
 
@@ -159,6 +145,7 @@ output_flush (void)
 {
   struct llx *llx;
 
+  flush_deferred_syntax ();
   for (llx = llx_head (&drivers); llx != llx_null (&drivers);
        llx = llx_next (llx))
     {
@@ -246,8 +233,10 @@ output_driver_track_current_command (const struct output_item *output_item,
 extern const struct output_driver_factory txt_driver_factory;
 extern const struct output_driver_factory list_driver_factory;
 extern const struct output_driver_factory html_driver_factory;
-extern const struct output_driver_factory odt_driver_factory;
 extern const struct output_driver_factory csv_driver_factory;
+#ifdef ODF_WRITE_SUPPORT
+extern const struct output_driver_factory odt_driver_factory;
+#endif
 #ifdef HAVE_CAIRO
 extern const struct output_driver_factory pdf_driver_factory;
 extern const struct output_driver_factory ps_driver_factory;
@@ -259,8 +248,10 @@ static const struct output_driver_factory *factories[] =
     &txt_driver_factory,
     &list_driver_factory,
     &html_driver_factory,
-    &odt_driver_factory,
     &csv_driver_factory,
+#ifdef ODF_WRITE_SUPPORT
+    &odt_driver_factory,
+#endif
 #ifdef HAVE_CAIRO
     &pdf_driver_factory,
     &ps_driver_factory,
@@ -302,16 +293,23 @@ output_driver_create (struct string_map *options)
   char *file_name;
   char *format;
 
+  format = string_map_find_and_delete (options, "format");
   file_name = string_map_find_and_delete (options, "output-file");
-  if (file_name == NULL)
-    file_name = xstrdup ("-");
 
-  format = string_map_find_and_delete (options, "format");
   if (format == NULL)
     {
-      const char *extension = strrchr (file_name, '.');
-      format = xstrdup (extension != NULL ? extension + 1 : "");
+      if (file_name != NULL)
+        {
+          const char *extension = strrchr (file_name, '.');
+          format = xstrdup (extension != NULL ? extension + 1 : "");
+        }
+      else
+        format = xstrdup ("txt");
     }
+  f = find_factory (format);
+
+  if (file_name == NULL)
+    file_name = xstrdup (f->default_file_name);
 
   /* XXX should use parse_enum(). */
   device_string = string_map_find_and_delete (options, "device");
@@ -323,12 +321,11 @@ output_driver_create (struct string_map *options)
     device_type = SETTINGS_DEVICE_LISTING;
   else
     {
-      error (0, 0, _("%s is not a valid device type (the choices are "
-                     "\"terminal\" and \"listing\")"), device_string);
+      msg (MW, _("%s is not a valid device type (the choices are `%s' and `%s')"),
+                     device_string, "terminal", "listing");
       device_type = default_device_type (file_name);
     }
 
-  f = find_factory (format);
   driver = f->create (file_name, device_type, options);
   if (driver != NULL)
     {
@@ -336,7 +333,7 @@ output_driver_create (struct string_map *options)
       const char *key;
 
       STRING_MAP_FOR_EACH_KEY (key, node, options)
-        error (0, 0, _("%s: unknown option \"%s\""), file_name, key);
+        msg (MW, _("%s: unknown option `%s'"), file_name, key);
     }
   string_map_clear (options);