Index: libabac/abac_pl_yap.c
===================================================================
--- libabac/abac_pl_yap.c	(revision 1e51e1b0c30eaf3de435555258a21c6e026f5a39)
+++ libabac/abac_pl_yap.c	(revision abf8d5d6db95faab21303e9aef5e16dd7d6d2b03)
@@ -24,7 +24,4 @@
 extern abac_aspect_t *abac_yy_get_rule_head_aspect();
 
-/* XXXX */
-extern YAP_Term  YAP_WriteBuffer(YAP_Term, char *, unsigned int, int);
-
 static int debug=0;
 
@@ -36,7 +33,13 @@
 // pl -- place holder for now 
 struct _abac_pl_t {
-    FILE *fptr;
-    char *fname;
-    char *yap_certs;
+   /* a_term <- prin */
+    char *aspect_term;
+    char *prin_term;
+    int query_made; /* 1+ yes, 0 no, rc is valid only if a query is made */
+    int query_rc; /* rc of query call */
+    int next_rc; /* rc of next-last query */
+      /* proof count = abac_list_size(proof_list) */
+    abac_list_t *proof_list; /* list of list of cred_list */ 
+    long safe_t; /* holding the YAP slot for the goal */
 };
 
@@ -134,8 +137,89 @@
   
     abac_pl_t *pl = abac_xmalloc(sizeof(abac_pl_t));
-    pl->fptr=NULL;
-    pl->fname=NULL;
-    pl->yap_certs=NULL;
+    pl->aspect_term=NULL;
+    pl->prin_term=NULL;
+    pl->query_made=0;
+    pl->query_rc=0;
+    pl->next_rc=0;
+      /* proof count = abac_list_size(proof_list) */
+    pl->proof_list=NULL;
+    pl->safe_t=0;
     return pl;
+}
+
+/* returns the number of solution so far */
+int abac_pl_proof_cnt(abac_pl_t *pl)
+{
+    if(pl->proof_list)
+        return abac_list_size(pl->proof_list);
+    return 0;
+}
+
+/* is this the conclusion of the query ? */
+/* 1 is yes, 0 N/A */
+int abac_pl_done_yet(abac_pl_t *pl)
+{
+    if(pl->query_made) {
+        if(pl->query_rc) {
+            if(!pl->next_rc) return 1;
+            return 0;
+        } else return 1; /* even init query failed */
+    }
+    return 0;
+}
+
+static void _free_p_list(abac_list_t *p)
+{
+    if( p != NULL ) {
+        if(abac_list_size(p) != 0) {
+            abac_credential_t *cur=NULL;
+            abac_list_foreach(p, cur,
+                abac_credential_free(cur);
+            );
+        }
+        abac_list_free(p);
+    }
+}
+
+static void _free_pf_list(abac_list_t *p)
+{
+    
+    if(p != NULL) {
+        if(abac_list_size(p) != 0) {
+            abac_list_t *cur=NULL;
+            abac_list_foreach(p, cur,
+                _free_p_list(cur);
+            );
+        }
+        abac_list_free(p);
+    }
+}
+
+void abac_pl_free(abac_pl_t *pl) {
+    if(pl->aspect_term) free(pl->aspect_term);
+    if(pl->prin_term) free(pl->prin_term);
+    _free_pf_list(pl->proof_list);
+    free(pl);
+}
+
+/* this is done whenever a new query starts in */
+static void _reset_pl(abac_pl_t *pl, char *aspect_term, char *prin_term)
+{
+    if(pl->safe_t) {
+        YAP_RecoverSlots(1);
+        YAP_Reset();
+    }
+    /* free the old ones */
+    if(pl->aspect_term) free(pl->aspect_term);
+    if(pl->prin_term) free(pl->prin_term);
+    pl->aspect_term = abac_xstrdup(aspect_term);
+    pl->prin_term = abac_xstrdup(prin_term);
+    pl->query_made=0;
+    pl->query_rc=0;
+    pl->next_rc=0;
+    _free_pf_list(pl->proof_list);
+    pl->proof_list=abac_list_new();
+    pl->safe_t=0;
+    
 }
 
