Re: Forth stdin and stdout
- From: jihtungpai@xxxxxxxxx
- Date: Fri, 16 Jan 2009 22:34:21 -0800 (PST)
On Jan 17, 5:14 am, m...@xxxxxx (Marcel Hendrix) wrote:
jihtung...@xxxxxxxxx writes Re: Forth stdin and stdout
I have a Chinese chess program that I would like to plug into a
graphic interface through UCCI protocol, which takes stdin and stdout.
I know western chess UCI protocol also use the stdin and stdout as
communication channel. So far, I have some troubles doing it in
Win32for and iForth. I wonder if someone have experience in this area
in any flavor of Forth that can be shared.
This is very awkward in iForth for Windows (but also other Forths, as
you may have noticed from the answers up to now).
I did something along the lines you want for the Winboard graphical
chessboard display and GUI under windows:
see ./include/ichess.prf (files winboard.frt etc.) Please consult
the notes in the ./dfwforth/bugs.frt file on redirection. You need
a commandline switch to turn on redirection: #10 (input), #01 (output),
or #11 (both input and output).
( I have found that for most problems you need both the commands piped
from the input, and programmed input from a Forth scripts intermixed.
Simple redirection is normally not sufficient. )
Here's how to to use printf() from iForth. Put the following in a
file c:\input.frt :
-- ---------------------------------------------------------------------------
NEEDS -dynlink
: TEST-FPRINTF2 ( -- )
0 0 0 LOCALS| dll 'printf |
S" msvcrt.dll" LIBRARY-OPEN ABORT" LIBRARY-OPEN failed" TO dll
S" printf" dll LIBRARY-FIND ABORT" LIBRARY-FIND failed" TO 'printf
S\" Number: %d\n" DROP 1234 2 'printf VFOREIGN
S\" Number: %g\n" DROP PI FPUSHD 3 'printf VFOREIGN
S\" Number: float %g, int %d\n" DROP PI FPUSHD 73823 4 'printf VFOREIGN
dll LIBRARY-CLOSE ABORT" LIBRARY-CLOSE failed" ;
TEST-FPRINTF2
BYE
-- ----------------------------------------------------------------------------
Then call iForth as follows:
C:>/dfwforth/ifwinnt/bin/iwserver #01 include iforth.prf include c:\input..frt >test.txt
Note the "#01". Without enabled output redirection, printf()'s result
will end in a Windows bitbucket somewhere.
Also note that floating-point arguments are put on the stack with FPUSHD ..
Pay attention to the S\" ..." strings. With S" ..\n" the printf() outputs
something won't find useful.
To use fprintf(), first create a valid FILE* with still other C library
functions. The C library apparently gets confused with stdout, stdin and stderr,
and the common "small-integer == file" tricks won't work. The problem is even
more severe because the iForth commandline is a parallel thread detached from
the main executable (it's input gets redirected twice).
The other approach I am trying is to use the C function calls "fgets"
and "fprintf" from dynamic link library. However, I have troubles
figuring out how to put the parameters on the stack for "fprintf". I
know how to prepare parameters for Win32for and iForth if they are
integer or address. The "fprintf" and "printf" parameters just puzzle
me. Take the following examples:
printf("Number: %d", 1234);
fprintf(stdout,"Number: %d", 1234);
What will be the parameters arrangement on the stack in Win32for or
iForth?
This is demonstrated above, but note that "stdout" will never work
for the Windows iForth. I suggest to create a valid FILE* to use here.
I warn against trying to get smart with fdopen() -- I wasted a few
fruitless hours with it already.
One related question: how do you pass floating number parameter?
Demonstrated above, use help FPUSHD for explanations.
-marcel
Hi Marcel,
With redirection switch #01 and #11, I was able to make redirection
work and save output to a file as the following example:
----
C:\dfwforth\ifwinnt\bin\iwserver.exe #01 include ichess.prf bye >
test.txt
---
The content if ichess.prf is:
---
pad 4 expect pad 4 type
cr .( id name CChess by Pai)
cr .( id author Pai)
---
It seems the iForth output can be redirected to stdout without
problem. However, I notice in one open source chess program, the
following function has to be executed before the communication through
stdin and stdout will work with UCCI GUI program:
// util_init()
void util_init() {
setvbuf(stdin,NULL,_IONBF,0);
setvbuf(stdout,NULL,_IONBF,0); // _IOLBF breaks on Windows!
}
Otherwise, the communication will hang there until time out. Judging
from the log file, the messages are never received by both sides due
to buffering.
It looks I need to use "No buffering" for stdin and stdout in order
for this to work. The problem is: what is the handle for stdin and
stdout in iForth so I can perform "setvbuf". I could not understand
your statments:
To use fprintf(), first create a valid FILE* with still other C library
functions. The C library apparently gets confused with stdout, stdin and stderr,
and the common "small-integer == file" tricks won't work.
This is demonstrated above, but note that "stdout" will never work
for the Windows iForth. "I suggest to create a valid FILE* to use here." <---
My question is how do I obtain the handle for stdout and stdin so I
can use "setvbuf" to set them to "No buffering?"
Best,
Pai
.
- Follow-Ups:
- Re: Forth stdin and stdout
- From: Marcel Hendrix
- Re: Forth stdin and stdout
- References:
- Re: Forth stdin and stdout
- From: jihtungpai
- Re: Forth stdin and stdout
- From: Marcel Hendrix
- Re: Forth stdin and stdout
- Prev by Date: Gforth ec mach.fs constants
- Next by Date: Re: Forth stdin and stdout
- Previous by thread: Re: Forth stdin and stdout
- Next by thread: Re: Forth stdin and stdout
- Index(es):
Relevant Pages
|