queryimmodules.c 6.05 KB
Newer Older
Owen Taylor's avatar
Owen Taylor committed
1 2 3
/* GTK+
 * querymodules.c:
 *
4
 * Copyright (C) 2000-2010 Red Hat Software
Owen Taylor's avatar
Owen Taylor committed
5 6 7 8 9 10 11 12
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Owen Taylor's avatar
Owen Taylor committed
14 15 16
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Owen Taylor's avatar
Owen Taylor committed
18 19
 */

20
#include "config.h"
Owen Taylor's avatar
Owen Taylor committed
21 22

#include <glib.h>
23
#include <glib/gprintf.h>
Owen Taylor's avatar
Owen Taylor committed
24 25 26 27 28 29 30

#include <errno.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

31
#ifdef USE_LA_MODULES
32
#define SOEXT ".la"
Owen Taylor's avatar
Owen Taylor committed
33
#else
34
#define SOEXT ("." G_MODULE_SUFFIX)
Owen Taylor's avatar
Owen Taylor committed
35 36
#endif

37
#include "gtk/gtkimcontextinfo.h"
38
#include "gtk/gtkversion.h"
39

40
#define GDK_DISABLE_DEPRECATION_WARNINGS
41 42
#undef GTK_DISABLE_DEPRECATED

Benjamin Otte's avatar
Benjamin Otte committed
43
#include "gtk/deprecated/gtkrc.h"
Owen Taylor's avatar
Owen Taylor committed
44

45 46
static void
escape_string (GString *contents, const char *str)
47 48 49 50
{
  while (TRUE)
    {
      char c = *str++;
51

52
      switch (c)
53 54 55 56
        {
        case '\0':
          goto done;
        case '\n':
57
          g_string_append (contents, "\\n");
58 59
          break;
        case '\"':
60
          g_string_append (contents, "\\\"");
61
          break;
62
#ifdef G_OS_WIN32
63 64 65 66
                /* Replace backslashes in path with forward slashes, so that
                 * it reads in without problems.
                 */
        case '\\':
67
          g_string_append (contents, "/");
68 69 70
          break;
#endif
        default:
71
          g_string_append_c (contents, c);
72
        }
73 74
    }

75
 done:;
76 77
}

Hans Breuer's avatar
Hans Breuer committed
78
static void
79
print_escaped (GString *contents, const char *str)
Owen Taylor's avatar
Owen Taylor committed
80
{
81 82 83 84
  g_string_append_c (contents, '"');
  escape_string (contents, str);
  g_string_append_c (contents, '"');
  g_string_append_c (contents, ' ');
Owen Taylor's avatar
Owen Taylor committed
85 86
}

Hans Breuer's avatar
Hans Breuer committed
87
static gboolean
88
query_module (const char *dir, const char *name, GString *contents)
Owen Taylor's avatar
Owen Taylor committed
89 90
{
  void          (*list)   (const GtkIMContextInfo ***contexts,
91
                           guint                    *n_contexts);
Owen Taylor's avatar
Owen Taylor committed
92

93 94 95 96 97
  gpointer list_ptr;
  gpointer init_ptr;
  gpointer exit_ptr;
  gpointer create_ptr;

Owen Taylor's avatar
Owen Taylor committed
98 99 100 101
  GModule *module;
  gchar *path;
  gboolean error = FALSE;

102
  if (g_path_is_absolute (name))
Owen Taylor's avatar
Owen Taylor committed
103 104
    path = g_strdup (name);
  else
105
    path = g_build_filename (dir, name, NULL);
106

Owen Taylor's avatar
Owen Taylor committed
107 108 109 110
  module = g_module_open (path, 0);

  if (!module)
    {
111
      g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
Owen Taylor's avatar
Owen Taylor committed
112 113
      error = TRUE;
    }
114

Owen Taylor's avatar
Owen Taylor committed
115
  if (module &&
116 117 118 119
      g_module_symbol (module, "im_module_list", &list_ptr) &&
      g_module_symbol (module, "im_module_init", &init_ptr) &&
      g_module_symbol (module, "im_module_exit", &exit_ptr) &&
      g_module_symbol (module, "im_module_create", &create_ptr))
Owen Taylor's avatar
Owen Taylor committed
120 121 122 123 124
    {
      const GtkIMContextInfo **contexts;
      guint n_contexts;
      int i;

125 126
      list = list_ptr;

127 128
      print_escaped (contents, path);
      g_string_append_c (contents, '\n');
Owen Taylor's avatar
Owen Taylor committed
129 130 131

      (*list) (&contexts, &n_contexts);

132
      for (i = 0; i < n_contexts; i++)
133
        {
134 135 136 137 138 139
          print_escaped (contents, contexts[i]->context_id);
          print_escaped (contents, contexts[i]->context_name);
          print_escaped (contents, contexts[i]->domain);
          print_escaped (contents, contexts[i]->domain_dirname);
          print_escaped (contents, contexts[i]->default_locales);
          g_string_append_c (contents, '\n');
140
        }
141
      g_string_append_c (contents, '\n');
Owen Taylor's avatar
Owen Taylor committed
142 143 144
    }
  else
    {
145
      g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
146
                 g_module_error ());
Owen Taylor's avatar
Owen Taylor committed
147 148 149 150 151 152 153 154
      error = TRUE;
    }

  g_free (path);
  if (module)
    g_module_close (module);

  return error;
