Capture the stdout from self

Started by dexter, November 10, 2011, 05:04:43 PM

Previous topic - Next topic

dexter

I want capture the stdou result created my the process  it self

like



#!/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

Lutz

#1
works for me on Mac OS X:


~> echo hello | newlisp inout
hello
~> echo hello | ./inout
hello
~>


and also on Windows XP:


C:Documents and SettingsLutz>echo hello | newlisp inout
hello

C:Documents and SettingsLutz>


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?

dexter

#2
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...

dexter

#3
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.

Lutz

#4
That would look like this:


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

(exit)


running:


~> ./inout
Hello~>

dexter

#5
and the length?



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

Lutz

#6
> (length "hello")
5
>

dexter

#7
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?

dexter

#8
I will have hunders print



generated by (eval-string  lsp_file)



I want the length of result

dexter

#9
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">https://github.com/guu/newlisp-fastcgi

Lutz

#10
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:



(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:



(inc cnt (write buffer stuff))


then finally write buffer to stdout:


(write 1 buffer cnt)

dexter

#11
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">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.

TedWalther

#12
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.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

TedWalther

#13
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?
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

dexter

#14
Do u have msn or gtalk?

dexterkidd$gmail.com/msn.com

I am doing it in another way now