Raw Terminal I/O in lisp



Hi all,

I'd appreciate any help for c programmer like me who is new to lisp.

My platform is linux 2.6 (sbcl or lispworks). I'm trying to figure out
how to do terminal I/O in noncanonical mode with lisp.

Below is a simplied version of the example code in APUE.
Here's what I think I need to do:

1. Export the tty_raw function via FFI.

2. Build a console lisp program executable with LW's delivery function
or sbcl's (sb-ext:save-lisp-and-die :executable t)

But I am not quite sure how to write that "main" program in lisp, with
the standard file descriptor opened and pass that descriptor to the
tty_raw FFI function. (I assume that the C file number 0 1 2 doesn't
mean anything in lisp)

Any help would be appreciated!

TIA,
fungsin.



/* raw_term_io.c */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

int tty_raw (int fd) /* put terminal into a raw mode */
{
int err;
struct termios buf;

/* Echo off, canonical mode off, extended input
processing off, signal chars off. */
buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);

/* No SIGINT on BREAK, CR-to-NL off, input parity
check off, don't strip 8th bit on input, output
flow control off. */
buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);

/* Clear size bits, parity checking off. */
buf.c_cflag &= ~(CSIZE | PARENB);

/* Set 8 bits/char.*/
buf.c_cflag |= CS8;

/* Output processing off. */
buf.c_oflag &= ~(OPOST);

/* 1 byte at a time, no timer. */
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
if (tcsetattr(fd, TCSAFLUSH, &buf) < 0)
return(-1);

return(0);
}

int main (int argc, char *argv[])
{
int i;
char c;

if (tty_raw(STDIN_FILENO) < 0)
exit(-1);
printf("Enter raw mode characters, terminate with DELETE\n");
while ((i = read(STDIN_FILENO, &c, 1)) == 1) {
if ((c &= 255) == 0177) /* 0177 = ASCII DELETE */
break;
printf("%o\n", c);
}
exit(0);
}

.



Relevant Pages

  • Re: Vancouver Lisp Users Group meeting for February 2008 - Doing Evil Things with CL
    ... solution would be for the C code to have actually typedefed char* as ... At the end of the day, people don't want a raw C to Lisp binding; ... I couldn't convince Apple's GCC to output dwarf. ...
    (comp.lang.lisp)
  • Re: C++ to CLOS mapping
    ... Stay away from LARGE c++ libraries as far as you can, ... 3rd it'll make you hate lisp, hate c++ & hate programming i general. ... Test(const char *name); ... The automated tool will create a C-library which exposes only functions with C-bindings, but uses the C++-compiler to compile the function implementations, hence you get rid of all the C++-ABI troubles. ...
    (comp.lang.lisp)
  • Re: changing or not changing , thats the question of pop
    ... >> Lisp pass the variable by value. ... >> POP set the value of stk to point to the next cdr in the list. ... you have to use macro. ... char my_pop{ ...
    (comp.lang.lisp)
  • Re: static, dynamic and implicitely typed languages
    ... (char "sure I'm a string" 1) ... That is instead of declaring the type of the argument, Lisp language ... Functions like char and car have to check that the type of the passed argument is correct. ...
    (comp.lang.lisp)
  • Re: Newbie: How do I exit from a loop when a condition is met?
    ... There is also a "Lisp ... > (loop for char across aString ... (loop for char across string ...
    (comp.lang.lisp)