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