Subject: [clisp-list] FFI C enumerations

From: "Eric de Groot" <[email-masked]>
Date: Mon, 28 May 2001 01:26:08 -0700

Hello.

What is the CLISP FFI C type for enum??  I can't find it in the
documentation, and i've tried (c-enum ) (c-enumeration ) (enum )
(enumeration ) and all give me an error.  The following fails, tells me
"*** - Incomplete FFI type ENUM_NET_TYPE is not allowed here.":

(def-c-enum enum_net_type NET_TYPE_TCPIP NET_TYPE_SOCKET NET_TYPE_NAMEDPIPE)

(def-c-struct st_net
  (nettype enum_net_type)
  (fd int)
  .... )

And with (c-enum ) and the others it just says it doesent know what they
are:

(def-c-struct st_net
  (nettype (c-enum enum_net_type))
  (fd int)
  .... )

here's the corresponding c code:

enum enum_net_type { NET_TYPE_TCPIP, NET_TYPE_SOCKET, NET_TYPE_NAMEDPIPE };

typedef struct st_net {
  enum enum_net_type nettype;
  int fd;
  ....
};

Thanks.

-Eric de Groot

---

From: Peter Wood <[email-masked]>
Date: Mon, 28 May 2001 16:23:06 +0200

> (def-c-enum enum_net_type NET_TYPE_TCPIP NET_TYPE_SOCKET NET_TYPE_NAMEDPIPE)
<snip>
>   enum enum_net_type nettype;

Hi,

There's probably a better answer than this one, but here goes:
In C enum is compatible with int, so you should be able to do,

(def-c-struct st_net
  (nettype int)
  (fd int))

Regards,
Peter

Ps. If you don't like having "int" for readability, you could 
do:

(def-c-type c_enum int)

before.

---

From: Marco Antoniotti <[email-masked]>
Date: Mon, 28 May 2001 13:04:10 -0400

> From: Peter Wood <[email-masked]>
> Content-Type: text/plain; charset=us-ascii
> Content-Disposition: inline
> User-Agent: Mutt/1.2.5i
> Sender: [email-masked]
> X-BeenThere: [email-masked]
> X-Mailman-Version: 2.0.5
> Precedence: bulk
> List-Help: <mailto:[email-masked]?subject=help>
> List-Post: <mailto:[email-masked]>
> List-Subscribe: <http://lists.sourceforge.net/lists/listinfo/clisp-list>,
> 	<mailto:[email-masked]?subject=subscribe>
> List-Id: CLISP user discussion <clisp-list.lists.sourceforge.net>
> List-Unsubscribe: <http://lists.sourceforge.net/lists/listinfo/clisp-list>,
> 	<mailto:[email-masked]?subject=unsubscribe>
> List-Archive: <http://lists.sourceforge.net/archives//clisp-list/>
> Date: Mon, 28 May 2001 16:23:06 +0200
> Content-Length: 1186
> 

That is true, but sometimes the C compiler may complain for functions
which do not actually use the enumerated type.

I believe this is a shortcoming of CLisp FFI (shared by ACL FFI).  It
should not be difficult to add them.  After all CMUCL and LW have C
enumerations.

But I am talking without knowing much.

Cheers

Marco

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.

---

From: "Eric de Groot" <[email-masked]>
Date: Mon, 28 May 2001 14:24:30 -0700

Thanks for the info.  I'll try using int.  Otherwise this really sucks.
When you say it shouldnt be to hard to add (c-enum ), do you it shouldnt be
to hard for me to add, or should be to hard to the developers of clisp?  Are
the defs for these functions/macros available for editing somewhere in my
local clisp tree?  Thanks.

-Eric de Groot
mailto:[email-masked]

----- Original Message -----
From: "Marco Antoniotti" <[email-masked]>
To: <[email-masked]>
Cc: <[email-masked]>
Sent: Monday, May 28, 2001 10:04 AM
Subject: Re: [clisp-list] FFI C enumerations

> > List-Help:
<mailto:[email-masked]?subject=help>
> > List-Subscribe:
<http://lists.sourceforge.net/lists/listinfo/clisp-list>,
> > List-Unsubscribe:
<http://lists.sourceforge.net/lists/listinfo/clisp-list>,
> _______________________________________________
> clisp-list mailing list
> [email-masked]
> http://lists.sourceforge.net/lists/listinfo/clisp-list
>

