Index: libabac/abac_pl_yy.c
===================================================================
--- libabac/abac_pl_yy.c	(revision da5afdf4d61c978dbd17b9d6f20e5a45dcab870a)
+++ libabac/abac_pl_yy.c	(revision b5a3da409a2c3f6cb8faf1966ee747bc954efd33)
@@ -11,23 +11,28 @@
 static int debug=0;
 
+extern void panic(char*);
+
+/* from abac_pl_gen.c */
+extern char *generate_pl_oset_constraint_clause(abac_oset_t *, char *);
 extern abac_list_t *generate_pl_role_clauses(abac_role_t *, abac_role_t *);
 extern abac_list_t *generate_pl_oset_clauses(abac_oset_t *, abac_oset_t *);
-extern void panic(char*);
 extern char *generate_pl_type_clause(char *, int);
-void _add_yy_id_certs(char *, int);
-static void _free_yy_oset(abac_yy_oset_t *ptr);
-static void _free_yy_term_data(abac_yy_term_data_t *ptr);
-abac_role_t *validate_head_role(abac_yy_expression_t *expr);
-
-abac_list_t *abac_yap_clauses=NULL;
-/* this is to track individual id credentials */
-abac_stack_t *abac_yap_id_clauses=NULL;
-
-int abac_yap_is_oset=0;
-abac_role_t *abac_yap_head_role=NULL;
-abac_role_t *abac_yap_tail_role=NULL;
-
-abac_oset_t *abac_yap_head_oset=NULL;
-abac_oset_t *abac_yap_tail_oset=NULL;
+/* from abac_oset.c */
+extern abac_oset_t *abac_oset_principal_new(char *);
+/* from rt2.y */
+extern void abac_yyinit();
+/* from abac_param.c */
+extern char *abac_term_type(int);
+
+
+int abac_yy_error_code = 0; /* keeping last error code */
+
+int abac_rule_is_oset=0;
+abac_list_t *abac_rule_clauses=NULL;
+abac_stack_t *abac_rule_id_clauses=NULL; /* tracking individual id credentials */
+abac_role_t *abac_rule_head_role=NULL;
+abac_role_t *abac_rule_tail_role=NULL;
+abac_oset_t *abac_rule_head_oset=NULL;
+abac_oset_t *abac_rule_tail_oset=NULL;
 
 /* to track id certs within a rule clause */
@@ -70,10 +75,11 @@
     int type; /* this needs to be implicitly determined */
     char *name;
-    char *condition;
-    void *cptr; // ptr to a saved abac_role_t
+    char *cond_str;
+    void *cond_ptr; // ptr to a saved abac_role_t
     abac_yy_expression_t *cond_head_expr;
 };
 
-/* if has condition, then it is a variable,
+/* integer,float,time,urn,string types,
+   if has condition, then it is a variable,
    ie,
     [int:?Year]:<1930..1932>,
@@ -86,6 +92,6 @@
     int type;
     char *name;
-    char *condition;
-    void *cptr; // ptr to a saved abac_oset_t
+    char *cond_str;
+    void *cond_ptr; // ptr to a saved abac_oset_t
     abac_list_t *cond_range;
     abac_yy_expression_t *cond_head_expr; // parking stub
@@ -144,8 +150,150 @@
    int bcnt;
 };
-static void _free_yy_term(abac_yy_term_t *ptr);
+
+static void _free_yy_expression(abac_yy_expression_t *);
 
 /************************************************************************/
