/* Interface to Perl from OCaml.
  * Copyright (C) 2003 Merjis Ltd.
- * $Id: perl_c.c,v 1.18 2004-12-11 13:38:32 rich Exp $
+ * $Id: perl_c.c,v 1.19 2004-12-11 15:40:09 rich Exp $
  */
 
 #include <stdio.h>
 #include <unistd.h>
 #include <alloca.h>
 
+#define CAML_NAME_SPACE 1
+
 #include <caml/alloc.h>
 #include <caml/callback.h>
 #include <caml/custom.h>
 #include <caml/memory.h>
 #include <caml/mlvalues.h>
 
-#ifndef PERL4CAML_NO_OFF64_T
-/* XXX This was required to avoid an error on my machine when loading the Perl
- * headers. Not clear why this is missing.
- */
-#define off64_t __off64_t
-#endif
-
-#ifndef PERL4CAML_NO_CRYPT_H
-/* XXX This is required by Perl >= 5.8.2. */
-#define __USE_GNU
-#include <crypt.h>
-#endif
-
 #include <EXTERN.h>
 #include <perl.h>
 
 {
   CAMLparam1 (unit);
   int argc = 4;
-  static char *argv[] = { "", "-w", "-e", "0" };
+  static char *argv[] = { "", "-w", "-e", "0", NULL };
 
-  PERL_SYS_INIT3 (NULL, NULL, NULL);
+  PERL_SYS_INIT (&argc, &argv);
   my_perl = perl_alloc ();
   perl_construct (my_perl);
   PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
-  perl_parse (my_perl, xs_init, argc, argv, NULL);
+  perl_parse (my_perl, xs_init, argc, argv, (char **) NULL);
   /*perl_run (my_perl);*/
 
   CAMLreturn (Val_unit);
   CAMLparam1 (svv);
   SV *sv = Sv_val (svv);
   CAMLlocal1 (f);
-  f = copy_double (SvNV (sv));
+  f = caml_copy_double (SvNV (sv));
   CAMLreturn (f);
 }
 
   CAMLlocal1 (strv);
   str = SvPV (sv, len);
   /* XXX This won't work if the string contains NUL. */
-  strv = copy_string (str);
+  strv = caml_copy_string (str);
   CAMLreturn (strv);
 }
 
 perl4caml_sv_of_string (value strv)
 {
   CAMLparam1 (strv);
-  CAMLreturn (Val_sv (newSVpv (String_val (strv), string_length (strv))));
+  CAMLreturn (Val_sv (newSVpv (String_val (strv), caml_string_length (strv))));
 }
 
 CAMLprim value
   SV *sv = Sv_val (svv);
 
   if (!SvROK (sv))
-    invalid_argument ("deref: SV is not a reference");
+    caml_invalid_argument ("deref: SV is not a reference");
   switch (SvTYPE (SvRV (sv))) {
   case SVt_IV:
   case SVt_NV:
   case SVt_PVMG:
     break;
   default:
-    invalid_argument ("deref: SV is not a reference to a scalar");
+    caml_invalid_argument ("deref: SV is not a reference to a scalar");
   }
   rsvv = Val_sv (SvRV (sv));
   CAMLreturn (rsvv);
   SV *sv = Sv_val (svv);
 
   if (!SvROK (sv))
-    invalid_argument ("deref_array: SV is not a reference");
+    caml_invalid_argument ("deref_array: SV is not a reference");
   switch (SvTYPE (SvRV (sv))) {
   case SVt_PVAV:
     break;
   default:
-    invalid_argument ("deref_array: SV is not a reference to an array");
+    caml_invalid_argument ("deref_array: SV is not a reference to an array");
   }
   ravv = Val_av ((AV *) SvRV (sv));
   CAMLreturn (ravv);
   SV *sv = Sv_val (svv);
 
   if (!SvROK (sv))
-    invalid_argument ("deref_hash: SV is not a reference");
+    caml_invalid_argument ("deref_hash: SV is not a reference");
   switch (SvTYPE (SvRV (sv))) {
   case SVt_PVHV:
     break;
   default:
-    invalid_argument ("deref_hash: SV is not a reference to a hash");
+    caml_invalid_argument ("deref_hash: SV is not a reference to a hash");
   }
   rhvv = Val_hv ((HV *) SvRV (sv));
   CAMLreturn (rhvv);
   CAMLparam2 (avv, i);
   AV *av = Av_val (avv);
   SV **svp = av_fetch (av, Int_val (i), 0);
