[cffi-devel] Help needed: Tcl/Tk via CFFI (OSX 10.4.6 Intel, AllegroCL)

Frank Goenninger fgoenninger at prion.de
Wed Apr 26 05:32:11 UTC 2006


Hi!

Thanks for looking into this ...
I want to call the functions Tcl_CreateInterp and Tcl_Eval from Lisp.  
So I defined the libs and the functions :

CODE:

(in-package :cl-user)

(defpackage :org.gt.test.cffi-tcltk
   (:use #:common-lisp)
   (:nicknames :gt.cffi-tcltk)
   (:export
    #:test-it          ;; test function
   ))

(in-package #:org.gt.test.cffi-tcltk)

(eval-when (:load-toplevel :compile-toplevel :execute)
#+asdf (progn
           #-cffi (progn
		    (asdf:operate 'asdf:load-op :cffi)
	            (use-package :cffi))
        )
)

(eval-when (:load-toplevel :execute)
   (progn
     (define-foreign-library Tcl
       (:darwin (:framework "Tcl")))
     (define-foreign-library Tk
       (:darwin (:framework "Tk")))
   )
)

(eval-when (:load-toplevel :compile :execute)
   (progn

     ;; See also:
     ;; http://aspn.activestate.com/ASPN/docs/ActiveTcl_/8.4/tcl/ 
TclLib/Eval.htm

     ;; Tcl_CreateInterp

     (defcfun ("Tcl_CreateInterp" %Tcl_CreateInterp) :pointer)

     (defun Tcl_CreateInterp ()
       (%Tcl_CreateInterp))

     ;; Tcl_EvalFile

     (defcfun ("Tcl_EvalFile" %Tcl_EvalFile) :int
       (interp        :pointer)
       (filename-cstr :pointer))

     (defun Tcl_EvalFile (interp filename)
       (with-foreign-pointer (filename-cstr (length filename) strlen)
	(setf (mem-ref filename-cstr :char (1- strlen)) 0)
	(lisp-string-to-foreign filename filename-cstr strlen)
         %Tcl_EvalFile interp filename-cstr))

     ;; Tcl_Eval

     (defcfun ("Tcl_Eval" %Tcl_Eval) :int
       (interp      :pointer)
       (script-cstr :pointer))

     (defun Tcl_Eval (interp script)
       (with-foreign-pointer (script-cstr (length script) strlen)
	(setf (mem-ref script-cstr :char (1- strlen)) 0)
	(lisp-string-to-foreign script script-cstr strlen)
	%Tcl_Eval interp script-cstr))
   )
)

---
Now, we should be able to call the functions as expected:

CODE:

;; Initialization mgmt - required to avoid multiple library loads

(defvar *initialized* nil)

(defun set-initialized ()
   (setq *initialized* t))

(defun reset-initialized ()
   (setq *initialized* nil))

;; Tcl/Tk functions: execute a file and execute a script

(defun exec-file (filename)
   (unless *initialized*          ;; Could be simplified
     (use-foreign-library Tcl)
     (use-foreign-library Tk)
     (set-initialized))

   (let* ((tcl-interp (Tcl_CreateInterp))
          (rc (Tcl_EvalFile tcl-interp filename)))
     (format t "~%Tcl_EvalFile returned ~D~%" rc)))

(defun exec-script (script)
   (unless *initialized*           ;; Could be simplified
     (use-foreign-library Tcl)
     (use-foreign-library Tk)
     (set-initialized))

   (let* ((tcl-interp (Tcl_CreateInterp))
          (rc (Tcl_Eval tcl-interp script)))
     (format t "~%Tcl_Eval returned ~D~%" rc)))

;; We test just the script here

(defun test-it ()
   (exec-script "puts \"Hi !\""))  ;; Hmmm - no output generated. Why ?

---

When calling the test function with:

CL-USER > (gt.cffi-tcltk:test-it)

I do not see any output anywhere. I also see that the returned value  
is like an error value ... I am on OS X 10.4.6 (Intel) with CFFI  
latest tarball and AllegroCL8.0.

Any help really appreciated !!!

Cheers
    Frank




More information about the cffi-devel mailing list