2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
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";
26 * COMPONENT_NAME: desktop
28 * FUNCTIONS: MigrateResources
32 * IBM CONFIDENTIAL -- (IBM Confidential Restricted when
33 * combined with the aggregated modules for this product)
34 * OBJECT CODE ONLY SOURCE MATERIALS
36 * (C) COPYRIGHT International Business Machines Corp. 1995
38 * US Government Users Restricted Rights - Use, duplication or
39 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
43 #include <Dt/DtNlUtils.h>
45 /* defines for return codes */
47 #define RC_OPEN_ERROR 1
48 #define RC_END_OF_FILE 2
49 #define RC_MEMORY_ALLOCATION_ERROR 3
50 #define RC_LINE_CONTINUED 4
51 #define RC_PARTIAL_LINE 5
52 #define RC_WRITE_ERROR_TEMP 6
54 /* type 1 resources are of the form:
57 Example of a resource specification of this type:
60 #define NUMBER_OF_CONVERT1 15
61 static char *convert1[NUMBER_OF_CONVERT1]
82 /* type 2 resources are of the form:
83 CONVERT2_0{* or .}CONVERT2_1{* or .}convert2[i]
85 Example of a resource specification of this type:
86 dtsession*extension*cycleTimeout: 10
88 #define CONVERT2_0 "dtsession"
89 #define CONVERT2_1 "extension"
90 #define NUMBER_OF_CONVERT2 5
91 static char *convert2[NUMBER_OF_CONVERT2]
102 /* type 3 resources are of the form:
103 convert31[i]{* or .}convert32[i]
105 Example of a resource specification of this type:
106 Dtwm*useIconBox: True
108 #define NUMBER_OF_CONVERT3 26
109 static char *convert31[NUMBER_OF_CONVERT3]
139 static char *convert32[NUMBER_OF_CONVERT3]
141 "displayResolution" ,
149 "keyboardFocusPolicy",
170 /* type 4 resources are of the form:
171 {* or .}convert4[i]{* or .}CONVERT4_2
173 Example of a resource specification of this type:
174 *XmText*FontList: Big
176 #define CONVERT4_2 "FontList"
177 #define NUMBER_OF_CONVERT4 2
178 static char *convert4[NUMBER_OF_CONVERT4]
186 /* type 5 resources are of the form:
187 convert5[i]{* or .}<any value>{* or .}CONVERT5_2
189 Example of a resource specification of this type:
190 Dtwm*0*helpResources: xxx
192 #define CONVERT5_2 "helpResources"
193 #define NUMBER_OF_CONVERT5 2
194 /* these are of the form p1*#*helpResources */
195 static char *convert5[NUMBER_OF_CONVERT5]
203 /* type 6 resources are of the form:
204 CONVERT6_0<any value>{* or .}convert6[i]
206 Example of a resource specification of this type:
209 #define CONVERT6_0 "oWsHelp"
210 #define NUMBER_OF_CONVERT6 11
211 static char *convert6[NUMBER_OF_CONVERT6]
227 /* type 7 resources are of the form:
228 CONVERT7_01{* or .}<any value>{* or .}convert7[i]
230 CONVERT7_02{* or .}<any value>{* or .}convert7[i]
232 Examples of resource specifications of this type:
233 Dtwm*0*initialWorkspace: xxx
234 Mwm*0*initialWorkspace: xxx
236 #define CONVERT7_01 "Mwm"
237 #define CONVERT7_02 "Dtwm"
238 #define NUMBER_OF_CONVERT7 3
239 /* these are of the form Mwm|Dtwm*XXX*resource */
240 static char *convert7[NUMBER_OF_CONVERT7]
248 /* type 8 resources are of the form:
249 CONVERT8_01{* or .}<any value>{* or .}<any value>{* or .}convert8[i]
251 CONVERT8_02{* or .}<any value>{* or .}<any value>{* or .}convert8[i]
253 Examples of resource specifications of this type:
254 Mwm*0*ws01*title: xxx
255 Dtwm*1ws02*title: xxx
257 #define CONVERT8_01 "Mwm"
258 #define CONVERT8_02 "Dtwm"
259 #define NUMBER_OF_CONVERT8 3
260 static char *convert8[NUMBER_OF_CONVERT8]
268 /* type 9 resources are of the form:
269 CONVERT9_01{* or .}<any value>{* or .}<any value>{* or .}convert91[i] \
272 CONVERT9_02{* or .}<any value>{* or .}<any value>{* or .}convert91[i] \
275 Example of resource specification of this type:
276 Dtwm*0*ws01*backdrop*image: Drops
278 #define CONVERT9_01 "Mwm"
279 #define CONVERT9_02 "Dtwm"
280 #define NUMBER_OF_CONVERT9 3
281 static char *convert91[NUMBER_OF_CONVERT9]
289 static char *convert92[NUMBER_OF_CONVERT9]
299 /* type 10 resources are of the form:
300 *<any value>{* or .}convert10[i]
302 Example of a resource specification of this type:
303 *0*ColorPalette: Default.dp
305 #define NUMBER_OF_CONVERT10 3
306 static char *convert10[NUMBER_OF_CONVERT10]
315 /* define the maximum fields in a resource specification
316 (which does not include the value of the resource) that
317 is required by this routine
319 #define NUMBER_OF_FIELDS 6
320 static char * field[NUMBER_OF_FIELDS];
322 /* this function determines whether a field extracted from the resource
323 matches a string in the specified array.
325 TRUE = if strings match
326 FALSE = if string do not match.
328 int check_match1(char * match[], int noelements, int match1)
332 for (i=0;((matched == FALSE) &&
335 if (strcmp(field[match1],match[i]) == 0)
342 /* this function determines whether a fields extracted from the resource
343 match strings in the specified arrays.
345 TRUE = if strings match
346 FALSE = if string do not match.
348 int check_match2(char * match1_str[], char * match2_str[],
349 int noelements, int match1, int match2)
353 for (i=0;((matched == FALSE) &&
357 if ((strcmp(field[match1],match1_str[i]) == 0) &&
358 (strcmp(field[match2],match2_str[i]) == 0))
366 /* this function reads all resources from the input file.
367 If the resource matches those written from within desktop on 4.1.1 and
368 4.1.2, the resource is written to the output file.
369 Otherwise, the resource is not written to the output file.
374 RC_MEMORY_ALLOCATION_ERROR
378 MigrateResources(char * inputfile, char * outputfile)
381 int size_of_buffer = 1024;
382 int size_of_mbuffer = 1024;
388 int type_found = FALSE;
393 int number_fields = 0;
394 char * inputbuffer=NULL;
395 char * inputbuffer1=NULL;
396 char * unmodified_buffer=NULL;
403 /* Initialize for multi-byte */
406 /* open input and output files and exit if not successful */
407 in_fh = fopen(inputfile,"r");
408 out_fh = fopen(outputfile,"w");
409 if ((out_fh != NULL) && (in_fh != NULL))
412 /* allocate buffers for reading lines from the input file */
413 inputbuffer = malloc ((size_of_buffer + 1) * sizeof(char *));
414 inputbuffer1 = malloc ((size_of_buffer + 1) * sizeof(char *));
415 unmodified_buffer = malloc ((size_of_mbuffer + 1)
417 if ((inputbuffer != NULL) &&
418 (inputbuffer1 != NULL) &&
419 (unmodified_buffer != NULL))
425 *inputbuffer1 = '\0';
426 *unmodified_buffer = '\0';
427 /* read and process each line from the input file */
431 /* read until an line has been read */
433 string = fgets((char *)inputbuffer1, size_of_buffer, in_fh);
435 /* if read was successful, then ...*/
438 /* determine if a larger unmodified buffer
439 needs to be allocated
441 new_size = strlen(unmodified_buffer) +
442 strlen(inputbuffer1) + 2;
444 if (new_size > size_of_mbuffer)
446 unmodified_buffer = realloc(unmodified_buffer,
448 size_of_mbuffer = new_size;
451 /* save unmodified data read */
452 strcat(unmodified_buffer,inputbuffer1);
454 /* set indicator if entire line was not read */
455 DtLastChar(inputbuffer1,&realend,&charlen);
456 if ((charlen == 1) && (*realend != '\n'))
458 rc = RC_PARTIAL_LINE;
462 /* if entire line was read but it ends
463 with a continuation character
464 then, remove the ending continuation character or
465 spaces preceded by an ending
466 continuation character.
468 realend=DtPrevChar(inputbuffer1,realend);
469 for (;((DtIsspace(realend) != 0) &&
470 (realend > inputbuffer1));)
472 realend=DtPrevChar(inputbuffer1,realend);
474 if ((mblen(realend,MB_CUR_MAX) == 1) &&
478 rc = RC_LINE_CONTINUED;
482 /* allocate larger input buffer if necessary */
483 new_size = strlen(inputbuffer) +
484 strlen(inputbuffer1) + 2;
486 if (new_size > size_of_buffer)
488 inputbuffer = realloc(inputbuffer,new_size);
489 size_of_buffer = new_size;
492 /* concatenate modified buffer to previously
495 strcat(inputbuffer,inputbuffer1);
501 } while ((rc == RC_LINE_CONTINUED) || (rc == RC_PARTIAL_LINE));
503 /* if read was successful, then determine if the resources
504 read are part of the set that need to be retained.
509 if (rc == RC_SUCCESS)
511 /* process non-comment lines */
512 if (strncmp(inputbuffer,"!",1) != 0)
514 /* determine the non-value portion of the
515 resource specification (i.e. the
516 part to the left of the ":" and to the
517 left of the first space).
519 resource_end = DtStrchr(inputbuffer,':');
520 first_space = DtStrchr(inputbuffer,' ');
521 if (resource_end != NULL)
523 if ((first_space != NULL) &&
524 (first_space < resource_end))
530 if (resource_end != NULL)
532 *resource_end = '\0';
533 /* determine the start of each field
534 in the non-value part of the resource.
535 It is assumed that the individual fields
536 are delimited by a "*" or ".".
538 field[0]=inputbuffer;
539 for (i=1;((i<NUMBER_OF_FIELDS) &&
540 (field[i-1] != NULL));
543 /* determine the location of the next
544 delimiter - "* or "."
545 Set field pointer to first one found.
547 field[i]=DtStrchr(field[i-1],'*');
548 tmp1=DtStrchr(field[i-1],'.');
549 if (((tmp1 != NULL) && (tmp1 < resource_end) &&
550 (tmp1 < field[i])) ||
555 if (field[i] != NULL)
557 /* terminate the previous field */
559 /* move pointer to start of field */
562 if (field[i] >= resource_end)
568 /* check for each type of resource
569 that should be retained in the output file.
570 See comments preceding the definition
571 of each array near the start of this file
572 for a description and example of each type.
574 if ((strlen(field[0]) == 0) &&
575 (number_fields == 2) &&
576 (check_match1(convert1,
577 NUMBER_OF_CONVERT1,1) == TRUE))
582 if ((type_found == FALSE) &&
583 (number_fields == 3) &&
584 (strcmp(field[0],CONVERT2_0) == 0) &&
585 (strcmp(field[1],CONVERT2_1) == 0) &&
586 (check_match1(convert2, NUMBER_OF_CONVERT2,2)
591 if ((type_found == FALSE) &&
592 (number_fields == 2) &&
593 (check_match2(convert31,convert32,
594 NUMBER_OF_CONVERT3,0,1) == TRUE))
598 if ((type_found == FALSE) &&
599 (number_fields == 3) &&
600 (strcmp(field[2],CONVERT4_2) == 0) &&
601 (check_match1(convert4,
602 NUMBER_OF_CONVERT4,1) == TRUE))
606 if ((type_found == FALSE) &&
607 (number_fields == 3) &&
608 (strcmp(field[2],CONVERT5_2) == 0) &&
609 (check_match1(convert5,
610 NUMBER_OF_CONVERT5,0) == TRUE))
614 if ((type_found == FALSE) &&
615 (number_fields == 2) &&
616 (strlen(field[0]) > strlen(CONVERT6_0)) &&
617 (strncmp(field[0], CONVERT6_0,
618 strlen(CONVERT6_0)) == 0) &&
619 (check_match1(convert6,
620 NUMBER_OF_CONVERT6,1) == TRUE))
624 if ((type_found == FALSE) &&
625 (number_fields == 3) &&
626 ((strcmp(field[0],CONVERT7_01) == 0) ||
627 (strcmp(field[0],CONVERT7_02) == 0)) &&
628 (check_match1(convert7,
629 NUMBER_OF_CONVERT7,2) == TRUE))
634 if ((type_found == FALSE) &&
635 (number_fields == 4) &&
636 ((strcmp(field[0],CONVERT8_01) == 0) ||
637 (strcmp(field[0],CONVERT8_02) == 0)) &&
638 (check_match1(convert8,
639 NUMBER_OF_CONVERT8,3) == TRUE))
643 if ((type_found == FALSE) &&
644 (number_fields == 5) &&
645 ((strcmp(field[0],CONVERT9_01) == 0) ||
646 (strcmp(field[0],CONVERT9_02) == 0)) &&
647 (check_match2(convert91,convert92,
648 NUMBER_OF_CONVERT9,3,4) == TRUE))
652 if ((type_found == FALSE) &&
653 (number_fields == 3) &&
654 (check_match1(convert10,
655 NUMBER_OF_CONVERT10,2) == TRUE))
662 /* if resource should be retained then
663 write resource to output file.
665 if (type_found == TRUE)
667 chars_written = fputs(unmodified_buffer,out_fh);
668 if (chars_written != strlen(unmodified_buffer))
670 if (rc == RC_SUCCESS)
672 rc = RC_WRITE_ERROR_TEMP;
678 } while (rc == RC_SUCCESS);
682 rc = RC_MEMORY_ALLOCATION_ERROR;
684 if (rc == RC_END_OF_FILE)
689 /* free all allocated buffers */
690 if (inputbuffer != NULL)
694 if (inputbuffer1 != NULL)
698 if (unmodified_buffer != NULL)
700 free(unmodified_buffer);