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