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