Beispiel #1
0
static int
get_option(term_t t, int *opt)
{ term_t tail = PL_copy_term_ref(t);
  term_t head = PL_new_term_ref();
  char *s;
  int option = 0;

  while( PL_get_list_ex(tail, head, tail) )
  { if ( PL_get_chars(head, &s, CVT_ATOM|CVT_EXCEPTION) )
    { if      ( streq(s, "cons"  ) ) option |= LOG_CONS;
      else if ( streq(s, "ndelay") ) option |= LOG_NDELAY;
      else if ( streq(s, "nowait") ) option |= LOG_NOWAIT;
      else if ( streq(s, "odelay") ) option |= LOG_ODELAY;
#ifdef LOG_PERROR
      else if ( streq(s, "perror") ) option |= LOG_PERROR;
#endif
      else if ( streq(s, "pid") )    option |= LOG_PID;
      else return PL_domain_error("syslog_option", head);
    } else
      return FALSE;
  }

  if ( PL_get_nil_ex(tail) )
  { *opt = option;
    return TRUE;
  }

  return FALSE;
}
cairo_bool_t
plcairo_term_to_ps_level(term_t            t,
                         cairo_ps_level_t *level)
{
    atom_t a;

    PLCAIRO_debug("    term: 0x%lx  --->  cairo_ps_level_t: %p", t, level);

    if ( !PL_get_atom(t, &a) )
    {   return PL_type_error("CairoPSLevel", t);
    }

    if ( !ATOM_cairo_ps_level_2 )
    {
        ATOM_cairo_ps_level_2 = PL_new_atom("CAIRO_PS_LEVEL_2");
        ATOM_cairo_ps_level_3 = PL_new_atom("CAIRO_PS_LEVEL_3");
    }

    if ( a == ATOM_cairo_ps_level_2 )
    {   *level = CAIRO_PS_LEVEL_2;
    }
    else if ( a == ATOM_cairo_ps_level_3 )
    {   *level = CAIRO_PS_LEVEL_3;
    }
    else
    {   return PL_domain_error("CairoPSLevel", t);
    }

    return TRUE;
}
Beispiel #3
0
static int get_show_map(term_t t, int *map) {
    int rc;
    term_t tail = PL_copy_term_ref(t);
    term_t head = PL_new_term_ref();

    *map = 0;
    while (PL_get_list(tail, head, tail)) {
        atom_t a;

        if (!(rc = PL_get_atom_ex(head, &a))) {
            goto out;
        }

        if (a == ATOM_atoms) {
            *map |= clingo_show_type_atoms;
        } else if (a == ATOM_terms) {
            *map |= clingo_show_type_terms;
        } else if (a == ATOM_shown) {
            *map |= clingo_show_type_shown;
        } else if (a == ATOM_csp) {
            *map |= clingo_show_type_csp;
        } else if (a == ATOM_comp) {
            *map |= clingo_show_type_complement;
        } else {
            rc = PL_domain_error("clingo_show", head);
            goto out;
        }
    }

    if (!(rc = PL_get_nil_ex(tail))) {
        goto out;
    }
out:
    return rc;
}
Beispiel #4
0
static foreign_t pl_clingo_assign_external(term_t ccontrol, term_t Atom,
                                           term_t Value) {
    clingo_env *ctl;
    clingo_symbol_t atom;
    clingo_truth_value_t value;
    int bv, rc;

    if (!(rc = get_clingo(ccontrol, &ctl))) {
        goto out;
    }

    if (!(rc = clingo_status(get_value(Atom, &atom, FALSE)))) {
        goto out;
    }

    if (PL_is_variable(Value)) {
        value = clingo_truth_value_free;
    } else if (PL_get_bool_ex(Value, &bv)) {
        value = bv ? clingo_truth_value_true : clingo_truth_value_false;
    } else {
        rc = PL_domain_error("assign_external", Value);
        goto out;
    }

    if (!(rc = clingo_status(
              clingo_control_assign_external(ctl->control, atom, value)))) {
        goto out;
    }

out:
    return rc;
}
Beispiel #5
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;
}
Beispiel #6
0
static int
get_short_ex(term_t t, short *p)
{ long v;

  if ( !PL_get_long_ex(t, &v) )
    return FALSE;
  if ( v < SHRT_MIN || v > SHRT_MAX )
    return PL_domain_error("short integer", t);

  *p = (short)v;

  return TRUE;
}
Beispiel #7
0
cairo_bool_t
plcairo_term_to_pattern_type(term_t                t,
                             cairo_pattern_type_t *pattern_type)
{
  atom_t a;

  PLCAIRO_debug("    term: 0x%lx  --->  cairo_pattern_type_t: %p", t, pattern_type);

  if ( !PL_get_atom(t, &a) )
  { return PL_type_error("CairoPatternType", t);
  }

  if ( !ATOM_cairo_pattern_type_solid )
  {
    ATOM_cairo_pattern_type_solid = PL_new_atom("CAIRO_PATTERN_TYPE_SOLID");
    ATOM_cairo_pattern_type_surface = PL_new_atom("CAIRO_PATTERN_TYPE_SURFACE");
    ATOM_cairo_pattern_type_linear = PL_new_atom("CAIRO_PATTERN_TYPE_LINEAR");
    ATOM_cairo_pattern_type_radial = PL_new_atom("CAIRO_PATTERN_TYPE_RADIAL");
    ATOM_cairo_pattern_type_mesh = PL_new_atom("CAIRO_PATTERN_TYPE_MESH");
    ATOM_cairo_pattern_type_raster_source = PL_new_atom("CAIRO_PATTERN_TYPE_RASTER_SOURCE");
  }

  if ( a == ATOM_cairo_pattern_type_solid )
  { *pattern_type = CAIRO_PATTERN_TYPE_SOLID;
  }
  else if ( a == ATOM_cairo_pattern_type_surface )
  { *pattern_type = CAIRO_PATTERN_TYPE_SURFACE;
  }
  else if ( a == ATOM_cairo_pattern_type_linear )
  { *pattern_type = CAIRO_PATTERN_TYPE_LINEAR;
  }
  else if ( a == ATOM_cairo_pattern_type_radial )
  { *pattern_type = CAIRO_PATTERN_TYPE_RADIAL;
  }
  else if ( a == ATOM_cairo_pattern_type_mesh )
  { *pattern_type = CAIRO_PATTERN_TYPE_MESH;
  }
  else if ( a == ATOM_cairo_pattern_type_raster_source )
  { *pattern_type = CAIRO_PATTERN_TYPE_RASTER_SOURCE;
  }
  else
  { return PL_domain_error("CairoPatternType", t);
  }

  return TRUE;
}
Beispiel #8
0
cairo_bool_t
plcairo_term_to_filter(term_t          t,
                       cairo_filter_t *filter)
{
  atom_t a;

  PLCAIRO_debug("    term: 0x%lx  --->  cairo_filter_t: %p", t, filter);

  if ( !PL_get_atom(t, &a) )
  { return PL_type_error("CairoFilter", t);
  }

  if ( !ATOM_cairo_filter_fast )
  {
    ATOM_cairo_filter_fast = PL_new_atom("CAIRO_FILTER_FAST");
    ATOM_cairo_filter_good = PL_new_atom("CAIRO_FILTER_GOOD");
    ATOM_cairo_filter_best = PL_new_atom("CAIRO_FILTER_BEST");
    ATOM_cairo_filter_nearest = PL_new_atom("CAIRO_FILTER_NEAREST");
    ATOM_cairo_filter_bilinear = PL_new_atom("CAIRO_FILTER_BILINEAR");
    ATOM_cairo_filter_gaussian = PL_new_atom("CAIRO_FILTER_GAUSSIAN");
  }

  if ( a == ATOM_cairo_filter_fast )
  { *filter = CAIRO_FILTER_FAST;
  }
  else if ( a == ATOM_cairo_filter_good )
  { *filter = CAIRO_FILTER_GOOD;
  }
  else if ( a == ATOM_cairo_filter_best )
  { *filter = CAIRO_FILTER_BEST;
  }
  else if ( a == ATOM_cairo_filter_nearest )
  { *filter = CAIRO_FILTER_NEAREST;
  }
  else if ( a == ATOM_cairo_filter_bilinear )
  { *filter = CAIRO_FILTER_BILINEAR;
  }
  else if ( a == ATOM_cairo_filter_gaussian )
  { *filter = CAIRO_FILTER_GAUSSIAN;
  }
  else
  { return PL_domain_error("CairoFilter", t);
  }

  return TRUE;
}
Beispiel #9
0
static int
md5_options(term_t options, optval *result)
{ term_t opts = PL_copy_term_ref(options);
  term_t opt = PL_new_term_ref();

					/* defaults */
  memset(result, 0, sizeof(*result));
  result->encoding = REP_UTF8;

  while(PL_get_list(opts, opt, opts))
  { atom_t aname;
    size_t arity;

    if ( PL_get_name_arity(opt, &aname, &arity) && arity == 1 )
    { term_t a = PL_new_term_ref();

      _PL_get_arg(1, opt, a);

      if ( aname == ATOM_encoding )
      { atom_t a_enc;

	if ( !PL_get_atom_ex(a, &a_enc) )
	  return FALSE;
	if ( a_enc == ATOM_utf8 )
	  result->encoding = REP_UTF8;
	else if ( a_enc == ATOM_octet )
	  result->encoding = REP_ISO_LATIN_1;
	else
	  return PL_domain_error("encoding", a);
      }
    } else
    { return PL_type_error("option", opt);
    }
  }

  if ( !PL_get_nil(opts) )
    return PL_type_error("list", opts);

  return TRUE;
}
Beispiel #10
0
cairo_bool_t
plcairo_term_to_extend(term_t          t,
                       cairo_extend_t *extend)
{
  atom_t a;

  PLCAIRO_debug("    term: 0x%lx  --->  cairo_extend_t: %p", t, extend);

  if ( !PL_get_atom(t, &a) )
  { return PL_type_error("CairoExtend", t);
  }

  if ( !ATOM_cairo_extend_none )
  {
    ATOM_cairo_extend_none = PL_new_atom("CAIRO_EXTEND_NONE");
    ATOM_cairo_extend_repeat = PL_new_atom("CAIRO_EXTEND_REPEAT");
    ATOM_cairo_extend_reflect = PL_new_atom("CAIRO_EXTEND_REFLECT");
    ATOM_cairo_extend_pad = PL_new_atom("CAIRO_EXTEND_PAD");
  }

  if ( a == ATOM_cairo_extend_none )
  { *extend = CAIRO_EXTEND_NONE;
  }
  else if ( a == ATOM_cairo_extend_repeat )
  { *extend = CAIRO_EXTEND_REPEAT;
  }
  else if ( a == ATOM_cairo_extend_reflect )
  { *extend = CAIRO_EXTEND_REFLECT;
  }
  else if ( a == ATOM_cairo_extend_pad )
  { *extend = CAIRO_EXTEND_PAD;
  }
  else
  { return PL_domain_error("CairoExtend", t);
  }

  return TRUE;
}
Beispiel #11
0
static int
get_facility(term_t t, int *fac)
{ char *s;
  int facility;

  if ( PL_get_chars(t, &s, CVT_ATOM|CVT_EXCEPTION) )
  { if      ( streq(s, "auth"  ) )   facility = LOG_AUTH;
#ifdef LOG_AUTHPRIV
    else if ( streq(s, "authpriv") ) facility = LOG_AUTHPRIV;
#endif
    else if ( streq(s, "cron") )     facility = LOG_CRON;
    else if ( streq(s, "daemon") )   facility = LOG_DAEMON;
#ifdef LOG_FTP
    else if ( streq(s, "ftp") )      facility = LOG_FTP;
#endif
    else if ( streq(s, "kern") )     facility = LOG_KERN;
    else if ( streq(s, "local0") )   facility = LOG_LOCAL0;
    else if ( streq(s, "local1") )   facility = LOG_LOCAL1;
    else if ( streq(s, "local2") )   facility = LOG_LOCAL2;
    else if ( streq(s, "local3") )   facility = LOG_LOCAL3;
    else if ( streq(s, "local4") )   facility = LOG_LOCAL4;
    else if ( streq(s, "local5") )   facility = LOG_LOCAL5;
    else if ( streq(s, "local6") )   facility = LOG_LOCAL6;
    else if ( streq(s, "local7") )   facility = LOG_LOCAL7;
    else if ( streq(s, "lpr") )      facility = LOG_LPR;
    else if ( streq(s, "mail") )     facility = LOG_MAIL;
    else if ( streq(s, "news") )     facility = LOG_NEWS;
    else if ( streq(s, "syslog") )   facility = LOG_SYSLOG;
    else if ( streq(s, "user") )     facility = LOG_USER;
    else if ( streq(s, "uucp") )     facility = LOG_UUCP;
    else return PL_domain_error("syslog_facility", t);
  } else
    return FALSE;

  *fac = facility;
  return TRUE;
}
Beispiel #12
0
static int
get_priority(term_t t, int *pri)
{ char *s;
  int priority;

  if ( PL_get_chars(t, &s, CVT_ATOM|CVT_EXCEPTION) )
  { if      ( streq(s, "emerg"  ) ) priority =	LOG_EMERG;
    else if ( streq(s, "alert") )   priority = LOG_ALERT;
    else if ( streq(s, "crit") )    priority = LOG_CRIT;
    else if ( streq(s, "err") )     priority = LOG_ERR;
    else if ( streq(s, "warning") ) priority = LOG_WARNING;
    else if ( streq(s, "notice") )  priority = LOG_NOTICE;
    else if ( streq(s, "info") )    priority = LOG_INFO;
    else if ( streq(s, "debug") )   priority = LOG_DEBUG;
    else
    { PL_domain_error("syslog_priority", t);
      return FALSE;
    }
  } else
    return FALSE;

  *pri = priority;
  return TRUE;
}
Beispiel #13
0
static foreign_t
pl_crypt(term_t passwd, term_t encrypted)
{ char *pw, *e;
  char salt[20];

  if ( !PL_get_chars(passwd, &pw, CVT_ATOM|CVT_STRING|CVT_LIST|BUF_RING) )
    return pl_error("crypt", 2, NULL, ERR_ARGTYPE,
		    1, passwd, "text");

  if ( PL_get_chars(encrypted, &e, CVT_ATOM|CVT_STRING|CVT_LIST|BUF_RING) )
  { char *s2;

    if ( strncmp(e, "$1$", 3) == 0 )	/* MD5 Hash */
    { char *p = strchr(e+3, '$');
      size_t slen;

      if ( p && (slen=(size_t)(p-e-3)) < sizeof(salt) )
      { strncpy(salt, e+3, slen);
	salt[slen] = 0;
	s2 = md5_crypt(pw, salt);
	return (strcmp(s2, e) == 0) ? TRUE : FALSE;
      } else
      { Sdprintf("No salt???\n");
	return FALSE;
      }
    } else
    { int rval;

      salt[0] = e[0];
      salt[1] = e[1];
      salt[2] = '\0';

      LOCK();
      rval = ( (s2 = crypt(pw, salt)) &&
	       strcmp(s2, e) == 0
	     );
      UNLOCK();

      return rval;
    }
  } else
  { term_t tail = PL_copy_term_ref(encrypted);
    term_t head = PL_new_term_ref();
    int slen = 2;
    int n;
    int (*unify)(term_t t, const char *s) = PL_unify_list_codes;
    char *s2;
    int rval;

    for(n=0; n<slen; n++)
    { if ( PL_get_list(tail, head, tail) )
      { int i;
	char *t;

	if ( PL_get_integer(head, &i) && i>=0 && i<=255 )
	{ salt[n] = i;
	} else if ( PL_get_atom_chars(head, &t) && t[1] == '\0' )
	{ salt[n] = t[0];
	  unify = PL_unify_list_chars;
	} else
	{ return pl_error("crypt", 2, NULL, ERR_ARGTYPE,
			  2, head, "character");
	}

	if ( n == 1 && salt[0] == '$' && salt[1] == '1' )
	  slen = 3;
	else if ( n == 2 && salt[2] == '$' )
	  slen = 8+3;
      } else
	break;
    }

    for( ; n < slen; n++ )
    { int c = 'a'+(int)(26.0*rand()/(RAND_MAX+1.0));

      if ( rand() & 0x1 )
	c += 'A' - 'a';

      salt[n] = c;
    }
    salt[n] = 0;
    LOCK();
    if ( slen > 2 )
    { s2 = md5_crypt(pw, salt);
    } else
    { s2 = crypt(pw, salt);
    }
    if ( s2 )
      rval = (*unify)(encrypted, s2);
    else
      rval = PL_domain_error("salt", encrypted);
    UNLOCK();

    return rval;
  }
}
Beispiel #14
0
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;
}
Beispiel #15
0
static foreign_t
archive_header_prop(term_t archive, term_t field)
{ archive_wrapper *ar;
  functor_t prop;

  if ( !get_archive(archive, &ar) )
    return FALSE;

  if ( !PL_get_functor(field, &prop) )
    return PL_type_error("compound", field);
  if ( ar->status != AR_NEW_ENTRY )
    return PL_permission_error("access", "archive_entry", archive);

  if ( prop == FUNCTOR_filetype1 )
  { __LA_MODE_T type = archive_entry_filetype(ar->entry);
    atom_t name;
    term_t arg = PL_new_term_ref();
    _PL_get_arg(1, field, arg);

    switch(type&AE_IFMT)
    { case AE_IFREG:  name = ATOM_file;             break;
      case AE_IFLNK:  name = ATOM_link;             break;
      case AE_IFSOCK: name = ATOM_socket;           break;
      case AE_IFCHR:  name = ATOM_character_device; break;
      case AE_IFBLK:  name = ATOM_block_device;     break;
      case AE_IFDIR:  name = ATOM_directory;        break;
      case AE_IFIFO:  name = ATOM_fifo;             break;
      default:
	return PL_unify_integer(arg, (type&AE_IFMT));
    }
    return PL_unify_atom(arg, name);
  } else if ( prop == FUNCTOR_mtime1 )
  { time_t stamp = archive_entry_mtime(ar->entry);
    term_t arg = PL_new_term_ref();
    _PL_get_arg(1, field, arg);

    return PL_unify_float(arg, (double)stamp);
  } else if ( prop == FUNCTOR_size1 )
  { int64_t size = archive_entry_size(ar->entry);
    term_t arg = PL_new_term_ref();
    _PL_get_arg(1, field, arg);

    return PL_unify_int64(arg, size);
  } else if ( prop == FUNCTOR_link_target1 )
  { __LA_MODE_T type = archive_entry_filetype(ar->entry);
    const wchar_t *target = NULL;

    switch(type&AE_IFMT)
    { case AE_IFLNK:
	target = archive_entry_symlink_w(ar->entry);
        break;
    }

    if ( target )
    { term_t arg = PL_new_term_ref();
      _PL_get_arg(1, field, arg);

      return PL_unify_wchars(arg, PL_ATOM, (size_t)-1, target);
    }

    return FALSE;
  } else if ( prop == FUNCTOR_format1 )
  { const char *s = archive_format_name(ar->archive);

    if ( s )
    { char lwr[50];
      char *o;
      term_t arg = PL_new_term_ref();
      _PL_get_arg(1, field, arg);

      for(o=lwr; *s && o < lwr+sizeof(lwr); )
	*o++ = tolower(*s++);

      *o = '\0';

      return PL_unify_atom_chars(arg, lwr);
    }
  }

  return PL_domain_error("archive_header_property", field);
}
Beispiel #16
0
static foreign_t
archive_open_stream(term_t data, term_t handle, term_t options)
{ IOSTREAM *datas;
  archive_wrapper *ar;
  term_t tail = PL_copy_term_ref(options);
  term_t head = PL_new_term_ref();
  term_t arg  = PL_new_term_ref();

  if ( !PL_get_stream_handle(data, &datas) )
    return FALSE;
  if ( !(datas->flags & SIO_INPUT) )
  { PL_release_stream(datas);
    return PL_domain_error("input_stream", data);
  }

  ar = PL_malloc(sizeof(*ar));
  memset(ar, 0, sizeof(*ar));
  ar->data = datas;
  ar->magic = ARCHIVE_MAGIC;
  if ( !PL_unify_blob(handle, ar, sizeof(*ar), &archive_blob) )
    return FALSE;

  while( PL_get_list_ex(tail, head, tail) )
  { atom_t name;
    int arity;

    if ( !PL_get_name_arity(head, &name, &arity) ||
	 !PL_get_arg(1, head, arg) )
      return PL_type_error("option", head);
    if ( name == ATOM_compression || name == ATOM_filter )
    { atom_t c;

      if ( !PL_get_atom_ex(arg, &c) )
	return FALSE;

      if ( c == ATOM_all )
	ar->type |= FILTER_ALL;
#ifdef FILTER_BZIP2
      else if ( c == ATOM_bzip2 )
	ar->type |= FILTER_BZIP2;
#endif
#ifdef FILTER_COMPRESS
      else if ( c == ATOM_compress )
	ar->type |= FILTER_COMPRESS;
#endif
#ifdef FILTER_GZIP
      else if ( c == ATOM_gzip )
	ar->type |= FILTER_GZIP;
#endif
#ifdef FILTER_GRZIP
      else if ( c == ATOM_grzip )
	ar->type |= FILTER_GRZIP;
#endif
#ifdef FILTER_LRZIP
      else if ( c == ATOM_lrzip )
	ar->type |= FILTER_LRZIP;
#endif
#ifdef FILTER_LZIP
      else if ( c == ATOM_lzip )
	ar->type |= FILTER_LZIP;
#endif
#ifdef FILTER_LZMA
      else if ( c == ATOM_lzma )
	ar->type |= FILTER_LZMA;
#endif
#ifdef FILTER_LZOP
      else if ( c == ATOM_lzop )
	ar->type |= FILTER_LZOP;
#endif
#ifdef FILTER_NONE
      else if ( c == ATOM_none )
	ar->type |= FILTER_NONE;
#endif
#ifdef FILTER_RPM
      else if ( c == ATOM_rpm )
	ar->type |= FILTER_RPM;
#endif
#ifdef FILTER_UU
      else if ( c == ATOM_uu )
	ar->type |= FILTER_UU;
#endif
#ifdef FILTER_XZ
      else if ( c == ATOM_xz )
	ar->type |= FILTER_XZ;
#endif
      else
	return PL_domain_error("filter", arg);
    } else if ( name == ATOM_format )
    { atom_t f;

      if ( !PL_get_atom_ex(arg, &f) )
	return FALSE;

      if ( f == ATOM_all )
	ar->type |= FORMAT_ALL;
#ifdef FORMAT_7ZIP
      else if ( f == ATOM_7zip )
	ar->type |= FORMAT_7ZIP;
#endif
#ifdef FORMAT_AR
      else if ( f == ATOM_ar )
	ar->type |= FORMAT_AR;
#endif
#ifdef FORMAT_CAB
      else if ( f == ATOM_cab )
	ar->type |= FORMAT_CAB;
#endif
#ifdef FORMAT_CPIO
      else if ( f == ATOM_cpio )
	ar->type |= FORMAT_CPIO;
#endif
#ifdef FORMAT_EMPTY
      else if ( f == ATOM_empty )
	ar->type |= FORMAT_EMPTY;
#endif
#ifdef FORMAT_GNUTAR
      else if ( f == ATOM_gnutar )
	ar->type |= FORMAT_GNUTAR;
#endif
#ifdef FORMAT_ISO9960
      else if ( f == ATOM_iso9960 )
	ar->type |= FORMAT_ISO9960;
#endif
#ifdef FORMAT_LHA
      else if ( f == ATOM_lha )
	ar->type |= FORMAT_LHA;
#endif
#ifdef FORMAT_MTREE
      else if ( f == ATOM_mtree )
	ar->type |= FORMAT_MTREE;
#endif
#ifdef FORMAT_RAR
      else if ( f == ATOM_rar )
	ar->type |= FORMAT_RAR;
#endif
#ifdef FORMAT_RAW
      else if ( f == ATOM_raw )
	ar->type |= FORMAT_RAW;
#endif
#ifdef FORMAT_TAR
      else if ( f == ATOM_tar )
	ar->type |= FORMAT_TAR;
#endif
#ifdef FORMAT_XAR
      else if ( f == ATOM_xar )
	ar->type |= FORMAT_XAR;
#endif
#ifdef FORMAT_ZIP
      else if ( f == ATOM_zip )
	ar->type |= FORMAT_ZIP;
#endif
      else
	return PL_domain_error("format", arg);
    } else if ( name == ATOM_close_parent )
    { if ( !PL_get_bool_ex(arg, &ar->close_parent) )
	return FALSE;
    }
  }
  if ( !PL_get_nil_ex(tail) )
    return FALSE;

  if ( !(ar->type & FILTER_ALL) )
    ar->type |= FILTER_ALL;
  if ( !(ar->type & FORMAT_MASK) )
    ar->type |= FORMAT_ALL;

  if ( !(ar->archive = archive_read_new()) )
    return PL_resource_error("memory");

  if ( (ar->type & FILTER_ALL) == FILTER_ALL )
  { archive_read_support_filter_all(ar->archive);
  } else
  {
#ifdef FILTER_BZIP2
    enable_type(ar, FILTER_BZIP2,    archive_read_support_filter_bzip2);
#endif
#ifdef FILTER_COMPRESS
    enable_type(ar, FILTER_COMPRESS, archive_read_support_filter_compress);
#endif
#ifdef FILTER_GZIP
    enable_type(ar, FILTER_GZIP,     archive_read_support_filter_gzip);
#endif
#ifdef FILTER_GRZIP
    enable_type(ar, FILTER_GRZIP,     archive_read_support_filter_grzip);
#endif
#ifdef FILTER_LRZIP
    enable_type(ar, FILTER_LRZIP,     archive_read_support_filter_lrzip);
#endif
#ifdef FILTER_LZIP
    enable_type(ar, FILTER_LZIP,     archive_read_support_filter_lzip);
#endif
#ifdef FILTER_LZMA
    enable_type(ar, FILTER_LZMA,     archive_read_support_filter_lzma);
#endif
#ifdef FILTER_LZOP
    enable_type(ar, FILTER_LZOP,     archive_read_support_filter_lzop);
#endif
#ifdef FILTER_NONE
    enable_type(ar, FILTER_NONE,     archive_read_support_filter_none);
#endif
#ifdef FILTER_RPM
    enable_type(ar, FILTER_RPM,      archive_read_support_filter_rpm);
#endif
#ifdef FILTER_UU
    enable_type(ar, FILTER_UU,       archive_read_support_filter_uu);
#endif
#ifdef FILTER_XZ
    enable_type(ar, FILTER_XZ,       archive_read_support_filter_xz);
#endif
  }

  if ( (ar->type & FORMAT_ALL) == FORMAT_ALL )
  { archive_read_support_format_all(ar->archive);
#ifdef FORMAT_RAW
    enable_type(ar, FORMAT_RAW,     archive_read_support_format_raw);
#endif
  } else
  {
#ifdef FORMAT_7ZIP
    enable_type(ar, FORMAT_7ZIP,    archive_read_support_format_7zip);
#endif
#ifdef FORMAT_AR
    enable_type(ar, FORMAT_AR,      archive_read_support_format_ar);
#endif
#ifdef FORMAT_CAB
    enable_type(ar, FORMAT_CAB,     archive_read_support_format_cab);
#endif
#ifdef FORMAT_CPIO
    enable_type(ar, FORMAT_CPIO,    archive_read_support_format_cpio);
#endif
#ifdef FORMAT_EMPTY
    enable_type(ar, FORMAT_EMPTY,   archive_read_support_format_empty);
#endif
#ifdef FORMAT_GNUTAR
    enable_type(ar, FORMAT_GNUTAR,  archive_read_support_format_gnutar);
#endif
#ifdef FORMAT_ISO9960
    enable_type(ar, FORMAT_ISO9960, archive_read_support_format_iso9660);
#endif
#ifdef FORMAT_LHA
    enable_type(ar, FORMAT_LHA,     archive_read_support_format_lha);
#endif
#ifdef FORMAT_MTREE
    enable_type(ar, FORMAT_MTREE,   archive_read_support_format_mtree);
#endif
#ifdef FORMAT_RAR
    enable_type(ar, FORMAT_RAR,     archive_read_support_format_rar);
#endif
#ifdef FORMAT_RAW
    enable_type(ar, FORMAT_RAW,     archive_read_support_format_raw);
#endif
#ifdef FORMAT_TAR
    enable_type(ar, FORMAT_TAR,     archive_read_support_format_tar);
#endif
#ifdef FORMAT_XAR
    enable_type(ar, FORMAT_XAR,     archive_read_support_format_xar);
#endif
#ifdef FORMAT_ZIP
    enable_type(ar, FORMAT_ZIP,     archive_read_support_format_zip);
#endif
  }

#ifdef HAVE_ARCHIVE_READ_OPEN1
  archive_read_set_callback_data(ar->archive, ar);
  archive_read_set_open_callback(ar->archive, ar_open);
  archive_read_set_read_callback(ar->archive, ar_read);
  archive_read_set_skip_callback(ar->archive, ar_skip);
  archive_read_set_seek_callback(ar->archive, ar_seek);
  archive_read_set_close_callback(ar->archive, ar_close);

  if ( archive_read_open1(ar->archive) == ARCHIVE_OK )
  { ar->status = AR_OPENED;
    return TRUE;
  }
#else
  if ( archive_read_open2(ar->archive, ar,
			  ar_open, ar_read, ar_skip, ar_close) == ARCHIVE_OK )
  { ar->status = AR_OPENED;
    return TRUE;
  }
#endif

  return archive_error(ar);
}