File: queryimmodules.c

package info (click to toggle)
gtk+2.0 2.24.32-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 128,080 kB
  • sloc: ansic: 574,621; makefile: 5,197; sh: 4,500; xml: 1,193; python: 1,119; perl: 749; awk: 72; cpp: 34
file content (256 lines) | stat: -rw-r--r-- 6,773 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* GTK+
 * querymodules.c:
 *
 * Copyright (C) 2000-2013 Red Hat Software
 *
 * 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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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 "config.h"

#include <glib.h>
#include <glib/gprintf.h>
#include <gmodule.h>

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

#ifdef USE_LA_MODULES
#define SOEXT ".la"
#else
#define SOEXT ("." G_MODULE_SUFFIX)
#endif

#include "gtk/gtkrc.h"
#include "gtk/gtkimmodule.h"
#include "gtk/gtkversion.h"

static void
escape_string (GString *contents, const char *str)
{
  while (TRUE)
    {
      char c = *str++;

      switch (c)
        {
        case '\0':
          goto done;
        case '\n':
          g_string_append (contents, "\\n");
          break;
        case '\"':
          g_string_append (contents, "\\\"");
          break;
#ifdef G_OS_WIN32
                /* Replace backslashes in path with forward slashes, so that
                 * it reads in without problems.
                 */
        case '\\':
          g_string_append (contents, "/");
          break;
#endif
        default:
          g_string_append_c (contents, c);
        }
    }

 done:;
}

static void
print_escaped (GString *contents, const char *str)
{
  g_string_append_c (contents, '"');
  escape_string (contents, str);
  g_string_append_c (contents, '"');
  g_string_append_c (contents, ' ');
}

static gboolean
query_module (const char *dir, const char *name, GString *contents)
{
  void          (*list)   (const GtkIMContextInfo ***contexts,
                           guint                    *n_contexts);
  void          (*init)   (GTypeModule              *type_module);
  void          (*exit)   (void);
  GtkIMContext *(*create) (const gchar             *context_id);

  gpointer list_ptr;
  gpointer init_ptr;
  gpointer exit_ptr;
  gpointer create_ptr;

  GModule *module;
  gchar *path;
  gboolean error = FALSE;

  if (g_path_is_absolute (name))
    path = g_strdup (name);
  else
    path = g_build_filename (dir, name, NULL);

  module = g_module_open (path, 0);

  if (!module)
    {
      g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
      error = TRUE;
    }

  if (module &&
      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))
    {
      const GtkIMContextInfo **contexts;
      guint n_contexts;
      int i;

      list = list_ptr;
      init = init_ptr;
      exit = exit_ptr;
      create = create_ptr;

      print_escaped (contents, path);
      g_string_append_c (contents, '\n');

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

      for (i = 0; i < n_contexts; i++)
        {
          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');
        }
      g_string_append_c (contents, '\n');
    }
  else
    {
      g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
                 g_module_error ());
      error = TRUE;
    }

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

  return error;
}

int main (int argc, char **argv)
{
  char *cwd;
  int i;
  char *path;
  gboolean error = FALSE;
  gchar *cache_file = NULL;
  gint first_file = 1;
  GString *contents;

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

  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);

  if (argc == first_file)  /* No file arguments given */
    {
      char **dirs;
      int i;
      GHashTable *dirs_done;

      path = gtk_rc_get_im_module_path ();

      g_string_append_printf (contents, "# ModulesPath = %s\n#\n", path);

      dirs = pango_split_file_list (path);
      dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);

      for (i = 0; dirs[i]; i++)
        if (!g_hash_table_lookup (dirs_done, dirs[i]))
          {
            GDir *dir = g_dir_open (dirs[i], 0, NULL);
            if (dir)
              {
                const char *dent;
                GList *list = NULL, *iterator = NULL;

                while ((dent = g_dir_read_name (dir)))
                  list = g_list_prepend (list, g_strdup (dent));

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

                g_list_free_full (list, g_free);
                g_dir_close (dir);
              }

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

      g_hash_table_destroy (dirs_done);
    }
  else
    {
      cwd = g_get_current_dir ();

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

      g_free (cwd);
    }

  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);
    }

  return error ? 1 : 0;
}