summaryrefslogtreecommitdiff
path: root/listres.c
blob: 7f7a0c53756ba8be0c8fe34337062c18beecf1be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * $XConsortium: listres.c,v 1.32 94/04/17 20:43:22 dave Exp $
 *
 * 
Copyright (c) 1989  X Consortium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.
 * *
 * Author:  Jim Fulton, MIT X Consortium
 */
/* $XFree86: xc/programs/listres/listres.c,v 1.3 2000/02/17 14:00:32 dawes Exp $ */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xos.h>
#include <X11/StringDefs.h>
#include <X11/IntrinsicP.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/Core.h>
#include <X11/Xmu/CharSet.h>
#include <X11/Xmu/WidgetNode.h>
#include <X11/Xaw/AllWidgets.h>

#define widget_list XawWidgetArray  /* or motif or ol or ... */
#define nwidgets XawWidgetCount


static XrmOptionDescRec Options[] = {
  { "-top", "*topObject", XrmoptionSepArg, (caddr_t) NULL },
  { "-format", "*resourceFormat", XrmoptionSepArg, (caddr_t) NULL },
  { "-tree", "*showTree", XrmoptionNoArg, (caddr_t) "on" },
  { "-nosuper", "*showSuper", XrmoptionNoArg, (caddr_t) "off" },
  { "-variable", "*showVariable", XrmoptionNoArg, (caddr_t) "on" },
};

typedef struct {
    Boolean show_tree;
    Boolean show_all;
    Boolean show_variable;
    Boolean show_superclass;
    char *top_object;
    char *format;
  } OptionsRec;

static OptionsRec options;

#define Offset(field) XtOffsetOf(OptionsRec, field)

static XtResource Resources[] = {
  { "showTree", "ShowTree", XtRBoolean, sizeof(Boolean),
      Offset(show_tree), XtRImmediate, (XtPointer) FALSE },
  { "showSuper", "ShowSuper", XtRBoolean, sizeof(Boolean),
      Offset(show_superclass), XtRImmediate, (caddr_t) TRUE },
  { "showVariable", "ShowVariable", XtRBoolean, sizeof(Boolean),
      Offset(show_variable), XtRImmediate, (caddr_t) FALSE },
  { "topObject", "TopObject", XtRString, sizeof(char *),
      Offset(top_object), XtRString, (caddr_t) "core" },
  { "resourceFormat", "ResourceFormat", XtRString, sizeof(char *),
      Offset(format), XtRString, (caddr_t) " %-16s %20s  %-20s  %s" },
};

#undef Offset

static const char *ProgramName;

static void
usage (int exitval)
{
    fprintf(stderr, "usage:  %s [-options...]\n%s\n", ProgramName,
	    "\nwhere options include:\n"
	    "    -all             list all known widget and object classes\n"
	    "    -tree            list all widgets and objects in a tree\n"
	    "    -nosuper         do not print superclass resources\n"
	    "    -variable        show variable name instead of class name\n"
	    "    -top name        object to be top of tree\n"
	    "    -format string   printf format for instance, class, type\n"
	    "    -help            print this message and exit\n"
	    "    -version         print version info and exit\n"
        );
    exit (exitval);
}

static void print_tree_level (register XmuWidgetNode *wn, register int level)
{
    register int i;

    if (!wn) return;

    for (i = 0; i < level; i++) {
	putchar (' '); putchar (' '); 
    }
    printf ("%d:  %s/%s\n", level, wn->label, XmuWnClassname(wn));
    print_tree_level (wn->children, level + 1);
    print_tree_level (wn->siblings, level);
}

static void tree_known_widgets (void)
{
    register int i;
    register XmuWidgetNode *wn;

    for (i = 0, wn = widget_list; i < nwidgets; i++, wn++) {
	if (!wn->superclass) {		/* list all rooted objects */
	    print_tree_level (wn, 0);
	}
    }
}


/*
 * print_classname - print out the superclass-to-subclass hierarchy of names
 * in the form super\sub\sub....
 */
static int print_classname (XmuWidgetNode *node, XmuWidgetNode *topnode,
			    int level, Bool showvar)
{
    int retval;

    if (node && node != topnode) {
	retval = print_classname (node->superclass, topnode, level + 1,
				  showvar);
    } else {
	retval = level - 1;
    }
    if (node)
      printf ("%s%s", showvar ? node->label : XmuWnClassname(node),
	      level ? "\\" : "");

    return retval;
}

static void list_known_widgets (void)
{
    int i;
    XmuWidgetNode *wn;
    int width = 0;

    for (i = 0, wn = widget_list; i < nwidgets; i++, wn++) {
	int l = strlen (wn->label);
	if (l > width) width = l;
    }
    for (i = 0, wn = widget_list; i < nwidgets; i++, wn++) {
	printf ("%-*s  ", width, wn->label);
	print_classname (wn, (XmuWidgetNode *) NULL, 0, False);
	putchar ('\n');
    }
}

