Page 1 of 1

Capture the stdout from self

Posted: Fri Nov 11, 2011 1:04 am
by dexter
I want capture the stdou result created my the process it self
like

Code: Select all

#!/usr/bin/newlisp
;
; - inout -
; read from stdin 0 into buffer
; than write to stdout 1
;
; USAGE: ./inout < inputfile > outputfile
;
(print "Hello")

(while (read 0 buffer 1024)
   (write 1 buffer 1024))

(exit)
But this code never works, it always be waiting
I dont want to do it like echo hello | t newlisp inout.lsp
Anyone have a better way?
thanks

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 2:23 am
by Lutz
works for me on Mac OS X:

Code: Select all

~> echo hello | newlisp inout
hello
~> echo hello | ./inout
hello
~> 
and also on Windows XP:

Code: Select all

C:\Documents and Settings\Lutz>echo hello | newlisp inout
hello

C:\Documents and Settings\Lutz>
The other way would be as shown in the comments with redirects from to files.

Not sure why you have that extra (print "Hello") in your code? What OS are you using?

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 2:29 am
by dexter
I am on linux

That's not What I want

I just want to run it ,and the result of (print "hello") inside the inout can be showed

Not echo hello | ./inout

just like
#./inout
and hello shows up

I want capture the print result inside the program ,not from outside like ech cat ,etc...

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 2:32 am
by dexter
Cause in fastcgi

I better to captured the length of stdout result

you know
all fastcgi result is created by print println ,etc, some basic functions


if I cant get the length of stdout,it'll be very hard to do fastcgi in newlisp

Since now I redirect all print result to a file in /tmp
but this way is not perfect,and it's slower than php

I want to capture all stdout result in a buffer , a buffer of memory ,that's the fastest way.

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 2:33 am
by Lutz
That would look like this:

Code: Select all

#!/usr/bin/newlisp
(print "Hello")

(exit)
running:

Code: Select all

~> ./inout
Hello~> 

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 2:35 am
by dexter
and the length?

You cant know the length of print ,all of the prints

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 2:36 am
by Lutz

Code: Select all

> (length "hello")
5
> 

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 2:38 am
by dexter
You just cant get it ,right?

I assum I dont know the content of print

(print anything I do not know)

how u get the length?

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 2:40 am
by dexter
I will have hunders print

generated by (eval-string lsp_file)

I want the length of result

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 3:04 am
by dexter
also
(catch) only return the last result of newlisp
like for 1000
the 1000 times result returned


it's so close, I nearly did the fastcgi part
Maybe it is wrong to develop fastcgi on newlisp ,I should do it in C ,like php-cgi

what a pity

https://github.com/guu/newlisp-fastcgi

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 3:40 am
by Lutz
instead of using 'print 'you could use the second syntax of 'write' printing to a string in memory. This way you can either accumulate the return values from 'write' as the number of characters printed or take the (length buffer), where 'buffer' is the string buffer, you wrote to:

Code: Select all

(set 'buffer "")  ; init buffer to empty string

(write buffer stuff-to-write)
(write buffer other-stuff)

(set 'n-of-bytes-written (length buffer))
or do:

Code: Select all

(inc cnt (write buffer stuff))
then finally write buffer to stdout:

Code: Select all

(write 1 buffer cnt)

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 6:54 am
by dexter
I know there are many way to skip this problem

But what I do is a host , running with nginx ,etc httpd

I could do my code ,but I cant control pepole to write my code
for example:

When I found out the bug in my prev version of fastcgi is

http://www.newlisp.org/environment.cgi

I put it into my server, then I see, the do-list loop results did not show up.
so I got it , my code has problems
and I also can not set env QUERY_STRING and else env vars with my code

I think this problem can not be skipped.
I must found a way to get all stuff printed by eval-string ,at least the length , so that
my code can run the old newlisp cgi code to fastcgi mode.

AM I RIGHT?
If it is not able to be done in newlisp ,tell me ,so I can change my way.

Re: Capture the stdout from self

Posted: Fri Nov 11, 2011 7:08 pm
by TedWalther
Dexter, I've been wanting FastCGI for newlisp for a while. I'm excited to see you here. Can you phone me? I'll send it to you in private message here on the forum. Or send me your number. Maybe I can help.

Re: Capture the stdout from self

Posted: Sat Nov 12, 2011 1:27 am
by TedWalther
Since you are already doing FastCGI, I recommend leaving the CGI module alone completely. How much code would you need to convert from using the CGI module?

Re: Capture the stdout from self

Posted: Sat Nov 12, 2011 2:55 am
by dexter
Do u have msn or gtalk?
dexterkidd$gmail.com/msn.com
I am doing it in another way now