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