-static void _free_yy_range(abac_list_t *ptr) 
+void abac_yy_set_error_code(int v)
+{
+     abac_yy_error_code=v;
+}
+
+/************************************************************************/
+abac_stack_t *abac_yy_get_rule_id_clauses()
+{
+    return abac_rule_id_clauses;
+}
+
+abac_stack_t *abac_yy_init_rule_id_clauses()
+{
+    abac_rule_id_clauses=abac_stack_new();
+    return abac_rule_id_clauses;
+}
+
+void abac_yy_free_rule_id_clauses()
+{
+    if (abac_rule_id_clauses != NULL)
+        abac_stack_free(abac_rule_id_clauses);
+}
+
+void abac_yy_set_rule_clauses(abac_list_t *clauses)
+{
+    abac_rule_clauses=clauses;
+    if(debug) {
+         printf("printing out abac_rule_clauses:\n");
+         abac_print_clauses(abac_rule_clauses,NULL);
+    }
+}
+
+abac_list_t *abac_yy_get_rule_clauses()
+{
+    return abac_rule_clauses;
+}
+
+void abac_yy_free_rule_clauses()
+{
+    if (abac_rule_clauses != NULL) {
+        char *cur;
+        abac_list_foreach(abac_yy_id_certs, cur,
+            if(cur) free(cur);
+        );
+        abac_list_free(abac_rule_clauses);
+    }
+}
+
+int abac_yy_get_rule_is_oset()
+{
+   return abac_rule_is_oset;
+}
+
+abac_role_t *abac_yy_get_rule_head_role()
+{
+    return abac_rule_head_role;
+}
+
+abac_role_t *abac_yy_get_rule_tail_role()
+{
+    return abac_rule_tail_role;
+}
+
+abac_oset_t *abac_yy_get_rule_head_oset()
+{
+    return abac_rule_head_oset;
+}
+
+abac_oset_t *abac_yy_get_rule_tail_oset()
+{
+    return abac_rule_tail_oset;
+}
+
+
+/************************************************************************/
+void abac_yy_init_yy_id_certs()
+{
+    abac_yy_id_certs = abac_list_new();
+}
+
+void abac_yy_free_yy_id_certs()
+{
+    abac_yy_id_cert_t *id;
+    abac_list_foreach(abac_yy_id_certs, id,
+        if(id) 
+            free(id->principalname);
+            free(id->clause);
+            free(id);
+    );
+    abac_list_free(abac_yy_id_certs);
+    abac_yy_id_certs = NULL;
+}
+
+int abac_yy_cnt_yy_id_certs()
+{
+    return abac_list_size(abac_yy_id_certs);
+}
+
+char *abac_yy_string_yy_id_certs()
+{
+    int first=1;
+    char *tmp=NULL;
+    abac_yy_id_cert_t *cur;
+    abac_list_foreach(abac_yy_id_certs, cur,
+        if(cur) 
+            if(first) {
+                tmp=abac_xstrdup(cur->clause);
+                first=0;
+                } else {
+                    int cnt=asprintf(&tmp,"%s,%s", tmp, cur->clause);
+            }
+    );
+    return tmp;
+}
+
+static void _add_yy_id_certs(char *principalname, int type)
+{
+    abac_yy_id_cert_t *id_cert=NULL;
+
+    int found=0;
+    abac_yy_id_cert_t *cur;
+    abac_list_foreach(abac_yy_id_certs, cur,
+        if(cur) 
+            if(strcmp(cur->principalname,principalname)==0) {
+               found=1;
+               break;
+            }
+    );
+
+    if (found) {
+        return;
+        } else {
+            id_cert=abac_xmalloc(sizeof(abac_yy_id_cert_t));
+            id_cert->principalname=abac_xstrdup(principalname);
+            id_cert->type=type;
+            id_cert->clause=generate_pl_type_clause(principalname,type);
+            abac_list_add(abac_yy_id_certs, id_cert);
+    }
+}
+
+/***********************************************************************/
+static void _free_yy_cond_range(abac_list_t *ptr) 
 {
     if (ptr != NULL) {
@@ -159,5 +307,5 @@
 }
 
-char *string_yy_cond_range(abac_list_t *ptr)
+static char *_string_yy_cond_range(abac_list_t *ptr)
 {
     assert(ptr);
@@ -210,5 +358,5 @@
 }
 
-abac_list_t *addto_yy_val_range(abac_list_t *ptr, char * val)
+abac_list_t *add_yy_val_range(abac_list_t *ptr, char * val)
 {
     abac_yy_range_t *range= 
@@ -220,5 +368,5 @@
 }
 
-static abac_list_t *_addto_yy_range(abac_list_t *ptr, int type, char * val)
+static abac_list_t *_add_yy_range(abac_list_t *ptr, int type, char * val)
 {
     abac_yy_range_t *range= 
@@ -233,5 +381,5 @@
 {
     abac_list_t *ptr=abac_list_new();
-    addto_yy_val_range(ptr,val);
+    add_yy_val_range(ptr,val);
     return ptr;
 }
@@ -240,6 +388,6 @@
 {
     abac_list_t *ptr=abac_list_new();
-    _addto_yy_range(ptr, e_yy_RANGE_MIN, min);
-    _addto_yy_range(ptr, e_yy_RANGE_MAX, max);
+    _add_yy_range(ptr, e_yy_RANGE_MIN, min);
+    _add_yy_range(ptr, e_yy_RANGE_MAX, max);
     return ptr;
 }
@@ -248,5 +396,5 @@
 {
     abac_list_t *ptr=abac_list_new();
-    _addto_yy_range(ptr, e_yy_RANGE_MIN, min);
+    _add_yy_range(ptr, e_yy_RANGE_MIN, min);
     return ptr;
 }
@@ -255,149 +403,185 @@
 {
     abac_list_t *ptr=abac_list_new();
-    _addto_yy_range(ptr, e_yy_RANGE_MAX, max);
+    _add_yy_range(ptr, e_yy_RANGE_MAX, max);
+    return ptr;
+}
+
+/***********************************************************************/
+abac_yy_term_data_t *make_yy_term_data(char* name)
+{
+    abac_yy_term_data_t *ptr=
+              (abac_yy_term_data_t*)abac_xmalloc(sizeof(abac_yy_term_data_t));
+    ptr->name=abac_xstrdup(name);
+    ptr->type=0;
+    ptr->is_variable=0;
+    ptr->cond_str=NULL;
+    ptr->cond_head_expr=NULL;
+    ptr->cond_ptr=NULL;
+    ptr->cond_range=NULL;
+    return ptr;
+}
+
+abac_list_t *get_yy_term_data_cond_range(abac_yy_term_data_t *ptr)
+{
+    return ptr->cond_range;
+}
+
+char *get_yy_term_data_name(abac_yy_term_data_t *ptr)
+{
+    return ptr->name;
+}
+
+int is_yy_term_data_has_constraint(abac_yy_term_data_t *ptr)
+{
+    if(ptr->cond_head_expr != NULL)
+        return 1;
+/* only select few */
+    if( ptr->cond_range != NULL && abac_term_is_integer_type(ptr->type))
+        return 1;
+    return 0;
+}
+
+void set_yy_term_data_cond_range(abac_yy_term_data_t *ptr,
+abac_list_t *range)
+{
+    ptr->cond_range=range;
+}
+
+void set_yy_term_data_cond_head_expr(abac_yy_term_data_t *ptr,
+abac_yy_expression_t *expr)
+{
+    ptr->cond_head_expr=expr;
+}
+
+abac_yy_expression_t *get_yy_term_data_cond_head_expr(abac_yy_term_data_t *ptr)
+{
+    return ptr->cond_head_expr;
+}
+
+void set_yy_term_data_type(abac_yy_term_data_t *ptr, char* typestr)
+{
+    int type=abac_term_verify_term_type(typestr);
+    ptr->type=type;
+}
+
+void set_yy_term_data_cond_ptr(abac_yy_term_data_t *ptr, void *vptr)
+{
+    ptr->cond_ptr=vptr;
+}
+
+void set_yy_term_data_cond_str(abac_yy_term_data_t *ptr, char *cond)
+{
+    ptr->cond_str=strdup(cond);
+}
+
+void set_yy_term_data_is_variable(abac_yy_term_data_t *ptr)
+{
+    ptr->is_variable=1;
+}
+
+int get_yy_term_data_is_variable(abac_yy_term_data_t *ptr)
+{
+    return ptr->is_variable;
+}
+
+static void _free_yy_term_data(abac_yy_term_data_t *ptr)
+{
+    free(ptr->name);
+    if(ptr->cond_str)
+        free(ptr->cond_str);
+    if(ptr->cond_head_expr)
+        _free_yy_expression(ptr->cond_head_expr);
+    if(ptr->cond_range);
+        _free_yy_cond_range(ptr->cond_range);
+    free(ptr);
+}
+
+abac_yy_term_principal_t *make_yy_term_principal(char* name)
+{
+    abac_yy_term_principal_t *ptr=
+            (abac_yy_term_principal_t*)abac_xmalloc(sizeof(abac_yy_term_principal_t));
+    ptr->name=abac_xstrdup(name);
+    ptr->cond_str=NULL;
+    ptr->cond_head_expr=NULL;
+    return ptr;
+}
+
+void set_yy_term_principal_cond_ptr(abac_yy_term_principal_t *ptr, void *vptr)
+{
+    ptr->cond_ptr=vptr;
+}
+
+void set_yy_term_principal_cond_str(abac_yy_term_principal_t *ptr, char *cond)
+{
+    ptr->cond_str=abac_xstrdup(cond);
+}
+
+abac_yy_expression_t *get_yy_term_principal_cond_head_expr(abac_yy_term_principal_t *ptr)
+{
+    return ptr->cond_head_expr;
+}
+
+void set_yy_term_principal_cond_head_expr(abac_yy_term_principal_t *ptr, 
+abac_yy_expression_t *expr)
+{
+    ptr->cond_head_expr=expr;
+}
+
+static void _free_yy_term_principal(abac_yy_term_principal_t *ptr)
+{
+    free(ptr->name);
+    if(ptr->cond_str)
+        free(ptr->cond_str);
+    if(ptr->cond_head_expr)
+        _free_yy_expression(ptr->cond_head_expr);
+    free(ptr);
+}
+
+abac_yy_term_t *make_yy_term_dterm_anonymous()
+{
+    abac_yy_term_t *ptr=(abac_yy_term_t*)abac_xmalloc(sizeof(abac_yy_term_t));
+    ptr->type = e_yy_DTERM_ANONYMOUS;
+    ptr->next=NULL;
+    ptr->bcnt=1;
+    return ptr;
+}
+
+abac_yy_term_t *make_yy_term_dterm_principal(abac_yy_term_principal_t *pptr)
+{
+    abac_yy_term_t *ptr=(abac_yy_term_t*)abac_xmalloc(sizeof(abac_yy_term_t));
+    ptr->type = e_yy_DTERM_PRINCIPAL;
+    ptr->term.p_ptr=pptr;
+    ptr->next=NULL;
+    ptr->bcnt=1;
+    return ptr;
+}
+
+abac_yy_term_t *make_yy_term_dterm_named(abac_yy_principal_t *nptr)
+{
+    abac_yy_term_t *ptr=(abac_yy_term_t*)abac_xmalloc(sizeof(abac_yy_term_t));
+    ptr->type = e_yy_DTERM_NAMED;
+    ptr->term.n_ptr=nptr;
+    ptr->next=NULL;
+    ptr->bcnt=1;
+    return ptr;
+}
+
+abac_yy_term_t *make_yy_term_dterm_data(abac_yy_term_data_t *dptr)
+{
+    abac_yy_term_t *ptr=(abac_yy_term_t*)abac_xmalloc(sizeof(abac_yy_term_t));
+    ptr->type = e_yy_DTERM_DATA;
+    ptr->term.d_ptr=dptr;
+    ptr->next=NULL;
+    ptr->bcnt=1;
     return ptr;
 }
 
 /************************************************************************/
-abac_stack_t *abac_get_yap_id_clauses()
-{
-    return abac_yap_id_clauses;
-}
-
-abac_stack_t *abac_init_yap_id_clauses()
-{
-    abac_yap_id_clauses=abac_stack_new();
-    return abac_yap_id_clauses;
-}
-
-void abac_free_yap_id_clauses()
-{
-    if (abac_yap_id_clauses != NULL)
-        abac_stack_free(abac_yap_id_clauses);
-}
-
-void set_yap_clauses(abac_list_t *clauses)
-{
-    abac_yap_clauses=clauses;
-    if(debug) {
-         printf("printing out abac_yap_clauses:\n");
-         abac_print_clauses(abac_yap_clauses,NULL);
-    }
-}
-
-abac_list_t *abac_get_yap_clauses()
-{
-    return abac_yap_clauses;
-}
-
-void abac_free_yap_clauses()
-{
-    if (abac_yap_clauses != NULL) {
-        char *cur;
-        abac_list_foreach(abac_yy_id_certs, cur,
-            if(cur) free(cur);
-        );
-        abac_list_free(abac_yap_clauses);
-    }
-}
-
-int abac_get_yap_is_oset()
-{
-   return abac_yap_is_oset;
-}
-
-abac_role_t *abac_get_yap_head_role()
-{
-    return abac_yap_head_role;
-}
-
-abac_role_t *abac_get_yap_tail_role()
-{
-    return abac_yap_tail_role;
-}
-
-abac_oset_t *abac_get_yap_head_oset()
-{
-    return abac_yap_head_oset;
-}
-
-abac_oset_t *abac_get_yap_tail_oset()
-{
-    return abac_yap_tail_oset;
-}
-
-/*******************************************************************/
-void abac_init_yy_id_certs()
-{
-    abac_yy_id_certs = abac_list_new();
-}
-
-void abac_free_yy_id_certs()
-{
-    abac_yy_id_cert_t *id;
-    abac_list_foreach(abac_yy_id_certs, id,
-        if(id) 
-            free(id->principalname);
-            free(id->clause);
-            free(id);
-    );
-    abac_list_free(abac_yy_id_certs);
-    abac_yy_id_certs = NULL;
-}
-
-int abac_cnt_yy_id_certs()
-{
-    return abac_list_size(abac_yy_id_certs);
-}
-
-char *abac_string_yy_id_certs()
-{
-    int first=1;
-    char *tmp=NULL;
-    abac_yy_id_cert_t *cur;
-    abac_list_foreach(abac_yy_id_certs, cur,
-        if(cur) 
-            if(first) {
-                tmp=abac_xstrdup(cur->clause);
-                first=0;
-                } else {
-                    int cnt=asprintf(&tmp,"%s,%s", tmp, cur->clause);
-            }
-    );
-    return tmp;
-}
-
-void _add_yy_id_certs(char *principalname, int type)
-{
-    abac_yy_id_cert_t *id_cert=NULL;
-
-    int found=0;
-    abac_yy_id_cert_t *cur;
-    abac_list_foreach(abac_yy_id_certs, cur,
-        if(cur) 
-            if(strcmp(cur->principalname,principalname)==0) {
-               found=1;
-               break;
-            }
-    );
-
-    if (found) {
-        return;
-        } else {
-            id_cert=abac_xmalloc(sizeof(abac_yy_id_cert_t));
-            id_cert->principalname=abac_xstrdup(principalname);
-            id_cert->type=type;
-            id_cert->clause=generate_pl_type_clause(principalname,type);
-            abac_list_add(abac_yy_id_certs, id_cert);
-    }
-}
-
-/****************************************************************************/
-void abac_init_yy_constraints()
+void abac_yy_init_yy_constraints()
 {
     abac_yy_constraints = abac_list_new();
 }
 
-void abac_free_yy_constraints()
+void abac_yy_free_yy_constraints()
 {
     abac_yy_constraint_t *cur;
@@ -411,10 +595,10 @@
 }
 
-int abac_cnt_yy_constraints()
+int abac_yy_cnt_yy_constraints()
 {
     return abac_list_size(abac_yy_constraints);
 }
 
-char *abac_string_yy_constraints()
+char *abac_yy_string_yy_constraints()
 {
     int first=1;
@@ -433,5 +617,5 @@
 }
 
-abac_yy_constraint_t *abac_add_yy_constraints(char *constraint)
+abac_yy_constraint_t *abac_yy_add_yy_constraints(char *constraint)
 {
     abac_yy_constraint_t *ptr=
@@ -472,4 +656,36 @@
     return ptr->sha;
 }
+
+/*************************************************************************/
+
+abac_yy_term_t *add_yy_term(abac_yy_term_t *nterm, abac_yy_term_t *terms)
+{
+    int i=terms->bcnt;
+    nterm->next=terms;
+    nterm->bcnt=i+1;
+    return nterm;
+}
+
+static void _free_yy_term(abac_yy_term_t *ptr)
+{
+    switch (ptr->type) {
+        case e_yy_DTERM_DATA:
+            _free_yy_term_data(ptr->term.d_ptr);
+            break;
+        case e_yy_DTERM_NAMED:
+            _free_yy_principal(ptr->term.n_ptr);
+            break;
+        case e_yy_DTERM_PRINCIPAL:
+            _free_yy_term_principal(ptr->term.p_ptr);
+            break;
+        case e_yy_DTERM_ANONYMOUS:
+            break;
+    }
+    if(ptr->next)
+        _free_yy_term(ptr->next);
+    free(ptr);
+}
+
+/*************************************************************************/
 
 abac_yy_role_t *make_yy_role(char *name, abac_yy_term_t *terms)
@@ -492,4 +708,26 @@
     free(ptr);
 }
+
+/***************************************************************************/
+abac_yy_oset_t *make_yy_oset(char *name, abac_yy_term_t *terms)
+{
+    abac_yy_oset_t *ptr=(abac_yy_oset_t*)abac_xmalloc(sizeof(abac_yy_oset_t));
+    ptr->name=abac_xstrdup(name);
+    ptr->terms=terms;
+    ptr->next=NULL;
+    ptr->bcnt=1;
+    return ptr;
+}
+
+static void _free_yy_oset(abac_yy_oset_t *ptr)
+{
+    free(ptr->name);
+    if(ptr->terms)
+        _free_yy_term(ptr->terms);
+    if(ptr->next)
+        _free_yy_oset(ptr->next);
+    free(ptr);
+}
+/***************************************************************************/
 
 /* type: e_yy_EXPR_NAMED, e_yy_EXPR_OBJECT, e_yy_EXPR_OSET,e_yy_EXPR_LINKED */
@@ -555,25 +793,5 @@
 }
 
