Matt, Thank you very much for your help. I coded according 
to your pseudo-code and it really works!!!

I still have one last question, though.  The output of the telnet
pipes back to parent which has a c/r (ASCII carriage return)
appended at the end of everyline in my Text widget window.
I am using motif 1.2 with text widget window to show all output
from telnet. If I replaced execl() of telnet to other command (ls or ps)
then it is OK.  For some reasons, my Text widget window
displays all hidden characters of the telnet output. 

Login: c/r
Passwd: c/r
c/r
$

Thanks again for your help.

John

----- Original Message ----- 
From: Matt Fahrner <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, May 03, 2000 11:03 AM
Subject: Re: Telnet program


> Ok then, the best I can do is give some pseudo-code:
> 
> - Program starts.
> - Create two pipes with pipe() calls.
> - fork()
> - Child chooses one pipe and dup2()'s the read end over descriptor 0
> (stdin)
> - Child takes other pipe and dup2()'s the write end over descriptor 1
> (stdout)
>   and maybe descriptor 2 (stderr).
> - Child (to be clean) close all the pipe()'s since they're now
> replacing stdin
>           and stdout.
> - Parent (to be clean) closes the same ends that were dup2()'ed (since
> those
>     technically belong to the child only.
>         - Child does an execv(), execl(), or your exec of choice of
> "telnet". Child
>           has now been officially replaced. You may or may not choose to
> pass an argument
>           that is the host to telnet (you can write "open XXXX" to it
> later instead if you
>           want.
>         - Parent continues.
> - Start parsing input and output.
> - Parent read()s from other end of pipe duped to child's descriptor 1
> until it
>           sees "login:", when it does it write()s the usename to the
> other end of the pipe 
>           duped to child's descriptor 0.
>         - Parent read()s until it sees "password:", when it does it
> write()s
>   the password. You may want to use regular expressions to do matching
> so
>           its more portable. You may also want to use select() or poll()
> to check
>   for read() or write()ability so you don't block (probably not in this
>           particular scenario though).
>         - Parent read()s until it sees the shell prompt (or output
> indicating the shell
>           in use is ready for input), then writes any command(s) it
> wants to execute.
> - Continue to read() and write() whatever's necessary to control what
> you're
>           doing.
> - When done wait3() for the child to complete (ie: exit).
> 
> "telnet" may not be happy with a pipe() as stdin and stdout. In which
> case you'll have to use pseudo-terminals (a whole nother ball of wax).
> You may have to read character by character and do your own buffering
> too. If you're lucky, the above will work.
> 
> - Matt

-- 
To unsubscribe:
mail -s unsubscribe [EMAIL PROTECTED] < /dev/null

Reply via email to