From 0fbc98097fac4d3e647684f344e508abae109fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Mon, 22 Aug 2016 16:32:25 +0100 Subject: [PATCH] glib-genmarshal: add --output option to write output to a file https://bugzilla.gnome.org/show_bug.cgi?id=770175 --- gobject/glib-genmarshal.c | 48 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/gobject/glib-genmarshal.c b/gobject/glib-genmarshal.c index ba13e4414..2941954b9 100644 --- a/gobject/glib-genmarshal.c +++ b/gobject/glib-genmarshal.c @@ -122,6 +122,9 @@ static const GScannerConfig scanner_config_template = }; static gchar * const std_marshaller_prefix = "g_cclosure_marshal"; static gchar *marshaller_prefix = "g_cclosure_user_marshal"; +static gchar *output_fn = NULL; +static gint output_fd = -1; +static gchar *output_tmpfn = NULL; static GHashTable *marshallers = NULL; static FILE *fout = NULL; static gboolean gen_cheader = FALSE; @@ -807,9 +810,23 @@ main (int argc, /* setup auxiliary structs */ scanner = g_scanner_new (&scanner_config_template); - fout = stdout; marshallers = g_hash_table_new (g_str_hash, g_str_equal); + if (output_fn) + { + output_tmpfn = g_strconcat (output_fn, ".XXXXXX", NULL); + if ((output_fd = g_mkstemp (output_tmpfn)) == -1) + { + g_printerr ("Failed to create temp file \"%s\": %s\n", output_tmpfn, g_strerror (errno)); + return EXIT_FAILURE; + } + + fout = fdopen (output_fd, "wb"); + g_assert (fout != NULL); + } + else + fout = stdout; + /* add standard marshallers of the GObject library */ if (std_includes) for (i = 0; i < G_N_ELEMENTS (gobject_marshallers); i++) @@ -939,6 +956,19 @@ main (int argc, g_hash_table_foreach_remove (marshallers, string_key_destroy, NULL); g_hash_table_destroy (marshallers); + if (output_fn) + { + fclose (fout); + if (g_rename (output_tmpfn, output_fn) != 0) + { + g_printerr ("Failed to rename \"%s\" to \"%s\": %s\n", output_tmpfn, output_fn, g_strerror (errno)); + g_unlink (output_tmpfn); + exit_status = EXIT_FAILURE; + } + + g_free (output_tmpfn); + } + return exit_status; } @@ -1002,6 +1032,21 @@ parse_args (gint *argc_p, } argv[i] = NULL; } + else if ((strcmp ("--output", argv[i]) == 0) || + (strncmp ("--output=", argv[i], 9) == 0)) + { + gchar *equal = argv[i] + 8; + + if (*equal == '=') + output_fn = equal + 1; + else if (i + 1 < argc) + { + output_fn = argv[i + 1]; + argv[i] = NULL; + i += 1; + } + argv[i] = NULL; + } else if (strcmp ("-h", argv[i]) == 0 || strcmp ("-?", argv[i]) == 0 || strcmp ("--help", argv[i]) == 0) @@ -1072,6 +1117,7 @@ print_blurb (FILE *bout, g_fprintf (bout, " --header Generate C headers\n"); g_fprintf (bout, " --body Generate C code\n"); g_fprintf (bout, " --prefix=string Specify marshaller prefix\n"); + g_fprintf (bout, " --output=file Write output into the specified file\n"); g_fprintf (bout, " --skip-source Skip source location comments\n"); g_fprintf (bout, " --stdinc, --nostdinc Include/use standard marshallers\n"); g_fprintf (bout, " --internal Mark generated functions as internal\n");