tools: clean up error message printing
This commit is contained in:
parent
8d19acd91a
commit
695f71d946
|
@ -30,6 +30,7 @@
|
||||||
#include "stardict.h"
|
#include "stardict.h"
|
||||||
#include "stardict-private.h"
|
#include "stardict-private.h"
|
||||||
#include "generator.h"
|
#include "generator.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
|
||||||
// --- Pronunciation generator -------------------------------------------------
|
// --- Pronunciation generator -------------------------------------------------
|
||||||
|
@ -149,7 +150,7 @@ worker_writer (WorkerData *data)
|
||||||
|
|
||||||
stardict_iterator_next (data->iterator);
|
stardict_iterator_next (data->iterator);
|
||||||
if (fprintf (data->child_stdin, "%s\n", x) < 0)
|
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);
|
g_free (x);
|
||||||
}
|
}
|
||||||
|
@ -169,16 +170,10 @@ get_void_entry (gchar *cmdline[])
|
||||||
if (!g_spawn_sync (NULL, cmdline, NULL,
|
if (!g_spawn_sync (NULL, cmdline, NULL,
|
||||||
G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL,
|
G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL,
|
||||||
&output, NULL, &exit_status, &error))
|
&output, NULL, &exit_status, &error))
|
||||||
{
|
fatal ("Error: couldn't spawn espeak: %s\n", error->message);
|
||||||
g_printerr ("Error: couldn't spawn espeak: %s", error->message);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (exit_status)
|
if (exit_status)
|
||||||
{
|
fatal ("Error: espeak returned %d\n", exit_status);
|
||||||
g_printerr ("Error: espeak returned %d\n", exit_status);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
@ -193,7 +188,7 @@ worker (WorkerData *data)
|
||||||
if (!g_spawn_async_with_pipes (NULL, data->cmdline, NULL,
|
if (!g_spawn_async_with_pipes (NULL, data->cmdline, NULL,
|
||||||
G_SPAWN_SEARCH_PATH, NULL, NULL,
|
G_SPAWN_SEARCH_PATH, NULL, NULL,
|
||||||
NULL, &child_in, &child_out, NULL, &error))
|
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");
|
data->child_stdin = fdopen (child_in, "wb");
|
||||||
if (!data->child_stdin)
|
if (!data->child_stdin)
|
||||||
|
@ -228,7 +223,7 @@ worker (WorkerData *data)
|
||||||
while ((c = fgetc (child_stdout)) != EOF && c != '\n')
|
while ((c = fgetc (child_stdout)) != EOF && c != '\n')
|
||||||
g_string_append_c (s, c);
|
g_string_append_c (s, c);
|
||||||
if (c == EOF)
|
if (c == EOF)
|
||||||
g_error ("eSpeak process died too soon");
|
fatal ("eSpeak process died too soon\n");
|
||||||
|
|
||||||
gchar *translation = g_string_free (s, FALSE);
|
gchar *translation = g_string_free (s, FALSE);
|
||||||
*output_end = translation;
|
*output_end = translation;
|
||||||
|
@ -246,11 +241,8 @@ worker (WorkerData *data)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fgetc (child_stdout) != EOF)
|
if (fgetc (child_stdout) != EOF)
|
||||||
{
|
fatal ("Error: eSpeak has written more lines than it should. "
|
||||||
g_printerr ("Error: eSpeak has written more lines than it should. "
|
|
||||||
"The output would be corrupt, aborting.\n");
|
"The output would be corrupt, aborting.\n");
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose (child_stdout);
|
fclose (child_stdout);
|
||||||
return g_thread_join (writer);
|
return g_thread_join (writer);
|
||||||
|
@ -313,18 +305,10 @@ G_GNUC_END_IGNORE_DEPRECATIONS
|
||||||
("input.ifo output-basename - add pronunciation to dictionaries");
|
("input.ifo output-basename - add pronunciation to dictionaries");
|
||||||
g_option_context_add_main_entries (ctx, entries, NULL);
|
g_option_context_add_main_entries (ctx, entries, NULL);
|
||||||
if (!g_option_context_parse (ctx, &argc, &argv, &error))
|
if (!g_option_context_parse (ctx, &argc, &argv, &error))
|
||||||
{
|
fatal ("Error: option parsing failed: %s\n", error->message);
|
||||||
g_printerr ("Error: option parsing failed: %s\n", error->message);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argc != 3)
|
if (argc != 3)
|
||||||
{
|
fatal ("%s", g_option_context_get_help (ctx, TRUE, FALSE));
|
||||||
gchar *help = g_option_context_get_help (ctx, TRUE, FALSE);
|
|
||||||
g_printerr ("%s", help);
|
|
||||||
g_free (help);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_option_context_free (ctx);
|
g_option_context_free (ctx);
|
||||||
|
|
||||||
|
@ -343,20 +327,13 @@ G_GNUC_END_IGNORE_DEPRECATIONS
|
||||||
printf ("Loading the original dictionary...\n");
|
printf ("Loading the original dictionary...\n");
|
||||||
StardictDict *dict = stardict_dict_new (argv[1], &error);
|
StardictDict *dict = stardict_dict_new (argv[1], &error);
|
||||||
if (!dict)
|
if (!dict)
|
||||||
{
|
fatal ("Error: opening the dictionary failed: %s\n", error->message);
|
||||||
g_printerr ("Error: opening the dictionary failed: %s\n",
|
|
||||||
error->message);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
gsize n_words = stardict_info_get_word_count
|
gsize n_words = stardict_info_get_word_count
|
||||||
(stardict_dict_get_info (dict));
|
(stardict_dict_get_info (dict));
|
||||||
|
|
||||||
if (n_processes <= 0)
|
if (n_processes <= 0)
|
||||||
{
|
fatal ("Error: there must be at least one process\n");
|
||||||
g_printerr ("Error: there must be at least one process\n");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((gsize) n_processes > n_words * 1024)
|
if ((gsize) n_processes > n_words * 1024)
|
||||||
{
|
{
|
||||||
|
@ -435,11 +412,8 @@ G_GNUC_END_IGNORE_DEPRECATIONS
|
||||||
// Put extended entries into a new dictionary
|
// Put extended entries into a new dictionary
|
||||||
Generator *generator = generator_new (argv[2], &error);
|
Generator *generator = generator_new (argv[2], &error);
|
||||||
if (!generator)
|
if (!generator)
|
||||||
{
|
fatal ("Error: failed to create the output dictionary: %s\n",
|
||||||
g_printerr ("Error: failed to create the output dictionary: %s\n",
|
|
||||||
error->message);
|
error->message);
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
StardictInfo *info = generator->info;
|
StardictInfo *info = generator->info;
|
||||||
stardict_info_copy (info, stardict_dict_get_info (dict));
|
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)
|
if (!generator_write_fields (generator, &start_link, &error)
|
||||||
|| !generator_finish_entry (generator,
|
|| !generator_finish_entry (generator,
|
||||||
stardict_iterator_get_word (iterator), &error))
|
stardict_iterator_get_word (iterator), &error))
|
||||||
{
|
fatal ("Error: write failed: %s\n", error->message);
|
||||||
g_printerr ("Error: write failed: %s\n", error->message);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_object_unref (entry);
|
g_object_unref (entry);
|
||||||
|
|
||||||
|
@ -513,11 +484,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
|
||||||
|
|
||||||
putchar ('\n');
|
putchar ('\n');
|
||||||
if (!generator_finish (generator, &error))
|
if (!generator_finish (generator, &error))
|
||||||
{
|
fatal ("Error: failed to write the dictionary: %s\n", error->message);
|
||||||
g_printerr ("Error: failed to write the dictionary: %s\n",
|
|
||||||
error->message);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
generator_free (generator);
|
generator_free (generator);
|
||||||
g_object_unref (dict);
|
g_object_unref (dict);
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
#include "stardict.h"
|
#include "stardict.h"
|
||||||
#include "stardict-private.h"
|
#include "stardict-private.h"
|
||||||
#include "generator.h"
|
#include "generator.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
enum { PIPE_READ, PIPE_WRITE };
|
enum { PIPE_READ, PIPE_WRITE };
|
||||||
|
|
||||||
|
@ -177,18 +178,10 @@ main (int argc, char *argv[])
|
||||||
(ctx, "Transform dictionaries using a filter program.");
|
(ctx, "Transform dictionaries using a filter program.");
|
||||||
g_option_context_set_description (ctx, "Test?");
|
g_option_context_set_description (ctx, "Test?");
|
||||||
if (!g_option_context_parse (ctx, &argc, &argv, &error))
|
if (!g_option_context_parse (ctx, &argc, &argv, &error))
|
||||||
{
|
fatal ("Error: option parsing failed: %s\n", error->message);
|
||||||
g_printerr ("Error: option parsing failed: %s\n", error->message);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argc < 3)
|
if (argc < 3)
|
||||||
{
|
fatal ("%s", g_option_context_get_help (ctx, TRUE, FALSE));
|
||||||
gchar *help = g_option_context_get_help (ctx, TRUE, FALSE);
|
|
||||||
g_printerr ("%s", help);
|
|
||||||
g_free (help);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// GLib is bullshit, getopt_long() always correctly removes this
|
// GLib is bullshit, getopt_long() always correctly removes this
|
||||||
gint program_argv_start = 3;
|
gint program_argv_start = 3;
|
||||||
|
@ -200,20 +193,16 @@ main (int argc, char *argv[])
|
||||||
printf ("Loading the original dictionary...\n");
|
printf ("Loading the original dictionary...\n");
|
||||||
StardictDict *dict = stardict_dict_new (argv[1], &error);
|
StardictDict *dict = stardict_dict_new (argv[1], &error);
|
||||||
if (!dict)
|
if (!dict)
|
||||||
{
|
fatal ("Error: opening the dictionary failed: %s\n", error->message);
|
||||||
g_printerr ("Error: opening the dictionary failed: %s\n",
|
|
||||||
error->message);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf ("Filtering entries...\n");
|
printf ("Filtering entries...\n");
|
||||||
gint child_in[2];
|
gint child_in[2];
|
||||||
if (!g_unix_open_pipe (child_in, 0, &error))
|
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 ();
|
FILE *child_out = tmpfile ();
|
||||||
if (!child_out)
|
if (!child_out)
|
||||||
g_error ("tmpfile: %s", strerror (errno));
|
fatal ("tmpfile: %s\n", strerror (errno));
|
||||||
|
|
||||||
GPid pid = -1;
|
GPid pid = -1;
|
||||||
if (!g_spawn_async_with_fds (NULL /* working_directory */,
|
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,
|
G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
|
||||||
NULL /* child_setup */, NULL /* user_data */,
|
NULL /* child_setup */, NULL /* user_data */,
|
||||||
&pid, child_in[PIPE_READ], fileno (child_out), STDERR_FILENO, &error))
|
&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))
|
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)
|
if (!g_close (child_in[PIPE_READ], &error)
|
||||||
|| !g_close (child_in[PIPE_WRITE], &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");
|
printf ("Waiting for the filter to finish...\n");
|
||||||
int wstatus = errno = 0;
|
int wstatus = errno = 0;
|
||||||
if (waitpid (pid, &wstatus, 0) < 1
|
if (waitpid (pid, &wstatus, 0) < 1
|
||||||
|| !WIFEXITED (wstatus) || WEXITSTATUS (wstatus) > 0)
|
|| !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),
|
GMappedFile *filtered = g_mapped_file_new_from_fd (fileno (child_out),
|
||||||
FALSE /* writable */, &error);
|
FALSE /* writable */, &error);
|
||||||
if (!filtered)
|
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");
|
printf ("Writing the new dictionary...\n");
|
||||||
Generator *generator = generator_new (argv[2], &error);
|
Generator *generator = generator_new (argv[2], &error);
|
||||||
if (!generator)
|
if (!generator)
|
||||||
{
|
fatal ("Error: failed to create the output dictionary: %s\n",
|
||||||
g_printerr ("Error: failed to create the output dictionary: %s\n",
|
|
||||||
error->message);
|
error->message);
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
StardictInfo *info = generator->info;
|
StardictInfo *info = generator->info;
|
||||||
stardict_info_copy (info, stardict_dict_get_info (dict));
|
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)
|
if (!update_from_filter (dict, generator, filtered, &error)
|
||||||
|| !generator_finish (generator, &error))
|
|| !generator_finish (generator, &error))
|
||||||
{
|
fatal ("Error: failed to write the dictionary: %s\n", error->message);
|
||||||
g_printerr ("Error: failed to write the dictionary: %s\n",
|
|
||||||
error->message);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_mapped_file_unref (filtered);
|
g_mapped_file_unref (filtered);
|
||||||
fclose (child_out);
|
fclose (child_out);
|
||||||
|
|
14
src/utils.c
14
src/utils.c
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* utils.c: miscellaneous utilities
|
* 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
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
* purpose with or without fee is hereby granted.
|
* purpose with or without fee is hereby granted.
|
||||||
|
@ -20,6 +20,7 @@
|
||||||
#include <gio/gio.h>
|
#include <gio/gio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
@ -99,3 +100,14 @@ update_curses_terminal_size (void)
|
||||||
refresh ();
|
refresh ();
|
||||||
#endif // HAVE_RESIZETERM && TIOCGWINSZ
|
#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);
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* utils.h: miscellaneous utilities
|
* 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
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
* purpose with or without fee is hereby granted.
|
* 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);
|
gchar *stream_read_string (GDataInputStream *dis, GError **error);
|
||||||
gboolean xstrtoul (unsigned long *out, const char *s, int base);
|
gboolean xstrtoul (unsigned long *out, const char *s, int base);
|
||||||
void update_curses_terminal_size (void);
|
void update_curses_terminal_size (void);
|
||||||
|
void fatal (const gchar *format, ...) G_GNUC_PRINTF (1, 2) G_GNUC_NORETURN;
|
||||||
|
|
||||||
#endif // ! UTILS_H
|
#endif // ! UTILS_H
|
||||||
|
|
Loading…
Reference in New Issue