Subject: [clisp-list] FFI question

From: myriam <[email-masked]>
Date: Tue Jan 29 09:43:01 2002

Hi!

How do you make an ffi call to something like gethostname where the
value of host is passed through a pointer and not returned? 

TIA

                                    myriam

---

From: myriam <[email-masked]>
Date: Thu Jan 31 06:25:05 2002

> value of host is passed through a pointer and not returned? 

Unfortunately, I still don't have an answer for that. 

I did: 

(defconstant *hostname* (make-string 15))

(def-call-out gethostname
   (:arguments (hostname c-string)
               (len int))
   (:return-type int))

(gethostname *hostname* 15)

but that does not set the value of *hostname*. 

                                    myriam

---

From: Erik Winkels <[email-masked]>
Date: Thu Jan 31 07:06:10 2002

> (defconstant *hostname* (make-string 15))
[...]
> (gethostname *hostname* 15)

Isn't that because you declared *hostname* to be a constant, hence
its value cannot be changed anymore subsequently?  Try defvar instead.

---

From: Sam Steingold <[email-masked]>
Date: Thu Jan 31 07:32:06 2002

> * In message <[email-masked]>
> * On the subject of "Re: [clisp-list] FFI question"
> * Sent on 31 Jan 2002 09:23:09 -0500
> * Honorable myriam <[email-masked]> writes:
>

commented out in CLISP/modules/bindings/linuxlibc6/linux.lisp:

(def-c-call-out gethostname (:arguments (name c-pointer) (len size_t)) ; ??
                            (:return-type int))
 
(def-c-call-out sethostname (:arguments (name (c-pointer) (len sizet)) ; ??
                            (:return-type int))

please try this and report the results.

Bruno should be able to give an authoritative question, but he is busy.

-- 
Sam Steingold (http://www.podval.org/~sds)
Keep Jerusalem united! <http://www.onejerusalem.org/Petition.asp>
Read, think and remember! <http://www.iris.org.il> <http://www.memri.org/>
OK, so you're a Ph.D.  Just don't touch anything.

---

From: myriam <[email-masked]>
Date: Thu Jan 31 08:07:04 2002

> please try this and report the results.

Okay, I've defined gethostname as above and tried: 

(defvar *hostname* (make-string 15))
;; I thought that a defconstant would make sure that the string does
;; not get garbage collected. 

(gethostname *hostname* 15)
*** - "" cannot be converted to the
foreign type FFI:C-POINTER

Any ideas?

> Bruno should be able to give an authoritative question, but he is

If not, hopefully he'll clarify. I am surprised that it's commented
out from the linux bindings. Unfortunately, I need to do something
like that. 

                                    myriam

---

From: Reini Urban <[email-masked]>
Date: Sat Feb 2 13:12:17 2002

myriam schrieb:
> *** - "

defvar assigns a variable on the lisp heap, but the def-c-call-out gethostname
needs 
a memory location on the extra c-heap, which is similar to a simple malloc
there.
does gethostname() strcpy the name to char* name or points to the string on
the system heap?

you would need something like this (untested) with a strcpy'ing gethostname.

(ffi:def-c-call-out gethostname (:arguments (name c-string) (len
size_t))(:return-type int))
(ffi:def-c-var c-hostname (:type c-string) (:alloc :malloc-free))
(if (zerop (linux:gethostname c-hostname 15)      ; 0 on success
  (defvar hostname (ffi:deref c-hostname)))         ; deref converts c-string
to lisp type

and link the glibc to clisp. don't know if the dynamic-ffi works here.

I've written a short and simple summary and overview of various FFI's at 
http://xarch.tu-graz.ac.at/autocad/lisp/ffis.html
-- 
Reini Urban
  http://xarch.tu-graz.ac.at/home/rurban/
  http://tv.mur.at/ (kulturelles)

---

From: Myriam Abramson <[email-masked]>
Date: Tue Feb 5 07:43:02 2002

Thanks but it's my understanding that def-c-var has to be the name of a c
variable. You can't make your own c-variables on the lisp side. 
Since there is no such c variable, the code below does not link: 

Undefined			 first referenced
 symbol  			    in file
_lisp__c_2Dhostname                 foreign.o
ld: fatal: Symbol referencing errors. No output written to lisp.run

> _______________________________________________
> clisp-list mailing list
> [email-masked]
> https://lists.sourceforge.net/lists/listinfo/clisp-list

-- 

                                    myriam