2 * xrdb - X resource manager database utility
8 * DIGITAL EQUIPMENT CORPORATION
9 * MAYNARD, MASSACHUSETTS
10 * MASSACHUSETTS INSTITUTE OF TECHNOLOGY
11 * CAMBRIDGE, MASSACHUSETTS
12 * ALL RIGHTS RESERVED.
14 * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
15 * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
16 * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
17 * ANY PURPOSE. IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
19 * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT RIGHTS,
20 * APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN ADDITION TO THAT
24 * Permission to use, copy, modify, and distribute this software and its
25 * documentation for any purpose and without fee is hereby granted, provided
26 * that the above copyright notice appear in all copies and that both that
27 * copyright notice and this permission notice appear in supporting
28 * documentation, and that the name of Digital Equipment Corporation not be
29 * used in advertising or publicity pertaining to distribution of the software
30 * without specific, written prior permission.
34 * this program is used to load, or dump the resource manager database
37 * Original Author: Jim Gettys, August 28, 1987
38 * Extensively Modified: Phil Karlton, January 5, 1987
39 * Modified a Bunch More: Bob Scheifler, February, 1991
47 #include <X11/Xutil.h>
48 #include <X11/Xatom.h>
50 #include <X11/Xmu/SysUtil.h>
57 #ifdef NEED_SYS_PARAM_H
58 # include <sys/param.h> /* defines MAXHOSTNAMELEN on BSD & Linux */
62 # include <netdb.h> /* defines MAXHOSTNAMELEN on Solaris */
65 #define SCREEN_RESOURCES "SCREEN_RESOURCES"
69 /* expected to be in path */
72 #define CPP "/usr/lib/cpp"
73 #endif /* __UNIXOS2__ */
76 #define INIT_BUFFER_SIZE 10000
77 #define INIT_ENTRY_SIZE 500
92 #define RESOURCE_PROPERTY_NAME "RESOURCE_MANAGER"
93 #define BACKUP_SUFFIX ".bak" /* for editting */
95 typedef struct _Entry {
100 typedef struct _Buffer {
104 typedef struct _Entries {
109 /* dynamically allocated strings */
110 #define CHUNK_SIZE 4096
111 typedef struct _String {
116 static char *ProgramName;
117 static Bool quiet = False;
118 static char tmpname[32];
119 static char *filename = NULL;
121 static Bool need_real_defines = False;
122 static char tmpname2[32];
124 static char tmpname3[32];
127 static int oper = OPLOAD;
128 static char *editFile = NULL;
129 static const char *cpp_program = NULL;
130 static const char* const cpp_locations[] = { CPP };
131 static char *backup_suffix = BACKUP_SUFFIX;
132 static Bool dont_execute = False;
133 static String defines;
134 static int defines_base;
135 #define MAX_CMD_DEFINES 512
136 static char *cmd_defines[MAX_CMD_DEFINES];
137 static int num_cmd_defines = 0;
138 static String includes;
140 static Buffer buffer;
141 static Entries newDB;
143 static void fatal(char *, ...);
144 static void addstring ( String *arg, const char *s );
145 static void addescapedstring ( String *arg, const char *s );
146 static void addtokstring ( String *arg, const char *s );
147 static void FormatEntries ( Buffer *buffer, Entries *entries );
148 static void StoreProperty ( Display *dpy, Window root, Atom res_prop );
149 static void Process ( int scrno, Bool doScreen, Bool execute );
150 static void ShuffleEntries ( Entries *db, Entries *dbs, int num );
151 static void ReProcess ( int scrno, Bool doScreen );
153 #ifndef HAVE_ASPRINTF
154 /* sprintf variant found in newer libc's which allocates string to print to */
155 static int _X_ATTRIBUTE_PRINTF(2,3)
156 asprintf(char ** ret, const char *format, ...)
162 va_start(ap, format);
163 len = vsnprintf(buf, sizeof(buf), format, ap);
169 if (len < sizeof(buf))
175 *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */
178 va_start(ap, format);
179 len = vsnprintf(*ret, len + 1, format, ap);
193 #endif /* HAVE_ASPRINTF */
196 InitBuffer(Buffer *b)
198 b->room = INIT_BUFFER_SIZE;
200 b->buff = (char *)malloc(INIT_BUFFER_SIZE*sizeof(char));
205 FreeBuffer(Buffer *b)
212 AppendToBuffer(Buffer *b, char *str, int len)
214 while (b->used + len > b->room) {
215 b->buff = (char *)realloc(b->buff, 2*b->room*(sizeof(char)));
218 strncpy(b->buff + b->used, str, len);
223 InitEntries(Entries *e)
225 e->room = INIT_ENTRY_SIZE;
227 e->entry = (Entry *)malloc(INIT_ENTRY_SIZE*sizeof(Entry));
231 FreeEntries(Entries *e)
235 for (i = 0; i < e->used; i++) {
236 if (e->entry[i].usable) {
237 free(e->entry[i].tag);
238 free(e->entry[i].value);
241 free((char *)e->entry);
245 AddEntry(Entries *e, Entry *entry)
249 for (n = 0; n < e->used; n++) {
250 if (!strcmp(e->entry[n].tag, entry->tag)) {
251 /* overwrite old entry */
252 if (e->entry[n].lineno && !quiet) {
254 "%s: \"%s\" on line %d overrides entry on line %d\n",
255 ProgramName, entry->tag, entry->lineno,
258 free(e->entry[n].tag);
259 free(e->entry[n].value);
260 entry->usable = True;
261 e->entry[n] = *entry;
262 return; /* ok to leave, now there's only one of each tag in db */
266 if (e->used == e->room) {
267 e->entry = (Entry *)realloc((char *)e->entry,
268 2*e->room*(sizeof(Entry)));
271 entry->usable = True;
272 e->entry[e->used++] = *entry;
277 CompareEntries(const void *e1, const void *e2)
279 return strcmp(((Entry *)e1)->tag, ((Entry *)e2)->tag);
283 AppendEntryToBuffer(Buffer *buffer, Entry *entry)
285 AppendToBuffer(buffer, entry->tag, strlen(entry->tag));
286 AppendToBuffer(buffer, ":\t", 2);
287 AppendToBuffer(buffer, entry->value, strlen(entry->value));
288 AppendToBuffer(buffer, "\n", 1);
292 * Return the position of the first unescaped occurrence of dest in string.
293 * If lines is non-null, return the number of newlines skipped over.
296 FindFirst(char *string, char dest, int *lines)
303 if (*string == '\\') {
304 if (*++string == '\0')
306 } else if (*string == dest)
308 if (*string == '\n' && lines)
315 GetEntries(Entries *entries, Buffer *buff, int bequiet)
317 register char *line, *colon, *temp, *str;
325 for ( ; str < buff->buff + buff->used;
326 str = line + 1, lineno += lines_skipped) {
327 line = FindFirst(str, '\n', &lines_skipped);
330 line = buff->buff + buff->used;
335 if (!bequiet && *str == '#') {
337 if (sscanf (str, "# %d", &dummy) == 1 ||
338 sscanf (str, "# line %d", &dummy) == 1)
343 *temp && *temp != '\n' && isascii(*temp) && isspace(*temp);
345 if (!*temp || *temp == '\n') continue;
347 colon = FindFirst(str, ':', NULL);
348 if (!colon || colon > line) {
349 if (!bequiet && !quiet)
351 "%s: colon missing on line %d, ignoring line\n",
352 ProgramName, lineno);
356 /* strip leading and trailing blanks from name and store result */
357 while (*str == ' ' || *str == '\t')
359 length = colon - str;
360 while (length && (str[length-1] == ' ' || str[length-1] == '\t'))
362 temp = (char *)malloc(length + 1);
363 strncpy(temp, str, length);
367 /* strip leading and trailing blanks from value and store result */
369 while (*colon == ' ' || *colon == '\t')
371 length = line - colon;
372 temp = (char *)malloc(length + 1);
373 strncpy(temp, colon, length);
376 entry.lineno = bequiet ? 0 : lineno;
378 AddEntry(entries, &entry);
383 GetEntriesString(Entries *entries, char *str)
389 buff.used = strlen(str);
390 GetEntries(entries, &buff, 1);
395 ReadFile(Buffer *buffer, FILE *input)
397 char buf[BUFSIZ + 1];
401 while (!feof(input) && (bytes = fread(buf, 1, BUFSIZ, input)) > 0) {
405 for (p = buf; p = strchr(p, '\r'); ) {
406 if (p[-1] == '\\' && p[1] == '\n') {
408 strcpy(p - 1, p + 2);
412 AppendToBuffer(buffer, buf, bytes);
414 AppendToBuffer(buffer, "", 1);
418 AddDef(String *buff, char *title, char *value)
421 if (need_real_defines) {
422 addstring(buff, "\n#define ");
423 addstring(buff, title);
424 if (value && (value[0] != '\0')) {
425 addstring(buff, " ");
426 addstring(buff, value);
432 if (oper == OPSYMBOLS)
433 addstring(buff, "\n-D");
435 addstring(buff, " -D");
437 addstring(buff, "-D");
438 addtokstring(buff, title);
439 if (value && (value[0] != '\0')) {
440 addstring(buff, "=");
441 addescapedstring(buff, value);
446 AddSimpleDef(String *buff, char *title)
448 AddDef(buff, title, (char *)NULL);
452 AddDefQ(String *buff, char *title, char *value)
455 if (need_real_defines)
456 AddDef(buff, title, value);
459 if (value && (value[0] != '\0')) {
460 AddSimpleDef(buff, title);
461 addstring(buff, "=\"");
462 addescapedstring(buff, value);
463 addstring(buff, "\"");
465 AddDef(buff, title, NULL);
469 AddNum(String *buff, char *title, int value)
472 snprintf(num, sizeof(num), "%d", value);
473 AddDef(buff, title, num);
477 AddDefTok(String *buff, char *prefix, char *title)
481 snprintf(name, sizeof(name), "%s%s", prefix, title);
482 AddSimpleDef(buff, name);
486 AddDefHostname(String *buff, char *title, char *value)
492 strncpy (name, value, sizeof(name)-1);
493 name[sizeof(name)-1] = '\0';
494 for (s = name; (c = *s); s++) {
495 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '.' && c != ':' && c != '-')
498 AddDef(buff, title, name);
502 AddUndef(String *buff, char *title)
505 if (need_real_defines) {
506 addstring(buff, "\n#undef ");
507 addstring(buff, title);
512 if (oper == OPSYMBOLS)
513 addstring(buff, "\n-U");
515 addstring(buff, " -U");
517 addstring(buff, "-U");
518 addtokstring(buff, title);
522 DoCmdDefines(String *buff)
527 for (i = 0; i < num_cmd_defines; i++) {
528 arg = cmd_defines[i];
530 val = strchr(arg, '=');
533 AddDefQ(buff, arg + 2, val + 1);
536 AddSimpleDef(buff, arg + 2);
538 AddUndef(buff, arg + 2);
543 Resolution(int pixels, int mm)
548 return ((pixels * 100000 / mm) + 50) / 100;
553 DoDisplayDefines(Display *display, String *defs, char *host)
555 #ifndef MAXHOSTNAMELEN
556 #define MAXHOSTNAMELEN 255
558 char client[MAXHOSTNAMELEN], server[MAXHOSTNAMELEN], *colon;
562 XmuGetHostname(client, MAXHOSTNAMELEN);
563 strncpy(server, XDisplayName(host), sizeof(server));
564 server[sizeof(server) - 1] = '\0';
565 /* search for final colon to skip over any embedded colons in IPv6
566 numeric address forms */
567 colon = strrchr(server, ':');
570 /* remove extra colon if there are exactly two, since it indicates
571 DECnet. Three colons is an IPv6 address ending in :: though. */
572 if ((colon > server) && (*(colon-1) == ':') &&
573 ( ((colon - 1) == server) || (*(colon-2) != ':') ) ) {
577 sscanf(colon, "%d", &n);
579 if (!*server || !strcmp(server, "unix") || !strcmp(server, "localhost"))
580 strcpy(server, client);
581 AddDefHostname(defs, "HOST", server); /* R3 compatibility */
582 AddDefHostname(defs, "SERVERHOST", server);
583 AddDefTok(defs, "SRVR_", server);
584 AddNum(defs, "DISPLAY_NUM", n);
585 AddDefHostname(defs, "CLIENTHOST", client);
586 AddDefTok(defs, "CLNT_", client);
587 AddNum(defs, "VERSION", ProtocolVersion(display));
588 AddNum(defs, "REVISION", ProtocolRevision(display));
589 AddDefQ(defs, "VENDOR", ServerVendor(display));
590 AddDefTok(defs, "VNDR_", ServerVendor(display));
591 AddNum(defs, "RELEASE", VendorRelease(display));
592 AddNum(defs, "NUM_SCREENS", ScreenCount(display));
593 extnames = XListExtensions(display, &n);
595 AddDefTok(defs, "EXT_", extnames[n]);
596 XFreeExtensionList(extnames);
599 static char *ClassNames[] = {
609 DoScreenDefines(Display *display, int scrno, String *defs)
613 XVisualInfo vinfo, *vinfos;
617 screen = ScreenOfDisplay(display, scrno);
618 visual = DefaultVisualOfScreen(screen);
619 vinfo.screen = scrno;
620 vinfos = XGetVisualInfo(display, VisualScreenMask, &vinfo, &nv);
621 AddNum(defs, "SCREEN_NUM", scrno);
622 AddNum(defs, "WIDTH", screen->width);
623 AddNum(defs, "HEIGHT", screen->height);
624 AddNum(defs, "X_RESOLUTION", Resolution(screen->width,screen->mwidth));
625 AddNum(defs, "Y_RESOLUTION", Resolution(screen->height,screen->mheight));
626 AddNum(defs, "PLANES", DisplayPlanes(display, scrno));
627 AddNum(defs, "BITS_PER_RGB", visual->bits_per_rgb);
628 AddDefQ(defs, "CLASS", ClassNames[visual->class]);
629 snprintf(name, sizeof(name), "CLASS_%s", ClassNames[visual->class]);
630 AddNum(defs, name, (int)visual->visualid);
631 switch(visual->class) {
636 AddSimpleDef(defs, "COLOR");
639 for (i = 0; i < nv; i++) {
640 for (j = i; --j >= 0; ) {
641 if (vinfos[j].class == vinfos[i].class &&
642 vinfos[j].depth == vinfos[i].depth)
646 snprintf(name, sizeof(name), "CLASS_%s_%d",
647 ClassNames[vinfos[i].class], vinfos[i].depth);
648 AddNum(defs, name, (int)vinfos[i].visualid);
651 XFree((char *)vinfos);
655 FindEntry(Entries *db, Buffer *b)
662 entry.usable = False;
667 phoney.entry = &entry;
668 GetEntries(&phoney, b, 1);
671 for (i = 0; i < db->used; i++) {
675 if (strcmp(e->tag, entry.tag))
678 if (strcmp(e->value, entry.value))
686 EditFile(Entries *new, FILE *in, FILE *out)
699 if (!fgets(buff, BUFSIZ, in))
701 AppendToBuffer(&b, buff, strlen(buff));
702 c = &b.buff[b.used - 1];
703 if ((*(c--) == '\n') && (b.used == 1 || *c != '\\'))
706 if ((e = FindEntry(new, &b)))
707 fprintf(out, "%s:\t%s\n", e->tag, e->value);
709 fwrite(b.buff, 1, b.used, out);
712 for (i = 0; i < new->used; i++) {
715 fprintf(out, "%s:\t%s\n", e->tag, e->value);
723 "usage: %s [-options ...] [filename]\n\n"
724 "where options include:\n"
725 " -display host:dpy display to use\n"
726 " -all do all resources [default]\n"
727 " -global do screen-independent resources\n"
728 " -screen do screen-specific resources for one screen\n"
729 " -screens do screen-specific resources for all screens\n"
730 " -n show but don't do changes\n"
731 " -cpp filename preprocessor to use [%s]\n"
732 " -nocpp do not use a preprocessor\n"
733 " -query query resources\n"
734 " -load load resources from file [default]\n"
735 " -override add in resources from file\n"
736 " -merge merge resources from file & sort\n"
737 " -edit filename edit resources into file\n"
738 " -backup string backup suffix for -edit [%s]\n"
739 " -symbols show preprocessor symbols\n"
740 " -remove remove resources\n"
741 " -retain avoid server reset (avoid using this)\n"
742 " -quiet don't warn about duplicates\n"
743 " -Dname[=value], -Uname, -Idirectory passed to preprocessor\n"
745 "A - or no input filename represents stdin.\n",
746 ProgramName, CPP, BACKUP_SUFFIX);
751 * The following is a hack until XrmParseCommand is ready. It determines
752 * whether or not the given string is an abbreviation of the arg.
756 isabbreviation(char *arg, char *s, int minslen)
762 if (!strcmp (arg, s)) return (True);
764 arglen = strlen (arg);
767 /* too long or too short */
768 if (slen >= arglen || slen < minslen) return (False);
771 if (strncmp (arg, s, slen) == 0) return (True);
778 addstring(String *arg, const char *s)
780 if(arg->used + strlen(s) + 1 >= arg->room) {
782 arg->val = (char *)realloc(arg->val, arg->room + CHUNK_SIZE);
784 arg->val = (char *)malloc(arg->room + CHUNK_SIZE);
786 fatal("%s: Not enough memory\n", ProgramName);
787 arg->room += CHUNK_SIZE;
793 arg->used += strlen(s);
797 addescapedstring(String *arg, const char *s)
801 for (c = copy; *s && c < ©[sizeof(copy)-1]; s++) {
803 case '"': case '\'': case '`':
812 addstring (arg, copy);
816 addtokstring(String *arg, const char *s)
820 for (c = copy; *s && c < ©[sizeof(copy)-1]; s++) {
821 if (!isalpha(*s) && !isdigit(*s) && *s != '_')
827 addstring (arg, copy);
832 main(int argc, char *argv[])
835 char *displayname = NULL;
836 int whichResources = RALL;
841 ProgramName = argv[0];
843 defines.room = defines.used = includes.room = includes.used = 0;
845 /* initialize the includes String struct */
846 addstring(&includes, "");
848 /* Pick the default cpp to use. This needs to be done before
849 * we parse the command line in order to honor -nocpp which sets
852 if (cpp_program == NULL) {
853 int number_of_elements
854 = (sizeof cpp_locations) / (sizeof cpp_locations[0]);
857 for (j = 0; j < number_of_elements; j++) {
858 if (access(cpp_locations[j], X_OK) == 0) {
859 cpp_program = cpp_locations[j];
865 /* needs to be replaced with XrmParseCommand */
867 for (i = 1; i < argc; i++) {
871 if (arg[1] == '\0') {
874 } else if (isabbreviation ("-help", arg, 2)) {
877 } else if (isabbreviation ("-display", arg, 2)) {
878 if (++i >= argc) Syntax ();
879 displayname = argv[i];
881 } else if (isabbreviation ("-geometry", arg, 3)) {
882 if (++i >= argc) Syntax ();
883 /* ignore geometry */
885 } else if (isabbreviation ("-cpp", arg, 2)) {
886 if (++i >= argc) Syntax ();
887 cpp_program = argv[i];
889 } else if (!strcmp ("-n", arg)) {
892 } else if (isabbreviation ("-nocpp", arg, 3)) {
895 } else if (isabbreviation ("-query", arg, 2)) {
898 } else if (isabbreviation ("-load", arg, 2)) {
901 } else if (isabbreviation ("-merge", arg, 2)) {
904 } else if (isabbreviation ("-override", arg, 2)) {
907 } else if (isabbreviation ("-symbols", arg, 3)) {
910 } else if (isabbreviation ("-remove", arg, 4)) {
913 } else if (isabbreviation ("-edit", arg, 2)) {
914 if (++i >= argc) Syntax ();
918 } else if (isabbreviation ("-backup", arg, 2)) {
919 if (++i >= argc) Syntax ();
920 backup_suffix = argv[i];
922 } else if (isabbreviation ("-all", arg, 2)) {
923 whichResources = RALL;
925 } else if (isabbreviation ("-global", arg, 3)) {
926 whichResources = RGLOBAL;
928 } else if (isabbreviation ("-screen", arg, 3)) {
929 whichResources = RSCREEN;
931 } else if (!strcmp ("-screens", arg)) {
932 whichResources = RSCREENS;
934 } else if (isabbreviation ("-retain", arg, 4)) {
937 } else if (isabbreviation ("-quiet", arg, 2)) {
940 } else if (arg[1] == 'I') {
941 addstring(&includes, " ");
942 addescapedstring(&includes, arg);
944 } else if (arg[1] == 'U' || arg[1] == 'D') {
945 if (num_cmd_defines < MAX_CMD_DEFINES) {
946 cmd_defines[num_cmd_defines++] = arg;
948 fatal("%s: Too many -U/-D arguments\n", ProgramName);
953 } else if (arg[0] == '=')
960 while ((i = open("/dev/null", O_RDONLY)) < 3)
961 ; /* make sure later freopen won't clobber things */
965 if (!(dpy = XOpenDisplay (displayname)))
966 fatal("%s: Can't open display '%s'\n", ProgramName,
967 XDisplayName (displayname));
969 if (whichResources == RALL && ScreenCount(dpy) == 1)
970 whichResources = RGLOBAL;
974 (oper == OPLOAD || oper == OPMERGE || oper == OPOVERRIDE)) {
975 need_real_defines = True;
977 strcpy(tmpname2, "xrdbD_XXXXXX");
978 strcpy(tmpname3, "\\temp\\xrdbD_XXXXXX");
981 { char *tmpdir=getenv("TMP");
982 if (!tmpdir) tmpdir="/";
983 sprintf(tmpname2, "%s/xrdbD_XXXXXX",tmpdir);
986 strcpy(tmpname2, "/tmp/xrdbD_XXXXXX");
989 (void) mktemp(tmpname2);
997 (oper == OPLOAD || oper == OPMERGE || oper == OPOVERRIDE) &&
998 (whichResources == RALL || whichResources == RSCREENS)
1001 char inputbuf[1024];
1003 strcpy(tmpname, "\\temp\\xrdb_XXXXXX");
1006 { char *tmpdir=getenv("TMP");
1007 if (!tmpdir) tmpdir="/";
1008 sprintf(tmpname, "%s/xrdb_XXXXXX",tmpdir);
1011 strcpy(tmpname, "/tmp/xrdb_XXXXXX");
1014 #ifndef HAVE_MKSTEMP
1015 (void) mktemp(tmpname);
1017 fp = fopen(filename, "w");
1020 int fd = mkstemp(tmpname);
1022 fp = fdopen(fd, "w");
1024 #endif /* MKSTEMP */
1026 fatal("%s: Failed to open temp file: %s\n", ProgramName,
1028 while (fgets(inputbuf, sizeof(inputbuf), stdin) != NULL)
1029 fputs(inputbuf, fp);
1033 DoDisplayDefines(dpy, &defines, displayname);
1034 defines_base = defines.used;
1035 need_newline = (oper == OPQUERY || oper == OPSYMBOLS ||
1036 (dont_execute && oper != OPREMOVE));
1037 InitBuffer(&buffer);
1038 if (whichResources == RGLOBAL)
1039 Process(DefaultScreen(dpy), False, True);
1040 else if (whichResources == RSCREEN)
1041 Process(DefaultScreen(dpy), True, True);
1042 else if (whichResources == RSCREENS ||
1043 (oper != OPLOAD && oper != OPMERGE && oper != OPOVERRIDE)) {
1044 if (whichResources == RALL && oper != OPSYMBOLS) {
1046 printf("! screen-independent resources\n");
1047 Process(0, False, True);
1051 for (i = 0; i < ScreenCount(dpy); i++) {
1053 if (oper == OPSYMBOLS)
1054 printf("# screen %d symbols\n", i);
1056 printf("! screen %d resources\n", i);
1057 printf("#if SCREEN_NUM == %d\n", i);
1060 Process(i, True, True);
1062 if (oper != OPSYMBOLS)
1064 if (i+1 != ScreenCount(dpy))
1072 dbs = (Entries *)malloc(ScreenCount(dpy) * sizeof(Entries));
1073 for (i = 0; i < ScreenCount(dpy); i++) {
1074 Process(i, True, False);
1077 InitEntries(&newDB);
1078 if (oper == OPMERGE || oper == OPOVERRIDE)
1079 GetEntriesString(&newDB, XResourceManagerString(dpy));
1080 ShuffleEntries(&newDB, dbs, ScreenCount(dpy));
1082 printf("! screen-independent resources\n");
1083 ReProcess(0, False);
1086 for (i = 0; i < ScreenCount(dpy); i++) {
1089 printf("! screen %d resources\n", i);
1090 printf("#if SCREEN_NUM == %d\n", i);
1095 if (i+1 != ScreenCount(dpy))
1104 XSetCloseDownMode(dpy, RetainPermanent);
1111 FormatEntries(Buffer *buffer, Entries *entries)
1118 if (oper == OPMERGE)
1119 qsort(entries->entry, entries->used, sizeof(Entry),
1121 for (i = 0; i < entries->used; i++) {
1122 if (entries->entry[i].usable)
1123 AppendEntryToBuffer(buffer, &entries->entry[i]);
1128 StoreProperty(Display *dpy, Window root, Atom res_prop)
1130 int len = buffer.used;
1131 int mode = PropModeReplace;
1132 unsigned char *buf = (unsigned char *)buffer.buff;
1133 int max = (XMaxRequestSize(dpy) << 2) - 28;
1138 XChangeProperty(dpy, root, res_prop, XA_STRING, 8, mode, buf, max);
1141 mode = PropModeAppend;
1142 } while (len > max);
1144 XChangeProperty(dpy, root, res_prop, XA_STRING, 8, mode, buf, len);
1145 if (mode != PropModeReplace)
1150 Process(int scrno, Bool doScreen, Bool execute)
1155 FILE *input, *output;
1158 defines.val[defines_base] = '\0';
1159 defines.used = defines_base;
1161 InitEntries(&newDB);
1162 DoScreenDefines(dpy, scrno, &defines);
1163 DoCmdDefines(&defines);
1165 xdefs = XScreenResourceString (ScreenOfDisplay(dpy, scrno));
1166 root = RootWindow(dpy, scrno);
1167 res_prop = XInternAtom(dpy, SCREEN_RESOURCES, False);
1169 xdefs = XResourceManagerString (dpy);
1170 root = RootWindow(dpy, 0);
1171 res_prop = XA_RESOURCE_MANAGER;
1173 if (oper == OPSYMBOLS) {
1174 printf ("%s\n", defines.val);
1175 } else if (oper == OPQUERY) {
1177 printf ("%s", xdefs); /* fputs broken in SunOS 4.0 */
1178 } else if (oper == OPREMOVE) {
1180 XDeleteProperty(dpy, root, res_prop);
1181 } else if (oper == OPEDIT) {
1182 char template[100], old[100];
1184 input = fopen(editFile, "r");
1185 snprintf(template, sizeof(template), "%sXXXXXX", editFile);
1186 #ifndef HAVE_MKSTEMP
1187 (void) mktemp(template);
1188 output = fopen(template, "w");
1191 int fd = mkstemp(template);
1192 output = fdopen(fd, "w");
1196 fatal("%s: can't open temporary file '%s'\n", ProgramName, template);
1197 GetEntriesString(&newDB, xdefs);
1198 EditFile(&newDB, input, output);
1202 snprintf(old, sizeof(old), "%s%s", editFile, backup_suffix);
1203 if (dont_execute) { /* then write to standard out */
1207 output = fopen (template, "r");
1209 while ((n = fread (buf, 1, sizeof buf, output)) > 0) {
1210 fwrite (buf, 1, n, stdout);
1216 rename (editFile, old);
1217 if (rename (template, editFile))
1218 fatal("%s: can't rename file '%s' to '%s'\n", ProgramName,
1219 template, editFile);
1222 if (oper == OPMERGE || oper == OPOVERRIDE)
1223 GetEntriesString(&newDB, xdefs);
1225 if (need_real_defines) {
1227 if (!(input = fopen(tmpname2, "w")))
1228 fatal("%s: can't open file '%s'\n", ProgramName, tmpname2);
1229 fputs(defines.val, input);
1230 fprintf(input, "\n#include \"%s\"\n", filename);
1232 (void) mktemp(tmpname3);
1233 if (asprintf(&cmd, "%s -P%s %s > %s", cpp_program, includes.val,
1234 tmpname2, tmpname3) == -1)
1235 fatal("%s: Out of memory\n", ProgramName);
1236 if (system(cmd) < 0)
1237 fatal("%s: cannot run '%s'\n", ProgramName, cmd);
1239 if (!(input = fopen(tmpname3, "r")))
1240 fatal("%s: can't open file '%s'\n", ProgramName, tmpname3);
1242 if (!freopen(tmpname2, "w+", stdin))
1243 fatal("%s: can't open file '%s'\n", ProgramName, tmpname2);
1244 fputs(defines.val, stdin);
1245 fprintf(stdin, "\n#include \"%s\"\n", filename);
1248 if (asprintf(&cmd, "%s -P%s", cpp_program, includes.val) == -1)
1249 fatal("%s: Out of memory\n", ProgramName);
1250 if (!(input = popen(cmd, "r")))
1251 fatal("%s: cannot run '%s'\n", ProgramName, cmd);
1257 if (!freopen (filename, "r", stdin))
1258 fatal("%s: can't open file '%s'\n", ProgramName, filename);
1262 (void) mktemp(tmpname3);
1263 if (asprintf(&cmd, "%s -P%s %s %s > %s", cpp_program,
1264 includes.val, defines.val,
1265 filename ? filename : "", tmpname3) == -1)
1266 fatal("%s: Out of memory\n", ProgramName);
1267 if (system(cmd) < 0)
1268 fatal("%s: cannot run '%s'\n", ProgramName, cmd);
1270 if (!(input = fopen(tmpname3, "r")))
1271 fatal("%s: can't open file '%s'\n", ProgramName, tmpname3);
1273 if (asprintf(&cmd, "%s -P%s %s %s", cpp_program,
1274 includes.val, defines.val,
1275 filename ? filename : "") == -1)
1276 fatal("%s: Out of memory\n", ProgramName);
1277 if (!(input = popen(cmd, "r")))
1278 fatal("%s: cannot run '%s'\n", ProgramName, cmd);
1287 ReadFile(&buffer, input);
1296 if (need_real_defines) {
1299 if (tmpname3[strlen(tmpname3) - 1] != 'X')
1304 GetEntries(&newDB, &buffer, 0);
1306 FormatEntries(&buffer, &newDB);
1308 if (buffer.used > 0) {
1309 fwrite (buffer.buff, 1, buffer.used, stdout);
1310 if (buffer.buff[buffer.used - 1] != '\n') putchar ('\n');
1312 } else if (buffer.used > 1 || !doScreen)
1313 StoreProperty (dpy, root, res_prop);
1315 XDeleteProperty (dpy, root, res_prop);
1319 FreeEntries(&newDB);
1320 if (doScreen && xdefs)
1325 ShuffleEntries(Entries *db, Entries *dbs, int num)
1328 register int i, j, k;
1330 char *curtag, *curvalue;
1332 hits = (int *)malloc(num * sizeof(int));
1334 for (i = 0; i < cur.used; i++) {
1335 curtag = cur.entry[i].tag;
1336 curvalue = cur.entry[i].value;
1337 for (j = 1; j < num; j++) {
1339 for (k = 0; k < cmp.used; k++) {
1340 if (cmp.entry[k].usable &&
1341 !strcmp(curtag, cmp.entry[k].tag) &&
1342 !strcmp(curvalue, cmp.entry[k].value))
1352 AddEntry(db, &cur.entry[i]);
1354 for (j = 0; j < num; j++)
1355 dbs[j].entry[hits[j]].usable = False;
1362 ReProcess(int scrno, Bool doScreen)
1367 FormatEntries(&buffer, &newDB);
1369 root = RootWindow(dpy, scrno);
1370 res_prop = XInternAtom(dpy, SCREEN_RESOURCES, False);
1372 root = RootWindow(dpy, 0);
1373 res_prop = XA_RESOURCE_MANAGER;
1376 if (buffer.used > 0) {
1377 fwrite (buffer.buff, 1, buffer.used, stdout);
1378 if (buffer.buff[buffer.used - 1] != '\n') putchar ('\n');
1381 if (buffer.used > 1 || !doScreen)
1382 StoreProperty (dpy, root, res_prop);
1384 XDeleteProperty (dpy, root, res_prop);
1386 FreeEntries(&newDB);
1390 fatal(char *msg, ...)
1395 perror(ProgramName);
1396 va_start(args, msg);
1397 vfprintf(stderr, msg, args);