2
* wstdio.c -- $Id: wstdio.c 685 2003-03-08 15:26:51Z travo $
3
* p_stdinit, p_stdout, p_stdin for MS Windows
5
* Copyright (c) 1999. See accompanying LEGAL file for details.
11
int (*w_stdinit)(void(**)(char*,long), void(**)(char*,long))= 0;
14
static void (*w_on_stdin)(char *input_line)= 0;
15
static void w_formout(char *line, void (*func)(char *, long));
17
static void (*w_stdout)(char *line, long len)= 0;
18
static void (*w_stderr)(char *line, long len)= 0;
23
p_stdinit(void (*on_stdin)(char *input_line))
25
char *mdi = p_getenv("NO_MDI");
26
if (mdi && mdi[0] && (mdi[0]!='0' || mdi[1])) w_no_mdi |= 1;
27
if (!w_no_mdi && !w_stdinit) w_no_mdi = 1, AllocConsole();
28
if (!w_no_mdi || con_stdinit(&w_stdout, &w_stderr)) {
29
w_stdinit(&w_stdout, &w_stderr);
30
} else if (w_main_window) {
31
/* without this, actual first window created does not show properly */
32
ShowWindow(w_main_window, SW_SHOWNORMAL);
33
ShowWindow(w_main_window, SW_HIDE);
35
w_on_stdin = on_stdin;
38
void p_stdout(char *output_line)
40
w_formout(output_line, w_stdout);
43
void p_stderr(char *output_line)
45
w_formout(output_line, w_stderr);
48
char *w_sendbuf(long len)
50
/* console app: called by worker to get buffer to hold stdin
51
* gui app: called by boss to get buffer to hold input line
52
* called by worker w_sendbuf(-1) to retrieve boss buffer
53
* therefore must use raw Windows memory manager routines */
56
HANDLE heap = GetProcessHeap();
57
if (len <= 0x100) len = 0x100;
58
else len = ((len-1)&0x3ff) + 0x100;
59
buf = buf? HeapReAlloc(heap, HEAP_GENERATE_EXCEPTIONS, buf, len+1) :
60
HeapAlloc(heap, HEAP_GENERATE_EXCEPTIONS, len+1);
69
for (i=j=0 ; buf[j] ; j=i) {
70
for (; buf[i] ; i++) if (buf[i]=='\r' || buf[i]=='\n') break;
73
if (cr && buf[i]=='\n') i++;
74
/* deliver one line at a time, not including newline */
75
if (w_on_stdin) w_on_stdin(buf+j);
80
w_formout(char *line, void (*func)(char *, long))
82
if (!p_signalling && line) {
84
HANDLE heap = GetProcessHeap();
85
long len = 256, j = 0, i = 0;
88
buf = HeapAlloc(heap, HEAP_GENERATE_EXCEPTIONS, 258);
91
buf = HeapReAlloc(heap, HEAP_GENERATE_EXCEPTIONS, buf, 2*len+2);
94
if (line[i]!='\n' || c=='\r' || console_mode) {
95
if (line[i] == '\a') {
99
} else if (console_mode && line[i]=='\r' && line[i+1]=='\n') {
106
buf[j++] = c = line[i++];
110
buf = HeapReAlloc(heap, HEAP_GENERATE_EXCEPTIONS, buf, 258);