summaryrefslogtreecommitdiffstats
path: root/private/mvdm/dos/command/cmdredir.c
blob: 5cf700c63e1b6b93c2abaa164e61bb994d0d3b74 (plain) (blame)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/*  cmdredir.c - SCS routines for redirection
 *
 *
 *  Modification History:
 *
 *  Sudeepb 22-Apr-1992 Created
 */

#include "cmd.h"

#include <cmdsvc.h>
#include <softpc.h>
#include <mvdm.h>
#include <ctype.h>

#define CMDREDIR_DEBUG	1

PPIPE_INPUT   cmdPipeList = NULL;

BOOL cmdCheckCopyForRedirection (pRdrInfo)
PREDIRCOMPLETE_INFO pRdrInfo;
{
PPIPE_INPUT  pPipe, pPipePrev;
PPIPE_OUTPUT pPipeOut;

    if (pRdrInfo == NULL)
        return TRUE;
    if (pRdrInfo->ri_pPipeStdIn != NULL) {

	//Piping and Pipe list is empty?
	ASSERT(cmdPipeList != NULL);

	// in most cases, we have only one pipe for stdin
	if (pRdrInfo->ri_pPipeStdIn == cmdPipeList){
	    pPipe = pRdrInfo->ri_pPipeStdIn;
	    cmdPipeList = pPipe->Next;
	}
	// multiple piping
	// search for the right one
	else {
	    pPipe = pPipePrev = cmdPipeList;
	    while (pPipe != NULL && pPipe != pRdrInfo->ri_pPipeStdIn){
		pPipePrev = pPipe;
		pPipe = pPipe->Next;
	    }
	    if (pPipe != NULL)
		// remove it from the list
		pPipePrev->Next = pPipe->Next;
	}
	if (pPipe != NULL) {
	    // grab the critical section. As soon as we have a
	    // a hold on the critical section, it is safe to kill
	    // the piping thread because it is in dormant unless
	    // it has terminated which is also safe for us.
	    EnterCriticalSection(&pPipe->CriticalSection);
	    // if the thread is till running, kill it
	    if (WaitForSingleObject(pPipe->hThread, 0)) {
		TerminateThread(pPipe->hThread, 0);
		WaitForSingleObject(pPipe->hThread, INFINITE);
	    }
	    LeaveCriticalSection(&pPipe->CriticalSection);
	    CloseHandle(pPipe->hFileWrite);
	    CloseHandle(pPipe->hPipe);
	    CloseHandle(pPipe->hDataEvent);
	    CloseHandle(pPipe->hThread);
	    DeleteCriticalSection(&pPipe->CriticalSection);
	    DeleteFile(pPipe->pFileName);
	    free(pPipe->pFileName);
	    free (pPipe);
	}
    }
    // the application is terminating, let the output thread knows
    // about it so it can exit appropriately.
    // the output thread is responsible for clean up
    if (pRdrInfo->ri_pPipeStdOut) {
	// The output thread must wait for the event before
	// it can exit.
	SetEvent((pRdrInfo->ri_pPipeStdOut)->hExitEvent);
	// wait 1 seconds for the thread to go away.
	// this is done because our parent process may put up
	// its prompt before our sibling process has a chance to
	// completely display data on its display surface.
	// note that we can not wait forever here because
	// the sibling process could be the other dos application and
	// we will be deadlock if it is the case
	WaitForSingleObject(pRdrInfo->ri_hStdOutThread, 1000);
	CloseHandle(pRdrInfo->ri_hStdOutThread);
    }
    if (pRdrInfo->ri_pPipeStdErr) {
	SetEvent((pRdrInfo->ri_pPipeStdErr)->hExitEvent);
	WaitForSingleObject(pRdrInfo->ri_hStdErrThread, 1000);
	CloseHandle(pRdrInfo->ri_hStdErrThread);
    }
    free (pRdrInfo);

    return TRUE;
}