@@ -189,5 +273,5 @@
      ([] is not possible, and don't care)
 */
-static void _credentials_from_string(abac_stack_t *credentials,char *slist) {
+static void _credentials_from_string(abac_list_t *credentials,char *slist) {
     char *cptr=slist; /* current ptr */
     char *sptr; /* string ptr */
@@ -214,5 +298,5 @@
              free(string); 
              if(cred) {
-                 int i=abac_stack_unique_push(credentials, cred);
+                 int i=abac_list_unique_add(credentials, cred);
                  if(i) cnt++;
                  } else {
@@ -228,5 +312,5 @@
                  free(string);
                  if(cred) {
-                     int i=abac_stack_unique_push(credentials, cred);
+                     int i=abac_list_unique_add(credentials, cred);
                      if(i) cnt++;
                      } else {
@@ -270,6 +354,6 @@
 }
 
-/* make a query and extract just 1 set of result */
-static abac_stack_t *_make_yap_query(char *prin, char *rule, char* estring)
+/* make a query and extract just first set of result */
+static abac_list_t *_make_yap_query(abac_pl_t *pl, char *prin, char *nm, char* estring)
 {
     YAP_Term *eterm0;
@@ -277,7 +361,11 @@
     YAP_Term arg[3];
 
-    abac_stack_t *cred_list = abac_stack_new();
-
-    if(debug) fprintf(stderr," the principal part(%s)\n", prin);
+    abac_list_t *cred_list = abac_list_new();
+
+    if(debug) { 
+       fprintf(stderr," the principal part(%s)\n", prin);
+       fprintf(stderr," the role/oset part(%s)\n", estring);
+       fprintf(stderr," nm part(%s)\n", nm);
+    } 
     if(prin[0]=='\'' || prin[0]=='"') {
         arg[0]=YAP_ReadBuffer(prin,eterm0);
@@ -295,11 +383,15 @@
     YAP_Term goal=YAP_MkApplTerm(func, 3, arg);
 
-    int rc =YAP_RunGoal( goal );
-    if (rc) {
+    pl->safe_t = YAP_InitSlot(goal);
+    pl->query_rc =YAP_RunGoal(goal);
+    pl->query_made=1;
+    pl->next_rc=pl->query_rc;
+    if (pl->query_rc) {
         printf("YAP query succeed\n");
         char *tmp=NULL;
         int tmp_sz=try_again(0, &tmp);
+        YAP_Term argterm=YAP_ArgOfTerm(3,YAP_GetFromSlot(pl->safe_t));
         while(1) {
-            YAP_WriteBuffer(arg[2], tmp, tmp_sz,YAP_WRITE_HANDLE_VARS);
+            YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS);
             if(strlen(tmp) > 5 && tmp[0]=='\[')
                 break;
@@ -313,24 +405,71 @@
         if(debug) fprintf(stderr,"    query answer : %s(%d)\n", tmp, strlen(tmp));
         _credentials_from_string(cred_list,tmp); 
-        if(abac_stack_size(cred_list)==0) {
+        if(abac_list_size(cred_list)==0) {
             fprintf(stderr,"CAN NOT retrieve result properly from YAP!!!\n");
             YAP_Exit(1);
         }
+        abac_list_add(pl->proof_list, cred_list);
         YAP_FreeSpaceFromYap(tmp);
-/**** XXX
-        while (YAP_RestartGoal()) {
-            if(debug) fprintf(stderr,"another success\n");
-            YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
-        if(debug) fprintf(stderr,"    restart query answer : %s\n", tmp);
-            _credentials_from_string(cred_list,tmp); 
-        }
-***/
     } else {
         printf("YAP query failed\n");
         /* YAP_Exit(1); */
     }
-/*** MEI ***/
-    YAP_Reset();
     return cred_list;
+}
+
+/* make a query for next possible solution proof */
+static abac_list_t *_query_again(abac_pl_t *pl)
+{
+    abac_list_t *cred_list=abac_list_new();
+
+    pl->next_rc=YAP_RestartGoal();
+    pl->query_made++;
+    if(debug) fprintf(stderr,"query_again: another success\n");
+
+    char *tmp=NULL;
+    int tmp_sz=try_again(0, &tmp);
+    YAP_Term argterm=YAP_ArgOfTerm(3,YAP_GetFromSlot(pl->safe_t));
+    if(pl->next_rc) {
+        while(1) {
+            YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS);
+            if(strlen(tmp) > 5 && tmp[0]=='\[')
+                break;
+            tmp_sz=try_again(tmp_sz,&tmp);
+            if(tmp_sz==0) {
+                fprintf(stderr," PANIC, run out of heap space..\n");
+                YAP_Exit(1);
+            }
+        }
+        /* this is returned as ['string1','string2'] */
+        if(debug) fprintf(stderr,"    query answer : %s(%d)\n", tmp, strlen(tmp));
+        _credentials_from_string(cred_list,tmp); 
+        if(abac_list_size(cred_list)==0) {
+            fprintf(stderr,"CAN NOT retrieve result properly from YAP!!!\n");
+            YAP_Exit(1);
+        }
+        abac_list_add(pl->proof_list, cred_list);
+        YAP_FreeSpaceFromYap(tmp);
+    }
+    return cred_list;
+}
+
+abac_stack_t *_make_cred_stack(abac_list_t *p)
+{
+    abac_stack_t *ret=abac_stack_new();
+    if(abac_list_size(p) != 0) {
+        abac_credential_t *cur=NULL;
+        abac_list_foreach(p, cur,
+            abac_stack_push(ret, (void *)abac_credential_dup(cur));
+        );
+    }
+    return ret;
+}
+
+/* force a  backtrack to get next solution proof */
+abac_stack_t *abac_pl_query_again(abac_pl_t *pl)
+{
+    abac_list_t *rlist=_query_again(pl);
+    abac_stack_t *ret=_make_cred_stack(rlist);
+    return ret;
 }
 
@@ -382,5 +521,8 @@
     }
 
-    ret=_make_yap_query(prin_nm,nm,tmp);
+    _reset_pl(pl,tmp,prin_nm);
+    abac_list_t *rlist=_make_yap_query(pl,prin_nm,nm,tmp);
+    /* generate the resulting stack from a list */
+    ret=_make_cred_stack(rlist);
     return ret;
 }
@@ -443,15 +585,2 @@
     return ret;
 }
-
-void abac_pl_free(abac_pl_t *pl) {
-    if(pl->fptr) {
-        fflush(pl->fptr);
-        free(pl->fptr);
-    }
-    if(pl->fname) {
-        unlink(pl->fname);
-        free(pl->fname);
-    }
-    free(pl);
-}
-
