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
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1998-2016. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#include "elog_global.h"
#include "elog_pipe_stdin.h"
/*
* Data for the handling of a pipe stdin,
* data is read in a separate thread, so locking and
* event signaling needs to be done.
*/
static CRITICAL_SECTION io_crit;
static char *stdin_buff = NULL;
static int stdin_siz = 0;
static int stdin_len = 0;
static int stdin_eof = 0;
/* end syncronized objects */
static int stdin_is_console = 0;
static HANDLE stdin_event;
DWORD WINAPI stdin_thread(LPVOID ptr){
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
char buff[1];
DWORD red;
for(;;){
if(!ReadFile(in, buff, (DWORD) 1, &red, NULL)){
if(GetLastError() == ERROR_BROKEN_PIPE){
EnterCriticalSection(&io_crit);
stdin_eof = 1;
SetEvent(stdin_event);
LeaveCriticalSection(&io_crit);
return 0;
}
return 1;
}else if(red == 0){
EnterCriticalSection(&io_crit);
stdin_eof = 1;
SetEvent(stdin_event);
LeaveCriticalSection(&io_crit);
return 0;
}
#ifdef HARDDEBUG
fprintf(stderr,"stdin_thread go data (%d)\n",(int)*buff);
#endif
EnterCriticalSection(&io_crit);
if(stdin_len + 1 >= stdin_siz){
if(!stdin_siz)
stdin_buff = malloc(stdin_siz = 100);
else
stdin_buff = realloc(stdin_buff, stdin_siz +=100);
}
stdin_buff[stdin_len++] = *buff;
SetEvent(stdin_event);
LeaveCriticalSection(&io_crit);
}
return 0;
}
BOOL peek_pipe_stdin_eof(void){
BOOL ret;
EnterCriticalSection(&io_crit);
if((ret = !!stdin_eof))
ResetEvent(stdin_event); /* Now we "unsignal" */
LeaveCriticalSection(&io_crit);
return ret;
}
int read_pipe_stdin(char *buff, int max){
int ret;
EnterCriticalSection(&io_crit);
if(stdin_len == 0){
if(!stdin_eof){
LeaveCriticalSection(&io_crit);
WaitForSingleObject(stdin_event,INFINITE);
EnterCriticalSection(&io_crit);
if(!stdin_len){
if(stdin_eof){
/* Stay signaled */
LeaveCriticalSection(&io_crit);
return 0;
} else {
ResetEvent(stdin_event);
LeaveCriticalSection(&io_crit);
return -1;
}
}
} else {
/* Stay signaled */
LeaveCriticalSection(&io_crit);
return 0;
}
}
#ifdef HARDDEBUG
fprintf(stderr,"read_pipe_stdin got data.\n"
"max = %d, stdin_len = %d, *stdin_buff = %d\n",
max,stdin_len,*stdin_buff);
#endif
/* stdin_len should be something now */
if(stdin_len > max){
memcpy(buff,stdin_buff,max);
memmove(stdin_buff,stdin_buff + max,stdin_len - max);
stdin_len -= max;
ret = max;
} else {
memcpy(buff,stdin_buff,stdin_len);
ret = stdin_len;
stdin_len = 0;
}
if(!stdin_eof) /* Stay signaled if EOF */
ResetEvent(stdin_event);
LeaveCriticalSection(&io_crit);
return ret;
}
BOOL setup_pipe_stdin(void){
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
DWORD dummy;
if(GetConsoleMode(in, &dummy)){
stdin_is_console = 1;
stdin_event = in;
return TRUE;
}
stdin_event = CreateEvent(NULL, TRUE, FALSE, NULL);
InitializeCriticalSection(&io_crit);
return (_beginthreadex(NULL,0,&stdin_thread,NULL,0,&dummy));
}
BOOL console_stdin(void){
return stdin_is_console;
}
HANDLE get_stdin_event(void){
return stdin_event;
}
|