newLISP Fan Club

Forum => newLISP in the real world => Topic started by: dexter on November 10, 2011, 05:04:43 PM

Title: Capture the stdout from self
Post by: dexter on November 10, 2011, 05:04:43 PM
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
Title: Re: Capture the stdout from self
Post by: Lutz on November 10, 2011, 06:23:45 PM
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?
Title: Re: Capture the stdout from self
Post by: dexter on November 10, 2011, 06:29:34 PM
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...
Title: Re: Capture the stdout from self
Post by: dexter on November 10, 2011, 06:32:55 PM
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.
Title: Re: Capture the stdout from self
Post by: Lutz on November 10, 2011, 06:33:46 PM
That would look like this:


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

(exit)


running:


~> ./inout
Hello~>
Title: Re: Capture the stdout from self
Post by: dexter on November 10, 2011, 06:35:20 PM
and the length?



You cant know the length of print ,all of the prints
Title: Re: Capture the stdout from self
Post by: Lutz on November 10, 2011, 06:36:25 PM
> (length "hello")
5
>
Title: Re: Capture the stdout from self
Post by: dexter on November 10, 2011, 06:38:16 PM
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?
Title: Re: Capture the stdout from self
Post by: dexter on November 10, 2011, 06:40:09 PM
I will have hunders print



generated by (eval-string  lsp_file)



I want the length of result
Title: Re: Capture the stdout from self
Post by: dexter on November 10, 2011, 07:04:56 PM
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
Title: Re: Capture the stdout from self
Post by: Lutz on November 10, 2011, 07:40:10 PM
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)
Title: Re: Capture the stdout from self
Post by: dexter on November 10, 2011, 10:54:03 PM
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.
Title: Re: Capture the stdout from self
Post by: TedWalther on November 11, 2011, 11:08:02 AM
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.
Title: Re: Capture the stdout from self
Post by: TedWalther on November 11, 2011, 05:27:06 PM
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?
Title: Re: Capture the stdout from self
Post by: dexter on November 11, 2011, 06:55:52 PM
Do u have msn or gtalk?

dexterkidd$gmail.com/msn.com

I am doing it in another way now