-abac_yy_oset_t *make_yy_oset(char *name, abac_yy_term_t *terms)
-{
-    abac_yy_oset_t *ptr=(abac_yy_oset_t*)abac_xmalloc(sizeof(abac_yy_oset_t));
-    ptr->name=abac_xstrdup(name);
-    ptr->terms=terms;
-    ptr->next=NULL;
-    ptr->bcnt=1;
-    return ptr;
-}
-
-static void _free_yy_oset(abac_yy_oset_t *ptr)
-{
-    free(ptr->name);
-    if(ptr->terms)
-        _free_yy_term(ptr->terms);
-    if(ptr->next)
-        _free_yy_oset(ptr->next);
-    free(ptr);
-}
-
-/**********************************************************************/
+/***************************************************************************/
 static void _oset_add_terms(int linked, abac_oset_t *oset, 
 abac_yy_term_t *terms)
@@ -591,6 +809,6 @@
                type=ptr->type;
                name=abac_xstrdup(ptr->name);
-               cond=abac_xstrdup(ptr->condition);
-               ptr=ptr->cptr;
+               cond=abac_xstrdup(ptr->cond_str);
+               ptr=ptr->cond_ptr;
                break;
            }
@@ -598,8 +816,8 @@
            {
                abac_yy_term_principal_t *ptr=curr->term.p_ptr;
-               type=abac_verify_term_type("principal");
+               type=abac_term_verify_term_type("principal");
                name=abac_xstrdup(ptr->name);
-               cond=abac_xstrdup(ptr->condition);
-               ptr=ptr->cptr;
+               cond=abac_xstrdup(ptr->cond_str);
+               ptr=ptr->cond_ptr;
                break;
            }
