/*
 * This file was generated automatically by ExtUtils::ParseXS version 3.40 from the
 * contents of open.xs. Do not edit this file, edit open.xs instead.
 *
 *    ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "open.xs"
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"

#define SAVE_AND_REPLACE_PP_IF_UNSET(real_function, op_to_replace, overload_function, OP_replace_mutex) do {\
    MUTEX_LOCK(&OP_replace_mutex);\
    if (!real_function) {\
        real_function = PL_ppaddr[op_to_replace];\
    }\
    if (PL_ppaddr[op_to_replace] != overload_function) {\
        PL_ppaddr[op_to_replace] = overload_function;\
    }\
    else {\
        /* Would be nice if we could warn here. */\
    }\
    MUTEX_UNLOCK(&OP_replace_mutex);\
} while (0)

#define overload_open_die_with_xs_sub 1
#define overload_open_max_function_pointers 2
OP* (*stuff_array[overload_open_max_function_pointers])(pTHX);
/* Declare function pointers for OP's */
OP* (*real_pp_open)(pTHX) = NULL;
OP* (*real_pp_sysopen)(pTHX) = NULL;

#define overload_open_max_args 99
#ifdef USE_ITHREADS
static perl_mutex OP_OPEN_replace_mutex;
static perl_mutex OP_SYSOPEN_replace_mutex;
#endif

OP * (*real_pp_open)(pTHX);
OP * (*real_pp_sysopen)(pTHX);
SV * cached_hook_open = NULL;
SV * cached_hook_sysopen = NULL;
CV * cached_code_hook_open = NULL;
CV * cached_code_hook_sysopen = NULL;
bool overload_is_sysopen(char *opname) {
    return strcmp(opname, "sysopen") == 0;
}
bool overload_is_open(char *opname) {
    return strcmp(opname, "open") == 0;
}
void set_cached_hooks_for_op (char *opname, SV *hook, CV *code_hook) {
    if (overload_is_open(opname)) {
        cached_hook_open      = hook;
        cached_code_hook_open = code_hook;
    }
    if (overload_is_sysopen(opname)) {
        cached_hook_sysopen      = hook;
        cached_code_hook_sysopen = code_hook;
    }
}
OP * overload_allopen(char *opname, char *global, OP* (*real_pp_func)(pTHX)) {
    SV *hook = get_sv(global, 0);
    /* If the hook evaluates as false, we should just call the original
     * function ( AKA overload::open->prehook_open() has not been called yet ) */
    if ( !hook || !SvTRUE( hook ) ) {
        set_cached_hooks_for_op(opname, NULL, NULL);
        return real_pp_func(aTHXR);
    }
    /* Check to make sure we have a coderef */
    if ( !SvROK( hook ) || SvTYPE( SvRV(hook) ) != SVt_PVCV ) {
        set_cached_hooks_for_op(opname, NULL, NULL);
        warn("override::open expected a code reference, but got something else");
        return real_pp_func(aTHXR);
    }
    /* Get the CV* that the reference refers to */
    CV* code_hook = (CV*) SvRV(hook);
    if ( CvISXSUB( code_hook ) ) {
        if ( overload_open_die_with_xs_sub )
            die("overload::open error. Cowardly refusing to hook an XS sub into %s", opname);
        return real_pp_func(aTHXR);
    }
    /* Found suitable hook. We can cache in now */
    set_cached_hooks_for_op(opname, hook, code_hook);

    /* CvDEPTH > 0 that means our hook is calling OP_OPEN. This is ok
     * just ensure we direct things to the original function */
    /* calling on the cached allows us to check the depth for both of the code functions */
    if (cached_code_hook_open) {
        if ( 0 < CvDEPTH( cached_code_hook_open ) ) {
            return real_pp_func(aTHXR);
        }
    }
    if (cached_code_hook_sysopen) {
        if ( 0 < CvDEPTH( cached_code_hook_sysopen ) ) {
            return real_pp_func(aTHXR);
        }
    }
    /* Once more for paranoia */
    if ( 0 < CvDEPTH( code_hook ) ) {
        return real_pp_func(aTHXR);
    }
    ENTER;
        /* Save the temporaries stack */
        SAVETMPS;
            /* sp (stack pointer) is used by some macros we call below. mysp is *ours* */
            SV **sp = PL_stack_sp;
            /* Save the stack pointer location */
            SV **mysp = PL_stack_sp;
            /*assert((PL_markstack_ptr > PL_markstack) || !"MARK underflow"); */
            /* DON'T call dMARK... it has unintended side effects.
             * it actually calls POPMARK! sad! */
            /* Initialize mark ourselves instead. */
            /* SV **mark = PL_stack_base + *PL_markstack_ptr; */
            /* Save the number of items (number of arguments) */
            ssize_t myitems = (ssize_t)(sp - (PL_stack_base + *PL_markstack_ptr));
            if (myitems < 0)
                DIE(aTHXR_ "panic: overload::open internal error. This should not happen.");

            PUSHMARK(sp);
                EXTEND(sp, myitems);
                ssize_t c;
                for ( c = 0; c < myitems; c++) {
                    /* We are going from last to first */
                    ssize_t i = myitems - 1 - c;
                    mPUSHs( newSVsv(*(mysp - i)) );
                }
            /*  PL_stack_sp = sp */
            PUTBACK; /* Closing bracket for XSUB arguments */
            I32 count = call_sv( (SV*)code_hook, G_VOID | G_DISCARD|G_EVAL |G_KEEPERR);
            /* G_VOID and G_DISCARD should cause us to not ask for any return
            * arguments from the call. */
            if (count) warn("call_sv was not supposed to get any arguments");
            /* The purpose of the macro "SPAGAIN" is to refresh the local copy of
            * the stack pointer. This is necessary because it is possible that
            * the memory allocated to the Perl stack has been reallocated during
            * the *call_pv* call */
            /*  sp = PL_stack_sp */
            SPAGAIN;

        /* FREETMPS cleans up all stuff on the temporaries stack added since SAVETMPS was called */
        FREETMPS;
    LEAVE;
    return real_pp_func(aTHXR);
}

