source: libabac/abac_term.c @ b1d4721

mei_rt2mei_rt2_fix_1
Last change on this file since b1d4721 was b1d4721, checked in by Mei <mei@…>, 12 years ago

1) add couple of forward declaration to remove compiler warnings

  • Property mode set to 100644
File size: 27.5 KB
Line 
1
2/***********************************************************
3   abac_term.c
4
5 dterm=data term of rt1
6 term is a kind of dterm
7***********************************************************/
8#include <assert.h>
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12#include <ctype.h>
13#include <time.h>
14
15#include "abac_internal.h"
16#include "abac_util.h"
17
18static int debug=0;
19extern char *strptime(const char *s, const char *format, struct tm *tm);
20
21/* itemtype is
22     e_ITEM_MIN=1,
23     e_ITEM_MAX=2,
24     e_ITEM_TARGET=3,
25 [a..b], [a..], [..b], [a],[a,b],[many a]
26*/
27struct _abac_item_t {
28    int itemtype;
29    char *val;
30};
31
32/* condtype is
33     e_COND_ROLE = 1,
34     e_COND_OSET = 2,
35     e_COND_RANGE = 3
36   vartype is one of termtype
37   string will contain either
38       the range literal (static constraint)
39      or
40       the oset/role prologized string (dynamic constraint)
41      it is set if this condition has been pre-processed for codegen
42   of_aspect ptr will only be set only if it is a dynamic
43       constraint
44   range_list is set only if needed
45*/
46struct _abac_condition_t {
47    int condtype;
48    int vartype;
49    char *string; 
50    abac_list_t *range_list;
51    abac_aspect_t *of_aspect;
52};
53
54/* termtype_t/name pair for the data terms
55   idtype_t/name/p_name pair for the named principal term
56*/
57struct _abac_term_t {
58    int type;
59    char *name;
60    int isnamed;
61    char *p_name; 
62    char *cn; //XXX ?? might not need this at all
63    abac_condition_t *constraint;
64}; 
65
66/* new, free, add one of terms */
67struct _abac_param_list_t {
68    abac_list_t *list;
69};
70
71/******************************************************************/
72char *abac_condtype_string(int i)
73{
74    if(i>_condname_cnt)
75        panic("abac_condtype_string: went out of range on condname");
76    return (char *) _condname[i];
77}
78char *abac_termtype_string(int i)
79{
80    if(i>_termname_cnt)
81        panic("abac_termtype_string: went out of range on termname");
82    return (char *) _termname[i];
83}
84
85int abac_verify_term_type(char *type) {
86    int i;
87
88    if (type == NULL)
89        panic("abac_verify_term_type: fail with NULL type\n");
90
91    for (i = 1; i <= _termname_cnt ; i++)
92        if(strcmp(type,_termname[i])==0)
93            return i;
94
95    printf("abac_verify_term_type: weird (%s)\n", type);
96    panic("abac_verify_term_type: fail with unfounded type \n");
97}
98
99int abac_max_item_type() { return e_ITEM_MAX;}
100int abac_min_item_type() { return e_ITEM_MIN;}
101int abac_target_item_type() { return e_ITEM_TARGET;}
102
103int abac_verify_item_type(char *type) {
104    int i;
105
106    if (type == NULL)
107        panic("abac_verify_item_type: fail with NULL type\n");
108
109    for (i = 1; i <= _itemname_cnt ; i++)
110        if(strcmp(type,_itemname[i])==0)
111            return i;
112    panic("abac_verify_item_type: fail with unfounded type\n");
113}
114
115char *abac_range_string(abac_list_t *ptr)
116{
117    assert(ptr);
118    char *tmp=NULL;
119    char *min=NULL;
120    char *max=NULL;
121    char *val=NULL;
122    int type;
123
124    abac_item_t *cur;
125    assert(ptr);
126    abac_list_foreach(ptr, cur,
127        type=cur->itemtype;
128        switch (type) {
129            case e_ITEM_MIN:
130                min=abac_xstrdup(cur->val);
131                break;
132            case e_ITEM_MAX:
133                max=abac_xstrdup(cur->val);
134                break;
135            case e_ITEM_TARGET:
136                if(val)
137                    asprintf(&val,"%s,%s",val,cur->val);
138                else val=abac_xstrdup(cur->val);
139                break;
140        }
141    );
142    if(max && min) {
143         asprintf(&tmp,"[%s .. %s]",min,max);
144         free(max);
145         free(min);
146         return tmp;
147    }
148    if(max) {
149         asprintf(&tmp,"[.. %s]",max);
150         free(max);
151         return tmp;
152    }
153    if(min) {
154         asprintf(&tmp,"[%s ..]",min);
155         free(min);
156         return tmp;
157    }
158    if(val) {
159         asprintf(&tmp,"[%s]",val);
160         free(val);
161         return tmp;
162    }
163    return NULL;
164}
165/******************************************************************/
166abac_item_t *abac_item_new(int itype, char *val)
167{
168     abac_item_t *nptr=
169             (abac_item_t *)abac_xmalloc(sizeof(abac_item_t));
170     nptr->itemtype=itype;
171     nptr->val=abac_xstrdup(val);
172     return nptr;
173}
174
175void abac_item_free(abac_item_t *ptr)
176{
177     assert(ptr);
178     assert(ptr->val);
179     free(ptr->val);
180     free(ptr);
181}
182
183abac_item_t *abac_item_dup(abac_item_t *ptr)
184{
185     abac_item_t *nptr=
186             (abac_item_t *)abac_xmalloc(sizeof(abac_item_t));
187     nptr->itemtype=ptr->itemtype;
188     nptr->val=abac_xstrdup(ptr->val);
189     return nptr;
190}
191
192int abac_item_type(abac_item_t *ptr)
193{
194    return ptr->itemtype;
195}
196
197char *abac_item_val(abac_item_t *ptr)
198{
199    return ptr->val;
200}
201
202/******************************************************************/
203static abac_condition_t *_abac_condition_new(int type, int vtype,
204char* condstr,void *cptr)
205{
206     if(debug) {
207         printf("   abac_condition_new: \n");
208         printf("     condtype is %d(%s)\n", type, abac_condtype_string(type));
209         printf("     vtype is %d(%s)\n", vtype, abac_termtype_string(type));
210         printf("     condstr is (%s)\n", condstr);
211         if(cptr) printf("     yes on cptr\n");
212             else printf("     no on cptr\n");
213     }
214     abac_condition_t *ptr=
215             (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t));
216     ptr->condtype=type;
217     ptr->vartype=vtype;
218     ptr->string=abac_xstrdup(condstr);
219     ptr->of_aspect=NULL;
220     ptr->range_list=NULL;
221     if(type==e_COND_RANGE) {
222          ptr->range_list=(abac_list_t *)cptr;
223          } else {
224             if(cptr) ptr->of_aspect=abac_aspect_dup((abac_aspect_t *)cptr);
225     }
226     return ptr;
227}
228
229void abac_condition_free(abac_condition_t *ptr)
230{
231     switch(ptr->condtype) {
232        case e_COND_OSET:
233        case e_COND_ROLE:
234            abac_aspect_free(ptr->of_aspect);
235            break;
236        case e_COND_RANGE:
237            /* do nothing */
238            break;
239     }
240     if(ptr->string) free(ptr->string);
241     if(ptr->range_list != NULL) {
242         abac_item_t *cur;
243         abac_list_foreach(ptr->range_list, cur,
244           abac_item_free(cur);
245         );
246         abac_list_free(ptr->range_list);
247     }
248     free(ptr);
249}
250
251abac_aspect_t *abac_condition_of_aspect(abac_condition_t *ptr)
252{
253   return ptr->of_aspect;
254}
255
256int abac_condition_is_range(abac_condition_t *ptr)
257{
258   if(ptr->condtype==e_COND_RANGE)
259      return 1;
260      else return 0;
261}
262
263/* called from backend, intended as stub for range constraint */
264abac_condition_t *abac_condition_create(char *vtype)
265{
266     abac_condition_t *nptr=
267             (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t));
268     nptr->condtype=e_COND_RANGE;
269     nptr->vartype=abac_verify_term_type(vtype);
270     nptr->string=NULL;
271     nptr->range_list=NULL;
272     nptr->of_aspect=NULL;
273     return nptr;
274}
275
276abac_condition_t *abac_condition_create_from_aspect(abac_aspect_t *ptr)
277{
278     assert(ptr);
279     abac_condition_t *nptr=
280             (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t));
281     if(abac_aspect_is_role(ptr))
282         nptr->condtype=e_COND_ROLE;
283         else nptr->condtype=e_COND_OSET;
284     nptr->vartype=e_TERM_PRINCIPAL;
285     nptr->string=NULL;
286     nptr->range_list=NULL;
287     nptr->of_aspect=abac_aspect_dup(ptr);
288     return nptr;
289}
290
291/* make a copy */
292abac_condition_t *abac_condition_dup(abac_condition_t *ptr)
293{
294     abac_condition_t *nptr=
295             (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t));
296     nptr->condtype=ptr->condtype;
297     nptr->vartype=ptr->vartype;
298     if(ptr->string)
299         nptr->string=abac_xstrdup(ptr->string);
300         else nptr->string=NULL;
301     if(ptr->of_aspect)
302         nptr->of_aspect=abac_aspect_dup(ptr->of_aspect);
303         else nptr->of_aspect=NULL;
304
305     if(ptr->range_list!=NULL) {
306         nptr->range_list=abac_list_new();
307         abac_item_t *cur;
308         abac_item_t *nitem;
309         abac_list_foreach(ptr->range_list, cur,
310           nitem=abac_item_dup(cur);
311           abac_list_add(nptr->range_list,nitem); 
312         );
313         } else {
314             nptr->range_list=NULL;
315     }
316     return nptr;
317}
318
319int abac_condition_vartype(abac_condition_t *ptr)
320{
321    return ptr->vartype;
322}
323
324abac_list_t *abac_condition_range_list(abac_condition_t *ptr)
325{
326     assert(ptr);
327     return ptr->range_list;
328}
329
330
331/***********************************************************************/
332static void _abac_condition_add_range_item(abac_condition_t *ptr, int itype, char* val)
333{
334    abac_item_t *nitem=abac_item_new(itype,val);
335    if(ptr->range_list==NULL)
336       ptr->range_list=abac_list_new();
337    abac_list_add(ptr->range_list,nitem);
338}
339
340void abac_condition_add_range_integer_item(abac_condition_t *ptr, int itype, int val)
341{
342    if(abac_condition_vartype(ptr) != abac_verify_term_type("integer"))
343       panic("abac_condition_add_range_integer_item, mismatched contraint type to abac integer term\n");
344    char *tmp=NULL;
345    asprintf(&tmp,"%d",val);
346    _abac_condition_add_range_item(ptr,itype,tmp);
347}
348void abac_condition_add_range_float_item(abac_condition_t *ptr, int itype, float val)
349{
350    if(abac_condition_vartype(ptr) != abac_verify_term_type("float"))
351       panic("abac_condition_add_range_float_item, mismatched contraint type to abac float term\n");
352    char *tmp=NULL;
353    asprintf(&tmp,"%f",val);
354    _abac_condition_add_range_item(ptr,itype,tmp);
355}
356
357void abac_condition_add_range_time_item(abac_condition_t *ptr, int itype, char* val)
358{
359    if(abac_condition_vartype(ptr) != abac_verify_term_type("time"))
360       panic("abac_condition_add_range_time_item, mismatched contraint type to abac time term\n");
361    _abac_condition_add_range_item(ptr,itype,val);
362}
363
364void abac_condition_add_range_urn_item(abac_condition_t *ptr, char *val)
365{
366    if(abac_condition_vartype(ptr) != abac_verify_term_type("urn"))
367       panic("abac_condition_add_range_urn_item, mismatched contraint type to abac urn term\n");
368    _abac_condition_add_range_item(ptr,e_ITEM_TARGET,val);
369}
370
371void abac_condition_add_range_string_item(abac_condition_t *ptr, char *val)
372{
373    if(abac_condition_vartype(ptr) != abac_verify_term_type("string"))
374       panic("abac_condition_add_range_string_item, mismatched contraint type to abac string term\n");
375    _abac_condition_add_range_item(ptr,e_ITEM_TARGET,val);
376}
377
378void abac_condition_add_range_boolean_item(abac_condition_t *ptr, char* val)
379{
380    if(abac_condition_vartype(ptr) != abac_verify_term_type("boolean"))
381       panic("abac_condition_add_range_boolean_item, mismatched contraint type to abac boolen term\n");
382    if(strcmp(val,"true")==0 || strcmp(val,"false")==0) {
383        _abac_condition_add_range_item(ptr,e_ITEM_TARGET,val);
384        } else {
385           panic("abac_condition_add_range_boolean_item, invalid matching value\n");
386    }
387}
388
389
390char *abac_condition_string(abac_condition_t *ptr)
391{
392     char *string=NULL;
393     switch(ptr->condtype) {
394        case e_COND_OSET:
395        case e_COND_ROLE:
396            string=abac_aspect_string_with_condition(ptr->of_aspect);
397            return string;
398            break;
399        case e_COND_RANGE:
400            if(ptr->string != NULL) {
401                string=abac_xstrdup(ptr->string);
402                return string;
403            }
404            if(ptr->range_list != NULL) {
405                string=abac_range_string(ptr->range_list);
406                return string;
407            }
408            break;
409     }
410     return string;
411}
412
413char *abac_condition_typed_string(abac_condition_t *ptr)
414{
415     char *string=NULL;
416     switch(ptr->condtype) {
417        case e_COND_OSET:
418        case e_COND_ROLE:
419            string=abac_aspect_typed_string_with_condition(ptr->of_aspect);
420            return string;
421            break;
422        case e_COND_RANGE:
423            if(ptr->string != NULL) {
424                string=abac_xstrdup(ptr->string);
425                return string;
426            }
427            if(ptr->range_list != NULL) {
428                string=abac_range_string(ptr->range_list);
429                return string;
430            }
431            break;
432     }
433     return string;
434}
435
436int abac_condition_set_range_string(abac_condition_t *ptr)
437{
438   char *string= abac_range_string(ptr->range_list);
439   ptr->string=string;
440   return 1;
441}
442
443int abac_condition_set_aspect_string(abac_condition_t *ptr, char *str)
444{
445   ptr->string=abac_xstrdup(str);
446   return 1;
447}
448
449int abac_condition_set_aspect_ptr(abac_condition_t *ptr, abac_aspect_t *aptr)
450{
451   ptr->of_aspect=abac_aspect_dup(aptr);
452   return 1;
453}
454
455void abac_condition_dump(abac_condition_t *ptr, char *offset)
456{
457   printf("...constraint...\n");
458   printf("%s condtype: %d(%s)\n",
459                offset, ptr->condtype,abac_condtype_string(ptr->condtype));
460   printf("%s vartype: %d(%s)\n",
461                offset, ptr->vartype,abac_termtype_string(ptr->vartype));
462   if(ptr->string)
463       printf("%s string: %s\n", offset, ptr->string);
464       else printf("%s string: --\n", offset);
465   if(ptr->range_list)
466       printf("%s range_list: %s\n",
467                     offset, abac_range_string(ptr->range_list));
468       else printf("%s range_list: --\n", offset);
469   if(ptr->of_aspect) {
470       char *tmp=NULL;
471       asprintf(&tmp,"  %s",offset);
472       abac_aspect_dump(ptr->of_aspect,tmp);
473       free(tmp);
474       } else printf("%s of_aspect: --\n", offset);
475   printf("................\n");
476}
477
478/******************************************************************/
479int abac_term_isvar(abac_term_t *term)
480{
481    if(isupper(term->name[0])) return 1;
482       else return 0;
483} 
484
485int abac_term_isnamed(abac_term_t *term)
486{
487    if(term->isnamed) return 1;
488       else return 0;
489}
490
491char *abac_term_name(abac_term_t *term)
492{
493    if(ABAC_IN_PROLOG && term->p_name) {
494        return term->p_name;
495    }
496    return term->name;
497}
498
499char *abac_term_cn(abac_term_t *term)
500{
501    return term->cn;
502}
503
504abac_condition_t *abac_term_constraint(abac_term_t *term)
505{
506    return term->constraint;
507}
508
509char *abac_term_type_name(abac_term_t *term)
510{
511    if(term->isnamed)
512        return abac_idtype_string(term->type);
513    return abac_termtype_string(term->type);
514}
515
516int abac_term_type(abac_term_t *term)
517{
518    return term->type;
519}
520
521static bool _abac_termtype_is_time_type(int i)
522{
523    if (i == e_TERM_TIME) 
524       return 1;
525    return 0;
526}
527
528bool abac_term_is_time_type(abac_term_t *term)
529{
530    return _abac_termtype_is_time_type(term->type);
531}
532
533static bool _abac_termtype_is_string_type(int i)
534{
535    if (i == e_TERM_STRING) 
536       return 1;
537    return 0;
538}
539
540bool abac_term_is_string(abac_term_t *term)
541{
542    return _abac_termtype_is_string_type(term->type);
543}
544
545static bool _abac_termtype_is_integer_type(int i)
546{
547    if (i == e_TERM_INTEGER) 
548       return 1;
549    return 0;
550}
551
552bool abac_term_is_integer(abac_term_t *term)
553{
554    return _abac_termtype_is_integer_type(term->type);
555}
556
557static bool _abac_termtype_is_urn_type(int i)
558{
559    if (i == e_TERM_URN) 
560       return 1;
561    return 0;
562}
563
564bool abac_term_is_urn(abac_term_t *term)
565{
566    return _abac_termtype_is_urn_type(term->type);
567}
568
569bool abac_term_is_alpha(abac_term_t *term)
570{
571    if(term->type == e_TERM_URN ||
572          term->type == e_TERM_STRING ||
573              term->type == e_TERM_BOOLEAN)
574        return 1;
575    return 0;
576}
577
578bool abac_term_is_numeric(abac_term_t *term)
579{
580    if(term->type == e_TERM_INTEGER ||
581          term->type == e_TERM_FLOAT)
582        return 1;
583    return 0;
584}
585
586bool abac_term_is_time(abac_term_t *term)
587{
588    if(term->type == e_TERM_TIME)
589        return 1;
590    return 0;
591}
592
593char *abac_term_to_time(char *string)
594{
595    struct tm tm1;
596    char buf[255];
597    char *init_string="19991212T000000";
598
599    strptime(init_string, "%Y%m%dT%H%M%S", &tm1);
600    strptime(string, "%Y%m%dT%H%M%S", &tm1);
601    strftime(buf, sizeof(buf), "time(%Y,%m,%d,%H,%M,%S)", &tm1);
602    return strdup(buf);
603}
604
605char *abac_time_to_term(char *string)
606{
607    char buf[255];
608    struct tm tm1;
609
610    char* rt=strptime(string, "time(%Y,%m,%d,%H,%M,%S)", &tm1);
611    if(rt==NULL)
612        return strdup(string);
613    strftime(buf, sizeof(buf), "%Y%m%dT%H%M%S", &tm1);
614    return strdup(buf);
615}
616
617/* called from yy, cptr is either abac_aspect_t or abac_list_t */
618abac_term_t *abac_term_new(int type, char *name, int isrange, char *cond, void *cptr)
619{
620     if(debug) {
621         printf("abac_term_new: \n");
622         printf("  type is %d(%s)\n", type, abac_termtype_string(type));
623         printf("  name is (%s)\n", name);
624         if(cond)
625             printf("  cond is %s\n", cond);
626             else printf("  no cond \n");
627         printf("  isrange is %d\n", isrange);
628         if(cptr) {
629             if(isrange) printf("  cptr is (%s)\n", 
630                                    abac_range_string((abac_list_t *)cptr));
631                else
632                   printf("  cptr is (%s)\n", 
633                          abac_aspect_string_with_condition((abac_aspect_t *)cptr));
634         }    else printf("  there is no cptr\n");
635     }
636     abac_condition_t *constraint=NULL;
637     if (cptr) {
638         if(isrange) {
639           constraint=_abac_condition_new(e_COND_RANGE,type,cond,cptr);
640           } else {
641             if(type==e_TERM_PRINCIPAL)
642               constraint=_abac_condition_new(e_COND_ROLE,type,cond,cptr);
643               else
644                 constraint=_abac_condition_new(e_COND_OSET,type,cond,cptr);
645         }
646     }
647     abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t));
648     ptr->isnamed=0;
649     ptr->type=type;
650     ptr->p_name=NULL;
651     /* reformat the term name if it is a time type */
652     if((!constraint) && _abac_termtype_is_time_type(type) && isdigit(name[0])) {
653         char *tmp=abac_term_to_time(name);
654         ptr->name=tmp;
655         } else {
656             ptr->name=abac_xstrdup(name);
657     }
658     ptr->constraint=constraint;
659     ptr->cn=NULL;
660     return ptr;
661}
662
663abac_term_t *abac_term_create(char *typename, char *name, abac_condition_t *cptr)
664{
665     int type=abac_verify_term_type(typename);
666     if(debug) {
667         printf("abac_term_create: \n");
668         printf("  type is %d(%s)\n", type, abac_termtype_string(type));
669         printf("  name is (%s)\n", name);
670         if(cptr)
671             printf("  cptr is (%s)\n", abac_condition_typed_string(cptr));
672             else printf("  there is no cptr\n");
673     }
674
675     abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t));
676     ptr->isnamed=0;
677     ptr->type=type;
678     ptr->p_name=NULL;
679     /* reformat the term name if it is a time type */
680     if((!cptr) && _abac_termtype_is_time_type(type) && isdigit(name[0])) {
681         char *tmp=abac_term_to_time(name);
682         ptr->name=tmp;
683         } else {
684             ptr->name=abac_xstrdup(name);
685     }
686     ptr->constraint=(cptr==NULL)?NULL:abac_condition_dup(cptr);
687     ptr->cn=NULL;
688     return ptr;
689}
690
691/* called from yy, named principal type */
692abac_term_t *abac_term_named_new(int idtype, char *name)
693{
694     if(debug) {
695         printf("abac_term_named_new: \n");
696         printf("  type is %d(%s)\n", idtype, abac_idtype_string(idtype));
697         printf("  name is (%s)\n", name);
698     }
699     abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t));
700     ptr->isnamed=1;
701     ptr->type=idtype;
702     ptr->name=abac_xstrdup(name);
703     ptr->p_name=NULL;
704     /* if ABAC_CN, no need to attach p */
705     if(!USE("ABAC_CN"))
706         asprintf(&ptr->p_name,"p%s",name);
707     ptr->constraint=NULL;
708     ptr->cn=NULL;
709     return ptr;
710}
711
712/* seems to be always idtype==e_TERM_PRINCIPAL */
713abac_term_t *abac_term_named_create(char *name)
714{
715     if(debug) {
716         printf("abac_term_named_create: \n");
717         printf("  name is (%s)\n", name);
718     }
719
720     abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t));
721     ptr->isnamed=1;
722     ptr->type=e_TERM_PRINCIPAL;
723     ptr->name=abac_xstrdup(name);
724     ptr->p_name=NULL;
725     /* if ABAC_CN, no need to attach p */
726     if(!USE("ABAC_CN"))
727         asprintf(&ptr->p_name,"p%s",name);
728     ptr->constraint=NULL;
729     ptr->cn=NULL;
730     return ptr;
731}
732
733/* seems to be always idtype==keyid type */
734abac_term_t *abac_term_id_create(abac_id_t *idptr)
735{
736     if(debug) {
737         printf("abac_term_id_create: \n");
738         printf("  name is (%s)\n", abac_id_name(idptr));
739     }
740
741     abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t));
742     ptr->isnamed=1;
743     ptr->type=abac_id_idtype(idptr);
744     ptr->name=abac_xstrdup(abac_id_name(idptr));
745     ptr->p_name=NULL;
746     /* if ABAC_CN, no need to attach p */
747     if(!USE("ABAC_CN"))
748         asprintf(&ptr->p_name,"p%s",ptr->name);
749     ptr->constraint=NULL;
750     ptr->cn=NULL;
751     return ptr;
752}
753
754
755abac_term_t *abac_term_dup(abac_term_t *ptr)
756{
757     assert(ptr);
758     abac_term_t *nptr=abac_xmalloc(sizeof(abac_term_t));
759     nptr->isnamed=ptr->isnamed;
760     nptr->type=ptr->type;
761     nptr->name=abac_xstrdup(ptr->name);
762     nptr->p_name=(ptr->p_name==NULL)?NULL:abac_xstrdup(ptr->p_name);
763     if(ptr->constraint!=NULL)
764        nptr->constraint=abac_condition_dup(ptr->constraint);
765        else nptr->constraint=NULL;
766     nptr->cn=(ptr->cn)==NULL?NULL:abac_xstrdup(ptr->cn);
767        nptr->cn=abac_xstrdup(ptr->cn);
768     return nptr;
769}
770
771void abac_term_free(abac_term_t *ptr)
772{
773    if(ptr->constraint) {
774         abac_condition_free(ptr->constraint);
775    }
776    free(ptr->name);
777    if(ptr->p_name) free(ptr->p_name);
778    if(ptr->cn) free(ptr->cn);
779    free(ptr);
780}
781
782abac_term_t *abac_term_add_constraint(abac_term_t *ptr, abac_condition_t *cond)
783{
784    /* make sure the types are matching */
785    if(abac_condition_vartype(cond) != abac_term_type(ptr)) {
786        panic("abac_term_add_constraint: mismatched constraint type with the dataterm\n");
787        return NULL;
788    }
789   
790    if(ptr->constraint) {
791        abac_condition_free(ptr->constraint);
792    }
793    ptr->constraint = abac_condition_dup(cond);
794    return ptr;
795}
796
797/* name or name:cond */
798static char *abac_term_string_with_condition(abac_term_t *ptr)
799{
800    char *tmp=NULL;
801    char *cond=NULL;
802    char *name=abac_term_name(ptr);
803    if(ptr->constraint) {
804       cond=abac_condition_string(ptr->constraint);
805    }
806    if(cond) {
807       asprintf(&tmp,"%s:%s", name, cond);
808       free(cond);
809    } else tmp=abac_xstrdup(name);
810    return tmp;
811}
812
813/* [type:name] or [typed:name:cond] */
814static char *abac_term_typed_string_with_condition(abac_term_t *ptr)
815{
816    char *tmp=NULL;
817    char *cond=NULL;
818    int free_name=0;
819    char *name=abac_term_name(ptr);
820    char *type=abac_term_type_name(ptr);
821    if(ptr->constraint) {
822       cond=abac_condition_typed_string(ptr->constraint);
823    }
824    /* special case if term is actually a time term... */
825    if(abac_term_is_time_type(ptr)) {
826       name=abac_time_to_term(name); /* this should be freed */
827       if(debug) printf("abac_term_typed_string_with_condition: trying to turn the time back\n");
828       free_name=1;
829    }
830    if(cond) {
831       if(abac_condition_is_range(ptr->constraint))
832           asprintf(&tmp,"[%s:?%s:%s]", type, name, cond);
833           else asprintf(&tmp,"[%s:?%s%s]", type, name, cond);
834       free(cond);
835       } else {
836           if(isupper(name[0])) 
837               asprintf(&tmp,"[%s:?%s]", type, name);
838           else asprintf(&tmp,"[%s:%s]", type, name);
839    }
840    if(debug) printf("abac_term_typed_string_with_condition: (%s)\n",tmp);
841    if(free_name) free(name);
842    return tmp;
843}
844
845char *abac_term_string(abac_term_t *ptr)
846{
847    return abac_xstrdup(abac_term_name(ptr));
848}
849
850char *abac_term_typed_string(abac_term_t *ptr)
851{
852    return abac_term_typed_string_with_condition(ptr);
853}
854
855/********************************************************************/
856abac_list_t *abac_param_list(abac_param_list_t *ptr)
857{
858    assert(ptr);
859    return ptr->list;
860}
861
862void abac_param_dump(abac_param_list_t *ptr, char *offset)
863{
864    printf("===params===\n");
865    abac_term_t  *cur;
866    abac_list_foreach(ptr->list, cur,
867           abac_term_dump(cur, offset);
868    );
869    printf("============\n");
870}
871
872abac_param_list_t *abac_param_list_new(abac_term_t *term)
873{
874    abac_param_list_t *ptr=abac_xmalloc(sizeof(abac_param_list_t));
875    ptr->list=abac_list_new();
876    abac_list_add(ptr->list, abac_term_dup(term));
877    return ptr;
878}
879
880abac_param_list_t *abac_param_list_free(abac_param_list_t *ptr)
881{
882    abac_list_t *list=ptr->list;
883    abac_term_t  *cur;
884    abac_list_foreach(list, cur,
885           abac_term_free(cur);
886    );
887    abac_list_free(list);
888    free(ptr);
889}
890
891abac_param_list_t *abac_param_list_add_term(abac_param_list_t *ptr, abac_term_t *term)
892{
893    abac_list_t *list=ptr->list;
894    abac_list_add(list, abac_term_dup(term));
895    return ptr;
896}
897
898
899/* term1:cond1,term2:cond2 */
900char* abac_param_list_string_with_condition(abac_param_list_t *ptr)
901{
902    if(ptr->list == NULL)
903        return "";
904
905    abac_term_t  *cur;
906    char *tmp=NULL, *final=NULL;
907    abac_list_foreach(ptr->list, cur,
908           char *s=abac_term_string_with_condition(cur);
909           if(final==NULL) {
910               final=abac_xstrdup(s);
911               } else {
912                  tmp=final;
913                  final=NULL;
914                  asprintf(&final,"%s,%s",tmp,s);
915                  free(tmp);
916           }
917           free(s);
918    );
919    return final;
920}
921
922/* [type:term1:cond1],[type:term2:cond2] */
923char* abac_param_list_typed_string_with_condition(abac_param_list_t *ptr)
924{
925    if(ptr->list == NULL)
926        return "";
927
928    abac_term_t  *cur;
929    char *tmp=NULL, *final=NULL;
930    abac_list_foreach(ptr->list, cur,
931           char *s=abac_term_typed_string_with_condition(cur);
932           if(final==NULL) {
933               final=abac_xstrdup(s);
934               } else {
935                  tmp=final;
936                  final=NULL;
937                  asprintf(&final,"%s,%s",tmp,s);
938                  if(debug) printf("typed param so far, %s\n", final);
939                  free(tmp);
940           }
941           free(s);
942    );
943    if(debug) printf("typed param so far, %s\n", final);
944    return final;
945}
946
947/* term1,term2 */
948char* abac_param_list_string(abac_param_list_t *ptr)
949{
950    abac_list_t *list=ptr->list;
951    if(list == NULL)
952        return "";
953    abac_term_t  *cur;
954    char *final=NULL;
955    char *tmp=NULL;
956
957    /* collect up all the string */
958    abac_list_foreach(list, cur,
959           char *s=abac_term_string(cur);
960           if(final==NULL) {
961               final=abac_xstrdup(s);
962               } else {
963                  tmp=final;
964                  final=NULL;
965                  asprintf(&final,"%s,%s",tmp,s);
966                  if(debug) printf("param so far, %s\n", final);
967                  free(tmp);
968           }
969           free(s);
970    );
971    if(debug) printf("param so far, %s\n", final);
972    return final;
973}
974
975/* make a stack of it for the abac.hh */
976abac_term_t **abac_param_list_vectorize(abac_param_list_t *ptr)
977{
978    abac_term_t **terms=NULL;
979    abac_list_t *list=ptr->list;
980    int cnt=0;
981    if(list != NULL)
982        cnt=abac_list_size(list);
983   
984    // make the array (leave space to NULL terminate it)
985    //      n.b., even if the list is empty, we still return an array that
986    //            only contains the NULL terminator
987    terms = abac_xmalloc(sizeof(abac_term_t *) * (cnt + 1));
988
989    abac_term_t  *cur;
990    int i = 0;
991    if(i<cnt) {
992        abac_list_foreach(list, cur,
993            terms[i++] = abac_term_dup(cur);
994        );
995    }
996    terms[i] = NULL;
997    return terms;
998}
999
1000
1001void abac_terms_free(abac_term_t **terms)
1002{
1003    /* always null terminating */
1004    assert(terms);
1005    int i; 
1006    for (i = 0; terms[i] != NULL; ++i) {
1007        abac_term_free(terms[i]);
1008    }
1009    free(terms);
1010}
1011
1012void abac_term_dump(abac_term_t *ptr, char *offset)
1013{
1014    printf("--- term---\n");
1015    printf("%s type : %s\n",offset, abac_term_type_name(ptr));
1016    printf("%s name : %s\n",offset, abac_term_name(ptr));
1017    printf("%s isnamed: %d\n",offset,ptr->isnamed);
1018    if(ptr->p_name)
1019        printf("%s p_name: %s\n",offset,ptr->p_name);
1020        else printf("%s p_name: --\n",offset);
1021    if(ptr->cn)
1022        printf("%s cn: %s\n",offset,ptr->cn);
1023        else printf("%s cn: --\n",offset);
1024    if(ptr->constraint) {
1025        char *tmp=NULL;
1026        asprintf(&tmp,"  %s", offset);
1027        abac_condition_dump(ptr->constraint,offset);
1028        free(tmp);
1029        } else printf("%s constraint: --\n",offset);
1030    printf("-----------\n");
1031}
1032
Note: See TracBrowser for help on using the repository browser.