[q-lang-users] Forth-like KEY
Brought to you by:
agraef
|
From: <mip...@ya...> - 2004-02-05 07:46:16
|
I thought I'd go ahead and share this, because it
took some effort and because I find the result very
useful =) This code implements a special
(immed_input E) which 'evaluates' E in the context of
a nonbuffered nonechoing terminal. This makes some
simple and not-so-simple terminal programming very
easy to do, in my experience.
Here I just went for immed_input and key, as familiar
from termios-fiddling Scheme programming -- but GHC
has (hSetBuffering h None) and (hSetEcho h False)
which would work more generally, nicely building to a
(getpass Prompt) as well as a 'key'.
I'd appreciate any comments anyone has on the style.
(also, does a wiki exist for Q that I could post such
code to?)
// Thanks to 'info libc' and q's example 'getpass.q'
ttysane F (ATTR, TRAPS)
= tcsetattr (fileno F) TCSANOW ATTR ||
zipwith trap TRAPS [SIGINT, SIGTSTP];
mutate [_|Xs] 0 M = [M|Xs];
mutate [X|Xs] N M = [X|mutate Xs (N - 1) M];
attr_raw (IF,OF,CF,LF,IS,OS,CC)
= (IF,OF,CF,LF2,IS,OS,CC2)
where LF2 = LF and not (ICANON or ECHO),
CC2 = mutate (mutate CC VMIN 1) VTIME 0;
ttyraw F = assert (isatty FD) || (ATTR, TRAPS)
where TRAPS = map (trap SIG_IGN) [SIGINT, SIGTSTP],
FD = fileno F,
ATTR = tcgetattr FD,
() = tcsetattr FD TCSAFLUSH (attr_raw ATTR);
special immed_input E;
immed_input E = Result
where F:File = fopen ctermid "r+",
(ATTR, TRAPS) = ttyraw F,
Result = E,
_ = ttysane F (ATTR, TRAPS);
key = immed_input readc;
________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping"
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html
|