Unix Programming - How to force keystrokes into the buffer on Unix

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2006 > How to force keystrokes into the buffer on Unix





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to force keystrokes into the buffer on Unix
Kenny McCormack

2006-04-29, 7:14 pm

I know there is a way to do this - that is, a system call that allows
you to push a character into the keyboard buffer - but I can't seem to
find it now (checked man pages for tty, termio(s) on several systems).

I found a workaround, with expect, like this:

expect -c 'spawn program;expect something;send key;interact'

but I'd prefer to avoid that as it brings in the overhead of expect and
a tty layer just to push one key. Anyway, I think I did this a long
time ago and that it is TIOsomething. Can anyone help?

David Schwartz

2006-04-29, 7:14 pm


"Kenny McCormack" <gazelle@xmission.xmission.com> wrote in message
news:e30i3r$huf$1@news.xmission.com...
>I know there is a way to do this - that is, a system call that allows
> you to push a character into the keyboard buffer - but I can't seem to
> find it now (checked man pages for tty, termio(s) on several systems).
>
> I found a workaround, with expect, like this:
>
> expect -c 'spawn program;expect something;send key;interact'
>
> but I'd prefer to avoid that as it brings in the overhead of expect and
> a tty layer just to push one key. Anyway, I think I did this a long
> time ago and that it is TIOsomething. Can anyone help?


I don't think you can avoid the overhead of a tty layer. If you don't
already have a tty, which would be the case if you were redirected from a
file or pipe, there would be nothing to push the character into.

DS


Kenny McCormack

2006-04-30, 1:25 am

In article <e310u5$luf$1@nntp.webmaster.com>,
David Schwartz <davids@webmaster.com> wrote:
>
>"Kenny McCormack" <gazelle@xmission.xmission.com> wrote in message
>news:e30i3r$huf$1@news.xmission.com...
>
> I don't think you can avoid the overhead of a tty layer. If you don't
>already have a tty, which would be the case if you were redirected from a
>file or pipe, there would be nothing to push the character into.
>
> DS


No, no, this is an ordinary, interactive program I am dealing with.
Assume I have an app that is reading and writing the keyboard, using
ncurses or something similar, and I want to push a keystroke to it.

In fact, as you can tell from my earlier description, I just need to
press one key, then go back into regular interactive mode.

I'm imagining pseudo-code like this:

fork,exec new app
sleep(10 or something like that, to give the app time to initialize)
push key into keyboard buffer
exit (and let the app [child program] continue to run)

I'm not joking when I say that there is a way to do it, and that it is
called TIO<something>. I just can't remember (or find in my man page
search) what that something is.

Barry Margolin

2006-04-30, 1:25 am

In article <e30i3r$huf$1@news.xmission.com>,
gazelle@xmission.xmission.com (Kenny McCormack) wrote:

> I know there is a way to do this - that is, a system call that allows
> you to push a character into the keyboard buffer - but I can't seem to
> find it now (checked man pages for tty, termio(s) on several systems).
>
> I found a workaround, with expect, like this:
>
> expect -c 'spawn program;expect something;send key;interact'
>
> but I'd prefer to avoid that as it brings in the overhead of expect and
> a tty layer just to push one key. Anyway, I think I did this a long
> time ago and that it is TIOsomething. Can anyone help?


I think you're thinking of TIOCSTI -- I believe STI stands for Store
Terminal Input.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Kenny McCormack

2006-04-30, 1:25 am

In article <barmar-9EAB5E.00314530042006@comcast.dca.giganews.com>,
Barry Margolin <barmar@alum.mit.edu> wrote:
>In article <e30i3r$huf$1@news.xmission.com>,
> gazelle@xmission.xmission.com (Kenny McCormack) wrote:
>
>
>I think you're thinking of TIOCSTI -- I believe STI stands for Store
>Terminal Input.


That sounds right. Could you tell me where to find the documentation
for it on a Linux system? I thought it would be in "man 4 tty", but it
doesn't seem to be.

Barry Margolin

2006-04-30, 1:25 am

In article <e31gos$7fn$1@news.xmission.com>,
gazelle@xmission.xmission.com (Kenny McCormack) wrote:

> In article <barmar-9EAB5E.00314530042006@comcast.dca.giganews.com>,
> Barry Margolin <barmar@alum.mit.edu> wrote:
>
> That sounds right. Could you tell me where to find the documentation
> for it on a Linux system? I thought it would be in "man 4 tty", but it
> doesn't seem to be.