@@ -667,9 +885,9 @@
      abac_oset_t *ret_oset=abac_oset_oset_new(principalname, osetname);
      if (ret_oset==NULL) {
-         set_error_code(ABAC_RT_ROLE_INVALID);
+         abac_yy_set_error_code(ABAC_RT_ROLE_INVALID);
          goto error;
      }
      if (!abac_oset_is_oset(ret_oset)) {
-         set_error_code(ABAC_RT_CERT_INVALID);
+         abac_yy_set_error_code(ABAC_RT_CERT_INVALID);
          goto error;
      }
@@ -715,6 +933,6 @@
      char *name=object->name;
      int type=object->type;
-     char *cond=object->condition;
-     void *ptr=object->cptr;
+     char *cond=object->cond_str;
+     void *ptr=object->cond_ptr;
      abac_term_t *term=abac_term_new(type,name,cond,ptr);
      abac_oset_t *ret_oset=abac_oset_object_new(term);
@@ -861,7 +1079,7 @@
         _free_yy_expression(headexpr);
         _free_yy_expression(tailexpr);
-        abac_yap_head_oset=head_oset;
-        abac_yap_tail_oset=tail_oset;
-        abac_yap_is_oset=1;
+        abac_rule_head_oset=head_oset;
+        abac_rule_tail_oset=tail_oset;
+        abac_rule_is_oset=1;
         return tmp;
 
