1
2
3
4
5
6
7
8
9
10
11
12
13
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
169
170
171
172
173
174
175
176
177
178
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
206
207
208
209
|
/* Copyright (C) 1992, 1995, 1996, 1997 Aladdin Enterprises. All rights reserved.
This file is part of Aladdin Ghostscript.
Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author
or distributor accepts any responsibility for the consequences of using it,
or for whether it serves any particular purpose or works at all, unless he
or she says so in writing. Refer to the Aladdin Ghostscript Free Public
License (the "License") for full details.
Every copy of Aladdin Ghostscript must include a copy of the License,
normally in a plain ASCII text file named PUBLIC. The License grants you
the right to copy, modify and redistribute Aladdin Ghostscript, but only
under certain conditions described in the License. Among other things, the
License requires that the copyright notice and this notice be preserved on
all copies.
*/
/* gp_msio.c */
/*
* Streams for Windows text window
* Original version by Russell Lang and Maurice Castro with help from
* Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
* initially created from gp_dosfb.c and gp_itbc.c 5th June 1992.
*/
/* Modified for Win32 & Microsoft C/C++ 8.0 32-Bit, 26.Okt.1994 */
/* by Friedrich Nowak */
/* Factored out from gp_mswin.c by JD 6/25/97 */
#include "stdio_.h"
#include <stdlib.h>
#include "gx.h"
#include "gp.h"
#include "windows_.h"
#include <shellapi.h>
#ifdef __WIN32__
#include <winspool.h>
#endif
#include "gp_mswin.h"
#include "gsdll.h"
#include "stream.h"
#include "gxiodev.h" /* must come after stream.h */
/* Imported from gp_msdos.c */
int gp_file_is_console(P1(FILE *));
/* ====== Substitute for stdio ====== */
/* Forward references */
private void win_std_init(void);
private stream_proc_process(win_std_read_process);
private stream_proc_process(win_std_write_process);
/* Use a pseudo IODevice to get win_stdio_init called at the right time. */
/* This is bad architecture; we'll fix it later. */
private iodev_proc_init(win_stdio_init);
gx_io_device gs_iodev_wstdio =
{
"wstdio", "Special",
{win_stdio_init, iodev_no_open_device,
iodev_no_open_file, iodev_no_fopen, iodev_no_fclose,
iodev_no_delete_file, iodev_no_rename_file,
iodev_no_file_status, iodev_no_enumerate_files
}
};
/* Do one-time initialization */
private int
win_stdio_init(gx_io_device * iodev, gs_memory_t * mem)
{
win_std_init(); /* redefine stdin/out/err to our window routines */
return 0;
}
/* Define alternate 'open' routines for our stdin/out/err streams. */
extern int iodev_stdin_open(P4(gx_io_device *, const char *, stream **,
gs_memory_t *));
private int
win_stdin_open(gx_io_device * iodev, const char *access, stream ** ps,
gs_memory_t * mem)
{
int code = iodev_stdin_open(iodev, access, ps, mem);
stream *s = *ps;
if (code != 1)
return code;
s->procs.process = win_std_read_process;
s->file = NULL;
return 0;
}
extern int iodev_stdout_open(P4(gx_io_device *, const char *, stream **,
gs_memory_t *));
private int
win_stdout_open(gx_io_device * iodev, const char *access, stream ** ps,
gs_memory_t * mem)
{
int code = iodev_stdout_open(iodev, access, ps, mem);
stream *s = *ps;
if (code != 1)
return code;
s->procs.process = win_std_write_process;
s->file = NULL;
return 0;
}
extern int iodev_stderr_open(P4(gx_io_device *, const char *, stream **,
gs_memory_t *));
private int
win_stderr_open(gx_io_device * iodev, const char *access, stream ** ps,
gs_memory_t * mem)
{
int code = iodev_stderr_open(iodev, access, ps, mem);
stream *s = *ps;
if (code != 1)
return code;
s->procs.process = win_std_write_process;
s->file = NULL;
return 0;
}
/* Patch stdin/out/err to use our windows. */
private void
win_std_init(void)
{
/* If stdxxx is the console, replace the 'open' routines, */
/* which haven't gotten called yet. */
if (gp_file_is_console(gs_stdin))
gs_findiodevice((const byte *)"%stdin", 6)->procs.open_device =
win_stdin_open;
if (gp_file_is_console(gs_stdout))
gs_findiodevice((const byte *)"%stdout", 7)->procs.open_device =
win_stdout_open;
if (gp_file_is_console(gs_stderr))
gs_findiodevice((const byte *)"%stderr", 7)->procs.open_device =
win_stderr_open;
}
private int
win_std_read_process(stream_state * st, stream_cursor_read * ignore_pr,
stream_cursor_write * pw, bool last)
{
int count = pw->limit - pw->ptr;
if (count == 0) /* empty buffer */
return 1;
/* callback to get more input */
count = (*pgsdll_callback) (GSDLL_STDIN, pw->ptr + 1, count);
if (count == 0) {
/* EOF */
/* what should we do? */
return EOFC;
}
pw->ptr += count;
return 1;
}
private int
win_std_write_process(stream_state * st, stream_cursor_read * pr,
stream_cursor_write * ignore_pw, bool last)
{
uint count = pr->limit - pr->ptr;
(*pgsdll_callback) (GSDLL_STDOUT, (char *)(pr->ptr + 1), count);
pr->ptr = pr->limit;
return 0;
}
/* This is used instead of the stdio version. */
/* The declaration must be identical to that in <stdio.h>. */
#if defined(_WIN32) && (defined(_MSC_VER) || defined(_WATCOM_))
#if defined(_CRTAPI2)
int _CRTAPI2
fprintf(FILE * file, const char *fmt,...)
#else
_CRTIMP int __cdecl
fprintf(FILE * file, const char *fmt,...)
#endif
#else
int _Cdecl _FARFUNC
fprintf(FILE _FAR * file, const char *fmt,...)
#endif
{
int count;
va_list args;
va_start(args, fmt);
if (gp_file_is_console(file)) {
char buf[1024];
count = vsprintf(buf, fmt, args);
(*pgsdll_callback) (GSDLL_STDOUT, buf, count);
} else
count = vfprintf(file, fmt, args);
va_end(args);
return count;
}
|