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

Derek Peschel dpeschel at eskimo.com
Mon Aug 7 22:36:56 EDT 2006


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



More information about the Python-list mailing list