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