Index: libabac/abac_pl_yy.c
===================================================================
--- libabac/abac_pl_yy.c	(revision 2a7335790cc0fe71375373451ebdc26c1e5e9cba)
+++ libabac/abac_pl_yy.c	(revision 54758a71f4b670d8bbc71a860349758063e1fa81)
@@ -1,2 +1,5 @@
+/**
+** abac_pl_yy.c 
+**/
 
 /* C declarations */
@@ -6,27 +9,22 @@
 #include <assert.h>
 
+#include "abac_stack.h"
+
+#include "abac_pl_yy.h"
 #include "abac_util.h"
-#include "abac_pl_yy.h"
+#include "abac_pl_gen.h"
+#include "abac_list.h"
+#include "abac_term.h"
+#include "abac_aspect.h"
 
 static int debug=0;
 
-int using_this=0;
-
-extern void panic(char*);
-
-/* from abac_pl_gen.c */
-extern abac_list_t *generate_pl_clauses(abac_aspect_t *, abac_aspect_t *);
-
-/* from rt2.y */
-extern void abac_yyinit();
-
-int abac_yy_error_code = 0; /* keeping last error code */
-
+abac_context_t *abac_context=NULL; /* context used for this parsing */
+static int yy_using_this=0;
+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_aspect_t *abac_rule_head_aspect=NULL;
 abac_aspect_t *abac_rule_tail_aspect=NULL;
-
 
 /* structure to hold the range information
@@ -84,4 +82,5 @@
    e_yy_DTERM_NAMED,     keyid:bob
    e_yy_DTERM_PRINCIPAL, principal:?P
+   e_yy_DTERM_THIS,      ?this 
    e_yy_DTERM_ANONYMOUS  ? */
 struct _abac_yy_term_t {
@@ -125,5 +124,8 @@
 };
 
+/* local */
 static void _free_yy_expression(abac_yy_expression_t *);
+abac_aspect_t *validate_head(int yytype, abac_yy_expression_t *expr);
+abac_aspect_t *validate_linked_tail(int yytype, abac_yy_expression_t *expr);
 
 /************************************************************************/
@@ -133,50 +135,4 @@
 }
 
-/************************************************************************/
-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) {
-         fprintf(stderr,"printing out abac_rule_clauses:\n");
-         abac_print_clauses(abac_rule_clauses,stderr);
-    }
-}
-
-abac_list_t *abac_yy_get_rule_clauses()
-{
-    return abac_rule_clauses;
-}
-
-void abac_yy_free_rule_clauses()
-{
-    if (abac_rule_clauses != NULL) {
-        abac_pl_free_id_certs();
-        abac_list_free(abac_rule_clauses);
-    }
-}
-
-/* just set it to NULL without freeing the structure */
-void abac_yy_reset_rule_clauses()
-{
-    abac_rule_clauses=NULL;
-}
-
 int abac_yy_get_rule_is_oset()
 {
@@ -193,5 +149,4 @@
     return abac_rule_tail_aspect;
 }
-
 
 /************************************************************************/
@@ -447,5 +402,10 @@
 static void _set_using_this()
 {
-    using_this=1;
+    yy_using_this=1;
+}
+
+int abac_yy_get_using_this()
+{
+    return yy_using_this;
 }
 
@@ -457,14 +417,5 @@
 void set_yy_term_principal_name(abac_yy_term_principal_t *ptr, char *name)
 {
-    /* handle special case when the name=this, change it to This */
-    if(strcmp(name,"this")==0) {
-       ptr->name=abac_xstrdup("This");
-       _set_using_this();
-       } else {
-         ptr->name=abac_xstrdup(name);
-         /* just in case name came in as This already */
-         if(strcasecmp(name,"this")==0) 
-           _set_using_this();
-    }
+    ptr->name=abac_xstrdup(name);
 }
 
@@ -503,4 +454,14 @@
         _free_yy_expression(ptr->cond_head_expr);
     free(ptr);
+}
+
+abac_yy_term_t *make_yy_term_dterm_this()
+{
+    abac_yy_term_t *ptr=(abac_yy_term_t*)abac_xmalloc(sizeof(abac_yy_term_t));
+    ptr->type = e_yy_DTERM_THIS;
+    ptr->next=NULL;
+    ptr->bcnt=1;
+    _set_using_this();
+    return ptr;
 }
 
