Don't declare 'Chunk' as both public and private, that's just daft.
[oweals/cde.git] / cde / lib / csa / convert4-3.c
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 librararies 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: convert4-3.c /main/1 1996/04/21 19:22:43 drk $ */
24 /*
25  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
26  *  (c) Copyright 1993, 1994 International Business Machines Corp.
27  *  (c) Copyright 1993, 1994 Novell, Inc.
28  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
29  */
30
31 #include <EUSCompat.h>
32 #include <malloc.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include "rtable3.h"
36 #include "rtable4.h"
37 #include "convert4-3.h"
38
39 /*
40  * forward declaration of static functions
41  */
42 static void free_excpt3(Except_3 *e);
43 static Abb_Appt_3 * abb4_to_abb3(Abb_Appt_4 *a4);
44 static void apptid4_to_apptid3(Apptid_4 *from, Apptid_3 *to);
45 static Reminder_3 * reminder4_to_reminder3(Reminder_4 *r4);
46 static Table_Res_Type_3 tablerestype4_to_tablerestype3(Table_Res_Type_4 t);
47 static void tablereslist4_to_tablereslist3(Table_Res_List_4 *from,
48                 Table_Res_List_3 *to);
49 static void period4_to_period3(Period_4 *p4, Period_3 *p3);
50 static void assign_tag4to3(Tag_4 *t4, Tag_3 *t3);
51 static void assign_status4to3(Appt_Status_4 *s4, Appt_Status_3 *s3);
52 static void assign_privacy4to3(Privacy_Level_4 *p4, Privacy_Level_3 *p3);
53 static Buffer_3 buffer4_to_buffer3(Buffer_4 b);
54 static Tag_3 * tag4_to_tag3(Tag_4 *t4);
55 static Attribute_3 * attr4_to_attr3(Attribute_4 *a4);
56 static Except_3 * except4_to_except3(Except_4 *e4);
57 static Table_Args_Type_3 argstag4_to_argstag3(Table_Args_Type_4 t);
58 static void args4_to_args3(Args_4 *from, Args_3 *to);
59 static Table_Args_3 * tableargs4_to_tableargs3(Table_Args_4 *a4);
60 static Registration_3 * reg4_to_reg3(Registration_4 *r4);
61 static void free_tag3(Tag_3 *t);
62
63 /**************** DATA TYPE (4->3) CONVERSION ROUTINES **************/
64
65 extern void
66 _DtCm_id4_to_id3(Id_4 *from, Id_3 *to)
67 {
68         if ((from==NULL) || (to==NULL)) return; 
69         to->tick = from->tick;
70         to->key = from->key;
71 }
72
73 extern Uid_3 *
74 _DtCm_uid4_to_uid3(Uid_4 *ui4)
75 {
76         Uid_3 *ui3, *head, *prev;
77
78         prev = head = NULL;
79         while (ui4 != NULL) {
80                 ui3 = (Uid_3 *)calloc(1, sizeof(Uid_3));
81                 _DtCm_id4_to_id3(&(ui4->appt_id), &(ui3->appt_id));
82                 ui3->next = NULL;
83
84                 if (head == NULL)
85                         head = ui3;
86                 else
87                         prev->next = ui3;
88                 prev = ui3;
89
90                 ui4 = ui4->next;
91         }
92         return(head);
93 }
94
95 extern Appt_3 *
96 _DtCm_appt4_to_appt3(Appt_4 *a4)
97 {
98         Appt_3 *a3, *head, *prev;
99
100         prev = head = NULL;
101         while (a4 != NULL) {
102                 a3  = (Appt_3 *)calloc(1, sizeof(Appt_3));
103                 _DtCm_id4_to_id3(&(a4->appt_id), &(a3->appt_id));
104                 a3->tag = tag4_to_tag3(a4->tag);
105                 a3->duration = a4->duration;
106                 a3->ntimes = a4->ntimes;
107                 a3->what = buffer4_to_buffer3(a4->what);
108                 period4_to_period3(&(a4->period), &(a3->period));
109                 a3->author = buffer4_to_buffer3(a4->author);
110                 a3->client_data = buffer4_to_buffer3(a4->client_data);
111                 a3->exception = except4_to_except3(a4->exception);
112                 a3->attr = attr4_to_attr3(a4->attr);
113                 assign_status4to3(&a4->appt_status, &a3->appt_status);
114                 assign_privacy4to3(&a4->privacy, &a3->privacy);
115                 a3->next = NULL;
116
117                 if (head == NULL)
118                         head = a3;
119                 else
120                         prev->next = a3;
121                 prev = a3;
122
123                 a4 = a4->next;
124         }
125         return(head);
126 }
127
128 extern void
129 _DtCm_free_appt3(Appt_3 *a)
130 {
131         Appt_3 *ptr;
132
133         while (a != NULL) {
134                 ptr = a->next;
135
136                 if (a->tag != NULL)
137                         free_tag3(a->tag);
138
139                 if (a->what != NULL)
140                         free(a->what);
141
142                 if (a->author != NULL)
143                         free(a->author);
144
145                 if (a->client_data != NULL)
146                         free(a->client_data);
147
148                 if (a->attr != NULL)
149                         _DtCm_free_attr3(a->attr);
150
151                 if (a->exception != NULL)
152                         free_excpt3(a->exception);
153
154                 free(a);
155
156                 a = ptr;
157         }
158 }
159
160 extern void
161 _DtCm_free_attr3(Attribute_3 *a)
162 {
163         Attribute_3 *ptr;
164
165         while (a != NULL) {
166                 ptr = a->next;
167                 if (a->attr != NULL)
168                         free(a->attr);
169                 if (a->value != NULL)
170                         free(a->value);
171                 if (a->clientdata != NULL)
172                         free(a->clientdata);
173                 free(a);
174
175                 a = ptr;
176         }
177 }
178
179 static void
180 free_excpt3(Except_3 *e)
181 {
182         Except_3 *ptr;
183
184         while (e != NULL) {
185                 ptr = e->next;
186                 free(e);
187                 e = ptr;
188         }
189 }
190
191 static Abb_Appt_3 *
192 abb4_to_abb3(Abb_Appt_4 *a4)
193 {
194         Abb_Appt_3 *a3, *head, *prev;
195
196         prev = head = NULL;
197         while (a4 != NULL) {
198                 a3 = (Abb_Appt_3 *)calloc(1, sizeof(Abb_Appt_3));
199                 _DtCm_id4_to_id3(&(a4->appt_id), &(a3->appt_id));
200                 a3->tag = tag4_to_tag3(a4->tag);
201                 a3->what = buffer4_to_buffer3(a4->what);
202                 a3->duration = a4->duration;
203                 period4_to_period3(&(a4->period), &(a3->period));
204                 assign_status4to3(&a4->appt_status, &a3->appt_status);
205                 assign_privacy4to3(&a4->privacy, &a3->privacy);
206                 a3->next = NULL;
207
208                 if (head == NULL)
209                         head = a3;
210                 else
211                         prev->next = a3;
212                 prev = a3;
213
214                 a4 = a4->next;
215         }
216         return(head);
217 }
218
219 static void
220 apptid4_to_apptid3(Apptid_4 *from, Apptid_3 *to)
221 {
222         if (from==NULL || to==NULL) return;
223         _DtCm_id4_to_id3(from->oid, to->oid);
224         to->new_appt = _DtCm_appt4_to_appt3(from->new_appt);
225 }
226
227 static Reminder_3 *
228 reminder4_to_reminder3(Reminder_4 *r4)
229 {
230         Reminder_3 *r3, *head, *prev;
231         Attribute_3 *attr3;
232
233         prev = head = NULL;
234         while (r4 != NULL) {
235                 r3 = (Reminder_3 *)calloc(1, sizeof(Reminder_3));
236                 _DtCm_id4_to_id3(&(r4->appt_id), &(r3->appt_id));
237                 r3->tick = r4->tick;
238                 attr3 = attr4_to_attr3(&(r4->attr));
239                 r3->attr = *attr3;
240                 free(attr3);
241                 r3->next = NULL;
242
243                 if (head == NULL)
244                         head = r3;
245                 else
246                         prev->next = r3;
247                 prev = r3;
248
249                 r4 = r4->next;
250         }
251         return(head);
252 }
253
254 static Table_Res_Type_3
255 tablerestype4_to_tablerestype3(Table_Res_Type_4 t)
256 {
257         switch(t) {
258         case AP_4:
259                 return(AP_3);
260         case RM_4:
261                 return(RM_3);
262         case AB_4:
263                 return(AB_3);
264         case ID_4:
265                 return(ID_3);
266         default:
267                 return(AP_3);
268         }
269 }
270
271 static void
272 tablereslist4_to_tablereslist3(Table_Res_List_4 *from, Table_Res_List_3 *to)
273 {
274         if (from==NULL || to==NULL) return;
275         to->tag = tablerestype4_to_tablerestype3(from->tag);
276         switch (from->tag) {
277         case AP_4:
278                 to->Table_Res_List_3_u.a = _DtCm_appt4_to_appt3(
279                         from->Table_Res_List_4_u.a);
280                 break;
281         case RM_4:
282                 to->Table_Res_List_3_u.r = reminder4_to_reminder3(
283                         from->Table_Res_List_4_u.r);
284                 break;
285         case AB_4:
286                 to->Table_Res_List_3_u.b = abb4_to_abb3(
287                         from->Table_Res_List_4_u.b);
288                 break;
289         case ID_4:
290                 to->Table_Res_List_3_u.i = _DtCm_uid4_to_uid3(
291                         from->Table_Res_List_4_u.i);
292         default:
293                 return;
294         }
295 }
296  
297 extern Access_Status_3
298 _DtCm_accstat4_to_accstat3(Access_Status_4 s)
299 {
300         switch(s) {
301         case access_ok_4:
302                 return(access_ok_3);
303         case access_added_4:
304                 return(access_added_3);
305         case access_removed_4:
306                 return(access_removed_3);
307         case access_failed_4:
308                 return(access_failed_3);
309         case access_exists_4:
310                 return(access_exists_3);
311         case access_partial_4:
312                 return(access_partial_3);
313         case access_other_4:
314         default:
315                 return(access_other_3);
316         }
317 }
318
319 extern Table_Res_3 *
320 _DtCm_tableres4_to_tableres3(Table_Res_4 *r4)
321 {
322         Table_Res_3 *r3;
323
324         if (r4==NULL) return((Table_Res_3 *)NULL);
325         r3 = (Table_Res_3 *)calloc(1, sizeof(Table_Res_3));
326         r3->status = _DtCm_accstat4_to_accstat3(r4->status);
327         tablereslist4_to_tablereslist3(&(r4->res), &(r3->res));
328         return(r3);
329 }
330
331 extern Access_Entry_3 *
332 _DtCm_acclist4_to_acclist3(Access_Entry_4 *l4)
333 {
334         Access_Entry_3 *l3, *head, *prev;
335
336         prev = head = NULL;
337         while (l4 != NULL) {
338                 l3 = (Access_Entry_3 *)calloc(1, sizeof(Access_Entry_3));
339                 l3->who = buffer4_to_buffer3(l4->who);
340                 l3->access_type = l4->access_type;
341                 l3->next = NULL;
342
343                 if (head == NULL)
344                         head = l3;
345                 else
346                         prev->next = l3;
347                 prev = l3;
348
349                 l4 = l4->next;
350         }
351         return(head);
352 }
353  
354 extern Access_Args_3 *
355 _DtCm_accargs4_to_accargs3(Access_Args_4 *a4)
356 {
357         Access_Args_3 *a3;
358
359         if (a4==NULL) return((Access_Args_3 *)NULL);
360         a3 = (Access_Args_3 *)calloc(1, sizeof(Access_Args_3));
361         a3->target = buffer4_to_buffer3(a4->target);
362         a3->access_list = _DtCm_acclist4_to_acclist3(a4->access_list);
363         return(a3);
364 }
365
366 extern Range_3 *
367 _DtCm_range4_to_range3(Range_4 *r4)
368 {
369         Range_3 *r3, *head, *prev;
370
371         prev = head = NULL;
372         while (r4 != NULL) {
373                 r3 = (Range_3 *)calloc(1, sizeof(Range_3));
374                 r3->key1 = r4->key1;
375                 r3->key2 = r4->key2;
376                 r3->next = NULL;
377
378                 if (head == NULL)
379                         head = r3;
380                 else
381                         prev->next = r3;
382                 prev = r3;
383
384                 r4 = r4->next;
385         }
386         return(head);
387 }
388
389 extern Keyrange_3 *
390 _DtCm_keyrange4_to_keyrange3(Keyrange_4 *r4)
391 {
392         Keyrange_3 *r3, *head, *prev;
393  
394         prev = head = NULL;
395         while (r4 != NULL) {
396                 r3 = (Keyrange_3 *)calloc(1, sizeof(Keyrange_3));
397                 r3->key = r4->key;
398                 r3->tick1 = r4->tick1;
399                 r3->tick2 = r4->tick2;
400                 r3->next = NULL;
401
402                 if (head == NULL)
403                         head = r3;
404                 else
405                         prev->next = r3;
406                 prev = r3;
407
408                 r4 = r4->next;
409         }
410         return(head);
411 }
412
413 extern Registration_Status_3 
414 _DtCm_regstat4_to_regstat3(Registration_Status_4 s)
415 {
416         switch (s) {
417         case registered_4:
418                 return(registered_3);
419         case failed_4:
420                 return(failed_3);
421         case deregistered_4:
422                 return(deregistered_3);
423         case confused_4:
424                 return(confused_3);
425         case reg_notable_4:
426         default:
427                 return(failed_3);
428         }
429 }
430
431 extern Table_Status_3
432 _DtCm_tablestat4_to_tablestat3(Table_Status_4 s)
433 {
434         switch(s) {
435         case ok_4:
436                 return(ok_3);
437         case duplicate_4:
438                 return(duplicate_3);
439         case badtable_4:
440                 return(badtable_3);
441         case notable_4:
442                 return(notable_3);
443         case denied_4:
444                 return(denied_3);
445         case other_4:
446         default:
447                 return(other_3);
448         }
449 }
450
451 extern Uid_3 *
452 _DtCm_uidopt4_to_uid3(Uidopt_4 *uidopt)
453 {
454         Uid_3 *uid3, *head, *prev;
455  
456         prev = head = NULL;
457         while (uidopt != NULL) {
458                 uid3 = (Uid_3 *)calloc(1, sizeof(Uid_3));
459                 _DtCm_id4_to_id3(&(uidopt->appt_id), &(uid3->appt_id));
460                 uid3->next = NULL;
461
462                 if (head == NULL)
463                         head = uid3;
464                 else
465                         prev->next = uid3;
466                 prev = uid3;
467
468                 uidopt = uidopt->next;
469         }
470         return(head);
471 }
472  
473 /*
474  * Repeating event types beyond "yearly" are mapped to "single"
475  * because the old front end does not recognize any other types
476  * Worse yet it uses Interval to index into an array which
477  * contains strings up to "yearly", any period types beyond that
478  * would index beyond the array and cause the front end to dump core.
479  */
480 static void
481 period4_to_period3(Period_4 *p4, Period_3 *p3)
482 {
483         if (p3 == NULL || p4 == NULL) return;
484
485         switch (p4->period) {
486  
487         case single_4:  p3->period = single_3;
488                         p3->nth = p4->nth;
489                         break;
490  
491         case daily_4:   p3->period = daily_3;
492                         p3->nth = p4->nth;
493                         break;
494  
495         case weekly_4:  p3->period = weekly_3;
496                         p3->nth = p4->nth;
497                         break;
498  
499         case biweekly_4: p3->period = biweekly_3;
500                         p3->nth = p4->nth;
501                         break;
502  
503         case monthly_4: p3->period = monthly_3;
504                         p3->nth = p4->nth;
505                         break;
506  
507         case yearly_4:  p3->period = yearly_3;
508                         p3->nth = p4->nth;
509                         break;
510
511         default: p3->period = single_3;
512                  p3->nth = 0;
513                         break;
514         }
515 }
516
517  
518 static void
519 assign_tag4to3(Tag_4 *t4, Tag_3 *t3)
520 {
521         switch (t4->tag) {
522  
523         case appointment_4: t3->tag = appointment_3;
524                         break;
525  
526         case reminder_4: t3->tag = reminder_3;
527                         break;
528  
529         case otherTag_4: t3->tag = otherTag_3;
530                         break;
531  
532         case holiday_4: t3->tag = holiday_3;
533                         break;
534  
535         case toDo_4: t3->tag = toDo_3;
536                         break;
537         }
538 }
539
540  
541 static void
542 assign_status4to3(Appt_Status_4 *s4, Appt_Status_3 *s3)
543 {
544         switch (*s4) {
545  
546         case active_4: *s3 = active_3;
547                         break;
548  
549         case pendingAdd_4: *s3 = pendingAdd_3;
550                         break;
551  
552         case pendingDelete_4: *s3 = pendingDelete_3;
553                         break;
554  
555         case committed_4: *s3 = committed_3;
556                         break;
557  
558         case cancelled_4: *s3 = cancelled_3;
559                         break;
560  
561         case completed_4: *s3 = completed_3;
562                         break;
563         }
564 }
565
566  
567 static void
568 assign_privacy4to3(Privacy_Level_4 *p4, Privacy_Level_3 *p3)
569 {
570         switch (*p4) {
571  
572         case public_4: *p3 = public_3;
573                         break;
574  
575         case private_4: *p3 = private_3;
576                         break;
577  
578         case semiprivate_4: *p3 = semiprivate_3;
579                         break;
580         }
581 }
582
583
584
585
586 static Buffer_3
587 buffer4_to_buffer3(Buffer_4 b)
588 {
589         Buffer_3 copy;
590         if (b!=NULL)
591                 copy = strdup(b);
592         else
593                 copy = calloc(1, 1);
594         return(copy);
595 }
596
597 static Tag_3 *
598 tag4_to_tag3(Tag_4 *t4)
599 {
600         Tag_3 *t3, *head, *prev;
601
602         prev = head = NULL;
603         while (t4 != NULL) {
604                 t3 = (Tag_3 *)calloc(1, sizeof(Tag_3));
605                 assign_tag4to3(t4, t3);
606                 t3->showtime = t4->showtime;
607                 t3->next = NULL;
608
609                 if (head == NULL)
610                         head = t3;
611                 else
612                         prev->next = t3;
613                 prev = t3;
614
615                 t4 = t4->next;
616         }
617         return(head);
618 }
619
620 static Attribute_3 *
621 attr4_to_attr3(Attribute_4 *a4)
622 {
623         Attribute_3 *a3, *head, *prev;
624  
625         prev = head = NULL;
626         while (a4 != NULL) {
627                 a3 = (Attribute_3 *)calloc(1, sizeof(Attribute_3));
628                 a3->next = NULL;
629                 a3->attr = buffer4_to_buffer3(a4->attr);
630                 a3->value = buffer4_to_buffer3(a4->value);
631                 a3->clientdata = buffer4_to_buffer3(a4->clientdata);
632
633                 if (head == NULL)
634                         head = a3;
635                 else
636                         prev->next = a3;
637                 prev = a3;
638
639                 a4 = a4->next;
640         }
641         return(head);
642 }
643
644 static Except_3 *
645 except4_to_except3(Except_4 *e4)
646 {
647         Except_3 *e3, *head, *prev;
648
649         prev = head = NULL;
650         while (e4 != NULL) {
651                 e3 = (Except_3 *)calloc(1, sizeof(Except_3));
652                 e3->ordinal = e4->ordinal;
653                 e3->next=NULL;
654
655                 if (head == NULL)
656                         head = e3;
657                 else
658                         prev->next = e3;
659                 prev = e3;
660
661                 e4 = e4->next;
662         }
663         return(head);
664 }
665  
666 static Table_Args_Type_3
667 argstag4_to_argstag3(Table_Args_Type_4 t)
668 {
669         switch(t) {
670         case TICK_4:
671                 return(TICK_3);
672         case APPTID_4:
673                 return(APPTID_3);
674         case UID_4:
675                 return(UID_3);
676         case APPT_4:
677                 return(APPT_3);
678         case RANGE_4:
679                 return(RANGE_3);
680         case KEYRANGE_4:
681                 return(KEYRANGE_3);
682         default:
683                 return(TICK_3);
684         }
685 }
686
687 static void
688 args4_to_args3(Args_4 *from, Args_3 *to)
689 {
690         if (from==NULL || to==NULL) return;
691         to->tag = argstag4_to_argstag3(from->tag);
692         switch(from->tag) {
693         case TICK_4:
694                 to->Args_3_u.tick = from->Args_4_u.tick;
695                 break;
696         case APPTID_4:
697                 to->Args_3_u.apptid.oid = (Id_3 *)calloc(1, sizeof(Id_3));
698                 apptid4_to_apptid3(
699                         &(from->Args_4_u.apptid),
700                         &(to->Args_3_u.apptid));
701                 break;
702         case UID_4:
703                 to->Args_3_u.key = _DtCm_uid4_to_uid3(from->Args_4_u.key);
704                 break;
705         case APPT_4:
706                 to->Args_3_u.appt = _DtCm_appt4_to_appt3(from->Args_4_u.appt);
707                 break;
708         case RANGE_4:
709                 to->Args_3_u.range = _DtCm_range4_to_range3(from->Args_4_u.range);
710                 break;
711         case KEYRANGE_4:
712                 to->Args_3_u.keyrange = _DtCm_keyrange4_to_keyrange3(
713                         from->Args_4_u.keyrange);
714         default:
715                 break;
716         }
717 }
718
719 static Table_Args_3 *
720 tableargs4_to_tableargs3(Table_Args_4 *a4)
721 {
722         Table_Args_3 *a3;
723         
724         if (a4==NULL) return((Table_Args_3 *)NULL);
725         a3 = (Table_Args_3 *)calloc(1, sizeof(Table_Args_3));
726         a3->target = buffer4_to_buffer3(a4->target);
727         args4_to_args3(&(a4->args), &(a3->args));
728         a3->pid = a4->pid;
729         return(a3);
730 }
731
732 static Registration_3 *
733 reg4_to_reg3(Registration_4 *r4)
734 {
735         Registration_3 *r3, *head, *prev;
736
737         prev = head = NULL;
738         while (r4 != NULL) {
739                 r3 = (Registration_3 *)calloc(1, sizeof(Registration_3));
740                 r3->target = buffer4_to_buffer3(r4->target);
741                 r3->prognum = r4->prognum;
742                 r3->versnum = r4->versnum;
743                 r3->procnum = r4->procnum;
744                 r3->next = NULL;
745                 r3->pid = r4->pid;
746
747                 if (head == NULL)
748                         head = r3;
749                 else
750                         prev->next = r3;
751                 prev = r3;
752
753                 r4 = r4->next;
754         }
755         return(head);
756 }
757
758 static void
759 free_tag3(Tag_3 *t)
760 {
761         Tag_3 *ptr;
762
763         while (t != NULL) {
764                 ptr = t->next;
765                 free(t);
766                 t = ptr;
767         }
768 }
769