<div dir="ltr">Foiled again by the google default of reply-only-to-one-person!<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Wesley W. Terpstra</b> <span dir="ltr"><<a href="mailto:wesley@terpstra.ca">wesley@terpstra.ca</a>></span><br>
Date: Mon, Sep 15, 2008 at 4:04 PM<br>Subject: Re: [MLton-user] Linking (?) 'print' means I can only run from the mingw command line<br>To: Vesa Karvonen <<a href="mailto:vesa.a.j.k@gmail.com">vesa.a.j.k@gmail.com</a>><br>
<br><br><div dir="ltr"><div><div class="Ih2E3d">On Mon, Sep 15, 2008 at 3:36 PM, Vesa Karvonen <span dir="ltr"><<a href="mailto:vesa.a.j.k@gmail.com" target="_blank">vesa.a.j.k@gmail.com</a>></span> wrote:<br><div class="gmail_quote">
<blockquote class="gmail_quote" style="border-left:1px solid rgb(204, 204, 204);margin:0pt 0pt 0pt 0.8ex;padding-left:1ex">
On Mon, Sep 15, 2008 at 2:30 PM, Wesley W. Terpstra <<a href="mailto:wesley@terpstra.ca" target="_blank">wesley@terpstra.ca</a>> wrote:<br>
[...]<br>
<div>> I tend towards option #2. stdin&out can come from openIn/Out "NUL". However,<br>
> if stderr is missing one loses exception information. Perhaps it could<br>
> create a popup window. A TextPrimIO.writer that creates a window and adds<br>
> text on write* calls shouldn't be that hard to implement.<br>
<br>
</div>BTW, this issue was also underlying the thread starting here:<br>
<br>
<a href="http://mlton.org/pipermail/mlton/2007-June/029811.html" target="_blank">http://mlton.org/pipermail/mlton/2007-June/029811.html</a><br>
<br>
Basically, some code was failing and (IIRC) then the program went into<br>
an infinite memory hogging loop, because the default handler couldn't<br>
write to stderr.<br>
</blockquote></div><br></div>It seems a pretty easy failure mode affecting people compiling with '-mwindows'. I did a bit of hacking in C to see how it could work to create a console on demand and link stdin/out/err to it. I figure you could just ShowWindow when a write happens.<br>
<br></div><blockquote style="border-left:1px solid rgb(204, 204, 204);margin:0pt 0pt 0pt 0.8ex;padding-left:1ex" class="gmail_quote">#define _WIN32_WINNT 0x0500
<br>#include <io.h>
<br>#include <fcntl.h>
<br>#include <stdint.h>
<br>#include <windows.h>
<br>#include <stdio.h>
<br> <br>int main() {
<br> HWND console;
<br> BOOL existed;
<br> int in, out, err;
<br>
<br> existed = fileno(stdout) == 1;
<br> <br> if (!existed) {
<br> AllocConsole();
<br>
<br> console = GetConsoleWindow();
<br> // ShowWindow(console, SW_HIDE);
<br> <br> in = _open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
<br> out = _open_osfhandle((intptr_t)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
<br> err = _open_osfhandle((intptr_t)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT);
<br> dup2(in, 0); if (in > 0) close(in); stdin ->_file = 0;
<br> dup2(out, 1); if (out > 1) close(out); stdout->_file = 1;
<br> dup2(err, 2); if (err > 2) close(err); stderr->_file = 2;
<br> setvbuf(stdin, NULL, _IONBF, 0);
<br> setvbuf(stdout, NULL, _IOLBF, 4096);
<br> setvbuf(stderr, NULL, _IONBF, 0);
<br> }
<br> <br> printf("mooo!\n");
<br> fflush(stdout);
<br> write(1, "baaa!\n", 6);
<br> <br> MessageBoxA(0, "Click.", "title", 0);
<br> <br> return 0;
<br>}
<br></blockquote><div><br>This code will only create a new console if needed. That means it will use the existing console of msys even if -mwindows. However, if -mwindows and run from explorer or command-line, it creates a console.<br>
<br>So if someone wants this to work in MLton, that code should probably be glued into some MLton_initStdio() method that gets called in the SML-side construction of stdOut/Err/In. That way it's only run if you actual might be using stdio. Somehow it also needs to be shown once a write occurs. I'm not sure how to do this since linked C code might also printf.<br>
</div></div>
</div><br></div>