glib/girepository/decompiler/decompiler.c

137 lines
4.0 KiB
C
Raw Normal View History

2012-02-03 19:42:56 +01:00
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
* GObject introspection: IDL generator
2005-05-09 16:24:46 +02:00
*
* Copyright (C) 2005 Matthias Clasen
* Copyright (C) 2008,2009 Red Hat, Inc.
2005-05-09 16:24:46 +02:00
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
2005-05-09 16:24:46 +02:00
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <locale.h>
2005-05-09 16:24:46 +02:00
#include <glib.h>
#include <glib/gstdio.h>
#include <gmodule.h>
#include <girepository.h>
2005-05-09 16:24:46 +02:00
#include "girwriter-private.h"
#include "gitypelib-internal.h"
2005-05-09 16:24:46 +02:00
2010-03-25 13:17:03 +01:00
int
2005-05-09 16:24:46 +02:00
main (int argc, char *argv[])
2010-03-25 13:17:03 +01:00
{
GIRepository *repository = NULL;
gboolean shlib = FALSE;
gchar *output = NULL;
gchar **includedirs = NULL;
gboolean show_all = FALSE;
gchar **input = NULL;
2005-05-09 16:24:46 +02:00
GOptionContext *context;
GError *error = NULL;
gboolean needs_prefix;
gboolean show_version = FALSE;
2005-05-09 16:24:46 +02:00
gint i;
2010-03-25 13:17:03 +01:00
GOptionEntry options[] =
{
{ "shlib", 0, 0, G_OPTION_ARG_NONE, &shlib, "handle typelib embedded in shlib", NULL },
2010-03-25 13:17:03 +01:00
{ "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
{ "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
{ "all", 0, 0, G_OPTION_ARG_NONE, &show_all, "show all available information", NULL, },
{ "version", 0, 0, G_OPTION_ARG_NONE, &show_version, "show program's version number and exit", NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
G_OPTION_ENTRY_NULL
};
2005-05-09 16:24:46 +02:00
g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
setlocale (LC_ALL, "");
2005-05-09 16:24:46 +02:00
context = g_option_context_new ("");
g_option_context_add_main_entries (context, options, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error))
{
g_fprintf (stderr, "failed to parse: %s\n", error->message);
g_error_free (error);
return 1;
}
2005-05-09 16:24:46 +02:00
if (show_version)
{
g_printf ("gi-decompile-typelib %u.%u.%u\n",
GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
return 0;
}
2010-03-25 13:17:03 +01:00
if (!input)
{
g_fprintf (stderr, "no input files\n");
2005-05-09 16:24:46 +02:00
return 1;
}
repository = gi_repository_new ();
if (includedirs != NULL)
for (i = 0; includedirs[i]; i++)
gi_repository_prepend_search_path (repository, includedirs[i]);
2005-05-09 16:24:46 +02:00
for (i = 0; input[i]; i++)
{
const char *namespace;
GMappedFile *mfile = NULL;
GBytes *bytes = NULL;
GITypelib *typelib;
2005-05-09 16:24:46 +02:00
2018-07-29 15:56:35 +02:00
error = NULL;
mfile = g_mapped_file_new (input[i], FALSE, &error);
if (!mfile)
g_error ("failed to read '%s': %s", input[i], error->message);
2005-05-09 16:24:46 +02:00
bytes = g_mapped_file_get_bytes (mfile);
g_clear_pointer (&mfile, g_mapped_file_unref);
2005-05-09 16:24:46 +02:00
if (input[i + 1] && output)
needs_prefix = TRUE;
else
needs_prefix = FALSE;
typelib = gi_typelib_new_from_bytes (bytes, &error);
if (!typelib)
g_error ("failed to create typelib '%s': %s", input[i], error->message);
namespace = gi_repository_load_typelib (repository, typelib, 0, &error);
if (namespace == NULL)
g_error ("failed to load typelib: %s", error->message);
gi_ir_writer_write (repository, output, namespace, needs_prefix, show_all);
2005-05-09 16:24:46 +02:00
/* when writing to stdout, stop after the first module */
if (input[i + 1] && !output)
{
g_fprintf (stderr, "warning, %d modules omitted\n",
g_strv_length (input) - 1);
break;
}
}
2010-03-25 13:17:03 +01:00
g_clear_object (&repository);
2005-05-09 16:24:46 +02:00
return 0;
}