queryimmodules.c 6.45 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
#include "gtk/gtkutilsprivate.h"
40

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

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

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

73
 done:;
74 75
}

Hans Breuer's avatar
Hans Breuer committed
76
static void
77
print_escaped (GString *contents, const char *str)
Owen Taylor's avatar
Owen Taylor committed
78
{
79 80 81 82
  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
83 84
}

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

91 92 93 94 95
  gpointer list_ptr;
  gpointer init_ptr;
  gpointer exit_ptr;
  gpointer create_ptr;

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

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

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

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

Owen Taylor's avatar
Owen Taylor committed
113
  if (module &&
114 115 116 117
      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
118 119 120 121 122
    {
      const GtkIMContextInfo **contexts;
      guint n_contexts;
      int i;

123 124
      list = list_ptr;

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

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

130
      for (i = 0; i < n_contexts; i++)
131
        {
132 133 134 135 136 137
          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');
138
        }
139
      g_string_append_c (contents, '\n');
Owen Taylor's avatar
Owen Taylor committed
140 141 142
    }
  else
    {
143
      g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
144
                 g_module_error ());
Owen Taylor's avatar
Owen Taylor committed
145 146 147 148 149 150 151 152
      error = TRUE;
    }

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

  return error;
153
}
Owen Taylor's avatar
Owen Taylor committed
154 155 156

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

165 166
  if (argc > 1 && strcmp (argv[1], "--update-cache") == 0)
    {
167
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
168
      cache_file = gtk_rc_get_im_module_file ();
169
G_GNUC_END_IGNORE_DEPRECATIONS
170 171
      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
    {
      char **dirs;
185
      GHashTable *dirs_done;
Owen Taylor's avatar
Owen Taylor committed
186

187
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Owen Taylor's avatar
Owen Taylor committed
188
      path = gtk_rc_get_im_module_path ();
189
G_GNUC_END_IGNORE_DEPRECATIONS
Owen Taylor's avatar
Owen Taylor committed
190

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

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

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

                while ((dent = g_dir_read_name (dir)))
206 207 208 209
                  list = g_list_prepend (list, g_strdup (dent));

                list = g_list_sort (list, (GCompareFunc) strcmp);
                for (iterator = list; iterator; iterator = iterator->next)
210
                  {
211 212
                    if (g_str_has_suffix (iterator->data, SOEXT))
                      error |= query_module (dirs[i], iterator->data, contents);
213 214
                  }

215
                g_list_free_full (list, g_free);
216 217
                g_dir_close (dir);
              }
218 219 220 221 222

            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
223 224 225
    }
  else
    {
226
      cwd = g_get_current_dir ();
227

228 229
      for (i = first_file; i < argc; i++)
        error |= query_module (cwd, argv[i], contents);
230 231

      g_free (cwd);
Owen Taylor's avatar
Owen Taylor committed
232
    }
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  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
251 252
  return error ? 1 : 0;
}