queryimmodules.c 4.92 KB
Newer Older
Owen Taylor's avatar
Owen Taylor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* GTK+
 * querymodules.c:
 *
 * Copyright (C) 2000 Red Hat Software
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

22
#include <config.h>
Owen Taylor's avatar
Owen Taylor committed
23 24

#include <glib.h>
25
#include <glib/gprintf.h>
Owen Taylor's avatar
Owen Taylor committed
26 27 28 29 30 31 32 33
#include <gmodule.h>

#include <errno.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

34
#ifdef USE_LA_MODULES
35
#define SOEXT ".la"
Owen Taylor's avatar
Owen Taylor committed
36
#else
37
#define SOEXT ("." G_MODULE_SUFFIX)
Owen Taylor's avatar
Owen Taylor committed
38 39 40 41
#endif

#include "gtk/gtkrc.h"
#include "gtk/gtkimmodule.h"
42
#include "gtk/gtkversion.h"
Owen Taylor's avatar
Owen Taylor committed
43

44 45 46
static char *
escape_string (const char *str)
{
47
  GString *result = g_string_new (NULL);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

  while (TRUE)
    {
      char c = *str++;
      
      switch (c)
	{
	case '\0':
	  goto done;
	case '\n':
	  g_string_append (result, "\\n");
	  break;
	case '\"':
	  g_string_append (result, "\\\"");
	  break;
63 64 65 66 67 68 69 70
#ifdef G_OS_WIN32
		/* Replace backslashes in path with forward slashes, so that
		 * it reads in without problems.
		 */
	case '\\':
	  g_string_append (result, "/");
	  break;
#endif	
71 72 73 74 75 76 77 78 79
	default:
	  g_string_append_c (result, c);
	}
    }

 done:
  return g_string_free (result, FALSE);
}

Hans Breuer's avatar
Hans Breuer committed
80
static void
Owen Taylor's avatar
Owen Taylor committed
81 82
print_escaped (const char *str)
{
83
  char *tmp = escape_string (str);
84
  g_printf ("\"%s\" ", tmp);
Owen Taylor's avatar
Owen Taylor committed
85 86 87
  g_free (tmp);
}

Hans Breuer's avatar
Hans Breuer committed
88
static gboolean
Owen Taylor's avatar
Owen Taylor committed
89 90 91 92 93 94 95 96 97 98 99 100
query_module (const char *dir, const char *name)
{
  void          (*list)   (const GtkIMContextInfo ***contexts,
 		           guint                    *n_contexts);
  void          (*init)   (GTypeModule              *type_module);
  void          (*exit)   (void);
  GtkIMContext *(*create) (const gchar             *context_id);

  GModule *module;
  gchar *path;
  gboolean error = FALSE;

101
  if (g_path_is_absolute (name))
Owen Taylor's avatar
Owen Taylor committed
102 103
    path = g_strdup (name);
  else
104
    path = g_build_filename (dir, name, NULL);
Owen Taylor's avatar
Owen Taylor committed
105 106 107 108 109
  
  module = g_module_open (path, 0);

  if (!module)
    {
110
      g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
Owen Taylor's avatar
Owen Taylor committed
111 112 113 114
      error = TRUE;
    }
	  
  if (module &&
115 116 117 118
      g_module_symbol (module, "im_module_list", (gpointer *) &list) &&
      g_module_symbol (module, "im_module_init", (gpointer *) &init) &&
      g_module_symbol (module, "im_module_exit", (gpointer *) &exit) &&
      g_module_symbol (module, "im_module_create", (gpointer *) &create))
Owen Taylor's avatar
Owen Taylor committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    {
      const GtkIMContextInfo **contexts;
      guint n_contexts;
      int i;

      print_escaped (path);
      fputs ("\n", stdout);

      (*list) (&contexts, &n_contexts);

      for (i=0; i<n_contexts; i++)
	{
	  print_escaped (contexts[i]->context_id);
	  print_escaped (contexts[i]->context_name);
	  print_escaped (contexts[i]->domain);
	  print_escaped (contexts[i]->domain_dirname);
	  print_escaped (contexts[i]->default_locales);
	  fputs ("\n", stdout);
	}
      fputs ("\n", stdout);
    }
  else
    {
142 143
      g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
		 g_module_error ());
Owen Taylor's avatar
Owen Taylor committed
144 145 146 147 148 149 150 151 152 153 154 155
      error = TRUE;
    }

  g_free (path);
  if (module)
    g_module_close (module);

  return error;
}		       

int main (int argc, char **argv)
{
156
  char *cwd;
Owen Taylor's avatar
Owen Taylor committed
157 158 159 160
  int i;
  char *path;
  gboolean error = FALSE;

161
  g_printf ("# GTK+ Input Method Modules file\n"
162 163 164 165 166 167
	    "# Automatically generated file, do not edit\n"
	    "# Created by %s from gtk+-%d.%d.%d\n"
	    "#\n",
	    argv[0],
	    GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);

Owen Taylor's avatar
Owen Taylor committed
168 169 170 171 172

  if (argc == 1)		/* No arguments given */
    {
      char **dirs;
      int i;
173
      GHashTable *dirs_done;
Owen Taylor's avatar
Owen Taylor committed
174 175 176

      path = gtk_rc_get_im_module_path ();

177
      g_printf ("# ModulesPath = %s\n#\n", path);
Owen Taylor's avatar
Owen Taylor committed
178 179

      dirs = pango_split_file_list (path);
180
      dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
Owen Taylor's avatar
Owen Taylor committed
181 182

      for (i=0; dirs[i]; i++)
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        if (!g_hash_table_lookup (dirs_done, dirs[i]))
          {
	    GDir *dir = g_dir_open (dirs[i], 0, NULL);
	    if (dir)
	      {
	        const char *dent;

	        while ((dent = g_dir_read_name (dir)))
	          {
	            if (g_str_has_suffix (dent, SOEXT))
	              error |= query_module (dirs[i], dent);
	          }
	        
	        g_dir_close (dir);
	      }

            g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
          }

      g_hash_table_destroy (dirs_done);
Owen Taylor's avatar
Owen Taylor committed
203 204 205
    }
  else
    {
206
      cwd = g_get_current_dir ();
Owen Taylor's avatar
Owen Taylor committed
207 208 209
      
      for (i=1; i<argc; i++)
	error |= query_module (cwd, argv[i]);
210 211

      g_free (cwd);
Owen Taylor's avatar
Owen Taylor committed
212 213 214 215
    }
  
  return error ? 1 : 0;
}