SmMigResources.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: SmMigResources.c /main/4 1996/05/08 20:11:34 drk $ */
  24. static char sccsid[] = "@(#)48 1.2 src/cde/cde1/dtsession/SmMigResources.c, desktop, cde41J, 9520A_all 5/16/95 08:31:12";
  25. /*
  26. * COMPONENT_NAME: desktop
  27. *
  28. * FUNCTIONS: MigrateResources
  29. *
  30. * ORIGINS: 27
  31. *
  32. * IBM CONFIDENTIAL -- (IBM Confidential Restricted when
  33. * combined with the aggregated modules for this product)
  34. * OBJECT CODE ONLY SOURCE MATERIALS
  35. *
  36. * (C) COPYRIGHT International Business Machines Corp. 1995
  37. * All Rights Reserved
  38. * US Government Users Restricted Rights - Use, duplication or
  39. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  40. */
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <Dt/DtNlUtils.h>
  44. /* defines for return codes */
  45. #define RC_SUCCESS 0
  46. #define RC_OPEN_ERROR 1
  47. #define RC_END_OF_FILE 2
  48. #define RC_MEMORY_ALLOCATION_ERROR 3
  49. #define RC_LINE_CONTINUED 4
  50. #define RC_PARTIAL_LINE 5
  51. #define RC_WRITE_ERROR_TEMP 6
  52. /* type 1 resources are of the form:
  53. [*.]convert1[i]
  54. Example of a resource specification of this type:
  55. *foreground: Blue
  56. */
  57. #define NUMBER_OF_CONVERT1 15
  58. static char *convert1[NUMBER_OF_CONVERT1]
  59. = {
  60. "displayResolution",
  61. "systemFont",
  62. "userFont",
  63. "FontList",
  64. "Font",
  65. "FontSet",
  66. "multiClickTime",
  67. "sessionVersion",
  68. "background",
  69. "foreground",
  70. "ColorUse",
  71. "HelpColorUse",
  72. "background",
  73. "foreground",
  74. "enableBtn1Transfer"
  75. }
  76. ;
  77. /* type 2 resources are of the form:
  78. CONVERT2_0{* or .}CONVERT2_1{* or .}convert2[i]
  79. Example of a resource specification of this type:
  80. dtsession*extension*cycleTimeout: 10
  81. */
  82. #define CONVERT2_0 "dtsession"
  83. #define CONVERT2_1 "extension"
  84. #define NUMBER_OF_CONVERT2 5
  85. static char *convert2[NUMBER_OF_CONVERT2]
  86. = {
  87. "cycleTimeout",
  88. "lockTimeout",
  89. "saverTimeout",
  90. "random",
  91. "saverList",
  92. }
  93. ;
  94. /* type 3 resources are of the form:
  95. convert31[i]{* or .}convert32[i]
  96. Example of a resource specification of this type:
  97. Dtwm*useIconBox: True
  98. */
  99. #define NUMBER_OF_CONVERT3 26
  100. static char *convert31[NUMBER_OF_CONVERT3]
  101. = {
  102. "dtsession",
  103. "dtsession",
  104. "dtsession",
  105. "dtsession",
  106. "dtsession",
  107. "dtsession",
  108. "Dtstyle",
  109. "Dtstyle",
  110. "Dtwm",
  111. "Dtwm",
  112. "Dtwm",
  113. "Dtwm",
  114. "wsHelp",
  115. "wsHelp",
  116. "wsHelp",
  117. "wsHelp",
  118. "wsHelp",
  119. "wsHelp",
  120. "wsHelp",
  121. "wsHelp",
  122. "wsHelp",
  123. "wsHelp",
  124. "wsHelp",
  125. "wsHelp",
  126. "wsHelp",
  127. "cachedHelp"
  128. }
  129. ;
  130. static char *convert32[NUMBER_OF_CONVERT3]
  131. = {
  132. "displayResolution" ,
  133. "sessionLanguage" ,
  134. "saverTimeout" ,
  135. "cycleTimeout" ,
  136. "lockTimeout" ,
  137. "saverList" ,
  138. "lockoutScale" ,
  139. "timeoutScale" ,
  140. "keyboardFocusPolicy",
  141. "focusAutoRaise" ,
  142. "moveOpaque" ,
  143. "useIconBox" ,
  144. "onScreen" ,
  145. "x" ,
  146. "y" ,
  147. "columns" ,
  148. "rows" ,
  149. "helpType" ,
  150. "vPCount" ,
  151. "tTitle" ,
  152. "helpVolume" ,
  153. "locationId" ,
  154. "stringData" ,
  155. "windowGroup" ,
  156. "wsName" ,
  157. "cachedCount"
  158. }
  159. ;
  160. /* type 4 resources are of the form:
  161. {* or .}convert4[i]{* or .}CONVERT4_2
  162. Example of a resource specification of this type:
  163. *XmText*FontList: Big
  164. */
  165. #define CONVERT4_2 "FontList"
  166. #define NUMBER_OF_CONVERT4 2
  167. static char *convert4[NUMBER_OF_CONVERT4]
  168. = {
  169. "XmText",
  170. "XmTextField"
  171. }
  172. ;
  173. /* type 5 resources are of the form:
  174. convert5[i]{* or .}<any value>{* or .}CONVERT5_2
  175. Example of a resource specification of this type:
  176. Dtwm*0*helpResources: xxx
  177. */
  178. #define CONVERT5_2 "helpResources"
  179. #define NUMBER_OF_CONVERT5 2
  180. /* these are of the form p1*#*helpResources */
  181. static char *convert5[NUMBER_OF_CONVERT5]
  182. = {
  183. "Dtwm",
  184. "Mwm"
  185. }
  186. ;
  187. /* type 6 resources are of the form:
  188. CONVERT6_0<any value>{* or .}convert6[i]
  189. Example of a resource specification of this type:
  190. oWsHelp10*x: xxx
  191. */
  192. #define CONVERT6_0 "oWsHelp"
  193. #define NUMBER_OF_CONVERT6 11
  194. static char *convert6[NUMBER_OF_CONVERT6]
  195. = {
  196. "x",
  197. "y",
  198. "columns",
  199. "rows",
  200. "helpType",
  201. "vPCount",
  202. "tTitle",
  203. "helpVolume",
  204. "locationId",
  205. "stringData",
  206. "workspaces"
  207. }
  208. ;
  209. /* type 7 resources are of the form:
  210. CONVERT7_01{* or .}<any value>{* or .}convert7[i]
  211. - or -
  212. CONVERT7_02{* or .}<any value>{* or .}convert7[i]
  213. Examples of resource specifications of this type:
  214. Dtwm*0*initialWorkspace: xxx
  215. Mwm*0*initialWorkspace: xxx
  216. */
  217. #define CONVERT7_01 "Mwm"
  218. #define CONVERT7_02 "Dtwm"
  219. #define NUMBER_OF_CONVERT7 3
  220. /* these are of the form Mwm|Dtwm*XXX*resource */
  221. static char *convert7[NUMBER_OF_CONVERT7]
  222. = {
  223. "initialWorkspace",
  224. "workspaceList",
  225. "workspaceCount"
  226. }
  227. ;
  228. /* type 8 resources are of the form:
  229. CONVERT8_01{* or .}<any value>{* or .}<any value>{* or .}convert8[i]
  230. - or -
  231. CONVERT8_02{* or .}<any value>{* or .}<any value>{* or .}convert8[i]
  232. Examples of resource specifications of this type:
  233. Mwm*0*ws01*title: xxx
  234. Dtwm*1ws02*title: xxx
  235. */
  236. #define CONVERT8_01 "Mwm"
  237. #define CONVERT8_02 "Dtwm"
  238. #define NUMBER_OF_CONVERT8 3
  239. static char *convert8[NUMBER_OF_CONVERT8]
  240. = {
  241. "title",
  242. "geometry",
  243. "iconBoxGeometry"
  244. }
  245. ;
  246. /* type 9 resources are of the form:
  247. CONVERT9_01{* or .}<any value>{* or .}<any value>{* or .}convert91[i] \
  248. {* or .}convert92[i]
  249. - or -
  250. CONVERT9_02{* or .}<any value>{* or .}<any value>{* or .}convert91[i] \
  251. {* or .}convert92[i]
  252. Example of resource specification of this type:
  253. Dtwm*0*ws01*backdrop*image: Drops
  254. */
  255. #define CONVERT9_01 "Mwm"
  256. #define CONVERT9_02 "Dtwm"
  257. #define NUMBER_OF_CONVERT9 3
  258. static char *convert91[NUMBER_OF_CONVERT9]
  259. = {
  260. "backdrop",
  261. "FrontPanel",
  262. "MyFrontPanel"
  263. }
  264. ;
  265. static char *convert92[NUMBER_OF_CONVERT9]
  266. = {
  267. "image" ,
  268. "geometry",
  269. "geometry"
  270. }
  271. ;
  272. /* type 10 resources are of the form:
  273. *<any value>{* or .}convert10[i]
  274. Example of a resource specification of this type:
  275. *0*ColorPalette: Default.dp
  276. */
  277. #define NUMBER_OF_CONVERT10 3
  278. static char *convert10[NUMBER_OF_CONVERT10]
  279. = {
  280. "MonochromePalette",
  281. "ColorUse",
  282. "ColorPalette"
  283. }
  284. ;
  285. /* define the maximum fields in a resource specification
  286. (which does not include the value of the resource) that
  287. is required by this routine
  288. */
  289. #define NUMBER_OF_FIELDS 6
  290. static char * field[NUMBER_OF_FIELDS];
  291. /* this function determines whether a field extracted from the resource
  292. matches a string in the specified array.
  293. Returns:
  294. TRUE = if strings match
  295. FALSE = if string do not match.
  296. */
  297. int check_match1(char * match[], int noelements, int match1)
  298. {
  299. int i;
  300. int matched = FALSE;
  301. for (i=0;((matched == FALSE) &&
  302. (i<noelements));i++)
  303. {
  304. if (strcmp(field[match1],match[i]) == 0)
  305. {
  306. matched =TRUE;
  307. }
  308. }
  309. return(matched);
  310. }
  311. /* this function determines whether a fields extracted from the resource
  312. match strings in the specified arrays.
  313. Returns:
  314. TRUE = if strings match
  315. FALSE = if string do not match.
  316. */
  317. int check_match2(char * match1_str[], char * match2_str[],
  318. int noelements, int match1, int match2)
  319. {
  320. int i;
  321. int matched = FALSE;
  322. for (i=0;((matched == FALSE) &&
  323. (i<noelements));i++)
  324. {
  325. if ((strcmp(field[match1],match1_str[i]) == 0) &&
  326. (strcmp(field[match2],match2_str[i]) == 0))
  327. {
  328. matched =TRUE;
  329. }
  330. }
  331. return(matched);
  332. }
  333. /* this function reads all resources from the input file.
  334. If the resource matches those written from within desktop on 4.1.1 and
  335. 4.1.2, the resource is written to the output file.
  336. Otherwise, the resource is not written to the output file.
  337. Return codes:
  338. RC_SUCCESS
  339. RC_OPEN_ERROR
  340. RC_MEMORY_ALLOCATION_ERROR
  341. RC_WRITE_ERROR_TEMP
  342. */
  343. MigrateResources(char * inputfile, char * outputfile)
  344. {
  345. int size_of_buffer = 1024;
  346. int size_of_mbuffer = 1024;
  347. int rc = RC_SUCCESS;
  348. FILE * out_fh;
  349. FILE * in_fh;
  350. int write_line;
  351. int chars_written;
  352. int type_found = FALSE;
  353. char * string;
  354. char * realend;
  355. int done=0;
  356. int new_size = 0;
  357. int number_fields = 0;
  358. char * inputbuffer=NULL;
  359. char * inputbuffer1=NULL;
  360. char * unmodified_buffer=NULL;
  361. char * resource_end;
  362. char * first_space;
  363. char * tmp1;
  364. int i;
  365. int charlen;
  366. /* Initialize for multi-byte */
  367. DtNlInitialize();
  368. done = RC_SUCCESS;
  369. /* open input and output files and exit if not successful */
  370. in_fh = fopen(inputfile,"r");
  371. out_fh = fopen(outputfile,"w");
  372. if ((out_fh != NULL) && (in_fh != NULL))
  373. {
  374. /* allocate buffers for reading lines from the input file */
  375. inputbuffer = malloc ((size_of_buffer + 1) * sizeof(char *));
  376. inputbuffer1 = malloc ((size_of_buffer + 1) * sizeof(char *));
  377. unmodified_buffer = malloc ((size_of_mbuffer + 1)
  378. * sizeof(char *));
  379. if ((inputbuffer != NULL) &&
  380. (inputbuffer1 != NULL) &&
  381. (unmodified_buffer != NULL))
  382. {
  383. do
  384. {
  385. *inputbuffer = '\0';
  386. *inputbuffer1 = '\0';
  387. *unmodified_buffer = '\0';
  388. /* read and process each line from the input file */
  389. do
  390. {
  391. /* read until an line has been read */
  392. rc = RC_SUCCESS;
  393. string = fgets((char *)inputbuffer1, size_of_buffer, in_fh);
  394. /* if read was successful, then ...*/
  395. if (string != NULL)
  396. {
  397. /* determine if a larger unmodified buffer
  398. needs to be allocated
  399. */
  400. new_size = strlen(unmodified_buffer) +
  401. strlen(inputbuffer1) + 2;
  402. if (new_size > size_of_mbuffer)
  403. {
  404. unmodified_buffer = realloc(unmodified_buffer,
  405. new_size);
  406. size_of_mbuffer = new_size;
  407. }
  408. /* save unmodified data read */
  409. strcat(unmodified_buffer,inputbuffer1);
  410. /* set indicator if entire line was not read */
  411. DtLastChar(inputbuffer1,&realend,&charlen);
  412. if ((charlen == 1) && (*realend != '\n'))
  413. {
  414. rc = RC_PARTIAL_LINE;
  415. }
  416. else
  417. {
  418. /* if entire line was read but it ends
  419. with a continuation character
  420. then, remove the ending continuation character or
  421. spaces preceded by an ending
  422. continuation character.
  423. */
  424. realend=DtPrevChar(inputbuffer1,realend);
  425. for (;((DtIsspace(realend) != 0) &&
  426. (realend > inputbuffer1));)
  427. {
  428. realend=DtPrevChar(inputbuffer1,realend);
  429. }
  430. if ((mblen(realend,MB_CUR_MAX) == 1) &&
  431. (*realend == '\\'))
  432. {
  433. *realend = '\0';
  434. rc = RC_LINE_CONTINUED;
  435. }
  436. }
  437. /* allocate larger input buffer if necessary */
  438. new_size = strlen(inputbuffer) +
  439. strlen(inputbuffer1) + 2;
  440. if (new_size > size_of_buffer)
  441. {
  442. inputbuffer = realloc(inputbuffer,new_size);
  443. size_of_buffer = new_size;
  444. }
  445. /* concatenate modified buffer to previously
  446. read buffer
  447. */
  448. strcat(inputbuffer,inputbuffer1);
  449. }
  450. else
  451. {
  452. rc = RC_END_OF_FILE;
  453. }
  454. } while ((rc == RC_LINE_CONTINUED) || (rc == RC_PARTIAL_LINE));
  455. /* if read was successful, then determine if the resources
  456. read are part of the set that need to be retained.
  457. */
  458. type_found = FALSE;
  459. number_fields = 0;
  460. type_found=FALSE;
  461. if (rc == RC_SUCCESS)
  462. {
  463. /* process non-comment lines */
  464. if (strncmp(inputbuffer,"!",1) != 0)
  465. {
  466. /* determine the non-value portion of the
  467. resource specification (i.e. the
  468. part to the left of the ":" and to the
  469. left of the first space).
  470. */
  471. resource_end = DtStrchr(inputbuffer,':');
  472. first_space = DtStrchr(inputbuffer,' ');
  473. if (resource_end != NULL)
  474. {
  475. if ((first_space != NULL) &&
  476. (first_space < resource_end))
  477. {
  478. resource_end = NULL;
  479. }
  480. }
  481. if (resource_end != NULL)
  482. {
  483. *resource_end = '\0';
  484. /* determine the start of each field
  485. in the non-value part of the resource.
  486. It is assumed that the individual fields
  487. are delimited by a "*" or ".".
  488. */
  489. field[0]=inputbuffer;
  490. for (i=1;((i<NUMBER_OF_FIELDS) &&
  491. (field[i-1] != NULL));
  492. i++)
  493. {
  494. /* determine the location of the next
  495. delimiter - "* or "."
  496. Set field pointer to first one found.
  497. */
  498. field[i]=DtStrchr(field[i-1],'*');
  499. tmp1=DtStrchr(field[i-1],'.');
  500. if (((tmp1 != NULL) && (tmp1 < resource_end) &&
  501. (tmp1 < field[i])) ||
  502. (field[i] == NULL))
  503. {
  504. field[i]=tmp1;
  505. }
  506. if (field[i] != NULL)
  507. {
  508. /* terminate the previous field */
  509. *field[i] = '\0';
  510. /* move pointer to start of field */
  511. field[i]++;
  512. }
  513. if (field[i] >= resource_end)
  514. {
  515. field[i]=NULL;
  516. }
  517. }
  518. number_fields = i-1;
  519. /* check for each type of resource
  520. that should be retained in the output file.
  521. See comments preceding the definition
  522. of each array near the start of this file
  523. for a description and example of each type.
  524. */
  525. if ((strlen(field[0]) == 0) &&
  526. (number_fields == 2) &&
  527. (check_match1(convert1,
  528. NUMBER_OF_CONVERT1,1) == TRUE))
  529. {
  530. type_found=TRUE;
  531. }
  532. if ((type_found == FALSE) &&
  533. (number_fields == 3) &&
  534. (strcmp(field[0],CONVERT2_0) == 0) &&
  535. (strcmp(field[1],CONVERT2_1) == 0) &&
  536. (check_match1(convert2, NUMBER_OF_CONVERT2,2)
  537. == TRUE))
  538. {
  539. type_found=TRUE;
  540. }
  541. if ((type_found == FALSE) &&
  542. (number_fields == 2) &&
  543. (check_match2(convert31,convert32,
  544. NUMBER_OF_CONVERT3,0,1) == TRUE))
  545. {
  546. type_found=TRUE;
  547. }
  548. if ((type_found == FALSE) &&
  549. (number_fields == 3) &&
  550. (strcmp(field[2],CONVERT4_2) == 0) &&
  551. (check_match1(convert4,
  552. NUMBER_OF_CONVERT4,1) == TRUE))
  553. {
  554. type_found=TRUE;
  555. }
  556. if ((type_found == FALSE) &&
  557. (number_fields == 3) &&
  558. (strcmp(field[2],CONVERT5_2) == 0) &&
  559. (check_match1(convert5,
  560. NUMBER_OF_CONVERT5,0) == TRUE))
  561. {
  562. type_found=TRUE;
  563. }
  564. if ((type_found == FALSE) &&
  565. (number_fields == 2) &&
  566. (strlen(field[0]) > strlen(CONVERT6_0)) &&
  567. (strncmp(field[0], CONVERT6_0,
  568. strlen(CONVERT6_0)) == 0) &&
  569. (check_match1(convert6,
  570. NUMBER_OF_CONVERT6,1) == TRUE))
  571. {
  572. type_found=TRUE;
  573. }
  574. if ((type_found == FALSE) &&
  575. (number_fields == 3) &&
  576. ((strcmp(field[0],CONVERT7_01) == 0) ||
  577. (strcmp(field[0],CONVERT7_02) == 0)) &&
  578. (check_match1(convert7,
  579. NUMBER_OF_CONVERT7,2) == TRUE))
  580. {
  581. type_found=TRUE;
  582. }
  583. if ((type_found == FALSE) &&
  584. (number_fields == 4) &&
  585. ((strcmp(field[0],CONVERT8_01) == 0) ||
  586. (strcmp(field[0],CONVERT8_02) == 0)) &&
  587. (check_match1(convert8,
  588. NUMBER_OF_CONVERT8,3) == TRUE))
  589. {
  590. type_found=TRUE;
  591. }
  592. if ((type_found == FALSE) &&
  593. (number_fields == 5) &&
  594. ((strcmp(field[0],CONVERT9_01) == 0) ||
  595. (strcmp(field[0],CONVERT9_02) == 0)) &&
  596. (check_match2(convert91,convert92,
  597. NUMBER_OF_CONVERT9,3,4) == TRUE))
  598. {
  599. type_found=TRUE;
  600. }
  601. if ((type_found == FALSE) &&
  602. (number_fields == 3) &&
  603. (check_match1(convert10,
  604. NUMBER_OF_CONVERT10,2) == TRUE))
  605. {
  606. type_found=TRUE;
  607. }
  608. }
  609. }
  610. /* if resource should be retained then
  611. write resource to output file.
  612. */
  613. if (type_found == TRUE)
  614. {
  615. chars_written = fputs(unmodified_buffer,out_fh);
  616. if (chars_written != strlen(unmodified_buffer))
  617. {
  618. if (rc == RC_SUCCESS)
  619. {
  620. rc = RC_WRITE_ERROR_TEMP;
  621. }
  622. }
  623. }
  624. }
  625. } while (rc == RC_SUCCESS);
  626. }
  627. else
  628. {
  629. rc = RC_MEMORY_ALLOCATION_ERROR;
  630. }
  631. if (rc == RC_END_OF_FILE)
  632. {
  633. rc = RC_SUCCESS;
  634. }
  635. /* free all allocated buffers */
  636. if (inputbuffer != NULL)
  637. {
  638. free(inputbuffer);
  639. }
  640. if (inputbuffer1 != NULL)
  641. {
  642. free(inputbuffer1);
  643. }
  644. if (unmodified_buffer != NULL)
  645. {
  646. free(unmodified_buffer);
  647. }
  648. }
  649. else
  650. {
  651. rc = RC_OPEN_ERROR;
  652. }
  653. if (in_fh != NULL)
  654. {
  655. fclose(in_fh);
  656. }
  657. if (out_fh != NULL)
  658. {
  659. fclose(out_fh);
  660. }
  661. return(rc);
  662. }