1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: iapi.c 9043 2008-08-28 22:48:19Z giles $ */
16
/* Public Application Programming Interface to Ghostscript interpreter */
22
#include "iapi.h" /* Public API */
31
/* number of threads to allow per process
32
* currently more than 1 is guarenteed to fail
34
static int gsapi_instance_counter = 0;
35
static const int gsapi_instance_max = 1;
37
/* Return revision numbers and strings of Ghostscript. */
38
/* Used for determining if wrong GSDLL loaded. */
39
/* This may be called before any other function. */
40
GSDLLEXPORT int GSDLLAPI
41
gsapi_revision(gsapi_revision_t *pr, int rvsize)
43
if (rvsize < sizeof(gsapi_revision_t))
44
return sizeof(gsapi_revision_t);
45
pr->product = gs_product;
46
pr->copyright = gs_copyright;
47
pr->revision = gs_revision;
48
pr->revisiondate = gs_revisiondate;
52
/* Create a new instance of Ghostscript.
53
* First instance per process call with *pinstance == NULL
54
* next instance in a proces call with *pinstance == copy of valid_instance pointer
55
* *pinstance is set to a new instance pointer.
57
GSDLLEXPORT int GSDLLAPI
58
gsapi_new_instance(void **pinstance, void *caller_handle)
60
gs_memory_t *mem = NULL;
61
gs_main_instance *minst = NULL;
63
if (pinstance == NULL)
66
/* limited to 1 instance, till it works :) */
67
if ( gsapi_instance_counter >= gsapi_instance_max )
69
++gsapi_instance_counter;
71
if (*pinstance == NULL)
72
/* first instance in this process */
73
mem = gs_malloc_init(NULL);
75
/* nothing different for second thread initialization
76
* seperate memory, ids, only stdio is process shared.
78
mem = gs_malloc_init(NULL);
81
minst = gs_main_alloc_instance(mem);
82
mem->gs_lib_ctx->top_of_system = (void*) minst;
83
mem->gs_lib_ctx->caller_handle = caller_handle;
84
mem->gs_lib_ctx->custom_color_callback = NULL;
85
mem->gs_lib_ctx->stdin_fn = NULL;
86
mem->gs_lib_ctx->stdout_fn = NULL;
87
mem->gs_lib_ctx->stderr_fn = NULL;
88
mem->gs_lib_ctx->poll_fn = NULL;
90
*pinstance = (void*)(mem->gs_lib_ctx);
94
/* Destroy an instance of Ghostscript */
95
/* We do not support multiple instances, so make sure
96
* we use the default instance only once.
98
GSDLLEXPORT void GSDLLAPI
99
gsapi_delete_instance(void *lib)
101
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
103
gs_main_instance *minst = get_minst_from_memory(ctx->memory);
105
ctx->caller_handle = NULL;
106
ctx->stdin_fn = NULL;
107
ctx->stdout_fn = NULL;
108
ctx->stderr_fn = NULL;
110
minst->display = NULL;
112
/* Release the memory (frees up everything) */
113
gs_malloc_release(minst->heap);
115
--gsapi_instance_counter;
119
/* Set the callback functions for stdio */
120
GSDLLEXPORT int GSDLLAPI
121
gsapi_set_stdio(void *lib,
122
int(GSDLLCALL *stdin_fn)(void *caller_handle, char *buf, int len),
123
int(GSDLLCALL *stdout_fn)(void *caller_handle, const char *str, int len),
124
int(GSDLLCALL *stderr_fn)(void *caller_handle, const char *str, int len))
126
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
129
ctx->stdin_fn = stdin_fn;
130
ctx->stdout_fn = stdout_fn;
131
ctx->stderr_fn = stderr_fn;
135
/* Set the callback function for polling */
136
GSDLLEXPORT int GSDLLAPI
137
gsapi_set_poll(void *lib,
138
int(GSDLLCALL *poll_fn)(void *caller_handle))
140
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
143
ctx->poll_fn = poll_fn;
147
/* Set the display callback structure */
148
GSDLLEXPORT int GSDLLAPI
149
gsapi_set_display_callback(void *lib, display_callback *callback)
151
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
154
get_minst_from_memory(ctx->memory)->display = callback;
155
/* not in a language switched build */
160
/* Initialise the interpreter */
161
GSDLLEXPORT int GSDLLAPI
162
gsapi_init_with_args(void *lib, int argc, char **argv)
164
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
167
return gs_main_init_with_args(get_minst_from_memory(ctx->memory), argc, argv);
172
/* The gsapi_run_* functions are like gs_main_run_* except
173
* that the error_object is omitted.
174
* An error object in minst is used instead.
177
/* Setup up a suspendable run_string */
178
GSDLLEXPORT int GSDLLAPI
179
gsapi_run_string_begin(void *lib, int user_errors,
182
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
186
return gs_main_run_string_begin(get_minst_from_memory(ctx->memory),
187
user_errors, pexit_code,
188
&(get_minst_from_memory(ctx->memory)->error_object));
192
GSDLLEXPORT int GSDLLAPI
193
gsapi_run_string_continue(void *lib,
194
const char *str, uint length, int user_errors, int *pexit_code)
196
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
200
return gs_main_run_string_continue(get_minst_from_memory(ctx->memory),
201
str, length, user_errors, pexit_code,
202
&(get_minst_from_memory(ctx->memory)->error_object));
205
GSDLLEXPORT int GSDLLAPI
206
gsapi_run_string_end(void *lib,
207
int user_errors, int *pexit_code)
209
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
213
return gs_main_run_string_end(get_minst_from_memory(ctx->memory),
214
user_errors, pexit_code,
215
&(get_minst_from_memory(ctx->memory)->error_object));
218
GSDLLEXPORT int GSDLLAPI
219
gsapi_run_string_with_length(void *lib,
220
const char *str, uint length, int user_errors, int *pexit_code)
222
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
226
return gs_main_run_string_with_length(get_minst_from_memory(ctx->memory),
227
str, length, user_errors, pexit_code,
228
&(get_minst_from_memory(ctx->memory)->error_object));
231
GSDLLEXPORT int GSDLLAPI
232
gsapi_run_string(void *lib,
233
const char *str, int user_errors, int *pexit_code)
235
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
236
return gsapi_run_string_with_length(get_minst_from_memory(ctx->memory),
237
str, (uint)strlen(str), user_errors, pexit_code);
240
GSDLLEXPORT int GSDLLAPI
241
gsapi_run_file(void *lib, const char *file_name,
242
int user_errors, int *pexit_code)
244
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
248
return gs_main_run_file(get_minst_from_memory(ctx->memory),
249
file_name, user_errors, pexit_code,
250
&(get_minst_from_memory(ctx->memory)->error_object));
254
/* Exit the interpreter */
255
GSDLLEXPORT int GSDLLAPI
256
gsapi_exit(void *lib)
258
gs_lib_ctx_t *ctx = (gs_lib_ctx_t *)lib;
262
gs_to_exit(ctx->memory, 0);
266
/* Visual tracer : */
267
struct vd_trace_interface_s;
268
extern struct vd_trace_interface_s * vd_trace0;
269
GSDLLEXPORT void GSDLLAPI
270
gsapi_set_visual_tracer(struct vd_trace_interface_s *I)