155
}
Owen Taylor's avatar
Owen Taylor committed
156 157 158

int main (int argc, char **argv)
{
159
  char *cwd;
Owen Taylor's avatar
Owen Taylor committed
160 161 162
  int i;
  char *path;
  gboolean error = FALSE;
163 164 165
  gchar *cache_file = NULL;
  gint first_file = 1;
  GString *contents;
Owen Taylor's avatar
Owen Taylor committed
166

167 168 169 170 171
  if (argc > 1 && strcmp (argv[1], "--update-cache") == 0)
    {
      cache_file = gtk_rc_get_im_module_file ();
      first_file = 2;
    }
172

173 174 175 176 177 178 179 180
  contents = g_string_new ("");
  g_string_append_printf (contents,
                          "# GTK+ Input Method Modules file\n"
                          "# Automatically generated file, do not edit\n"
                          "# Created by %s from gtk+-%d.%d.%d\n"
                          "#\n",
                          argv[0],
                          GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
Owen Taylor's avatar
Owen Taylor committed
181

182
  if (argc == first_file)  /* No file arguments given */
Owen Taylor's avatar
Owen Taylor committed
183 184 185
    {
      char **dirs;
      int i;
186
      GHashTable *dirs_done;
Owen Taylor's avatar
Owen Taylor committed
187 188 189

      path = gtk_rc_get_im_module_path ();

190
      g_string_append_printf (contents, "# ModulesPath = %s\n#\n", path);
Owen Taylor's avatar
Owen Taylor committed
191 192

      dirs = pango_split_file_list (path);
193
      dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
Owen Taylor's avatar
Owen Taylor committed
194

195
      for (i = 0; dirs[i]; i++)
196 197
        if (!g_hash_table_lookup (dirs_done, dirs[i]))
          {
198 199 200 201 202 203 204 205
            GDir *dir = g_dir_open (dirs[i], 0, NULL);
            if (dir)
              {
                const char *dent;

                while ((dent = g_dir_read_name (dir)))
                  {
                    if (g_str_has_suffix (dent, SOEXT))
206
                      error |= query_module (dirs[i], dent, contents);
207 208 209 210
                  }

                g_dir_close (dir);
              }
211 212 213 214 215

            g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
          }

      g_hash_table_destroy (dirs_done);
Owen Taylor's avatar
Owen Taylor committed
216 217 218
    }
  else
    {
219
      cwd = g_get_current_dir ();
220

221 222
      for (i = first_file; i < argc; i++)
        error |= query_module (cwd, argv[i], contents);
223 224

      g_free (cwd);
Owen Taylor's avatar
Owen Taylor committed
225
    }
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  if (!error)
    {
      if (cache_file)
        {
          GError *err;

          err = NULL;
          if (!g_file_set_contents (cache_file, contents->str, -1, &err))
            {
                g_fprintf (stderr, "%s\n", err->message);
                error = 1;
            }
        }
      else
        g_print ("%s\n", contents->str);
    }

Owen Taylor's avatar
Owen Taylor committed
244 245
  return error ? 1 : 0;
}