BOOL cmdCreateTempFile (phTempFile,ppszTempFile)
PHANDLE phTempFile;
PCHAR	*ppszTempFile;
{

PCHAR pszTempPath = NULL;
DWORD TempPathSize;
PCHAR pszTempFileName;
HANDLE hTempFile;
SECURITY_ATTRIBUTES sa;

    pszTempPath = malloc(MAX_PATH + 12);

    if (pszTempPath == NULL)
	return FALSE;

    if ((TempPathSize = GetTempPath (
			    MAX_PATH,
			    pszTempPath)) == 0){
	free (pszTempPath);
	return FALSE;
    }

    if (TempPathSize >= MAX_PATH) {
	free (pszTempPath);
	return FALSE;
    }

    // CMDCONF.C depends on the size of this buffer
    if ((pszTempFileName = malloc (MAX_PATH + 13)) == NULL){
	free (pszTempPath);
	return FALSE;
    }

         // if this fails it probably means we have a bad path
    if (!GetTempFileName(pszTempPath, "scs", 0, pszTempFileName))
       {
          // lets get something else, which should succeed
         TempPathSize = GetWindowsDirectory(pszTempPath, MAX_PATH);
         if (!TempPathSize || TempPathSize >= MAX_PATH)
             strcpy(pszTempPath, "\\");

          // try again and hope for the best
         GetTempFileName(pszTempPath, "scs", 0, pszTempFileName);
         }


    // must have a security descriptor so that the child process
    // can inherit this file handle. This is done because when we
    // shell out with piping the 32 bits application must have inherited
    // the temp filewe created, see cmdGetStdHandle
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa.lpSecurityDescriptor = NULL;
    sa.bInheritHandle = TRUE;

    if ((hTempFile = CreateFile (pszTempFileName,
				 GENERIC_READ | GENERIC_WRITE,
				 FILE_SHARE_READ | FILE_SHARE_WRITE,
				 &sa,
				 OPEN_ALWAYS,
                                 FILE_ATTRIBUTE_TEMPORARY,
				 NULL)) == (HANDLE)-1){
	free (pszTempFileName);
	free (pszTempPath);
	return FALSE;
    }

    *phTempFile = hTempFile;
    *ppszTempFile = pszTempFileName;
    free (pszTempPath);
    return TRUE;
}

/* cmdCheckStandardHandles - Check if we have to do anything to support
 *			     standard io redirection, if so save away
 *			     pertaining information.
 *
 *  Entry - pVDMInfo - VDMInfo Structure
 *	    pbStdHandle - pointer to bit array for std handles
 *
 *  EXIT  - return NULL if no redirection involved
 *          return pointer to REDIRECTION_INFO
 */

PREDIRCOMPLETE_INFO cmdCheckStandardHandles (
    PVDMINFO pVDMInfo,
    USHORT UNALIGNED *pbStdHandle
    )
{
USHORT bTemp = 0;
PREDIRCOMPLETE_INFO pRdrInfo;

    if (pVDMInfo->StdIn)
	bTemp |= MASK_STDIN;

    if (pVDMInfo->StdOut)
	bTemp |= MASK_STDOUT;

    if (pVDMInfo->StdErr)
	bTemp |= MASK_STDERR;

    if(bTemp){

        if ((pRdrInfo = malloc (sizeof (REDIRCOMPLETE_INFO))) == NULL) {
            RcErrorDialogBox(EG_MALLOC_FAILURE, NULL, NULL);
            TerminateVDM();
        }

        RtlZeroMemory ((PVOID)pRdrInfo, sizeof(REDIRCOMPLETE_INFO));
        pRdrInfo->ri_hStdErr = pVDMInfo->StdErr;
        pRdrInfo->ri_hStdOut = pVDMInfo->StdOut;
        pRdrInfo->ri_hStdIn  = pVDMInfo->StdIn;

        nt_std_handle_notification(TRUE);
        fSoftpcRedirection = TRUE;
    }
    else{
        pRdrInfo = NULL;
        nt_std_handle_notification(FALSE);
        fSoftpcRedirection = FALSE;
    }

    *pbStdHandle = bTemp;
    return pRdrInfo;
}

/* cmdGetStdHandle - Get the 32 bit NT standard handle for the VDM
 *
 *
 *  Entry - Client (CX) - 0,1 or 2 (stdin stdout stderr)
 *          Client (AX:BX) - redirinfo pointer
 *
 *  EXIT  - Client (BX:CX) - 32 bit handle
 *	    Client (DX:AX) - file size
 */

