Index: tools/pipetools/piperead.cpp =================================================================== --- tools/pipetools/piperead.cpp (revision 45247) +++ tools/pipetools/piperead.cpp (working copy) @@ -2,14 +2,11 @@ // piperead.cpp // // Martin Fuchs, 30.11.2003 -// +// +// Jan Roeloffzen, 26.1.2010 +// Pipe client, based on msdn example + -// -// Invoke as: "piperead [pipe_name]", -// for example: "piperead com_1" -// - - #define WIN32_LEAN_AND_MEAN #include #include @@ -21,24 +18,15 @@ #endif +#define BUFSIZE 1024 + static void print_error(DWORD win32_error) { fprintf(stderr, "WIN32 error %lu\n", win32_error); } - -int main(int argc, char** argv) +static int pipeServer(char *path) { - char path[MAX_PATH]; - const char* pipe_name; - - if (argc > 1) - pipe_name = *++argv; - else - pipe_name = "com_1"; - - sprintf(path, "\\\\.\\pipe\\%s", pipe_name); - HANDLE hPipe = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX|FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_WAIT|PIPE_TYPE_BYTE, 1, 4096, 4096, 30000, NULL); if (hPipe == INVALID_HANDLE_VALUE) { @@ -48,23 +36,26 @@ for(;;) { DWORD read; - BYTE buffer[1024]; + BYTE buffer[BUFSIZE]; if (!ReadFile(hPipe, buffer, sizeof(buffer), &read, NULL)) { DWORD error = GetLastError(); - if (error == ERROR_PIPE_LISTENING) + if (error == ERROR_PIPE_LISTENING) { + fprintf(stderr,"INVALID_HANDLE_VALUE\n"); Sleep(1000); - else if (error == ERROR_BROKEN_PIPE) { + } else if (error == ERROR_BROKEN_PIPE) { CloseHandle(hPipe); hPipe = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX|FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_WAIT|PIPE_TYPE_BYTE, 1, 4096, 4096, 30000, NULL); if (hPipe == INVALID_HANDLE_VALUE) { + fprintf(stderr,"INVALID_HANDLE_VALUE\n"); print_error(GetLastError()); return 1; } } else { + fprintf(stderr,"error %lu\n",error); print_error(error); break; } @@ -79,3 +70,132 @@ return 0; } + +static int pipeClient(char *path) +{ + HANDLE hPipe=INVALID_HANDLE_VALUE; + TCHAR chBuf[BUFSIZE]; + BOOL fSuccess = FALSE; + DWORD cbRead; + DWORD Err; + int res = 0; + + setvbuf(stdout, NULL, _IONBF, 0); + while (1) { + hPipe = CreateFile( + path, // pipe name + GENERIC_READ, + 0, // no sharing + NULL, // default security attributes + OPEN_EXISTING, // opens existing pipe + 0, // default attributes + NULL); // no template file + + // Break if the pipe handle is valid. + if (hPipe != INVALID_HANDLE_VALUE) + break; + + // Exit if an error other than ERROR_PIPE_BUSY occurs. + if (GetLastError() != ERROR_PIPE_BUSY) { + fprintf(stderr,"Could not open pipe. Error=%lu\n", GetLastError() ); + res = -1; + break; + } + + // All pipe instances are busy, so wait for 20 seconds. + if ( ! WaitNamedPipe(path, 20000)) { + fprintf(stderr,"Could not open pipe: 20 second wait timed out."); + res = -2; + break; + } + } + + if (!res) do { + fSuccess = ReadFile( + hPipe, // pipe handle + chBuf, // buffer to receive reply + BUFSIZE, // size of buffer + &cbRead, // number of bytes read + NULL); // not overlapped + + if ( ! fSuccess ) { + Err = GetLastError(); + if ( Err == ERROR_MORE_DATA ) { + fSuccess = TRUE; + } else { + fprintf(stderr, "ReadFile: Error %lu \n", Err ); + res = -9; + break; + } + } + + fwrite(chBuf,1,cbRead,stdout); + } while ( fSuccess); + + if ( ! fSuccess) { + fprintf(stderr, "ReadFile from pipe failed. Error=%lu\n", GetLastError() ); + res = -5; + } + + if (hPipe != INVALID_HANDLE_VALUE) + CloseHandle(hPipe); + + return res; + +} + +void usage(void) +{ + fprintf(stderr, "Usage: piperead [-c] \n"); + fprintf(stderr, "-c means Client mode\n"); + fprintf(stderr, "Example: piperead -c \\\\.\\pipe\\kdbg | log2lines -c\n\n"); +} + +int main(int argc, char** argv) +{ + char path[MAX_PATH]; + const char* pipe_name; + const char* clientMode; + int res = 0; + + pipe_name = "com_1"; + clientMode = NULL; + switch (argc) { + case 3: + clientMode = *++argv; + if (strcmp(clientMode,"-c") != 0) { + clientMode = NULL; + fprintf(stderr,"Invalid option: %s\n", clientMode); + res = -6; + } + //fall through + case 2: + pipe_name = *++argv; + if (strcmp(pipe_name,"-h") == 0) { + res = -7; + } + break; + default: + res = -8; + break; + } + if (res) { + usage(); + return res; + } + + if ( pipe_name[0] == '\\' ) { + //assume caller specified full path + sprintf(path, "%s", pipe_name); + } else { + sprintf(path, "\\\\.\\pipe\\%s", pipe_name); + } + + if ( clientMode ) { + res = pipeClient(path); + } else { + res = pipeServer(path); + } + + return res; +} Index: tools/pipetools/pipetools.mak =================================================================== --- tools/pipetools/pipetools.mak (revision 0) +++ tools/pipetools/pipetools.mak (revision 0) @@ -0,0 +1,47 @@ +PIPETOOLS_BASE = $(TOOLS_BASE)$(SEP)pipetools +PIPETOOLS_BASE_ = $(PIPETOOLS_BASE)$(SEP) +PIPETOOLS_INT = $(INTERMEDIATE_)$(PIPETOOLS_BASE) +PIPETOOLS_INT_ = $(PIPETOOLS_INT)$(SEP) +PIPETOOLS_OUT = $(OUTPUT_)$(PIPETOOLS_BASE) +PIPETOOLS_OUT_ = $(PIPETOOLS_OUT)$(SEP) + +$(PIPETOOLS_INT): | $(TOOLS_INT) + $(ECHO_MKDIR) + ${mkdir} $@ + +ifneq ($(INTERMEDIATE),$(OUTPUT)) +$(PIPETOOLS_OUT): | $(TOOLS_OUT) + $(ECHO_MKDIR) + ${mkdir} $@ +endif + + +PIPETOOLS_TARGET = \ + $(PIPETOOLS_OUT_)piperead$(EXEPOSTFIX) + +PIPETOOLS_SOURCES = \ + $(PIPETOOLS_BASE_)piperead.cpp + +PIPETOOLS_OBJECTS = \ + $(addprefix $(INTERMEDIATE_), $(PIPETOOLS_SOURCES:.cpp=.o)) + +PIPETOOLS_HOST_CFLAGS = $(TOOLS_CFLAGS) + +PIPETOOLS_HOST_LFLAGS = $(TOOLS_LFLAGS) + +.PHONY: pipetools +pipetools: $(PIPETOOLS_TARGET) + +$(PIPETOOLS_TARGET): $(PIPETOOLS_OBJECTS) | $(PIPETOOLS_OUT) + $(ECHO_HOSTLD) + ${host_gcc} $(PIPETOOLS_OBJECTS) $(PIPETOOLS_HOST_LFLAGS) -o $@ + +$(PIPETOOLS_INT_)piperead.o: $(PIPETOOLS_BASE_)piperead.cpp | $(PIPETOOLS_INT) + $(ECHO_HOSTCC) + ${host_gcc} $(PIPETOOLS_HOST_CFLAGS) -c $< -o $@ + + +.PHONY: pipetools_clean +pipetools_clean: + -@$(rm) $(PIPETOOLS_TARGET) $(PIPETOOLS_OBJECTS) 2>$(NUL) +clean: pipetools_clean Index: tools/tools.mak =================================================================== --- tools/tools.mak (revision 45247) +++ tools/tools.mak (working copy) @@ -44,6 +44,7 @@ include tools/buildno/buildno.mak include tools/gendib/gendib.mak include tools/rsym/log2lines.mak +include tools/pipetools/pipetools.mak include tools/nci/nci.mak ifeq ($(ARCH),powerpc) include tools/ofw_interface/ofw_interface.mak