Gui4Cli has a debug output console.
I would like to use that for input too: a line should execute a given Gui4Cli commandline
This would allow to test those commandlines, to load or open gui's to do in fact anything that could be done from a gadget's command sequence.
How could this be done? A pseudo-code sketch would be much appreciated
Sun, 2012-03-04 13:33
#1
Adding a console input to an application
It depends how it is opened. If you can access it have you tried reading from it? There are functions like WaitForChar() to test for characters and then you could Read() or FGetC() from it.
Gui4Cli opens a lot of windows (guis) the user wants to manipulate.
Errors, debug info prograamed output are all directed to an outpu console, opened with the first output that needs it.
In main() this console seems to be 'opened' but it won't appear if not in debug mode
later the main loop looks like
I guess the console window must for input be treated in this loop. It must have the focus and be recognised as such. But that's me just repeating what i did read. I guess you can't put in the WaitForChar() just like that in the loop?
For output the print statements are all over the place as expected
You could use WaitForChar asynchronously, i.e. send an ACTION_WAIT_CHAR to the handler and add the replyport to your ALLsignals.
@Thomas,
I am not the author of Gui4Cli, and still a lousy C-programmer with too little ready knowledge of all the system libraries used. So the impressive list of ports opened does not reflect the level o my understanding.
I did read chapters 9 & 4 of the RKRM , looked at WaitForChar, but i am still lost a bit, confused with stdin & stdout versus the console device as documented in chapter 4 and the use of the main while loop and uyour solution with ACTION_WAIT_CHAR and adding a replyport toe Allsignals
Based on what i understand, the solution i can see is as follows
- The code opening the console above and containing:
has to be completed for input
If seems that in the main loop a second one is needed
that you can enter and leave (switch between gui-inputmode and console-inputmode
- you enter when the console get the focus and leave when another gui gets it
- or you enter and leave it using the Gui4Cli appmenu
In this second loop Gui4Cli writes a prompt, waits for and reads the user's commandline (can be the launch of a commandsequence in one of the loaded gui's, print the results (can be a debugging output corresponding to the mentioned commandsequnce)
In a simple no gui program you could use here sscanf and printf functions, so probably you can use those here too (as well as OS4 system I/O and WaitForChar) ?
Thanks for your help
One major drawback is that the console window opens whenever somebody accesses it. The call to WaitForChar is one such access which means that the console window opens immediately and you can never close it.
Here is a working example:
Please don't fiddle around with SelectInput and SelectOutput if you don't understand what they do.
Especially calling one of them without remembering the return value is dangerous. What does your cleanup routine do if it finds DEFcpOPEN == 1 ?
@Thomas
Thank you very much for the example, and your warning
just this for now
Did you never get a "file handle closed twice" guru?
The handle you supply to SelectOutput must not be closed. You have to reset it to the old handle before you may close it.
So in the beginning it should say
and in the end of the program
DEFcpOPEN is not needed at all because DEFcpOPEN != 0 is equivalent to DEFcp != NULL.
SelectOutput() sets the value which is returned by Output() and which is used by Printf() and the like. SelectInput() sets the value which is returned by Input(). Between SelectOutput(your_window) and SelectOutput(old_output) your_window is in use by the system and may under no circumstances be closed. The same applies to SelectInput(your_window) and SelectInput(old_input).
Till now never, but i don't have much experience with the console being used for input too. on the other hand i don't see clearly why i should experience it more in this case. DEFcp is the only console handle opened and in principle it remains open till cleanup. (There are cases where it will be closed and reopened with a new consolespec (eg with the GUI4CLI "SET OUTPUT CON:...." command)
I do not see however why introducing
in the closing sequence would prevent a double closure: they don't modify the handle DEFcp that has to be closed, and they ar not conditionals ?
Seems right to me.If i stick to having only one console open, at most, at any time, i must be safe ?
Thanks for your detailed and insightfull comments.
But you do not clean up. That's the problem. With SelectOutput(DEFcp) you tell the Shell that DEFcp is the output window. When your program quits you Close(DEFcp) but the Shell still thinks it is the output window. So if the Shell tries to write something out after your program has ended, it will crash because it tries to access DEFcp which you already closed. And if the Shell ends after your program, it will receive the "handle closed twice" guru.
Each SelectOutput(something) must be complemented by a SelectOutput(oldvalue) and each SelectInput(something) must be complemented by a SelectInput(oldvalue).
There are indeed a number of cases that i have to consider ( I am working with Gui4 Cli on a dayly base nearly ten years now, never having had a problem of "double closing" or other, but i prefer to walk on safe ground: i may have systematically avoided the problem in the way i start up Gui4Cli)
Launching the Gui4Cli interpreter can be done in following ways
- 1. Gui4Cli is launched from a shell directly or with the Gui4Cli launcher "guis:gui" (not the OS4 prefs editor here) To avoid confusion i'll name it "gui4"
( gui4 takes the same options or tooltypes as Gui4Cli )
- 2. I launch Gui4Cli with a WBIcon and XICON as standard tool pointing to a DOS script
2.1 launching Gui4Clli directly
2.2 launching Gui4Cli using the "gui4" launcher
- 3. Gui4Cli is launched from Workbench icon with the Gui4Cli launcher "gui4" as standard tool.
I'll look into these cases more closely, but for now:
In the first case(s) the shell used for launching will be the output console unless you have defined an other console output with arg OUTPUT.
(i think you have such a shell in cases 2.1 & 2.2 too)
When launched from WB however a predefined "Defconsolespec" will be used in the opeing of DEFcp
If the tooltuypes define an other one the latter will be used.
As a matter of fact in my Gui4Cli prefs (which is just another Gui4Cli script) ithe OUTPUT is set to KCON:something .
I do use case 2.1 as a standard launch.
A CON: window appears, that is then replaced with a KCON;
The only problem i had (systematically )was when closing Gui4Cli i had a message telling me some signalbit was not cleared.
Maybe i never verified what happens with the Con: shell when i us it.w
Time to do that.
Like to thank everybodys who has been helpfull answering this & other questions on this great forum
Resulting in my first major expansion of Gui4Cli's functionality:
http://www.os4coding.net/blog/josduchit/gui4cli-progress