---

From: "Eric de Groot" <[email-masked]>
Date: Mon, 28 May 2001 16:32:38 -0700

I just posted a question about using an enumeration defined with def-c-enum
as a var type, but there doesent seem to be support for it.  What's the
purpose of (def-c-enum ) then?  How can it be used if there isent an enum
type??  Thanks.

-eric

---

From: "Eric de Groot" <[email-masked]>
Date: Mon, 28 May 2001 17:11:09 -0700

More FFI problems;)  I switched my enumerations over to ints so clisp gets
past that, but instead now I get:

Compiling file /usr/local/lib/clisp/callmystuff.lisp ...
*** - Lisp stack overflow. RESET

What causes a stack overflow?  In callmystuff.lisp I have about 5-6
(def-c-structs ) each with about 10-15 members, followed by a single
(def-c-call-out ) at the end of the file:

(def-c-call-out myfun (:arguments (myarg (c-ptr st_mystuff) :in :alloca))
                           (:return-type (c-ptr st_mystuff)))

If I remove/comment out this c call out the compilation goes fine, showing 0
errors, but at soon as I add this back in I get the Lisp stack overflow.
Any ideas?  Thanks.

-Eric de Groot

---

From: "Eric de Groot" <[email-masked]>
Date: Mon, 28 May 2001 21:21:55 -0700

I still don't get this.  I think I figured out the source of the problem
though:

(def-c-struct st_stuff
  (next (c-ptr st_stuff))
  ...)

(def-c-struct st_mystuff
    (stuff (c-ptr st_stuff))
    ...)

If I remove the (next (c-ptr st_stuff)) I get no stack overflow.  st_stuff
is a linked list, the next pointer is screwing me up.  It's only when I use
st_mystuff in the def-c-call-out, if change the types in the def-c-call-out
to something else it also compiles fine.

Does anyone even use CLISP's FFI?  On the CLISP page there are some clisp
packages, but from lookin around in the source it doesent look like any use
the FFI.

Thanks again.

-Eric de Groot

----- Original Message -----
From: "Eric de Groot" <[email-masked]>
To: <[email-masked]>
Sent: Monday, May 28, 2001 5:11 PM
Subject: [clisp-list] FFI: "Lisp stack overflow. RESET"

> > Marco Antoniotti
========================================================

---

From: "Eric de Groot" <[email-masked]>
Date: Tue, 29 May 2001 02:12:57 -0700

Hello-

    Yeah, i'm just using int now instead, thanks, but I have a new problem
maybee you can help me with.  Btw, I'm running:

GNU CLISP 2.25.1 (released 2001-04-06) (built 3199588721) (memory
3199589569)
on Debian GNU/Linux 2.2

I'm trying to write the FFI lisp for libmysqlclient, here's what I've got:

mysql.lisp:

