gsdll.c 5.1 KB

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