-  if (svp == 0) invalid_argument ("av_get: index out of bounds");
+  if (svp == 0) caml_invalid_argument ("av_get: index out of bounds");
   CAMLreturn (Val_sv (*svp));
 }
 
   HV *hv = Hv_val (hvv);
   SV *sv = Sv_val (svv);
   SvREFCNT_inc (sv);
-  if (hv_store (hv, String_val (key), string_length (key), sv, 0) == 0)
+  if (hv_store (hv, String_val (key), caml_string_length (key), sv, 0) == 0)
     SvREFCNT_dec (sv);
   CAMLreturn (Val_unit);
 }
 {
   CAMLparam2 (hvv, key);
   HV *hv = Hv_val (hvv);
-  SV **svp = hv_fetch (hv, String_val (key), string_length (key), 0);
-  if (svp == 0) raise_not_found ();
+  SV **svp = hv_fetch (hv, String_val (key), caml_string_length (key), 0);
+  if (svp == 0) caml_raise_not_found ();
   CAMLreturn (Val_sv (*svp));
 }
 
 {
   CAMLparam2 (hvv, key);
   HV *hv = Hv_val (hvv);
-  bool r = hv_exists (hv, String_val (key), string_length (key));
+  bool r = hv_exists (hv, String_val (key), caml_string_length (key));
   CAMLreturn (r ? Val_true : Val_false);
 }
 
 {
   CAMLparam2 (hvv, key);
   HV *hv = Hv_val (hvv);
-  hv_delete (hv, String_val (key), string_length (key), G_DISCARD);
+  hv_delete (hv, String_val (key), caml_string_length (key), G_DISCARD);
   CAMLreturn (Val_unit);
 }
 
 
   create = unoption (optcreate, Val_false);
   sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
-  if (sv == NULL) raise_not_found ();
+  if (sv == NULL) caml_raise_not_found ();
 
   CAMLreturn (Val_sv (sv));
 }
 
   create = unoption (optcreate, Val_false);
   av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
-  if (av == NULL) raise_not_found ();
+  if (av == NULL) caml_raise_not_found ();
 
   CAMLreturn (Val_av (av));
 }
 
   create = unoption (optcreate, Val_false);
   hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
-  if (hv == NULL) raise_not_found ();
+  if (hv == NULL) caml_raise_not_found ();
 
   CAMLreturn (Val_hv (hv));
 }
       STRLEN n_a;
       const char *err = SvPV (errsv, n_a);
 
-      errv = copy_string (err);
+      errv = caml_copy_string (err);
 
-      raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
+      caml_raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
     }
 }
 
   for (i = 0; i < count; ++i) {
     SV *sv;
 
-    cons = alloc (2, 0);
+    cons = caml_alloc (2, 0);
     Field (cons, 1) = list;
     list = cons;
     sv = newSVsv (POPs);
   for (i = 0; i < count; ++i) {
     SV *sv;
 
-    cons = alloc (2, 0);
+    cons = caml_alloc (2, 0);
     Field (cons, 1) = list;
     list = cons;
     sv = newSVsv (POPs);
    */
   list = Val_int (0);
   for (i = 0; i < count; ++i) {
-    cons = alloc (2, 0);
+    cons = caml_alloc (2, 0);
     Field (cons, 1) = list;
     list = cons;
     Field (cons, 0) = Val_sv (newSVsv (POPs));
 {
   CAMLparam0 ();
   CAMLlocal1 (rv);
-  rv = alloc (1, Abstract_tag);
+  rv = caml_alloc (1, Abstract_tag);
   Field(rv, 0) = (value) ptr;
   CAMLreturn (rv);
 }
 {
   CAMLparam0 ();
   CAMLlocal1 (rv);
-  rv = alloc_custom (&xv_custom_operations, sizeof (void *), 0, 1);
+  rv = caml_alloc_custom (&xv_custom_operations, sizeof (void *), 0, 1);
   Xv_val (rv) = sv;
   CAMLreturn (rv);
 }