tools: clean up error message printing

This commit is contained in:
Přemysl Eric Janouch 2020-09-03 23:54:12 +02:00
parent 8d19acd91a
commit 695f71d946
Signed by: p
GPG Key ID: A0420B94F92B9493
4 changed files with 42 additions and 80 deletions

View File

@ -30,6 +30,7 @@
#include "stardict.h"
#include "stardict-private.h"
#include "generator.h"
#include "utils.h"
// --- Pronunciation generator -------------------------------------------------
@ -149,7 +150,7 @@ worker_writer (WorkerData *data)
stardict_iterator_next (data->iterator);
if (fprintf (data->child_stdin, "%s\n", x) < 0)
g_error ("write to eSpeak failed: %s", strerror (errno));
fatal ("write to eSpeak failed: %s\n", strerror (errno));
g_free (x);
}
@ -169,16 +170,10 @@ get_void_entry (gchar *cmdline[])
if (!g_spawn_sync (NULL, cmdline, NULL,
G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL,
&output, NULL, &exit_status, &error))
{
g_printerr ("Error: couldn't spawn espeak: %s", error->message);
exit (EXIT_FAILURE);
}
fatal ("Error: couldn't spawn espeak: %s\n", error->message);
if (exit_status)
{
g_printerr ("Error: espeak returned %d\n", exit_status);
exit (EXIT_FAILURE);
}
fatal ("Error: espeak returned %d\n", exit_status);
return output;
}
@ -193,7 +188,7 @@ worker (WorkerData *data)
if (!g_spawn_async_with_pipes (NULL, data->cmdline, NULL,
G_SPAWN_SEARCH_PATH, NULL, NULL,
NULL, &child_in, &child_out, NULL, &error))
g_error ("g_spawn() failed: %s", error->message);
fatal ("g_spawn: %s\n", error->message);
data->child_stdin = fdopen (child_in, "wb");
if (!data->child_stdin)
@ -228,7 +223,7 @@ worker (WorkerData *data)
while ((c = fgetc (child_stdout)) != EOF && c != '\n')
g_string_append_c (s, c);
if (c == EOF)
g_error ("eSpeak process died too soon");
fatal ("eSpeak process died too soon\n");
gchar *translation = g_string_free (s, FALSE);
*output_end = translation;
@ -246,11 +241,8 @@ worker (WorkerData *data)
}
if (fgetc (child_stdout) != EOF)
{
g_printerr ("Error: eSpeak has written more lines than it should. "
fatal ("Error: eSpeak has written more lines than it should. "
"The output would be corrupt, aborting.\n");
exit (EXIT_FAILURE);
}
fclose (child_stdout);
return g_thread_join (writer);
@ -313,18 +305,10 @@ G_GNUC_END_IGNORE_DEPRECATIONS
("input.ifo output-basename - add pronunciation to dictionaries");
g_option_context_add_main_entries (ctx, entries, NULL);
if (!g_option_context_parse (ctx, &argc, &argv, &error))
{
g_printerr ("Error: option parsing failed: %s\n", error->message);
exit (EXIT_FAILURE);
}
fatal ("Error: option parsing failed: %s\n", error->message);
if (argc != 3)
{
gchar *help = g_option_context_get_help (ctx, TRUE, FALSE);
g_printerr ("%s", help);
g_free (help);
exit (EXIT_FAILURE);
}
fatal ("%s", g_option_context_get_help (ctx, TRUE, FALSE));
g_option_context_free (ctx);
@ -343,20 +327,13 @@ G_GNUC_END_IGNORE_DEPRECATIONS
printf ("Loading the original dictionary...\n");
StardictDict *dict = stardict_dict_new (argv[1], &error);
if (!dict)
{
g_printerr ("Error: opening the dictionary failed: %s\n",
error->message);
exit (EXIT_FAILURE);
}
fatal ("Error: opening the dictionary failed: %s\n", error->message);
gsize n_words = stardict_info_get_word_count
(stardict_dict_get_info (dict));
if (n_processes <= 0)
{
g_printerr ("Error: there must be at least one process\n");
exit (EXIT_FAILURE);
}
fatal ("Error: there must be at least one process\n");
if ((gsize) n_processes > n_words * 1024)
{
@ -435,11 +412,8 @@ G_GNUC_END_IGNORE_DEPRECATIONS
// Put extended entries into a new dictionary
Generator *generator = generator_new (argv[2], &error);
if (!generator)
{
g_printerr ("Error: failed to create the output dictionary: %s\n",
fatal ("Error: failed to create the output dictionary: %s\n",
error->message);
exit (EXIT_FAILURE);
}
StardictInfo *info = generator->info;
stardict_info_copy (info, stardict_dict_get_info (dict));
@ -493,10 +467,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
if (!generator_write_fields (generator, &start_link, &error)
|| !generator_finish_entry (generator,
stardict_iterator_get_word (iterator), &error))
{
g_printerr ("Error: write failed: %s\n", error->message);
exit (EXIT_FAILURE);
}
fatal ("Error: write failed: %s\n", error->message);
g_object_unref (entry);
@ -513,11 +484,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
putchar ('\n');
if (!generator_finish (generator, &error))
{
g_printerr ("Error: failed to write the dictionary: %s\n",
error->message);
exit (EXIT_FAILURE);
}
fatal ("Error: failed to write the dictionary: %s\n", error->message);
generator_free (generator);
g_object_unref (dict);

View File

@ -34,6 +34,7 @@
#include "stardict.h"
#include "stardict-private.h"
#include "generator.h"
#include "utils.h"
enum { PIPE_READ, PIPE_WRITE };
@ -177,18 +178,10 @@ main (int argc, char *argv[])
(ctx, "Transform dictionaries using a filter program.");
g_option_context_set_description (ctx, "Test?");
if (!g_option_context_parse (ctx, &argc, &argv, &error))
{
g_printerr ("Error: option parsing failed: %s\n", error->message);
exit (EXIT_FAILURE);
}
fatal ("Error: option parsing failed: %s\n", error->message);
if (argc < 3)
{
gchar *help = g_option_context_get_help (ctx, TRUE, FALSE);
g_printerr ("%s", help);
g_free (help);
exit (EXIT_FAILURE);
}
fatal ("%s", g_option_context_get_help (ctx, TRUE, FALSE));
// GLib is bullshit, getopt_long() always correctly removes this
gint program_argv_start = 3;
@ -200,20 +193,16 @@ main (int argc, char *argv[])
printf ("Loading the original dictionary...\n");
StardictDict *dict = stardict_dict_new (argv[1], &error);
if (!dict)
{
g_printerr ("Error: opening the dictionary failed: %s\n",
error->message);
exit (EXIT_FAILURE);
}
fatal ("Error: opening the dictionary failed: %s\n", error->message);
printf ("Filtering entries...\n");
gint child_in[2];
if (!g_unix_open_pipe (child_in, 0, &error))
g_error ("g_unix_open_pipe: %s", error->message);
fatal ("g_unix_open_pipe: %s\n", error->message);
FILE *child_out = tmpfile ();
if (!child_out)
g_error ("tmpfile: %s", strerror (errno));
fatal ("tmpfile: %s\n", strerror (errno));
GPid pid = -1;
if (!g_spawn_async_with_fds (NULL /* working_directory */,
@ -221,32 +210,29 @@ main (int argc, char *argv[])
G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
NULL /* child_setup */, NULL /* user_data */,
&pid, child_in[PIPE_READ], fileno (child_out), STDERR_FILENO, &error))
g_error ("g_spawn: %s", error->message);
fatal ("g_spawn: %s\n", error->message);
if (!write_to_filter (dict, child_in[PIPE_WRITE], &error))
g_error ("write_to_filter: %s", error->message);
fatal ("write_to_filter: %s\n", error->message);
if (!g_close (child_in[PIPE_READ], &error)
|| !g_close (child_in[PIPE_WRITE], &error))
g_error ("g_close: %s", error->message);
fatal ("g_close: %s\n", error->message);
printf ("Waiting for the filter to finish...\n");
int wstatus = errno = 0;
if (waitpid (pid, &wstatus, 0) < 1
|| !WIFEXITED (wstatus) || WEXITSTATUS (wstatus) > 0)
g_error ("Filter failed (%s, status %d)", strerror (errno), wstatus);
fatal ("Filter failed (%s, status %d)\n", strerror (errno), wstatus);
GMappedFile *filtered = g_mapped_file_new_from_fd (fileno (child_out),
FALSE /* writable */, &error);
if (!filtered)
g_error ("g_mapped_file_new_from_fd: %s", error->message);
fatal ("g_mapped_file_new_from_fd: %s\n", error->message);
printf ("Writing the new dictionary...\n");
Generator *generator = generator_new (argv[2], &error);
if (!generator)
{
g_printerr ("Error: failed to create the output dictionary: %s\n",
fatal ("Error: failed to create the output dictionary: %s\n",
error->message);
exit (EXIT_FAILURE);
}
StardictInfo *info = generator->info;
stardict_info_copy (info, stardict_dict_get_info (dict));
@ -256,11 +242,7 @@ main (int argc, char *argv[])
if (!update_from_filter (dict, generator, filtered, &error)
|| !generator_finish (generator, &error))
{
g_printerr ("Error: failed to write the dictionary: %s\n",
error->message);
exit (EXIT_FAILURE);
}
fatal ("Error: failed to write the dictionary: %s\n", error->message);
g_mapped_file_unref (filtered);
fclose (child_out);

View File

@ -1,7 +1,7 @@
/*
* utils.c: miscellaneous utilities
*
* Copyright (c) 2013 - 2015, Přemysl Eric Janouch <p@janouch.name>
* Copyright (c) 2013 - 2020, Přemysl Eric Janouch <p@janouch.name>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
@ -20,6 +20,7 @@
#include <gio/gio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include <curses.h>
#include <termios.h>
@ -99,3 +100,14 @@ update_curses_terminal_size (void)
refresh ();
#endif // HAVE_RESIZETERM && TIOCGWINSZ
}
/// Print a fatal error message and terminate the process immediately.
void
fatal (const gchar *format, ...)
{
va_list ap;
va_start (ap, format);
vfprintf (stderr, format, ap);
exit (EXIT_FAILURE);
va_end (ap);
}

View File

@ -1,7 +1,7 @@
/*
* utils.h: miscellaneous utilities
*
* Copyright (c) 2013 - 2015, Přemysl Eric Janouch <p@janouch.name>
* Copyright (c) 2013 - 2020, Přemysl Eric Janouch <p@janouch.name>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
@ -40,5 +40,6 @@ gboolean stream_read_all (GByteArray *ba, GInputStream *is, GError **error);
gchar *stream_read_string (GDataInputStream *dis, GError **error);
gboolean xstrtoul (unsigned long *out, const char *s, int base);
void update_curses_terminal_size (void);
void fatal (const gchar *format, ...) G_GNUC_PRINTF (1, 2) G_GNUC_NORETURN;
#endif // ! UTILS_H