1/* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "config.h"
19
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24
25#include <glib-object.h>
26#include <glib/gprintf.h>
27
28
29static gchar *indent_inc = NULL;
30static guint spacing = 1;
31static FILE *f_out = NULL;
32static GType root = 0;
33static gboolean recursion = TRUE;
34
35#if 0
36# define O_SPACE "\\as"
37# define O_ESPACE " "
38# define O_BRANCH "\\aE"
39# define O_VLINE "\\al"
40# define O_LLEAF "\\aL"
41# define O_KEY_FILL "_"
42#else
43# define O_SPACE " "
44# define O_ESPACE ""
45# define O_BRANCH "+"
46# define O_VLINE "|"
47# define O_LLEAF "`"
48# define O_KEY_FILL "_"
49#endif
50
51static void
52show_nodes (GType type,
53 GType sibling,
54 const gchar *indent)
55{
56 GType *children;
57 guint i;
58
59 if (!type)
60 return;
61
62 children = g_type_children (type, NULL);
63
64 if (type != root)
65 for (i = 0; i < spacing; i++)
66 g_fprintf (file: f_out, format: "%s%s\n", indent, O_VLINE);
67
68 g_fprintf (file: f_out, format: "%s%s%s%s",
69 indent,
70 sibling ? O_BRANCH : (type != root ? O_LLEAF : O_SPACE),
71 O_ESPACE,
72 g_type_name (type));
73
74 for (i = strlen (s: g_type_name (type)); i <= strlen (s: indent_inc); i++)
75 fputs (O_KEY_FILL, stream: f_out);
76
77 fputc (c: '\n', stream: f_out);
78
79 if (children && recursion)
80 {
81 gchar *new_indent;
82 GType *child;
83
84 if (sibling)
85 new_indent = g_strconcat (string1: indent, O_VLINE, indent_inc, NULL);
86 else
87 new_indent = g_strconcat (string1: indent, O_SPACE, indent_inc, NULL);
88
89 for (child = children; *child; child++)
90 show_nodes (type: child[0], sibling: child[1], indent: new_indent);
91
92 g_free (mem: new_indent);
93 }
94
95 g_free (mem: children);
96}
97
98static gint
99help (gchar *arg)
100{
101 g_fprintf (stderr, format: "usage: gobject-query <qualifier> [-r <type>] [-{i|b} \"\"] [-s #] [-{h|x|y}]\n");
102 g_fprintf (stderr, format: " -r specify root type\n");
103 g_fprintf (stderr, format: " -n don't descend type tree\n");
104 g_fprintf (stderr, format: " -h guess what ;)\n");
105 g_fprintf (stderr, format: " -b specify indent string\n");
106 g_fprintf (stderr, format: " -i specify incremental indent string\n");
107 g_fprintf (stderr, format: " -s specify line spacing\n");
108 g_fprintf (stderr, format: "qualifiers:\n");
109 g_fprintf (stderr, format: " froots iterate over fundamental roots\n");
110 g_fprintf (stderr, format: " tree print type tree\n");
111
112 return arg != NULL;
113}
114
115int
116main (gint argc,
117 gchar *argv[])
118{
119 GLogLevelFlags fatal_mask;
120 gboolean gen_froots = 0;
121 gboolean gen_tree = 0;
122 gint i;
123 const gchar *iindent = "";
124
125 f_out = stdout;
126
127 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
128 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
129 g_log_set_always_fatal (fatal_mask);
130
131 root = G_TYPE_OBJECT;
132
133 for (i = 1; i < argc; i++)
134 {
135 if (strcmp (s1: "-s", s2: argv[i]) == 0)
136 {
137 i++;
138 if (i < argc)
139 spacing = atoi (nptr: argv[i]);
140 }
141 else if (strcmp (s1: "-i", s2: argv[i]) == 0)
142 {
143 i++;
144 if (i < argc)
145 {
146 char *p;
147 guint n;
148
149 p = argv[i];
150 while (*p)
151 p++;
152 n = p - argv[i];
153 indent_inc = g_new (gchar, n * strlen (O_SPACE) + 1);
154 *indent_inc = 0;
155 while (n)
156 {
157 n--;
158 strcpy (dest: indent_inc, O_SPACE);
159 }
160 }
161 }
162 else if (strcmp (s1: "-b", s2: argv[i]) == 0)
163 {
164 i++;
165 if (i < argc)
166 iindent = argv[i];
167 }
168 else if (strcmp (s1: "-r", s2: argv[i]) == 0)
169 {
170 i++;
171 if (i < argc)
172 root = g_type_from_name (name: argv[i]);
173 }
174 else if (strcmp (s1: "-n", s2: argv[i]) == 0)
175 {
176 recursion = FALSE;
177 }
178 else if (strcmp (s1: "froots", s2: argv[i]) == 0)
179 {
180 gen_froots = 1;
181 }
182 else if (strcmp (s1: "tree", s2: argv[i]) == 0)
183 {
184 gen_tree = 1;
185 }
186 else if (strcmp (s1: "-h", s2: argv[i]) == 0)
187 {
188 return help (NULL);
189 }
190 else if (strcmp (s1: "--help", s2: argv[i]) == 0)
191 {
192 return help (NULL);
193 }
194 else
195 return help (arg: argv[i]);
196 }
197
198 if (!gen_froots && !gen_tree)
199 return help (arg: argv[i-1]);
200
201 if (!indent_inc)
202 {
203 indent_inc = g_new (gchar, strlen (O_SPACE) + 1);
204 *indent_inc = 0;
205 strcpy (dest: indent_inc, O_SPACE);
206 }
207
208 if (gen_tree)
209 show_nodes (type: root, sibling: 0, indent: iindent);
210 if (gen_froots)
211 {
212 root = ~0;
213 for (i = 0; i <= G_TYPE_FUNDAMENTAL_MAX; i += G_TYPE_MAKE_FUNDAMENTAL (1))
214 {
215 const gchar *name = g_type_name (type: i);
216
217 if (name)
218 show_nodes (type: i, sibling: 0, indent: iindent);
219 }
220 }
221
222 return 0;
223}
224

source code of gtk/subprojects/glib/gobject/gobject-query.c