VOID cmdGetStdHandle (VOID)
{
USHORT iStdHandle;
PREDIRCOMPLETE_INFO pRdrInfo;

    iStdHandle = getCX();
    pRdrInfo = (PREDIRCOMPLETE_INFO) (((ULONG)getAX() << 16) + (ULONG)getBX());

    switch (iStdHandle) {

        case HANDLE_STDIN:

	    if (GetFileType(pRdrInfo->ri_hStdIn) == FILE_TYPE_PIPE) {
		if (!cmdHandleStdinWithPipe (pRdrInfo)) {
		    RcErrorDialogBox(EG_MALLOC_FAILURE, NULL, NULL);
		    TerminateVDM();
		    setCF(1);
		    return;
		}
		setCX ((USHORT)pRdrInfo->ri_hStdInFile);
		setBX ((USHORT)((ULONG)pRdrInfo->ri_hStdInFile >> 16));
	    }
	    else {
		setCX ((USHORT)pRdrInfo->ri_hStdIn);
		setBX ((USHORT)((ULONG)pRdrInfo->ri_hStdIn >> 16));
	    }
	    break;

	case HANDLE_STDOUT:
	    if (GetFileType (pRdrInfo->ri_hStdOut) == FILE_TYPE_PIPE){
		if (!cmdHandleStdOutErrWithPipe(pRdrInfo, HANDLE_STDOUT)) {
		    RcErrorDialogBox(EG_MALLOC_FAILURE, NULL, NULL);
		    TerminateVDM();
		    setCF(1);
		    return;
		}
		setCX ((USHORT)pRdrInfo->ri_hStdOutFile);
		setBX ((USHORT)((ULONG)pRdrInfo->ri_hStdOutFile >> 16));

	    }
	    else {
		// sudeepb 16-Mar-1992; This will be a compatibilty problem.
		// If the user gives the command "dosls > lpt1" we will
		// inherit the 32 bit handle of lpt1, so the ouput will
		// directly go to the LPT1 and a DOS TSR/APP hooking int17
		// wont see this printing. Is this a big deal???
		setCX ((USHORT)pRdrInfo->ri_hStdOut);
		setBX ((USHORT)((ULONG)pRdrInfo->ri_hStdOut >> 16));
	    }
	    break;

	case HANDLE_STDERR:

            if (pRdrInfo->ri_hStdErr == pRdrInfo->ri_hStdOut
                              && pRdrInfo->ri_hStdOutFile != 0) {
                setCX ((USHORT)pRdrInfo->ri_hStdOutFile);
                setBX ((USHORT)((ULONG)pRdrInfo->ri_hStdOutFile >> 16));
                pRdrInfo->ri_hStdErrFile = pRdrInfo->ri_hStdOutFile;
		break;
	    }

	    if (GetFileType (pRdrInfo->ri_hStdErr) == FILE_TYPE_PIPE){
		if(!cmdHandleStdOutErrWithPipe(pRdrInfo, HANDLE_STDERR)) {
		    RcErrorDialogBox(EG_MALLOC_FAILURE, NULL, NULL);
		    TerminateVDM();
		    setCF(1);
		    return;
		}
                setCX ((USHORT)pRdrInfo->ri_hStdErrFile);
                setBX ((USHORT)((ULONG)pRdrInfo->ri_hStdErrFile >> 16));
	    }
	    else {
                setCX ((USHORT)pRdrInfo->ri_hStdErr);
                setBX ((USHORT)((ULONG)pRdrInfo->ri_hStdErr >> 16));
	    }
	    break;
    }
    setAX(0);
    setDX(0);
    setCF(0);
    return;
}

