PDA

View Full Version : stdout is not what thinBasic thinks



Vandalf
18-11-2017, 00:46
I've been wanting to add some command-line interactivity to some of my programs, but I found that the console module's standard console output cannot be redirected: any attempt to do so results in a pseudo-console-output being sent to the console regardless, or no output being shown, while the "real" console output is empty. The same situation happens with the console module's standard error output, and it also happens when the "CON" file (console) is used for output.
What does work as intended is the CGI_write function from the CGI module.

Note that I am using thinbasicc.exe to interpret the scripts, not thinbasic.exe

This is the code I tested:


'cgiwrite.tbasic
'--------------------
#Bundle Type CONSOLE
Uses "cgi"
cgi_write("hello world")

'--------------------
'consolewriteline.tbasic
'--------------------
#Bundle Type CONSOLE
Uses "console"
Console_WriteLine("hello world")

'--------------------
'consolewritelineerror.tbasic
'--------------------
#Bundle Type CONSOLE
Uses "console"
Console_WriteLineerror("hello world")

'--------------------
'fileappend.tbasic
'--------------------
#Bundle Type CONSOLE
Uses "file"
FILE_Append("con","hello world")

'--------------------
'fileoutput.tbasic
'--------------------
#Bundle Type CONSOLE
Uses "file"
Dim afile As Number
afile = FILE_Open("CON","OUTPUT")
FILE_LinePrint(afile,"hello world")
FILE_Close(afile)
'--------------------

The code was tested as both thinBasic scripts and as bundled executables.

This is an additional file, "dummy.bat", used for reference purposes:

echo hello world

This is the test, a file called "tests.bat":

@echo off
c:\thinbasic\thinbasicc.exe "%cd%\cgiwrite.tbasic" > "%cd%\cgiwrite.txt" 2> "%cd%\cgiwrite2.txt"
c:\thinbasic\thinbasicc.exe "%cd%\consolewriteline.tbasic" > "%cd%\consolewriteline.txt" 2> "%cd%\consolewriteline2.txt"
c:\thinbasic\thinbasicc.exe "%cd%\consolewritelineerror.tbasic" > "%cd%\consoleerror.txt" 2> "%cd%\consoleerror2.txt"
c:\thinbasic\thinbasicc.exe "%cd%\fileappend.tbasic" > "%cd%\fileappend.txt" 2> "%cd%\fileappend2.txt"
c:\thinbasic\thinbasicc.exe "%cd%\fileoutput.tbasic" > "%cd%\fileoutput.txt" 2> "%cd%\fileoutput2.txt"
@cmd /c "%cd%\dummy.bat" > "%cd%\dummy.txt" 2> "%cd%\dummy2.txt"
if exist "%cd%\cgiwrite.exe" ("%cd%\cgiwrite.exe" > "%cd%\cgiwriteexe.txt" 2>"%cd%\cgiwriteexe2.txt" )
if exist "%cd%\consolewriteline.exe" ("%cd%\consolewriteline.exe" > "%cd%\consolewritelineexe.txt" 2>"%cd%\consolewritelineexe2.txt" )
if exist "%cd%\consolewritelineerror.exe" ("%cd%\consolewritelineerror.exe" > "%cd%\consoleerrorexe.txt" 2>"%cd%\consoleerrorexe2.txt" )
if exist "%cd%\fileappend.exe" ("%cd%\fileappend.exe" > "%cd%\fileappendexe.txt" 2>"%cd%\fileappendexe2.txt" )
if exist "%cd%\fileoutput.exe" ("%cd%\fileoutput.exe" > "%cd%\fileappendexe.txt" 2>"%cd%\fileappendexe2.txt" )

dir *.txt

Note that the "2>" is meant to redirect error output, while ">" redirects normal output.

This is the output of the test:

hello world
hello world
'[irrelevant lines omitted]
17/11/2017 19:07 11 cgiwrite.txt
17/11/2017 19:07 0 cgiwrite2.txt
17/11/2017 19:07 11 cgiwriteexe.txt
17/11/2017 19:07 0 cgiwriteexe2.txt
17/11/2017 19:07 0 consoleerror.txt
17/11/2017 19:07 0 consoleerror2.txt
17/11/2017 19:07 0 consoleerrorexe.txt
17/11/2017 19:07 0 consoleerrorexe2.txt
17/11/2017 19:07 0 consolewriteline.txt
17/11/2017 19:07 0 consolewriteline2.txt
17/11/2017 19:07 0 consolewritelineexe.txt
17/11/2017 19:07 0 consolewritelineexe2.txt
17/11/2017 19:07 13 dummy.txt
17/11/2017 19:07 0 dummy2.txt
17/11/2017 19:07 0 fileappend.txt
17/11/2017 19:07 0 fileappend2.txt
17/11/2017 19:07 0 fileappendexe.txt
17/11/2017 19:07 0 fileappendexe2.txt
17/11/2017 19:07 0 fileoutput.txt
17/11/2017 19:07 0 fileoutput2.txt

