gsdll.c 5.68 KB
Newer Older
1
/* Copyright (C) 2001-2019 Artifex Software, Inc.
2
   All Rights Reserved.
3

4 5 6
   This software is provided AS-IS with no warranty, either express or
   implied.

7 8 9 10 11
   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
12 13
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
*/

/* Dynamic Link Library interface for OS/2 and MS-Windows Ghostscript */
/* front end to gs.c */

/* This has been reimplemented to call the new DLL interface in iapi.h */

#ifdef _Windows
#include <windows.h>
#endif
#ifdef __OS2__
#define INCL_DOS
#define INCL_WIN
#include <os2.h>
#endif

#include "stdpre.h"
#include "iapi.h"	/* Ghostscript interpreter public interface */
#include "string_.h"
#include "ierrors.h"
#include "gscdefs.h"
#include "gstypes.h"
#include "iref.h"
#include "iminst.h"
#include "imain.h"

#include "gsdll.h"	/* old DLL public interface */

/* MacGSView still requires that hwnd be exported
   through the old dll interface. We do that here,
   but expect to remove it when that client has been
   ported to the gsapi interface. */
#ifdef __MACOS__
extern HWND hwndtext;
#endif

/****** SINGLE-INSTANCE HACK ******/
/* GLOBAL WARNING */
GSDLL_CALLBACK pgsdll_callback = NULL;	/* callback for messages and stdio to caller */

static gs_main_instance *pgs_minst = NULL;

/****** SINGLE-INSTANCE HACK ******/

/* local functions */
static int GSDLLCALL gsdll_old_stdin(void *caller_handle, char *buf, int len);
static int GSDLLCALL gsdll_old_stdout(void *caller_handle, const char *str, int len);
static int GSDLLCALL gsdll_old_stderr(void *caller_handle, const char *str, int len);
static int GSDLLCALL gsdll_old_poll(void *caller_handle);

/* ---------- DLL exported functions ---------- */

/* arguments are:
67
 * 1. callback function for stdio and for notification of
68 69 70 71 72 73
 *   sync_output, output_page and resize events
 * 2. window handle, used as parent.  Use NULL if you have no window.
 * 3. argc
 * 4. argv
 */
int GSDLLEXPORT GSDLLAPI
74
gsdll_init_with_encoding(GSDLL_CALLBACK callback, HWND hwnd, int argc, char * argv[], int encoding)
75 76 77 78
{
    int code;

    if ((code = gsapi_new_instance(&pgs_minst, (void *)1)) < 0)
79
        return -1;
80

81 82
    gsapi_set_stdio(pgs_minst,
        gsdll_old_stdin, gsdll_old_stdout, gsdll_old_stderr);
83 84 85 86 87
    gsapi_set_poll(pgs_minst, gsdll_old_poll);
    /* ignore hwnd */

/* rest of MacGSView compatibilty hack */
#ifdef __MACOS__
88
        hwndtext=hwnd;
89 90 91 92 93 94
#endif

/****** SINGLE-INSTANCE HACK ******/
    pgsdll_callback = callback;
/****** SINGLE-INSTANCE HACK ******/

95 96 97
    code = gsapi_set_arg_encoding(pgs_minst, encoding);
    if (code >= 0)
        code = gsapi_init_with_args(pgs_minst, argc, argv);
98
    if (code == gs_error_Quit) {
99 100
        gsapi_exit(pgs_minst);
        return GSDLL_INIT_QUIT;
101 102 103 104
    }
    return code;
}

105 106 107 108 109 110 111 112 113 114
int GSDLLEXPORT GSDLLAPI
gsdll_init(GSDLL_CALLBACK callback, HWND hwnd, int argc, char * argv[])
{
    return gsdll_init_with_encoding(callback, hwnd, argc, argv, GS_ARG_ENCODING_LOCAL);
}

#ifdef __WIN32__
int GSDLLEXPORT GSDLLAPI
gsdll_initW(GSDLL_CALLBACK callback, HWND hwnd, int argc, wchar_t * argv[])
{
115
    return gsdll_init_with_encoding(callback, hwnd, argc, (char **)argv, GS_ARG_ENCODING_UTF16LE);
116 117 118 119 120 121 122 123
}
int GSDLLEXPORT GSDLLAPI
gsdll_initA(GSDLL_CALLBACK callback, HWND hwnd, int argc, char * argv[])
{
    return gsdll_init_with_encoding(callback, hwnd, argc, argv, GS_ARG_ENCODING_LOCAL);
}
#endif

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/* if return value < 0, then error occured and caller should call */
/* gsdll_exit, then unload library */
int GSDLLEXPORT GSDLLAPI
gsdll_execute_begin(void)
{
    int exit_code;
    return gsapi_run_string_begin(pgs_minst, 0, &exit_code);
}

/* if return value < 0, then error occured and caller should call */
/* gsdll_execute_end, then gsdll_exit, then unload library */
int GSDLLEXPORT GSDLLAPI
gsdll_execute_cont(const char * str, int len)
{
    int exit_code;
139 140
    int code = gsapi_run_string_continue(pgs_minst, str, len,
        0, &exit_code);
141
    if (code == gs_error_NeedInput)
142
        code = 0;		/* this is not an error */
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    return code;
}

/* if return value < 0, then error occured and caller should call */
/* gsdll_exit, then unload library */
int GSDLLEXPORT GSDLLAPI
gsdll_execute_end(void)
{
    int exit_code;
    return gsapi_run_string_end(pgs_minst, 0, &exit_code);
}

int GSDLLEXPORT GSDLLAPI
gsdll_exit(void)
{
    int code = gsapi_exit(pgs_minst);

    gsapi_delete_instance(pgs_minst);
    return code;
}

/* Return revision numbers and strings of Ghostscript. */
/* Used for determining if wrong GSDLL loaded. */
/* This may be called before any other function. */
int GSDLLEXPORT GSDLLAPI
gsdll_revision(const char ** product, const char ** copyright,
169
               long * revision, long * revisiondate)
170 171
{
    if (product)
172
        *product = gs_product;
173
    if (copyright)
174
        *copyright = gs_copyright;
175
    if (revision)
176
        *revision = gs_revision;
177
    if (revisiondate)
178
        *revisiondate = gs_revisiondate;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    return 0;
}

static int GSDLLCALL
gsdll_old_stdin(void *caller_handle, char *buf, int len)
{
    return (*pgsdll_callback)(GSDLL_STDIN, buf, len);
}
static int GSDLLCALL
gsdll_old_stdout(void *caller_handle, const char *str, int len)
{
    return (*pgsdll_callback)(GSDLL_STDOUT, (char *)str, len);
}

static int GSDLLCALL
gsdll_old_stderr(void *caller_handle, const char *str, int len)
{
    return (*pgsdll_callback)(GSDLL_STDOUT, (char *)str, len);
}

static int GSDLLCALL
gsdll_old_poll(void *caller_handle)
{
    return (*pgsdll_callback)(GSDLL_POLL, NULL, 0);
}

/* end gsdll.c */