]> Pileus Git - ~andy/gtk/blob - gtk/queryimmodules.c
Copy code from queryloaders.c which turns backslashes in slashes on win32.
[~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
44 static char *
45 escape_string (const char *str)
46 {
47   GString *result = g_string_new (NULL);
48
49   while (TRUE)
50     {
51       char c = *str++;
52       
53       switch (c)
54         {
55         case '\0':
56           goto done;
57         case '\n':
58           g_string_append (result, "\\n");
59           break;
60         case '\"':
61           g_string_append (result, "\\\"");
62           break;
63 #ifdef G_OS_WIN32
64                 /* Replace backslashes in path with forward slashes, so that
65                  * it reads in without problems.
66                  */
67         case '\\':
68           g_string_append (result, "/");
69           break;
70 #endif  
71         default:
72           g_string_append_c (result, c);
73         }
74     }
75
76  done:
77   return g_string_free (result, FALSE);
78 }
79
80 static void
81 print_escaped (const char *str)
82 {
83   char *tmp = escape_string (str);
84   g_printf ("\"%s\" ", tmp);
85   g_free (tmp);
86 }
87
88 static gboolean
89 query_module (const char *dir, const char *name)
90 {
91   void          (*list)   (const GtkIMContextInfo ***contexts,
92                            guint                    *n_contexts);
93   void          (*init)   (GTypeModule              *type_module);
94   void          (*exit)   (void);
95   GtkIMContext *(*create) (const gchar             *context_id);
96
97   GModule *module;
98   gchar *path;
99   gboolean error = FALSE;
100
101   if (g_path_is_absolute (name))
102     path = g_strdup (name);
103   else
104     path = g_build_filename (dir, name, NULL);
105   
106   module = g_module_open (path, 0);
107
108   if (!module)
109     {
110       g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
111       error = TRUE;
112     }
113           
114   if (module &&
115       g_module_symbol (module, "im_module_list", (gpointer *) &list) &&
116       g_module_symbol (module, "im_module_init", (gpointer *) &init) &&
117       g_module_symbol (module, "im_module_exit", (gpointer *) &exit) &&
118       g_module_symbol (module, "im_module_create", (gpointer *) &create))
119     {
120       const GtkIMContextInfo **contexts;
121       guint n_contexts;
122       int i;
123
124       print_escaped (path);
125       fputs ("\n", stdout);
126
127       (*list) (&contexts, &n_contexts);
128
129       for (i=0; i<n_contexts; i++)
130         {
131           print_escaped (contexts[i]->context_id);
132           print_escaped (contexts[i]->context_name);
133           print_escaped (contexts[i]->domain);
134           print_escaped (contexts[i]->domain_dirname);
135           print_escaped (contexts[i]->default_locales);
136           fputs ("\n", stdout);
137         }
138       fputs ("\n", stdout);
139     }
140   else
141     {
142       g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
143                  g_module_error ());
144       error = TRUE;
145     }
146
147   g_free (path);
148   if (module)
149     g_module_close (module);
150
151   return error;
152 }                      
153
154 int main (int argc, char **argv)
155 {
156   char *cwd;
157   int i;
158   char *path;
159   gboolean error = FALSE;
160
161   g_printf ("# GTK+ Input Method Modules file\n"
162           "# Automatically generated file, do not edit\n"
163           "#\n");
164
165   if (argc == 1)                /* No arguments given */
166     {
167       char **dirs;
168       int i;
169       GHashTable *dirs_done;
170
171       path = gtk_rc_get_im_module_path ();
172
173       g_printf ("# ModulesPath = %s\n#\n", path);
174
175       dirs = pango_split_file_list (path);
176       dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
177
178       for (i=0; dirs[i]; i++)
179         if (!g_hash_table_lookup (dirs_done, dirs[i]))
180           {
181             GDir *dir = g_dir_open (dirs[i], 0, NULL);
182             if (dir)
183               {
184                 const char *dent;
185
186                 while ((dent = g_dir_read_name (dir)))
187                   {
188                     if (g_str_has_suffix (dent, SOEXT))
189                       error |= query_module (dirs[i], dent);
190                   }
191                 
192                 g_dir_close (dir);
193               }
194
195             g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
196           }
197
198       g_hash_table_destroy (dirs_done);
199     }
200   else
201     {
202       cwd = g_get_current_dir ();
203       
204       for (i=1; i<argc; i++)
205         error |= query_module (cwd, argv[i]);
206
207       g_free (cwd);
208     }
209   
210   return error ? 1 : 0;
211 }