/*------------------------------------------------------------------------

 Module:        D:\NLCC\PIPE.C

 Author:        JACOB

 Project:

 State:

 Creation Date:

 Description:   This file implements a pipe between the preprocessor

                and the compiler proper for the OS2 system.

                The preprocessor 'main' function has to be modified to be called

                'cpp', a function that will receive the original

                command line arguments. (The variables 'Savedargc' and

                'Savedargv'). The preprocessor should use for output

                the function WriteToLcc, implemented below. In a

                similar fashion, the compiler should use for input the

                function 'ReadFromCpp', instead of reading from a

                file.

                Before any of those are used, the function OpenPipe

                should be called. That function assumes that the

                globals with 'Savedargc' and 'Savedargv are already

                set.

                The implementation starts a thread for the cpp

                process, and an anonymous pipe for communication

                between the two threads.

				The code of the preprocessor is linked with the rest of the

				compiler.

                COPYRIGHT.

                This code is copyrighted by the author. If you use it

                without sending the author a huge amount of money, the

                gods will never forgive you such a sin, and you will

                rejoin the author in hell... :-)

                Jacob Navia

                Logiciels/Informatique

                41 rue Maurice Ravel

                93430 Villetaneuse

                France.

------------------------------------------------------------------------*/

#include <windows.h>

#include <io.h>

extern int Savedargc;

extern char **Savedargv;

static SECURITY_ATTRIBUTES saAttr;

static HANDLE ReadHandleCpp,WriteHandleCpp,ReadHandleAsm,WriteHandleAsm;

static HANDLE HandleAsm;

extern int Cpp(int,char **);

unsigned long threadIdAsm;

int StartCpp(unsigned long ignore)

{

//	return(Cpp(Savedargc,Savedargv));

}



void OpenPipe(int which)

{

	unsigned long threadIdCpp;



	if (which == 0) {

	    CreatePipe(&ReadHandleCpp, &WriteHandleCpp, NULL, 0);

		CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartCpp,(LPVOID)0,0,&threadIdCpp);

	}

//	SetThreadPriority(threadId,THREAD_PRIORITY_ABOVE_NORMAL);



}

#if 0

int ReadFromCpp(char *buf,int siz)

{

	ULONG br,e;



	if (ReadHandleCpp == (HANDLE)0) return(0);

	br = 0;

	if (ReadFile(ReadHandleCpp,buf,siz,&br,NULL) == 0) {

		e = GetLastError();

	}

	return(br);

}

#endif

int WriteToLcc(char *buf,int siz)

{

	ULONG br,e;



	if (WriteHandleCpp == (HANDLE)0) return(0);

	br = 0;

	if (WriteFile(WriteHandleCpp,buf,siz,&br,NULL) == 0) {

		e = GetLastError();

	}

	return(br);

}

void ClosePipe(int which)

{

	if (which == 0) {

		CloseHandle(WriteHandleCpp);

		WriteHandleCpp = (HANDLE)0;

	}

}



