Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / lib / csa / convert4-2.c
1 /* $XConsortium: convert4-2.c /main/1 1996/04/21 19:22:37 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 "rtable2.h"
14 #include "rtable4.h"
15 #include "convert4-2.h"
16
17 /*
18  * forward declaration of static functions
19  */
20 static void free_excpt2(Except_2 *e);
21 static Buffer_2 buffer4_to_buffer2(Buffer_4 b);
22 static Period_2 period4_to_period2(Period_4 *p);
23 static Tag_2 tag4_to_tag2(Tag_4 *t);
24 static Attribute_2 * attr4_to_attr2(Attribute_4 *a4);
25 static Except_2 * except4_to_except2(Except_4 *e4);
26 static Abb_Appt_2 * abb4_to_abb2(Abb_Appt_4 *a4);
27 static Reminder_2 * reminder4_to_reminder2(Reminder_4 *r4);
28 static Table_Res_Type_2 tablerestype4_to_tablerestype2(Table_Res_Type_4 t);
29 static void tablereslist4_to_tablereslist2(Table_Res_List_4 *from,
30                 Table_Res_List_2 *to);
31 static Table_Args_Type_2 argstag4_to_argstag2(Table_Args_Type_4 t);
32 static void args4_to_args2(Args_4 *from, Args_2 *to);
33 static Table_Args_2 * tableargs4_to_tableargs2(Table_Args_4 *a4);
34 static Registration_2 * reg4_to_reg2(Registration_4 *r4);
35
36 /**************** DATA TYPE (4->2) CONVERSION ROUTINES **************/
37
38 extern void
39 _DtCm_id4_to_id2(Id_4 *from, Id_2 *to)
40 {
41         if ((from==NULL) || (to==NULL)) return; 
42         to->tick = from->tick;
43         to->key = from->key;
44 }
45
46 extern Uid_2 *
47 _DtCm_uid4_to_uid2(Uid_4 *ui4)
48 {
49         Uid_2 *ui2, *head, *prev;
50
51         prev = head = NULL;
52         while (ui4 != NULL) {
53                 ui2 = (Uid_2 *)calloc(1, sizeof(Uid_2));
54                 _DtCm_id4_to_id2(&(ui4->appt_id), &(ui2->appt_id));
55                 ui2->next = NULL;
56
57                 if (head == NULL)
58                         head = ui2;
59                 else
60                         prev->next = ui2;
61                 prev = ui2;
62
63                 ui4 = ui4->next;
64         }
65         return(head);
66 }
67
68 extern Appt_2 *
69 _DtCm_appt4_to_appt2(Appt_4 *a4)
70 {
71         Appt_2 *a2, *head, *prev;
72         struct Attribute_4 *item;
73
74         prev = head = NULL;
75         while (a4 != NULL) {
76                 a2  = (Appt_2 *)calloc(1, sizeof(Appt_2));
77                 _DtCm_id4_to_id2(&(a4->appt_id), &(a2->appt_id));
78                 a2->tag = tag4_to_tag2(a4->tag);
79                 a2->duration = a4->duration;
80                 a2->ntimes = a4->ntimes;
81                 a2->what = buffer4_to_buffer2(a4->what);
82                 a2->script = "";
83                 a2->period = period4_to_period2(&(a4->period));
84                 a2->author = buffer4_to_buffer2(a4->author);
85                 a2->client_data = buffer4_to_buffer2(a4->client_data);
86                 a2->attr = attr4_to_attr2(a4->attr);
87
88                 /* Pick the mailto field out of the attribute list
89                    client data field; put it back into the appt struct
90                    proper.
91                 */
92                 item = a4->attr;
93                 while(item!=NULL) {
94                         if (strcmp(item->attr, "ml")==0) {
95                                 a2->mailto=buffer4_to_buffer2(item->clientdata);
96                                 break;
97                         }
98                         item=item->next;
99                 }
100
101                 a2->exception = except4_to_except2(a4->exception);
102                 a2->next = NULL;
103
104                 if (head == NULL)
105                         head = a2;
106                 else
107                         prev->next = a2;
108                 prev = a2;
109
110                 a4 = a4->next;
111         }
112         return(head);
113 }
114
115 extern Access_Status_2
116 _DtCm_accstat4_to_accstat2(Access_Status_4 s)
117 {
118         switch(s) {
119         case access_ok_4:
120                 return(access_ok_2);
121         case access_added_4:
122                 return(access_added_2);
123         case access_removed_4:
124                 return(access_removed_2);
125         case access_failed_4:
126                 return(access_failed_2);
127         case access_exists_4:
128                 return(access_exists_2);
129         case access_partial_4:
130                 return(access_partial_2);
131         case access_other_4:
132         default:
133                 return(access_other_2);
134         }
135 }
136
137 extern Table_Res_2 *
138 _DtCm_tableres4_to_tableres2(Table_Res_4 *r4)
139 {
140         Table_Res_2 *r2;
141
142         if (r4==NULL) return((Table_Res_2 *)NULL);
143         r2 = (Table_Res_2 *)calloc(1, sizeof(Table_Res_2));
144         r2->status = _DtCm_accstat4_to_accstat2(r4->status);
145         tablereslist4_to_tablereslist2(&(r4->res), &(r2->res));
146         return(r2);
147 }
148
149 extern Access_Entry_2 *
150 _DtCm_acclist4_to_acclist2(Access_Entry_4 *l4)
151 {
152         Access_Entry_2 *l2, *head, *prev;
153
154         prev = head = NULL;
155         while (l4 != NULL) {
156                 l2 = (Access_Entry_2 *)calloc(1, sizeof(Access_Entry_2));
157                 l2->who = buffer4_to_buffer2(l4->who);
158                 l2->access_type = l4->access_type;
159                 l2->next = NULL;
160
161                 if (head == NULL)
162                         head = l2;
163                 else
164                         prev->next = l2;
165                 prev = l2;
166
167                 l4 = l4->next;
168         }
169         return(head);
170 }
171  
172 extern Access_Args_2 *
173 _DtCm_accargs4_to_accargs2(Access_Args_4 *a4)
174 {
175         Access_Args_2 *a2;
176
177         if (a4==NULL) return((Access_Args_2 *)NULL);
178         a2 = (Access_Args_2 *)calloc(1, sizeof(Access_Args_2));
179         a2->target = buffer4_to_buffer2(a4->target);
180         a2->access_list = _DtCm_acclist4_to_acclist2(a4->access_list);
181         return(a2);
182 }
183
184 extern Range_2 *
185 _DtCm_range4_to_range2(Range_4 *r4)
186 {
187         Range_2 *r2, *head, *prev;
188
189         prev = head = NULL;
190         while (r4 != NULL) {
191                 r2 = (Range_2 *)calloc(1, sizeof(Range_2));
192                 r2->key1 = r4->key1;
193                 r2->key2 = r4->key2;
194                 r2->next = NULL;
195
196                 if (head == NULL)
197                         head = r2;
198                 else
199                         prev->next = r2;
200                 prev = r2;
201
202                 r4 = r4->next;
203         }
204         return(head);
205 }
206
207 extern Registration_Status_2 
208 _DtCm_regstat4_to_regstat2(Registration_Status_4 s)
209 {
210         switch (s) {
211         case registered_4:
212                 return(registered_2);
213         case failed_4:
214                 return(failed_2);
215         case deregistered_4:
216                 return(deregistered_2);
217         case confused_4:
218                 return(confused_2);
219         case reg_notable_4:
220         default:
221                 return(failed_2);
222         }
223 }
224
225 extern Table_Status_2
226 _DtCm_tablestat4_to_tablestat2(Table_Status_4 s)
227 {
228         switch(s) {
229         case ok_4:
230                 return(ok_2);
231         case duplicate_4:
232                 return(duplicate_2);
233         case badtable_4:
234                 return(badtable_2);
235         case notable_4:
236                 return(notable_2);
237         case denied_4:
238                 return(denied_2);
239         case other_4:
240         default:
241                 return(other_2);
242         }
243 }
244
245 extern Uid_2 *
246 _DtCm_uidopt4_to_uid2(Uidopt_4 *uidopt)
247 {
248         Uid_2 *uid2, *head, *prev;
249  
250         prev = head = NULL;
251         while (uidopt != NULL) {
252                 uid2 = (Uid_2 *)calloc(1, sizeof(Uid_2));
253                 _DtCm_id4_to_id2(&(uidopt->appt_id), &(uid2->appt_id));
254                 uid2->next = NULL;
255
256                 if (head == NULL)
257                         head = uid2;
258                 else
259                         prev->next = uid2;
260                 prev = uid2;
261
262                 uidopt = uidopt->next;
263         }
264         return(head);
265 }
266  
267 extern void
268 _DtCm_free_attr2(Attribute_2 *a)
269 {
270         Attribute_2 *ptr;
271
272         while (a != NULL) {
273                 ptr = a->next;
274                 if (a->attr != NULL)
275                         free(a->attr);
276                 if (a->value != NULL)
277                         free(a->value);
278                 free(a);
279
280                 a = ptr;
281         }
282 }
283
284 extern void
285 _DtCm_free_appt2(Appt_2 *a)
286 {
287         Appt_2 *ptr;
288
289         while (a != NULL) {
290                 ptr = a->next;
291
292                 if (a->what != NULL)
293                         free(a->what);
294
295                 if (a->mailto != NULL)
296                         free(a->mailto);
297
298                 if (a->script != NULL)
299                         free(a->script);
300
301                 if (a->author != NULL)
302                         free(a->author);
303
304                 if (a->client_data != NULL)
305                         free(a->client_data);
306
307                 if (a->attr != NULL)
308                         _DtCm_free_attr2(a->attr);
309
310                 if (a->exception != NULL)
311                         free_excpt2(a->exception);
312
313                 free(a);
314
315                 a = ptr;
316         }
317 }
318
319 static void
320 free_excpt2(Except_2 *e)
321 {
322         Except_2 *ptr;
323
324         while (e != NULL) {
325                 ptr = e->next;
326                 free(e);
327                 e = ptr;
328         }
329 }
330
331 static Buffer_2
332 buffer4_to_buffer2(Buffer_4 b)
333 {
334         Buffer_2 copy;
335         if (b!=NULL)
336                 copy = strdup(b);
337         else
338                 copy = calloc(1, 1);
339         return(copy);
340 }
341
342 /*
343  * Repeating event types beyond "yearly" are mapped to "single"
344  * because the old front end does not recognize any other types
345  * Worse yet it uses Period_2 to index into an array which
346  * contains strings up to "yearly", any period types beyond that
347  * would index beyond the array and cause the front end to dump core.
348  */
349 static Period_2
350 period4_to_period2(Period_4 *p)
351 {
352         if (p==NULL) return(otherPeriod_2);
353         switch (p->period) {
354         case single_4:
355                 return(single_2);
356         case daily_4:
357                 return(daily_2);
358         case weekly_4:
359                 return(weekly_2);
360         case biweekly_4:
361                 return(biweekly_2);
362         case monthly_4:
363                 return(monthly_2);
364         case yearly_4:
365                 return(yearly_2);
366         default:
367                 return(single_2);
368         }
369 }
370
371 static Tag_2
372 tag4_to_tag2(Tag_4 *t)
373 {
374         if (t==NULL) return(otherTag_2);
375         switch(t->tag) {
376         case appointment_4:
377                 return(appointment_2);
378         case reminder_4:
379                 return(reminder_2);
380         case otherTag_4:
381         case holiday_4:
382         case toDo_4:
383         default:
384                 return(otherTag_2);
385         }
386 }
387
388 static Attribute_2 *
389 attr4_to_attr2(Attribute_4 *a4)
390 {
391         Attribute_2 *a2, *head, *prev;
392  
393         prev = head = NULL;
394         while (a4 != NULL) {
395                 a2 = (Attribute_2 *)calloc(1, sizeof(Attribute_2));
396                 a2->next = NULL;
397                 a2->attr = strdup(a4->attr);
398                 a2->value = strdup(a4->value);
399
400                 if (head == NULL)
401                         head = a2;
402                 else
403                         prev->next = a2;
404                 prev = a2;
405
406                 a4 = a4->next;
407         }
408         return(head);
409 }
410
411 static Except_2 *
412 except4_to_except2(Except_4 *e4)
413 {
414         Except_2 *e2, *head, *prev;
415
416         prev = head = NULL;
417         while (e4 != NULL) {
418                 e2 = (Except_2 *)calloc(1, sizeof(Except_2));
419                 e2->ordinal = e4->ordinal;
420                 e2->next=NULL;
421
422                 if (head == NULL)
423                         head = e2;
424                 else
425                         prev->next = e2;
426                 prev = e2;
427
428                 e4 = e4->next;
429         }
430         return(head);
431 }
432  
433 static Abb_Appt_2 *
434 abb4_to_abb2(Abb_Appt_4 *a4)
435 {
436         Abb_Appt_2 *a2, *head, *prev;
437
438         prev = head = NULL;
439         while (a4 != NULL) {
440                 a2 = (Abb_Appt_2 *)calloc(1, sizeof(Abb_Appt_2));
441                 _DtCm_id4_to_id2(&(a4->appt_id), &(a2->appt_id));
442                 a2->what = buffer4_to_buffer2(a4->what);
443                 a2->duration = a4->duration;
444                 a2->period = period4_to_period2(&(a4->period));
445                 a2->next = NULL;
446
447                 if (head == NULL)
448                         head = a2;
449                 else
450                         prev->next = a2;
451                 prev = a2;
452
453                 a4 = a4->next;
454         }
455         return(head);
456 }
457
458 static void
459 apptid4_to_apptid2(Apptid_4 *from, Apptid_2 *to)
460 {
461         if (from==NULL || to==NULL) return;
462         _DtCm_id4_to_id2(from->oid, to->oid);
463         to->new_appt = _DtCm_appt4_to_appt2(from->new_appt);
464 }
465
466 static Reminder_2 *
467 reminder4_to_reminder2(Reminder_4 *r4)
468 {
469         Reminder_2 *r2, *head, *prev;
470         Attribute_2 *attr2;
471
472         prev = head = NULL;
473         while (r4 != NULL) {
474                 r2 = (Reminder_2 *)calloc(1, sizeof(Reminder_2));
475                 _DtCm_id4_to_id2(&(r4->appt_id), &(r2->appt_id));
476                 r2->tick = r4->tick;
477                 attr2 = attr4_to_attr2(&(r4->attr));
478                 r2->attr = *attr2;
479                 free(attr2);
480                 r2->next = NULL;
481
482                 if (head == NULL)
483                         head = r2;
484                 else
485                         prev->next = r2;
486                 prev = r2;
487
488                 r4 = r4->next;
489         }
490         return(head);
491 }
492
493 static Table_Res_Type_2
494 tablerestype4_to_tablerestype2(Table_Res_Type_4 t)
495 {
496         switch(t) {
497         case AP_4:
498                 return(AP_2);
499         case RM_4:
500                 return(RM_2);
501         case AB_4:
502                 return(AB_2);
503         case ID_4:
504                 return(ID_2);
505         default:
506                 return(AP_2);
507         }
508 }
509
510 static void
511 tablereslist4_to_tablereslist2(Table_Res_List_4 *from, Table_Res_List_2 *to)
512 {
513         if (from==NULL || to==NULL) return;
514         to->tag = tablerestype4_to_tablerestype2(from->tag);
515         switch (from->tag) {
516         case AP_4:
517                 to->Table_Res_List_2_u.a = _DtCm_appt4_to_appt2(
518                         from->Table_Res_List_4_u.a);
519                 break;
520         case RM_4:
521                 to->Table_Res_List_2_u.r = reminder4_to_reminder2(
522                         from->Table_Res_List_4_u.r);
523                 break;
524         case AB_4:
525                 to->Table_Res_List_2_u.b = abb4_to_abb2(
526                         from->Table_Res_List_4_u.b);
527                 break;
528         case ID_4:
529                 to->Table_Res_List_2_u.i = _DtCm_uid4_to_uid2(
530                         from->Table_Res_List_4_u.i);
531         default:
532                 return;
533         }
534 }
535  
536 static Table_Args_Type_2
537 argstag4_to_argstag2(Table_Args_Type_4 t)
538 {
539         switch(t) {
540         case TICK_4:
541                 return(TICK_2);
542         case APPTID_4:
543                 return(APPTID_2);
544         case UID_4:
545                 return(UID_2);
546         case APPT_4:
547                 return(APPT_2);
548         case RANGE_4:
549                 return(RANGE_2);
550         default:
551                 return(TICK_2);
552         }
553 }
554
555 static void
556 args4_to_args2(Args_4 *from, Args_2 *to)
557 {
558         if (from==NULL || to==NULL) return;
559         to->tag = argstag4_to_argstag2(from->tag);
560         switch(from->tag) {
561         case TICK_4:
562                 to->Args_2_u.tick = from->Args_4_u.tick;
563                 break;
564         case APPTID_4:
565                 to->Args_2_u.apptid.oid = (Id_2 *)calloc(1, sizeof(Id_2));
566                 apptid4_to_apptid2(
567                         &(from->Args_4_u.apptid),
568                         &(to->Args_2_u.apptid));
569                 break;
570         case UID_4:
571                 to->Args_2_u.key = _DtCm_uid4_to_uid2(from->Args_4_u.key);
572                 break;
573         case APPT_4:
574                 to->Args_2_u.appt = _DtCm_appt4_to_appt2(from->Args_4_u.appt);
575                 break;
576         case RANGE_4:
577                 to->Args_2_u.range = _DtCm_range4_to_range2(from->Args_4_u.range);
578                 break;
579         default:
580                 break;
581         }
582 }
583
584 static Table_Args_2 *
585 tableargs4_to_tableargs2(Table_Args_4 *a4)
586 {
587         Table_Args_2 *a2;
588         
589         if (a4==NULL) return((Table_Args_2 *)NULL);
590         a2 = (Table_Args_2 *)calloc(1, sizeof(Table_Args_2));
591         a2->target = buffer4_to_buffer2(a4->target);
592         args4_to_args2(&(a4->args), &(a2->args));
593         return(a2);
594 }
595
596 static Registration_2 *
597 reg4_to_reg2(Registration_4 *r4)
598 {
599         Registration_2 *r2, *head, *prev;
600
601         prev = head = NULL;
602         while (r4 != NULL) {
603                 r2 = (Registration_2 *)calloc(1, sizeof(Registration_2));
604                 r2->target = buffer4_to_buffer2(r4->target);
605                 r2->prognum = r4->prognum;
606                 r2->versnum = r4->versnum;
607                 r2->procnum = r4->procnum;
608                 r2->next = NULL;
609
610                 if (head == NULL)
611                         head = r2;
612                 else
613                         prev->next = r2;
614                 prev = r2;
615
616                 r4 = r4->next;
617         }
618         return(head);
619 }
620