Forum > General
capture apl_exec stdout
toby:
hello
i can run the libapl apl_exec function in an fpc program but need to capture it's stdout and redirect to an output buffer instead of the screen and then covert into ansistring in the fpc program itself not as an external program which runcommand will do
this is example c++ code that does this
std::stringstream outbuffer;
std::streambuf *coutbuf = std::cout.rdbuf();
std::cout.rdbuf(outbuffer.rdbuf());
std::stringstream errbuffer;
std::streambuf *cerrbuf = std::cerr.rdbuf();
std::cerr.rdbuf(errbuffer.rdbuf());
execerr = apl_exec (cmd.toStdString ().c_str ());
std::cout.rdbuf(coutbuf);
std::cerr.rdbuf(cerrbuf);
outString = QString (outbuffer.str ().c_str ());
errString = QString (errbuffer.str ().c_str ());
'while apl_exec is running, standard output and standard error will be redirected to output and error buffers instead of the screen. These are then converted into strings.'
Thaddy:
Simple example:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program redirectstdout;{$apptype console}uses streamIO,classes;varf: TextFile;s: TStringStream;begin f:=stdout; s := TStringStream.Create; AssignStream(f, s); Rewrite(f); Writeln(f, 'Hello World'); CloseFile(f); writeln('redirected: ', s.datastring); s.Free;end.
But it is better to use Tprocess, simple example:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$H+}uses sysutils,classes, process; var p:Tprocess; l:Tstrings; readcount:integer; a,b:string; i:integer;begin p :=TProcess.Create(nil); try l := Tstringlist.create; try try P.Options := []; p.executable:='D:\fpctrunk\bin\x86_64-win64\ppcx64.exe'; p.parameters.Add('-h'); p.runcommandloop(a,b,i);// accumulate stdout in a, stderr in b l.add(a); l.add(b); except on E:Exception do writeln(E.Message); end; writeln(l.text); finally l.free; end finally p.free; end; readln;end.
Side note: I was not aware of TProcess.RunCommandLoop until I wrote the example. It basically avoids messing with pipes yourself and greatly simplifies things. Because I was not aware of that, note you may need a fpc version 3.2.0 or higher, but then again it may have always been there.
If you have Pascal headers or C headers for libapl I can give a much better example, but when I try to find them I ran into many 404s
toby:
Hi Thaddy,
your second example is for running an external program which is what i resorted to using my libapl/fpc coding but i need one fpc program that can run apl_exec and bring the results into the fpc program itself - it gets really complicaed having to maintain separate programs for each apl_exec line etc
i used your first code example and it worked for your example but the apl_exec stdout was not the screen output display but only the return value (0)
i would be glad to supply my fpc headers and c headers (libapl) but first do you have a working apl/libapl installed?
there are c/libapl and python3/lib_gnu_apl (and even lua headers but i don't use them) included with the apl svn source
i compiled apl from svn source to get libapl/c and lib_gnu_apl/python3 headers but don't know if the distro versions have these done
i have c and python3 libapl programs using apl_exec that can test if you have a good apl/libapl installation
my fpc headers are simple and use the c/libapl headers and give good libapl/apl_exec for some very complicated apl expressions (the apl maintainer says that what i am doing shouldn't be doable with what he knows about the libapl - libapl is actually c headers for apl and separately authored from apl itself)
i can do everything i want with apl and fpc using external apl scripting libapl/fpc programs using runcommand but the libapl runs the same code almost 2x faster and
the libapl (whether fpc/c/python3) appears to be giving it's output to something other then stdout
i believe i am the only person working with fpc-3.2.2 and apl/libapl
---
TRon:
--- Quote from: toby on January 14, 2023, 09:23:46 pm ---the libapl (whether fpc/c/python3) appears to be giving it's output to something other then stdout
--- End quote ---
I/O channels depend on how things were build, see https://gist.github.com/houmei/cfd9e570b8de4d8fd55ada228d5ff004#file-readme-2-configure
--- Quote ---i believe i am the only person working with fpc-3.2.2 and apl/libapl
--- End quote ---
Official GNU sources for apl do not compile on my setup (debian gcc 12.2). Official GNU apl .deb does not seem to include libapl.
toby:
i have been asked if
> Does freepascal start foreign code in a separate process? Without reopening stdout?
---
Hi TRon
I/O channels depend on how things were build, see https://gist.github.com/houmei/cfd9e570b8de4d8fd55ada228d5ff004#file-readme-2-configure
compiling libapl --with-android made no difference-
-
'Official GNU sources for apl do not compile on my setup (debian gcc 12.2). Official GNU apl .deb does not seem to include libapl.'
this is what i thought was the situation
I would be glad to help you compile apl and libapl i am on a 'debian' system with gcc-12.2.0 also
this is the svn line
svn co https://svn.savannah.gnu.org/svn/apl/trunk apl
you need to compile apl and libapl separately
for apl
configure --without-gtk3 --without-sqlite3 --without-postgresql --without-pcre
make
make install
type 'apl' abd then 2+2 )off to exit you will need apl keyboard and apl font we can do them after things work
for libapl
configure --with-libapl --without-gtk3 --without-sqlite3 --without-postgresql --without-pcre
make
make install
check if /usr/local/lib/apl/libapl.[a la so] have been created
you need to edit /etc/ld.so.conf and put the line /usr/local/lib/apl in it
ldconfig
this is example ;ibapl/c code
// libapl.c compile with : g++ -O2 libapl.c -o libapl -L /usr/local/lib/apl -lapl
#include <stdio.h>
#include <stdlib.h>
#include <apl/libapl.h>
int main (int argc, char * argv[])
{
init_libapl(argv[0], 0);
apl_exec("2+2");
return 0;
}
i had to edit some source files to make libapl work - lets see if your system causes problems before we edit them
i have also edited files to change locations of default apl config files etc - lets see what happens with default compiles and installation and if you want to use what i have done
let me know how this goes
Navigation
[0] Message Index
[#] Next page