]> Pileus Git - ~andy/gtk/blob - gtk/queryimmodules.c
Change FSF Address
[~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, 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
40 #define GDK_DISABLE_DEPRECATION_WARNINGS
41 #undef GTK_DISABLE_DEPRECATED
42
43 #include "gtk/deprecated/gtkrc.h"
44
45 static void
46 escape_string (GString *contents, const char *str)
47 {
48   while (TRUE)
49     {
50       char c = *str++;
51
52       switch (c)
53         {
54         case '\0':
55           goto done;
56         case '\n':
57           g_string_append (contents, "\\n");
58           break;
59         case '\"':
60           g_string_append (contents, "\\\"");
61           break;
62 #ifdef G_OS_WIN32
63                 /* Replace backslashes in path with forward slashes, so that
64                  * it reads in without problems.
65                  */
66         case '\\':
67           g_string_append (contents, "/");
68           break;
69 #endif
70         default:
71           g_string_append_c (contents, c);
72         }
73     }
74
75  done:;
76 }
77
78 static void
79 print_escaped (GString *contents, const char *str)
80 {
81   g_string_append_c (contents, '"');
82   escape_string (contents, str);
83   g_string_append_c (contents, '"');
84   g_string_append_c (contents, ' ');
85 }
86
87 static gboolean
88 query_module (const char *dir, const char *name, GString *contents)
89 {
90   void          (*list)   (const GtkIMContextInfo ***contexts,
91                            guint                    *n_contexts);
92
93   gpointer list_ptr;
94   gpointer init_ptr;
95   gpointer exit_ptr;
96   gpointer create_ptr;
97
98   GModule *module;
99   gchar *path;
100   gboolean error = FALSE;
101
102   if (g_path_is_absolute (name))
103     path = g_strdup (name);
104   else
105     path = g_build_filename (dir, name, NULL);
106
107   module = g_module_open (path, 0);
108
109   if (!module)
110     {
111       g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
112       error = TRUE;
113     }
114
115   if (module &&
116       g_module_symbol (module, "im_module_list", &list_ptr) &&
117       g_module_symbol (module, "im_module_init", &init_ptr) &&
118       g_module_symbol (module, "im_module_exit", &exit_ptr) &&
119       g_module_symbol (module, "im_module_create", &create_ptr))
120     {
121       const GtkIMContextInfo **contexts;
122       guint n_contexts;
123       int i;
124
125       list = list_ptr;
126
127       print_escaped (contents, path);
128       g_string_append_c (contents, '\n');
129
130       (*list) (&contexts, &n_contexts);
131
132       for (i = 0; i < n_contexts; i++)
133         {
134           print_escaped (contents, contexts[i]->context_id);
135           print_escaped (contents, contexts[i]->context_name);
136           print_escaped (contents, contexts[i]->domain);
137           print_escaped (contents, contexts[i]->domain_dirname);
138           print_escaped (contents, contexts[i]->default_locales);
139           g_string_append_c (contents, '\n');
140         }
141       g_string_append_c (contents, '\n');
142     }
143   else
144     {
145       g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
146                  g_module_error ());
147       error = TRUE;
148     }
149
150   g_free (path);
151   if (module)
152     g_module_close (module);
153
154   return error;
155 }
156
157 int main (int argc, char **argv)
158 {
159   char *cwd;
160   int i;
161   char *path;
162   gboolean error = FALSE;
163   gchar *cache_file = NULL;
164   gint first_file = 1;
165   GString *contents;
166
167   if (argc > 1 && strcmp (argv[1], "--update-cache") == 0)
168     {
169       cache_file = gtk_rc_get_im_module_file ();
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       int i;
186       GHashTable *dirs_done;
187
188       path = gtk_rc_get_im_module_path ();
189
190       g_string_append_printf (contents, "# ModulesPath = %s\n#\n", path);
191
192       dirs = pango_split_file_list (path);
193       dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
194
195       for (i = 0; dirs[i]; i++)
196         if (!g_hash_table_lookup (dirs_done, dirs[i]))
197           {
198             GDir *dir = g_dir_open (dirs[i], 0, NULL);
199             if (dir)
200               {
201                 const char *dent;
202
203                 while ((dent = g_dir_read_name (dir)))
204                   {
205                     if (g_str_has_suffix (dent, SOEXT))
206                       error |= query_module (dirs[i], dent, contents);
207                   }
208
209                 g_dir_close (dir);
210               }
211
212             g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
213           }
214
215       g_hash_table_destroy (dirs_done);
216     }
217   else
218     {
219       cwd = g_get_current_dir ();
220
221       for (i = first_file; i < argc; i++)
222         error |= query_module (cwd, argv[i], contents);
223
224       g_free (cwd);
225     }
226
227   if (!error)
228     {
229       if (cache_file)
230         {
231           GError *err;
232
233           err = NULL;
234           if (!g_file_set_contents (cache_file, contents->str, -1, &err))
235             {
236                 g_fprintf (stderr, "%s\n", err->message);
237                 error = 1;
238             }
239         }
240       else
241         g_print ("%s\n", contents->str);
242     }
243
244   return error ? 1 : 0;
245 }