Blame gtk/queryimmodules.c

Packit 98cdb6
/* GTK+
Packit 98cdb6
 * querymodules.c:
Packit 98cdb6
 *
Packit 98cdb6
 * Copyright (C) 2000-2013 Red Hat Software
Packit 98cdb6
 *
Packit 98cdb6
 * This library is free software; you can redistribute it and/or
Packit 98cdb6
 * modify it under the terms of the GNU Library General Public
Packit 98cdb6
 * License as published by the Free Software Foundation; either
Packit 98cdb6
 * version 2 of the License, or (at your option) any later version.
Packit 98cdb6
 *
Packit 98cdb6
 * This library is distributed in the hope that it will be useful,
Packit 98cdb6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 98cdb6
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 98cdb6
 * Library General Public License for more details.
Packit 98cdb6
 *
Packit 98cdb6
 * You should have received a copy of the GNU Library General Public
Packit 98cdb6
 * License along with this library; if not, write to the
Packit 98cdb6
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 98cdb6
 * Boston, MA 02111-1307, USA.
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
#include "config.h"
Packit 98cdb6
Packit 98cdb6
#include <glib.h>
Packit 98cdb6
#include <glib/gprintf.h>
Packit 98cdb6
#include <gmodule.h>
Packit 98cdb6
Packit 98cdb6
#include <errno.h>
Packit 98cdb6
#include <string.h>
Packit 98cdb6
#ifdef HAVE_UNISTD_H
Packit 98cdb6
#include <unistd.h>
Packit 98cdb6
#endif
Packit 98cdb6
Packit 98cdb6
#ifdef USE_LA_MODULES
Packit 98cdb6
#define SOEXT ".la"
Packit 98cdb6
#else
Packit 98cdb6
#define SOEXT ("." G_MODULE_SUFFIX)
Packit 98cdb6
#endif
Packit 98cdb6
Packit 98cdb6
#include "gtk/gtkrc.h"
Packit 98cdb6
#include "gtk/gtkimmodule.h"
Packit 98cdb6
#include "gtk/gtkversion.h"
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
escape_string (GString *contents, const char *str)
Packit 98cdb6
{
Packit 98cdb6
  while (TRUE)
Packit 98cdb6
    {
Packit 98cdb6
      char c = *str++;
Packit 98cdb6
Packit 98cdb6
      switch (c)
Packit 98cdb6
        {
Packit 98cdb6
        case '\0':
Packit 98cdb6
          goto done;
Packit 98cdb6
        case '\n':
Packit 98cdb6
          g_string_append (contents, "\\n");
Packit 98cdb6
          break;
Packit 98cdb6
        case '\"':
Packit 98cdb6
          g_string_append (contents, "\\\"");
Packit 98cdb6
          break;
Packit 98cdb6
#ifdef G_OS_WIN32
Packit 98cdb6
                /* Replace backslashes in path with forward slashes, so that
Packit 98cdb6
                 * it reads in without problems.
Packit 98cdb6
                 */
Packit 98cdb6
        case '\\':
Packit 98cdb6
          g_string_append (contents, "/");
Packit 98cdb6
          break;
Packit 98cdb6
#endif
Packit 98cdb6
        default:
Packit 98cdb6
          g_string_append_c (contents, c);
Packit 98cdb6
        }
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
 done:;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
