Let query utilities update the cache file directly

This is much more convenient for packagers than having to
redirect the output into the cache file, and much less error-prone.
This commit is contained in:
Matthias Clasen 2010-05-17 22:58:25 -04:00
parent 0b0f176ac4
commit 735bee47be
4 changed files with 154 additions and 72 deletions

View File

@ -17,6 +17,7 @@
<refsynopsisdiv> <refsynopsisdiv>
<cmdsynopsis> <cmdsynopsis>
<command>gdk-pixbuf-query-loaders-3.0</command> <command>gdk-pixbuf-query-loaders-3.0</command>
<arg choice="opt">--update-cache</arg>
<arg choice="opt" rep="repeat">module</arg> <arg choice="opt" rep="repeat">module</arg>
</cmdsynopsis> </cmdsynopsis>
</refsynopsisdiv> </refsynopsisdiv>
@ -25,7 +26,7 @@
<para> <para>
<command>gdk-pixbuf-query-loaders-3.0</command> collects information about <command>gdk-pixbuf-query-loaders-3.0</command> collects information about
loadable modules for <application>gdk-pixbuf</application> and writes it to loadable modules for <application>gdk-pixbuf</application> and writes it to
<filename>stdout</filename>. the default cache file location, or to <filename>stdout</filename>.
</para> </para>
<para> <para>
If called without arguments, it looks for modules in the If called without arguments, it looks for modules in the
@ -43,6 +44,16 @@ can be set to point <application>gdk-pixbuf</application> at the file.
</para> </para>
</refsect1> </refsect1>
<refsect1><title>Options</title>
<variablelist>
<varlistentry>
<term>--update-cache</term>
<listitem><para>Write the output to the default cache location instead of
<filename>stdout</filename></para></listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1><title>Environment</title> <refsect1><title>Environment</title>
<para> <para>
The environment variable <envar>GDK_PIXBUF_MODULEDIR</envar> can be used The environment variable <envar>GDK_PIXBUF_MODULEDIR</envar> can be used

View File

@ -17,6 +17,7 @@
<refsynopsisdiv> <refsynopsisdiv>
<cmdsynopsis> <cmdsynopsis>
<command>gtk-query-immodules-3.0</command> <command>gtk-query-immodules-3.0</command>
<arg choice="opt">--update-cache</arg>
<arg choice="opt" rep="repeat">module</arg> <arg choice="opt" rep="repeat">module</arg>
</cmdsynopsis> </cmdsynopsis>
</refsynopsisdiv> </refsynopsisdiv>
@ -24,7 +25,8 @@
<refsect1><title>Description</title> <refsect1><title>Description</title>
<para> <para>
<command>gtk-query-immodules-3.0</command> collects information about loadable <command>gtk-query-immodules-3.0</command> collects information about loadable
input method modules for GTK+ and writes it to <filename>stdout</filename>. input method modules for GTK+ and writes it to the default cache file
location, or to <filename>stdout</filename>.
</para> </para>
<para> <para>
If called without arguments, it looks for modules in the GTK+ input method If called without arguments, it looks for modules in the GTK+ input method
@ -42,6 +44,16 @@ can be set to point GTK+ at the file.
</para> </para>
</refsect1> </refsect1>
<refsect1><title>Options</title>
<variablelist>
<varlistentry>
<term>--update-cache</term>
<listitem><para>Write the output to the default cache location instead of
<filename>stdout</filename></para></listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1><title>Environment</title> <refsect1><title>Environment</title>
<para> <para>
The environment variable <link linkend="GTK_PATH"><envar>GTK_PATH</envar></link> The environment variable <link linkend="GTK_PATH"><envar>GTK_PATH</envar></link>

View File

