xrdb.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. /*
  2. * xrdb - X resource manager database utility
  3. *
  4. */
  5. /*
  6. * COPYRIGHT 1987, 1991
  7. * DIGITAL EQUIPMENT CORPORATION
  8. * MAYNARD, MASSACHUSETTS
  9. * MASSACHUSETTS INSTITUTE OF TECHNOLOGY
  10. * CAMBRIDGE, MASSACHUSETTS
  11. * ALL RIGHTS RESERVED.
  12. *
  13. * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
  14. * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
  15. * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
  16. * ANY PURPOSE. IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
  17. *
  18. * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT RIGHTS,
  19. * APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN ADDITION TO THAT
  20. * SET FORTH ABOVE.
  21. *
  22. *
  23. * Permission to use, copy, modify, and distribute this software and its
  24. * documentation for any purpose and without fee is hereby granted, provided
  25. * that the above copyright notice appear in all copies and that both that
  26. * copyright notice and this permission notice appear in supporting
  27. * documentation, and that the name of Digital Equipment Corporation not be
  28. * used in advertising or publicity pertaining to distribution of the software
  29. * without specific, written prior permission.
  30. */
  31. /*
  32. * this program is used to load, or dump the resource manager database
  33. * in the server.
  34. *
  35. * Original Author: Jim Gettys, August 28, 1987
  36. * Extensively Modified: Phil Karlton, January 5, 1987
  37. * Modified a Bunch More: Bob Scheifler, February, 1991
  38. */
  39. #ifdef HAVE_CONFIG_H
  40. #include <config.h>
  41. #endif
  42. #include <X11/Xlib.h>
  43. #include <X11/Xutil.h>
  44. #include <X11/Xatom.h>
  45. #include <X11/Xos.h>
  46. #include <X11/Xmu/SysUtil.h>
  47. #include <stdio.h>
  48. #include <ctype.h>
  49. #include <errno.h>
  50. #include <stdlib.h>
  51. #include <stdarg.h>
  52. #ifdef NEED_SYS_PARAM_H
  53. # include <sys/param.h> /* defines MAXHOSTNAMELEN on BSD & Linux */
  54. #endif
  55. #ifdef NEED_NETDB_H
  56. # include <netdb.h> /* defines MAXHOSTNAMELEN on Solaris */
  57. #endif
  58. #define SCREEN_RESOURCES "SCREEN_RESOURCES"
  59. #ifndef CPP
  60. #ifdef __UNIXOS2__
  61. /* expected to be in path */
  62. #define CPP "cpp"
  63. #else
  64. #define CPP "/usr/lib/cpp"
  65. #endif /* __UNIXOS2__ */
  66. #endif /* CPP */
  67. #define INIT_BUFFER_SIZE 10000
  68. #define INIT_ENTRY_SIZE 500
  69. #define RALL 0
  70. #define RGLOBAL 1
  71. #define RSCREEN 2
  72. #define RSCREENS 3
  73. #define OPSYMBOLS 0
  74. #define OPQUERY 1
  75. #define OPREMOVE 2
  76. #define OPEDIT 3
  77. #define OPLOAD 4
  78. #define OPMERGE 5
  79. #define OPOVERRIDE 6
  80. #define RESOURCE_PROPERTY_NAME "RESOURCE_MANAGER"
  81. #define BACKUP_SUFFIX ".bak" /* for editting */
  82. typedef struct _Entry {
  83. char *tag, *value;
  84. int lineno;
  85. Bool usable;
  86. } Entry;
  87. typedef struct _Buffer {
  88. char *buff;
  89. int room, used;
  90. } Buffer;
  91. typedef struct _Entries {
  92. Entry *entry;
  93. int room, used;
  94. } Entries;
  95. /* dynamically allocated strings */
  96. #define CHUNK_SIZE 4096
  97. typedef struct _String {
  98. char *val;
  99. int room, used;
  100. } String;
  101. static char *ProgramName;
  102. static Bool quiet = False;
  103. static char tmpname[32];
  104. static char *filename = NULL;
  105. #ifdef PATHETICCPP
  106. static Bool need_real_defines = False;
  107. static char tmpname2[32];
  108. #endif
  109. #ifdef WIN32
  110. static char tmpname3[32];
  111. #endif
  112. static int oper = OPLOAD;
  113. static char *editFile = NULL;
  114. static const char *cpp_program = NULL;
  115. static const char* const cpp_locations[] = { CPP };
  116. static const char *backup_suffix = BACKUP_SUFFIX;
  117. static Bool dont_execute = False;
  118. static String defines;
  119. static int defines_base;
  120. #define MAX_CMD_DEFINES 512
  121. static char *cmd_defines[MAX_CMD_DEFINES];
  122. static int num_cmd_defines = 0;
  123. static String includes;
  124. static Display *dpy;
  125. static Buffer buffer;
  126. static Entries newDB;
  127. static void fatal(const char *, ...) _X_ATTRIBUTE_PRINTF(1,2) _X_NORETURN;
  128. static void addstring ( String *arg, const char *s );
  129. static void addescapedstring ( String *arg, const char *s );
  130. static void addtokstring ( String *arg, const char *s );
  131. static void FormatEntries ( Buffer *b, Entries *entries );
  132. static void StoreProperty ( Display *display, Window root, Atom res_prop );
  133. static void Process ( int scrno, Bool doScreen, Bool execute );
  134. static void ShuffleEntries ( Entries *db, Entries *dbs, int num );
  135. static void ReProcess ( int scrno, Bool doScreen );
  136. #ifndef HAVE_ASPRINTF
  137. /* sprintf variant found in newer libc's which allocates string to print to */
  138. static int _X_ATTRIBUTE_PRINTF(2,3)
  139. asprintf(char ** ret, const char *format, ...)
  140. {
  141. char buf[256];
  142. int len;
  143. va_list ap;
  144. va_start(ap, format);
  145. len = vsnprintf(buf, sizeof(buf), format, ap);
  146. va_end(ap);
  147. if (len < 0)
  148. return -1;
  149. if (len < sizeof(buf))
  150. {
  151. *ret = strdup(buf);
  152. }
  153. else
  154. {
  155. *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */
  156. if (*ret != NULL)
  157. {
  158. va_start(ap, format);
  159. len = vsnprintf(*ret, len + 1, format, ap);
  160. va_end(ap);
  161. if (len < 0) {
  162. free(*ret);
  163. *ret = NULL;
  164. }
  165. }
  166. }
  167. if (*ret == NULL)
  168. return -1;
  169. return len;
  170. }
  171. #endif /* HAVE_ASPRINTF */
  172. static void
  173. InitBuffer(Buffer *b)
  174. {
  175. b->room = INIT_BUFFER_SIZE;
  176. b->used = 0;
  177. b->buff = malloc(INIT_BUFFER_SIZE*sizeof(char));
  178. }
  179. #ifdef notyet
  180. static void
  181. FreeBuffer(Buffer *b)
  182. {
  183. free(b->buff);
  184. }
  185. #endif
  186. static void
  187. AppendToBuffer(Buffer *b, const char *str, int len)
  188. {
  189. while (b->used + len > b->room) {
  190. b->buff = realloc(b->buff, 2*b->room*(sizeof(char)));
  191. b->room *= 2;
  192. }
  193. strncpy(b->buff + b->used, str, len);
  194. b->used += len;
  195. }
  196. static void
  197. InitEntries(Entries *e)
  198. {
  199. e->room = INIT_ENTRY_SIZE;
  200. e->used = 0;
  201. e->entry = malloc(INIT_ENTRY_SIZE*sizeof(Entry));
  202. }
  203. static void
  204. FreeEntries(Entries *e)
  205. {
  206. register int i;
  207. for (i = 0; i < e->used; i++) {
  208. if (e->entry[i].usable) {
  209. free(e->entry[i].tag);
  210. free(e->entry[i].value);
  211. }
  212. }
  213. free((char *)e->entry);
  214. }
  215. static void
  216. AddEntry(Entries *e, Entry *entry)
  217. {
  218. register int n;
  219. for (n = 0; n < e->used; n++) {
  220. if (!strcmp(e->entry[n].tag, entry->tag)) {
  221. /* overwrite old entry */
  222. if (e->entry[n].lineno && !quiet) {
  223. fprintf (stderr,
  224. "%s: \"%s\" on line %d overrides entry on line %d\n",
  225. ProgramName, entry->tag, entry->lineno,
  226. e->entry[n].lineno);
  227. }
  228. free(e->entry[n].tag);
  229. free(e->entry[n].value);
  230. entry->usable = True;
  231. e->entry[n] = *entry;
  232. return; /* ok to leave, now there's only one of each tag in db */
  233. }
  234. }
  235. if (e->used == e->room) {
  236. e->entry = realloc(e->entry, 2 * e->room * (sizeof(Entry)));
  237. e->room *= 2;
  238. }
  239. entry->usable = True;
  240. e->entry[e->used++] = *entry;
  241. }
  242. static int
  243. CompareEntries(const void *e1, const void *e2)
  244. {
  245. return strcmp(((const Entry *)e1)->tag, ((const Entry *)e2)->tag);
  246. }
  247. static void
  248. AppendEntryToBuffer(Buffer *b, Entry *entry)
  249. {
  250. AppendToBuffer(b, entry->tag, strlen(entry->tag));
  251. AppendToBuffer(b, ":\t", 2);
  252. AppendToBuffer(b, entry->value, strlen(entry->value));
  253. AppendToBuffer(b, "\n", 1);
  254. }
  255. /*
  256. * Return the position of the first unescaped occurrence of dest in string.
  257. * If lines is non-null, return the number of newlines skipped over.
  258. */
  259. static const char *
  260. FindFirst(const char *string, char dest, int *lines)
  261. {
  262. if (lines)
  263. *lines = 0;
  264. for (;;) {
  265. if (*string == '\0')
  266. return NULL;
  267. if (*string == '\\') {
  268. if (*++string == '\0')
  269. return NULL;
  270. } else if (*string == dest)
  271. return string;
  272. if (*string == '\n' && lines)
  273. (*lines)++;
  274. string++;
  275. }
  276. }
  277. static void
  278. GetEntries(Entries *entries, Buffer *buff, int bequiet)
  279. {
  280. const char *line, *colon, *temp, *str;
  281. Entry entry;
  282. register int length;
  283. int lineno = 0;
  284. int lines_skipped;
  285. str = buff->buff;
  286. if (!str) return;
  287. for ( ; str < buff->buff + buff->used;
  288. str = line + 1, lineno += lines_skipped) {
  289. line = FindFirst(str, '\n', &lines_skipped);
  290. lineno++;
  291. if (!line)
  292. line = buff->buff + buff->used;
  293. if (*str == '!')
  294. continue;
  295. if (*str == '\n')
  296. continue;
  297. if (!bequiet && *str == '#') {
  298. int dummy;
  299. if (sscanf (str, "# %d", &dummy) == 1 ||
  300. sscanf (str, "# line %d", &dummy) == 1)
  301. lineno = dummy - 1;
  302. continue;
  303. }
  304. for (temp = str;
  305. *temp && *temp != '\n' && isascii(*temp) && isspace(*temp);
  306. temp++) ;
  307. if (!*temp || *temp == '\n') continue;
  308. colon = FindFirst(str, ':', NULL);
  309. if (!colon || colon > line) {
  310. if (!bequiet && !quiet)
  311. fprintf (stderr,
  312. "%s: colon missing on line %d, ignoring line\n",
  313. ProgramName, lineno);
  314. continue;
  315. }
  316. /* strip leading and trailing blanks from name and store result */
  317. while (*str == ' ' || *str == '\t')
  318. str++;
  319. length = colon - str;
  320. while (length && (str[length-1] == ' ' || str[length-1] == '\t'))
  321. length--;
  322. entry.tag = malloc(length + 1);
  323. strncpy(entry.tag, str, length);
  324. entry.tag[length] = '\0';
  325. /* strip leading and trailing blanks from value and store result */
  326. colon++;
  327. while (*colon == ' ' || *colon == '\t')
  328. colon++;
  329. length = line - colon;
  330. entry.value = malloc(length + 1);
  331. strncpy(entry.value, colon, length);
  332. entry.value[length] = '\0';
  333. entry.lineno = bequiet ? 0 : lineno;
  334. AddEntry(entries, &entry);
  335. }
  336. }
  337. static void
  338. GetEntriesString(Entries *entries, char *str)
  339. {
  340. Buffer buff;
  341. if (str && *str) {
  342. buff.buff = str;
  343. buff.used = strlen(str);
  344. GetEntries(entries, &buff, 1);
  345. }
  346. }
  347. static void
  348. ReadFile(Buffer *b, FILE *input)
  349. {
  350. char buf[BUFSIZ + 1];
  351. register int bytes;
  352. b->used = 0;
  353. while (!feof(input) && (bytes = fread(buf, 1, BUFSIZ, input)) > 0) {
  354. #ifdef WIN32
  355. char *p;
  356. buf[bytes] = '\0';
  357. for (p = buf; p = strchr(p, '\r'); ) {
  358. if (p[-1] == '\\' && p[1] == '\n') {
  359. bytes -= 3;
  360. strcpy(p - 1, p + 2);
  361. }
  362. }
  363. #endif
  364. AppendToBuffer(b, buf, bytes);
  365. }
  366. AppendToBuffer(b, "", 1);
  367. }
  368. static void
  369. AddDef(String *buff, const char *title, const char *value)
  370. {
  371. #ifdef PATHETICCPP
  372. if (need_real_defines) {
  373. addstring(buff, "\n#define ");
  374. addtokstring(buff, title);
  375. if (value && (value[0] != '\0')) {
  376. addstring(buff, " ");
  377. addstring(buff, value);
  378. }
  379. return;
  380. }
  381. #endif
  382. if (buff->used) {
  383. if (oper == OPSYMBOLS)
  384. addstring(buff, "\n-D");
  385. else
  386. addstring(buff, " -D");
  387. } else
  388. addstring(buff, "-D");
  389. addtokstring(buff, title);
  390. if (value && (value[0] != '\0')) {
  391. addstring(buff, "=");
  392. addescapedstring(buff, value);
  393. }
  394. }
  395. static void
  396. AddSimpleDef(String *buff, const char *title)
  397. {
  398. AddDef(buff, title, (char *)NULL);
  399. }
  400. static void
  401. AddDefQ(String *buff, const char *title, const char *value)
  402. {
  403. #ifdef PATHETICCPP
  404. if (need_real_defines)
  405. AddDef(buff, title, value);
  406. else
  407. #endif
  408. if (value && (value[0] != '\0')) {
  409. AddSimpleDef(buff, title);
  410. addstring(buff, "=\"");
  411. addescapedstring(buff, value);
  412. addstring(buff, "\"");
  413. } else
  414. AddDef(buff, title, NULL);
  415. }
  416. static void
  417. AddNum(String *buff, const char *title, int value)
  418. {
  419. char num[20];
  420. snprintf(num, sizeof(num), "%d", value);
  421. AddDef(buff, title, num);
  422. }
  423. static void
  424. AddDefTok(String *buff, const char *prefix, char *title)
  425. {
  426. char name[512];
  427. snprintf(name, sizeof(name), "%s%s", prefix, title);
  428. AddSimpleDef(buff, name);
  429. }
  430. static void
  431. AddDefHostname(String *buff, const char *title, const char *value)
  432. {
  433. char *s;
  434. char name[512];
  435. char c;
  436. strncpy (name, value, sizeof(name)-1);
  437. name[sizeof(name)-1] = '\0';
  438. for (s = name; (c = *s); s++) {
  439. if (!isalpha(c) && !isdigit(c) && c != '_' && c != '.' && c != ':' && c != '-')
  440. *s = '_';
  441. }
  442. AddDef(buff, title, name);
  443. }
  444. static void
  445. AddUndef(String *buff, const char *title)
  446. {
  447. #ifdef PATHETICCPP
  448. if (need_real_defines) {
  449. addstring(buff, "\n#undef ");
  450. addstring(buff, title);
  451. return;
  452. }
  453. #endif
  454. if (buff->used) {
  455. if (oper == OPSYMBOLS)
  456. addstring(buff, "\n-U");
  457. else
  458. addstring(buff, " -U");
  459. } else
  460. addstring(buff, "-U");
  461. addtokstring(buff, title);
  462. }
  463. static void
  464. DoCmdDefines(String *buff)
  465. {
  466. int i;
  467. char *arg, *val;
  468. for (i = 0; i < num_cmd_defines; i++) {
  469. arg = cmd_defines[i];
  470. if (arg[1] == 'D') {
  471. val = strchr(arg, '=');
  472. if (val) {
  473. *val = '\0';
  474. AddDefQ(buff, arg + 2, val + 1);
  475. *val = '=';
  476. } else
  477. AddSimpleDef(buff, arg + 2);
  478. } else if (arg[1] == 'U') {
  479. AddUndef(buff, arg + 2);
  480. } else if (!strcmp(arg, "-undef") && oper != OPSYMBOLS) {
  481. addstring(buff, " -undef");
  482. }
  483. }
  484. }
  485. static int
  486. Resolution(int pixels, int mm)
  487. {
  488. if (mm == 0)
  489. return 0;
  490. else
  491. return ((pixels * 100000 / mm) + 50) / 100;
  492. }
  493. static void
  494. DoDisplayDefines(Display *display, String *defs, char *host)
  495. {
  496. #ifndef MAXHOSTNAMELEN
  497. #define MAXHOSTNAMELEN 255
  498. #endif
  499. char client[MAXHOSTNAMELEN], server[MAXHOSTNAMELEN], *colon;
  500. char **extnames;
  501. int n;
  502. XmuGetHostname(client, MAXHOSTNAMELEN);
  503. strncpy(server, XDisplayName(host), sizeof(server));
  504. server[sizeof(server) - 1] = '\0';
  505. /* search for final colon to skip over any embedded colons in IPv6
  506. numeric address forms */
  507. colon = strrchr(server, ':');
  508. n = 0;
  509. if (colon) {
  510. /* remove extra colon if there are exactly two, since it indicates
  511. DECnet. Three colons is an IPv6 address ending in :: though. */
  512. if ((colon > server) && (*(colon-1) == ':') &&
  513. ( ((colon - 1) == server) || (*(colon-2) != ':') ) ) {
  514. *(colon-1) = ':';
  515. }
  516. *colon++ = '\0';
  517. sscanf(colon, "%d", &n);
  518. }
  519. if (!*server || !strcmp(server, "unix") || !strcmp(server, "localhost"))
  520. strcpy(server, client);
  521. AddDefHostname(defs, "HOST", server); /* R3 compatibility */
  522. AddDefHostname(defs, "SERVERHOST", server);
  523. AddDefTok(defs, "SRVR_", server);
  524. AddNum(defs, "DISPLAY_NUM", n);
  525. AddDefHostname(defs, "CLIENTHOST", client);
  526. AddDefTok(defs, "CLNT_", client);
  527. AddNum(defs, "VERSION", ProtocolVersion(display));
  528. AddNum(defs, "REVISION", ProtocolRevision(display));
  529. AddDefQ(defs, "VENDOR", ServerVendor(display));
  530. AddDefTok(defs, "VNDR_", ServerVendor(display));
  531. AddNum(defs, "RELEASE", VendorRelease(display));
  532. AddNum(defs, "NUM_SCREENS", ScreenCount(display));
  533. extnames = XListExtensions(display, &n);
  534. while (--n >= 0)
  535. AddDefTok(defs, "EXT_", extnames[n]);
  536. XFreeExtensionList(extnames);
  537. }
  538. static const char *ClassNames[] = {
  539. "StaticGray",
  540. "GrayScale",
  541. "StaticColor",
  542. "PseudoColor",
  543. "TrueColor",
  544. "DirectColor"
  545. };
  546. #define NUM_CLASS_NAMES (int)(sizeof(ClassNames) / sizeof(ClassNames[0]))
  547. static void
  548. DoScreenDefines(Display *display, int scrno, String *defs)
  549. {
  550. Screen *screen;
  551. Visual *visual;
  552. XVisualInfo vinfo, *vinfos;
  553. int nv, i, j;
  554. char name[50];
  555. screen = ScreenOfDisplay(display, scrno);
  556. visual = DefaultVisualOfScreen(screen);
  557. vinfo.screen = scrno;
  558. vinfos = XGetVisualInfo(display, VisualScreenMask, &vinfo, &nv);
  559. AddNum(defs, "SCREEN_NUM", scrno);
  560. AddNum(defs, "WIDTH", screen->width);
  561. AddNum(defs, "HEIGHT", screen->height);
  562. AddNum(defs, "X_RESOLUTION", Resolution(screen->width,screen->mwidth));
  563. AddNum(defs, "Y_RESOLUTION", Resolution(screen->height,screen->mheight));
  564. AddNum(defs, "PLANES", DisplayPlanes(display, scrno));
  565. AddNum(defs, "BITS_PER_RGB", visual->bits_per_rgb);
  566. if (visual->class >= 0 && visual->class < NUM_CLASS_NAMES) {
  567. AddDefQ(defs, "CLASS", ClassNames[visual->class]);
  568. snprintf(name, sizeof(name), "CLASS_%s", ClassNames[visual->class]);
  569. AddNum(defs, name, (int)visual->visualid);
  570. }
  571. else {
  572. fprintf(stderr,
  573. "%s: unknown visual type %d for default visual id 0x%lx\n",
  574. ProgramName, visual->class, visual->visualid);
  575. }
  576. switch(visual->class) {
  577. case StaticColor:
  578. case PseudoColor:
  579. case TrueColor:
  580. case DirectColor:
  581. AddSimpleDef(defs, "COLOR");
  582. break;
  583. }
  584. for (i = 0; i < nv; i++) {
  585. for (j = i; --j >= 0; ) {
  586. if (vinfos[j].class == vinfos[i].class &&
  587. vinfos[j].depth == vinfos[i].depth)
  588. break;
  589. }
  590. if (j < 0) {
  591. if (vinfos[i].class >= 0 && vinfos[i].class < NUM_CLASS_NAMES) {
  592. snprintf(name, sizeof(name), "CLASS_%s_%d",
  593. ClassNames[vinfos[i].class], vinfos[i].depth);
  594. AddNum(defs, name, (int)vinfos[i].visualid);
  595. }
  596. else {
  597. fprintf(stderr,
  598. "%s: unknown visual type %d for visual id 0x%lx\n",
  599. ProgramName, vinfos[i].class, vinfos[i].visualid);
  600. }
  601. }
  602. }
  603. XFree((char *)vinfos);
  604. }
  605. static Entry *
  606. FindEntry(Entries *db, Buffer *b)
  607. {
  608. int i;
  609. register Entry *e;
  610. Entries phoney;
  611. Entry entry;
  612. entry.usable = False;
  613. entry.tag = NULL;
  614. entry.value = NULL;
  615. phoney.used = 0;
  616. phoney.room = 1;
  617. phoney.entry = &entry;
  618. GetEntries(&phoney, b, 1);
  619. if (phoney.used < 1)
  620. return NULL;
  621. for (i = 0; i < db->used; i++) {
  622. e = &db->entry[i];
  623. if (!e->usable)
  624. continue;
  625. if (strcmp(e->tag, entry.tag))
  626. continue;
  627. e->usable = False;
  628. if (strcmp(e->value, entry.value))
  629. return e;
  630. return NULL;
  631. }
  632. return NULL;
  633. }
  634. static void
  635. EditFile(Entries *new, FILE *in, FILE *out)
  636. {
  637. Buffer b;
  638. char buff[BUFSIZ];
  639. register Entry *e;
  640. register char *c;
  641. int i;
  642. InitBuffer(&b);
  643. while (in) {
  644. b.used = 0;
  645. while (1) {
  646. buff[0] ='\0';
  647. if (!fgets(buff, BUFSIZ, in))
  648. goto cleanup;
  649. AppendToBuffer(&b, buff, strlen(buff));
  650. c = &b.buff[b.used - 1];
  651. if ((*(c--) == '\n') && (b.used == 1 || *c != '\\'))
  652. break;
  653. }
  654. if ((e = FindEntry(new, &b)))
  655. fprintf(out, "%s:\t%s\n", e->tag, e->value);
  656. else
  657. fwrite(b.buff, 1, b.used, out);
  658. }
  659. cleanup:
  660. for (i = 0; i < new->used; i++) {
  661. e = &new->entry[i];
  662. if (e->usable)
  663. fprintf(out, "%s:\t%s\n", e->tag, e->value);
  664. }
  665. }
  666. static void _X_NORETURN
  667. Syntax (void)
  668. {
  669. fprintf (stderr,
  670. "usage: %s [-options ...] [filename]\n\n"
  671. "where options include:\n"
  672. " -help print this help message\n"
  673. " -version print the program version\n"
  674. " -display host:dpy display to use\n"
  675. " -all do all resources [default]\n"
  676. " -global do screen-independent resources\n"
  677. " -screen do screen-specific resources for one screen\n"
  678. " -screens do screen-specific resources for all screens\n"
  679. " -n show but don't do changes\n"
  680. " -cpp filename preprocessor to use [%s]\n"
  681. " -nocpp do not use a preprocessor\n"
  682. " -query query resources\n"
  683. " -load load resources from file [default]\n"
  684. " -override add in resources from file\n"
  685. " -merge merge resources from file & sort\n"
  686. " -edit filename edit resources into file\n"
  687. " -backup string backup suffix for -edit [%s]\n"
  688. " -symbols show preprocessor symbols\n"
  689. " -remove remove resources\n"
  690. " -retain avoid server reset (avoid using this)\n"
  691. " -quiet don't warn about duplicates\n"
  692. " -Dname[=value], -Uname, -Idirectory passed to preprocessor\n"
  693. "\n"
  694. "A - or no input filename represents stdin.\n",
  695. ProgramName, cpp_program ? cpp_program : "", BACKUP_SUFFIX);
  696. exit (1);
  697. }
  698. /*
  699. * The following is a hack until XrmParseCommand is ready. It determines
  700. * whether or not the given string is an abbreviation of the arg.
  701. */
  702. static Bool
  703. isabbreviation(const char *arg, const char *s, int minslen)
  704. {
  705. int arglen;
  706. int slen;
  707. /* exact match */
  708. if (!strcmp (arg, s)) return (True);
  709. arglen = strlen (arg);
  710. slen = strlen (s);
  711. /* too long or too short */
  712. if (slen >= arglen || slen < minslen) return (False);
  713. /* abbreviation */
  714. if (strncmp (arg, s, slen) == 0) return (True);
  715. /* bad */
  716. return (False);
  717. }
  718. static void
  719. addstring(String *arg, const char *s)
  720. {
  721. if(arg->used + strlen(s) + 1 >= arg->room) {
  722. if(arg->val)
  723. arg->val = realloc(arg->val, arg->room + CHUNK_SIZE);
  724. else
  725. arg->val = malloc(arg->room + CHUNK_SIZE);
  726. if(arg->val == NULL)
  727. fatal("%s: Not enough memory\n", ProgramName);
  728. arg->room += CHUNK_SIZE;
  729. }
  730. if(arg->used)
  731. strcat(arg->val, s);
  732. else
  733. strcpy(arg->val, s);
  734. arg->used += strlen(s);
  735. }
  736. static void
  737. addescapedstring(String *arg, const char *s)
  738. {
  739. char copy[512], *c;
  740. for (c = copy; *s && c < &copy[sizeof(copy)-1]; s++) {
  741. switch (*s) {
  742. case '"': case '\'': case '`':
  743. case '$': case '\\':
  744. *c++ = '_';
  745. break;
  746. default:
  747. *c++ = *s;
  748. }
  749. }
  750. *c = 0;
  751. addstring (arg, copy);
  752. }
  753. static void
  754. addtokstring(String *arg, const char *s)
  755. {
  756. char copy[512], *c;
  757. for (c = copy; *s && c < &copy[sizeof(copy)-1]; s++) {
  758. if (!isalpha(*s) && !isdigit(*s) && *s != '_')
  759. *c++ = '_';
  760. else
  761. *c++ = *s;
  762. }
  763. *c = 0;
  764. addstring (arg, copy);
  765. }
  766. int
  767. main(int argc, char *argv[])
  768. {
  769. int i;
  770. char *displayname = NULL;
  771. int whichResources = RALL;
  772. int retainProp = 0;
  773. FILE *fp = NULL;
  774. Bool need_newline;
  775. ProgramName = argv[0];
  776. defines.room = defines.used = includes.room = includes.used = 0;
  777. /* initialize the includes String struct */
  778. addstring(&includes, "");
  779. /* Pick the default cpp to use. This needs to be done before
  780. * we parse the command line in order to honor -nocpp which sets
  781. * it back to NULL.
  782. */
  783. if (cpp_program == NULL) {
  784. int number_of_elements
  785. = (sizeof cpp_locations) / (sizeof cpp_locations[0]);
  786. int j;
  787. for (j = 0; j < number_of_elements; j++) {
  788. char *end, *dup;
  789. /* cut off arguments */
  790. dup = strdup(cpp_locations[j]);
  791. end = strchr(dup,' ');
  792. if (end)
  793. *end = '\0';
  794. if (access(dup, X_OK) == 0) {
  795. cpp_program = cpp_locations[j];
  796. free(dup);
  797. break;
  798. }
  799. free(dup);
  800. }
  801. }
  802. /* needs to be replaced with XrmParseCommand */
  803. for (i = 1; i < argc; i++) {
  804. char *arg = argv[i];
  805. if (arg[0] == '-') {
  806. if (arg[1] == '\0') {
  807. filename = NULL;
  808. continue;
  809. } else if (isabbreviation ("-help", arg, 2)) {
  810. Syntax ();
  811. /* doesn't return */
  812. } else if (isabbreviation ("-version", arg, 2)) {
  813. printf("%s\n", PACKAGE_STRING);
  814. exit(0);
  815. } else if (isabbreviation ("-display", arg, 2)) {
  816. if (++i >= argc) Syntax ();
  817. displayname = argv[i];
  818. continue;
  819. } else if (isabbreviation ("-geometry", arg, 3)) {
  820. if (++i >= argc) Syntax ();
  821. /* ignore geometry */
  822. continue;
  823. } else if (isabbreviation ("-cpp", arg, 2)) {
  824. if (++i >= argc) Syntax ();
  825. cpp_program = argv[i];
  826. continue;
  827. } else if (!strcmp ("-n", arg)) {
  828. dont_execute = True;
  829. continue;
  830. } else if (isabbreviation ("-nocpp", arg, 3)) {
  831. cpp_program = NULL;
  832. continue;
  833. } else if (isabbreviation ("-query", arg, 2)) {
  834. oper = OPQUERY;
  835. continue;
  836. } else if (isabbreviation ("-load", arg, 2)) {
  837. oper = OPLOAD;
  838. continue;
  839. } else if (isabbreviation ("-merge", arg, 2)) {
  840. oper = OPMERGE;
  841. continue;
  842. } else if (isabbreviation ("-override", arg, 2)) {
  843. oper = OPOVERRIDE;
  844. continue;
  845. } else if (isabbreviation ("-symbols", arg, 3)) {
  846. oper = OPSYMBOLS;
  847. continue;
  848. } else if (isabbreviation ("-remove", arg, 4)) {
  849. oper = OPREMOVE;
  850. continue;
  851. } else if (isabbreviation ("-edit", arg, 2)) {
  852. if (++i >= argc) Syntax ();
  853. oper = OPEDIT;
  854. editFile = argv[i];
  855. continue;
  856. } else if (isabbreviation ("-backup", arg, 2)) {
  857. if (++i >= argc) Syntax ();
  858. backup_suffix = argv[i];
  859. continue;
  860. } else if (isabbreviation ("-all", arg, 2)) {
  861. whichResources = RALL;
  862. continue;
  863. } else if (isabbreviation ("-global", arg, 3)) {
  864. whichResources = RGLOBAL;
  865. continue;
  866. } else if (isabbreviation ("-screen", arg, 3)) {
  867. whichResources = RSCREEN;
  868. continue;
  869. } else if (!strcmp ("-screens", arg)) {
  870. whichResources = RSCREENS;
  871. continue;
  872. } else if (isabbreviation ("-retain", arg, 4)) {
  873. retainProp = 1;
  874. continue;
  875. } else if (isabbreviation ("-quiet", arg, 2)) {
  876. quiet = True;
  877. continue;
  878. } else if (arg[1] == 'I') {
  879. addstring(&includes, " ");
  880. addescapedstring(&includes, arg);
  881. continue;
  882. } else if (arg[1] == 'U' || arg[1] == 'D') {
  883. if (num_cmd_defines < MAX_CMD_DEFINES) {
  884. cmd_defines[num_cmd_defines++] = arg;
  885. } else {
  886. fatal("%s: Too many -U/-D arguments\n", ProgramName);
  887. }
  888. continue;
  889. } else if (!strcmp ("-undef", arg)) {
  890. if (num_cmd_defines < MAX_CMD_DEFINES) {
  891. cmd_defines[num_cmd_defines++] = "-undef";
  892. } else {
  893. fatal("%s: Too many cpp arguments\n", ProgramName);
  894. }
  895. continue;
  896. }
  897. Syntax ();
  898. } else if (arg[0] == '=')
  899. continue;
  900. else
  901. filename = arg;
  902. } /* end for */
  903. #ifndef WIN32
  904. while ((i = open("/dev/null", O_RDONLY)) < 3)
  905. ; /* make sure later freopen won't clobber things */
  906. (void) close(i);
  907. #endif
  908. /* Open display */
  909. if (!(dpy = XOpenDisplay (displayname)))
  910. fatal("%s: Can't open display '%s'\n", ProgramName,
  911. XDisplayName (displayname));
  912. if (whichResources == RALL && ScreenCount(dpy) == 1)
  913. whichResources = RGLOBAL;
  914. #ifdef PATHETICCPP
  915. if (cpp_program &&
  916. (oper == OPLOAD || oper == OPMERGE || oper == OPOVERRIDE)) {
  917. need_real_defines = True;
  918. #ifdef WIN32
  919. strcpy(tmpname2, "xrdbD_XXXXXX");
  920. strcpy(tmpname3, "\\temp\\xrdbD_XXXXXX");
  921. #else
  922. #ifdef __UNIXOS2__
  923. { char *tmpdir=getenv("TMP");
  924. if (!tmpdir) tmpdir="/";
  925. sprintf(tmpname2, "%s/xrdbD_XXXXXX",tmpdir);
  926. }
  927. #else
  928. strcpy(tmpname2, "/tmp/xrdbD_XXXXXX");
  929. #endif
  930. #endif
  931. (void) mktemp(tmpname2);
  932. }
  933. #endif
  934. if (!filename &&
  935. #ifdef PATHETICCPP
  936. need_real_defines
  937. #else
  938. (oper == OPLOAD || oper == OPMERGE || oper == OPOVERRIDE) &&
  939. (whichResources == RALL || whichResources == RSCREENS)
  940. #endif
  941. ) {
  942. char inputbuf[1024];
  943. #ifdef WIN32
  944. strcpy(tmpname, "\\temp\\xrdb_XXXXXX");
  945. #else
  946. #ifdef __UNIXOS2__
  947. { char *tmpdir=getenv("TMP");
  948. if (!tmpdir) tmpdir="/";
  949. sprintf(tmpname, "%s/xrdb_XXXXXX",tmpdir);
  950. }
  951. #else
  952. strcpy(tmpname, "/tmp/xrdb_XXXXXX");
  953. #endif
  954. #endif
  955. #ifndef HAVE_MKSTEMP
  956. (void) mktemp(tmpname);
  957. filename = tmpname;
  958. fp = fopen(filename, "w");
  959. #else
  960. {
  961. int fd = mkstemp(tmpname);
  962. filename = tmpname;
  963. fp = fdopen(fd, "w");
  964. }
  965. #endif /* MKSTEMP */
  966. if (!fp)
  967. fatal("%s: Failed to open temp file: %s\n", ProgramName,
  968. filename);
  969. while (fgets(inputbuf, sizeof(inputbuf), stdin) != NULL)
  970. fputs(inputbuf, fp);
  971. fclose(fp);
  972. }
  973. DoDisplayDefines(dpy, &defines, displayname);
  974. defines_base = defines.used;
  975. need_newline = (oper == OPQUERY || oper == OPSYMBOLS ||
  976. (dont_execute && oper != OPREMOVE));
  977. InitBuffer(&buffer);
  978. if (whichResources == RGLOBAL)
  979. Process(DefaultScreen(dpy), False, True);
  980. else if (whichResources == RSCREEN)
  981. Process(DefaultScreen(dpy), True, True);
  982. else if (whichResources == RSCREENS ||
  983. (oper != OPLOAD && oper != OPMERGE && oper != OPOVERRIDE)) {
  984. if (whichResources == RALL && oper != OPSYMBOLS) {
  985. if (need_newline)
  986. printf("! screen-independent resources\n");
  987. Process(0, False, True);
  988. if (need_newline)
  989. printf("\n");
  990. }
  991. for (i = 0; i < ScreenCount(dpy); i++) {
  992. if (need_newline) {
  993. if (oper == OPSYMBOLS)
  994. printf("# screen %d symbols\n", i);
  995. else {
  996. printf("! screen %d resources\n", i);
  997. printf("#if SCREEN_NUM == %d\n", i);
  998. }
  999. }
  1000. Process(i, True, True);
  1001. if (need_newline) {
  1002. if (oper != OPSYMBOLS)
  1003. printf("#endif\n");
  1004. if (i+1 != ScreenCount(dpy))
  1005. printf("\n");
  1006. }
  1007. }
  1008. }
  1009. else {
  1010. Entries *dbs;
  1011. dbs = malloc(ScreenCount(dpy) * sizeof(Entries));
  1012. for (i = 0; i < ScreenCount(dpy); i++) {
  1013. Process(i, True, False);
  1014. dbs[i] = newDB;
  1015. }
  1016. InitEntries(&newDB);
  1017. if (oper == OPMERGE || oper == OPOVERRIDE)
  1018. GetEntriesString(&newDB, XResourceManagerString(dpy));
  1019. ShuffleEntries(&newDB, dbs, ScreenCount(dpy));
  1020. if (need_newline)
  1021. printf("! screen-independent resources\n");
  1022. ReProcess(0, False);
  1023. if (need_newline)
  1024. printf("\n");
  1025. for (i = 0; i < ScreenCount(dpy); i++) {
  1026. newDB = dbs[i];
  1027. if (need_newline) {
  1028. printf("! screen %d resources\n", i);
  1029. printf("#if SCREEN_NUM == %d\n", i);
  1030. }
  1031. ReProcess(i, True);
  1032. if (need_newline) {
  1033. printf("#endif\n");
  1034. if (i+1 != ScreenCount(dpy))
  1035. printf("\n");
  1036. }
  1037. }
  1038. }
  1039. if (fp)
  1040. unlink(filename);
  1041. if (retainProp)
  1042. XSetCloseDownMode(dpy, RetainPermanent);
  1043. XCloseDisplay(dpy);
  1044. exit (0);
  1045. }
  1046. static void
  1047. FormatEntries(Buffer *b, Entries *entries)
  1048. {
  1049. register int i;
  1050. b->used = 0;
  1051. if (!entries->used)
  1052. return;
  1053. if (oper == OPMERGE)
  1054. qsort(entries->entry, entries->used, sizeof(Entry),
  1055. CompareEntries);
  1056. for (i = 0; i < entries->used; i++) {
  1057. if (entries->entry[i].usable)
  1058. AppendEntryToBuffer(b, &entries->entry[i]);
  1059. }
  1060. }
  1061. static void
  1062. StoreProperty(Display *display, Window root, Atom res_prop)
  1063. {
  1064. int len = buffer.used;
  1065. int mode = PropModeReplace;
  1066. unsigned char *buf = (unsigned char *)buffer.buff;
  1067. int max = (XMaxRequestSize(display) << 2) - 28;
  1068. if (len > max) {
  1069. XGrabServer(display);
  1070. do {
  1071. XChangeProperty(display, root, res_prop, XA_STRING, 8, mode, buf, max);
  1072. buf += max;
  1073. len -= max;
  1074. mode = PropModeAppend;
  1075. } while (len > max);
  1076. }
  1077. XChangeProperty(display, root, res_prop, XA_STRING, 8, mode, buf, len);
  1078. if (mode != PropModeReplace)
  1079. XUngrabServer(display);
  1080. }
  1081. static void
  1082. Process(int scrno, Bool doScreen, Bool execute)
  1083. {
  1084. char *xdefs;
  1085. Window root;
  1086. Atom res_prop;
  1087. FILE *input, *output;
  1088. char *cmd;
  1089. defines.val[defines_base] = '\0';
  1090. defines.used = defines_base;
  1091. buffer.used = 0;
  1092. InitEntries(&newDB);
  1093. DoScreenDefines(dpy, scrno, &defines);
  1094. DoCmdDefines(&defines);
  1095. if (doScreen) {
  1096. xdefs = XScreenResourceString (ScreenOfDisplay(dpy, scrno));
  1097. root = RootWindow(dpy, scrno);
  1098. res_prop = XInternAtom(dpy, SCREEN_RESOURCES, False);
  1099. } else {
  1100. xdefs = XResourceManagerString (dpy);
  1101. root = RootWindow(dpy, 0);
  1102. res_prop = XA_RESOURCE_MANAGER;
  1103. }
  1104. if (oper == OPSYMBOLS) {
  1105. printf ("%s\n", defines.val);
  1106. } else if (oper == OPQUERY) {
  1107. if (xdefs)
  1108. printf ("%s", xdefs); /* fputs broken in SunOS 4.0 */
  1109. } else if (oper == OPREMOVE) {
  1110. if (xdefs)
  1111. XDeleteProperty(dpy, root, res_prop);
  1112. } else if (oper == OPEDIT) {
  1113. char template[100], old[100];
  1114. input = fopen(editFile, "r");
  1115. snprintf(template, sizeof(template), "%sXXXXXX", editFile);
  1116. #ifndef HAVE_MKSTEMP
  1117. (void) mktemp(template);
  1118. output = fopen(template, "w");
  1119. #else
  1120. {
  1121. int fd = mkstemp(template);
  1122. output = fdopen(fd, "w");
  1123. }
  1124. #endif
  1125. if (!output)
  1126. fatal("%s: can't open temporary file '%s'\n", ProgramName, template);
  1127. GetEntriesString(&newDB, xdefs);
  1128. EditFile(&newDB, input, output);
  1129. if (input)
  1130. fclose(input);
  1131. fclose(output);
  1132. snprintf(old, sizeof(old), "%s%s", editFile, backup_suffix);
  1133. if (dont_execute) { /* then write to standard out */
  1134. char buf[BUFSIZ];
  1135. int n;
  1136. output = fopen (template, "r");
  1137. if (output) {
  1138. while ((n = fread (buf, 1, sizeof buf, output)) > 0) {
  1139. fwrite (buf, 1, n, stdout);
  1140. }
  1141. fclose (output);
  1142. }
  1143. unlink (template);
  1144. } else {
  1145. rename (editFile, old);
  1146. if (rename (template, editFile))
  1147. fatal("%s: can't rename file '%s' to '%s'\n", ProgramName,
  1148. template, editFile);
  1149. }
  1150. } else {
  1151. const char *cpp_addflags = "";
  1152. if (oper == OPMERGE || oper == OPOVERRIDE)
  1153. GetEntriesString(&newDB, xdefs);
  1154. /* Add -P flag only if using cpp, not another preprocessor */
  1155. if (cpp_program) {
  1156. const char *cp = strstr(cpp_program, "cpp");
  1157. if (cp && ((cp[3] == '\0') || cp[3] == ' '))
  1158. cpp_addflags = "-P";
  1159. }
  1160. #ifdef PATHETICCPP
  1161. if (need_real_defines) {
  1162. #ifdef WIN32
  1163. if (!(input = fopen(tmpname2, "w")))
  1164. fatal("%s: can't open file '%s'\n", ProgramName, tmpname2);
  1165. fputs(defines.val, input);
  1166. fprintf(input, "\n#include \"%s\"\n", filename);
  1167. fclose(input);
  1168. (void) mktemp(tmpname3);
  1169. if (asprintf(&cmd, "%s %s %s %s > %s", cpp_program, cpp_addflags,
  1170. includes.val, tmpname2, tmpname3) == -1)
  1171. fatal("%s: Out of memory\n", ProgramName);
  1172. if (system(cmd) < 0)
  1173. fatal("%s: cannot run '%s'\n", ProgramName, cmd);
  1174. free(cmd);
  1175. if (!(input = fopen(tmpname3, "r")))
  1176. fatal("%s: can't open file '%s'\n", ProgramName, tmpname3);
  1177. #else
  1178. if (!freopen(tmpname2, "w+", stdin))
  1179. fatal("%s: can't open file '%s'\n", ProgramName, tmpname2);
  1180. fputs(defines.val, stdin);
  1181. fprintf(stdin, "\n#include \"%s\"\n", filename);
  1182. fflush(stdin);
  1183. fseek(stdin, 0, 0);
  1184. if (asprintf(&cmd, "%s %s %s", cpp_program, cpp_addflags,
  1185. includes.val) == -1)
  1186. fatal("%s: Out of memory\n", ProgramName);
  1187. if (!(input = popen(cmd, "r")))
  1188. fatal("%s: cannot run '%s'\n", ProgramName, cmd);
  1189. free(cmd);
  1190. #endif
  1191. } else {
  1192. #endif
  1193. if (filename) {
  1194. if (!freopen (filename, "r", stdin))
  1195. fatal("%s: can't open file '%s'\n", ProgramName, filename);
  1196. }
  1197. if (cpp_program) {
  1198. #ifdef WIN32
  1199. (void) mktemp(tmpname3);
  1200. if (asprintf(&cmd, "%s %s %s %s %s > %s", cpp_program,
  1201. cpp_addflags, includes.val, defines.val,
  1202. filename ? filename : "", tmpname3) == -1)
  1203. fatal("%s: Out of memory\n", ProgramName);
  1204. if (system(cmd) < 0)
  1205. fatal("%s: cannot run '%s'\n", ProgramName, cmd);
  1206. free(cmd);
  1207. if (!(input = fopen(tmpname3, "r")))
  1208. fatal("%s: can't open file '%s'\n", ProgramName, tmpname3);
  1209. #else
  1210. if (asprintf(&cmd, "%s %s %s %s %s", cpp_program,
  1211. cpp_addflags, includes.val, defines.val,
  1212. filename ? filename : "") == -1)
  1213. fatal("%s: Out of memory\n", ProgramName);
  1214. if (!(input = popen(cmd, "r")))
  1215. fatal("%s: cannot run '%s'\n", ProgramName, cmd);
  1216. free(cmd);
  1217. #endif
  1218. } else {
  1219. input = stdin;
  1220. }
  1221. #ifdef PATHETICCPP
  1222. }
  1223. #endif
  1224. ReadFile(&buffer, input);
  1225. if (cpp_program) {
  1226. #ifdef WIN32
  1227. fclose(input);
  1228. #else
  1229. pclose(input);
  1230. #endif
  1231. }
  1232. #ifdef PATHETICCPP
  1233. if (need_real_defines) {
  1234. unlink(tmpname2);
  1235. #ifdef WIN32
  1236. if (tmpname3[strlen(tmpname3) - 1] != 'X')
  1237. unlink(tmpname3);
  1238. #endif
  1239. }
  1240. #endif
  1241. GetEntries(&newDB, &buffer, 0);
  1242. if (execute) {
  1243. FormatEntries(&buffer, &newDB);
  1244. if (dont_execute) {
  1245. if (buffer.used > 0) {
  1246. fwrite (buffer.buff, 1, buffer.used, stdout);
  1247. if (buffer.buff[buffer.used - 1] != '\n') putchar ('\n');
  1248. }
  1249. } else if (buffer.used > 1 || !doScreen)
  1250. StoreProperty (dpy, root, res_prop);
  1251. else
  1252. XDeleteProperty (dpy, root, res_prop);
  1253. }
  1254. }
  1255. if (execute)
  1256. FreeEntries(&newDB);
  1257. if (doScreen && xdefs)
  1258. XFree(xdefs);
  1259. }
  1260. static void
  1261. ShuffleEntries(Entries *db, Entries *dbs, int num)
  1262. {
  1263. int *hits;
  1264. register int i, j, k;
  1265. Entries cur, cmp;
  1266. char *curtag, *curvalue;
  1267. hits = malloc(num * sizeof(int));
  1268. cur = dbs[0];
  1269. for (i = 0; i < cur.used; i++) {
  1270. curtag = cur.entry[i].tag;
  1271. curvalue = cur.entry[i].value;
  1272. for (j = 1; j < num; j++) {
  1273. cmp = dbs[j];
  1274. for (k = 0; k < cmp.used; k++) {
  1275. if (cmp.entry[k].usable &&
  1276. !strcmp(curtag, cmp.entry[k].tag) &&
  1277. !strcmp(curvalue, cmp.entry[k].value))
  1278. {
  1279. hits[j] = k;
  1280. break;
  1281. }
  1282. }
  1283. if (k == cmp.used)
  1284. break;
  1285. }
  1286. if (j == num) {
  1287. AddEntry(db, &cur.entry[i]);
  1288. hits[0] = i;
  1289. for (j = 0; j < num; j++)
  1290. dbs[j].entry[hits[j]].usable = False;
  1291. }
  1292. }
  1293. free((char *)hits);
  1294. }
  1295. static void
  1296. ReProcess(int scrno, Bool doScreen)
  1297. {
  1298. Window root;
  1299. Atom res_prop;
  1300. FormatEntries(&buffer, &newDB);
  1301. if (doScreen) {
  1302. root = RootWindow(dpy, scrno);
  1303. res_prop = XInternAtom(dpy, SCREEN_RESOURCES, False);
  1304. } else {
  1305. root = RootWindow(dpy, 0);
  1306. res_prop = XA_RESOURCE_MANAGER;
  1307. }
  1308. if (dont_execute) {
  1309. if (buffer.used > 0) {
  1310. fwrite (buffer.buff, 1, buffer.used, stdout);
  1311. if (buffer.buff[buffer.used - 1] != '\n') putchar ('\n');
  1312. }
  1313. } else {
  1314. if (buffer.used > 1 || !doScreen)
  1315. StoreProperty (dpy, root, res_prop);
  1316. else
  1317. XDeleteProperty (dpy, root, res_prop);
  1318. }
  1319. FreeEntries(&newDB);
  1320. }
  1321. static void
  1322. fatal(const char *msg, ...)
  1323. {
  1324. va_list args;
  1325. if (errno != 0)
  1326. perror(ProgramName);
  1327. va_start(args, msg);
  1328. vfprintf(stderr, msg, args);
  1329. va_end(args);
  1330. exit(1);
  1331. }