/* Portions Copyright (C) 2001 artofcode LLC.
   Portions Copyright (C) 1996, 2001 Artifex Software Inc.
   Portions Copyright (C) 1988, 2000 Aladdin Enterprises.
   This software is based in part on the work of the Independent JPEG Group.
   All Rights Reserved.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/ or
   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
   San Rafael, CA  94903, (415)492-9861, for further information. */

/*$RCSfile: iapi.c,v $ $Revision: 1.6 $ */
/* Public Application Programming Interface to Ghostscript interpreter */

#include "string_.h"
#include "ierrors.h"
#include "gscdefs.h"
#include "gstypes.h"
#include "iapi.h"	/* Public API */
#include "iref.h"
#include "iminst.h"
#include "imain.h"
#include "imainarg.h"
#include "gsmemory.h"
#include "gsmalloc.h"
#include "gslibctx.h"

/*
 * GLOBAL WARNING GLOBAL WARNING GLOBAL WARNING GLOBAL WARNING
 *
 *                 The nasty global variables
 *
 * GLOBAL WARNING GLOBAL WARNING GLOBAL WARNING GLOBAL WARNING
 */
int gsapi_instance_counter = 0;
/* extern gs_main_instance *gs_main_instance_default(); */


/* Return revision numbers and strings of Ghostscript. */
/* Used for determining if wrong GSDLL loaded. */
/* This may be called before any other function. */
GSDLLEXPORT int GSDLLAPI
gsapi_revision(gsapi_revision_t *pr, int rvsize)
{
    if (rvsize < sizeof(gsapi_revision_t))
	return sizeof(gsapi_revision_t);
    pr->product = gs_product;
    pr->copyright = gs_copyright;
    pr->revision = gs_revision;
    pr->revisiondate = gs_revisiondate;
    return 0;
}

/* Create a new instance of Ghostscript. */
/* We do not support multiple instances, so make sure
 * we use the default instance only once.
 */
GSDLLEXPORT int GSDLLAPI 
gsapi_new_instance(gs_main_instance **pinstance, void *caller_handle)
{
    gs_main_instance *minst;
    gs_memory_t *mem;
    if (gsapi_instance_counter != 0) {
	*pinstance = NULL;
	return e_Fatal;
    }
    gsapi_instance_counter++;
    
    mem = gs_malloc_init(NULL);
    minst = gs_main_alloc_instance(mem);
    
    mem->gs_lib_ctx->caller_handle = caller_handle;
    mem->gs_lib_ctx->stdin_fn = NULL;
    mem->gs_lib_ctx->stdout_fn = NULL;
    mem->gs_lib_ctx->stderr_fn = NULL;

    // hack 
    // minst->poll_fn = NULL;
    minst->i_ctx_p = NULL;
    *pinstance = minst;
    return 0;
}

/* Destroy an instance of Ghostscript */
/* We do not support multiple instances, so make sure
 * we use the default instance only once.
 */
GSDLLEXPORT void GSDLLAPI 
gsapi_delete_instance(gs_main_instance *minst)
{
    if ((gsapi_instance_counter > 0) && (minst != NULL)) {
	minst->heap->gs_lib_ctx->caller_handle = NULL;
	minst->heap->gs_lib_ctx->stdin_fn = NULL;
	minst->heap->gs_lib_ctx->stdout_fn = NULL;
	minst->heap->gs_lib_ctx->stderr_fn = NULL;
	minst->heap->gs_lib_ctx->poll_fn = NULL;
	minst->display = NULL;
	gsapi_instance_counter--;
    }
}

/* Set the callback functions for stdio */
GSDLLEXPORT int GSDLLAPI 
gsapi_set_stdio(gs_main_instance *minst,
    int(GSDLLCALL *stdin_fn)(void *caller_handle, char *buf, int len),
    int(GSDLLCALL *stdout_fn)(void *caller_handle, const char *str, int len),
    int(GSDLLCALL *stderr_fn)(void *caller_handle, const char *str, int len))
{
    if (minst == NULL)
	return e_Fatal;
    minst->heap->gs_lib_ctx->stdin_fn = stdin_fn;
    minst->heap->gs_lib_ctx->stdout_fn = stdout_fn;
    minst->heap->gs_lib_ctx->stderr_fn = stderr_fn;
    return 0;
}

