Blame gdk-pixbuf/queryloaders.c

Packit a4058c
/* -*- mode: C; c-file-style: "linux" -*- */
Packit a4058c
/* GdkPixbuf library
Packit a4058c
 * queryloaders.c:
Packit a4058c
 *
Packit a4058c
 * Copyright (C) 2002 The Free Software Foundation
Packit a4058c
 *
Packit a4058c
 * Author: Matthias Clasen <maclas@gmx.de>
Packit a4058c
 *
Packit a4058c
 * This library is free software; you can redistribute it and/or
Packit a4058c
 * modify it under the terms of the GNU Library General Public
Packit a4058c
 * License as published by the Free Software Foundation; either
Packit a4058c
 * version 2 of the License, or (at your option) any later version.
Packit a4058c
 *
Packit a4058c
 * This library is distributed in the hope that it will be useful,
Packit a4058c
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4058c
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a4058c
 * Library General Public License for more details.
Packit a4058c
 *
Packit a4058c
 * You should have received a copy of the GNU Library General Public
Packit a4058c
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit a4058c
 */
Packit a4058c
Packit a4058c
#include "config.h"
Packit a4058c
Packit a4058c
#include <glib.h>
Packit a4058c
#include <glib/gprintf.h>
Packit a4058c
#include <gmodule.h>
Packit a4058c
Packit a4058c
#include <errno.h>
Packit a4058c
#include <string.h>
Packit a4058c
#ifdef HAVE_UNISTD_H
Packit a4058c
#include <unistd.h>
Packit a4058c
#endif
Packit a4058c
Packit a4058c
#include "gdk-pixbuf/gdk-pixbuf.h"
Packit a4058c
#include "gdk-pixbuf/gdk-pixbuf-private.h"
Packit a4058c
Packit a4058c
#ifdef USE_LA_MODULES
Packit a4058c
#define SOEXT ".la"
Packit a4058c
#else
Packit a4058c
#define SOEXT ("." G_MODULE_SUFFIX)
Packit a4058c
#endif
Packit a4058c
#define SOEXT_LEN (strlen (SOEXT))
Packit a4058c
Packit a4058c
#ifdef G_OS_WIN32
Packit a4058c
#include <windows.h>
Packit a4058c
#endif
Packit a4058c
Packit a4058c
static void
Packit a4058c
print_escaped (GString *contents, const char *str)
Packit a4058c
{
Packit a4058c
        gchar *tmp = g_strescape (str, "");
Packit a4058c
        g_string_append_printf (contents, "\"%s\" ", tmp);
Packit a4058c
        g_free (tmp);
Packit a4058c
}
Packit a4058c
Packit a4058c
static int
Packit a4058c
loader_sanity_check (const char *path, GdkPixbufFormat *info, GdkPixbufModule *vtable)
Packit a4058c
{
Packit a4058c
        const GdkPixbufModulePattern *pattern;
Packit a4058c
        const char *error = "";
Packit a4058c
Packit a4058c
        for (pattern = info->signature; pattern->prefix; pattern++)
Packit a4058c
        {
Packit a4058c
                int prefix_len = strlen (pattern->prefix);
Packit a4058c
                if (prefix_len == 0)
Packit a4058c
                {
Packit a4058c
                        error = "empty pattern";
Packit a4058c
Packit a4058c
                        goto error;
Packit a4058c
                }
Packit a4058c
                if (pattern->mask)
Packit a4058c
                {
Packit a4058c
                        int mask_len = strlen (pattern->mask);
Packit a4058c
                        if (mask_len != prefix_len)
Packit a4058c
                        {
Packit a4058c
                                error = "mask length mismatch";
Packit a4058c
Packit a4058c
                                goto error;
Packit a4058c
                        }
Packit a4058c
                        if (strspn (pattern->mask, " !xzn*") < mask_len)
Packit a4058c
                        {
Packit a4058c
                                error = "bad char in mask";
Packit a4058c
Packit a4058c
                                goto error;
Packit a4058c
                        }
Packit a4058c
                }
Packit a4058c
        }
Packit a4058c
Packit a4058c
        if (!vtable->load && !vtable->begin_load && !vtable->load_animation)
Packit a4058c
        {
Packit a4058c
                error = "no load method implemented";
Packit a4058c
Packit a4058c
                goto error;
Packit a4058c
        }
Packit a4058c
Packit a4058c
        if (vtable->begin_load && (!vtable->stop_load || !vtable->load_increment))
Packit a4058c
        {
Packit a4058c
                error = "incremental loading support incomplete";
Packit a4058c
Packit a4058c
                goto error;
Packit a4058c
        }
Packit a4058c
Packit a4058c
        if ((info->flags & GDK_PIXBUF_FORMAT_WRITABLE) && !(vtable->save || vtable->save_to_callback))
Packit a4058c
        {
Packit a4058c
                error = "loader claims to support saving but doesn't implement save";
Packit a4058c
                goto error;
Packit a4058c
        }
Packit a4058c
Packit a4058c
        return 1;
Packit a4058c
Packit a4058c
 error:
Packit a4058c
        g_fprintf (stderr, "Loader sanity check failed for %s: %s\n",
Packit a4058c
                   path, error);
Packit a4058c
Packit a4058c
        return 0;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
write_loader_info (GString *contents, const char *path, GdkPixbufFormat *info)
Packit a4058c
{
Packit a4058c
        const GdkPixbufModulePattern *pattern;
Packit a4058c
        char **mime;
Packit a4058c
        char **ext;
Packit a4058c
Packit a4058c
        g_string_append_printf (contents, "\"%s\"\n", path);
Packit a4058c
        g_string_append_printf (contents, "\"%s\" %u \"%s\" \"%s\" \"%s\"\n",
Packit a4058c
                  info->name,
Packit a4058c
                  info->flags,
Packit a4058c
                  info->domain ? info->domain : GETTEXT_PACKAGE,
Packit a4058c
                  info->description,
Packit a4058c
                  info->license ? info->license : "");
Packit a4058c
        for (mime = info->mime_types; *mime; mime++) {
Packit a4058c
                g_string_append_printf (contents, "\"%s\" ", *mime);
Packit a4058c
        }
Packit a4058c
        g_string_append (contents, "\"\"\n");
Packit a4058c
        for (ext = info->extensions; *ext; ext++) {
Packit a4058c
                g_string_append_printf (contents, "\"%s\" ", *ext);
Packit a4058c
        }
Packit a4058c
        g_string_append (contents, "\"\"\n");
Packit a4058c
        for (pattern = info->signature; pattern->prefix; pattern++) {
Packit a4058c
                print_escaped (contents, pattern->prefix);
Packit a4058c
                print_escaped (contents, pattern->mask ? (const char *)pattern->mask : "");
Packit a4058c
                g_string_append_printf (contents, "%d\n", pattern->relevance);
Packit a4058c
        }
Packit a4058c
        g_string_append_c (contents, '\n');
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
query_module (GString *contents, const char *dir, const char *file)
Packit a4058c
{
Packit a4058c
        char *path;
Packit a4058c
        GModule *module;
Packit a4058c
        void                    (*fill_info)     (GdkPixbufFormat *info);
Packit a4058c
        void                    (*fill_vtable)   (GdkPixbufModule *module);
Packit a4058c
        gpointer fill_info_ptr;
Packit a4058c
        gpointer fill_vtable_ptr;
Packit a4058c
Packit a4058c
        if (g_path_is_absolute (file))
Packit a4058c
                path = g_strdup (file);
Packit a4058c
        else
Packit a4058c
                path = g_build_filename (dir, file, NULL);
Packit a4058c
Packit a4058c
        module = g_module_open (path, 0);
Packit a4058c
        if (module &&
Packit a4058c
            g_module_symbol (module, "fill_info", &fill_info_ptr) &&
Packit a4058c
            g_module_symbol (module, "fill_vtable", &fill_vtable_ptr)) {
Packit a4058c
                GdkPixbufFormat *info;
Packit a4058c
                GdkPixbufModule *vtable;
Packit a4058c
Packit a4058c
#ifdef G_OS_WIN32
Packit a4058c
                /* Replace backslashes in path with forward slashes, so that
Packit a4058c
                 * it reads in without problems.
Packit a4058c
                 */
Packit a4058c
                {
Packit a4058c
                        char *p = path;
Packit a4058c
                        while (*p) {
Packit a4058c
                                if (*p == '\\')
Packit a4058c
                                        *p = '/';
Packit a4058c
                                p++;
Packit a4058c
                        }
Packit a4058c
                }
Packit a4058c
#endif
Packit a4058c
                info = g_new0 (GdkPixbufFormat, 1);
Packit a4058c
                vtable = g_new0 (GdkPixbufModule, 1);
Packit a4058c
Packit a4058c
                vtable->module = module;
Packit a4058c
Packit a4058c
                fill_info = fill_info_ptr;
Packit a4058c
                fill_vtable = fill_vtable_ptr;
Packit a4058c
Packit a4058c
                (*fill_info) (info);
Packit a4058c
                (*fill_vtable) (vtable);
Packit a4058c
Packit a4058c
                if (loader_sanity_check (path, info, vtable))
Packit a4058c
                        write_loader_info (contents, path, info);
Packit a4058c
Packit a4058c
                g_free (info);
Packit a4058c
                g_free (vtable);
Packit a4058c
        }
Packit a4058c
        else {
Packit a4058c
                if (module == NULL)
Packit a4058c
                        g_fprintf (stderr, "g_module_open() failed for %s: %s\n", path,
Packit a4058c
                                   g_module_error());
Packit a4058c
                else
Packit a4058c
                        g_fprintf (stderr, "Cannot load loader %s\n", path);
Packit a4058c
        }
Packit a4058c
        if (module)
Packit a4058c
                g_module_close (module);
Packit a4058c
        g_free (path);
Packit a4058c
}
Packit a4058c
Packit a4058c
#ifdef G_OS_WIN32
Packit a4058c
Packit a4058c
static char *
Packit a4058c
get_toplevel (void)
Packit a4058c
{
Packit a4058c
  static char *toplevel = NULL;
Packit a4058c
Packit a4058c
  if (toplevel == NULL)
Packit a4058c
          toplevel = g_win32_get_package_installation_directory_of_module (NULL);
Packit a4058c
Packit a4058c
  return toplevel;
Packit a4058c
}
Packit a4058c
Packit a4058c
static char *
Packit a4058c
get_libdir (void)
Packit a4058c
{
Packit a4058c
  static char *libdir = NULL;
Packit a4058c
Packit a4058c
  if (libdir == NULL)
Packit a4058c
          libdir = g_build_filename (get_toplevel (), "lib", NULL);
Packit a4058c
Packit a4058c
  return libdir;
Packit a4058c
}
Packit a4058c
Packit a4058c
#undef GDK_PIXBUF_LIBDIR
Packit a4058c
#define GDK_PIXBUF_LIBDIR get_libdir()
Packit a4058c
Packit a4058c
#endif
Packit a4058c
Packit a4058c
static gchar *
Packit a4058c
gdk_pixbuf_get_module_file (void)
Packit a4058c
{
Packit a4058c
        gchar *result = g_strdup (g_getenv ("GDK_PIXBUF_MODULE_FILE"));
Packit a4058c
Packit a4058c
        if (!result)
Packit a4058c
                result = g_build_filename (GDK_PIXBUF_LIBDIR, "gdk-pixbuf-2.0", GDK_PIXBUF_BINARY_VERSION, "loaders.cache", NULL);
Packit a4058c
Packit a4058c
        return result;
Packit a4058c
}
Packit a4058c
Packit a4058c
int main (int argc, char **argv)
Packit a4058c
{
Packit a4058c
        gint i;
Packit a4058c
        const gchar *prgname;
Packit a4058c
        GString *contents;
Packit a4058c
        gchar *cache_file = NULL;
Packit a4058c
        gint first_file = 1;
Packit a4058c
Packit a4058c
#ifdef G_OS_WIN32
Packit a4058c
        gchar *libdir;
Packit a4058c
        gchar *runtime_prefix;
Packit a4058c
        gchar *slash;
Packit a4058c
Packit a4058c
        if (g_ascii_strncasecmp (PIXBUF_LIBDIR, GDK_PIXBUF_PREFIX, strlen (GDK_PIXBUF_PREFIX)) == 0 &&
Packit a4058c
            G_IS_DIR_SEPARATOR (PIXBUF_LIBDIR[strlen (GDK_PIXBUF_PREFIX)])) {
Packit a4058c
                /* GDK_PIXBUF_PREFIX is a prefix of PIXBUF_LIBDIR, as it
Packit a4058c
                 * normally is. Replace that prefix in PIXBUF_LIBDIR
Packit a4058c
                 * with the installation directory on this machine.
Packit a4058c
                 * We assume this invokation of
Packit a4058c
                 * gdk-pixbuf-query-loaders is run from either a "bin"
Packit a4058c
                 * subdirectory of the installation directory, or in
Packit a4058c
                 * the installation directory itself.
Packit a4058c
                 */
Packit a4058c
                wchar_t fn[1000];
Packit a4058c
                GetModuleFileNameW (NULL, fn, G_N_ELEMENTS (fn));
Packit a4058c
                runtime_prefix = g_utf16_to_utf8 (fn, -1, NULL, NULL, NULL);
Packit a4058c
                slash = strrchr (runtime_prefix, '\\');
Packit a4058c
                *slash = '\0';
Packit a4058c
                slash = strrchr (runtime_prefix, '\\');
Packit a4058c
                /* If running from some weird location, or from the
Packit a4058c
                 * build directory (either in the .libs folder where
Packit a4058c
                 * libtool places the real executable when using a
Packit a4058c
                 * wrapper, or directly from the gdk-pixbuf folder),
Packit a4058c
                 * use the compile-time libdir.
Packit a4058c
                 */
Packit a4058c
                if (slash == NULL ||
Packit a4058c
                    g_ascii_strcasecmp (slash + 1, ".libs") == 0 ||
Packit a4058c
                    g_ascii_strcasecmp (slash + 1, "gdk-pixbuf") == 0) {
Packit a4058c
                        libdir = PIXBUF_LIBDIR;
Packit a4058c
                }
Packit a4058c
                else {
Packit a4058c
                        if (slash != NULL && g_ascii_strcasecmp (slash + 1, "bin") == 0) {
Packit a4058c
                                *slash = '\0';
Packit a4058c
                        }
Packit a4058c
Packit a4058c
                        libdir = g_strconcat (runtime_prefix,
Packit a4058c
                                              "/",
Packit a4058c
                                              PIXBUF_LIBDIR + strlen (GDK_PIXBUF_PREFIX) + 1,
Packit a4058c
                                              NULL);
Packit a4058c
                }
Packit a4058c
        }
Packit a4058c
        else {
Packit a4058c
                libdir = PIXBUF_LIBDIR;
Packit a4058c
        }
Packit a4058c
Packit a4058c
#undef PIXBUF_LIBDIR
Packit a4058c
#define PIXBUF_LIBDIR libdir
Packit a4058c
Packit a4058c
#endif
Packit a4058c
Packit a4058c
	/* This call is necessary to ensure we actually link against libgobject;
Packit a4058c
	 * otherwise it may be stripped if -Wl,--as-needed is in use.
Packit a4058c
	 * 
Packit a4058c
	 * The reason we need to link against libgobject is because it now has
Packit a4058c
	 * a global constructor.  If the dynamically loaded modules happen
Packit a4058c
	 * to dlclose() libgobject, then reopen it again, we're in for trouble.
Packit a4058c
	 *
Packit a4058c
	 * See: https://bugzilla.gnome.org/show_bug.cgi?id=686822
Packit a4058c
	 */
Packit a4058c
	g_type_ensure (G_TYPE_OBJECT);
Packit a4058c
Packit a4058c
        if (argc > 1 && strcmp (argv[1], "--update-cache") == 0) {
Packit a4058c
                cache_file = gdk_pixbuf_get_module_file ();
Packit a4058c
                first_file = 2;
Packit a4058c
        }
Packit a4058c
Packit a4058c
        contents = g_string_new ("");
Packit a4058c
Packit a4058c
        prgname = g_get_prgname ();
Packit a4058c
        g_string_append_printf (contents,
Packit a4058c
                                "# GdkPixbuf Image Loader Modules file\n"
Packit a4058c
                                "# Automatically generated file, do not edit\n"
Packit a4058c
                                "# Created by %s from gdk-pixbuf-%s\n"
Packit a4058c
                                "#\n",
Packit a4058c
                                (prgname ? prgname : "gdk-pixbuf-query-loaders"),
Packit a4058c
                                GDK_PIXBUF_VERSION);
Packit a4058c
Packit a4058c
        if (argc == first_file) {
Packit a4058c
#ifdef USE_GMODULE
Packit a4058c
                const char *path;
Packit a4058c
                GDir *dir;
Packit a4058c
                GList *l, *modules;
Packit a4058c
Packit a4058c
                path = g_getenv ("GDK_PIXBUF_MODULEDIR");
Packit a4058c
#ifdef G_OS_WIN32
Packit a4058c
                if (path != NULL && *path != '\0')
Packit a4058c
                        path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
Packit a4058c
#endif
Packit a4058c
                if (path == NULL || *path == '\0')
Packit a4058c
                        path = PIXBUF_LIBDIR;
Packit a4058c
Packit a4058c
                g_string_append_printf (contents, "# LoaderDir = %s\n#\n", path);
Packit a4058c
Packit a4058c
                modules = NULL;
Packit a4058c
                dir = g_dir_open (path, 0, NULL);
Packit a4058c
                if (dir) {
Packit a4058c
                        const char *dent;
Packit a4058c
Packit a4058c
                        while ((dent = g_dir_read_name (dir))) {
Packit a4058c
                                gint len = strlen (dent);
Packit a4058c
                                if (len > SOEXT_LEN &&
Packit a4058c
                                    strcmp (dent + len - SOEXT_LEN, SOEXT) == 0) {
Packit a4058c
                                        modules = g_list_prepend (modules,
Packit a4058c
                                                                  g_strdup (dent));
Packit a4058c
                                }
Packit a4058c
                        }
Packit a4058c
                        g_dir_close (dir);
Packit a4058c
                }
Packit a4058c
                modules = g_list_sort (modules, (GCompareFunc)strcmp);
Packit a4058c
                for (l = modules; l != NULL; l = l->next)
Packit a4058c
                        query_module (contents, path, l->data);
Packit a4058c
                g_list_free_full (modules, g_free);
Packit a4058c
#else
Packit a4058c
                g_string_append_printf (contents, "# dynamic loading of modules not supported\n");
Packit a4058c
#endif
Packit a4058c
        }
Packit a4058c
        else {
Packit a4058c
                char *cwd = g_get_current_dir ();
Packit a4058c
Packit a4058c
                for (i = first_file; i < argc; i++) {
Packit a4058c
                        char *infilename = argv[i];
Packit a4058c
#ifdef G_OS_WIN32
Packit a4058c
                        infilename = g_locale_to_utf8 (infilename,
Packit a4058c
                                                       -1, NULL, NULL, NULL);
Packit a4058c
#endif
Packit a4058c
                        query_module (contents, cwd, infilename);
Packit a4058c
                }
Packit a4058c
                g_free (cwd);
Packit a4058c
        }
Packit a4058c
Packit a4058c
        if (cache_file) {
Packit a4058c
                GError *err;
Packit a4058c
Packit a4058c
                err = NULL;
Packit a4058c
                if (!g_file_set_contents (cache_file, contents->str, -1, &err)) {
Packit a4058c
                        g_fprintf (stderr, "%s\n", err->message);
Packit a4058c
                }
Packit a4058c
        }
Packit a4058c
        else
Packit a4058c
                g_print ("%s\n", contents->str);
Packit a4058c
Packit a4058c
        return 0;
Packit a4058c
}