]> Pileus Git - ~andy/gtk/blob - gtk/queryimmodules.c
2ff3f91d20a6d652fb1c4543e321895d4d55d9d1
[~andy/gtk] / gtk / queryimmodules.c
1 /* GTK+
2  * querymodules.c:
3  *
4  * Copyright (C) 2000-2010 Red Hat Software
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include <glib.h>
25 #include <glib/gprintf.h>
26
27 #include <errno.h>
28 #include <string.h>
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #ifdef USE_LA_MODULES
34 #define SOEXT ".la"
35 #else
36 #define SOEXT ("." G_MODULE_SUFFIX)
37 #endif
38
39 #include "gtk/gtkimcontextinfo.h"
40 #include "gtk/gtkversion.h"
41
42 #define GDK_DISABLE_DEPRECATION_WARNINGS
43 #undef GTK_DISABLE_DEPRECATED
44
45 #include "gtk/deprecated/gtkrc.h"
46
47 static void
48 escape_string (GString *contents, const char *str)
49 {
50   while (TRUE)
51     {
52       char c = *str++;
53
54       switch (c)
55         {
56         case '\0':
57           goto done;
58         case '\n':
59           g_string_append (contents, "\\n");
60           break;
61         case '\"':
62           g_string_append (contents, "\\\"");
63           break;
64 #ifdef G_OS_WIN32
65                 /* Replace backslashes in path with forward slashes, so that
66                  * it reads in without problems.
67                  */
68         case '\\':
69           g_string_append (contents, "/");
70           break;
71 #endif
72         default:
73           g_string_append_c (contents, c);
74         }
75     }
76
77  done:;
78 }
79
80 static void
81 print_escaped (GString *contents, const char *str)
82 {
83   g_string_append_c (contents, '"');
84   escape_string (contents, str);
85   g_string_append_c (contents, '"');
86   g_string_append_c (contents, ' ');
87 }
88
89 static gboolean
90 query_module (const char *dir, const char *name, GString *contents)
91 {
92   void          (*list)   (const GtkIMContextInfo ***contexts,
93                            guint                    *n_contexts);
94
95   gpointer list_ptr;
96   gpointer init_ptr;
97   gpointer exit_ptr;
98   gpointer create_ptr;
99
100   GModule *module;
101   gchar *path;
102   gboolean error = FALSE;
103
104   if (g_path_is_absolute (name))
105     path = g_strdup (name);
106   else
107     path = g_build_filename (dir, name, NULL);
108
109   module = g_module_open (path, 0);
110
111   if (!module)
112     {
113       g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
114       error = TRUE;
115     }
116
117   if (module &&
118       g_module_symbol (module, "im_module_list", &list_ptr) &&
119       g_module_symbol (module, "im_module_init", &init_ptr) &&
120       g_module_symbol (module, "im_module_exit", &exit_ptr) &&
121       g_module_symbol (module, "im_module_create", &create_ptr))
122     {
123       const GtkIMContextInfo **contexts;
124       guint n_contexts;
125       int i;
126
127       list = list_ptr;
128
129       print_escaped (contents, path);
130       g_string_append_c (contents, '\n');
131
132       (*list) (&contexts, &n_contexts);
133
134       for (i = 0; i < n_contexts; i++)
135         {
136           print_escaped (contents, contexts[i]->context_id);
137           print_escaped (contents, contexts[i]->context_name);
138           print_escaped (contents, contexts[i]->domain);
139           print_escaped (contents, contexts[i]->domain_dirname);
140           print_escaped (contents, contexts[i]->default_locales);
141           g_string_append_c (contents, '\n');
142         }
143       g_string_append_c (contents, '\n');
144     }
145   else
146     {
147       g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
148                  g_module_error ());
149       error = TRUE;
150     }
151
152   g_free (path);
153   if (module)
154     g_module_close (module);
155
156   return error;
157 }
158
159 int main (int argc, char **argv)
160 {
161   char *cwd;
162   int i;
163   char *path;
164   gboolean error = FALSE;
165   gchar *cache_file = NULL;
166   gint first_file = 1;
167   GString *contents;
168
169   if (argc > 1 && strcmp (argv[1], "--update-cache") == 0)
170     {
171       cache_file = gtk_rc_get_im_module_file ();
172       first_file = 2;
173     }
174
175   contents = g_string_new ("");
176   g_string_append_printf (contents,
177                           "# GTK+ Input Method Modules file\n"
178                           "# Automatically generated file, do not edit\n"
179                           "# Created by %s from gtk+-%d.%d.%d\n"
180                           "#\n",
181                           argv[0],
182                           GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
183
184   if (argc == first_file)  /* No file arguments given */
185     {
186       char **dirs;
187       int i;
188       GHashTable *dirs_done;
189
190       path = gtk_rc_get_im_module_path ();
191
192       g_string_append_printf (contents, "# ModulesPath = %s\n#\n", path);
193
194       dirs = pango_split_file_list (path);
195       dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
196
197       for (i = 0; dirs[i]; i++)
198         if (!g_hash_table_lookup (dirs_done, dirs[i]))
199           {
200             GDir *dir = g_dir_open (dirs[i], 0, NULL);
201             if (dir)
202               {
203                 const char *dent;
204
205                 while ((dent = g_dir_read_name (dir)))
206                   {
207                     if (g_str_has_suffix (dent, SOEXT))
208                       error |= query_module (dirs[i], dent, contents);
209                   }
210
211                 g_dir_close (dir);
212               }
213
214             g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
215           }
216
217       g_hash_table_destroy (dirs_done);
218     }
219   else
220     {
221       cwd = g_get_current_dir ();
222
223       for (i = first_file; i < argc; i++)
224         error |= query_module (cwd, argv[i], contents);
225
226       g_free (cwd);
227     }
228
229   if (!error)
230     {
231       if (cache_file)
232         {
233           GError *err;
234
235           err = NULL;
236           if (!g_file_set_contents (cache_file, contents->str, -1, &err))
237             {
238                 g_fprintf (stderr, "%s\n", err->message);
239                 error = 1;
240             }
241         }
242       else
243         g_print ("%s\n", contents->str);
244     }
245
246   return error ? 1 : 0;
247 }