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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21
22 #include <glib.h>
23 #include <glib/gprintf.h>
24
25 #include <errno.h>
26 #include <string.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #ifdef USE_LA_MODULES
32 #define SOEXT ".la"
33 #else
34 #define SOEXT ("." G_MODULE_SUFFIX)
35 #endif
36
37 #include "gtk/gtkimcontextinfo.h"
38 #include "gtk/gtkversion.h"
39 #include "gtk/gtkutilsprivate.h"
40
41 #include "gtk/deprecated/gtkrc.h"
42
43 static void
escape_string(GString * contents,const char * str)44 escape_string (GString *contents, const char *str)
45 {
46 while (TRUE)
47 {
48 char c = *str++;
49
50 switch (c)
51 {
52 case '\0':
53 goto done;
54 case '\n':
55 g_string_append (contents, "\\n");
56 break;
57 case '\"':
58 g_string_append (contents, "\\\"");
59 break;
60 #ifdef G_OS_WIN32
61 /* Replace backslashes in path with forward slashes, so that
62 * it reads in without problems.
63 */
64 case '\\':
65 g_string_append (contents, "/");
66 break;
67 #endif
68 default:
69 g_string_append_c (contents, c);
70 }
71 }
72
73 done:;
74 }
75
76 static void
print_escaped(GString * contents,const char * str)77 print_escaped (GString *contents, const char *str)
78 {
79 g_string_append_c (contents, '"');
80 escape_string (contents, str);
81 g_string_append_c (contents, '"');
82 g_string_append_c (contents, ' ');
83 }
84
85 static gboolean
query_module(const char * dir,const char * name,GString * contents)86 query_module (const char *dir, const char *name, GString *contents)
87 {
88 void (*list) (const GtkIMContextInfo ***contexts,
89 guint *n_contexts);
90
91 gpointer list_ptr;
92 gpointer init_ptr;
93 gpointer exit_ptr;
94 gpointer create_ptr;
95
96 GModule *module;
97 gchar *path;
98 gboolean error = FALSE;
99
100 if (g_path_is_absolute (name))
101 path = g_strdup (name);
102 else
103 path = g_build_filename (dir, name, NULL);
104
105 module = g_module_open (path, 0);
106
107 if (!module)
108 {
109 g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
110 error = TRUE;
111 }
112
113 if (module &&
114 g_module_symbol (module, "im_module_list", &list_ptr) &&
115 g_module_symbol (module, "im_module_init", &init_ptr) &&
116 g_module_symbol (module, "im_module_exit", &exit_ptr) &&
117 g_module_symbol (module, "im_module_create", &create_ptr))
118 {
119 const GtkIMContextInfo **contexts;
120 guint n_contexts;
121 int i;
122
123 list = list_ptr;
124
125 print_escaped (contents, path);
126 g_string_append_c (contents, '\n');
127
128 (*list) (&contexts, &n_contexts);
129
130 for (i = 0; i < n_contexts; i++)
131 {
132 print_escaped (contents, contexts[i]->context_id);
133 print_escaped (contents, contexts[i]->context_name);
134 print_escaped (contents, contexts[i]->domain);
135 print_escaped (contents, contexts[i]->domain_dirname);
136 print_escaped (contents, contexts[i]->default_locales);
137 g_string_append_c (contents, '\n');
138 }
139 g_string_append_c (contents, '\n');
140 }
141 else
142 {
143 g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
144 g_module_error ());
145 error = TRUE;
146 }
147
148 g_free (path);
149 if (module)
150 g_module_close (module);
151
152 return error;
153 }
154
main(int argc,char ** argv)155 int main (int argc, char **argv)
156 {
157 char *cwd;
158 int i;
159 char *path;
160 gboolean error = FALSE;
161 gchar *cache_file = NULL;
162 gint first_file = 1;
163 GString *contents;
164
165 if (argc > 1 && strcmp (argv[1], "--update-cache") == 0)
166 {
167 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
168 cache_file = gtk_rc_get_im_module_file ();
169 G_GNUC_END_IGNORE_DEPRECATIONS
170 first_file = 2;
171 }
172
173 contents = g_string_new ("");
174 g_string_append_printf (contents,
175 "# GTK+ Input Method Modules file\n"
176 "# Automatically generated file, do not edit\n"
177 "# Created by %s from gtk+-%d.%d.%d\n"
178 "#\n",
179 argv[0],
180 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
181
182 if (argc == first_file) /* No file arguments given */
183 {
184 char **dirs;
185 GHashTable *dirs_done;
186
187 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
188 path = gtk_rc_get_im_module_path ();
189 G_GNUC_END_IGNORE_DEPRECATIONS
190
191 g_string_append_printf (contents, "# ModulesPath = %s\n#\n", path);
192
193 dirs = gtk_split_file_list (path);
194 dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
195
196 for (i = 0; dirs[i]; i++)
197 if (!g_hash_table_lookup (dirs_done, dirs[i]))
198 {
199 GDir *dir = g_dir_open (dirs[i], 0, NULL);
200 if (dir)
201 {
202 const char *dent;
203 GList *list = NULL, *iterator = NULL;
204
205 while ((dent = g_dir_read_name (dir)))
206 list = g_list_prepend (list, g_strdup (dent));
207
208 list = g_list_sort (list, (GCompareFunc) strcmp);
209 for (iterator = list; iterator; iterator = iterator->next)
210 {
211 if (g_str_has_suffix (iterator->data, SOEXT))
212 error |= query_module (dirs[i], iterator->data, contents);
213 }
214
215 g_list_free_full (list, g_free);
216 g_dir_close (dir);
217 }
218
219 g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
220 }
221
222 g_hash_table_destroy (dirs_done);
223 }
224 else
225 {
226 cwd = g_get_current_dir ();
227
228 for (i = first_file; i < argc; i++)
229 error |= query_module (cwd, argv[i], contents);
230
231 g_free (cwd);
232 }
233
234 if (!error)
235 {
236 if (cache_file)
237 {
238 GError *err;
239
240 err = NULL;
241 if (!g_file_set_contents (cache_file, contents->str, -1, &err))
242 {
243 g_fprintf (stderr, "%s\n", err->message);
244 error = 1;
245 }
246 }
247 else
248 g_print ("%s\n", contents->str);
249 }
250
251 return error ? 1 : 0;
252 }
253