BOOL cmdHandleStdOutErrWithPipe(
    PREDIRCOMPLETE_INFO pRdrInfo,
    USHORT  HandleType
    )
{

    HANDLE  hFile;
    PCHAR   pFileName;
    PPIPE_OUTPUT pPipe;
    BYTE    *Buffer;
    DWORD   ThreadId;
    HANDLE  hEvent;
    HANDLE  hFileWrite;
    HANDLE  hThread;

    if(!cmdCreateTempFile(&hFile,&pFileName))
	return FALSE;
    // must have a different handle so that writter(dos app) and reader(us)
    // wont use the same handle object(especially, file position)
    hFileWrite = CreateFile(pFileName,
			    GENERIC_WRITE | GENERIC_READ,
			    FILE_SHARE_READ | FILE_SHARE_WRITE,
			    NULL,
			    OPEN_EXISTING,
			    FILE_ATTRIBUTE_TEMPORARY,
			    NULL
			   );
    if (hFileWrite == INVALID_HANDLE_VALUE) {
	CloseHandle(hFile);
	DeleteFile(pFileName);
	return FALSE;
    }
    Buffer = malloc(sizeof(PIPE_OUTPUT) + PIPE_OUTPUT_BUFFER_SIZE);
    if (Buffer == NULL) {
	CloseHandle(hFile);
	CloseHandle(hFileWrite);
	DeleteFile(pFileName);
	return FALSE;
    }
    pPipe = (PPIPE_OUTPUT)Buffer;
    pPipe->Buffer = Buffer + sizeof(PIPE_OUTPUT);
    pPipe->BufferSize = PIPE_OUTPUT_BUFFER_SIZE;
    pPipe->hFile = hFileWrite;
    pPipe->pFileName = pFileName;
    pPipe->hExitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (pPipe->hExitEvent == NULL) {
	CloseHandle(hFile);
	CloseHandle(hFileWrite);
	DeleteFile(pFileName);
	free(pPipe);
	return FALSE;
    }

    if (HandleType == HANDLE_STDOUT) {
	pPipe->hPipe = pRdrInfo->ri_hStdOut;
	pRdrInfo->ri_pPipeStdOut = pPipe;
	pRdrInfo->ri_hStdOutFile = hFile;

    }
    else {
	pPipe->hPipe = pRdrInfo->ri_hStdErr;
	pRdrInfo->ri_pPipeStdErr = pPipe;
	pRdrInfo->ri_hStdErrFile = hFile;

    }
    hThread = CreateThread ((LPSECURITY_ATTRIBUTES)NULL,
			    (DWORD)0,
			    (LPTHREAD_START_ROUTINE)cmdPipeOutThread,
			    (LPVOID)pPipe,
			    0,
			    &ThreadId
			    );
    if (hThread == NULL) {
	CloseHandle(pPipe->hExitEvent);
	CloseHandle(hFileWrite);
	CloseHandle(hFile);
	DeleteFile(pFileName);
	free(Buffer);
	return FALSE;
    }
    if (HandleType == HANDLE_STDOUT)
	pRdrInfo->ri_hStdOutThread = hThread;
    else
	pRdrInfo->ri_hStdErrThread = hThread;
    return TRUE;
}

/* independent thread to read application stdout(file) to NTVDM stdout(PIPE).
   The CPU thread would notify us through hExitEvent when the application
   is terminating(thus, we can detect EOF and exit
 */

VOID  cmdPipeOutThread(LPVOID lpParam)
{
    PPIPE_OUTPUT pPipe;
    DWORD	 BytesRead;
    DWORD	 BytesWritten;
    BOOL	 ExitPending;

    pPipe = (PPIPE_OUTPUT)lpParam;

    ExitPending = FALSE;

    while(ReadFile(pPipe->hFile, pPipe->Buffer, pPipe->BufferSize, &BytesRead, NULL) ) {
	// go nothing doesn't mean it hits EOF!!!!!!
	// we can not just exit now, instead, we have to wait and poll
	// until the application is terminated.
	//
	if (BytesRead == 0) {
	    // if read nothing and the application is gone, we can quit now
	    if (ExitPending)
		break;
	    if (!WaitForSingleObject(pPipe->hExitEvent, PIPE_OUTPUT_TIMEOUT))
		ExitPending = TRUE;
	}
	else {
	    if (!WriteFile(pPipe->hPipe, pPipe->Buffer, BytesRead, &BytesWritten, NULL) ||
		BytesWritten != BytesRead)
		break;
	}
    }
    // if we were out of loop because of errors, wait for the cpu thread.
    if (!ExitPending)
	WaitForSingleObject(pPipe->hExitEvent, INFINITE);

    CloseHandle(pPipe->hFile);
    CloseHandle(pPipe->hPipe);
    CloseHandle(pPipe->hExitEvent);
    DeleteFile(pPipe->pFileName);
    free(pPipe->pFileName);
    free(pPipe);
    ExitThread(0);
}

