From: Andrei
Subject: unix:ioctl example, please!
Date: 
Message-ID: <2e04fd43.0409161253.4cca186b@posting.google.com>
Hi,

I am new to lisp. Currently I am trying to write a small program that
controls the linux device driver (an ATI capture card in my case) with
CMUCL.
I am puzzled at how I can return a c-structure using the
unix:unix-ioctl system call. I understand it has to do with some
"alien" functions but I'd rather take a look at some sample code
first. Could anyone point me to such a sample, please?

Thanks in advance,
Andrew
From: Eric Daniel
Subject: Re: unix:ioctl example, please!
Date: 
Message-ID: <10kkc6m1j88ikf9@corp.supernews.com>
In article <····························@posting.google.com>, Andrei wrote:
>  Hi,
>  
>  I am new to lisp. Currently I am trying to write a small program that
>  controls the linux device driver (an ATI capture card in my case) with
>  CMUCL.
>  I am puzzled at how I can return a c-structure using the
>  unix:unix-ioctl system call. I understand it has to do with some
>  "alien" functions but I'd rather take a look at some sample code
>  first. Could anyone point me to such a sample, please?
>  
>  Thanks in advance,
>  Andrew

Here's an example for the "sockaddr" structure used by socket system
calls:

This comes from the CMUCL source code:

#|      
/*      
 * Socket address, internet style.
 */
struct sockaddr_in {
        u_char  sin_len;
        u_char  sin_family;
        u_short sin_port;
        struct  in_addr sin_addr;
        char    sin_zero[8];
};
struct in_addr {
        u_long s_addr;
};    
      
|#    
#+BSD 
(def-alien-type inet-sockaddr
    (struct nil
      (sin-len unsigned-char)
      (family  unsigned-char)
      (port    unsigned-short)
      (addr    unsigned-long)
      (zero    (array char 8))))
    
#-BSD
(def-alien-type inet-sockaddr
  (struct nil
    (family #-alpha short #+alpha unsigned-short)
    (port unsigned-short)
    (addr #-alpha unsigned-long #+alpha unsigned-int)
    (zero (array char 8))))
    



This is how I use that "inet-sockaddr" definition in my code:

  (let ((socket (ext:create-inet-socket kind)))
    (alien:with-alien ((sockaddr ext::inet-sockaddr))
      (setf (alien:slot sockaddr 'ext::family) ext::af-inet)
      (setf (alien:slot sockaddr 'ext::port) (ext:htons port))
      (setf (alien:slot sockaddr 'ext::addr) addr)
      (unix:unix-bind socket (alien:alien-sap sockaddr)
         (alien:alien-size ext::inet-sockaddr :bytes)))
      ;;  Do something with the socket
    )


-- 
Eric Daniel