@@ -876,215 +1094,201 @@
 }
 
+/****************************************************************/
+static void _role_add_terms(int linked, abac_role_t *role, 
+abac_yy_term_t *terms)
+{
+    abac_yy_term_t *curr = terms;
+    int type;
+    char *name=NULL;
+    char *cond=NULL;
+    void *ptr=NULL;
+    while (curr) {
+        switch (curr->type) {
+           case e_yy_DTERM_DATA:
+           {
+               abac_yy_term_data_t *ptr=curr->term.d_ptr;
+               type=ptr->type;
+               name=abac_xstrdup(ptr->name);
+               cond=abac_xstrdup(ptr->cond_str);
+               ptr=ptr->cond_ptr;
+               break;
+           }
+           case e_yy_DTERM_PRINCIPAL:
+           {
+              abac_yy_term_principal_t *ptr=curr->term.p_ptr;
+              type=abac_term_verify_term_type("principal");
+              name=abac_xstrdup(ptr->name);
+              cond=abac_xstrdup(ptr->cond_str);
+              ptr=ptr->cond_ptr;
+              break;
+           }
+           case e_yy_DTERM_NAMED:
+           {
+              abac_yy_principal_t *ptr=curr->term.n_ptr;
+              type=ptr->type;
+/* XXX should be switching to sha in the future */
+              name=abac_xstrdup(ptr->cn);
+              cond=NULL;
+              ptr=NULL;
+              break;
+           }
+           case e_yy_DTERM_ANONYMOUS:
+           {
+              type=0;
+              name=abac_xstrdup("_");
+              cond=NULL;
+              ptr=NULL;
+              break;
+           }
+       }
+       abac_term_t *param=abac_term_new(type,name,cond,ptr);
+       if (linked) 
+           abac_role_role_add_linked_param(role, param);
+           else
+                abac_role_role_add_param(role, param);
+       curr=curr->next;
+   }
+}
+
+static void _role_add_linked_terms(abac_role_t *role, abac_yy_term_t *terms)
+{
+    int linked=1;
+    _role_add_terms(linked, role, terms);
+}
+
+static void _role_add_role_terms(abac_role_t *role, abac_yy_term_t *terms)
+{
+    int linked=0;
+    _role_add_terms(linked, role, terms);
+}
+
+
+abac_role_t *validate_intersected_tail_role(abac_role_t *role)
+{
+    abac_role_t *ret_role=NULL;
+    abac_list_t *prereqs = abac_list_new();
+    abac_list_add(prereqs, role);
+    ret_role = abac_role_intersection_new("intersectingROLE", prereqs);
+    return ret_role;
+}
+
+abac_role_t *validate_head_role(abac_yy_expression_t *expr)
+{
+     abac_yy_principal_t *principal=expr->principal;
+     abac_yy_role_t *role=expr->role;
+
+     char *principalname=get_yy_principal_principalname(principal);
+     char *rolename=role->name;
+     abac_yy_term_t *terms=role->terms;
+     abac_role_t *ret_role=NULL;
+
+     ret_role=abac_role_role_new(principalname, rolename);
+     if (ret_role==NULL) {
+         abac_yy_set_error_code(ABAC_RT_ROLE_INVALID);
+         goto error;
+     }
+     if (!abac_role_is_role(ret_role)) {
+         abac_yy_set_error_code(ABAC_RT_CERT_INVALID);
+         goto error;
+     }
+     // insert the params for the role
+     if(terms) {
+          _role_add_role_terms(ret_role, terms);
+     }
+
+     _add_yy_id_certs(principal->cn,principal->type);
+
+     return ret_role;
+
+error:
+     return NULL;
+}
+
+abac_role_t *validate_named_tail_role(abac_yy_expression_t *expr)
+{
+     abac_yy_principal_t *principal=expr->principal;
+     char *principalname=get_yy_principal_principalname(principal);
+     abac_role_t *ret_role=NULL;
+
+     ret_role = abac_role_principal_new(principalname);
+     _add_yy_id_certs(principal->cn,principal->type);
+
+     if (ret_role==NULL)
+         goto error;
+     return ret_role;
+
+error:
+     panic("can not generate a simple named tail role");
+     return NULL;
+}
+
+abac_role_t *validate_role_tail_role(abac_yy_expression_t *expr)
+{
+     abac_yy_principal_t *principal=expr->principal;
+     abac_yy_role_t *role=expr->role;
+
+     char *principalname=get_yy_principal_principalname(principal);
+
+     char *rolename=role->name;
+     abac_yy_term_t *terms=role->terms;
+
+     int ret;
+     abac_role_t *ret_role=NULL;
+
+     ret_role = abac_role_role_new(principalname, rolename);
+     if (ret_role==NULL)
+         goto error;
+
+     if(terms) {
+         _role_add_role_terms(ret_role, terms);
+     }
+
+     _add_yy_id_certs(principal->cn,principal->type);
+
+     return ret_role;
+
+error:
+     panic("can not generate a simple tail role");
+     return NULL;
+}
+/* need to validate/generate a linked role out of this */
+abac_role_t *validate_linked_tail_role(abac_yy_expression_t *expr)
+{
+     abac_yy_principal_t *principal=expr->principal;
+     abac_yy_role_t *role=expr->role;
+     abac_yy_role_t *linked_role=expr->linked_role;
+
+     char *principalname=get_yy_principal_principalname(principal);
+
+     char *rolename=role->name;
+     abac_yy_term_t *terms=role->terms;
+
+     char *linkedrolename=linked_role->name;
+     abac_yy_term_t *linked_terms=linked_role->terms;
+
+     int ret;
+     abac_role_t *ret_role=NULL;
+
+     ret_role = abac_role_linking_new(principalname, linkedrolename, rolename);
+     if (ret_role==NULL)
+         goto error;
+
+     if(linked_terms) {
+         _role_add_linked_terms(ret_role, linked_terms);
+     }
+     if(terms) {
+         _role_add_role_terms(ret_role, terms);
+     }
+
+     _add_yy_id_certs(principal->cn,principal->type);
+
+     return ret_role;
+
+error:
+     panic("can not generate linked tail role");
+     return NULL;
+}
+
 /***********************************************************************/
