appres.c revision 27935385
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#include <X11/Intrinsic.h>
33#include <stdio.h>
34#include <stdlib.h>
35
36#define NONAME "-AppResTest-"
37
38static char *ProgramName;
39
40static XrmQuark XrmQString;
41
42static void _X_NORETURN
43usage (void)
44{
45    fprintf (stderr,
46	     "usage:  %s  [class [instance]] [-1] [-V] [toolkitoptions]\n"
47	     "-1      list resources only at the specified level\n"
48	     "-V      print command version and exit\n"
49             "The number of class and instance elements must be equal.\n",
50	     ProgramName);
51    exit (1);
52}
53
54/* stolen from Xlib Xrm.c */
55static void
56PrintBindingQuarkList(XrmBindingList bindings,
57				  XrmQuarkList quarks,
58				  FILE* stream)
59{
60    Bool	firstNameSeen;
61
62    for (firstNameSeen = False; *quarks; bindings++, quarks++) {
63	if (*bindings == XrmBindLoosely) {
64	    (void) fprintf(stream, "*");
65	} else if (firstNameSeen) {
66	    (void) fprintf(stream, ".");
67	}
68	firstNameSeen = True;
69	(void) fputs(XrmQuarkToString(*quarks), stream);
70    }
71}
72
73/* stolen from Xlib Xrm.c */
74/* output out the entry in correct file syntax */
75/*ARGSUSED*/
76static Bool
77DumpEntry(XrmDatabase *db,
78	  XrmBindingList bindings,
79	  XrmQuarkList quarks,
80	  XrmRepresentation *type,
81	  XrmValuePtr value,
82	  XPointer data)
83{
84    FILE			*stream = (FILE *)data;
85    register unsigned int	i;
86    register char		*s;
87    register char		c;
88
89    if (*type != XrmQString)
90	(void) putc('!', stream);
91    PrintBindingQuarkList(bindings, quarks, stream);
92    s = value->addr;
93    i = value->size;
94    if (*type == XrmQString) {
95	(void) fputs(":\t", stream);
96	if (i)
97	    i--;
98    }
99    else
100	fprintf(stream, "=%s:\t", XrmRepresentationToString(*type));
101    if (i && (*s == ' ' || *s == '\t'))
102	(void) putc('\\', stream); /* preserve leading whitespace */
103    while (i--) {
104	c = *s++;
105	if (c == '\n') {
106	    if (i)
107		(void) fputs("\\n\\\n", stream);
108	    else
109		(void) fputs("\\n", stream);
110	} else if (c == '\\')
111	    (void) fputs("\\\\", stream);
112	else if ((c < ' ' && c != '\t') ||
113		 ((unsigned char)c >= 0x7f && (unsigned char)c < 0xa0))
114	    (void) fprintf(stream, "\\%03o", (unsigned char)c);
115	else
116	    (void) putc(c, stream);
117    }
118    (void) putc('\n', stream);
119    return False;
120}
121
122int
123main (int argc, char *argv[])
124{
125    Widget toplevel;
126    char *iname = NONAME, *cname = NONAME;
127    XtAppContext xtcontext;
128    XrmName names[101];
129    XrmClass classes[101];
130    int i;
131    int mode = XrmEnumAllLevels;
132
133    ProgramName = argv[0];
134    if (argc > 1 && argv[1][0] != '-') {
135	cname = argv[1];
136	if (argc > 2 && argv[2][0] != '-')
137	    iname = argv[2];
138    }
139
140    XrmStringToClassList(cname, classes);
141    XrmStringToNameList(iname, names);
142    for (i = 0; names[i]; i++)
143	;
144    if (!i || classes[i] || !classes[i-1]) usage ();
145    argv[0] = XrmNameToString(names[0]);
146
147    toplevel = XtAppInitialize(&xtcontext, XrmClassToString(classes[0]),
148			       NULL, 0, &argc, argv, NULL, NULL, 0);
149
150    iname = NULL;
151    cname = NULL;
152    for (i = 1; i < argc; i++) {
153	if (!strcmp(argv[i], "-1"))
154	    mode = XrmEnumOneLevel;
155	else if (!strcmp(argv[i], "-V")) {
156	    printf("%s\n", PACKAGE_STRING);
157	    exit(0);
158	}
159	else if (argv[i][0] == '-') {
160	    fprintf(stderr, "%s: unrecognized option '%s'\n",
161		    ProgramName, argv[i]);
162	    usage();
163	}
164	else if (!cname)
165	    cname = argv[i];
166	else if (!iname)
167	    iname = argv[i];
168	else
169	    usage();
170    }
171
172    if (!iname) {
173	XtGetApplicationNameAndClass(XtDisplay(toplevel), &iname, &cname);
174	names[0] = XrmStringToName(iname);
175    }
176
177    XrmQString = XrmPermStringToQuark("String");
178
179    XrmEnumerateDatabase(XtDatabase(XtDisplay(toplevel)),
180			 names, classes, mode,
181			 DumpEntry, (XPointer)stdout);
182
183    return (0);
184}
185