/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000, 2006, 2009, 2010,
+ Copyright (C) 1997-9, 2000, 2006, 2009, 2010,
2011, 2013 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
#include "gl/minmax.h"
#include "gl/progname.h"
+#include "gl/relocatable.h"
#include "gl/xalloc.h"
#include "gl/xvasprintf.h"
void
vmsg (enum msg_class class, const char *format, va_list args)
{
- struct msg m;
-
- m.category = msg_class_to_category (class);
- m.severity = msg_class_to_severity (class);
- m.text = xvasprintf (format, args);
- m.file_name = NULL;
- m.first_line = m.last_line = 0;
- m.first_column = m.last_column = 0;
-
- msg_emit (&m);
+ struct msg *m = xmalloc (sizeof *m);
+ *m = (struct msg) {
+ .category = msg_class_to_category (class),
+ .severity = msg_class_to_severity (class),
+ .text = xvasprintf (format, args),
+ };
+ msg_emit (m);
}
/* Writes error message in CLASS, with text FORMAT, formatted with
msg_error (int errnum, const char *format, ...)
{
va_list args;
- char *e;
- struct msg m;
-
- m.category = MSG_C_GENERAL;
- m.severity = MSG_S_ERROR;
-
va_start (args, format);
- e = xvasprintf (format, args);
+ struct string s = DS_EMPTY_INITIALIZER;
+ ds_put_vformat (&s, format, args);
va_end (args);
-
- m.file_name = NULL;
- m.first_line = m.last_line = 0;
- m.first_column = m.last_column = 0;
- m.text = xasprintf (_("%s: %s"), e, strerror (errnum));
- free (e);
-
- msg_emit (&m);
+ ds_put_format (&s, ": %s", strerror (errnum));
+
+ struct msg *m = xmalloc (sizeof *m);
+ *m = (struct msg) {
+ .category = MSG_C_GENERAL,
+ .severity = MSG_S_ERROR,
+ .text = ds_steal_cstr (&s),
+ };
+ msg_emit (m);
}
msg_aux = aux;
}
\f
+/* msg_location. */
+
+void
+msg_location_uninit (struct msg_location *loc)
+{
+ free (loc->file_name);
+}
+
+void
+msg_location_destroy (struct msg_location *loc)
+{
+ if (loc)
+ {
+ msg_location_uninit (loc);
+ free (loc);
+ }
+}
+
+struct msg_location *
+msg_location_dup (const struct msg_location *src)
+{
+ if (!src)
+ return NULL;
+
+ struct msg_location *dst = xmalloc (sizeof *dst);
+ *dst = (struct msg_location) {
+ .file_name = xstrdup_if_nonnull (src->file_name),
+ .first_line = src->first_line,
+ .last_line = src->last_line,
+ .first_column = src->first_column,
+ .last_column = src->last_column,
+ };
+ return dst;
+}
+
+bool
+msg_location_is_empty (const struct msg_location *loc)
+{
+ return !loc || (!loc->file_name
+ && loc->first_line <= 0
+ && loc->first_column <= 0);
+}
+
+void
+msg_location_format (const struct msg_location *loc, struct string *s)
+{
+ if (!loc)
+ return;
+
+ if (loc->file_name)
+ ds_put_cstr (s, loc->file_name);
+
+ int l1 = loc->first_line;
+ int l2 = MAX (loc->first_line, loc->last_line - 1);
+ int c1 = loc->first_column;
+ int c2 = MAX (loc->first_column, loc->last_column - 1);
+
+ if (l1 > 0)
+ {
+ if (loc->file_name)
+ ds_put_byte (s, ':');
+
+ if (l2 > l1)
+ {
+ if (c1 > 0)
+ ds_put_format (s, "%d.%d-%d.%d", l1, c1, l2, c2);
+ else
+ ds_put_format (s, "%d-%d", l1, l2);
+ }
+ else
+ {
+ if (c1 > 0)
+ {
+ if (c2 > c1)
+ {
+ /* The GNU coding standards say to use
+ LINENO-1.COLUMN-1-COLUMN-2 for this case, but GNU
+ Emacs interprets COLUMN-2 as LINENO-2 if I do that.
+ I've submitted an Emacs bug report:
+ http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7725.
+
+ For now, let's be compatible. */
+ ds_put_format (s, "%d.%d-%d.%d", l1, c1, l1, c2);
+ }
+ else
+ ds_put_format (s, "%d.%d", l1, c1);
+ }
+ else
+ ds_put_format (s, "%d", l1);
+ }
+ }
+ else if (c1 > 0)
+ {
+ if (c2 > c1)
+ ds_put_format (s, ".%d-%d", c1, c2);
+ else
+ ds_put_format (s, ".%d", c1);
+ }
+}
+\f
+/* msg_stack */
+
+void
+msg_stack_destroy (struct msg_stack *stack)
+{
+ if (stack)
+ {
+ msg_location_destroy (stack->location);
+ free (stack->description);
+ free (stack);
+ }
+}
+
+struct msg_stack *
+msg_stack_dup (const struct msg_stack *src)
+{
+ struct msg_stack *dst = xmalloc (sizeof *src);
+ *dst = (struct msg_stack) {
+ .location = msg_location_dup (src->location),
+ .description = xstrdup_if_nonnull (src->description),
+ };
+ return dst;
+}
+\f
/* Working with messages. */
const char *
/* Duplicate a message */
struct msg *
-msg_dup (const struct msg *m)
+msg_dup (const struct msg *src)
{
- struct msg *new_msg;
-
- new_msg = xmemdup (m, sizeof *m);
- if (m->file_name != NULL)
- new_msg->file_name = xstrdup (m->file_name);
- new_msg->text = xstrdup (m->text);
-
- return new_msg;
+ struct msg_stack **ms = xmalloc (src->n_stack * sizeof *ms);
+ for (size_t i = 0; i < src->n_stack; i++)
+ ms[i] = msg_stack_dup (src->stack[i]);
+
+ struct msg *dst = xmalloc (sizeof *dst);
+ *dst = (struct msg) {
+ .category = src->category,
+ .severity = src->severity,
+ .stack = ms,
+ .n_stack = src->n_stack,
+ .location = msg_location_dup (src->location),
+ .command_name = xstrdup_if_nonnull (src->command_name),
+ .text = xstrdup (src->text),
+ };
+ return dst;
}
/* Frees a message created by msg_dup().
void
msg_destroy (struct msg *m)
{
- free (m->file_name);
- free (m->text);
- free (m);
+ if (m)
+ {
+ for (size_t i = 0; i < m->n_stack; i++)
+ msg_stack_destroy (m->stack[i]);
+ free (m->stack);
+ msg_location_destroy (m->location);
+ free (m->text);
+ free (m->command_name);
+ free (m);
+ }
}
char *
-msg_to_string (const struct msg *m, const char *command_name)
+msg_to_string (const struct msg *m)
{
struct string s;
ds_init_empty (&s);
- if (m->category != MSG_C_GENERAL
- && (m->file_name || m->first_line > 0 || m->first_column > 0))
+ for (size_t i = 0; i < m->n_stack; i++)
{
- int l1 = m->first_line;
- int l2 = MAX (m->first_line, m->last_line - 1);
- int c1 = m->first_column;
- int c2 = MAX (m->first_column, m->last_column - 1);
-
- if (m->file_name)
- ds_put_format (&s, "%s", m->file_name);
-
- if (l1 > 0)
- {
- if (!ds_is_empty (&s))
- ds_put_byte (&s, ':');
-
- if (l2 > l1)
- {
- if (c1 > 0)
- ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l2, c2);
- else
- ds_put_format (&s, "%d-%d", l1, l2);
- }
- else
- {
- if (c1 > 0)
- {
- if (c2 > c1)
- {
- /* The GNU coding standards say to use
- LINENO-1.COLUMN-1-COLUMN-2 for this case, but GNU
- Emacs interprets COLUMN-2 as LINENO-2 if I do that.
- I've submitted an Emacs bug report:
- http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7725.
-
- For now, let's be compatible. */
- ds_put_format (&s, "%d.%d-%d.%d", l1, c1, l1, c2);
- }
- else
- ds_put_format (&s, "%d.%d", l1, c1);
- }
- else
- ds_put_format (&s, "%d", l1);
- }
- }
- else if (c1 > 0)
+ const struct msg_stack *ms = m->stack[i];
+ if (!msg_location_is_empty (ms->location))
{
- if (c2 > c1)
- ds_put_format (&s, ".%d-%d", c1, c2);
- else
- ds_put_format (&s, ".%d", c1);
+ msg_location_format (ms->location, &s);
+ ds_put_cstr (&s, ": ");
}
+ ds_put_format (&s, "%s\n", ms->description);
+ }
+ if (m->category != MSG_C_GENERAL && !msg_location_is_empty (m->location))
+ {
+ msg_location_format (m->location, &s);
ds_put_cstr (&s, ": ");
}
ds_put_format (&s, "%s: ", msg_severity_to_string (m->severity));
- if (m->category == MSG_C_SYNTAX && command_name != NULL)
- ds_put_format (&s, "%s: ", command_name);
+ if (m->category == MSG_C_SYNTAX && m->command_name != NULL)
+ ds_put_format (&s, "%s: ", m->command_name);
ds_put_cstr (&s, m->text);
}
-static int entrances = 0;
-
static void
-ship_message (struct msg *m)
+ship_message (const struct msg *m)
{
- entrances++;
- if ( ! m->shipped )
- {
- if (msg_handler && entrances <= 1)
- msg_handler (m, msg_aux);
- else
- {
- fwrite (m->text, 1, strlen (m->text), stderr);
- fwrite ("\n", 1, 1, stderr);
- }
- }
- m->shipped = true;
- entrances--;
+ enum { MAX_STACK = 4 };
+ static const struct msg *stack[MAX_STACK];
+ static size_t n;
+
+ /* If we're recursing on a given message, or recursing deeply, drop it. */
+ if (n >= MAX_STACK)
+ return;
+ for (size_t i = 0; i < n; i++)
+ if (stack[i] == m)
+ return;
+
+ stack[n++] = m;
+ if (msg_handler && n <= 1)
+ msg_handler (m, msg_aux);
+ else
+ fprintf (stderr, "%s\n", m->text);
+ n--;
}
static void
submit_note (char *s)
{
- struct msg m;
-
- m.category = MSG_C_GENERAL;
- m.severity = MSG_S_NOTE;
- m.file_name = NULL;
- m.first_line = 0;
- m.last_line = 0;
- m.first_column = 0;
- m.last_column = 0;
- m.text = s;
- m.shipped = false;
-
+ struct msg m = {
+ .category = MSG_C_GENERAL,
+ .severity = MSG_S_NOTE,
+ .text = s,
+ };
ship_message (&m);
free (s);
}
-
-
static void
process_msg (struct msg *m)
{
if (too_many_errors
|| (too_many_notes && m->severity == MSG_S_NOTE)
- || (warnings_off && m->severity == MSG_S_WARNING) )
+ || (warnings_off && m->severity == MSG_S_WARNING))
return;
ship_message (m);
}
-/* Emits M as an error message.
- Frees allocated data in M. */
+/* Emits M as an error message. Takes ownership of M. */
void
msg_emit (struct msg *m)
{
- m->shipped = false;
if (!messages_disabled)
- process_msg (m);
-
- free (m->text);
+ process_msg (m);
+ msg_destroy (m);
}
/* Disables message output until the next call to msg_enable. If
\f
/* Private functions. */
-void
-request_bug_report (const char *msg)
+static char fatal_error_message[1024];
+static int fatal_error_message_bytes = 0;
+
+static char diagnostic_information[1024];
+static int diagnostic_information_bytes = 0;
+
+static int
+append_message (char *msg, int bytes_used, const char *fmt, ...)
+{
+ va_list va;
+ va_start (va, fmt);
+ int ret = vsnprintf (msg + bytes_used, 1024 - bytes_used, fmt, va);
+ va_end (va);
+ assert (ret >= 0);
+
+ return ret;
+}
+
+
+/* Generate a row of asterisks held in statically allocated memory */
+static struct substring
+generate_banner (void)
+{
+ static struct substring banner;
+ if (!banner.string)
+ banner = ss_cstr ("******************************************************\n");
+ return banner;
+}
+
+const char *
+prepare_fatal_error_message (void)
+{
+ fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, generate_banner ().string);
+
+ fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "You have discovered a bug in PSPP. Please report this\n");
+ fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
+ fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "message, *plus* several lines of output just above it.\n");
+ fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "For the best chance at having the bug fixed, also\n");
+ fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "include the syntax file that triggered it and a sample\n");
+ fatal_error_message_bytes += append_message (fatal_error_message, fatal_error_message_bytes, "of any data file used for input.\n");
+ return fatal_error_message;
+}
+
+const char *
+prepare_diagnostic_information (void)
{
- fprintf (stderr, "******************************************************\n");
- fprintf (stderr, "You have discovered a bug in PSPP. Please report this\n");
- fprintf (stderr, "to " PACKAGE_BUGREPORT ". Please include this entire\n");
- fprintf (stderr, "message, *plus* several lines of output just above it.\n");
- fprintf (stderr, "For the best chance at having the bug fixed, also\n");
- fprintf (stderr, "include the syntax file that triggered it and a sample\n");
- fprintf (stderr, "of any data file used for input.\n");
- fprintf (stderr, "proximate cause: %s\n", msg);
- fprintf (stderr, "version: %s\n", stat_version);
- fprintf (stderr, "host_system: %s\n", host_system);
- fprintf (stderr, "build_system: %s\n", build_system);
- fprintf (stderr, "locale_dir: %s\n", locale_dir);
- fprintf (stderr, "compiler version: %s\n",
+ diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "version: %s\n", version);
+ diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "host_system: %s\n", host_system);
+ diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "build_system: %s\n", build_system);
+ diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "locale_dir: %s\n", relocate (locale_dir));
+ diagnostic_information_bytes += append_message (diagnostic_information, diagnostic_information_bytes, "compiler version: %s\n",
#ifdef __VERSION__
__VERSION__
#else
"Unknown"
#endif
- );
- fprintf (stderr, "******************************************************\n");
+);
+
+ return diagnostic_information;
}
+void
+request_bug_report (const char *msg)
+{
+ write (STDERR_FILENO, fatal_error_message, fatal_error_message_bytes);
+ write (STDERR_FILENO, "proximate cause: ", 21);
+ write (STDERR_FILENO, msg, strlen (msg));
+ write (STDERR_FILENO, "\n", 1);
+ write (STDERR_FILENO, diagnostic_information, diagnostic_information_bytes);
+ const struct substring banner = generate_banner ();
+ write (STDERR_FILENO, banner.string, banner.length);
+}