01-22-06 11:11 PM
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[I'm reading this in comp.unix.programmer, and hence assuming you are
using the SUS/POSIX API with C or C++, but looking at the other
newsgroups this originated in, I may be mistaken, in which case I
apologise for the confusion.]
markhobley@hotpop.deletethisbit.com (Mark Hobley) writes:
> In alt.comp.os.linux Bill Marcum <bmarcum@iglou.com> wrote:
>
> What about if I wanted to obtain user input?
>
> Could I abort the input process if the data was not entered in time?
Yes. Consider that system calls can be interrupted by signals (this
can be specified by using sigaction and omitting SA_RESTART to disable
the restarting of interrupted system calls). Therefore, if you use
alarm or setitimer to trigger a SIGALRM at some point in the future, a
read() call will be stopped, and you can detect that. (The signal
handler can be empty in this instance; we are just using it as a means
of interrupting the user input).
See
http://cvs.alioth.debian.org/cgi-bi...ot=buildd-tools
for a simple example of this. This is for simple username/password
entry (it's a PAM conversation wrapper), and relies upon the
terminal/termios line buffering. Two separate timeouts are
implemented: a warning timeout and a final fatal timeout.
> Even better, could I reset the timer, if a user suddenly started typing?
Yes. However, you'll need to disable line buffering by tweaking the
termios for the controlling tty, so that you get input
character-by-character instead of line-by-line. I would recommend
using setitimer rather than alarm in this instance, so you can
continually adjust the timer with finer accuracy. You might need to
handle erase/del/bs if you wish to allow the user to correct the
input, or perhaps use ncurses in this instance.
Regards,
Roger
- --
Roger Leigh
Printing on GNU/Linux? http://gutenprint.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>
iD8DBQFD03csVcFcaSW/uEgRArvKAJ9CTSCSNvIOihEmm/EMfkND2cpL5ACdHIUS
cG6XmPcldbvtdhbFmrOS3PQ=
=BPB0
-----END PGP SIGNATURE-----
[ Post a follow-up to this message ]
|