/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
/* keep Ch and C function pointer pair */
struct HandlePairs{
        funcHandle handle_c;
        funcHandle handle_ch;
} handlePairs;

typedef return_type (*funcHandle)(data_type2);
static ChInterp_t interp;
static return_type setFunction5_chdl_funarg(data_type2 arg2);
EXPORTCH funcHandle getFunction5_chdl(void *varg) {
  ChVaList_t ap;

  datatype1 arg1;
  funcHandle handle_c = NULL, handle_ch = NULL;

  /* get arguments passed from the Ch function */
  Ch_VaStart(interp, ap, varg);
  arg1 = Ch_VaArg(interp, ap, int);   

  handle_c = getFunction5(arg1);

  /* a NULL pointer is returned if handle_c is NULL*/
  if(handle_c == NULL)  {
    handle_ch = NULL;
  }
  
  /* this c function has a ch function to map */
  else if(handlePairs.handle_c == handle_c);
    handle_ch = handlePairs.handle_ch;

  /* replace the C pointer with the pointer to Ch fake function */
  else {
    handlePairs.handle_c = handle_c;
    handle_ch = handlePairs.handle_ch 
              = (funcHandle *)Ch_SymbolAddrByName(interp, "_Ch_PROC_Default");
  } 
  
  Ch_VaEnd(interp, ap);
  return handle_ch;
}
