appres.c revision 291ca6f5
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 (void)
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 (1);
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    if (argc > 1 && argv[1][0] != '-') {
137	cname = argv[1];
138	if (argc > 2 && argv[2][0] != '-')
139	    iname = argv[2];
140    }
141
142    XrmStringToClassList(cname, classes);
143    XrmStringToNameList(iname, names);
144    for (i = 0; names[i]; i++)
145	;
146    if (!i || classes[i] || !classes[i-1]) usage ();
147    argv[0] = XrmNameToString(names[0]);
148
149    toplevel = XtAppInitialize(&xtcontext, XrmClassToString(classes[0]),
150			       NULL, 0, &argc, argv, NULL, NULL, 0);
151
152    iname = NULL;
153    cname = NULL;
154    for (i = 1; i < argc; i++) {
155	if (!strcmp(argv[i], "-1"))
156	    mode = XrmEnumOneLevel;
157	else if (!strcmp(argv[i], "-V")) {
158	    printf("%s\n", PACKAGE_STRING);
159	    exit(0);
160	}
161	else if (argv[i][0] == '-') {
162	    fprintf(stderr, "%s: unrecognized option '%s'\n",
163		    ProgramName, argv[i]);
164	    usage();
165	}
166	else if (!cname)
167	    cname = argv[i];
168	else if (!iname)
169	    iname = argv[i];
170	else
171	    usage();
172    }
173
174    if (!iname) {
175	XtGetApplicationNameAndClass(XtDisplay(toplevel), &iname, &cname);
176	names[0] = XrmStringToName(iname);
177    }
178
179    XrmQString = XrmPermStringToQuark("String");
180
181    XrmEnumerateDatabase(XtDatabase(XtDisplay(toplevel)),
182			 names, classes, mode,
183			 DumpEntry, (XPointer)stdout);
184
185    return (0);
186}
187