/*************************
 * swi_list_length
 *************************/
int
swi_list_length(term_t pl_list)
{
    fid_t       frame;
    predicate_t pr_length;
    term_t      pl_args, pl_length;
    int         length;


    frame = PL_open_foreign_frame();

    pr_length = PL_predicate("length", 2, NULL);    
    pl_args   = PL_new_term_refs(2);
    pl_length = pl_args + 1;
    length    = -1;

    if (!PL_unify(pl_args, pl_list) || 
        !PL_call_predicate(NULL, PL_Q_NORMAL, pr_length, pl_args))
        goto out;

    PL_get_integer(pl_length, &length);
    
 out:
    PL_discard_foreign_frame(frame);

    return length;
}
Beispiel #2
0
static char *prolog_cerrar(modulo_t *modulo)
{  
  prolog_dato_t *prolog = (prolog_dato_t*)modulo->m_dato;
  PL_discard_foreign_frame(prolog->m_fid);
  PL_cleanup(0);   
  free(prolog);
  free(modulo);
  return "cerrado";
}
Beispiel #3
0
static void
call_prolog_goal(prolog_goal *g)
{ fid_t fid;
  static predicate_t pred = NULL;
  int rc;

  if ( !pred )
    pred = PL_predicate("call", 1, "user");

  if ( (fid = PL_open_foreign_frame()) )
  { term_t t = PL_new_term_ref();
    term_t vars;
    rc = PL_recorded(g->goal, t);
    PL_erase(g->goal);
    g->goal = 0;
    g->state = G_RUNNING;
    if ( rc )
    { qid_t qid;
      int flags = PL_Q_NORMAL;

      if ( g->acknowledge )
      { flags |= PL_Q_CATCH_EXCEPTION;
	vars = PL_new_term_ref();
	if ( !PL_get_arg(2, t, vars) ||		/* Goal-Vars */
	     !PL_get_arg(1, t, t) )
	{ PL_warning("ERROR: in_pce_thread: bad goal-vars term");
	}
      } else
      { vars = 0;
      }

      if ( (qid = PL_open_query(g->module, flags, pred, t)) )
      { rc = PL_next_solution(qid);

	if ( rc )
	{ g->state = G_TRUE;
	  if ( vars )
	    g->result = PL_record(vars);
	} else
	{ term_t ex;

	  if ( g->acknowledge && (ex=PL_exception(qid)) )
	  { g->result = PL_record(ex);
	    g->state = G_ERROR;
	  } else
	  { g->state = G_FALSE;
	  }
	}

	PL_cut_query(qid);
      } else
	PL_warning("ERROR: pce: out of global stack");
    }
    PL_discard_foreign_frame(fid);
  } else
    PL_warning("ERROR: pce: out of global stack");
}
Beispiel #4
0
int
query_loop(atom_t goal, int loop)
{ GET_LD
  int rc;
  int clear_stacks = (LD->query == NULL);

  do
  { fid_t fid;
    qid_t qid = 0;
    term_t except = 0;
    predicate_t p;

    if ( !resetProlog(clear_stacks) )
      goto error;
    if ( !(fid = PL_open_foreign_frame()) )
      goto error;

    p = PL_pred(PL_new_functor(goal, 0), MODULE_system);

    if ( (qid = PL_open_query(MODULE_system, PL_Q_NORMAL, p, 0)) )
    { rc = PL_next_solution(qid);
    } else
    { error:
      except = exception_term;
      rc = FALSE;			/* Won't get any better */
      break;
    }

    if ( !rc && (except = PL_exception(qid)) )
    { atom_t a;

      tracemode(FALSE, NULL);
      debugmode(DBG_OFF, NULL);
      setPrologFlagMask(PLFLAG_LASTCALL);
      if ( PL_get_atom(except, &a) && a == ATOM_aborted )
      {
#ifdef O_DEBUGGER
        callEventHook(PLEV_ABORT);
#endif
        printMessage(ATOM_informational, PL_ATOM, ATOM_aborted);
      }
    }

    if ( qid ) PL_close_query(qid);
    if ( fid ) PL_discard_foreign_frame(fid);
    if ( !except )
      break;
  } while(loop);

  return rc;
}
Beispiel #5
0
static bool
clearSourceAdmin(SourceFile sf)
{ GET_LD
  int rc = FALSE;

  fid_t fid = PL_open_foreign_frame();
  term_t name = PL_new_term_ref();
  static predicate_t pred = NULL;

  if ( !pred )
    pred = PL_predicate("$clear_source_admin", 1, "system");

  PL_put_atom(name, sf->name);
  rc = PL_call_predicate(MODULE_system, PL_Q_NORMAL, pred, name);

  PL_discard_foreign_frame(fid);

  return rc;
}
Beispiel #6
0
int
query_loop(atom_t goal, int loop)
{ GET_LD
  int rc;
  int clear_stacks = (LD->query == NULL);

  do
  { fid_t fid;
    qid_t qid = 0;
    term_t except = 0;
    predicate_t p;

    if ( !resetProlog(clear_stacks) )
      goto error;
    if ( !(fid = PL_open_foreign_frame()) )
      goto error;

    p = PL_pred(PL_new_functor(goal, 0), MODULE_system);

    if ( (qid = PL_open_query(MODULE_system, PL_Q_NORMAL, p, 0)) )
    { rc = PL_next_solution(qid);
    } else
    { error:
      except = exception_term;
      rc = -1;				/* Won't get any better */
      break;
    }

    if ( !rc && (except = PL_exception(qid)) )
    { restore_after_exception(except);
      rc = -1;
    }

    if ( qid ) PL_close_query(qid);
    if ( fid ) PL_discard_foreign_frame(fid);
    if ( !except )
      break;
  } while(loop);

  return rc;
}
Beispiel #7
0
void initpyswipl() {
char *plargs[3];
term_t swipl_load;
fid_t swipl_fid;

	/**********************************************************/
	/* Initialize the prolog kernel.                          */
	/* The kernel is embedded (linked in) so I am setting the */
	/* the startup path to be the current directory. Also,    */
	/* I'm sending the -q flag to supress the startup banner. */
	/**********************************************************/
	plargs[0]="./";
	plargs[1]="-q";
	plargs[2]="-nosignals";
	PL_initialise(3,plargs);

	/**********************************************************/
	/* Load the pyrun predicate.                              */
	/* The pyrun.pl file has to be in the current working     */
	/* directory.                                             */
	/**********************************************************/
	swipl_fid=PL_open_foreign_frame();
	swipl_load=PL_new_term_ref();

	/**********************************************************/
	/* Changed by Nathan Denny July 18, 2001                  */
	/* No longer necessary to include pyrun.pl                */
	/**********************************************************/
	/*PL_chars_to_term("consult('pyrun.pl')", swipl_load);*/
	PL_chars_to_term("assert(pyrun(GoalString,BindingList):-(atom_codes(A,GoalString),atom_to_term(A,Goal,BindingList),call(Goal))).", swipl_load);

	PL_call(swipl_load,NULL);
	PL_discard_foreign_frame(swipl_fid);

	/**********************************************************/
	/* Call the Python module initializer.                    */
	/**********************************************************/
	(void) Py_InitModule("pyswipl",pyswiplMethods);
}
Beispiel #8
0
static PyObject* pyswipl_run(PyObject* self_Py, PyObject* args_Py) {
char* goalString;
char* answer;
int answerCount;

PyObject* answerList_Py;
PyObject* answerString_Py;
PyObject* bindingList_Py;
PyObject* binding_Py;

term_t swipl_args;
term_t swipl_goalCharList;
term_t swipl_bindingList;
term_t swipl_head;
term_t swipl_list;
predicate_t swipl_predicate;
qid_t swipl_qid;
fid_t swipl_fid;


	/**********************************************************/
	/* The queryString_C should be a python string represting */
	/* the query to be executed on the prolog system.         */
	/**********************************************************/
	if(!PyArg_ParseTuple(args_Py, "s", &goalString))
		return NULL;
	else {

		/**********************************************************/
		/* Create a Python list to hold the lists of bindings.    */
		/**********************************************************/
	  //if ( answerList_Py != NULL )
	  // Py_DECREF(answerList_Py);
	  answerList_Py=PyList_New(0);

		/**********************************************************/
		/* Open a foreign frame and initialize the term refs.     */
		/**********************************************************/
		swipl_fid=PL_open_foreign_frame();
		swipl_head=PL_new_term_ref();		/* Used in unpacking the binding List       */
		swipl_args=PL_new_term_refs(2);		/* The compound term for arguments to run/2 */
		swipl_goalCharList=swipl_args;		/* Alias for arg 1                          */
		swipl_bindingList=swipl_args+1;         /* Alias for arg 2                          */

		/**********************************************************/
		/* Pack the query string into the argument compund term.  */
		/**********************************************************/
		PL_put_list_chars(swipl_goalCharList,goalString);

		/**********************************************************/
		/* Generate a predicate to pyrun/2                        */
		/**********************************************************/
		swipl_predicate=PL_predicate("pyrun",2,NULL);

		/**********************************************************/
		/* Open the query, and iterate through the solutions.     */
		/**********************************************************/
		swipl_qid=PL_open_query(NULL,PL_Q_NORMAL,swipl_predicate, swipl_args);
		while(PL_next_solution(swipl_qid)) {

			/**********************************************************/
			/* Create a Python list to hold the bindings.             */
			/**********************************************************/
			bindingList_Py=PyList_New(0);

			/**********************************************************/
			/* Step through the bindings and add each to the list.    */
			/**********************************************************/
			swipl_list=PL_copy_term_ref(swipl_bindingList);
			while(PL_get_list(swipl_list, swipl_head, swipl_list)) {
				PL_get_chars(swipl_head, &answer, CVT_ALL|CVT_WRITE|BUF_RING);
				answerString_Py = PyString_FromString(answer);
				PyList_Append(bindingList_Py, answerString_Py);
				Py_DECREF(answerString_Py);
			}

			/**********************************************************/
			/* Add this binding list to the list of all solutions.    */
			/**********************************************************/
			PyList_Append(answerList_Py, bindingList_Py);
			Py_DECREF(bindingList_Py); 
		}

		/**********************************************************/
		/* Free this foreign frame...                             */
		/* Added by Nathan Denny, July 18, 2001.                  */
		/* Fixes a bug with running out of global stack when      */
		/* asserting _lots_ of facts.                             */
		/**********************************************************/
		PL_close_query(swipl_qid);
		PL_discard_foreign_frame(swipl_fid);
	
		/**********************************************************/
		/* Return the list of solutions.                          */
		/**********************************************************/
		return answerList_Py;	
	}
}
/*************************
 * libprolog_load_file
 *************************/
