Add gobject-introspection.changes file
[profile/ivi/gobject-introspection.git] / girepository / gdump.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2  * GObject introspection: Dump introspection data
3  *
4  * Copyright (C) 2008 Colin Walters <walters@verbum.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser 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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser 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 <stdlib.h>
23
24 #include <glib.h>
25 #include <glib-object.h>
26 #include <gio/gio.h>
27
28 /* This file is both compiled into libgirepository.so, and installed
29  * on the filesystem.  But for the dumper, we want to avoid linking
30  * to libgirepository; see
31  * https://bugzilla.gnome.org/show_bug.cgi?id=630342
32  */
33 #ifdef G_IREPOSITORY_COMPILATION
34 #include "config.h"
35 #include "girepository.h"
36 #endif
37
38 #include <string.h>
39
40 static void
41 escaped_printf (GOutputStream *out, const char *fmt, ...)
42 {
43   char *str;
44   va_list args;
45   gsize written;
46   GError *error = NULL;
47
48   va_start (args, fmt);
49
50   str = g_markup_vprintf_escaped (fmt, args);
51   if (!g_output_stream_write_all (out, str, strlen (str), &written, NULL, &error))
52     {
53       g_critical ("failed to write to iochannel: %s", error->message);
54       g_clear_error (&error);
55     }
56   g_free (str);
57
58   va_end (args);
59 }
60
61 static void
62 goutput_write (GOutputStream *out, const char *str)
63 {
64   gsize written;
65   GError *error = NULL;
66   if (!g_output_stream_write_all (out, str, strlen (str), &written, NULL, &error))
67     {
68       g_critical ("failed to write to iochannel: %s", error->message);
69       g_clear_error (&error);
70     }
71 }
72
73 typedef GType (*GetTypeFunc)(void);
74 typedef GQuark (*ErrorQuarkFunc)(void);
75
76 static GType
77 invoke_get_type (GModule *self, const char *symbol, GError **error)
78 {
79   GetTypeFunc sym;
80   GType ret;
81
82   if (!g_module_symbol (self, symbol, (void**)&sym))
83     {
84       g_set_error (error,
85                    G_IO_ERROR,
86                    G_IO_ERROR_FAILED,
87                    "Failed to find symbol '%s'", symbol);
88       return G_TYPE_INVALID;
89     }
90
91   ret = sym ();
92   if (ret == G_TYPE_INVALID)
93     {
94       g_set_error (error,
95                    G_IO_ERROR,
96                    G_IO_ERROR_FAILED,
97                    "Function '%s' returned G_TYPE_INVALID", symbol);
98     }
99   return ret;
100 }
101
102 static GQuark
103 invoke_error_quark (GModule *self, const char *symbol, GError **error)
104 {
105   ErrorQuarkFunc sym;
106
107   if (!g_module_symbol (self, symbol, (void**)&sym))
108     {
109       g_set_error (error,
110                    G_IO_ERROR,
111                    G_IO_ERROR_FAILED,
112                    "Failed to find symbol '%s'", symbol);
113       return G_TYPE_INVALID;
114     }
115
116   return sym ();
117 }
118
119 static void
120 dump_properties (GType type, GOutputStream *out)
121 {
122   guint i;
123   guint n_properties;
124   GParamSpec **props;
125
126   if (G_TYPE_FUNDAMENTAL (type) == G_TYPE_OBJECT)
127     {
128       GObjectClass *klass;
129       klass = g_type_class_ref (type);
130       props = g_object_class_list_properties (klass, &n_properties);
131     }
132   else
133     {
134       void *klass;
135       klass = g_type_default_interface_ref (type);
136       props = g_object_interface_list_properties (klass, &n_properties);
137     }
138
139   for (i = 0; i < n_properties; i++)
140     {
141       GParamSpec *prop;
142
143       prop = props[i];
144       if (prop->owner_type != type)
145         continue;
146
147       escaped_printf (out, "    <property name=\"%s\" type=\"%s\" flags=\"%d\"/>\n",
148                       prop->name, g_type_name (prop->value_type), prop->flags);
149     }
150   g_free (props);
151 }
152
153 static void
154 dump_signals (GType type, GOutputStream *out)
155 {
156   guint i;
157   guint n_sigs;
158   guint *sig_ids;
159
160   sig_ids = g_signal_list_ids (type, &n_sigs);
161   for (i = 0; i < n_sigs; i++)
162     {
163       guint sigid;
164       GSignalQuery query;
165       guint j;
166
167       sigid = sig_ids[i];
168       g_signal_query (sigid, &query);
169
170       escaped_printf (out, "    <signal name=\"%s\" return=\"%s\"",
171                       query.signal_name, g_type_name (query.return_type));
172
173       if (query.signal_flags & G_SIGNAL_RUN_FIRST)
174         escaped_printf (out, " when=\"first\"");
175       else if (query.signal_flags & G_SIGNAL_RUN_LAST)
176         escaped_printf (out, " when=\"last\"");
177       else if (query.signal_flags & G_SIGNAL_RUN_CLEANUP)
178         escaped_printf (out, " when=\"cleanup\"");
179 #if GLIB_CHECK_VERSION(2, 29, 15)
180       else if (query.signal_flags & G_SIGNAL_MUST_COLLECT)
181         escaped_printf (out, " when=\"must-collect\"");
182 #endif
183       if (query.signal_flags & G_SIGNAL_NO_RECURSE)
184         escaped_printf (out, " no-recurse=\"1\"");
185
186       if (query.signal_flags & G_SIGNAL_DETAILED)
187         escaped_printf (out, " detailed=\"1\"");
188
189       if (query.signal_flags & G_SIGNAL_ACTION)
190         escaped_printf (out, " action=\"1\"");
191
192       if (query.signal_flags & G_SIGNAL_NO_HOOKS)
193         escaped_printf (out, " no-hooks=\"1\"");
194
195       goutput_write (out, ">\n");
196
197       for (j = 0; j < query.n_params; j++)
198         {
199           escaped_printf (out, "      <param type=\"%s\"/>\n",
200                           g_type_name (query.param_types[j]));
201         }
202       goutput_write (out, "    </signal>\n");
203     }
204 }
205
206 static void
207 dump_object_type (GType type, const char *symbol, GOutputStream *out)
208 {
209   guint n_interfaces;
210   guint i;
211   GType *interfaces;
212
213   escaped_printf (out, "  <class name=\"%s\" get-type=\"%s\"",
214                   g_type_name (type), symbol);
215   if (type != G_TYPE_OBJECT)
216     {
217       GString *parent_str;
218       GType parent;
219       gboolean first = TRUE;
220
221       parent = g_type_parent (type);
222       parent_str = g_string_new ("");
223       while (parent != G_TYPE_INVALID)
224         {
225           if (first)
226             first = FALSE;
227           else
228             g_string_append_c (parent_str, ',');
229           g_string_append (parent_str, g_type_name (parent));
230           parent = g_type_parent (parent);
231         }
232
233       escaped_printf (out, " parents=\"%s\"", parent_str->str);
234
235       g_string_free (parent_str, TRUE);
236     }
237
238   if (G_TYPE_IS_ABSTRACT (type))
239     escaped_printf (out, " abstract=\"1\"");
240   goutput_write (out, ">\n");
241
242   interfaces = g_type_interfaces (type, &n_interfaces);
243   for (i = 0; i < n_interfaces; i++)
244     {
245       GType itype = interfaces[i];
246       escaped_printf (out, "    <implements name=\"%s\"/>\n",
247                       g_type_name (itype));
248     }
249   dump_properties (type, out);
250   dump_signals (type, out);
251   goutput_write (out, "  </class>\n");
252 }
253
254 static void
255 dump_interface_type (GType type, const char *symbol, GOutputStream *out)
256 {
257   guint n_interfaces;
258   guint i;
259   GType *interfaces;
260
261   escaped_printf (out, "  <interface name=\"%s\" get-type=\"%s\">\n",
262                   g_type_name (type), symbol);
263
264   interfaces = g_type_interface_prerequisites (type, &n_interfaces);
265   for (i = 0; i < n_interfaces; i++)
266     {
267       GType itype = interfaces[i];
268       if (itype == G_TYPE_OBJECT)
269         {
270           /* Treat this as implicit for now; in theory GInterfaces are
271            * supported on things like GstMiniObject, but right now
272            * the introspection system only supports GObject.
273            * http://bugzilla.gnome.org/show_bug.cgi?id=559706
274            */
275           continue;
276         }
277       escaped_printf (out, "    <prerequisite name=\"%s\"/>\n",
278                       g_type_name (itype));
279     }
280   dump_properties (type, out);
281   dump_signals (type, out);
282   goutput_write (out, "  </interface>\n");
283 }
284
285 static void
286 dump_boxed_type (GType type, const char *symbol, GOutputStream *out)
287 {
288   escaped_printf (out, "  <boxed name=\"%s\" get-type=\"%s\"/>\n",
289                   g_type_name (type), symbol);
290 }
291
292 static void
293 dump_flags_type (GType type, const char *symbol, GOutputStream *out)
294 {
295   guint i;
296   GFlagsClass *klass;
297
298   klass = g_type_class_ref (type);
299   escaped_printf (out, "  <flags name=\"%s\" get-type=\"%s\">\n",
300                   g_type_name (type), symbol);
301
302   for (i = 0; i < klass->n_values; i++)
303     {
304       GFlagsValue *value = &(klass->values[i]);
305
306       escaped_printf (out, "    <member name=\"%s\" nick=\"%s\" value=\"%d\"/>\n",
307                       value->value_name, value->value_nick, value->value);
308     }
309   goutput_write (out, "  </flags>\n");
310 }
311
312 static void
313 dump_enum_type (GType type, const char *symbol, GOutputStream *out)
314 {
315   guint i;
316   GEnumClass *klass;
317
318   klass = g_type_class_ref (type);
319   escaped_printf (out, "  <enum name=\"%s\" get-type=\"%s\">\n",
320                   g_type_name (type), symbol);
321
322   for (i = 0; i < klass->n_values; i++)
323     {
324       GEnumValue *value = &(klass->values[i]);
325
326       escaped_printf (out, "    <member name=\"%s\" nick=\"%s\" value=\"%d\"/>\n",
327                       value->value_name, value->value_nick, value->value);
328     }
329   goutput_write (out, "  </enum>");
330 }
331
332 static void
333 dump_fundamental_type (GType type, const char *symbol, GOutputStream *out)
334 {
335   guint n_interfaces;
336   guint i;
337   GType *interfaces;
338   GString *parent_str;
339   GType parent;
340   gboolean first = TRUE;
341
342
343   escaped_printf (out, "  <fundamental name=\"%s\" get-type=\"%s\"",
344                   g_type_name (type), symbol);
345
346   if (G_TYPE_IS_ABSTRACT (type))
347     escaped_printf (out, " abstract=\"1\"");
348
349   if (G_TYPE_IS_INSTANTIATABLE (type))
350     escaped_printf (out, " instantiatable=\"1\"");
351
352   parent = g_type_parent (type);
353   parent_str = g_string_new ("");
354   while (parent != G_TYPE_INVALID)
355     {
356       if (first)
357         first = FALSE;
358       else
359         g_string_append_c (parent_str, ',');
360       if (!g_type_name (parent))
361         break;
362       g_string_append (parent_str, g_type_name (parent));
363       parent = g_type_parent (parent);
364     }
365
366   if (parent_str->len > 0)
367     escaped_printf (out, " parents=\"%s\"", parent_str->str);
368   g_string_free (parent_str, TRUE);
369
370   goutput_write (out, ">\n");
371
372   interfaces = g_type_interfaces (type, &n_interfaces);
373   for (i = 0; i < n_interfaces; i++)
374     {
375       GType itype = interfaces[i];
376       escaped_printf (out, "    <implements name=\"%s\"/>\n",
377                       g_type_name (itype));
378     }
379   goutput_write (out, "  </fundamental>\n");
380 }
381
382 static void
383 dump_type (GType type, const char *symbol, GOutputStream *out)
384 {
385   switch (g_type_fundamental (type))
386     {
387     case G_TYPE_OBJECT:
388       dump_object_type (type, symbol, out);
389       break;
390     case G_TYPE_INTERFACE:
391       dump_interface_type (type, symbol, out);
392       break;
393     case G_TYPE_BOXED:
394       dump_boxed_type (type, symbol, out);
395       break;
396     case G_TYPE_FLAGS:
397       dump_flags_type (type, symbol, out);
398       break;
399     case G_TYPE_ENUM:
400       dump_enum_type (type, symbol, out);
401       break;
402     case G_TYPE_POINTER:
403       /* GValue, etc.  Just skip them. */
404       break;
405     default:
406       dump_fundamental_type (type, symbol, out);
407       break;
408     }
409 }
410
411 static void
412 dump_error_quark (GQuark quark, const char *symbol, GOutputStream *out)
413 {
414   escaped_printf (out, "  <error-quark function=\"%s\" domain=\"%s\"/>\n",
415                   symbol, g_quark_to_string (quark));
416 }
417
418 /**
419  * g_irepository_dump:
420  * @arg: Comma-separated pair of input and output filenames
421  * @error: a %GError
422  *
423  * Argument specified is a comma-separated pair of filenames; i.e. of
424  * the form "input.txt,output.xml".  The input file should be a
425  * UTF-8 Unix-line-ending text file, with each line containing the name
426  * of a GType _get_type function.
427  *
428  * The output file should already exist, but be empty.  This function will
429  * overwrite its contents.
430  *
431  * Returns: %TRUE on success, %FALSE on error
432  */
433 #ifndef G_IREPOSITORY_COMPILATION
434 static gboolean
435 dump_irepository (const char *arg, GError **error) G_GNUC_UNUSED;
436 static gboolean
437 dump_irepository (const char *arg, GError **error)
438 #else
439 gboolean
440 g_irepository_dump (const char *arg, GError **error)
441 #endif
442 {
443   GHashTable *output_types;
444   char **args;
445   GFile *input_file;
446   GFile *output_file;
447   GFileInputStream *input;
448   GFileOutputStream *output;
449   GDataInputStream *in;
450   GModule *self;
451   gboolean caught_error = FALSE;
452
453   self = g_module_open (NULL, 0);
454   if (!self)
455     {
456       g_set_error (error,
457                    G_IO_ERROR,
458                    G_IO_ERROR_FAILED,
459                    "failed to open self: %s",
460                    g_module_error ());
461       return FALSE;
462     }
463
464   args = g_strsplit (arg, ",", 2);
465
466   input_file = g_file_new_for_path (args[0]);
467   output_file = g_file_new_for_path (args[1]);
468
469   input = g_file_read (input_file, NULL, error);
470   if (input == NULL)
471     return FALSE;
472
473   output = g_file_replace (output_file, NULL, FALSE, 0, NULL, error);
474   if (output == NULL)
475     {
476       g_input_stream_close (G_INPUT_STREAM (input), NULL, NULL);
477       return FALSE;
478     }
479
480   goutput_write (G_OUTPUT_STREAM (output), "<?xml version=\"1.0\"?>\n");
481   goutput_write (G_OUTPUT_STREAM (output), "<dump>\n");
482
483   output_types = g_hash_table_new (NULL, NULL);
484
485   in = g_data_input_stream_new (G_INPUT_STREAM (input));
486   g_object_unref (input);
487
488   while (TRUE)
489     {
490       gsize len;
491       char *line = g_data_input_stream_read_line (in, &len, NULL, NULL);
492       const char *function;
493
494       if (line == NULL || *line == '\0')
495         {
496           g_free (line);
497           break;
498         }
499
500       g_strchomp (line);
501
502       if (strncmp (line, "get-type:", strlen ("get-type:")) == 0)
503         {
504           GType type;
505
506           function = line + strlen ("get-type:");
507
508           type = invoke_get_type (self, function, error);
509
510           if (type == G_TYPE_INVALID)
511             {
512               g_printerr ("Invalid GType function: '%s'\n", function);
513               caught_error = TRUE;
514               g_free (line);
515               break;
516             }
517
518           if (g_hash_table_lookup (output_types, (gpointer) type))
519             goto next;
520           g_hash_table_insert (output_types, (gpointer) type, (gpointer) type);
521
522           dump_type (type, function, G_OUTPUT_STREAM (output));
523         }
524       else if (strncmp (line, "error-quark:", strlen ("error-quark:")) == 0)
525         {
526           GQuark quark;
527           function = line + strlen ("error-quark:");
528           quark = invoke_error_quark (self, function, error);
529
530           if (quark == 0)
531             {
532               g_printerr ("Invalid error quark function: '%s'\n", function);
533               caught_error = TRUE;
534               g_free (line);
535               break;
536             }
537
538           dump_error_quark (quark, function, G_OUTPUT_STREAM (output));
539         }
540
541
542     next:
543       g_free (line);
544     }
545
546   g_hash_table_destroy (output_types);
547
548   goutput_write (G_OUTPUT_STREAM (output), "</dump>\n");
549
550   {
551     GError **ioerror;
552     /* Avoid overwriting an earlier set error */
553     if (caught_error)
554       ioerror = NULL;
555     else
556       ioerror = error;
557     if (!g_input_stream_close (G_INPUT_STREAM (in), NULL, ioerror))
558       return FALSE;
559     if (!g_output_stream_close (G_OUTPUT_STREAM (output), NULL, ioerror))
560       return FALSE;
561   }
562
563   return !caught_error;
564 }