I haven't yet figured out exactly which programs are responsible for the two "hello world"s in the test's output.

After reading this thread (http://www.thinbasic.com/community/showthread.php?8039-Can-thinbundle-create-a-console-exe), I changed the scripts' extensions to .thinbasicc (and changed the test accordingly), just to be sure, but the test's output was still the same.

ErosOlmi
19-11-2017, 14:42
Ciao Vandalf

Console module was designed to have the maximum speed when developing Console applications.
While CGI module was developed to intercept output when using thinBasic for server side scripting. CGI module never had any real interest in users so it is almost abandoned

I will see what I can do for Console module in order to redirect output but not sure I will be able without loosing speed performance.

Eros

Vandalf
19-11-2017, 21:32
I think it would be more helpful to point out these facts in the documentation/help file of the console module. Such that users wanting to have redirect-able output would know to use CGI_Write. Then updating the console module would be less necessary.

As an aside, note that, while printing text to the console is typically one of the slowest operations in any program, it is the console itself that is mostly responsible for the lack of speed, not the programs. (At least, this is what I have understood)

As an example, the following command takes 20 seconds to run in my machine:

FOR /L %I IN (1,1,6000) DO ECHO %I
Whereas the following takes 18 seconds:

FOR /L %I IN (1,1,6000) DO ECHO %I >> I.TXT
The difference in speed is simply due to the redirection, that makes it so text is not actually printed to the console: it goes directly to a file. And note that it is a FOR command: a redirection happens for every ECHO, as opposed to happening only once while all the ECHOs are being executed. That would be even faster.

ErosOlmi
20-11-2017, 07:53
OK, I will amend help. I will also see if I can do a redirect

What language is your example? Seems command shell.

The following is a thinBasic Console example.
On my machine (see my signature):

first loop takes 5.8 seconds mainly because PrintL needs to take care of buffer scrolling
second loop using PrintAt takes less than 1 second. PrintAt is very very fast and developed for max speed for users in need to design console script heavily using console screen



uses "console"

double T1 = Timer
for I as long = 1 to 6000
PrintL I
Next
double T2 = Timer
printl "Times taken:", format$(T2 - T1, "#0.00")
printl "-Press a key to do next loop-"
WaitKey

Console_Cls

T1 = Timer
for I as long = 1 to 6000
Printat I, 10, 10
Next
T2 = Timer
printl "Times taken:", format$(T2 - T1, "#0.00")
printl "-Press a key to end-"

WaitKey


Anyway, the question is:

are you searching for redirection?
thinBasic Console module does not perform redirection so I need to see how to implement it
or are you searching for speed?
Even if interpreted, thinBasic Console module is one of the fastest console programming environment

Vandalf
20-11-2017, 11:09
The language in the example is cmd.exe, the one used for writing batch files.

I stumbled into this issue because I wanted console output that would work with standard redirections and pipes, as seen in most command shells. The CGI_write function fulfills that purpose perfectly well.
I see where the "need for speed" in console outputs comes from; that's not a bad thing, though it's not what I was looking for this time.

This seems more like a documentation issue than a programming issue. If these design decisions were noted in the help file, it might have saved me a little bit of hair-pulling. It wasn't too bad, since fortunately I had dipped my toes in the CGI module beforehand. :cool:

ErosOlmi
20-11-2017, 15:29
Ok, thanks.
I will amend help and sorry for time spent in searching for info.

Petr Schreiber
20-11-2017, 20:16
Hi Eros,

I also noticed the built in StdOut command seem to not do what expected. I presume these are from times... like... 2004/2005 :)
Executing the following with thinbasic.exe and thinbasicc.exe does result in zero bytes file, when redirected:


uses "console"


stdOut("Hello world")


Having the ability to write to stdout and stderr would be highly useful - especially for using thinBasic script in chain with other tools.


Petr

ErosOlmi
20-11-2017, 21:02
Yes, that time when Console module was developed by my friend Roberto.
Some functionalities never get completed.