home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / source / gs5.10 / gp_msio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-22  |  5.5 KB  |  201 lines

  1. /* Copyright (C) 1992, 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gp_msio.c */
  20. /*
  21.  * Streams for Windows text window
  22.  * Original version by Russell Lang and Maurice Castro with help from
  23.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  24.  * initially created from gp_dosfb.c and gp_itbc.c 5th June 1992.
  25.  */
  26.  
  27. /* Modified for Win32 & Microsoft C/C++ 8.0 32-Bit, 26.Okt.1994 */
  28. /* by Friedrich Nowak                                           */
  29.  
  30. /* Factored out from gp_mswin.c by JD 6/25/97 */
  31.  
  32. #include "stdio_.h"
  33. #include <stdlib.h>
  34. #include "gx.h"
  35. #include "gp.h"
  36. #include "windows_.h"
  37. #include <shellapi.h>
  38. #ifdef __WIN32__
  39. #include <winspool.h>
  40. #endif
  41. #include "gp_mswin.h"
  42. #include "gsdll.h"
  43.  
  44. #include "stream.h"
  45. #include "gxiodev.h"            /* must come after stream.h */
  46.  
  47. /* Imported from gp_msdos.c */
  48. int gp_file_is_console(P1(FILE *));
  49.  
  50.  
  51. /* ====== Substitute for stdio ====== */
  52.  
  53. /* Forward references */
  54. private void win_std_init(void);
  55. private stream_proc_process(win_std_read_process);
  56. private stream_proc_process(win_std_write_process);
  57.  
  58. /* Use a pseudo IODevice to get win_stdio_init called at the right time. */
  59. /* This is bad architecture; we'll fix it later. */
  60. private iodev_proc_init(win_stdio_init);
  61. gx_io_device gs_iodev_wstdio = {
  62.     "wstdio", "Special",
  63.     { win_stdio_init, iodev_no_open_device,
  64.       iodev_no_open_file, iodev_no_fopen, iodev_no_fclose,
  65.       iodev_no_delete_file, iodev_no_rename_file,
  66.       iodev_no_file_status, iodev_no_enumerate_files
  67.     }
  68. };
  69.  
  70. /* Do one-time initialization */
  71. private int
  72. win_stdio_init(gx_io_device *iodev, gs_memory_t *mem)
  73. {
  74.     win_std_init();        /* redefine stdin/out/err to our window routines */
  75.     return 0;
  76. }
  77.  
  78. /* Define alternate 'open' routines for our stdin/out/err streams. */
  79.  
  80. extern int iodev_stdin_open(P4(gx_io_device *, const char *, stream **,
  81.                    gs_memory_t *));
  82. private int
  83. win_stdin_open(gx_io_device *iodev, const char *access, stream **ps,
  84.   gs_memory_t *mem)
  85. {    int code = iodev_stdin_open(iodev, access, ps, mem);
  86.     stream *s = *ps;
  87.     if ( code != 1 )
  88.       return code;
  89.     s->procs.process = win_std_read_process;
  90.     s->file = NULL;
  91.     return 0;
  92. }
  93.  
  94. extern int iodev_stdout_open(P4(gx_io_device *, const char *, stream **,
  95.                 gs_memory_t *));
  96. private int
  97. win_stdout_open(gx_io_device *iodev, const char *access, stream **ps,
  98.   gs_memory_t *mem)
  99. {    int code = iodev_stdout_open(iodev, access, ps, mem);
  100.     stream *s = *ps;
  101.     if ( code != 1 )
  102.       return code;
  103.     s->procs.process = win_std_write_process;
  104.     s->file = NULL;
  105.     return 0;
  106. }
  107.  
  108. extern int iodev_stderr_open(P4(gx_io_device *, const char *, stream **,
  109.                 gs_memory_t *));
  110. private int
  111. win_stderr_open(gx_io_device *iodev, const char *access, stream **ps,
  112.   gs_memory_t *mem)
  113. {    int code = iodev_stderr_open(iodev, access, ps, mem);
  114.     stream *s = *ps;
  115.     if ( code != 1 )
  116.       return code;
  117.     s->procs.process = win_std_write_process;
  118.     s->file = NULL;
  119.     return 0;
  120. }
  121.  
  122. /* Patch stdin/out/err to use our windows. */
  123. private void
  124. win_std_init(void)
  125. {
  126.     /* If stdxxx is the console, replace the 'open' routines, */
  127.     /* which haven't gotten called yet. */
  128.  
  129.     if ( gp_file_is_console(gs_stdin) )
  130.       gs_findiodevice((const byte *)"%stdin", 6)->procs.open_device =
  131.         win_stdin_open;
  132.  
  133.     if ( gp_file_is_console(gs_stdout) )
  134.       gs_findiodevice((const byte *)"%stdout", 7)->procs.open_device =
  135.         win_stdout_open;
  136.  
  137.     if ( gp_file_is_console(gs_stderr) )
  138.       gs_findiodevice((const byte *)"%stderr", 7)->procs.open_device =
  139.         win_stderr_open;
  140. }
  141.  
  142. private int
  143. win_std_read_process(stream_state *st, stream_cursor_read *ignore_pr,
  144.   stream_cursor_write *pw, bool last)
  145. {
  146.     int count = pw->limit - pw->ptr;
  147.  
  148.     if ( count == 0 )         /* empty buffer */
  149.         return 1;
  150.  
  151. /* callback to get more input */
  152.     count = (*pgsdll_callback)(GSDLL_STDIN, pw->ptr+1, count);
  153.     if (count == 0) {
  154.         /* EOF */
  155.         /* what should we do? */
  156.         return EOFC;
  157.     }
  158.  
  159.     pw->ptr += count;
  160.     return 1;
  161. }
  162.  
  163.  
  164. private int
  165. win_std_write_process(stream_state *st, stream_cursor_read *pr,
  166.   stream_cursor_write *ignore_pw, bool last)
  167. {    uint count = pr->limit - pr->ptr;
  168.     (*pgsdll_callback)(GSDLL_STDOUT, (char *)(pr->ptr +1), count);
  169.     pr->ptr = pr->limit;
  170.     return 0;
  171. }
  172.  
  173. /* This is used instead of the stdio version. */
  174. /* The declaration must be identical to that in <stdio.h>. */
  175. #if defined(_WIN32) && (defined(_MSC_VER) || defined(_WATCOM_))
  176. #if defined(_CRTAPI2)
  177. int _CRTAPI2
  178. fprintf(FILE *file, const char *fmt, ...)
  179. #else
  180. _CRTIMP int __cdecl
  181. fprintf(FILE *file, const char *fmt, ...)
  182. #endif
  183. #else
  184. int _Cdecl _FARFUNC
  185. fprintf(FILE _FAR *file, const char *fmt, ...)
  186. #endif
  187. {
  188. int count;
  189. va_list args;
  190.     va_start(args,fmt);
  191.     if ( gp_file_is_console(file) ) {
  192.         char buf[1024];
  193.         count = vsprintf(buf,fmt,args);
  194.         (*pgsdll_callback)(GSDLL_STDOUT, buf, count);
  195.     }
  196.     else
  197.         count = vfprintf(file, fmt, args);
  198.     va_end(args);
  199.     return count;
  200. }
  201.