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