Merge commit 'origin/stable'
[pspp-builds.git] / src / ui / terminal / msg-ui.c
index 7cb652c021df77b3b42fe3ab70de9eeb4d7e582a..682d753d8fbd36200f68e33677c239e5ef5b6c17 100644 (file)
@@ -1,37 +1,38 @@
-/* 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 "unilbrk.h"
+#include "localcharset.h"
 
 #include <libpspp/msg-locator.h>
 #include <libpspp/getl.h>
 #include <data/settings.h>
 #include <libpspp/message.h>
 #include <libpspp/str.h>
+#include <output/journal.h>
+#include <output/output.h>
+#include <output/table.h>
 #include <errno.h>
 #include <stdio.h>
-
+#include <stdlib.h>
+#include <unistd.h>
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -46,103 +47,105 @@ static void handle_msg (const struct msg *);
 
 static FILE *msg_file ;
 
-void 
+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_file = stdout;
 
-  if ( error_file ) 
+  if ( error_file )
     {
       msg_file = fopen (error_file, "a");
-      if ( NULL == msg_file ) 
+      if ( NULL == msg_file )
        {
          int err = errno;
          printf ( _("Cannot open %s (%s). "
-                    "Writing errors to stdout instead.\n"), 
+                    "Writing errors to stdout instead.\n"),
                   error_file, strerror(err) );
          msg_file = stdout;
        }
     }
-  msg_init (handle_msg);
+  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)
+      if (settings_get_errorbreak () && error_count)
         msg (MN, _("Terminating execution of syntax file due to error."));
-      else if (error_count > get_mxerrs() )
+      else if (error_count > settings_get_mxerrs () )
         msg (MN, _("Errors (%d) exceeds limit (%d)."),
-             error_count, get_mxerrs());
-      else if (error_count + warning_count > get_mxwarns() )
+             error_count, settings_get_mxerrs ());
+      else if (error_count + warning_count > settings_get_mxwarns () )
         msg (MN, _("Warnings (%d) exceed limit (%d)."),
-             error_count + warning_count, get_mxwarns() );
+             error_count + warning_count, settings_get_mxwarns () );
       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;
 }
 \f
-static void dump_message (char *msg, unsigned width, unsigned indent, FILE *);
-static void dump_line (int line_indent, const char *line, size_t length,
-                       FILE *);
+typedef void write_line_func (int indent, struct substring line, void *aux);
+static void dump_message (char *msg, unsigned width, unsigned indent,
+                          write_line_func *, void *aux);
+static write_line_func write_stream;
+static write_line_func write_journal;
 
 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. */
@@ -163,26 +166,42 @@ handle_msg (const struct msg *m)
 
   if (severity->name != NULL)
     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_put_format (&string, "%s: ", msg_get_command_name ());
 
   ds_put_cstr (&string, m->text);
 
-  if (msg_file != stdout || get_error_routing_to_terminal ())
-    dump_message (ds_cstr (&string), get_viewwidth (), 8, msg_file);
+  if (msg_file != stdout || settings_get_error_routing_to_terminal ())
+    dump_message (ds_cstr (&string),
+                  isatty (fileno (msg_file)) ? settings_get_viewwidth () : INT_MAX, 8,
+                  write_stream, msg_file);
+
+  dump_message (ds_cstr (&string), 78, 0, write_journal, NULL);
+
+  if (settings_get_error_routing_to_listing ())
+    {
+      /* Disable screen output devices, because the error should
+         already have been reported to the screen with the
+         dump_message call above. */
+      outp_enable_device (false, OUTP_DEV_SCREEN);
+      tab_output_text (TAB_LEFT, ds_cstr (&string));
+      outp_enable_device (true, OUTP_DEV_SCREEN);
+    }
 
   ds_destroy (&string);
 }
 
 /* Divides MSG into lines of WIDTH width for the first line and
    WIDTH - INDENT width for each succeeding line, and writes the
-   lines to STREAM. */
+   lines by calling DUMP_LINE for each line, passing AUX as
+   auxiliary data. */
 static void
-dump_message (char *msg, unsigned width, unsigned indent, FILE *stream)
+dump_message (char *msg, unsigned width, unsigned indent,
+              write_line_func *dump_line, void *aux)
 {
   size_t length = strlen (msg);
   char *string, *breaks;
@@ -198,15 +217,14 @@ dump_message (char *msg, unsigned width, unsigned indent, FILE *stream)
     {
       free (string);
       free (breaks);
-      fputs (msg, stream);
-      putc ('\n', stream);
+      dump_line (0, ss_cstr (msg), aux);
       return;
     }
 
   /* Break into lines. */
   if (indent > width / 3)
     indent = width / 3;
-  mbs_width_linebreaks (string, length,
+  ulc_width_linebreaks (string, length,
                         width - indent, -indent, 0,
                         NULL, locale_charset (), breaks);
 
@@ -214,42 +232,44 @@ 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]) 
+    if (breaks[i] == UC_BREAK_POSSIBLE || breaks[i] == UC_BREAK_MANDATORY)
       {
-      case UC_BREAK_POSSIBLE:
-        /* Break before this character,
-           and include this character in the next line. */
-        dump_line (line_indent, &string[line_start], i - line_start, stream);
-        line_start = i;
-        line_indent = indent;
-        break;
-      case UC_BREAK_MANDATORY:
-        /* Break before this character,
-           but don't include this character in the next line
-           (because it'string a new-line). */
-        dump_line (line_indent, &string[line_start], i - line_start, stream);
-        line_start = i + 1;
+        dump_line (line_indent,
+                   ss_buffer (string + line_start, i - line_start), aux);
         line_indent = indent;
-        break;
-      default:
-        break;
+
+        /* UC_BREAK_POSSIBLE means that a line break can be
+           inserted, and that the character should be included
+           in the next line.
+           UC_BREAK_MANDATORY means that this character is a line
+           break, so it should not be included in the next line. */
+        line_start = i + (breaks[i] == UC_BREAK_MANDATORY);
       }
   if (line_start < length)
-    dump_line (line_indent, &string[line_start], length - line_start, stream);
+    dump_line (line_indent,
+               ss_buffer (string + line_start, length - line_start), aux);
 
   free (string);
   free (breaks);
 }
 
-/* Write LINE_INDENT spaces, the LENGTH characters in LINE, then
-   a new-line to STREAM. */
+/* Write LINE_INDENT spaces, LINE, then a new-line to STREAM. */
 static void
-dump_line (int line_indent, const char *line, size_t length, FILE *stream)
+write_stream (int line_indent, struct substring line, void *stream_)
 {
+  FILE *stream = stream_;
   int i;
   for (i = 0; i < line_indent; i++)
     putc (' ', stream);
-  fwrite (line, 1, length, stream);
+  fwrite (ss_data (line), 1, ss_length (line), stream);
   putc ('\n', stream);
 }
 
+/* Writes LINE to the journal. */
+static void
+write_journal (int line_indent UNUSED, struct substring line, void *unused UNUSED)
+{
+  char *s = xstrndup (ss_data (line), ss_length (line));
+  journal_write (true, s);
+  free (s);
+}