-/*
-struct _abac_yy_term_data_t {
-    int is_variable;
-    int type;
-    char *name;
-    char *condition;
-    void *cptr; // ptr to a saved abac_oset_t
-    abac_list_t *cond_range;
-    abac_yy_expression_t *cond_head_expr;
-};
-*/
-
-abac_yy_term_data_t *make_yy_term_data(char* name)
-{
-    abac_yy_term_data_t *ptr=
-              (abac_yy_term_data_t*)abac_xmalloc(sizeof(abac_yy_term_data_t));
-    ptr->name=abac_xstrdup(name);
-    ptr->type=0;
-    ptr->is_variable=0;
-    ptr->condition=NULL;
-    ptr->cond_head_expr=NULL;
-    ptr->cptr=NULL;
-    ptr->cond_range=NULL;
-    return ptr;
-}
-
-abac_list_t *get_yy_term_data_cond_range(abac_yy_term_data_t *ptr)
-{
-    return ptr->cond_range;
-}
-
-char *get_yy_term_data_name(abac_yy_term_data_t *ptr)
-{
-    return ptr->name;
-}
-
-int is_yy_term_data_has_constraint(abac_yy_term_data_t *ptr)
-{
-    if(ptr->cond_head_expr != NULL)
-        return 1;
-/* only select few */
-    if( ptr->cond_range != NULL && abac_term_is_integer_type(ptr->type))
-        return 1;
-    return 0;
-}
-
-void set_yy_term_data_cond_range(abac_yy_term_data_t *ptr,
-abac_list_t *range)
-{
-    ptr->cond_range=range;
-}
-
-void set_yy_term_data_cond_head_expr(abac_yy_term_data_t *ptr,
-abac_yy_expression_t *expr)
-{
-    ptr->cond_head_expr=expr;
-}
-
-abac_yy_expression_t *get_yy_term_data_cond_head_expr(abac_yy_term_data_t *ptr)
-{
-    return ptr->cond_head_expr;
-}
-
-void set_yy_term_data_type(abac_yy_term_data_t *ptr, char* typestr)
-{
-    int type=abac_verify_term_type(typestr);
-    ptr->type=type;
-}
-
-void set_yy_term_data_cptr(abac_yy_term_data_t *ptr, void *vptr)
-{
-    ptr->cptr=vptr;
-}
-
-void set_yy_term_data_condition(abac_yy_term_data_t *ptr, char *cond)
-{
-    ptr->condition=strdup(cond);
-}
-
-void set_yy_term_data_is_variable(abac_yy_term_data_t *ptr)
-{
-    ptr->is_variable=1;
-}
-
-int get_yy_term_data_is_variable(abac_yy_term_data_t *ptr)
-{
-    return ptr->is_variable;
-}
-
-static void _free_yy_term_data(abac_yy_term_data_t *ptr)
-{
-    free(ptr->name);
-    if(ptr->condition)
-        free(ptr->condition);
-    if(ptr->cond_head_expr)
-        _free_yy_expression(ptr->cond_head_expr);
-    if(ptr->cond_range);
-        _free_yy_range(ptr->cond_range);
-    free(ptr);
-}
-
-abac_yy_term_principal_t *make_yy_term_principal(char* name)
-{
-    abac_yy_term_principal_t *ptr=
-            (abac_yy_term_principal_t*)abac_xmalloc(sizeof(abac_yy_term_principal_t));
-    ptr->name=abac_xstrdup(name);
-    ptr->condition=NULL;
-    ptr->cond_head_expr=NULL;
-    return ptr;
-}
-
-void set_yy_term_principal_cptr(abac_yy_term_principal_t *ptr, void *vptr)
-{
-    ptr->cptr=vptr;
-}
-
-void set_yy_term_principal_condition(abac_yy_term_principal_t *ptr, char *cond)
-{
-    ptr->condition=abac_xstrdup(cond);
-}
-
-abac_yy_expression_t *get_yy_term_principal_cond_head_expr(abac_yy_term_principal_t *ptr)
-{
-    return ptr->cond_head_expr;
-}
-
-void set_yy_term_principal_cond_head_expr(abac_yy_term_principal_t *ptr, 
-abac_yy_expression_t *expr)
-{
-    ptr->cond_head_expr=expr;
-}
-
-static void _free_yy_term_principal(abac_yy_term_principal_t *ptr)
-{
-    free(ptr->name);
-    if(ptr->condition)
-        free(ptr->condition);
-    if(ptr->cond_head_expr)
-        _free_yy_expression(ptr->cond_head_expr);
-    free(ptr);
-}
-
-abac_yy_term_t *make_yy_term_dterm_anonymous()
-{
-    abac_yy_term_t *ptr=(abac_yy_term_t*)abac_xmalloc(sizeof(abac_yy_term_t));
-    ptr->type = e_yy_DTERM_ANONYMOUS;
-    ptr->next=NULL;
-    ptr->bcnt=1;
-    return ptr;
-}
-
-abac_yy_term_t *make_yy_term_dterm_principal(abac_yy_term_principal_t *pptr)
-{
-    abac_yy_term_t *ptr=(abac_yy_term_t*)abac_xmalloc(sizeof(abac_yy_term_t));
-    ptr->type = e_yy_DTERM_PRINCIPAL;
-    ptr->term.p_ptr=pptr;
-    ptr->next=NULL;
-    ptr->bcnt=1;
-    return ptr;
-}
-
-abac_yy_term_t *make_yy_term_dterm_named(abac_yy_principal_t *nptr)
-{
-    abac_yy_term_t *ptr=(abac_yy_term_t*)abac_xmalloc(sizeof(abac_yy_term_t));
-    ptr->type = e_yy_DTERM_NAMED;
-    ptr->term.n_ptr=nptr;
-    ptr->next=NULL;
-    ptr->bcnt=1;
-    return ptr;
-}
-
-abac_yy_term_t *make_yy_term_dterm_data(abac_yy_term_data_t *dptr)
-{
-    abac_yy_term_t *ptr=(abac_yy_term_t*)abac_xmalloc(sizeof(abac_yy_term_t));
-    ptr->type = e_yy_DTERM_DATA;
-    ptr->term.d_ptr=dptr;
-    ptr->next=NULL;
-    ptr->bcnt=1;
-    return ptr;
-}
-
-abac_yy_term_t *add_yy_term(abac_yy_term_t *nterm, abac_yy_term_t *terms)
-{
-    int i=terms->bcnt;
-    nterm->next=terms;
-    nterm->bcnt=i+1;
-    return nterm;
-}
-
-static void _free_yy_term(abac_yy_term_t *ptr)
-{
-    switch (ptr->type) {
-        case e_yy_DTERM_DATA:
-            _free_yy_term_data(ptr->term.d_ptr);
-            break;
-        case e_yy_DTERM_NAMED:
-            _free_yy_principal(ptr->term.n_ptr);
-            break;
-        case e_yy_DTERM_PRINCIPAL:
-            _free_yy_term_principal(ptr->term.p_ptr);
-            break;
-        case e_yy_DTERM_ANONYMOUS:
-            break;
-    }
-    if(ptr->next)
-        _free_yy_term(ptr->next);
-    free(ptr);
-}
-
-/****************************************************************/
 /* add the condition to constraint list */
 char *make_yy_oset_constraint(abac_yy_term_data_t *ptr, char *tail_string)
