1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
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.1 of the License, or (at your option) any later version.
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.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 #include <glib-object.h>
28 #include <glib/gprintf.h>
31 static gchar *indent_inc = NULL;
32 static guint spacing = 1;
33 static FILE *f_out = NULL;
34 static GType root = 0;
35 static gboolean recursion = TRUE;
42 #define O_KEY_FILL "_"
45 show_nodes (GType type,
55 children = g_type_children (type, NULL);
57 g_fprintf (f_out, "%s%s%s%s",
59 sibling ? O_BRANCH : (type != root ? O_LLEAF : O_SPACE),
63 for (i = strlen (g_type_name (type)); i <= strlen (indent_inc); i++)
64 fputs (O_KEY_FILL, f_out);
68 if (children && recursion)
74 new_indent = g_strconcat (indent, O_VLINE, indent_inc, NULL);
76 new_indent = g_strconcat (indent, O_SPACE, indent_inc, NULL);
78 for (child = children; *child; child++)
79 show_nodes (child[0], child[1], new_indent);
88 help (const gchar *arg)
90 g_fprintf (stdout, "usage: gobject-query <qualifier> [-r <type>] [-{i|b} \"\"] [-s #] [-{h|x|y}]\n");
91 g_fprintf (stdout, " -r specify root type\n");
92 g_fprintf (stdout, " -n don't descend type tree\n");
93 g_fprintf (stdout, " -h show help\n");
94 g_fprintf (stdout, " -b specify indent string\n");
95 g_fprintf (stdout, " -i specify incremental indent string\n");
96 g_fprintf (stdout, " -s specify line spacing\n");
97 g_fprintf (stdout, "qualifiers:\n");
98 g_fprintf (stdout, " froots iterate over fundamental roots\n");
99 g_fprintf (stdout, " tree print type tree\n");
108 GLogLevelFlags fatal_mask;
109 gboolean gen_froots = 0;
110 gboolean gen_tree = 0;
112 const gchar *iindent = "";
116 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
117 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
118 g_log_set_always_fatal (fatal_mask);
120 root = G_TYPE_OBJECT;
122 for (i = 1; i < argc; i++)
124 if (strcmp ("-s", argv[i]) == 0)
128 spacing = atoi (argv[i]);
130 else if (strcmp ("-i", argv[i]) == 0)
142 indent_inc = g_new (gchar, n * strlen (O_SPACE) + 1);
147 strcpy (indent_inc, O_SPACE);
151 else if (strcmp ("-b", argv[i]) == 0)
157 else if (strcmp ("-r", argv[i]) == 0)
161 root = g_type_from_name (argv[i]);
163 else if (strcmp ("-n", argv[i]) == 0)
167 else if (strcmp ("froots", argv[i]) == 0)
171 else if (strcmp ("tree", argv[i]) == 0)
175 else if (strcmp ("--version", argv[i]) == 0)
177 g_print (PACKAGE_VERSION "\n");
180 else if (strcmp ("-h", argv[i]) == 0 ||
181 strcmp ("--help", argv[i]) == 0)
186 return help (argv[i]);
189 if (!gen_froots && !gen_tree)
190 return help ((argc > 0) ? argv[i-1] : NULL);
194 indent_inc = g_new (gchar, strlen (O_SPACE) + 1);
196 strcpy (indent_inc, O_SPACE);
200 show_nodes (root, 0, iindent);
204 for (i = 0; i <= G_TYPE_FUNDAMENTAL_MAX; i += G_TYPE_MAKE_FUNDAMENTAL (1))
206 const gchar *name = g_type_name (i);
207 GType sibling = i + G_TYPE_MAKE_FUNDAMENTAL (1);
209 if (sibling > G_TYPE_FUNDAMENTAL_MAX || g_type_name (sibling) == NULL)
213 show_nodes (i, sibling, iindent);