/* Set the callback function for polling */
GSDLLEXPORT int GSDLLAPI 
gsapi_set_poll(gs_main_instance *minst, 
    int(GSDLLCALL *poll_fn)(void *caller_handle))
{
    if (minst == NULL)
	return e_Fatal;
    minst->heap->gs_lib_ctx->poll_fn = poll_fn;
    return 0;
}

/* Set the display callback structure */
GSDLLEXPORT int GSDLLAPI 
gsapi_set_display_callback(gs_main_instance *minst, display_callback *callback)
{
    if (minst == NULL)
	return e_Fatal;
    minst->display = callback;
    return 0;
}


/* Initialise the interpreter */
GSDLLEXPORT int GSDLLAPI 
gsapi_init_with_args(gs_main_instance *minst, int argc, char **argv)
{
    if (minst == NULL)
	return e_Fatal;
    return gs_main_init_with_args(minst, argc, argv);
}



/* The gsapi_run_* functions are like gs_main_run_* except
 * that the error_object is omitted.
 * An error object in minst is used instead.
 */

/* Setup up a suspendable run_string */
GSDLLEXPORT int GSDLLAPI 
gsapi_run_string_begin(gs_main_instance *minst, int user_errors, 
	int *pexit_code)
{
    if (minst == NULL)
	return e_Fatal;

    return gs_main_run_string_begin(minst, 
	user_errors, pexit_code, &minst->error_object);
}


GSDLLEXPORT int GSDLLAPI 
gsapi_run_string_continue(gs_main_instance *minst, 
	const char *str, uint length, int user_errors, int *pexit_code)
{
    if (minst == NULL)
	return e_Fatal;

    return gs_main_run_string_continue(minst,
	str, length, user_errors, pexit_code, &minst->error_object);
}

GSDLLEXPORT int GSDLLAPI 
gsapi_run_string_end(gs_main_instance *minst, 
	int user_errors, int *pexit_code)
{
    if (minst == NULL)
	return e_Fatal;

    return gs_main_run_string_end(minst,
	user_errors, pexit_code, &minst->error_object);
}

GSDLLEXPORT int GSDLLAPI 
gsapi_run_string_with_length(gs_main_instance *minst, 
	const char *str, uint length, int user_errors, int *pexit_code)
{
    if (minst == NULL)
	return e_Fatal;

    return gs_main_run_string_with_length(minst,
	str, length, user_errors, pexit_code, &minst->error_object);
}

GSDLLEXPORT int GSDLLAPI 
gsapi_run_string(gs_main_instance *minst, 
	const char *str, int user_errors, int *pexit_code)
{
    return gsapi_run_string_with_length(minst, 
	str, (uint)strlen(str), user_errors, pexit_code);
}

GSDLLEXPORT int GSDLLAPI 
gsapi_run_file(gs_main_instance *minst, const char *file_name, 
	int user_errors, int *pexit_code)
{
    if (minst == NULL)
	return e_Fatal;

    return gs_main_run_file(minst,
	file_name, user_errors, pexit_code, &minst->error_object);
}


/* Exit the interpreter */
GSDLLEXPORT int GSDLLAPI 
gsapi_exit(gs_main_instance *minst)
{
    if (minst == NULL)
	return e_Fatal;

    gs_to_exit(minst->heap, 0);
    return 0;
}

/* Visual tracer : */
struct vd_trace_interface_s;
extern struct vd_trace_interface_s * vd_trace0;
GSDLLEXPORT void GSDLLAPI
gsapi_set_visual_tracer(struct vd_trace_interface_s *I)
{   vd_trace0 = I;
}

/* end of iapi.c */