@@ -1097,7 +1301,7 @@
 
        char *tmp=generate_pl_oset_constraint_clause(head_oset,tail_string);
-       set_yy_term_data_condition(ptr,tmp);
-       set_yy_term_data_cptr(ptr,head_oset);
-       abac_add_yy_constraints(tmp);
+       set_yy_term_data_cond_str(ptr,tmp);
+       set_yy_term_data_cond_ptr(ptr,head_oset);
+       abac_yy_add_yy_constraints(tmp);
        return tmp;
    }
@@ -1117,6 +1321,6 @@
    int is_range=1; /* either , or | */
    if(rlist) {
-       char *rstring=string_yy_cond_range(rlist);
-       set_yy_term_data_condition(ptr,rstring);
+       char *rstring=_string_yy_cond_range(rlist);
+       set_yy_term_data_cond_str(ptr,rstring);
        abac_yy_range_t *cur;
      
@@ -1147,5 +1351,5 @@
        );
        asprintf(&tmplist,"(%s)",tmplist);
-       abac_add_yy_constraints(tmplist);
+       abac_yy_add_yy_constraints(tmplist);
    }
 }
@@ -1163,7 +1367,7 @@
 
    tmp=generate_pl_role_constraint_clause(head_role,tail_string);
-   set_yy_term_principal_condition(ptr,tmp);
-   set_yy_term_principal_cptr(ptr,head_role);
-   abac_add_yy_constraints(tmp);
+   set_yy_term_principal_cond_str(ptr,tmp);
+   set_yy_term_principal_cond_ptr(ptr,head_role);
+   abac_yy_add_yy_constraints(tmp);
    return tmp;
 
@@ -1171,201 +1375,4 @@
    return NULL;
 }