@@ -615,4 +576,5 @@
             break;
         case e_yy_DTERM_ANONYMOUS:
+        case e_yy_DTERM_THIS:
             break;
     }
@@ -718,10 +680,13 @@
 void make_yy_oset_constraint(abac_yy_term_data_t *ptr, char *tail_string)
 {
-   abac_yy_expression_t *head_expr=get_yy_term_data_cond_head_expr(ptr);
-   if(head_expr) {
-       abac_aspect_t *head_aspect=validate_head(e_yy_OSET_TYPE,head_expr);
-       if(head_aspect != NULL) {
+   abac_yy_expression_t *cond_expr=get_yy_term_data_cond_head_expr(ptr);
+   if(cond_expr) {
+       abac_aspect_t *cond_aspect=NULL;
+       if(cond_expr->type == e_yy_EXPR_LINKED)
+           cond_aspect=validate_linked_tail(e_yy_OSET_TYPE,cond_expr);
+       else cond_aspect=validate_head(e_yy_OSET_TYPE,cond_expr);
+       if(cond_aspect != NULL) {
            if(debug) fprintf(stderr,"make_yy_oset_constraint..\n");
-           set_yy_term_data_cond_ptr(ptr,head_aspect);
+           set_yy_term_data_cond_ptr(ptr,cond_aspect);
        }
    }
@@ -731,10 +696,13 @@
 void make_yy_role_constraint(abac_yy_term_principal_t *ptr, char *tail_string)
 {
-   abac_yy_expression_t *head_expr=get_yy_term_principal_cond_head_expr(ptr);
-   if(head_expr) {
-       abac_aspect_t *head_aspect=validate_head(e_yy_ROLE_TYPE,head_expr);
-       if(head_aspect != NULL) {
+   abac_yy_expression_t *cond_expr=get_yy_term_principal_cond_head_expr(ptr);
+   if(cond_expr) {
+       abac_aspect_t *cond_aspect=NULL;
+       if(cond_expr->type == e_yy_EXPR_LINKED)
+           cond_aspect=validate_linked_tail(e_yy_ROLE_TYPE,cond_expr);
+       else cond_aspect=validate_head(e_yy_ROLE_TYPE,cond_expr);
+       if(cond_aspect != NULL) {
           if(debug) fprintf(stderr,"make_yy_role_constraint..\n");
-          set_yy_term_principal_cond_ptr(ptr,head_aspect);
+          set_yy_term_principal_cond_ptr(ptr,cond_aspect);
           set_yy_term_principal_isrange(ptr,0);
        }
@@ -798,4 +766,10 @@
               break;
            }
+           case e_yy_DTERM_THIS:
+           {
+              type=abac_verify_term_type("this");
+              name=abac_xstrdup("This");
+              break;
+           }
        }
        abac_term_t *param=NULL;
@@ -985,8 +959,7 @@
 
 /**********************************************************************/
-
-abac_list_t *make_oset_statement(abac_yy_expression_t *headexpr,
-                       abac_yy_expression_t *tailexpr) 
+int make_oset_statement(abac_yy_expression_t *headexpr, abac_yy_expression_t *tailexpr) 
 { 
+    int rc=0;
     abac_aspect_t *head_oset=NULL;
     abac_aspect_t *tail_oset=NULL;
@@ -994,6 +967,8 @@
 /* build up left side's abac oset structure */
     head_oset=validate_head(e_yy_OSET_TYPE, headexpr);
-    if(head_oset == NULL)
+    if(head_oset == NULL) {
+        rc=1;
         goto error;
+    }
 
 /* build up the right side's abac oset structure */
@@ -1016,6 +991,8 @@
                     break;
             }
-            if(curr_oset==NULL)
+            if(curr_oset==NULL) {
+                rc=1;
                 goto error;
+            }
 
             /* check if first one */
@@ -1031,13 +1008,4 @@
         } /* while */
 
-        preprocess_pl_head(head_oset);
-        preprocess_pl_tail(tail_oset);
-
-/* XXX collect up type clauses, constraint clauses and 
-   generate rule clauses */
-        abac_list_t *tmp=generate_pl_clauses(head_oset,tail_oset);
-        if(tmp == NULL)
-            goto error;
-
         if(debug) {
             fprintf(stderr,"aspect head_oset: ");
@@ -1049,4 +1017,5 @@
         }
 
+error:
         _free_yy_expression(headexpr);
         _free_yy_expression(tailexpr);
@@ -1054,14 +1023,5 @@
         abac_rule_tail_aspect=tail_oset;
         abac_rule_is_oset=1;
-        return tmp;
-
-error:
-        _free_yy_expression(headexpr);
-        _free_yy_expression(tailexpr);
-        if(head_oset)
-            abac_aspect_free(head_oset);
-        if(tail_oset)
-            abac_aspect_free(tail_oset);
-        return NULL;
+    return rc;
 }
 
@@ -1102,9 +1062,14 @@
 }
 /*****************************************************************************/
-/* build up the abac structure and also create the yap clause
-   and insert it into db */
-abac_list_t *make_role_statement(abac_yy_expression_t *headexpr,
-abac_yy_expression_t *tailexpr) 
+abac_context_t *get_abac_context()
+{
+    assert(abac_context);
+    return abac_context;
+}
+
+/* build up the abac structure */
+int make_role_statement(abac_yy_expression_t *headexpr, abac_yy_expression_t *tailexpr) 
 { 
+    int rc=0;
     abac_aspect_t *head_role=NULL;
     abac_aspect_t *tail_role=NULL;
@@ -1112,6 +1077,8 @@
 /* build up left side's abac role structure */
     head_role=validate_head(e_yy_ROLE_TYPE,headexpr);
-    if(head_role == NULL)
+    if(head_role == NULL) {
+        rc=1;
         goto error;
+    }
 
 /* build up the right side's abac role structure */
@@ -1131,6 +1098,8 @@
                     break;
             }
-            if(curr_role==NULL)
+            if(curr_role==NULL) {
+                rc=1;
                 goto error;
+            }
 
             /* check if first one */
@@ -1146,13 +1115,4 @@
         } /* while */
 
-        preprocess_pl_head(head_role);
-        preprocess_pl_tail(tail_role);
-
-/* collect up type clauses, constraint clauses and 
-   generate the final rule clauses */
-        abac_list_t *tmp=generate_pl_clauses(head_role,tail_role);
-        if(tmp == NULL)
-            goto error;
-
         if(debug) {
             fprintf(stderr,"aspect head_role: ");
@@ -1164,4 +1124,5 @@
         }
 
+error:
         _free_yy_expression(headexpr);
         _free_yy_expression(tailexpr);
@@ -1169,22 +1130,37 @@
         abac_rule_tail_aspect=tail_role;
         abac_rule_is_oset=0;
-        return tmp;
-
-error:
-        _free_yy_expression(headexpr);
-        _free_yy_expression(tailexpr);
-        if(head_role)
-            abac_aspect_free(head_role);
-        if(tail_role)
-            abac_aspect_free(tail_role);
-        return NULL;
+
+        return rc;
 }
 
 /************************************************************************/
-void abac_yy_init()
-{
+void abac_yy_init(abac_context_t *ctxt)
+{
+   yy_using_this=0;
+   abac_context=ctxt;
    abac_yyinit();
-   abac_yy_init_rule_id_clauses();
-}
-
-
+}
+
+/************************************************************************/
+/* this is called to parse a string into 2 aspect structures            */
+/* NEED TO BE LOCKED HERE XXX */
+int abac_yy_parse(abac_context_t *ctxt, char* attr_string, abac_aspect_t **head_aspect, abac_aspect_t **tail_aspect, int *using_this)
+{
+    /*ctxt could be null*/
+    abac_ll_reset_yyfptr(attr_string);
+    abac_yy_init(ctxt);
+    int rc=yyparse();
+    if (rc) {
+        free(attr_string);
+        abac_yy_set_error_code(ABAC_RT_CERT_INVALID);
+        return 1;
+    }
+    *head_aspect = abac_yy_get_rule_head_aspect();
+    *tail_aspect = abac_yy_get_rule_tail_aspect();
+    *using_this= abac_yy_get_using_this();
+    return 0;
+}
+
+
+
+
