Re: Forth stdin and stdout
- From: mhx@xxxxxx (Marcel Hendrix)
- Date: Fri, 16 Jan 2009 23:14:38 +0200
jihtungpai@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
.
- Follow-Ups:
- Re: Forth stdin and stdout
- From: jihtungpai
- Re: Forth stdin and stdout
- References:
- Re: Forth stdin and stdout
- From: jihtungpai
- Re: Forth stdin and stdout
- Prev by Date: Re: nibz processor
- Next by Date: F-PC: Writing to stdout and/or file
- Previous by thread: Re: Forth stdin and stdout
- Next by thread: Re: Forth stdin and stdout
- Index(es):
Relevant Pages
|