Ejemplo n.º 1
0
static foreign_t
pl_hmac_sha(term_t key, term_t data, term_t mac, term_t options)
{ char *sdata, *skey;
  size_t datalen, keylen;
  optval opts;
  unsigned char digest[SHA2_MAX_DIGEST_SIZE];

  if ( !PL_get_nchars(key, &keylen, &skey,
		      CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION) )
    return FALSE;
  if ( !PL_get_nchars(data, &datalen, &sdata,
		      CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION) )
    return FALSE;

  if ( !sha_options(options, &opts) )
    return FALSE;

  switch(opts.algorithm)
  { case ALGORITHM_SHA1:
      hmac_sha1((unsigned char*)skey, (unsigned long)keylen,
		(unsigned char*)sdata, (unsigned long)datalen,
		digest, (unsigned long)opts.digest_size);
      break;
    case ALGORITHM_SHA256:
      hmac_sha256((unsigned char*)skey, (unsigned long)keylen,
		  (unsigned char*)sdata, (unsigned long)datalen,
		  digest, (unsigned long)opts.digest_size);
      break;
    default:
      return pl_error(NULL, 0, "HMAC-SHA only for SHA-1 and SHA-256",
		      ERR_DOMAIN, opts.algorithm_term, "algorithm");
  }

  return PL_unify_list_ncodes(mac, opts.digest_size, (char*)digest);
}
Ejemplo n.º 2
0
static foreign_t python_f(term_t tmod, term_t fname, term_t tf) {
  char *s;
  size_t len;
  PyObject *pF, *pModule;
  
  /* if an atom, fetch again */
  if (PL_is_atom(tmod)) {
    PyObject *pName;

    if (!PL_get_nchars(fname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
      return FALSE;
    }
#if PY_MAJOR_VERSION < 3
    pName = PyString_FromString(s);
#else
    pName = PyUnicode_FromString(s);
#endif
    if (pName == NULL) {
      {
        return false;
      }
    }
    pModule = PyImport_Import(pName);
    PyErr_Clear();
  } else if (!(pModule = term_to_python(tmod, true))) {
    PyErr_Clear();
    {
      return false;
    }
  }
  if (!PL_get_nchars(fname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
    {
      return false;
    }
  }
  pF = PyObject_GetAttrString(pModule, s);
  PyErr_Print();
  Py_DECREF(pModule);
  if (pF == NULL || !PyCallable_Check(pF)) {
    {
      return false;
    }
  }
  {
    foreign_t rc = python_to_ptr(pF, tf);
    return rc;
  }
}
Ejemplo n.º 3
0
static foreign_t
turtle_name(term_t name)
{ char *s;
  pl_wchar_t *w;
  size_t len;

  if ( PL_get_nchars(name, &len, &s, CVT_ATOM) )
  { const char *e = &s[len];

    if ( !wcis_name_start_char(s[0]&0xff) )
      return FALSE;
    for(s++; s<e; s++)
    { if ( !wcis_name_char(s[0]&0xff) )
	return FALSE;
    }
    return TRUE;
  } else if ( PL_get_wchars(name, &len, &w, CVT_ATOM|CVT_EXCEPTION) )
  { const pl_wchar_t *e = &w[len];

    if ( !wcis_name_start_char(w[0]) )
      return FALSE;
    for(w++; w<e; w++)
    { if ( !wcis_name_char(w[0]) )
	return FALSE;
    }
    return TRUE;
  } else
    return FALSE;
}
Ejemplo n.º 4
0
static foreign_t
turtle_write_uri(term_t Stream, term_t Value)
{ size_t len;
  char *s;
  pl_wchar_t *w;
  IOSTREAM *out;

  if ( !PL_get_stream_handle(Stream, &out) )
    return FALSE;

  if ( PL_get_nchars(Value, &len, &s, CVT_ATOM|CVT_STRING) )
  { const char *e = &s[len];

    Sputcode('<', out);
    for(; s<e; s++)
    { if ( ttl_put_ucharacter(out, s[0]&0xff) < 0 )
	break;
    }
    Sputcode('>', out);
    return PL_release_stream(out);
  } else if ( PL_get_wchars(Value, &len, &w, CVT_ATOM|CVT_EXCEPTION) )
  { const pl_wchar_t *e = &w[len];

    Sputcode('<', out);
    for(; w<e; w++)
    { if ( ttl_put_ucharacter(out, w[0]) < 0 )
	break;
    }
    Sputcode('>', out);
    return PL_release_stream(out);
  } else
  { PL_release_stream(out);
    return FALSE;
  }
}
Ejemplo n.º 5
0
static foreign_t python_run_file(term_t file) {
  char *s;
  size_t len;
  char si[256];
  s = si;
  if (PL_get_nchars(file, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
#if PY_MAJOR_VERSION < 3
    PyObject *PyFileObject = PyFile_FromString(si, "r");
    PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "test.py", 1);
#else
    FILE *f = fopen(s, "r");
    if (f == NULL) {
      return false;
    }
    PyRun_SimpleFileEx(f, s, 1);
#endif
    {
      {
        return true;
      }
    }
  }
  {
    return false;
  }
}
Ejemplo n.º 6
0
static foreign_t
udp_send(term_t Socket, term_t Data, term_t To, term_t Options)
{ struct sockaddr_in sockaddr;
#ifdef __WINDOWS__
  int alen = sizeof(sockaddr);
#else
  int alen = sizeof(sockaddr);
#endif
  int socket;
  int flags = 0L;
  char *data;
  size_t dlen;
  ssize_t n;

  if ( !PL_get_nchars(Data, &dlen, &data, CVT_ALL|CVT_EXCEPTION) )
    return FALSE;

  if ( !tcp_get_socket(Socket, &socket) ||
       !nbio_get_sockaddr(To, &sockaddr) )
    return FALSE;

  if ( (n=nbio_sendto(socket, data,
		      (int)dlen,
		      flags,
		      (struct sockaddr*)&sockaddr, alen)) == -1 )
    return nbio_error(errno, TCP_ERRNO);

  return TRUE;
}
Ejemplo n.º 7
0
static foreign_t
pl_tipc_subscribe(term_t Socket, term_t Address,
		  term_t timeout, term_t filter, term_t usr_handle)
{ struct sockaddr_tipc sockaddr;
  struct tipc_subscr subscr;
  int socket;
  unsigned time, filt;
  char *handle;
  size_t handle_len;
  SOCKET fd;

  memset(&subscr, 0, sizeof(subscr));
  memset(&sockaddr, 0, sizeof(sockaddr));

  if ( !tipc_get_socket(Socket, &socket) ||
       !nbio_get_tipc_sockaddr(Address, &sockaddr))
    return FALSE;

  if(sockaddr.addrtype != TIPC_ADDR_NAMESEQ)
    return pl_error(NULL, 0, NULL, ERR_DOMAIN, Address, "name_seq/3");

  if( !get_uint(timeout, &time))
    return pl_error(NULL, 0, NULL, ERR_DOMAIN, timeout, "integer");

  if( !get_uint(filter, &filt))
    return pl_error(NULL, 0, NULL, ERR_DOMAIN, filter, "integer");

  if ( !PL_get_nchars(usr_handle, &handle_len, &handle, CVT_ALL|CVT_EXCEPTION) )
    return FALSE;

  if(tipc_version > 1)
  { struct tipc_name_seq *p = &subscr.seq,
                         *p1 = &sockaddr.addr.nameseq;

    p->type = htonl(p1->type);
    p->lower = htonl(p1->lower);
    p->upper = htonl(p1->upper);

    subscr.timeout = htonl(time);
    subscr.filter = htonl((filt == V1_TIPC_SUB_SERVICE) 
                          ? TIPC_SUB_SERVICE 
                          : filt);
  } else {
    memcpy(&subscr.seq, &sockaddr.addr.nameseq, sizeof(subscr.seq));
    subscr.timeout = time;
    subscr.filter = filt;
  }
  memcpy(&subscr.usr_handle, handle, 
         (handle_len < sizeof(subscr.usr_handle)) 
          ? handle_len
          : sizeof(subscr.usr_handle));

  fd = nbio_fd(socket);

  if ( (send(fd, &subscr, sizeof(subscr), 0)) != sizeof(subscr) )
    return nbio_error(errno, TCP_ERRNO);
  else
    return TRUE;
}
Ejemplo n.º 8
0
static int python_import(term_t mname, term_t mod) {
  PyObject *pName, *pModule;
  term_t arg = PL_new_term_ref();
  char s0[MAXPATHLEN], *s = s0;

  while (true) {
    size_t len;

    len = (MAXPATHLEN - 1) - (s - s0);
    if (PL_is_pair(mname)) {
      char *sa;
      if (!PL_get_arg(1, mname, arg) || !PL_get_atom_chars(arg, &sa) ||
          !PL_get_arg(2, mname, mname)) {
        return false;
      }
      s = stpcpy(s, sa);
      *s++ = '.';
      s[0] = '\0';
    } else if (!PL_get_nchars(mname, &len, &s,
                              CVT_ALL | CVT_EXCEPTION | REP_UTF8)) {
      {
        return false;
      }
    } else {
      break;
    }
  }
#if PY_MAJOR_VERSION < 3
  pName = PyString_FromString(s0);
#else
  pName = PyUnicode_FromString(s0);
#endif
  if (pName == NULL) {
    {
      return false;
    }
  }
  pModule = PyImport_Import(pName);
  PyErr_Clear();
  Py_DECREF(pName);
  if (pModule == NULL) {
#if EXTRA_MESSSAGES
    if (PyErr_Occurred())
      PyErr_Print();
    PyErr_Clear();
#endif
    {
      return false;
    }
  }
  ActiveModules[active_modules++] = pModule;
  {    foreign_t rc = python_to_ptr(pModule, mod);
    return rc;
  }
}
Ejemplo n.º 9
0
static foreign_t
pl_prefix_string(term_t ord, term_t pre, term_t t2)
{ OrdTable ot;
  char *s1, *s2;
  size_t l1, l2;
  unsigned int flags = (CVT_ATOM|CVT_STRING|CVT_LIST|BUF_RING|CVT_EXCEPTION);

  if ( !get_order_table(ord, &ot) )
    return error(ERR_INSTANTIATION, "prefix_string/3", 1, ord);
  if ( !PL_get_nchars(pre, &l1, &s1, flags) )
    return FALSE;
  if ( !PL_get_nchars(t2, &l2, &s2, flags) )
    return FALSE;

  if ( l1 <= l2 &&
       compare_strings(s1, s2, l1, ot) == 0 )
    return TRUE;

  return FALSE;
}
Ejemplo n.º 10
0
static int get_null_terminated_string(term_t t, char **s, int flags) {
    size_t len;

    if (PL_get_nchars(t, &len, s, flags | REP_UTF8 | CVT_EXCEPTION)) {
        if (len == strlen(*s)) {
            return TRUE;
        }
        return PL_domain_error("null_terminated_string", t);
    }

    return FALSE;
}
Ejemplo n.º 11
0
static foreign_t python_run_command(term_t cmd) {
  char *s;
  bool rc = false;
  size_t len;
  char si[256];

  s = si;
  if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
    PyRun_SimpleString(s);
    rc = true;
  }
   return rc;
}
Ejemplo n.º 12
0
static foreign_t
pl_compare_strings(term_t ord, term_t t1, term_t t2, term_t result)
{ OrdTable ot;
  char *s1, *s2;
  size_t len;
  int rval;
  unsigned int flags = (CVT_ATOM|CVT_STRING|CVT_LIST|BUF_RING|CVT_EXCEPTION);

  if ( !get_order_table(ord, &ot) )
    return error(ERR_INSTANTIATION, "compare_strings/4", 1, ord);
  if ( !PL_get_nchars(t1, &len, &s1, flags) )
    return FALSE;
  if ( !PL_get_nchars(t2, &len, &s2, flags) )
    return FALSE;

  rval = compare_strings(s1, s2, len, ot);

  return PL_unify_atom(result,
		       rval == 0 ? ATOM_eq :
		       rval <  0 ? ATOM_lt :
		       	           ATOM_gt);
}
Ejemplo n.º 13
0
static foreign_t
snowball(term_t lang, term_t in, term_t out)
{ struct sb_stemmer *stemmer = NULL;
  char *s;
  size_t len, olen;
  const sb_symbol *stemmed;

  if ( !get_lang_stemmer(lang, &stemmer) )
    return FALSE;
  if ( !PL_get_nchars(in, &len, &s,
		      CVT_ATOM|CVT_STRING|CVT_LIST|REP_UTF8|CVT_EXCEPTION) )
    return FALSE;

  if ( !(stemmed = sb_stemmer_stem(stemmer, (const sb_symbol*)s, (int)len)) )
    return resource_error("memory");
  olen = sb_stemmer_length(stemmer);

  return PL_unify_chars(out, PL_ATOM|REP_UTF8, olen, (const char*)stemmed);
}
Ejemplo n.º 14
0
static foreign_t
pl_no_diacritics_atom(term_t in, term_t out)
{ char *s, *to;
  size_t len;
  wchar_t *ws, *wto;
  
  if (PL_get_nchars(in, &len, &s, CVT_ATOMIC))
  { if ((to = alloca(len+1)))
    { no_diacritics(s, to);
      return PL_unify_chars(out, PL_ATOM, len, to);
    }
  } else
  if (PL_get_wchars(in, &len, &ws, CVT_ATOMIC))
  { if ((wto = alloca((len+1)*sizeof(wchar_t))))
    { wno_diacritics(ws, len, wto);
      return PL_unify_wchars(out, PL_ATOM, len, wto);
    }
  }

  return FALSE;
}
Ejemplo n.º 15
0
static foreign_t
md5_hash(term_t from, term_t md5, term_t options)
{ char *data;
  size_t datalen;
  optval opts;
  md5_byte_t digest[16];
  md5_state_t state;

  if ( !md5_options(options, &opts) )
    return FALSE;

  if ( !PL_get_nchars(from, &datalen, &data,
		      CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION|opts.encoding) )
    return FALSE;

  md5_init(&state);
  md5_append(&state, (const md5_byte_t *)data, datalen);
  md5_finish(&state, digest);

  return md5_unify_digest(md5, digest);
}
Ejemplo n.º 16
0
static foreign_t
pl_sha_hash_ctx(term_t old_ctx, term_t from, term_t new_ctx, term_t hash)
{ char *data;
  size_t datalen;
  struct context *cp;
  size_t clen;
  unsigned char hval[SHA2_MAX_DIGEST_SIZE];

  if ( !PL_get_nchars(from, &datalen, &data,
		      CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION) )
    return FALSE;

  if ( !PL_get_string_chars(old_ctx, (char **)&cp, &clen) )
    return FALSE;

  if ( clen != sizeof (*cp)
       || cp->magic != CONTEXT_MAGIC ) {
    return pl_error(NULL, 0, "Invalid OldContext passed",
		    ERR_DOMAIN, old_ctx, "algorithm");
  }

  if ( cp->opts.algorithm == ALGORITHM_SHA1 )
  { sha1_ctx *c1p = &(cp->context.sha1);
    sha1_hash((unsigned char*)data, (unsigned long)datalen, c1p);
    if ( !PL_unify_string_nchars(new_ctx, sizeof(*cp), (char*)cp) )
      return FALSE;
    sha1_end((unsigned char *)hval, c1p);
  } else
  { sha2_ctx *c1p = &(cp->context.sha2);
    sha2_hash((unsigned char*)data, (unsigned long)datalen, c1p);
    if ( !PL_unify_string_nchars(new_ctx, sizeof(*cp), (char*)cp) )
      return FALSE;
    sha2_end((unsigned char *)hval, c1p);
  }

  /* . */
  return PL_unify_list_ncodes(hash, cp->opts.digest_size, (char*)hval);
}
Ejemplo n.º 17
0
static int
get_echars_arg_ex(int i, term_t from, term_t arg, echar **sp, size_t *lenp)
{ const echar *s, *e;

  if ( !PL_get_arg(i, from, arg) )
    return FALSE;

#ifdef __WINDOWS__
  if ( !PL_get_wchars(arg, lenp, sp,
		      CVT_ATOMIC|CVT_EXCEPTION) )
#else
  if ( !PL_get_nchars(arg, lenp, sp,
		      CVT_ATOMIC|CVT_EXCEPTION|REP_FN) )
#endif
    return FALSE;

  for(s = *sp, e = s+*lenp; s<e; s++)
  { if ( !*s )
      return domain_error(arg, "text_non_zero_code");
  }

  return TRUE;
}
Ejemplo n.º 18
0
static foreign_t python_o(term_t tmod, term_t fname, term_t tf) {
  char *s;
  size_t len;
  PyObject *pO, *pModule;
  
  pModule = term_to_python(tmod, true);
  if (!PL_get_nchars(fname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
    {
      return false;
    }
  }
  pO = PyObject_GetAttrString(pModule, s);
  if (pO == NULL) {
    {
      return false;
    }
  }
  {
    foreign_t rc = python_to_ptr(pO, tf);
    ;
    return rc;
  }
}
Ejemplo n.º 19
0
static foreign_t
pl_sha_hash(term_t from, term_t hash, term_t options)
{ char *data;
  size_t datalen;
  optval opts;
  unsigned char hval[SHA2_MAX_DIGEST_SIZE];

  if ( !sha_options(options, &opts) )
    return FALSE;

  if ( !PL_get_nchars(from, &datalen, &data,
		      CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION) )
    return FALSE;

  if ( opts.algorithm == ALGORITHM_SHA1 )
  { sha1((unsigned char*)hval,
	 (unsigned char*)data, (unsigned long)datalen);
  } else
  { sha2((unsigned char*)hval, (unsigned long) opts.digest_size,
	 (unsigned char*)data, (unsigned long)datalen);
  }

  return PL_unify_list_ncodes(hash, opts.digest_size, (char*)hval);
}
Ejemplo n.º 20
0
static foreign_t
do_quote(term_t in, term_t quoted, char **map, int maxchr)
{ char *inA = NULL;
  wchar_t *inW = NULL;
  size_t len;
  const unsigned  char *s;
  charbuf buffer;
  int changes = 0;
  int rc;

  if ( !PL_get_nchars(in, &len, &inA, CVT_ATOMIC) &&
       !PL_get_wchars(in, &len, &inW, CVT_ATOMIC) )
    return sgml2pl_error(ERR_TYPE, "atom", in);
  if ( len == 0 )
    return PL_unify(in, quoted);

  init_buf(&buffer);

  if ( inA )
  { for(s = (unsigned char*)inA ; len-- > 0; s++ )
    { int c = *s;

      if ( map[c] )
      { if ( !add_str_buf(&buffer, map[c]) )
	  return FALSE;

	changes++;
      } else if ( c > maxchr )
      { char buf[10];

	sprintf(buf, "&#%d;", c);
	if ( !add_str_buf(&buffer, buf) )
	  return FALSE;

	changes++;
      } else
      { add_char_buf(&buffer, c);
      }
    }

    if ( changes > 0 )
      rc = PL_unify_atom_nchars(quoted, used_buf(&buffer), buffer.bufp);
    else
      rc = PL_unify(in, quoted);
  } else
  { for( ; len-- > 0; inW++ )
    { int c = *inW;

      if ( c <= 0xff && map[c] )
      { if ( !add_str_bufW(&buffer, map[c]) )
	  return FALSE;

	changes++;
      } else if ( c > maxchr )
      { char buf[10];

	sprintf(buf, "&#%d;", c);
	if ( !add_str_bufW(&buffer, buf) )
	  return FALSE;

	changes++;
      }else
      { add_char_bufW(&buffer, c);
      }
    }

    if ( changes > 0 )
      rc = PL_unify_wchars(quoted, PL_ATOM,
			   used_buf(&buffer)/sizeof(wchar_t),
			   (wchar_t*)buffer.bufp);
    else
      rc = PL_unify(in, quoted);
  }

  free_buf(&buffer);

  return rc;
}
Ejemplo n.º 21
0
int
PL_get_chars_ex(term_t t, char **s, unsigned int flags)
{ return PL_get_nchars(t, NULL, s, flags|CVT_EXCEPTION);
}
Ejemplo n.º 22
0
int
PL_get_nchars_ex(term_t t, size_t *len, char **s, unsigned int flags)
{ return PL_get_nchars(t, len, s, flags|CVT_EXCEPTION);
}
Ejemplo n.º 23
0
static foreign_t python_run_script(term_t cmd, term_t fun) {
  char si[256], sf[256];
  size_t len = 255, len1 = 255;
  PyObject *pName, *pModule, *pFunc;
  PyObject *pArgs = NULL, *pValue;
  char *s;

  s = si;
  if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION) &&
      (s = sf) != NULL &&
      PL_get_nchars(fun, &len1, &s, CVT_ALL | CVT_EXCEPTION)) {

#if PY_MAJOR_VERSION < 3
    pName = PyString_FromString("rbm");
#else
    // asssumes UTF-8
    pName = PyUnicode_FromString("rbm");
#endif
    /* Error checking of pName left out */

    pModule = PyImport_Import(pName);
    PyErr_Clear();
    Py_DECREF(pName);

    if (pModule != NULL) {
      pFunc = PyObject_GetAttrString(pModule, sf);
      /* pFunc is a new reference */

      if (pFunc && PyCallable_Check(pFunc)) {
        pValue = PyObject_CallObject(pFunc, pArgs);
        if (pValue != NULL) {
          Py_DECREF(pValue);
        } else {
          Py_DECREF(pFunc);
          Py_DECREF(pModule);
          PyErr_Print();
          fprintf(stderr, "Call failed\n");
          {
            return false;
          }
        }
      } else {
        if (PyErr_Occurred())
          PyErr_Print();
        fprintf(stderr, "Cannot find function \"%s\"\n", sf);
      }
      Py_XDECREF(pFunc);
      Py_DECREF(pModule);
    } else {
      PyErr_Print();
      {
        return false;
      }
    }
    {
      return true;
    }
  }
  {
    return false;
  }
}
Ejemplo n.º 24
0
static bool get_value(term_t t, clingo_symbol_t *val, int minus) {
    switch (PL_term_type(t)) {
    case PL_INTEGER: {
        int i;

        if (PL_get_integer(t, &i)) {
            clingo_symbol_create_number(i, val);
            return true;
        }
        return false;
    }
    case PL_ATOM: {
        char *s;
        size_t len;

        if (PL_get_nchars(t, &len, &s, CVT_ATOM | REP_UTF8 | CVT_EXCEPTION)) {
            return clingo_symbol_create_id(s, !minus, val); /* no sign */
        }
        return false;
    }
    case PL_STRING: {
        char *s;
        size_t len;

        if (PL_get_nchars(t, &len, &s, CVT_STRING | REP_UTF8 | CVT_EXCEPTION)) {
            return clingo_symbol_create_string(s, val);
        }
        return false;
    }
    case PL_TERM: {
        bool rc;
        term_t arg;
        atom_t name;
        size_t arity; /* TBD: -atom, #const */
        clingo_symbol_t *values = NULL;

        if (!(rc = get_name_arity(t, &name, &arity))) {
            clingo_set_error(clingo_error_runtime, "prolog error");
            goto out_term;
        }
        arg = PL_new_term_ref();

        if (name == ATOM_minus && arity == 1) {
            if (!(rc = get_value(arg, val, TRUE))) {
                goto out_term;
            }
        } else if (name == ATOM_hash && arity == 1) {
            atom_t a;

            _PL_get_arg(1, t, arg);
            if (!(rc = PL_get_atom_ex(arg, &a))) {
                clingo_set_error(clingo_error_runtime, "prolog error");
                goto out_term;
            }

            if (a == ATOM_inf) {
                clingo_symbol_create_infimum(val);
            } else if (a == ATOM_sup) {
                clingo_symbol_create_supremum(val);
            } else {
                rc = false;
                clingo_set_error(clingo_error_runtime, "bad value");
                goto out_term;
            }
        } else {
            const char *id = PL_atom_chars(name); /* TBD: errors */
            size_t i;

            if (!(values = malloc(sizeof(*values) * arity))) {
                rc = false;
                clingo_set_error(clingo_error_bad_alloc, "memory");
                goto out_term;
            }

            for (i = 0; i < arity; i++) {
                _PL_get_arg(i + 1, t, arg);
                if (!(rc = get_value(arg, &values[i], FALSE))) {
                    goto out_term;
                }
            }
            PL_reset_term_refs(arg);

            if (!(rc = clingo_symbol_create_function(id, values, arity, !minus,
                                                     val))) {
                goto out_term;
            }
        }
    out_term:
        if (values) {
            free(values);
        }
        return rc;
    }
    default:
        clingo_set_error(clingo_error_runtime, "bad value");
        return false;
    }
}