int
libprolog_load_file(char *path, int extension)
{
    char        *loader = extension ? "load_foreign_library" : "consult";
    predicate_t  pr_loader;
    fid_t        frame;
    qid_t        qid;
    term_t       pl_path;
    int          success;


    /*
     * load the given file (native prolog or foreign library)
     *
     * Notes: 
     *     The prolog predicate consult/1 does not seem to fail or raise an
     *     exception upon errors. It merely produces an error message and
     *     tries to continue or gives up processing the input file. In either
     *     case it succeeds (ie. the goal consult(path) is always proven in
     *     the prolog sense).
     *
     *     This default behaviour is not acceptable for us. As a library we
     *     want to let our caller know whether loading was successful or not.
     *     Otherwise it would be impossible to write even remotely reliable
     *     applications using this library.
     *
     *     To detect errors we have special prolog glue code that hooks into
     *     SWI Prologs user:message_hook and lets us know about errors
     *     (libprolog:mark_error) if loading is active (libprolog:loading).
     *     Currently the glue code prints an error message but it would be
     *     fairly easy to collect the errors here and let our caller print
     *     them if needed. For the time being this glue code lives in policy.pl
     *     but will eventually be separated out (to libprolog.pl ?).
     */


    libprolog_clear_errors();
    libprolog_load_start();
    
    frame = PL_open_foreign_frame();
    
    pr_loader = PL_predicate(loader, 1, NULL);
    pl_path   = PL_new_term_ref();
    PL_put_atom_chars(pl_path, path);
    
    qid     = PL_open_query(NULL, NORMAL_QUERY_FLAGS, pr_loader, pl_path);
    success = PL_next_solution(qid);
    if (PL_exception(qid)) {
#if 0
        char **exception = collect_exception(qid, &exception);
        libprolog_dump_exception(exception);
#endif
        success = FALSE;
    }
    PL_close_query(qid);

    PL_discard_foreign_frame(frame);
    
    libprolog_load_done();
    
    if (libprolog_has_errors())
        return FALSE;
    else
        return success;
}
Beispiel #10
0
void parse_seq_op_ (EtalisExecNode* operatorNode,term_t t)
{

    assert(operatorNode != NULL);

    fid_t fid = PL_open_foreign_frame();


    /*get components of the operator*/

    term_t _level_1 = PL_new_term_refs(2);
    term_t _left_event=_level_1;
    term_t _right_event=_level_1+1;
    atom_t _left_event_name,_right_event_name;

    int temp_arity,i=0;

    /* TODO check for embedded operators, if operator -> parse right function ; else get atomic events */
    PL_get_arg(1,t,_left_event);
    PL_get_name_arity(_left_event,&_left_event_name,&temp_arity);

    char * tt = PL_atom_chars(_left_event_name);
    i=0;
    while(strcmp(CEP_LUT_[i].CEP_name,PL_atom_chars(_left_event_name)) != 0 && i<LUT_Size) i++; /* #CONT */

     if(i != LUT_Size) /*The operator is found in the CEP_LUT_*/ /* create a temp event */
    {
        EtalisEventNode* TempEvent = (EtalisEventNode*)calloc(1,sizeof(EtalisEventNode)); /* this temp event is the complex event of the embedded operation */
        TempEvent->event.arity = 0;

        strcpy(TempEvent->event.name,"temp_"); /* TODO Temp Events */ /* Temp Events have no arguments */
        TempEvent->parentNode = operatorNode;
        strcpy(TempEvent->parentNode->name,tt);
        TempEvent->is_temp = ETALIS_TRUE;
        operatorNode->leftChild = TempEvent;

        EtalisExecNode* NewNodeRule = (EtalisExecNode*)calloc(1,sizeof(EtalisExecNode)); /* binarization and allocation of the embedded operator */
        TempEvent->childNode  = NewNodeRule;
        NewNodeRule->parentEvent = TempEvent;
        TempEvent->trigger = _seq_win_cep_l;
        switch (CEP_LUT_[i].CEP_arity)
        {
        case 1:
            break;
        case 2:
            /* found embedded */
            CEP_LUT_[i].parser_func(NewNodeRule,_left_event);
            break;
        default:
            printf("error compiling the rules\n");
        }
    }
    else /* an atomic event has been found */
    {
        operatorNode->leftChild = (EtalisEventNode*) calloc(1,sizeof(EtalisEventNode));
        PL_get_name_arity(_left_event,&_left_event_name,(int *)&((operatorNode->leftChild)->event.arity));
        strcpy(operatorNode->leftChild->event.name,PL_atom_chars(_left_event_name));
        operatorNode->leftChild->childNode = NULL;
        operatorNode->leftChild->parentNode=operatorNode;
        operatorNode->leftChild->is_temp = ETALIS_FALSE;

        if (operatorNode->parentEvent->is_temp)
                strcat(operatorNode->parentEvent->event.name,operatorNode->leftChild->event.name);

        operatorNode->leftChild->trigger = _seq_win_cep_l;

    }

    PL_get_arg(2,t,_right_event);

    operatorNode->rightChild = (EtalisEventNode*) calloc(1,sizeof(EtalisEventNode));
    PL_get_name_arity(_right_event,&_right_event_name,(int *)&(operatorNode->rightChild->event.arity));
    strcpy(operatorNode->rightChild->event.name,PL_atom_chars(_right_event_name));
    operatorNode->rightChild->childNode = NULL;
    operatorNode->rightChild->parentNode = operatorNode;
    operatorNode->rightChild->is_temp = ETALIS_FALSE;
    PL_discard_foreign_frame(fid);

    if (operatorNode->parentEvent->is_temp)
    {
            strcat(operatorNode->parentEvent->event.name,operatorNode->rightChild->event.name);
            operatorNode->rightChild->trigger = _seq_win_cep_l;
    }
    else /* event is latest event in the call list */
    {
        operatorNode->rightChild->trigger = _seq_batch_r;
    }




    ;
}
static HDDEDATA CALLBACK 
DdeCallback(UINT type, UINT fmt, HCONV hconv, HSZ hsz1, HSZ hsz2,
            HDDEDATA hData, DWORD dwData1, DWORD dwData2)
{
  switch(type)
  {  case XTYP_CONNECT:
     { fid_t cid = PL_open_foreign_frame();
       term_t argv = PL_new_term_refs(3);
       predicate_t pred = PL_pred(FUNCTOR_dde_connect3, MODULE_dde);
       int rval;

       PL_put_atom(   argv+0, hszToAtom(hsz2)); /* topic */
       PL_put_atom(   argv+1, hszToAtom(hsz1)); /* service */
       PL_put_integer(argv+2, dwData2 ? 1 : 0); /* same instance */
       rval = PL_call_predicate(MODULE_dde, TRUE, pred, argv);
       PL_discard_foreign_frame(cid);

       return (void *)rval;
     }
     case XTYP_CONNECT_CONFIRM:
     { fid_t cid = PL_open_foreign_frame();
       term_t argv = PL_new_term_refs(3);
       predicate_t pred = PL_pred(FUNCTOR_dde_connect_confirm3, MODULE_dde);
       int plhandle;

       if ( (plhandle = allocServerHandle(hconv)) >= 0 )
       { fid_t cid = PL_open_foreign_frame();
	 term_t argv = PL_new_term_refs(3);
	 predicate_t pred = PL_pred(FUNCTOR_dde_connect_confirm3, MODULE_dde);

	 PL_put_atom(   argv+0, hszToAtom(hsz2)); /* topic */
	 PL_put_atom(   argv+1, hszToAtom(hsz1)); /* service */
	 PL_put_integer(argv+2, plhandle);

	 PL_call_predicate(MODULE_dde, TRUE, pred, argv);
	 PL_discard_foreign_frame(cid);
       }

       return NULL;
     }
     case XTYP_DISCONNECT:
     { fid_t cid = PL_open_foreign_frame();
       term_t argv = PL_new_term_refs(1);
       predicate_t pred = PL_pred(FUNCTOR_dde_disconnect1, MODULE_dde);
       int plhandle = findServerHandle(hconv);
       
       if ( plhandle >= 0 && plhandle < MAX_CONVERSATIONS )
	 server_handle[plhandle] = (HCONV)NULL;

       PL_put_integer(argv+0, plhandle);
       PL_call_predicate(MODULE_dde, TRUE, pred, argv);
       PL_discard_foreign_frame(cid);

       return NULL;
     }
     case XTYP_EXECUTE:
     { int plhandle = findServerHandle(hconv);
       HDDEDATA rval = DDE_FNOTPROCESSED;
       fid_t cid = PL_open_foreign_frame();
       term_t argv = PL_new_term_refs(3);
       predicate_t pred = PL_pred(FUNCTOR_dde_execute3, MODULE_dde);

       DEBUG(0, Sdprintf("Got XTYP_EXECUTE request\n"));

       PL_put_integer(argv+0, plhandle);
       PL_put_atom(   argv+1, hszToAtom(hsz1));
       unify_hdata(   argv+2, hData);
       if ( PL_call_predicate(MODULE_dde, TRUE, pred, argv) )
	 rval = (void *) DDE_FACK;
       PL_discard_foreign_frame(cid);
       DdeFreeDataHandle(hData);
       return rval;
     }
     case XTYP_REQUEST:
     { HDDEDATA data = (HDDEDATA) NULL;

       if ( fmt == CF_TEXT )
       { fid_t cid = PL_open_foreign_frame();
	 term_t argv = PL_new_term_refs(4);
	 predicate_t pred = PL_pred(FUNCTOR_dde_request4, MODULE_dde);
	 int plhandle = findServerHandle(hconv);

	 PL_put_integer( argv+0, plhandle);
	 PL_put_atom(	 argv+1, hszToAtom(hsz1)); /* topic */
	 PL_put_atom(    argv+2, hszToAtom(hsz2)); /* item */
	 PL_put_variable(argv+3);

	 if ( PL_call_predicate(MODULE_dde, TRUE, pred, argv) )
	 { char *s;

	   if ( PL_get_chars(argv+3, &s, CVT_ALL) )
	     data = DdeCreateDataHandle(ddeInst, s, strlen(s)+1,
					0, hsz2, CF_TEXT, 0);
	 }
	 PL_discard_foreign_frame(cid);
       }

       return data;
     }
     default:
       ;
  }

  return (HDDEDATA)NULL;
}
Beispiel #12
0
static HDDEDATA CALLBACK
DdeCallback(UINT type, UINT fmt, HCONV hconv, HSZ hsz1, HSZ hsz2,
            HDDEDATA hData, DWORD dwData1, DWORD dwData2)
{ GET_LD
  DWORD ddeInst = LD->os.dde_instance;

  switch(type)
  {  case XTYP_CONNECT:
     { fid_t cid = PL_open_foreign_frame();
       term_t argv = PL_new_term_refs(3);
       predicate_t pred = PL_pred(FUNCTOR_dde_connect3, MODULE_dde);
       int rval;

       if ( unify_hsz(ddeInst, argv+0, hsz2) &&		/* topic */
	    unify_hsz(ddeInst, argv+1, hsz1) &&		/* service */
	    PL_unify_integer(argv+2, dwData2 ? 1 : 0) )	/* same instance */
       { rval = PL_call_predicate(MODULE_dde, TRUE, pred, argv);
       } else
       { rval = FALSE;
       }
       PL_discard_foreign_frame(cid);

       return (void *)(intptr_t)rval;
     }
     case XTYP_CONNECT_CONFIRM:
     { int plhandle;

       if ( (plhandle = allocServerHandle(hconv)) >= 0 )
       { fid_t cid = PL_open_foreign_frame();
	 term_t argv = PL_new_term_refs(3);
	 predicate_t pred = PL_pred(FUNCTOR_dde_connect_confirm3, MODULE_dde);

	 if ( unify_hsz(ddeInst, argv+0, hsz2) &&		/* topic */
	      unify_hsz(ddeInst, argv+1, hsz1) &&		/* service */
	      PL_unify_integer(argv+2, plhandle) )
	   PL_call_predicate(MODULE_dde, TRUE, pred, argv);

	 PL_discard_foreign_frame(cid);
       }

       return NULL;
     }
     case XTYP_DISCONNECT:
     { fid_t cid = PL_open_foreign_frame();
       term_t argv = PL_new_term_refs(1);
       predicate_t pred = PL_pred(FUNCTOR_dde_disconnect1, MODULE_dde);
       int plhandle = findServerHandle(hconv);

       if ( plhandle >= 0 && plhandle < MAX_CONVERSATIONS )
	 server_handle[plhandle] = (HCONV)NULL;

       PL_put_integer(argv+0, plhandle);
       PL_call_predicate(MODULE_dde, TRUE, pred, argv);
       PL_discard_foreign_frame(cid);

       return NULL;
     }
     case XTYP_EXECUTE:
     { int plhandle = findServerHandle(hconv);
       HDDEDATA rval = DDE_FNOTPROCESSED;
       fid_t cid = PL_open_foreign_frame();
       term_t argv = PL_new_term_refs(3);
       predicate_t pred = PL_pred(FUNCTOR_dde_execute3, MODULE_dde);

       DEBUG(1, Sdprintf("Got XTYP_EXECUTE request\n"));

       PL_put_integer(argv+0, plhandle);
       unify_hsz(ddeInst, argv+1, hsz1);
       unify_hdata(   argv+2, hData);
       if ( PL_call_predicate(MODULE_dde, TRUE, pred, argv) )
	 rval = (void *) DDE_FACK;
       PL_discard_foreign_frame(cid);
       DdeFreeDataHandle(hData);
       return rval;
     }
     case XTYP_REQUEST:
     { HDDEDATA data = (HDDEDATA) NULL;

       if ( fmt == CF_UNICODETEXT )
       { fid_t cid = PL_open_foreign_frame();
	 term_t argv = PL_new_term_refs(4);
	 predicate_t pred = PL_pred(FUNCTOR_dde_request4, MODULE_dde);
	 int plhandle = findServerHandle(hconv);

	 PL_put_integer( argv+0, plhandle);
	 unify_hsz(ddeInst, argv+1, hsz1);	/* topic */
	 unify_hsz(ddeInst, argv+2, hsz2);	/* item */

	 if ( PL_call_predicate(MODULE_dde, TRUE, pred, argv) )
	 { wchar_t *s;
	   size_t len;

					/* TBD: error handling */
	   if ( PL_get_wchars(argv+3, &len, &s, CVT_ALL) )
	     data = DdeCreateDataHandle(ddeInst,
					(unsigned char*) s,
					(DWORD)(len+1)*sizeof(wchar_t),
					0, hsz2, CF_UNICODETEXT, 0);
	 }
	 PL_discard_foreign_frame(cid);
       }

       return data;
     }
     default:
       ;
  }

  return (HDDEDATA)NULL;
}
Beispiel #13
0
static foreign_t
current_alarms(term_t time, term_t goal, term_t id, term_t status,
	       term_t matching)
{ Event ev;
  term_t next = PL_new_term_ref();
  term_t g    = PL_new_term_ref();
  term_t tail = PL_copy_term_ref(matching);
  term_t head = PL_new_term_ref();
  term_t av   = PL_new_term_refs(4);
  pthread_t self = pthread_self();

  LOCK();
  ev = TheSchedule()->first;

  for(; ev; ev = ev->next)
  { atom_t s;
    double at;
    fid_t fid;

    if ( !pthread_equal(self, ev->thread_id) )
      continue;

    fid = PL_open_foreign_frame();

    if ( ev->flags & EV_DONE )
      s = ATOM_done;
    else if ( ev == TheSchedule()->scheduled )
      s = ATOM_next;
    else
      s = ATOM_scheduled;

    if ( !PL_unify_atom(status, s) )
      goto nomatch;

    PL_recorded(ev->goal, g);
    if ( !PL_unify_term(goal,
			PL_FUNCTOR, FUNCTOR_module2,
			  PL_ATOM, PL_module_name(ev->module),
			  PL_TERM, g) )
      goto nomatch;

    at = (double)ev->at.tv_sec + (double)ev->at.tv_usec / 1000000.0;
    if ( !PL_unify_float(time, at) )
      goto nomatch;

    if ( !unify_timer(id, ev) )
      goto nomatch;

    PL_discard_foreign_frame(fid);

    if ( !PL_put_float(av+0, at) ||		/* time */
	 !PL_recorded(ev->goal, av+1) ||	/* goal */
	 !PL_put_variable(av+2) ||		/* id */
	 !unify_timer(av+2, ev) ||
	 !PL_put_atom(av+3, s) ||		/* status */
	 !PL_cons_functor_v(next, FUNCTOR_alarm4, av) )
    { PL_close_foreign_frame(fid);
      UNLOCK();
      return FALSE;
    }

    if ( PL_unify_list(tail, head, tail) &&
	 PL_unify(head, next) )
    { continue;
    } else
    { PL_close_foreign_frame(fid);
      UNLOCK();

      return FALSE;
    }

  nomatch:
    PL_discard_foreign_frame(fid);
  }
  UNLOCK();

  return PL_unify_nil(tail);
}