Page 1 of 1
Problem executing a selfmade linux executable from within PB
Posted: Tue Apr 28, 2009 2:53 am
by AND51
Hello!
I almost finished my "PB Interpreter", which allows me to write my whole websites in PureBasic.

Unfortunately, I got a strange problem: My interpreter (a console app) cannot run another console app.
I've got this console executable to be compiled and executed:
Code: Select all
OpenConsole()
PrintN("Content-Type: text/html")
PrintN("")
Print("Hello from PureBasic "+Str(Random(999)))
The code gets compiled correctly, the executable is being created in /tmp/. I can even run it in the terminal and I get this output:
output wrote:Content-Type: text/html
Hello from PureBasic 561
But when my interpreter tries to run the executable, an error occurs. I use this code:
Code: Select all
OpenConsole()
; other code
Define program=RunProgram(exe$, commandline$, "", #PB_Program_Open|#PB_Program_Read|PB_Program_Write)
Define *output, available
While ProgramRunning(program)
available=AvailableProgramOutput(program)
If available
*output=AllocateMemory(available)
If *output
ReadProgramData(program, *output, available)
; pass the output back to Apache by writing it to OUR stdout
WriteConsoleData(*output, available)
EndIf
EndIf
Delay(10)
Wend
I cannot read the output and STDERR only contains "Speicherzugriffsfehler (Core Dumped)" (english: Memory Access Error).
What is going wrong here? I'm new to Linux/Unix, but I already got some basic knowledge, i. e. about pipes, stdin, stdout, stderr, bash/shell, etc.
I can also provide this executable so that you can download and run it on your machine for testing.
Oh yes, and my server is running with PB 4.30 and openSUSE 10.3.
Posted: Tue Apr 28, 2009 10:44 am
by bembulak
[OT:]
I almost finished my "PB Interpreter",
Sounds interesting! Is is possible to use it as an interactive Shell, like in Ruby or Python?
This would be great!

Posted: Tue Apr 28, 2009 5:02 pm
by AND51
Well, I don't know Ruby or Phyton in particular.
When I log into my server via SSH, I can type "/purebasic/interpreter /purebasic/test.pb". Then my interpreter takes test.pb, compiles it with the PB compiler, reads the outout of test.exe and passes this output back to the terminal.
Additionally, my interpreter can also be executed by the webserver Apache. So, if you call www.and51.de/cgi-bin/guestbook.pb, you will get a website generated by PureBasic code and not by Perl or PHP. Is this what you mean?
However, I currently encounter problems with executuing a freshly compiled executable. Neither can I run it, nor can I read its output.
I plan to publish my project, when it's finished, so that you can also use PureBasic for your website on your virtual server.
But this may take a while, as I am very new to Linux/Unix. There are so many tiny things that bug me, e. g. different user accounts, access only via SSH, working via command-line everywhere, etc...
I might finish faster, if you are able to help me, if you want. I am grateful for every kind of support.
Posted: Tue Apr 28, 2009 6:32 pm
by walker
may you have to put an & at the end of your 2nd program (you call within your console-app) to get it running independently from your pgm... just a guess
Posted: Tue Apr 28, 2009 7:14 pm
by AND51
Hello walker,
Thank your very much for your advice. I immediately tried to run it with a trailing apmersand, but it doesn't seem to work either.
When I try to append a & when I run my executable from the console, the output does not appear, because it is executed in the background.
Now I continued working and tried it again and again, with and without ampersand, and I achieved a success:
Currently it works, I can run it without ampersand.
Let's see, how long I'm lucky...
Posted: Tue Apr 28, 2009 8:01 pm
by AND51
Well, I got the next problem, but this one is not so big as the previous one.
A compiled executable can now be executed and read. But it
won't end?
This executable
Code: Select all
OpenConsole()
PrintN("Content-Type: text/html")
PrintN("")
PrintN("<pre>Hallo von PureBasic"+Str(Random(999)))
PrintN("Aktuelles Verzeichnis: "+getcurrentdirectory())
PrintN("ProgramFilename: "+ProgramFileName())
Print("EXIT")
CloseConsole()
End
is being executed and read by this piece of code:
Code: Select all
OpenConsole()
; other code
Define program=RunProgram(exe$, commandline$, dir$, #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Error)
Define available, *output
While ProgramRunning(program)
available=AvailableProgramOutput(program)
If available
*output=AllocateMemory(available)
If *output
ReadProgramData(program, *output, available)
; redirecting programs output directly to apache by writing it to OUR console (our stdout)
WriteConsoleData(*output, available)
EndIf
EndIf
Delay(10)
Wend
PrintN("This line will never be printed")
CloseConsole()
End
Don't worry, 'exe$', 'commandline$' and 'dir$' are correctly set.
But why doesn't this loop break, when the sub-program prints "EXIT" and quits itself?
Posted: Tue Apr 28, 2009 8:49 pm
by walker
... two thoughts.
first: a bug in closeconsole() (regarding to the doc, you have to use killprogramm() and closeprogramm() for completely removing a prog started with RunProgram)
I think, closeconsole only uses "KillProgramm" for itself but not closeprogram for disconnecting from it's caller... so the program-id is still valid :roll:
second: A BUG

Posted: Tue Apr 28, 2009 9:22 pm
by AND51
Well, at the moment, I don't have a clue. I'm confused:
1) Using CloseProgram() does not only close STDIN, but also STDOUT. So when I finished my input to that sub-executable and I use CloseProgram() then, I cannot read its output.
2) The sub-executable is correct. So the mistake must be somewhere else. But is my interpreter really wrong? Do I have made a logical mistake? I don't think so.
3) I can't the the sub-executable in the taskmanager (top). So I assume it quits correctly. // Edit: It must be a bug: I can get the ProgramExitCode(), but it is still running. So either the bug is in ProgramRunning() or, as you said, in CloseConsole().
I will post this in the bug section.
Posted: Tue Apr 28, 2009 9:37 pm
by walker
.. may you can work with a woraround
simply send a special string (something unique) just before quitting the called app and if it's read by your programm, use kill/Closeprogramm to cut off the connection ... may that's a way untill the bug (still considering it is one) fixed
Posted: Tue Apr 28, 2009 9:58 pm
by AND51
Yes, I already thought of this solution, too.
If there won't be quick help, I must do so until the bugfix.
Meanwhile, I posted this in the Linux bugsection.
There is a link to the script that is compiled and executed everytime, you visit the page. So you can see my interpreter
live!
@ bembulak:
You may click this link, too. I'm sure, you will, as you are interested.
Note: there is also some debugging output, so don't wonder.
Posted: Wed Apr 29, 2009 12:32 am
by walker
hey..... that's pretty cool

(tried your link)
you can prog an app using your webbrowser simply by sending a .pb source

Posted: Wed Apr 29, 2009 6:27 am
by AND51
Yes, you are right. First, I wanted only my website to be in PureBasic, but then I got aware of the fact that I can also open a port on the server side to receive .pb-files. I'm sure there are some possibilities.
Posted: Wed Apr 29, 2009 10:06 pm
by AND51
Hey walker and bembulak (and all others),
I published this project in the german forum:
http://www.purebasic.fr/german/viewtopi ... nterpreter
You can participiate there. I will publish it in this forum, too, when I collected some ideas from the german members.