diff options
| author | Přemysl Eric Janouch <p@janouch.name> | 2020-09-03 23:54:12 +0200 | 
|---|---|---|
| committer | Přemysl Eric Janouch <p@janouch.name> | 2020-09-04 00:13:34 +0200 | 
| commit | 695f71d9462ce3fe0bdcbe7fae9015ce254512fd (patch) | |
| tree | 88ca5eebc13f3868c6c9c850c6eb00a8aa085e58 | |
| parent | 8d19acd91af9592d862ef2a7aa8e95eea4160152 (diff) | |
| download | tdv-695f71d9462ce3fe0bdcbe7fae9015ce254512fd.tar.gz tdv-695f71d9462ce3fe0bdcbe7fae9015ce254512fd.tar.xz tdv-695f71d9462ce3fe0bdcbe7fae9015ce254512fd.zip | |
tools: clean up error message printing
| -rw-r--r-- | src/add-pronunciation.c | 61 | ||||
| -rw-r--r-- | src/transform.c | 44 | ||||
| -rw-r--r-- | src/utils.c | 14 | ||||
| -rw-r--r-- | src/utils.h | 3 | 
4 files changed, 42 insertions, 80 deletions
| diff --git a/src/add-pronunciation.c b/src/add-pronunciation.c index 26261f9..eb0d464 100644 --- a/src/add-pronunciation.c +++ b/src/add-pronunciation.c @@ -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); diff --git a/src/transform.c b/src/transform.c index 2d5c2f2..f9909b7 100644 --- a/src/transform.c +++ b/src/transform.c @@ -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); diff --git a/src/utils.c b/src/utils.c index ac203a5..89cca9a 100644 --- a/src/utils.c +++ b/src/utils.c @@ -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); +} diff --git a/src/utils.h b/src/utils.h index 5ca818d..178a1d9 100644 --- a/src/utils.h +++ b/src/utils.h @@ -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 | 