PP(pp_overload_open) {
    return overload_allopen("open", "overload::open::GLOBAL_OPEN", real_pp_open);
}

PP(pp_overload_sysopen) {
    return overload_allopen("sysopen", "overload::open::GLOBAL_SYSOPEN",
        real_pp_sysopen);
}

#line 159 "open.c"
#ifndef PERL_UNUSED_VAR
#  define PERL_UNUSED_VAR(var) if (0) var = var
#endif

#ifndef dVAR
#  define dVAR		dNOOP
#endif


/* This stuff is not part of the API! You have been warned. */
#ifndef PERL_VERSION_DECIMAL
#  define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#endif
#ifndef PERL_DECIMAL_VERSION
#  define PERL_DECIMAL_VERSION \
	  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#endif
#ifndef PERL_VERSION_GE
#  define PERL_VERSION_GE(r,v,s) \
	  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
#endif
#ifndef PERL_VERSION_LE
#  define PERL_VERSION_LE(r,v,s) \
	  (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
#endif

/* XS_INTERNAL is the explicit static-linkage variant of the default
 * XS macro.
 *
 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
 * "STATIC", ie. it exports XSUB symbols. You probably don't want that
 * for anything but the BOOT XSUB.
 *
 * See XSUB.h in core!
 */


/* TODO: This might be compatible further back than 5.10.0. */
#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
#  undef XS_EXTERNAL
#  undef XS_INTERNAL
#  if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
#    define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
#    define XS_INTERNAL(name) STATIC XSPROTO(name)
#  endif
#  if defined(__SYMBIAN32__)
#    define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
#    define XS_INTERNAL(name) EXPORT_C STATIC XSPROTO(name)
#  endif
#  ifndef XS_EXTERNAL
#    if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
#      define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
#      define XS_INTERNAL(name) STATIC void name(pTHX_ CV* cv __attribute__unused__)
#    else
#      ifdef __cplusplus
#        define XS_EXTERNAL(name) extern "C" XSPROTO(name)
#        define XS_INTERNAL(name) static XSPROTO(name)
#      else
#        define XS_EXTERNAL(name) XSPROTO(name)
#        define XS_INTERNAL(name) STATIC XSPROTO(name)
#      endif
#    endif
#  endif
#endif

/* perl >= 5.10.0 && perl <= 5.15.1 */


/* The XS_EXTERNAL macro is used for functions that must not be static
 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
 * macro defined, the best we can do is assume XS is the same.
 * Dito for XS_INTERNAL.
 */
#ifndef XS_EXTERNAL
#  define XS_EXTERNAL(name) XS(name)
#endif
#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) XS(name)
#endif

/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
 * internal macro that we're free to redefine for varying linkage due
 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
 */

#undef XS_EUPXS
#if defined(PERL_EUPXS_ALWAYS_EXPORT)
#  define XS_EUPXS(name) XS_EXTERNAL(name)
#else
   /* default to internal */
#  define XS_EUPXS(name) XS_INTERNAL(name)
#endif

#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)

/* prototype to pass -Wmissing-prototypes */
STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params);

STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params)
{
    const GV *const gv = CvGV(cv);

    PERL_ARGS_ASSERT_CROAK_XS_USAGE;

    if (gv) {
        const char *const gvname = GvNAME(gv);
        const HV *const stash = GvSTASH(gv);
        const char *const hvname = stash ? HvNAME(stash) : NULL;

        if (hvname)
	    Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
        else
	    Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
	Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#define croak_xs_usage        S_croak_xs_usage

#endif

/* NOTE: the prototype of newXSproto() is different in versions of perls,
 * so we define a portable version of newXSproto()
 */
#ifdef newXS_flags
#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
#else
#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
#endif /* !defined(newXS_flags) */

#if PERL_VERSION_LE(5, 21, 5)
#  define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
#else
#  define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
#endif

#line 303 "open.c"

XS_EUPXS(XS_overload__open__test_xs_function); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_overload__open__test_xs_function)
{
    dVAR; dXSARGS;
    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
    {
#line 156 "open.xs"
        printf("running test xs function\n");
#line 314 "open.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_overload__open__install_open); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_overload__open__install_open)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    {
#line 161 "open.xs"
        SAVE_AND_REPLACE_PP_IF_UNSET(real_pp_open, OP_OPEN, Perl_pp_overload_open, OP_OPEN_replace_mutex);
#line 329 "open.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_overload__open__install_sysopen); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_overload__open__install_sysopen)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    {
#line 166 "open.xs"
        SAVE_AND_REPLACE_PP_IF_UNSET(real_pp_sysopen, OP_SYSOPEN, Perl_pp_overload_sysopen, OP_SYSOPEN_replace_mutex);
#line 344 "open.c"
    }
    XSRETURN_EMPTY;
}

#ifdef __cplusplus
extern "C"
#endif
XS_EXTERNAL(boot_overload__open); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_overload__open)
{
#if PERL_VERSION_LE(5, 21, 5)
    dVAR; dXSARGS;
#else
    dVAR; dXSBOOTARGSXSAPIVERCHK;
#endif
#if (PERL_REVISION == 5 && PERL_VERSION < 9)
    char* file = __FILE__;
#else
    const char* file = __FILE__;
#endif

    PERL_UNUSED_VAR(file);

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#if PERL_VERSION_LE(5, 21, 5)
    XS_VERSION_BOOTCHECK;
#  ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#  endif
#endif

        (void)newXSproto_portable("overload::open::_test_xs_function", XS_overload__open__test_xs_function, file, ";@");
        (void)newXSproto_portable("overload::open::_install_open", XS_overload__open__install_open, file, "");
        (void)newXSproto_portable("overload::open::_install_sysopen", XS_overload__open__install_sysopen, file, "");
#if PERL_VERSION_LE(5, 21, 5)
#  if PERL_VERSION_GE(5, 9, 0)
    if (PL_unitcheckav)
        call_list(PL_scopestack_ix, PL_unitcheckav);
#  endif
    XSRETURN_YES;
#else
    Perl_xs_boot_epilog(aTHX_ ax);
#endif
}