/* ARGSUSED */
static void print_resources (XmuWidgetNode *node, const char *format,
			     XmuWidgetNode *topnode, Bool showsuper,
			     Bool showvar)
{
    Cardinal i;
    XtResourceList res = node->resources;
    XmuWidgetNode **wn = node->resourcewn;

    for (i = 0; i < node->nresources; i++, res++, wn++) {
	if (!showsuper && *wn != node) continue;
	printf (format, showvar ? (*wn)->label : XmuWnClassname(*wn),
		res->resource_name, res->resource_class, res->resource_type);
	putchar ('\n');
    }
    if (node->nconstraints > 0) {
	printf (format, "----", "----", "----", "----");
	putchar ('\n');
    }
    res = node->constraints;
    wn = node->constraintwn;
    for (i = 0; i < node->nconstraints; i++, res++, wn++) {
	if (!showsuper && *wn != node) continue;
	printf (format, showvar ? (*wn)->label : XmuWnClassname(*wn),
		res->resource_name, res->resource_class, res->resource_type);
	putchar ('\n');
    }
    return;
}


/*
 * list_resources - display resources of a widget, identifying class from
 * which they come
 */
static void
list_resources (XmuWidgetNode *node, const char *format,
		XmuWidgetNode *topnode, Widget toplevel,
		Bool showsuper, Bool showvar)
{
    static Bool first = True;

    XmuWnFetchResources (node, toplevel, topnode);
    if (first) {
	printf (format, showvar ? "Variable" : "WidgetClass",
		"Instance", "Class", "Type");
	putchar ('\n');
	printf (format, showvar ? "--------" : "-----------",
		"--------", "-----", "----");
	putchar ('\n');
	first = False;
    }
    printf ("%s:  ", node->label);
    print_classname (node, topnode, 0, showvar);
    putchar ('\n');
    print_resources (node, format, topnode, showsuper, showvar);
    putchar ('\n');
}


int
main (int argc, char **argv)
{
    int i;
    XtAppContext appcon;
    XmuWidgetNode *topnode;
    Widget toplevel, container;

    ProgramName = argv[0];

    XtSetLanguageProc(NULL, (XtLanguageProc) NULL, NULL);

    /* Handle args that don't require opening a display */
    for (int n = 1; n < argc; n++) {
	const char *argn = argv[n];
	/* accept single or double dash for -help & -version */
	if (argn[0] == '-' && argn[1] == '-') {
	    argn++;
	}
	if (strcmp(argn, "-help") == 0) {
	    usage(0);
	}
	if (strcmp(argn, "-version") == 0) {
	    puts(PACKAGE_STRING);
	    exit(0);
	}
    }

    toplevel = XtAppInitialize (&appcon, "Listres", Options, XtNumber(Options),
				&argc, argv, NULL, NULL, 0);
    container = XtCreateWidget ("dummy", widgetClass, toplevel, NULL, ZERO);

    XtGetApplicationResources (toplevel, (caddr_t) &options,
			       Resources, XtNumber(Resources), NULL, ZERO);
    XmuWnInitializeNodes (widget_list, nwidgets);
    if (argc == 1) {
	if (options.show_tree) {
	    tree_known_widgets();
	} else {
	    list_known_widgets();
	}
	exit (0);
    }

    topnode = XmuWnNameToNode (widget_list, nwidgets, options.top_object);
    argc--, argv++;			/* skip command */

    if (argc > 0 && argv[0][0] == '-') {
	int len = strlen (argv[0]);
	if (len >= 2 && strncmp(argv[0], "-all", len) == 0) {
	    XmuWidgetNode *wn;
	    for (i = 0, wn = widget_list; i < nwidgets; i++, wn++) {
		list_resources (wn, options.format, topnode, container,
				(Bool) options.show_superclass,
				(Bool) options.show_variable);
	    }
	} else {
	  fprintf(stderr, "Unknown argument: %s\n", argv[0]);
	  usage(1);
	}
    } else {
	for (; argc > 0; argc--, argv++) {
	    XmuWidgetNode *node;

	    if (argv[0][0] == '-') {
		fprintf(stderr, "Unknown argument: %s\n", argv[0]);
		usage(1);
	    }
	    node = XmuWnNameToNode (widget_list, nwidgets, *argv);
	    if (!node) {
		fprintf (stderr, "%s:  unable to find widget \"%s\"\n",
			 ProgramName, *argv);
		continue;
	    }
	    list_resources (node, options.format, topnode, container,
			    (Bool) options.show_superclass,
			    (Bool) options.show_variable);
	}
    }
    exit (0);
}