print_escaped (GString *contents, const char *str)
Packit 98cdb6
{
Packit 98cdb6
  g_string_append_c (contents, '"');
Packit 98cdb6
  escape_string (contents, str);
Packit 98cdb6
  g_string_append_c (contents, '"');
Packit 98cdb6
  g_string_append_c (contents, ' ');
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static gboolean
Packit 98cdb6
query_module (const char *dir, const char *name, GString *contents)
Packit 98cdb6
{
Packit 98cdb6
  void          (*list)   (const GtkIMContextInfo ***contexts,
Packit 98cdb6
                           guint                    *n_contexts);
Packit 98cdb6
  void          (*init)   (GTypeModule              *type_module);
Packit 98cdb6
  void          (*exit)   (void);
Packit 98cdb6
  GtkIMContext *(*create) (const gchar             *context_id);
Packit 98cdb6
Packit 98cdb6
  gpointer list_ptr;
Packit 98cdb6
  gpointer init_ptr;
Packit 98cdb6
  gpointer exit_ptr;
Packit 98cdb6
  gpointer create_ptr;
Packit 98cdb6
Packit 98cdb6
  GModule *module;
Packit 98cdb6
  gchar *path;
Packit 98cdb6
  gboolean error = FALSE;
Packit 98cdb6
Packit 98cdb6
  if (g_path_is_absolute (name))
Packit 98cdb6
    path = g_strdup (name);
Packit 98cdb6
  else
Packit 98cdb6
    path = g_build_filename (dir, name, NULL);
Packit 98cdb6
Packit 98cdb6
  module = g_module_open (path, 0);
Packit 98cdb6
Packit 98cdb6
  if (!module)
Packit 98cdb6
    {
Packit 98cdb6
      g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
Packit 98cdb6
      error = TRUE;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  if (module &&
Packit 98cdb6
      g_module_symbol (module, "im_module_list", &list_ptr) &&
Packit 98cdb6
      g_module_symbol (module, "im_module_init", &init_ptr) &&
Packit 98cdb6
      g_module_symbol (module, "im_module_exit", &exit_ptr) &&
Packit 98cdb6
      g_module_symbol (module, "im_module_create", &create_ptr))
Packit 98cdb6
    {
Packit 98cdb6
      const GtkIMContextInfo **contexts;
Packit 98cdb6
      guint n_contexts;
Packit 98cdb6
      int i;
Packit 98cdb6
Packit 98cdb6
      list = list_ptr;
Packit 98cdb6
      init = init_ptr;
Packit 98cdb6
      exit = exit_ptr;
Packit 98cdb6
      create = create_ptr;
Packit 98cdb6
Packit 98cdb6
      print_escaped (contents, path);
Packit 98cdb6
      g_string_append_c (contents, '\n');
Packit 98cdb6
Packit 98cdb6
      (*list) (&contexts, &n_contexts);
Packit 98cdb6
Packit 98cdb6
      for (i = 0; i < n_contexts; i++)
Packit 98cdb6
        {
Packit 98cdb6
          print_escaped (contents, contexts[i]->context_id);
Packit 98cdb6
          print_escaped (contents, contexts[i]->context_name);
Packit 98cdb6
          print_escaped (contents, contexts[i]->domain);
Packit 98cdb6
          print_escaped (contents, contexts[i]->domain_dirname);
Packit 98cdb6
          print_escaped (contents, contexts[i]->default_locales);
Packit 98cdb6
          g_string_append_c (contents, '\n');
Packit 98cdb6
        }
Packit 98cdb6
      g_string_append_c (contents, '\n');
Packit 98cdb6
    }
Packit 98cdb6
  else
Packit 98cdb6
    {
Packit 98cdb6
      g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
Packit 98cdb6
                 g_module_error ());
Packit 98cdb6
      error = TRUE;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  g_free (path);
Packit 98cdb6
  if (module)
Packit 98cdb6
    g_module_close (module);
Packit 98cdb6
Packit 98cdb6
  return error;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
int main (int argc, char **argv)
Packit 98cdb6
{
Packit 98cdb6
  char *cwd;
Packit 98cdb6
  int i;
Packit 98cdb6
  char *path;
Packit 98cdb6
  gboolean error = FALSE;
Packit 98cdb6
  gchar *cache_file = NULL;
Packit 98cdb6
  gint first_file = 1;
Packit 98cdb6
  GString *contents;
Packit 98cdb6
Packit 98cdb6
  if (argc > 1 && strcmp (argv[1], "--update-cache") == 0)
Packit 98cdb6
    {
Packit 98cdb6
      cache_file = gtk_rc_get_im_module_file ();
Packit 98cdb6
      first_file = 2;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  contents = g_string_new ("");
Packit 98cdb6
  g_string_append_printf (contents,
Packit 98cdb6
                          "# GTK+ Input Method Modules file\n"
Packit 98cdb6
                          "# Automatically generated file, do not edit\n"
Packit 98cdb6
                          "# Created by %s from gtk+-%d.%d.%d\n"
Packit 98cdb6
                          "#\n",
Packit 98cdb6
                          argv[0],
Packit 98cdb6
                          GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
Packit 98cdb6
Packit 98cdb6
  if (argc == first_file)  /* No file arguments given */
Packit 98cdb6
    {
Packit 98cdb6
      char **dirs;
Packit 98cdb6
      int i;
Packit 98cdb6
      GHashTable *dirs_done;
Packit 98cdb6
Packit 98cdb6
      path = gtk_rc_get_im_module_path ();
Packit 98cdb6
Packit 98cdb6
      g_string_append_printf (contents, "# ModulesPath = %s\n#\n", path);
Packit 98cdb6
Packit 98cdb6
      dirs = pango_split_file_list (path);
Packit 98cdb6
      dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
Packit 98cdb6
Packit 98cdb6
      for (i = 0; dirs[i]; i++)
Packit 98cdb6
        if (!g_hash_table_lookup (dirs_done, dirs[i]))
Packit 98cdb6
          {
Packit 98cdb6
            GDir *dir = g_dir_open (dirs[i], 0, NULL);
Packit 98cdb6
            if (dir)
Packit 98cdb6
              {
Packit 98cdb6
                const char *dent;
Packit 98cdb6
Packit 98cdb6
                while ((dent = g_dir_read_name (dir)))
Packit 98cdb6
                  {
Packit 98cdb6
                    if (g_str_has_suffix (dent, SOEXT))
Packit 98cdb6
                      error |= query_module (dirs[i], dent, contents);
Packit 98cdb6
                  }
Packit 98cdb6
Packit 98cdb6
                g_dir_close (dir);
Packit 98cdb6
              }
Packit 98cdb6
Packit 98cdb6
            g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
Packit 98cdb6
          }
Packit 98cdb6
Packit 98cdb6
      g_hash_table_destroy (dirs_done);
Packit 98cdb6
    }
Packit 98cdb6
  else
Packit 98cdb6
    {
Packit 98cdb6
      cwd = g_get_current_dir ();
Packit 98cdb6
Packit 98cdb6
      for (i = first_file; i < argc; i++)
Packit 98cdb6
        error |= query_module (cwd, argv[i], contents);
Packit 98cdb6
Packit 98cdb6
      g_free (cwd);
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  if (!error)
Packit 98cdb6
    {
Packit 98cdb6
      if (cache_file)
Packit 98cdb6
        {
Packit 98cdb6
          GError *err;
Packit 98cdb6
Packit 98cdb6
          err = NULL;
Packit 98cdb6
          if (!g_file_set_contents (cache_file, contents->str, -1, &err))
Packit 98cdb6
            {
Packit 98cdb6
                g_fprintf (stderr, "%s\n", err->message);
Packit 98cdb6
                error = 1;
Packit 98cdb6
            }
Packit 98cdb6
        }
Packit 98cdb6
      else
Packit 98cdb6
        g_print ("%s\n", contents->str);
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  return error ? 1 : 0;
Packit 98cdb6
}