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); }
static int unify_number(term_t t, const char *s, size_t len) { char buf[100]; char *a, *o; const char *i; int rc; if ( len+1 > sizeof(buf) ) { if ( !(a = malloc(len+1)) ) return PL_resource_error("memory"); } else { a = buf; } for(i=s,o=a; len-- > 0; ) *o++ = (char)*i++; *o = '\0'; rc = PL_chars_to_term(a, t); if ( a != buf ) free(a); return rc; }
static foreign_t pl_uuid(term_t UUID, term_t options) { unsigned int mode = UUID_MAKE_V1; atom_t format = ATOM_atom; uuid_t *uuid; char *ns = NULL; char *str = NULL; int rc; uuid_rc_t urc; if ( !PL_get_nil(options) ) { term_t tail = PL_copy_term_ref(options); term_t head = PL_new_term_ref(); term_t arg = PL_new_term_ref(); while( PL_get_list(tail, head, tail) ) { atom_t name; size_t arity; if ( !PL_get_name_arity(head, &name, &arity) || arity != 1 ) return PL_type_error("option", head); _PL_get_arg(1, head, arg); if ( name == ATOM_version ) { int v; if ( !PL_get_integer_ex(arg, &v) ) return FALSE; switch(v) { case 1: mode = UUID_MAKE_V1; break; case 2: mode = UUID_MAKE_MC; break; case 3: mode = UUID_MAKE_V3; break; case 4: mode = UUID_MAKE_V4; break; case 5: mode = UUID_MAKE_V5; break; default: return PL_domain_error("uuid_version", arg); } } else if ( name == ATOM_format ) { if ( !PL_get_atom_ex(arg, &format) ) return FALSE; if ( format != ATOM_atom && format != ATOM_integer ) return PL_domain_error("uuid_format", arg); } else { char *newns = NULL; if ( name == ATOM_dns ) { newns = "ns:DNS"; } else if ( name == ATOM_url ) { newns = "ns:URL"; } else if ( name == ATOM_oid ) { newns = "ns:OID"; } else if ( name == ATOM_x500 ) { newns = "ns:X500"; } if ( newns ) { ns = newns; if ( !PL_get_chars(arg, &str, CVT_ATOM|CVT_EXCEPTION) ) return FALSE; if ( mode == UUID_MAKE_V1 ) mode = UUID_MAKE_V3; } } } if ( !PL_get_nil_ex(tail) ) return FALSE; } switch(mode) { case UUID_MAKE_V1: case UUID_MAKE_MC: case UUID_MAKE_V4: uuid_create(&uuid); if ( (urc=uuid_make(uuid, mode)) != UUID_RC_OK ) return PL_warning("UUID: make: %s\n", uuid_error(urc)); break; case UUID_MAKE_V3: case UUID_MAKE_V5: { uuid_t *uuid_ns; if ( !ns ) return PL_existence_error("uuid_context", options); uuid_create(&uuid); uuid_create(&uuid_ns); uuid_load(uuid_ns, ns); if ( (urc=uuid_make(uuid, mode, uuid_ns, str)) != UUID_RC_OK ) return PL_warning("UUID: make: %s\n", uuid_error(urc)); uuid_destroy(uuid_ns); break; } default: assert(0); return FALSE; } if ( format == ATOM_atom ) { char buf[UUID_LEN_STR+1]; void *ptr = buf; size_t datalen = sizeof(buf); if ( (urc=uuid_export(uuid, UUID_FMT_STR, &ptr, &datalen)) != UUID_RC_OK ) return PL_warning("UUID: export: %s\n", uuid_error(urc)); rc = PL_unify_chars(UUID, PL_ATOM|REP_ISO_LATIN_1, (size_t)-1, buf); } else if ( format == ATOM_integer ) { char buf[UUID_LEN_SIV+1]; void *ptr = buf; size_t datalen = sizeof(buf); term_t tmp = PL_new_term_ref(); if ( (urc=uuid_export(uuid, UUID_FMT_SIV, &ptr, &datalen)) != UUID_RC_OK ) return PL_warning("UUID: export: %s\n", uuid_error(urc)); rc = ( PL_chars_to_term(buf, tmp) && PL_unify(UUID, tmp) ); } else { assert(0); return FALSE; } uuid_destroy(uuid); return rc; }