capture stderr from (process ...

Q&A's, tips, howto's
Locked
nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

capture stderr from (process ...

Post by nigelbrown »

Hi,
I'm trying porting from python to newlisp (of course) but python has possibility of assigning stderr output to a pipe when starting a process
eg from chatsniff1.0.py - http://chatsniff.sourceforge.net/
pipe = Popen('tethereal -i '+str(gDevices.index(gCurrentDevice) + 1)+' -l -V -R \"aim || ymsg || msnms || jabber\"', bufsize = 1, cwd = path, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines = True)

while (process in newlisp syntax is : (process str-command int-pipe-in int-pipe-out [int-win32-option])
i.e. only stdin stdout directable to pipes.
The code in nl-filesys.c is:

CELL * p_process(CELL * params)
{
char * command;
int forkResult;
int inpipe = 0, outpipe = 0;

params = getString(params, &command);
if(params != nilCell)
{
params = getInteger(params, (UINT*)&inpipe);
getInteger(params, (UINT *)&outpipe);
}

if((forkResult = fork()) == -1)
return(nilCell);
if(forkResult == 0)
{
/* redirect stdin and stdout to pipe handles */
if(inpipe)
{
close(STDIN_FILENO);
if(dup2(inpipe, STDIN_FILENO) == -1) exit(0);
close(inpipe);
}
if(outpipe)
{
close(STDOUT_FILENO);
if(dup2(outpipe, STDOUT_FILENO) == -1) exit(0);
close(outpipe);
}

system(command);
exit(0);
}
could another option after [int-win32-option] be [int-pipe-err]
and newlisp do STDERR_FILENO to pipe?
Or is this not recommended?

Is there another way? - I see in
return value thread ( http://www.alh.net/newlisp/phpbb/viewtopic.php?t=789 ) that stderr can be handled from exec
Lutz:
> (exec "ls *.abc 2>&1")
("ls: *.abc: No such file or directory")
>
The 2>&1 spec tell ls to redirect error output to stdout.

but don't know enough about system() to know if that approach is possible for (process.

Any tips?

Nigel

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Hello Nigel,

Did you try a combination of switching (device [int] )
to input/output/erro or pipe?
That could be a workaround, i did not test it though..

regards, Norman.
-- (define? (Cornflakes))

nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

Post by nigelbrown »

Hi Norman,
I'm not sure what you mean by switching?

I've experimented with redirecting 2>&1 with a single line command and it works:
> (process "ls *.abc 2>&1" procin procout)
11118
> (read-line myin)
"ls: *.abc: No such file or directory"
>

Looking more at system() it does start a shell for the process so I'll use the 2>&1 approach.
Longer term newlisp might like to look at having optional capture of stderr to its own pipe (Lutz?).
full eg from python
pipe = Popen('tethereal -i '+str(gDevices.index(gCurrentDevice) + 1)+' -l -V -R \"aim || ymsg || msnms || jabber\"', bufsize = 1, cwd = path, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines = True)
global maintetherealpid
maintetherealpid = pipe.pid
pipe.stdin.close()
f = pipe.stdout
e = pipe.stderr
...

Nigel

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

Post by Lutz »

Currently only in Win32 stderr is redirected to the stdout pipe. On Linux/UNIX you can use the stderr redirection: 2>&1 on 'process' and 'exec'.

Yes, perhaps in the future an optional switch, although 2>&1 works already.

Lutz

nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

Post by nigelbrown »

Hi Lutz,
I agree it's not a priority. However, it would allow easier recognition and handling of error or warning messages.

Regards
Nigel

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

Post by Lutz »

I have added an optiion for 8.7.5

Lutz

nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

Post by nigelbrown »

Fantastic! Thank you Lutz, it will make my parsing of stdout much easier with stderr going elsewhere.

Regards
Nigel

Locked