1 /* Copyright (C) 1989, 2001 artofcode, LLC. All rights reserved.
2
3 This software is provided AS-IS with no warranty, either express or
4 implied.
5
6 This software is distributed under license and may not be copied,
7 modified or distributed except as expressly authorized under the terms
8 of the license contained in the file LICENSE in this distribution.
9
10 For more information about licensing, please refer to
11 http://www.ghostscript.com/licensing/. For information on
12 commercial licensing, go to http://www.artifex.com/licensing/ or
13 contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14 San Rafael, CA 94903, U.S.A., +1(415)492-9861.
15 */
16 /* Portions Copyright (C) 1994-2000 Ghostgum Software Pty Ltd. All rights reserved. */
17
18
19 /* $Id: gsdll.c,v 1.12 2004/08/04 23:33:29 stefan Exp $ */
20 /* Dynamic Link Library interface for OS/2 and MS-Windows Ghostscript */
21 /* front end to gs.c */
22
23 /* This has been reimplemented to call the new DLL interface in iapi.h */
24
25
26 #ifdef _Windows
27 #include <windows.h>
28 #endif
29 #ifdef __OS2__
30 #define INCL_DOS
31 #define INCL_WIN
32 #include <os2.h>
33 #endif
34
35 #include "stdpre.h"
36 #include "iapi.h" /* Ghostscript interpreter public interface */
37 #include "string_.h"
38 #include "ierrors.h"
39 #include "gscdefs.h"
40 #include "gstypes.h"
41 #include "iref.h"
42 #include "iminst.h"
43 #include "imain.h"
44
45 #include "gsdll.h" /* old DLL public interface */
46
47 /* MacGSView still requires that hwnd be exported
48 through the old dll interface. We do that here,
49 but expect to remove it when that client has been
50 ported to the gsapi interface. */
51 #ifdef __MACOS__
52 extern HWND hwndtext;
53 #endif
54
55 /****** SINGLE-INSTANCE HACK ******/
56 /* GLOBAL WARNING */
57 GSDLL_CALLBACK pgsdll_callback = NULL; /* callback for messages and stdio to caller */
58
59 static gs_main_instance *pgs_minst = NULL;
60
61
62 /****** SINGLE-INSTANCE HACK ******/
63
64
65 /* local functions */
66 private int GSDLLCALL gsdll_old_stdin(void *caller_handle, char *buf, int len);
67 private int GSDLLCALL gsdll_old_stdout(void *caller_handle, const char *str, int len);
68 private int GSDLLCALL gsdll_old_stderr(void *caller_handle, const char *str, int len);
69 private int GSDLLCALL gsdll_old_poll(void *caller_handle);
70
71
72 /* ---------- DLL exported functions ---------- */
73
74 /* arguments are:
75 * 1. callback function for stdio and for notification of
76 * sync_output, output_page and resize events
77 * 2. window handle, used as parent. Use NULL if you have no window.
78 * 3. argc
79 * 4. argv
80 */
81 int GSDLLEXPORT GSDLLAPI
gsdll_init(GSDLL_CALLBACK callback,HWND hwnd,int argc,char * argv[])82 gsdll_init(GSDLL_CALLBACK callback, HWND hwnd, int argc, char * argv[])
83 {
84 int code;
85
86 if ((code = gsapi_new_instance(&pgs_minst, (void *)1)) < 0)
87 return -1;
88
89 gsapi_set_stdio(pgs_minst,
90 gsdll_old_stdin, gsdll_old_stdout, gsdll_old_stderr);
91 gsapi_set_poll(pgs_minst, gsdll_old_poll);
92 /* ignore hwnd */
93
94 /* rest of MacGSView compatibilty hack */
95 #ifdef __MACOS__
96 hwndtext=hwnd;
97 #endif
98
99 /****** SINGLE-INSTANCE HACK ******/
100 pgsdll_callback = callback;
101 /****** SINGLE-INSTANCE HACK ******/
102
103 code = gsapi_init_with_args(pgs_minst, argc, argv);
104 if (code == e_Quit) {
105 gsapi_exit(pgs_minst);
106 return GSDLL_INIT_QUIT;
107 }
108 return code;
109 }
110
111 /* if return value < 0, then error occured and caller should call */
112 /* gsdll_exit, then unload library */
113 int GSDLLEXPORT GSDLLAPI
gsdll_execute_begin(void)114 gsdll_execute_begin(void)
115 {
116 int exit_code;
117 return gsapi_run_string_begin(pgs_minst, 0, &exit_code);
118 }
119
120 /* if return value < 0, then error occured and caller should call */
121 /* gsdll_execute_end, then gsdll_exit, then unload library */
122 int GSDLLEXPORT GSDLLAPI
gsdll_execute_cont(const char * str,int len)123 gsdll_execute_cont(const char * str, int len)
124 {
125 int exit_code;
126 int code = gsapi_run_string_continue(pgs_minst, str, len,
127 0, &exit_code);
128 if (code == e_NeedInput)
129 code = 0; /* this is not an error */
130 return code;
131 }
132
133 /* if return value < 0, then error occured and caller should call */
134 /* gsdll_exit, then unload library */
135 int GSDLLEXPORT GSDLLAPI
gsdll_execute_end(void)136 gsdll_execute_end(void)
137 {
138 int exit_code;
139 return gsapi_run_string_end(pgs_minst, 0, &exit_code);
140 }
141
142 int GSDLLEXPORT GSDLLAPI
gsdll_exit(void)143 gsdll_exit(void)
144 {
145 int code = gsapi_exit(pgs_minst);
146
147 gsapi_delete_instance(pgs_minst);
148 return code;
149 }
150
151 /* Return revision numbers and strings of Ghostscript. */
152 /* Used for determining if wrong GSDLL loaded. */
153 /* This may be called before any other function. */
154 int GSDLLEXPORT GSDLLAPI
gsdll_revision(const char ** product,const char ** copyright,long * revision,long * revisiondate)155 gsdll_revision(const char ** product, const char ** copyright,
156 long * revision, long * revisiondate)
157 {
158 if (product)
159 *product = gs_product;
160 if (copyright)
161 *copyright = gs_copyright;
162 if (revision)
163 *revision = gs_revision;
164 if (revisiondate)
165 *revisiondate = gs_revisiondate;
166 return 0;
167 }
168
169
170 private int GSDLLCALL
gsdll_old_stdin(void * caller_handle,char * buf,int len)171 gsdll_old_stdin(void *caller_handle, char *buf, int len)
172 {
173 return (*pgsdll_callback)(GSDLL_STDIN, buf, len);
174 }
175 private int GSDLLCALL
gsdll_old_stdout(void * caller_handle,const char * str,int len)176 gsdll_old_stdout(void *caller_handle, const char *str, int len)
177 {
178 return (*pgsdll_callback)(GSDLL_STDOUT, (char *)str, len);
179 }
180
181 private int GSDLLCALL
gsdll_old_stderr(void * caller_handle,const char * str,int len)182 gsdll_old_stderr(void *caller_handle, const char *str, int len)
183 {
184 return (*pgsdll_callback)(GSDLL_STDOUT, (char *)str, len);
185 }
186
187 private int GSDLLCALL
gsdll_old_poll(void * caller_handle)188 gsdll_old_poll(void *caller_handle)
189 {
190 return (*pgsdll_callback)(GSDLL_POLL, NULL, 0);
191 }
192
193 /* end gsdll.c */
194