www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Windows and piped stdin...?

reply alex <alex alexi.ch> writes:
Hi,

Perhaps this is a common known problem, but I have really no idea what it could
be.

Take this piece of code (say, main.d):

import std.stdio;
import std.cstream;

int main(char[][] args) {
	while (!din.eof()) {
		writefln("in: %s", din.readLine());
	}
  return 0;
}

This just reads from standard input and outputs until stdin ends.

It seems that Windows does never send an EOF when using like this:

do_some_output | main.exe

The output will never stop printing "in: "

The same program works fine under every flavor of Linux..... It is even working
when using like this in Windows:

main.exe < some_input_file.txt

Some idea what this could be? Is it possible that Windows won't send a EOF on
pipes....?

thanks

alex
Feb 14 2007
parent reply alex <alex alexi.ch> writes:
No, sorry, it is NEVER working, not with "main.exe < input file" nor with just
"main.exe" and putting some text manually in (end with ^Z).... It really seems
that no EOF is sent on windows stdin....?

any ideas?

alex
Feb 14 2007
parent Jascha Wetzel <"[firstname]" mainia.de> writes:
the problem appears to be in the c runtime that's used via std.c.stdio.
i worked around this by using win32 api directly:

/**
 * Wraps win32 stdio functions. Respects io redirection into pipes.
 */
class DbgIO
{
	static HANDLE ddb_read, ddb_write;
	static char[] buffer;

	static this()
	{
		ddb_read = GetStdHandle(STD_INPUT_HANDLE);
		ddb_write = GetStdHandle(STD_OUTPUT_HANDLE);
	}

	/**
	 *
	 */
	static char[] readln()
	{
		char[128] buf;
		uint read;

		int nl_index;
		do
		{
			ReadFile(ddb_read, buf.ptr, buf.length, &read, null);
			buffer ~= buf[0..read];
			nl_index = find(buffer, '\n');
		} while ( nl_index < 0 );
		char[] ret = buffer[0..nl_index];
		buffer = buffer[nl_index+1..$];
		return ret;
	}

	/**
	 *
	 */
	static void print(...)
	{
		char[] str;

		void putc(dchar c)
		{
			str ~= c;
		}

		doFormat(&putc, _arguments, _argptr);
		uint written;
		WriteFile(ddb_write, str.ptr, str.length, &written, null);
	}

	/**
	 *
	 */
	static void println(...)
	{
		char[] str;

		void putc(dchar c)
		{
			str ~= c;
		}

		doFormat(&putc, _arguments, _argptr);
		uint written;
		str ~= '\n';
		WriteFile(ddb_write, str.ptr, str.length, &written, null);
	}
}


alex wrote:
 No, sorry, it is NEVER working, not with "main.exe < input file" nor with just
"main.exe" and putting some text manually in (end with ^Z).... It really seems
that no EOF is sent on windows stdin....?
 
 any ideas?
 
 alex

Feb 18 2007