BOOL cmdHandleStdinWithPipe (
    PREDIRCOMPLETE_INFO pRdrInfo
    )
{

    HANDLE  hStdinFile;
    PCHAR   pStdinFileName;
    PPIPE_INPUT pPipe;
    BYTE    *Buffer;
    DWORD   ThreadId;
    HANDLE  hEvent;
    HANDLE  hFileWrite;

    if(!cmdCreateTempFile(&hStdinFile,&pStdinFileName))
	return FALSE;


    // must have a different handle so that reader(dos app) and writter(us)
    // wont use the same handle object(especially, file position)
    hFileWrite = CreateFile(pStdinFileName,
			    GENERIC_WRITE | GENERIC_READ,
			    FILE_SHARE_READ | FILE_SHARE_WRITE,
			    NULL,
			    OPEN_EXISTING,
			    FILE_ATTRIBUTE_TEMPORARY,
			    NULL
			   );
    if (hFileWrite == INVALID_HANDLE_VALUE) {
	CloseHandle(hStdinFile);
	DeleteFile(pStdinFileName);
	return FALSE;
    }
    Buffer = malloc(sizeof(PIPE_INPUT) + PIPE_INPUT_BUFFER_SIZE);
    if (Buffer == NULL) {
	CloseHandle(hStdinFile);
	CloseHandle(hFileWrite);
	DeleteFile(pStdinFileName);
	return FALSE;
    }
    hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (hEvent == NULL) {
	CloseHandle(hStdinFile);
	CloseHandle(hFileWrite);
	DeleteFile(pStdinFileName);
	free(Buffer);
	return FALSE;
    }
    pPipe = (PPIPE_INPUT)Buffer;
    pPipe->Buffer = Buffer + sizeof(PIPE_INPUT);
    pPipe->BufferSize = PIPE_INPUT_BUFFER_SIZE;
    pPipe->fEOF = FALSE;
    pPipe->hFileWrite = hFileWrite;
    pPipe->hFileRead  = hStdinFile;
    pPipe->hDataEvent = hEvent;
    pPipe->hPipe = pRdrInfo->ri_hStdIn;
    pPipe->pFileName = pStdinFileName;
    InitializeCriticalSection(&pPipe->CriticalSection);
    pPipe->hThread = CreateThread ((LPSECURITY_ATTRIBUTES)NULL,
			       (DWORD)0,
			       (LPTHREAD_START_ROUTINE)cmdPipeInThread,
			       (LPVOID)pPipe,
			       0,
			       &ThreadId
			      );
    if (pPipe->hThread == NULL) {
	CloseHandle(hFileWrite);
	CloseHandle(pPipe->hDataEvent);
	CloseHandle(hStdinFile);
	DeleteFile(pStdinFileName);
	free(Buffer);
	return FALSE;
    }
    // always have the new node in the head of the list because
    // it is the node used by the top command.com running in the process.
    // We may have multiple command.com instances running in the same
    // ntvdm proecess and each command.com has a private PREDIRCOMPLETE_INFO
    // associated with it if its stdin is redirected to a pipe.
    pPipe->Next = cmdPipeList;
    cmdPipeList = pPipe;
    pRdrInfo->ri_hStdInFile = hStdinFile;
    pRdrInfo->ri_pPipeStdIn = pPipe;
    return TRUE;
}

/* Independent thread to read from pipe(NTVDM STDIN) and write to
   file(DOS application STDIN) until either the pipe is broken or
   there are some errors.
   This thread may never terminate itself because it can block
   in the ReadFile call to the pipe forever. If this is the case,
   we have to rely on the CPU thread to kill it. To allow the CPU
   thread safely launching the killing, this thread yields the
   critical section when it is safe to be killed and the CPU thread
   would claim the critical section first before going for kill.
 */

