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