@ -50,10 +50,10 @@
#endif #endif
static void static void
print_escaped (const char *str) print_escaped (GString *contents, const char *str)
{ {
gchar *tmp = g_strescape (str, ""); gchar *tmp = g_strescape (str, "");
g_printf ("\"%s\" ", tmp); g_string_append_printf (contents, "\"%s\" ", tmp);
g_free (tmp); g_free (tmp);
} }
@ -78,13 +78,13 @@ loader_sanity_check (const char *path, GdkPixbufFormat *info, GdkPixbufModule *v
if (mask_len != prefix_len) if (mask_len != prefix_len)
{ {
error = "mask length mismatch"; error = "mask length mismatch";
goto error; goto error;
} }
if (strspn (pattern->mask, " !xzn*") < mask_len) if (strspn (pattern->mask, " !xzn*") < mask_len)
{ {
error = "bad char in mask"; error = "bad char in mask";
goto error; goto error;
} }
} }
@ -109,48 +109,48 @@ loader_sanity_check (const char *path, GdkPixbufFormat *info, GdkPixbufModule *v
error = "loader claims to support saving but doesn't implement save"; error = "loader claims to support saving but doesn't implement save";
goto error; goto error;
} }
return 1; return 1;
error: error:
g_fprintf (stderr, "Loader sanity check failed for %s: %s\n", g_fprintf (stderr, "Loader sanity check failed for %s: %s\n",
path, error); path, error);
return 0; return 0;
} }
static void static void
write_loader_info (const char *path, GdkPixbufFormat *info) write_loader_info (GString *contents, const char *path, GdkPixbufFormat *info)
{ {
const GdkPixbufModulePattern *pattern; const GdkPixbufModulePattern *pattern;
char **mime; char **mime;
char **ext; char **ext;
g_printf("\"%s\"\n", path); g_string_append_printf (contents, "\"%s\"\n", path);
g_printf ("\"%s\" %u \"%s\" \"%s\" \"%s\"\n", g_string_append_printf (contents, "\"%s\" %u \"%s\" \"%s\" \"%s\"\n",
info->name, info->name,
info->flags, info->flags,
info->domain ? info->domain : GETTEXT_PACKAGE, info->domain ? info->domain : GETTEXT_PACKAGE,
info->description, info->description,
info->license ? info->license : ""); info->license ? info->license : "");
for (mime = info->mime_types; *mime; mime++) { for (mime = info->mime_types; *mime; mime++) {
g_printf ("\"%s\" ", *mime); g_string_append_printf (contents, "\"%s\" ", *mime);
} }
g_printf ("\"\"\n"); g_string_append (contents, "\"\"\n");
for (ext = info->extensions; *ext; ext++) { for (ext = info->extensions; *ext; ext++) {
g_printf ("\"%s\" ", *ext); g_string_append_printf (contents, "\"%s\" ", *ext);
} }
g_printf ("\"\"\n"); g_string_append (contents, "\"\"\n");
for (pattern = info->signature; pattern->prefix; pattern++) { for (pattern = info->signature; pattern->prefix; pattern++) {
print_escaped (pattern->prefix); print_escaped (contents, pattern->prefix);
print_escaped (pattern->mask ? (const char *)pattern->mask : ""); print_escaped (contents, pattern->mask ? (const char *)pattern->mask : "");
g_printf ("%d\n", pattern->relevance); g_string_append_printf (contents, "%d\n", pattern->relevance);
} }
g_printf ("\n"); g_string_append_c (contents, '\n');
} }
static void static void
query_module (const char *dir, const char *file) query_module (GString *contents, const char *dir, const char *file)
{ {
char *path; char *path;
GModule *module; GModule *module;
@ -170,7 +170,7 @@ query_module (const char *dir, const char *file)
g_module_symbol (module, "fill_vtable", &fill_vtable_ptr)) { g_module_symbol (module, "fill_vtable", &fill_vtable_ptr)) {
GdkPixbufFormat *info; GdkPixbufFormat *info;
GdkPixbufModule *vtable; GdkPixbufModule *vtable;
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
/* Replace backslashes in path with forward slashes, so that /* Replace backslashes in path with forward slashes, so that
* it reads in without problems. * it reads in without problems.
@ -194,10 +194,10 @@ query_module (const char *dir, const char *file)
(*fill_info) (info); (*fill_info) (info);
(*fill_vtable) (vtable); (*fill_vtable) (vtable);
if (loader_sanity_check (path, info, vtable)) if (loader_sanity_check (path, info, vtable))
write_loader_info (path, info); write_loader_info (contents, path, info);
g_free (info); g_free (info);
g_free (vtable); g_free (vtable);
} }
@ -213,10 +213,24 @@ query_module (const char *dir, const char *file)
g_free (path); g_free (path);
} }
static gchar *
gdk_pixbuf_get_module_file (void)
{
gchar *result = g_strdup (g_getenv ("GDK_PIXBUF_MODULE_FILE"));
if (!result)
result = g_build_filename (GTK_LIBDIR, "gtk-3.0", GTK_BINARY_VERSION, "loaders.cache", NULL);
return result;
}
int main (int argc, char **argv) int main (int argc, char **argv)
{ {
gint i; gint i;
gchar *prgname; gchar *prgname;
GString *contents;
gchar *cache_file = NULL;
gint first_file = 1;
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
gchar *libdir; gchar *libdir;
@ -269,15 +283,24 @@ int main (int argc, char **argv)
#define PIXBUF_LIBDIR libdir #define PIXBUF_LIBDIR libdir
#endif #endif
prgname = g_get_prgname ();
g_printf ("# GdkPixbuf Image Loader Modules file\n"
"# Automatically generated file, do not edit\n"
"# Created by %s from gtk+-%s\n"
"#\n",
(prgname ? prgname : "gdk-pixbuf-query-loaders-3.0"),
GDK_PIXBUF_VERSION);
if (argc == 1) { if (argc > 1 && strcmp (argv[1], "--update-cache") == 0) {
cache_file = gdk_pixbuf_get_module_file ();
first_file = 2;
}
contents = g_string_new ("");
prgname = g_get_prgname ();
g_string_append_printf (contents,
"# GdkPixbuf Image Loader Modules file\n"
"# Automatically generated file, do not edit\n"
"# Created by %s from gtk+-%s\n"
"#\n",
(prgname ? prgname : "gdk-pixbuf-query-loaders-3.0"),
GDK_PIXBUF_VERSION);
if (argc == first_file) {
#ifdef USE_GMODULE #ifdef USE_GMODULE
const char *path; const char *path;
GDir *dir; GDir *dir;
@ -290,7 +313,7 @@ int main (int argc, char **argv)
if (path == NULL || *path == '\0') if (path == NULL || *path == '\0')
path = PIXBUF_LIBDIR; path = PIXBUF_LIBDIR;
g_printf ("# LoaderDir = %s\n#\n", path); g_string_append_printf (contents, "# LoaderDir = %s\n#\n", path);
dir = g_dir_open (path, 0, NULL); dir = g_dir_open (path, 0, NULL);
if (dir) { if (dir) {
@ -300,13 +323,13 @@ int main (int argc, char **argv)
gint len = strlen (dent); gint len = strlen (dent);
if (len > SOEXT_LEN && if (len > SOEXT_LEN &&
strcmp (dent + len - SOEXT_LEN, SOEXT) == 0) { strcmp (dent + len - SOEXT_LEN, SOEXT) == 0) {
query_module (path, dent); query_module (contents, path, dent);
} }
} }
g_dir_close (dir); g_dir_close (dir);
} }
#else #else
g_printf ("# dynamic loading of modules not supported\n"); g_string_append_printf (contents, "# dynamic loading of modules not supported\n");
#endif #endif
} }
else { else {
@ -318,10 +341,21 @@ int main (int argc, char **argv)
infilename = g_locale_to_utf8 (infilename, infilename = g_locale_to_utf8 (infilename,
-1, NULL, NULL, NULL); -1, NULL, NULL, NULL);
#endif #endif
query_module (cwd, infilename); query_module (contents, cwd, infilename);
} }
g_free (cwd); g_free (cwd);
} }
if (cache_file) {
GError *err;
err = NULL;
if (!g_file_set_contents (cache_file, contents->str, -1, &err)) {
g_fprintf (stderr, "%s\n", err->message);
}
}
else
g_print ("%s\n", contents->str);
return 0; return 0;
} }

View File

@ -41,11 +41,9 @@
#include "gtk/gtkimmodule.h" #include "gtk/gtkimmodule.h"
#include "gtk/gtkversion.h" #include "gtk/gtkversion.h"
static char * static void
escape_string (const char *str) escape_string (GString *contents, const char *str)
{ {
GString *result = g_string_new (NULL);
while (TRUE) while (TRUE)
{ {
char c = *str++; char c = *str++;
@ -55,38 +53,38 @@ escape_string (const char *str)
case '\0': case '\0':
goto done; goto done;
case '\n': case '\n':
g_string_append (result, "\\n"); g_string_append (contents, "\\n");
break; break;
case '\"': case '\"':
g_string_append (result, "\\\""); g_string_append (contents, "\\\"");
break; break;
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
/* Replace backslashes in path with forward slashes, so that /* Replace backslashes in path with forward slashes, so that
* it reads in without problems. * it reads in without problems.
*/ */
case '\\': case '\\':
g_string_append (result, "/"); g_string_append (contents, "/");
break; break;
#endif #endif
default: default:
g_string_append_c (result, c); g_string_append_c (contents, c);
} }
} }
done: done:;
return g_string_free (result, FALSE);
} }
static void static void
print_escaped (const char *str) print_escaped (GString *contents, const char *str)
{ {
char *tmp = escape_string (str); g_string_append_c (contents, '"');
g_printf ("\"%s\" ", tmp); escape_string (contents, str);
g_free (tmp); g_string_append_c (contents, '"');
g_string_append_c (contents, ' ');
} }
static gboolean static gboolean
query_module (const char *dir, const char *name) query_module (const char *dir, const char *name, GString *contents)
{ {
void (*list) (const GtkIMContextInfo ***contexts, void (*list) (const GtkIMContextInfo ***contexts,
guint *n_contexts); guint *n_contexts);
@ -131,21 +129,21 @@ query_module (const char *dir, const char *name)
exit = exit_ptr; exit = exit_ptr;
create = create_ptr; create = create_ptr;
print_escaped (path); print_escaped (contents, path);
fputs ("\n", stdout); g_string_append_c (contents, '\n');
(*list) (&contexts, &n_contexts); (*list) (&contexts, &n_contexts);
for (i=0; i<n_contexts; i++) for (i = 0; i < n_contexts; i++)
{ {
print_escaped (contexts[i]->context_id); print_escaped (contents, contexts[i]->context_id);
print_escaped (contexts[i]->context_name); print_escaped (contents, contexts[i]->context_name);
print_escaped (contexts[i]->domain); print_escaped (contents, contexts[i]->domain);
print_escaped (contexts[i]->domain_dirname); print_escaped (contents, contexts[i]->domain_dirname);
print_escaped (contexts[i]->default_locales); print_escaped (contents, contexts[i]->default_locales);
fputs ("\n", stdout); g_string_append_c (contents, '\n');
} }
fputs ("\n", stdout); g_string_append_c (contents, '\n');
} }
else else
{ {
@ -167,16 +165,26 @@ int main (int argc, char **argv)
int i; int i;
char *path; char *path;
gboolean error = FALSE; gboolean error = FALSE;
gchar *cache_file = NULL;
gint first_file = 1;
GString *contents;
g_printf ("# GTK+ Input Method Modules file\n" if (argc > 1 && strcmp (argv[1], "--update-cache") == 0)
"# Automatically generated file, do not edit\n" {
"# Created by %s from gtk+-%d.%d.%d\n" cache_file = gtk_rc_get_im_module_file ();
"#\n", first_file = 2;
argv[0], }
GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
contents = g_string_new ("");
g_string_append_printf (contents,
"# GTK+ Input Method Modules file\n"
"# 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);
if (argc == 1) /* No arguments given */ if (argc == first_file) /* No file arguments given */
{ {
char **dirs; char **dirs;
int i; int i;
@ -184,7 +192,7 @@ int main (int argc, char **argv)
path = gtk_rc_get_im_module_path (); path = gtk_rc_get_im_module_path ();
g_printf ("# ModulesPath = %s\n#\n", path); g_string_append_printf (contents, "# ModulesPath = %s\n#\n", path);
dirs = pango_split_file_list (path); dirs = pango_split_file_list (path);
dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL); dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
@ -200,7 +208,7 @@ int main (int argc, char **argv)
while ((dent = g_dir_read_name (dir))) while ((dent = g_dir_read_name (dir)))
{ {
if (g_str_has_suffix (dent, SOEXT)) if (g_str_has_suffix (dent, SOEXT))
error |= query_module (dirs[i], dent); error |= query_module (dirs[i], dent, contents);
} }
g_dir_close (dir); g_dir_close (dir);
@ -215,11 +223,28 @@ int main (int argc, char **argv)
{ {
cwd = g_get_current_dir (); cwd = g_get_current_dir ();
for (i = 1; i < argc; i++) for (i = first_file; i < argc; i++)
error |= query_module (cwd, argv[i]); error |= query_module (cwd, argv[i], contents);
g_free (cwd); g_free (cwd);
} }
if (!error)
{
if (cache_file)
{
GError *err;
err = NULL;
if (!g_file_set_contents (cache_file, contents->str, -1, &err))
{
g_fprintf (stderr, "%s\n", err->message);
error = 1;
}
}
else
g_print ("%s\n", contents->str);
}
return error ? 1 : 0; return error ? 1 : 0;
} }