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