Capture the stdout from self

Q&A's, tips, howto's
Locked
dexter
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

Capture the stdout from self

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Capture the stdout from self

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

dexter
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

Re: Capture the stdout from self

Post 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...

dexter
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

Re: Capture the stdout from self

Post 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.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Capture the stdout from self

Post by Lutz »

That would look like this:

Code: Select all

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

(exit)
running:

Code: Select all

~> ./inout
Hello~> 

dexter
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

Re: Capture the stdout from self

Post by dexter »

and the length?

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Capture the stdout from self

Post by Lutz »

Code: Select all

> (length "hello")
5
> 

dexter
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

Re: Capture the stdout from self

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

dexter
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

Re: Capture the stdout from self

Post by dexter »

I will have hunders print

generated by (eval-string lsp_file)

I want the length of result

dexter
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

Re: Capture the stdout from self

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Capture the stdout from self

Post 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)

dexter
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

Re: Capture the stdout from self

Post 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.

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: Capture the stdout from self

Post 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.
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
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: Capture the stdout from self

Post 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?
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
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

Re: Capture the stdout from self

Post by dexter »

Do u have msn or gtalk?
dexterkidd$gmail.com/msn.com
I am doing it in another way now

Locked