]> Pileus Git - ~andy/gtk/blob - gtk/queryimmodules.c
print out the version and binary name in the header comment. Problem
[~andy/gtk] / gtk / queryimmodules.c
1 /* GTK+
2  * querymodules.c:
3  *
4  * Copyright (C) 2000 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 #include <gmodule.h>
27
28 #include <errno.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #if USE_LA_MODULES
35 #define SOEXT ".la"
36 #else
37 #define SOEXT ("." G_MODULE_SUFFIX)
38 #endif
39
40 #include <pango/pango-utils.h>
41 #include "gtk/gtkrc.h"
42 #include "gtk/gtkimmodule.h"
43 #include "gtk/gtkversion.h"
44
45 static char *
46 escape_string (const char *str)
47 {
48   GString *result = g_string_new (NULL);
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 (result, "\\n");
60           break;
61         case '\"':
62           g_string_append (result, "\\\"");
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 (result, "/");
70           break;
71 #endif  
72         default:
73           g_string_append_c (result, c);
74         }
75     }
76
77  done:
78   return g_string_free (result, FALSE);
79 }
80
81 static void
82 print_escaped (const char *str)
83 {
84   char *tmp = escape_string (str);
85   g_printf ("\"%s\" ", tmp);
86   g_free (tmp);
87 }
88
89 static gboolean
90 query_module (const char *dir, const char *name)
91 {
92   void          (*list)   (const GtkIMContextInfo ***contexts,
93                            guint                    *n_contexts);
94   void          (*init)   (GTypeModule              *type_module);
95   void          (*exit)   (void);
96   GtkIMContext *(*create) (const gchar             *context_id);
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", (gpointer *) &list) &&
117       g_module_symbol (module, "im_module_init", (gpointer *) &init) &&
118       g_module_symbol (module, "im_module_exit", (gpointer *) &exit) &&
119       g_module_symbol (module, "im_module_create", (gpointer *) &create))
120     {
121       const GtkIMContextInfo **contexts;
122       guint n_contexts;
123       int i;
124
125       print_escaped (path);
126       fputs ("\n", stdout);
127
128       (*list) (&contexts, &n_contexts);
129
130       for (i=0; i<n_contexts; i++)
131         {
132           print_escaped (contexts[i]->context_id);
133           print_escaped (contexts[i]->context_name);
134           print_escaped (contexts[i]->domain);
135           print_escaped (contexts[i]->domain_dirname);
136           print_escaped (contexts[i]->default_locales);
137           fputs ("\n", stdout);
138         }
139       fputs ("\n", stdout);
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
155 int main (int argc, char **argv)
156 {
157   char *cwd;
158   int i;
159   char *path;
160   gboolean error = FALSE;
161
162   g_printf ("# GTK+ Input Method Modules file\n"
163             "# Automatically generated file, do not edit\n"
164             "# Created by %s from gtk+-%d.%d.%d\n"
165             "#\n",
166             argv[0],
167             GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
168
169
170   if (argc == 1)                /* No arguments given */
171     {
172       char **dirs;
173       int i;
174       GHashTable *dirs_done;
175
176       path = gtk_rc_get_im_module_path ();
177
178       g_printf ("# ModulesPath = %s\n#\n", path);
179
180       dirs = pango_split_file_list (path);
181       dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
182
183       for (i=0; dirs[i]; i++)
184         if (!g_hash_table_lookup (dirs_done, dirs[i]))
185           {
186             GDir *dir = g_dir_open (dirs[i], 0, NULL);
187             if (dir)
188               {
189                 const char *dent;
190
191                 while ((dent = g_dir_read_name (dir)))
192                   {
193                     if (g_str_has_suffix (dent, SOEXT))
194                       error |= query_module (dirs[i], dent);
195                   }
196                 
197                 g_dir_close (dir);
198               }
199
200             g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
201           }
202
203       g_hash_table_destroy (dirs_done);
204     }
205   else
206     {
207       cwd = g_get_current_dir ();
208       
209       for (i=1; i<argc; i++)
210         error |= query_module (cwd, argv[i]);
211
212       g_free (cwd);
213     }
214   
215   return error ? 1 : 0;
216 }