Changing UNIX tty driver keys -- Suggested changes to "termios" module

D

Derek Peschel

Should I add an RFE to SourceForge too? I'd like a wide audience in case
someone has enough experience to comment or is solving the same problem.

I'm using the urwid library which uses curses. On my system (Mac OS 10.3.7)
I specifically have ncurses. The programs I'm running turn off echoing and
set raw mode but don't disable interrupts. For development purposes I like
having interrupts, but my preferred keystrokes (WordStar) conflict with the
tty driver's use of ^C, ^Z, ^V, and maybe other keys.

Here's a piece of code based on the example in section 8.8.1 of the Python
Library Reference. It doesn't handle ^V yet.
------------------------------------------------------------------------------
termios_cc = 6 # magic index -- not 4 which is position
# in the C struct
termios__POSIX_VDISABLE = '\xff' # signals are set to this
# when they don't corres-
# pond to any char.

fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
oldterm_int = oldterm[termios_cc][termios.VINTR]
oldterm_quit = oldterm[termios_cc][termios.VQUIT]
if ord(oldterm_int) != 3: # ^C
sys.exit("interrupt char isn't ^C")
if ord(oldterm_quit) != 28: # ^\
sys.exit("quit char isn't ^\\")
# no way to check whether applications (telnet, screen)
# are looking for ^^
# no check yet for any signals set to ^^

newterm = termios.tcgetattr(fd)
newterm[termios_cc][termios.VQUIT] = chr(30) # ^^
newterm[termios_cc][termios.VINTR] = chr(28) # ^\
try:
termios.tcsetattr(fd, termios.TCSADRAIN, newterm)
self.ui.run_wrapper(self.run)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, oldterm)
------------------------------------------------------------------------------

I'd like to handle errors and race conditions better, but I don't know what
kinds can happen in practice. I'd also like to make the code work on other
versions of UNIX.

Easy suggested improvements to the library: Define _POSIX_VDISABLE and names
for the fields of the struct, so that termios__POSIX_VDISABLE and termios_cc
become termios._POSIX_VDISABLE and termios.cc.

Harder improvements: Some functions that abstract the things I'm doing
(checking the current characters, changing a group of them in one operation).
I assume two signals should never be set to the same character, unless they
are disabled. Is it possible to make the state variables invisible? Is that
a good idea?

Thanks,

-- Derek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top