gp_msio.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Copyright (C) 1992, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises. 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. /* $Id: gp_msio.c,v 1.6 2002/06/16 05:48:55 lpd Exp $ */
  14. /*
  15. * Streams for Windows text window
  16. *
  17. * Original version by Russell Lang and Maurice Castro with help from
  18. * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  19. * initially created from gp_dosfb.c and gp_itbc.c 5th June 1992.
  20. */
  21. /* Modified for Win32 & Microsoft C/C++ 8.0 32-Bit, 26.Okt.1994 */
  22. /* by Friedrich Nowak */
  23. /* Factored out from gp_mswin.c by JD 6/25/97 */
  24. /*
  25. * The MSVC compiler, when invoked with the /MD switch, considers that the
  26. * dllimport qualifier on fprintf in stdio.h and the dllexport qualifier
  27. * on the definition of fprintf in this file are incompatible.
  28. * We use a hack (similar to the one in stdpre.h to deal with sys/types.h)
  29. * to work around this.
  30. */
  31. #define fprintf UNDEFINE_fprintf
  32. #include "stdio_.h"
  33. #undef fprintf
  34. #include <stdlib.h>
  35. #include "gx.h"
  36. #include "gp.h"
  37. #include "windows_.h"
  38. #include <shellapi.h>
  39. #ifdef __WIN32__
  40. #include <winspool.h>
  41. #endif
  42. #include "gp_mswin.h"
  43. #include "gsdll.h"
  44. #include "stream.h"
  45. #include "gxiodev.h" /* must come after stream.h */
  46. /* Imported from gp_msdos.c */
  47. int gp_file_is_console(FILE *);
  48. /* ====== Substitute for stdio ====== */
  49. /* Forward references */
  50. private void win_std_init(void);
  51. private stream_proc_process(win_std_read_process);
  52. private stream_proc_process(win_std_write_process);
  53. private stream_proc_available(win_std_available);
  54. /* Use a pseudo IODevice to get win_stdio_init called at the right time. */
  55. /* This is bad architecture; we'll fix it later. */
  56. private iodev_proc_init(win_stdio_init);
  57. const gx_io_device gs_iodev_wstdio = {
  58. /* The name is null to keep this from showing up as a resource. */
  59. 0, "Special",
  60. {win_stdio_init, iodev_no_open_device,
  61. iodev_no_open_file, iodev_no_fopen, iodev_no_fclose,
  62. iodev_no_delete_file, iodev_no_rename_file,
  63. iodev_no_file_status, iodev_no_enumerate_files
  64. }
  65. };
  66. /* Do one-time initialization */
  67. private int
  68. win_stdio_init(gx_io_device * iodev, gs_memory_t * mem)
  69. {
  70. win_std_init(); /* redefine stdin/out/err to our window routines */
  71. return 0;
  72. }
  73. /* Define alternate 'open' routines for our stdin/out/err streams. */
  74. extern const gx_io_device gs_iodev_stdin;
  75. private int
  76. win_stdin_open(gx_io_device * iodev, const char *access, stream ** ps,
  77. gs_memory_t * mem)
  78. {
  79. int code = gs_iodev_stdin.procs.open_device(iodev, access, ps, mem);
  80. stream *s = *ps;
  81. if (code != 1)
  82. return code;
  83. s->procs.process = win_std_read_process;
  84. s->procs.available = win_std_available;
  85. s->file = NULL;
  86. return 0;
  87. }
  88. extern const gx_io_device gs_iodev_stdout;
  89. private int
  90. win_stdout_open(gx_io_device * iodev, const char *access, stream ** ps,
  91. gs_memory_t * mem)
  92. {
  93. int code = gs_iodev_stdout.procs.open_device(iodev, access, ps, mem);
  94. stream *s = *ps;
  95. if (code != 1)
  96. return code;
  97. s->procs.process = win_std_write_process;
  98. s->procs.available = win_std_available;
  99. s->procs.flush = s_std_write_flush;
  100. s->file = NULL;
  101. return 0;
  102. }
  103. extern const gx_io_device gs_iodev_stderr;
  104. private int
  105. win_stderr_open(gx_io_device * iodev, const char *access, stream ** ps,
  106. gs_memory_t * mem)
  107. {
  108. int code = gs_iodev_stderr.procs.open_device(iodev, access, ps, mem);
  109. stream *s = *ps;
  110. if (code != 1)
  111. return code;
  112. s->procs.process = win_std_write_process;
  113. s->procs.available = win_std_available;
  114. s->procs.flush = s_std_write_flush;
  115. s->file = NULL;
  116. return 0;
  117. }
  118. /* Patch stdin/out/err to use our windows. */
  119. private void
  120. win_std_init(void)
  121. {
  122. /* If stdxxx is the console, replace the 'open' routines, */
  123. /* which haven't gotten called yet. */
  124. if (gp_file_is_console(gs_stdin))
  125. gs_findiodevice((const byte *)"%stdin", 6)->procs.open_device =
  126. win_stdin_open;
  127. if (gp_file_is_console(gs_stdout))
  128. gs_findiodevice((const byte *)"%stdout", 7)->procs.open_device =
  129. win_stdout_open;
  130. if (gp_file_is_console(gs_stderr))
  131. gs_findiodevice((const byte *)"%stderr", 7)->procs.open_device =
  132. win_stderr_open;
  133. }
  134. private int
  135. win_std_read_process(stream_state * st, stream_cursor_read * ignore_pr,
  136. stream_cursor_write * pw, bool last)
  137. {
  138. int count = pw->limit - pw->ptr;
  139. if (count == 0) /* empty buffer */
  140. return 1;
  141. /* callback to get more input */
  142. count = (*pgsdll_callback) (GSDLL_STDIN, pw->ptr + 1, count);
  143. if (count == 0) {
  144. /* EOF */
  145. /* what should we do? */
  146. return EOFC;
  147. }
  148. pw->ptr += count;
  149. return 1;
  150. }
  151. private int
  152. win_std_available(register stream * s, long *pl)
  153. {
  154. *pl = -1; // EOF, since we can't do it
  155. return 0; // OK
  156. }
  157. private int
  158. win_std_write_process(stream_state * st, stream_cursor_read * pr,
  159. stream_cursor_write * ignore_pw, bool last)
  160. {
  161. uint count = pr->limit - pr->ptr;
  162. (*pgsdll_callback) (GSDLL_STDOUT, (char *)(pr->ptr + 1), count);
  163. pr->ptr = pr->limit;
  164. return 0;
  165. }
  166. /* This is used instead of the stdio version. */
  167. /* The declaration must be identical to that in <stdio.h>. */
  168. #if defined(_WIN32) && (defined(_MSC_VER) || defined(_WATCOM_))
  169. #if defined(_CRTAPI2)
  170. int _CRTAPI2
  171. fprintf(FILE * file, const char *fmt,...)
  172. #else
  173. _CRTIMP int __cdecl
  174. fprintf(FILE * file, const char *fmt,...)
  175. #endif
  176. #else
  177. int _Cdecl _FARFUNC
  178. fprintf(FILE _FAR * file, const char *fmt,...)
  179. #endif
  180. {
  181. int count;
  182. va_list args;
  183. va_start(args, fmt);
  184. if (gp_file_is_console(file)) {
  185. char buf[1024];
  186. count = vsprintf(buf, fmt, args);
  187. (*pgsdll_callback) (GSDLL_STDOUT, buf, count);
  188. } else
  189. count = vfprintf(file, fmt, args);
  190. va_end(args);
  191. return count;
  192. }