Index: libabac/abac_pl_yap.c
===================================================================
--- libabac/abac_pl_yap.c	(revision 53e540d5b3ba8b09dcdf1f90cf514182a491d9d3)
+++ libabac/abac_pl_yap.c	(revision 202a7f97d0dc5cb5eb2b43332f0711eafa4d7170)
@@ -21,4 +21,6 @@
 
 extern char *abac_cn_with_role(abac_role_t *);
+extern abac_list_t *abac_credential_clauses(abac_credential_t *cred);
+static int debug=0;
 
 // pl
@@ -31,5 +33,5 @@
 static int _insert_clause(char *str)
 {
-    YAP_Term eterm;
+    YAP_Term *eterm;
     YAP_Term goalArgs=YAP_ReadBuffer(str, eterm);
     char *tmp=YAP_CompileClause(goalArgs);
@@ -118,8 +120,8 @@
 
     if (YAP_RunGoal(YAP_MkAtomTerm(YAP_LookupAtom("source")))) {
-        printf("calling source..\n");
+        if(debug) printf("calling source..\n");
         } else {
-        printf("calling source failed..\n");
-        YAP_Exit(1);
+            if(debug) printf("calling source failed..\n");
+            YAP_Exit(1);
     }
 
@@ -134,15 +136,30 @@
 
 /**
- * Add a credential to the credential pl.
+ * Add a credential to the db
  */
 int abac_pl_add_credential(abac_pl_t *pl, abac_credential_t *cred)
 {
     int rc=0;
-    char *str = abac_credential_clause(cred);
-    if (str != NULL) {
-        rc=_insert_cred_clause(str);
-    }
-    _show_yap_db("calling within abac_pl_add_credential");
+    abac_list_t *clauses=abac_credential_clauses(cred);
+    if (clauses != NULL) {
+        char *cur;
+        abac_list_foreach(clauses, cur,
+            if(cur) {
+                if(debug) printf("inserting =>%s\n",cur);
+                rc=_insert_cred_clause(cur);
+            }
+        );
+    }
     return rc;
+}
+
+int abac_pl_add_type_credential(abac_pl_t *pl, abac_id_cert_t *id_cert)
+{
+    char *clause=abac_id_clause(id_cert);
+    if (clause != NULL) {
+        int rc=_insert_cred_clause(clause);
+        return rc;
+    }
+    return 0;
 }
 
@@ -152,11 +169,15 @@
      ([] is not possible, and don't care)
 */
-static void _credentials_from_string(abac_list_t *credentials,char *slist) {
+static void _credentials_from_string(abac_stack_t *credentials,char *slist) {
     char *cptr=slist; /* current ptr */
     char *sptr; /* string ptr */
     char *ptr;
-    int len=strlen(ptr);
+    int len=0;
     char *string;
     abac_credential_t *cred=NULL;
+    int cnt=0;
+
+    if(debug)
+        printf("DEBUG:responds from yap(%s)\n",slist);
 
     /* find first [' */  
@@ -171,9 +192,11 @@
         if(ptr!=NULL) {
              cptr=ptr+3;
-             len=ptr-sptr;
+             len=(ptr-sptr);
              string=strndup(sptr,len);
              cred=abac_credential_lookup(string);
+             free(string); 
              if(cred) {
-                 abac_list_add(credentials, abac_credential_dup(cred));
+                 abac_stack_push(credentials, cred);
+                 cnt++;
                  } else {
                  printf("BAD BAD BAD\n");
@@ -183,9 +206,11 @@
              ptr=strstr(cptr,"']");
              if(ptr!=NULL) {
-                 len=ptr-sptr;
+                 len=(ptr-sptr);
                  string=strndup(sptr,len);
                  cred=abac_credential_lookup(string);
+                 free(string);
                  if(cred) {
-                     abac_list_add(credentials, abac_credential_dup(cred));
+                     abac_stack_push(credentials, cred);
+                     cnt++;
                      } else {
                      printf("BAD BAD BAD\n");
@@ -195,15 +220,20 @@
         }
     }
+    if(debug)
+        printf("DEBUG:total %d credentials\n", cnt);
 }
 
 /* 2 types 
-      acme.buys_rocket <- coyote 
+      acme.buys_rocket <- coyote (coyote=prin, acme.buys_rocket=role)
       acme.buys_rocket <- acme.preferred_customer  -- NOT DONE YET */
-static abac_list_t *_query(abac_pl_t *pl, abac_role_t* role, abac_role_t* role_prin )
+static abac_stack_t *_query(abac_pl_t *pl, abac_role_t* role, abac_role_t* role_prin )
 {
     YAP_Term *eterm;
     YAP_Term arg[3];
-    char tmp[1000];
-    abac_list_t *cred_list = NULL;
+    char tmp[2000];
+    abac_stack_t *cred_list = abac_stack_new();
+
+    if(debug)
+        _show_yap_db("DEBUG:calling within _query");
 
     char *cn_prin=abac_cn_with_role(role_prin);
@@ -211,5 +241,5 @@
 
     if(abac_role_role_name(role_prin)!=NULL) {
-        printf("fail, query's call need proper principal id !!!\n");
+        printf("fail, a.r <- b.r query is not implemented yet !!!\n");
         YAP_Exit(1);
     }
@@ -228,7 +258,6 @@
     int rc =YAP_RunGoal( goal );
     if (rc) {
-        cred_list = abac_list_new();
         printf("success, query call ok.. \n");
-        YAP_WriteBuffer(arg[2], tmp, 1000,YAP_WRITE_HANDLE_VARS);
+        YAP_WriteBuffer(arg[2], tmp, 2000,YAP_WRITE_HANDLE_VARS);
         /* printf("    answer : %s(%d)\n", tmp, strlen(tmp)); */
         /* this is returned as ['string1','string2'] */
@@ -236,5 +265,5 @@
         while (YAP_RestartGoal()) {
             printf("another success\n");
-            YAP_WriteBuffer(arg[2], tmp, 1000,YAP_WRITE_HANDLE_VARS);
+            YAP_WriteBuffer(arg[2], tmp, 2000,YAP_WRITE_HANDLE_VARS);
             /* printf("    answer : %s\n", tmp); */
             _credentials_from_string(cred_list,tmp); 
@@ -252,15 +281,12 @@
 --role acme.prefer_customer.buy_rockets --principlal coyote
  */
-abac_list_t *abac_pl_query(abac_pl_t *pl, char *role_name, char *principal)
+abac_stack_t *abac_pl_query(abac_pl_t *pl, char *role_name, char *principal)
 {
     abac_role_t *role = abac_role_from_string(role_name);
     abac_role_t *prin_role = abac_role_from_string(principal);
-    char *query_clause = NULL;
-    abac_list_t *ret=NULL;
+    abac_stack_t *ret=NULL;
 
     // give up on bogus roles
     if (role == NULL || prin_role == NULL) {
-        free(role);
-        free(prin_role);
         return NULL;
     }
@@ -275,6 +301,6 @@
  * (which returns in string form)
  */
-abac_list_t *abac_pl_credentials(abac_pl_t *pl) {
-    abac_list_t *credentials = abac_list_new();
+abac_stack_t *abac_pl_credentials(abac_pl_t *pl) {
+    abac_stack_t *credentials = abac_stack_new();
     /* fix up a stream to stream the credentials from prolog engine */
     return credentials;
