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