[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[OBSD Programming] how to wait for input?
- To: misc_(_at_)_openbsd_(_dot_)_org
- Subject: [OBSD Programming] how to wait for input?
- From: Christian Schäfer <caefer_(_at_)_krachstoff_(_dot_)_net>
- Date: Tue, 17 Aug 2004 17:27:57 +0200
hi all!
I think I have a problem that must have been adressed several times
before regarding unix programming. but except hints of a possible
solution, all I can find is a bunch of man pages that I can not yet use
altogether to achieve what I need. maybe with your help I can understand
the way I have to go..
the goal of what I and some others are programming is two communication
pipes to a external device (here a cisco switch or router).
we can access the device by
$ cu -s 9600 -l /dev/tty00
since we do not want to re-invent the wheel, we want to use cu/tip to do
the communication work for us. so we execvp() the command, fork() it and
dup2() stdin and stdout to our pipes. so we have a reading and a writing
pipe to that process. this works already.
that the device is in canonical mode and we read all characters from
that device until a linefeed (\n) occurs. we then can process this input
as a string.
but this device often send a prompt, that is a string _not_ ending with
a linefeed. we can only know a promt when there is something read from
the pipe followed by a period of nothing.
we want to read this as well and add the linefeed manually.
now what I do not know is, how can I read from a pipe (FILE*) and
timeout at some point?
getc() will try to read forever when there is nothing send. we need a
safe way to know if there actaully _is_ something to read first.
something like this:
while(..)
{
switch(is_there_something(pipestream, TIMEOUT_IN_MSEC))
{
case -1: /* some error occured */ break;
case 0; /* nothing to read therefor timed out */
default; /* read something */
}
}
can this be done? I hope so.
need some urgend reply here.
if I can pass some more information, please ask.
thanks in advance!
regards
/christian
Visit your host, monkey.org