appres.c revision 9683b4e0
1/*
2 *
3Copyright 1989, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24 * *
25 * Author:  Jim Fulton, MIT X Consortium
26 */
27
28#ifdef HAVE_CONFIG_H
29# include "config.h"
30#endif
31
32#define _CONST_X_STRING
33
34#include <X11/Intrinsic.h>
35#include <stdio.h>
36#include <stdlib.h>
37
38#define NONAME "-AppResTest-"
39
40static char *ProgramName;
41
42static XrmQuark XrmQString;
43
44static void _X_NORETURN
45usage (int exitval)
46{
47    fprintf (stderr,
48	     "usage:  %s  [class [instance]] [-1] [-V] [toolkitoptions]\n"
49	     "-1      list resources only at the specified level\n"
50	     "-V      print command version and exit\n"
51             "The number of class and instance elements must be equal.\n",
52	     ProgramName);
53    exit (exitval);
54}
55
56/* stolen from Xlib Xrm.c */
57static void
58PrintBindingQuarkList(XrmBindingList bindings,
59				  XrmQuarkList quarks,
60				  FILE* stream)
61{
62    Bool	firstNameSeen;
63
64    for (firstNameSeen = False; *quarks; bindings++, quarks++) {
65	if (*bindings == XrmBindLoosely) {
66	    (void) fprintf(stream, "*");
67	} else if (firstNameSeen) {
68	    (void) fprintf(stream, ".");
69	}
70	firstNameSeen = True;
71	(void) fputs(XrmQuarkToString(*quarks), stream);
72    }
73}
74
75/* stolen from Xlib Xrm.c */
76/* output out the entry in correct file syntax */
77/*ARGSUSED*/
78static Bool
79DumpEntry(XrmDatabase *db,
80	  XrmBindingList bindings,
81	  XrmQuarkList quarks,
82	  XrmRepresentation *type,
83	  XrmValuePtr value,
84	  XPointer data)
85{
86    FILE			*stream = (FILE *)data;
87    register unsigned int	i;
88    register char		*s;
89    register char		c;
90
91    if (*type != XrmQString)
92	(void) putc('!', stream);
93    PrintBindingQuarkList(bindings, quarks, stream);
94    s = value->addr;
95    i = value->size;
96    if (*type == XrmQString) {
97	(void) fputs(":\t", stream);
98	if (i)
99	    i--;
100    }
101    else
102	fprintf(stream, "=%s:\t", XrmRepresentationToString(*type));
103    if (i && (*s == ' ' || *s == '\t'))
104	(void) putc('\\', stream); /* preserve leading whitespace */
105    while (i--) {
106	c = *s++;
107	if (c == '\n') {
108	    if (i)
109		(void) fputs("\\n\\\n", stream);
110	    else
111		(void) fputs("\\n", stream);
112	} else if (c == '\\')
113	    (void) fputs("\\\\", stream);
114	else if ((c < ' ' && c != '\t') ||
115		 ((unsigned char)c >= 0x7f && (unsigned char)c < 0xa0))
116	    (void) fprintf(stream, "\\%03o", (unsigned char)c);
117	else
118	    (void) putc(c, stream);
119    }
120    (void) putc('\n', stream);
121    return False;
122}
123
124int
125main (int argc, char *argv[])
126{
127    Widget toplevel;
128    String iname = NONAME, cname = NONAME;
129    XtAppContext xtcontext;
130    XrmName names[101];
131    XrmClass classes[101];
132    int i;
133    int mode = XrmEnumAllLevels;
134
135    ProgramName = argv[0];
136
137    /* Handle args that don't require opening a display */
138    for (int n = 1; n < argc; n++) {
139	const char *argn = argv[n];
140	/* accept single or double dash for -help & -version, but not -V */
141	if (argn[0] == '-' && argn[1] == '-') {
142	    argn++;
143	}
144	if (strcmp(argn, "-help") == 0) {
145	    usage(0);
146	}
147	if ((strcmp(argn, "-version") == 0) || (strcmp(argv[n], "-V") == 0)) {
148	    puts(PACKAGE_STRING);
149	    exit(0);
150	}
151    }
152
153    if (argc > 1 && argv[1][0] != '-') {
154	cname = argv[1];
155	if (argc > 2 && argv[2][0] != '-')
156	    iname = argv[2];
157    }
158
159    XrmStringToClassList(cname, classes);
160    XrmStringToNameList(iname, names);
161    for (i = 0; names[i]; i++)
162	;
163    if (!i || classes[i] || !classes[i-1])
164	usage(1);
165    argv[0] = XrmNameToString(names[0]);
166
167    toplevel = XtAppInitialize(&xtcontext, XrmClassToString(classes[0]),
168			       NULL, 0, &argc, argv, NULL, NULL, 0);
169
170    iname = NULL;
171    cname = NULL;
172    for (i = 1; i < argc; i++) {
173	if (!strcmp(argv[i], "-1"))
174	    mode = XrmEnumOneLevel;
175	else if (argv[i][0] == '-') {
176	    fprintf(stderr, "%s: unrecognized option '%s'\n",
177		    ProgramName, argv[i]);
178	    usage(1);
179	}
180	else if (!cname)
181	    cname = argv[i];
182	else if (!iname)
183	    iname = argv[i];
184	else
185	    usage(1);
186    }
187
188    if (!iname) {
189	XtGetApplicationNameAndClass(XtDisplay(toplevel), &iname, &cname);
190	names[0] = XrmStringToName(iname);
191    }
192
193    XrmQString = XrmPermStringToQuark("String");
194
195    XrmEnumerateDatabase(XtDatabase(XtDisplay(toplevel)),
196			 names, classes, mode,
197			 DumpEntry, (XPointer)stdout);
198
199    return (0);
200}
201