Index: libabac/abac_pl_yap.c
===================================================================
--- libabac/abac_pl_yap.c	(revision f2d67a54fd3b4f39a02a2f946632cfe16b73c482)
+++ libabac/abac_pl_yap.c	(revision 46df1bccf39ae760708f33fb41ec1819bdc1fde2)
@@ -19,4 +19,5 @@
 #include "uthash.h"
 
+extern abac_credential_t *abac_credential_dup(abac_credential_t *cred);
 extern abac_list_t *abac_credential_clauses(abac_credential_t *);
 extern char *abac_id_clause(abac_id_credential_t *);
@@ -81,5 +82,5 @@
     int query_type;
     abac_list_t *proof_list; /* list of list of cred_list */ 
-    long safe_t; /* holding the YAP slot for the goal */
+    long safe_t; /* holding the YAP's saved goal */
 };
 
@@ -126,6 +127,5 @@
 
     printf("\n\n/* ========= yap db (%s) */\n",msg);
-    YAP_Term goal=YAP_ReadBuffer(lstr, &eterm);
-    int rc =YAP_RunGoal( goal );
+    int rc=YAP_RunGoalOnce(YAP_ReadBuffer(lstr,&eterm));
     if (rc) {
         printf("/* listing ok.. */\n");
@@ -164,8 +164,11 @@
 abac_pl_t *abac_pl_new(void) {
 
+    YAP_Term eterm;
+
     if (YAP_FastInit(NULL) == YAP_BOOT_ERROR)
         YAP_Exit(1);
 
-    if (YAP_RunGoal(YAP_MkAtomTerm(YAP_LookupAtom("source")))) {
+    int rc=YAP_RunGoalOnce(YAP_ReadBuffer("source",&eterm));
+    if(rc) {
         if(debug) fprintf(stderr, "calling source..\n");
         } else {
@@ -277,8 +280,15 @@
 char *what, char *whom, char *aspect, char *whatwith, int pcnt)
 {
-    if(pl->safe_t) {
-        YAP_RecoverSlots(1);
-        YAP_Reset();
-    }
+    if(pl->safe_t != 0) {
+#ifdef  YAP_VERSION 
+        if(YAP_VERSION == "6.2.3") {
+            YAP_RecoverSlots(1); 
+            YAP_Reset();  
+            } else { /* 6.3.2 seems to have YAP_Reset problem */
+                YAP_RecoverSlots(1); 
+        }
+    }
+#endif
+
     /* free the old ones */
     if(pl->who) free(pl->who);
@@ -350,10 +360,11 @@
 }
 
-/* cases, 
+/* cases, for YAP 6.2.x 
      ['str']
      ['str1','str2']
      ([] is not possible, and don't care)
 */
-static void _credentials_from_string(abac_list_t *credentials,char *slist) {
+static void _credentials_from_string_2(abac_list_t *credentials,char *slist) 
+{
     char *cptr=slist; /* current ptr */
     char *sptr; /* string ptr */
@@ -408,7 +419,78 @@
 }
 
+/*  XXX just for yap 6.3.2
+     [str]
+     [str1,str2]
+     ([] is not possible, and don't care)
+*/
+static void _credentials_from_string_3(abac_list_t *credentials,char *slist)
+{
+    char *cptr=slist; /* current ptr */
+    char *sptr; /* string ptr */
+    char *ptr;
+    int len=0;
+    char *string;
+    abac_credential_t *cred=NULL;
+    int cnt=0;
+
+    /* find first [ */  
+    ptr=strstr(cptr,"[");
+    if(ptr == NULL) 
+         return;
+    cptr=ptr+1;
+    sptr=cptr;
+    while (1) {
+        /* find next "," or "]" */
+        if(debug) fprintf(stderr,"START with %d\n", strlen(cptr));
+        ptr=strstr(cptr,",");
+        if(ptr!=NULL) {
+             cptr=ptr+1;
+             len=(ptr-sptr);
+             string=strndup(sptr,len);
+             cred=abac_credential_lookup(string);
+             free(string); 
+             if(cred) {
+                 int i=abac_list_unique_add(credentials, cred);
+                 if(i) cnt++;
+                 } else {
+                 printf("BAD BAD\n");
+             }
+             sptr=cptr;
+             } else {
+             ptr=strstr(cptr,"]");
+             if(ptr!=NULL) {
+                 len=(ptr-sptr);
+                 string=strndup(sptr,len);
+                 cred=abac_credential_lookup(string);
+                 free(string);
+                 if(cred) {
+                     int i=abac_list_unique_add(credentials, cred);
+                     if(i) cnt++;
+                     } else {
+                     printf("BAD BAD BAD\n");
+                 }
+                 break;
+             }
+        }
+    }
+    if(debug)
+        fprintf(stderr,"DEBUG:total %d credentials\n", cnt);
+}
+
+static void _credentials_from_string(abac_list_t *credentials,char *slist) {
+#ifdef YAP_VERSION 
+    if(YAP_VERSION == "6.2.3")
+         _credentials_from_string_2(credentials,slist);
+    else
+         _credentials_from_string_3(credentials,slist);
+#endif
+    if(debug) 
+         fprintf(stderr,"_credentials_from_string: missing YAP_VERSION\n");
+}
+
 /* MAX 1024x1024, double as it goes */
 static int try_again(int sz, char **tptr)
 {
+   int debug=1;
    int blk=1024*2; /* 2048 */
    int max=1024*1024;
@@ -417,6 +499,8 @@
 
    if(sz==0) {
+/* XXX size = (sizeof(char) * (blk+1)); */
        size = (sizeof(char) * (blk+1));
        } else {
+           if(debug) fprintf(stderr,"calling try_again needing extra space!!\n");
            if (sz>=max) {
                size=0;
@@ -437,4 +521,26 @@
 }
 
+static int _check_buffer(char *tmp)
+{
+    int len=strlen(tmp); /* just for reference :-( */
+    if (len < 3) return 0;
+    if (tmp[0] != '[') return 0;
+    if (tmp[len-1] == ']') return 1;
+    int i=0; 
+    for(i=3; i<=len; i++) {
+        if(tmp[i]==']') {
+           if(i==len) fprintf(stderr," _check_buffer, match up..\n");
+             else {
+                 fprintf(stderr," _check_buffer, did not match up ..\n");
+                 tmp[i+1]='\0';
+           }
+           return 1;
+        }
+    }
+    if(debug)
+        fprintf(stderr," _check_buffer, did not find the closing square bracket ..\n");
+    return 0;
+}
+
 abac_list_t *run_goal(abac_pl_t *pl, YAP_Term *arg)
 {
@@ -445,5 +551,7 @@
 
     pl->safe_t = YAP_InitSlot(goal);
+if(debug) fprintf(stderr," run_goal, safe_t is %ld\n", pl->safe_t);
     pl->query_rc =YAP_RunGoal(goal);
+    goal=YAP_GetFromSlot(pl->safe_t);
     pl->query_made=1;
     pl->next_rc=pl->query_rc;
@@ -453,23 +561,30 @@
             else 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));
+        int tmp_sz=0; 
+        if(YAP_VERSION == "6.2.3")
+            tmp_sz=try_again(0, &tmp);
+        YAP_Term argterm=YAP_ArgOfTerm(3,goal);
         while(1) {
-/* not in 6.2.2
-            int rc=YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS);
-*/
-            YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS);
-/*
+            int rc=1; /* XXX rc does not seems to be right... */
+            if(YAP_VERSION == "6.2.3") {
+                YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS); 
+                } else {
+                    rc=YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS);
+            }
 if(debug) fprintf(stderr,"call YAP_WriteBuffer: rc(%d) tmp_sz(%d)\n", rc, tmp_sz);
-*/
-            if(strlen(tmp) > 5 && tmp[0]=='\[' && tmp[strlen(tmp)-1]=='\]' ) {
-if(debug) fprintf(stderr,"what came back .. (%d)(%s)\n", strlen(tmp),tmp);
+if(debug) fprintf(stderr,"what came back .. rc(%d)(%d:%c)\n",rc,strlen(tmp),tmp[strlen(tmp)-1]);
+
+/** it seems that 6.3 gives me some garbage at the back.. */
+            if(rc==1 && _check_buffer(tmp)) {
+if(debug) fprintf(stderr,".. okay\n");
                 break;
-            }
-            tmp_sz=try_again(tmp_sz,&tmp);
+            } else {
+if(debug) fprintf(stderr,".. not okay\n");
+                tmp_sz=try_again(tmp_sz,&tmp);
 if(debug) fprintf(stderr,"try_again: tmp_sz(%d)\n", tmp_sz);
-            if(tmp_sz==0) {
-                fprintf(stderr," PANIC, run out of heap space..\n");
-                YAP_Exit(1);
+                if(tmp_sz==0) {
+                    fprintf(stderr," PANIC, run out of heap space..\n");
+                    YAP_Exit(1);
+                }
             }
         }
@@ -484,5 +599,5 @@
         YAP_FreeSpaceFromYap(tmp);
     } else {
-        /* don't print anything if it is running a parital proof goal */
+        /* don't print anything if it is running a partial proof goal */
         if(!pl->is_partial) 
             printf("YAP query failed\n");
@@ -741,8 +856,9 @@
     pl->next_rc=YAP_RestartGoal();
     pl->query_made++;
+    YAP_Term goal=YAP_GetFromSlot(pl->safe_t);
 
     char *tmp=NULL;
     int tmp_sz=try_again(0, &tmp);
-    YAP_Term argterm=YAP_ArgOfTerm(3,YAP_GetFromSlot(pl->safe_t));
+    YAP_Term argterm=YAP_ArgOfTerm(3,goal);
     if(pl->next_rc) {
         if(debug) fprintf(stderr,"query_again: another success\n");
@@ -784,5 +900,5 @@
 void _print_cred_list(abac_list_t *p)
 {
-    if(debug) fprintf(stderr,"_print_cred_list(%s)items:\n",abac_list_size(p));
+    if(debug) fprintf(stderr,"_print_cred_list(%d)items:\n",abac_list_size(p));
     if(abac_list_size(p) != 0) {
         abac_credential_t *cur=NULL;
