Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / ui / terminal / msg-ui.c
index 3422da0292743065ab83bbd2b0def8b41e7a727c..6c40a8c78c60cd5a4e13c76d489d9abc8fe1cb13 100644 (file)
@@ -1,32 +1,33 @@
-/* PSPP - computes sample statistics.
+/* PSPP - a program for statistical analysis.
    Copyright (C) 1997-9, 2000, 2006 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
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   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
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 
 #include "msg-ui.h"
 
-#include "exit.h"
 #include "linebreak.h"
 
-#include <language/line-buffer.h>
+#include <libpspp/msg-locator.h>
+#include <libpspp/getl.h>
 #include <data/settings.h>
 #include <libpspp/message.h>
+#include <libpspp/str.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 /* Number of errors, warnings reported. */
 static int error_count;
 static int warning_count;
+static const char *error_file;
 
 static void handle_msg (const struct msg *);
 
+static FILE *msg_file ;
+
+void
+msg_ui_set_error_file (const char *filename)
+{
+  error_file = filename;
+}
+
 void
-msg_ui_init (void) 
+msg_ui_init (struct source_stream *ss)
 {
-  msg_init (handle_msg);
+  msg_file = stdout;
+
+  if ( error_file )
+    {
+      msg_file = fopen (error_file, "a");
+      if ( NULL == msg_file )
+       {
+         int err = errno;
+         printf ( _("Cannot open %s (%s). "
+                    "Writing errors to stdout instead.\n"),
+                  error_file, strerror(err) );
+         msg_file = stdout;
+       }
+    }
+  msg_init (ss, handle_msg);
 }
 
 void
-msg_ui_done (void) 
+msg_ui_done (void)
 {
   msg_done ();
+  msg_locator_done ();
+
+  if ( msg_file ) /* FIXME: do we really want to close stdout ?? */
+    fclose (msg_file);
 }
 
 /* Checks whether we've had so many errors that it's time to quit
    processing this syntax file. */
 void
-check_msg_count (void)
+check_msg_count (struct source_stream *ss)
 {
-  if (!getl_is_interactive ()) 
+  if (!getl_is_interactive (ss))
     {
       if (get_errorbreak () && error_count)
         msg (MN, _("Terminating execution of syntax file due to error."));
@@ -68,18 +96,18 @@ check_msg_count (void)
       else
         return;
 
-      getl_abort_noninteractive (); 
+      getl_abort_noninteractive (ss);
     }
 }
 
 void
-reset_msg_count (void) 
+reset_msg_count (void)
 {
   error_count = warning_count = 0;
 }
 
 bool
-any_errors (void) 
+any_errors (void)
 {
   return error_count > 0;
 }
@@ -91,26 +119,26 @@ static void dump_line (int line_indent, const char *line, size_t length,
 static void
 handle_msg (const struct msg *m)
 {
-  struct category 
+  struct category
     {
       bool show_command_name;   /* Show command name with error? */
       bool show_file_location;  /* Show syntax file location? */
     };
 
-  static const struct category categories[] = 
+  static const struct category categories[] =
     {
       {false, false},           /* MSG_GENERAL. */
       {true, true},             /* MSG_SYNTAX. */
       {false, true},            /* MSG_DATA. */
     };
 
-  struct severity 
+  struct severity
     {
       const char *name;         /* How to identify this severity. */
       int *count;               /* Number of msgs with this severity so far. */
     };
-  
-  static struct severity severities[] = 
+
+  static struct severity severities[] =
     {
       {N_("error"), &error_count},          /* MSG_ERROR. */
       {N_("warning"), &warning_count},      /* MSG_WARNING. */
@@ -119,30 +147,29 @@ handle_msg (const struct msg *m)
 
   const struct category *category = &categories[m->category];
   const struct severity *severity = &severities[m->severity];
-  struct string string = DS_INITIALIZER;
+  struct string string = DS_EMPTY_INITIALIZER;
 
   if (category->show_file_location && m->where.file_name)
     {
-      ds_printf (&string, "%s:", m->where.file_name);
+      ds_put_format (&string, "%s:", m->where.file_name);
       if (m->where.line_number != -1)
-       ds_printf (&string, "%d:", m->where.line_number);
-      ds_putc (&string, ' ');
+       ds_put_format (&string, "%d:", m->where.line_number);
+      ds_put_char (&string, ' ');
     }
 
   if (severity->name != NULL)
-    ds_printf (&string, "%s: ", gettext (severity->name));
-  
+    ds_put_format (&string, "%s: ", gettext (severity->name));
+
   if (severity->count != NULL)
     ++*severity->count;
-  
+
   if (category->show_command_name && msg_get_command_name () != NULL)
-    ds_printf (&string, "%s: ", msg_get_command_name ());
+    ds_put_format (&string, "%s: ", msg_get_command_name ());
 
-  ds_puts (&string, m->text);
+  ds_put_cstr (&string, m->text);
 
-  /* FIXME: Check set_messages and set_errors to determine where to
-     send errors and messages. */
-  dump_message (ds_c_str (&string), get_viewwidth (), 8, stdout);
+  if (msg_file != stdout || get_error_routing_to_terminal ())
+    dump_message (ds_cstr (&string), get_viewwidth (), 8, msg_file);
 
   ds_destroy (&string);
 }
@@ -183,7 +210,7 @@ dump_message (char *msg, unsigned width, unsigned indent, FILE *stream)
   line_start = 0;
   line_indent = 0;
   for (i = 0; i < length; i++)
-    switch (breaks[i]) 
+    switch (breaks[i])
       {
       case UC_BREAK_POSSIBLE:
         /* Break before this character,