VOID cmdPipeInThread(LPVOID lpParam)
{
    PPIPE_INPUT pPipe;
    DWORD	BytesRead, BytesWritten;
    BOOL	ReadStatus, WriteStatus;
    BOOL	ApplicationTerminated, fEOF;

    pPipe = (PPIPE_INPUT)lpParam;
    while (TRUE) {

	// this read can take forever without getting back anything
	ReadStatus = ReadFile(pPipe->hPipe, pPipe->Buffer,
			      pPipe->BufferSize, &BytesRead, NULL);

	// claim the critical section so we won't get killed
	// by the CPU thread
	EnterCriticalSection(&pPipe->CriticalSection);
	if (ReadStatus) {
	    if (BytesRead != 0) {
		WriteStatus = WriteFile(pPipe->hFileWrite,
					pPipe->Buffer,
					BytesRead,
					&BytesWritten,
					NULL
					);
		if (pPipe->WaitData && WriteStatus && BytesWritten != 0)
		    SetEvent(pPipe->hDataEvent);
	    }
	}
	else {
	    if (GetLastError() == ERROR_BROKEN_PIPE) {

		// pipe is broken and more data to read?
		ASSERT(BytesRead == 0);
		pPipe->fEOF = TRUE;
		LeaveCriticalSection(&pPipe->CriticalSection);
		break;
	    }
	}
	// as soon as we leave the critical seciton, the CPU thread may
	// step in and kill us
	LeaveCriticalSection(&pPipe->CriticalSection);
    }
    ExitThread(0);
}

/* cmdPipeFileDataEOF - Check for new data or EOF
 *
 *
 *  Entry - hFile, DOS application STDIN file handle(file)
 *	    &fEOF, to return if the pipe is broken
 *  EXIT  - TRUE if either there are new data or EOF is true
 *	    *fEOF == TRUE if EOF
 */

BOOL cmdPipeFileDataEOF(HANDLE hFile, BOOL *fEOF)
{
    PPIPE_INPUT pPipe;
    BOOL	NewData;
    DWORD	WaitStatus;

    pPipe = cmdPipeList;
    while (pPipe != NULL && pPipe->hFileRead != hFile)
	pPipe = pPipe->Next;

    NewData = TRUE;
    *fEOF = TRUE;

    if (pPipe != NULL) {
	EnterCriticalSection(&pPipe->CriticalSection);
	*fEOF = pPipe->fEOF;
	if (!(*fEOF)) {
	    pPipe->WaitData = TRUE;
	    LeaveCriticalSection(&pPipe->CriticalSection);
	    WaitStatus = WaitForSingleObject(pPipe->hDataEvent, PIPE_INPUT_TIMEOUT);
	    EnterCriticalSection(&pPipe->CriticalSection);
	    *fEOF = pPipe->fEOF;
	    pPipe->WaitData = FALSE;
	    NewData = WaitStatus == 0 ? TRUE : FALSE;
	}
	LeaveCriticalSection(&pPipe->CriticalSection);
    }
    return(NewData || *fEOF);
}

/* cmdPipeFileEOF - Check if the pipe is broken
 *
 *
 *  Entry - hFile, DOS application STDIN file handle(file)
 *
 *  EXIT  - TRUE if the write end of the pipe is closed
 */


BOOL cmdPipeFileEOF(HANDLE hFile)
{
    PPIPE_INPUT pPipe;
    BOOL       fEOF;

    pPipe = cmdPipeList;
    while (pPipe != NULL && pPipe->hFileRead != hFile)
	pPipe = pPipe->Next;

    fEOF = TRUE;

    if (pPipe != NULL) {
	EnterCriticalSection(&pPipe->CriticalSection);
	fEOF = pPipe->fEOF;
	LeaveCriticalSection(&pPipe->CriticalSection);
    }
    if (!fEOF) {
	Sleep(PIPE_INPUT_TIMEOUT);
	EnterCriticalSection(&pPipe->CriticalSection);
	fEOF = pPipe->fEOF;
	LeaveCriticalSection(&pPipe->CriticalSection);
    }
    return (fEOF);
}