-
-/****************************************************************/
-static void _role_add_terms(int linked, abac_role_t *role, 
-abac_yy_term_t *terms)
-{
-    abac_yy_term_t *curr = terms;
-    int type;
-    char *name=NULL;
-    char *cond=NULL;
-    void *ptr=NULL;
-    while (curr) {
-        switch (curr->type) {
-           case e_yy_DTERM_DATA:
-           {
-               abac_yy_term_data_t *ptr=curr->term.d_ptr;
-               type=ptr->type;
-               name=abac_xstrdup(ptr->name);
-               cond=abac_xstrdup(ptr->condition);
-               ptr=ptr->cptr;
-               break;
-           }
-           case e_yy_DTERM_PRINCIPAL:
-           {
-              abac_yy_term_principal_t *ptr=curr->term.p_ptr;
-              type=abac_verify_term_type("principal");
-              name=abac_xstrdup(ptr->name);
-              cond=abac_xstrdup(ptr->condition);
-              ptr=ptr->cptr;
-              break;
-           }
-           case e_yy_DTERM_NAMED:
-           {
-              abac_yy_principal_t *ptr=curr->term.n_ptr;
-              type=ptr->type;
-/* XXX should be switching to sha in the future */
-              name=abac_xstrdup(ptr->cn);
-              cond=NULL;
-              ptr=NULL;
-              break;
-           }
-           case e_yy_DTERM_ANONYMOUS:
-           {
-              type=0;
-              name=abac_xstrdup("_");
-              cond=NULL;
-              ptr=NULL;
-              break;
-           }
-       }
-       abac_term_t *param=abac_term_new(type,name,cond,ptr);
-       if (linked) 
-           abac_role_role_add_linked_param(role, param);
-           else
-                abac_role_role_add_param(role, param);
-       curr=curr->next;
-   }
-}
-
-static void _role_add_linked_terms(abac_role_t *role, abac_yy_term_t *terms)
-{
-    int linked=1;
-    _role_add_terms(linked, role, terms);
-}
-
-static void _role_add_role_terms(abac_role_t *role, abac_yy_term_t *terms)
-{
-    int linked=0;
-    _role_add_terms(linked, role, terms);
-}
-
-
-abac_role_t *validate_intersected_tail_role(abac_role_t *role)
-{
-    abac_role_t *ret_role=NULL;
-    abac_list_t *prereqs = abac_list_new();
-    abac_list_add(prereqs, role);
-    ret_role = abac_role_intersection_new("intersectingROLE", prereqs);
-    return ret_role;
-}
-
-abac_role_t *validate_head_role(abac_yy_expression_t *expr)
-{
-     abac_yy_principal_t *principal=expr->principal;
-     abac_yy_role_t *role=expr->role;
-
-     char *principalname=get_yy_principal_principalname(principal);
-     char *rolename=role->name;
-     abac_yy_term_t *terms=role->terms;
-     abac_role_t *ret_role=NULL;
-
-     ret_role=abac_role_role_new(principalname, rolename);
-     if (ret_role==NULL) {
-         set_error_code(ABAC_RT_ROLE_INVALID);
-         goto error;
-     }
-     if (!abac_role_is_role(ret_role)) {
-         set_error_code(ABAC_RT_CERT_INVALID);
-         goto error;
-     }
-     // insert the params for the role
-     if(terms) {
-          _role_add_role_terms(ret_role, terms);
-     }
-
-     _add_yy_id_certs(principal->cn,principal->type);
-
-     return ret_role;
-
-error:
-     return NULL;
-}
-
-abac_role_t *validate_named_tail_role(abac_yy_expression_t *expr)
-{
-     abac_yy_principal_t *principal=expr->principal;
-     char *principalname=get_yy_principal_principalname(principal);
-     abac_role_t *ret_role=NULL;
-
-     ret_role = abac_role_principal_new(principalname);
-     _add_yy_id_certs(principal->cn,principal->type);
-
-     if (ret_role==NULL)
-         goto error;
-     return ret_role;
-
-error:
-     panic("can not generate a simple named tail role");
-     return NULL;
-}
-
-abac_role_t *validate_role_tail_role(abac_yy_expression_t *expr)
-{
-     abac_yy_principal_t *principal=expr->principal;
-     abac_yy_role_t *role=expr->role;
-
-     char *principalname=get_yy_principal_principalname(principal);
-
-     char *rolename=role->name;
-     abac_yy_term_t *terms=role->terms;
-
-     int ret;
-     abac_role_t *ret_role=NULL;
-
-     ret_role = abac_role_role_new(principalname, rolename);
-     if (ret_role==NULL)
-         goto error;
-
-     if(terms) {
-         _role_add_role_terms(ret_role, terms);
-     }
-
-     _add_yy_id_certs(principal->cn,principal->type);
-
-     return ret_role;
-
-error:
-     panic("can not generate a simple tail role");
-     return NULL;
-}
-/* need to validate/generate a linked role out of this */
-abac_role_t *validate_linked_tail_role(abac_yy_expression_t *expr)
-{
-     abac_yy_principal_t *principal=expr->principal;
-     abac_yy_role_t *role=expr->role;
-     abac_yy_role_t *linked_role=expr->linked_role;
-
-     char *principalname=get_yy_principal_principalname(principal);
-
-     char *rolename=role->name;
-     abac_yy_term_t *terms=role->terms;
-
-     char *linkedrolename=linked_role->name;
-     abac_yy_term_t *linked_terms=linked_role->terms;
-
-     int ret;
-     abac_role_t *ret_role=NULL;
-
-     ret_role = abac_role_linking_new(principalname, linkedrolename, rolename);
-     if (ret_role==NULL)
-         goto error;
-
-     if(linked_terms) {
-         _role_add_linked_terms(ret_role, linked_terms);
-     }
-     if(terms) {
-         _role_add_role_terms(ret_role, terms);
-     }
-
-     _add_yy_id_certs(principal->cn,principal->type);
-
-     return ret_role;
-
-error:
-     panic("can not generate linked tail role");
-     return NULL;
-}
-
 
 /* build up the abac structure and also create the yap clause
@@ -1426,7 +1433,7 @@
         _free_yy_expression(headexpr);
         _free_yy_expression(tailexpr);
-        abac_yap_head_role=head_role;
-        abac_yap_tail_role=tail_role;
-        abac_yap_is_oset=0;
+        abac_rule_head_role=head_role;
+        abac_rule_tail_role=tail_role;
+        abac_rule_is_oset=0;
         return tmp;
 
@@ -1442,2 +1449,12 @@
 
 
+/************************************************************************/
+void abac_yy_init()
+{
+   abac_yyinit();
+   abac_yy_init_rule_id_clauses();
+   abac_yy_init_yy_id_certs();
+   abac_yy_init_yy_constraints();
+}
+
+