Don't know where it is on Linux, but that's where it is on my OS X
system:

TIOCSTI char *cp
Simulate typed input. Pretend as if the terminal
received the character pointed to by cp.

If it's not there on Linux, it may not implement this ioctl in the first
place.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
noogie.brown@gmail.com

2006-04-30, 7:17 am


Kenny McCormack wrote:
> I know there is a way to do this - that is, a system call that allows
> you to push a character into the keyboard buffer - but I can't seem to
> find it now (checked man pages for tty, termio(s) on several systems).
>
> I found a workaround, with expect, like this:
>
> expect -c 'spawn program;expect something;send key;interact'
>
> but I'd prefer to avoid that as it brings in the overhead of expect and
> a tty layer just to push one key. Anyway, I think I did this a long
> time ago and that it is TIOsomething. Can anyone help?


Maybe not exactly what your looking for, but better to mention than not
I guess - ncurses has ungetch to do that sort of thing, but obviously
then you'd have to make your program use ncurses.

Kenny McCormack

2006-04-30, 7:17 am

In article <1146397140.102951.190400@u72g2000cwu.googlegroups.com>,
<noogie.brown@gmail.com> wrote:
>
>Kenny McCormack wrote:
>
>Maybe not exactly what your looking for, but better to mention than not
>I guess - ncurses has ungetch to do that sort of thing, but obviously
>then you'd have to make your program use ncurses.


One of the conditions is that the app is not re-compilable, i.e., no
source, i.e., a "black box". If I could modify the app, I'd just modify
it so that it didn't need the keystroke to be pushed into it. I.e., the
point is that the app has a misfeature that requires a keystroke in
order to disable (to disable the misfeature).

Anyway, I'm beginning to like the Expect solution more and more as time
goes by. Still wish I knew where to find this function in the Linux man
pages. (Yes, I'm pretty sure it *is* implemented in Linux, just badly
documented; I could be wrong about this, of course)

Spoon

2006-04-30, 7:17 am

Kenny McCormack wrote:

> In article <barmar-9EAB5E.00314530042006@comcast.dca.giganews.com>,
> Barry Margolin <barmar@alum.mit.edu> wrote:
>
>
>
> That sounds right. Could you tell me where to find the documentation
> for it on a Linux system? I thought it would be in "man 4 tty", but it
> doesn't seem to be.


Kenny!

Did you get tired of trolling in clc? :-) (Just kidding.)

Google came up with a few interesting hits:

http://www.die.net/doc/linux/man/man4/tty_ioctl.4.html
Faking input
TIOCSTI const char *argp
Insert the given byte in the input queue.


http://www.phrack.org/show.php?p=50&a=5
"Stuffing keystrokes is not a problem, since we can use the TIOCSTI
ioctl to stuff keystrokes into the input stream."


http://www.google.com/search?q=TIOCSTI+ioctl+tty

Regards.
Diomidis Spinellis

2006-04-30, 1:25 pm

Kenny McCormack wrote:
> In article <barmar-9EAB5E.00314530042006@comcast.dca.giganews.com>,
> Barry Margolin <barmar@alum.mit.edu> wrote:
>
> That sounds right. Could you tell me where to find the documentation
> for it on a Linux system? I thought it would be in "man 4 tty", but it
> doesn't seem to be.


Grep is your friend for locating function and macro definitions. To
find the include file where TIOCSTI is defined run something like:

grep TIOCSTI /usr/include/**/*.h

On a Linux box I have access to the above command returns:

/usr/include/asm-i386/ioctls.h:#define TIOCSTI 0x5412
/usr/include/asm-x86_64/ioctls.h:#define TIOCSTI 0x5412
/usr/include/linux/compat_ioctl.h:COMPATIBLE_IOCTL(TIOCSTI)

Similarly you can find the relevant manual page by running:

cd /usr/share/man/man2
for i in *; do (zcat $i | grep TIOCSTI) && echo $i; done

If the above fails (it didn't help me on the systems I tried it on), you
can read the corresponding kernel source code, look at manual pages of
other (hopefully better documented) systems, or Google for example code.
For TIOCSTI, see for
http://www.developerweb.net/forum/a...php/t-3799.html

--
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality?cup
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com