]> Pileus Git - ~andy/gtk/blob - gtk/queryimmodules.c
Use g_printf instead of system printf. (#99327)
[~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 #ifdef G_OS_WIN32
35 #define SOEXT ".dll"
36 #else
37 #define SOEXT ".so"
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 ("");
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         default:
64           g_string_append_c (result, c);
65         }
66     }
67
68  done:
69   return g_string_free (result, FALSE);
70 }
71
72 static void
73 print_escaped (const char *str)
74 {
75   char *tmp = escape_string (str);
76   g_printf ("\"%s\" ", tmp);
77   g_free (tmp);
78 }
79
80 static gboolean
81 query_module (const char *dir, const char *name)
82 {
83   void          (*list)   (const GtkIMContextInfo ***contexts,
84                            guint                    *n_contexts);
85   void          (*init)   (GTypeModule              *type_module);
86   void          (*exit)   (void);
87   GtkIMContext *(*create) (const gchar             *context_id);
88
89   GModule *module;
90   gchar *path;
91   gboolean error = FALSE;
92
93   if (g_path_is_absolute (name))
94     path = g_strdup (name);
95   else
96     path = g_build_filename (dir, name, NULL);
97   
98   module = g_module_open (path, 0);
99
100   if (!module)
101     {
102       g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
103       error = TRUE;
104     }
105           
106   if (module &&
107       g_module_symbol (module, "im_module_list", (gpointer *) &list) &&
108       g_module_symbol (module, "im_module_init", (gpointer *) &init) &&
109       g_module_symbol (module, "im_module_exit", (gpointer *) &exit) &&
110       g_module_symbol (module, "im_module_create", (gpointer *) &create))
111     {
112       const GtkIMContextInfo **contexts;
113       guint n_contexts;
114       int i;
115
116       print_escaped (path);
117       fputs ("\n", stdout);
118
119       (*list) (&contexts, &n_contexts);
120
121       for (i=0; i<n_contexts; i++)
122         {
123           print_escaped (contexts[i]->context_id);
124           print_escaped (contexts[i]->context_name);
125           print_escaped (contexts[i]->domain);
126           print_escaped (contexts[i]->domain_dirname);
127           print_escaped (contexts[i]->default_locales);
128           fputs ("\n", stdout);
129         }
130       fputs ("\n", stdout);
131     }
132   else
133     {
134       g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
135                  g_module_error ());
136       error = TRUE;
137     }
138
139   g_free (path);
140   if (module)
141     g_module_close (module);
142
143   return error;
144 }                      
145
146 int main (int argc, char **argv)
147 {
148   char *cwd;
149   int i;
150   char *path;
151   gboolean error = FALSE;
152
153   g_printf ("# GTK+ Input Method Modules file\n"
154           "# Automatically generated file, do not edit\n"
155           "#\n");
156
157   if (argc == 1)                /* No arguments given */
158     {
159       char **dirs;
160       int i;
161
162       path = gtk_rc_get_im_module_path ();
163
164       g_printf ("# ModulesPath = %s\n#\n", path);
165
166       dirs = pango_split_file_list (path);
167
168       for (i=0; dirs[i]; i++)
169         {
170           GDir *dir = g_dir_open (dirs[i], 0, NULL);
171           if (dir)
172             {
173               const char *dent;
174
175               while ((dent = g_dir_read_name (dir)))
176                 {
177                   int len = strlen (dent);
178                   if (len > 3 && strcmp (dent + len - strlen (SOEXT), SOEXT) == 0)
179                     error |= query_module (dirs[i], dent);
180                 }
181               
182               g_dir_close (dir);
183             }
184         }
185     }
186   else
187     {
188       cwd = g_get_current_dir ();
189       
190       for (i=1; i<argc; i++)
191         error |= query_module (cwd, argv[i]);
192
193       g_free (cwd);
194     }
195   
196   return error ? 1 : 0;
197 }