(in-package "MYSQL" :use '("LISP" "FFI"))

; typedef struct st_net {
;   enum enum_net_type nettype;
;   my_socket fd;
;   int fcntl;
;   unsigned char *buff,*buff_end,*write_pos,*read_pos;
;   char last_error[MYSQL_ERRMSG_SIZE];
;   unsigned int last_errno,max_packet,timeout,pkt_nr;
;   my_bool error,return_errno,compress;
;   unsigned long remain_in_buf,length, buf_length, where_b;
;   my_bool more;
;   char save_char;
; } NET;

(def-c-struct st_net
  (nettype int)
  (fd int)
  (fcntl int)
  (buff (c-ptr uchar))
  (buff_end (c-ptr uchar))
  (write_pos (c-ptr uchar))
  (read_pos (c-ptr uchar))
  (last_error (c-array char 200))
  (last_errno uint)
  (max_packet uint)
  (timeout uint)
  (pkt_nr uint)
  (error char)
  (return_errno char)
  (compress char)
  (remain_in_buf ulong)
  (length ulong)
  (buf_length ulong)
  (where_b ulong)
  (more char)
  (save_char char))

; typedef struct st_mysql_field {
;   char *name;   /* Name of column */
;   char *table;   /* Table of column if column was a field */
;   char *def;   /* Default value (set by mysql_list_fields) */
;   enum enum_field_types type; /* Type of field. Se mysql_com.h for types
*/
;   unsigned int length;  /* Width of column */
;   unsigned int max_length; /* Max width of selected set */
;   unsigned int flags;  /* Div flags */
;   unsigned int decimals; /* Number of decimals in field */
; } MYSQL_FIELD;

(def-c-struct st_mysql_field
  (name c-string)
  (table c-string)
  (def c-string)
  (type int)
  (length uint)
  (max_length uint)
  (flags uint)
  (decimals uint))

; typedef struct st_used_mem {   /* struct for once_alloc */
;  struct st_used_mem *next;   /* Next block in use */
;  unsigned int left;    /* memory left in block  */
;  unsigned int size;    /* size of block */
; } USED_MEM;

(def-c-struct st_used_mem
  (next (c-ptr st_used_mem)) ; *** comment this line out and no stack
overflow.. ***
  (left uint)
  (size uint))

; typedef struct st_mem_root {
;   USED_MEM *free;
;   USED_MEM *used;
;   unsigned int min_malloc;
;   unsigned int block_size;
;   void (*error_handler)(void);
; } MEM_ROOT;

(def-c-struct st_mem_root
  (free (c-ptr st_used_mem))
  (used (c-ptr st_used_mem))
  (min_malloc uint)
  (block_size uint)
  (error_handler (c-function)))

; struct st_mysql_options {
;   unsigned int connect_timeout,client_flag;
;   my_bool compress,named_pipe;
;   unsigned int port;
;   char *host,*init_command,*user,*password,*unix_socket,*db;
;   char *my_cnf_file,*my_cnf_group;
; };

(def-c-struct st_mysql_options
  (connect_timeout uint)
  (client_flag uint)
  (compress char)
  (named_pipe char)
  (port uint)
  (host c-string)
  (init_command c-string)
  (user c-string)
  (password c-string)
  (unix_socket c-string)
  (db c-string)
  (my_cnf_file c-string)
  (my_cnf_group c-string))

; typedef struct st_mysql {
;   NET  net;   /* Communication parameters */
;   char  *host,*user,*passwd,*unix_socket,*server_version,*host_info,
;   *info,*db;
;   unsigned int port,client_flag,server_capabilities;
;   unsigned int protocol_version;
;   unsigned int field_count;
;   unsigned long thread_id;  /* Id for connection in server */
;   my_ulonglong affected_rows;
;   my_ulonglong insert_id;  /* id if insert on table with NEXTNR */
;   my_ulonglong extra_info;  /* Used by mysqlshow */
;   unsigned long packet_length;
;   enum mysql_status status;
;   MYSQL_FIELD *fields;
;   MEM_ROOT field_alloc;
;   my_bool free_me;  /* If free in mysql_close */
;   my_bool reconnect;  /* set to 1 if automatic reconnect */
;   struct st_mysql_options options;
; } MYSQL;

(def-c-struct st_mysql
  (net st_net)
  (host c-string)
  (user c-string)
  (passwd c-string)
  (unix_socket c-string)
  (server_version c-string)
  (host_info c-string)
  (info c-string)
  (db c-string)
  (port uint)
  (client_flag uint)
  (server_capabilities uint)
  (protocol_version uint)
  (field_count uint)
  (thread_id ulong)
  (affected_rows ulong)
  (insert_id ulong)
  (extra_info ulong)
  (packet_length ulong)
  (status int)
  (fields (c-ptr st_mysql_field))
  (field_alloc st_mem_root)
  (free_me char)
  (reconnect char)
  (options st_mysql_options))

; MYSQL *  STDCALL mysql_init(MYSQL *mysql);

(def-c-call-out mysql_init (:arguments (mysql (c-ptr st_mysql) :in :alloca))
                           (:return-type (c-ptr st_mysql)))

; EOF

clisp compiles it fine as long as the (next ) member is commented out in
st_used_mem.  Any ideas?

Thanks.

-Eric de Groot

----- Original Message -----
From: "Peter Wood" <[email-masked]>
To: "Eric de Groot" <[email-masked]>
Sent: Monday, May 28, 2001 11:21 PM
Subject: Re: [clisp-list] What's the purpose of (def-c-enum ) then?

> On Mon, May 28, 2001 at 04:32:38PM -0700, Eric de Groot wrote:
> Hi
> def-c-enum works fine.  The reason your original didn't work (I think)
> is that you used it inside the def-c-struct.
>
> You had:
>
> This declares NET_TYPE_* to be constants 0, 1, 2 respectively.
>
> (def-c-struct st_net
>   (nettype enum_net_type)
>   (fd int)
>   .... )
>
> The first statement is fine.  But you can't even do the second in C!
> Your C "equivalent" which you gave was:
>
> typedef struct st_net {
> which seems correct(1), although *not* the same thing.  If you were
> going to write an equivalent in FFI it should be:
>
> (def-c-struct st_net
>  (def-c-enum enum_net_type ...)
>
> I don't think you can do that.  Thats why I suggested declaring
> nettype to be an int.
>
> You also ask if anyone uses the FFI.  Yes, look in
> $clisp-src/modules/*.
>
> You should also give more information when you ask questions.  What
> clisp version are you using?  What is it running on.  Have you made
> any changes, etc.  It's really difficult trying to *guess* what you
> are doing.
>
> Regards,
> Peter
>

---

From: Peter Wood <[email-masked]>
Date: Tue, 29 May 2001 13:15:44 +0200

> GNU CLISP 2.25.1 (released 2001-04-06) (built 3199588721) (memory

The latest is 2.26.

>   (next (c-ptr st_used_mem)) ; *** comment this line out and no stack

I don't know why this happens.  Sorry. 

Regards,
Peter

---

From: Marco Antoniotti <[email-masked]>
Date: Tue, 29 May 2001 09:36:05 -0400

> Content-Type: text/plain;
> 	charset="iso-8859-1"
> X-Priority: 3
> X-MSMail-Priority: Normal
> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
> Date: Mon, 28 May 2001 14:24:30 -0700
> Content-Length: 3852
> 

It would certinly be easier for the CLisp main developers.  Myself
personally, wouldn't know where to start to look at.

Cheers

---

From: Sam Steingold <[email-masked]>
Date: 29 May 2001 10:58:37 -0400

> * In message <00c901c0e7d3$dd7df8b0$4381fb18@CX417245D>
> * On the subject of "[clisp-list] FFI: "Lisp stack overflow. RESET""
> * Sent on Mon, 28 May 2001 17:11:09 -0700
> * Honorable "Eric de Groot" <[email-masked]> writes:
>
> More FFI problems;) I switched my enumerations over to ints so clisp

please try this patch for your enum problems.:
------------------------------------------
--- foreign1.lisp.~1.5.~	Fri May  4 18:06:07 2001
+++ foreign1.lisp	Tue May 29 10:52:44 2001
@@ -1051,6 +1051,7 @@
       (push `(DEFCONSTANT ,item ,next-value) forms)
       (setq next-value `(1+ ,item))
     )
+    (setf (gethash name *c-type-table*) 'int)
     `(PROGN ,@(nreverse forms) ',name)
 ) )
 
------------------------------------------

> gets past that, but instead now I get:
> 

please send "callmystuff.lisp" (trim is down as much as possible)

People, _PLEASE_ _DO_ edit the quoted text in your messages.
100 lines of appended old messasges and 50 lines of headers of old
messages are _NOT_ appreciated.
Thanks.

-- 
Sam Steingold (http://www.podval.org/~sds)
Support Israel's right to defend herself! <http://www.i-charity.com/go/israel>
Read what the Arab leaders say to their people on <http://www.memri.org/>
Diplomacy is the art of saying "nice doggy" until you can find a rock.

---

From: Sam Steingold <[email-masked]>
Date: 30 May 2001 13:24:55 -0400

> * In message <013c01c0e7f6$e518c230$4381fb18@CX417245D>
> * On the subject of "Re: [clisp-list] FFI: "Lisp stack overflow. RESET""
> * Sent on Mon, 28 May 2001 21:21:55 -0700

I believe I fixed this bug,
You will have to get the sources from the CVS.

Thanks for reporting the bug.

-- 
Sam Steingold (http://www.podval.org/~sds)
Support Israel's right to defend herself! <http://www.i-charity.com/go/israel>
Read what the Arab leaders say to their people on <http://www.memri.org/>
(let((a'(list'let(list(list'a(list'quote a)))a)))`(let((a(quote ,a))),a))