[Ecls-list] how to call a lisp program von C with ECL

fBechmann at t-online.de fBechmann at t-online.de
Fri Nov 4 23:06:03 UTC 2005


Hi Dean,

thx for the exhaustive answer ;)

I'll check it.

Best regards,
   Frank

Dean O'Connor wrote:

> Hello Frank,
>
> I was in the same position a few weeks ago and was asking similar 
> questions.
> Unfortunately there does not seem to be an abundance of up to date 
> doco for how to do this yet.
>
> This file CVS file msvc\c\cinit.c has a basic example.
> Also recently some examples of calling Lisp scripts from multiple C 
> threads was added to CVS, into subdir examples\threads\import.
>
> Note: The last arg of cl_def_c_function specifies how many args the 
> declared function has.
>
> Probably one of my best sources of examples came from the XChatlisp 
> plugin that uses ECL.
> This C file has good stuff: 
> http://cvs.sourceforge.net/viewcvs.py/xchatlisp/eclplugin/plugin.c?rev=1.20&view=markup 
>
>
> If you look in the mail list archives and search for "dean" you will 
> find my thread of questions and answers that should help aswell.
>
> This link should show it:
> http://sourceforge.net/search/?type_of_search=mlists&forum_id=1307&group_id=30035&atid=0&words=dean&Search=Search 
>
>
> Accessing string args was something that took this mailing list to 
> answer.
> Here are some snippets from my C++ app and Lisp scripts that should 
> give you examples of how to accomplish it.
> Note: I configured my ecl with --enable-threads. Also I am not sure 
> about my values for the 2nd arg of ecl_make_foreign_data, but it seems 
> to work :)
>
> *** Simple loading of Lisp script in C ***
>
> int main(int argc, char* argv[])
> {
>        cl_boot(argc, argv);
>
>        printf("Booting CL completed.\n");
>
>        cl_object form_base = c_string_to_object("(load 
> \"myscript\")");        // No need for file extension. This will auto 
> detect .lsp .lisp or .fasl (compiled) files
>
>        cl_safe_eval(form_base, Cnil, OBJNULL);      // Execute the 
> command. ie. load the script.
>
>    return 0;
> }
>
> **** Setting foreign strings/int in C ****
>
>        char name[] = "VAR1";
>        char value[] = "STRING-VALUE";
>
>        // Note: Here you are setting the actual string pointer as the 
> foreign data value.
>
>        cl_object ff_obj = ecl_make_foreign_data(c_string_to_object("(* 
> :char)"), strlen(value)+1, value);
>        cl_set(c_string_to_object(name), ff_obj);
>
>       int global_int = 10;
>
>       cl_object ff_obj2 = 
> ecl_make_foreign_data(c_string_to_object("INT"), 
> sizeof(global_int),&global_int);
>      cl_set(c_string_to_object("VAR2"), ff_obj2);
>
> **** A C function (added to Lisp world) that returns a foreign string 
> ****
>
> char returned_str[] = "String returned";
>
> static cl_object
> new_function()
> {
>        cl_object ff_obj = ecl_make_foreign_data(c_string_to_object("(* 
> :char)"), strlen(returned_str)+1, returned_str);
>        return ff_obj;
> }
>
> **** Lisp script: Accessing those foreign variables ***
>
> ;; Note: Further examples of using convert-from-foreign-string can be 
> found in CVS source contrib/win32/win32.lisp
>
> ;; To get foreign string values (eg. that value of "VAR1" variable set 
> in above C function)
>
> (ffi:convert-from-foreign-string var1 :null-terminated-p t)
>
> ;; To get an foreign integer value.
>
> (ffi:deref-pointer var2 :int)
>
> **** Alternate (OLD ?) method for setting foreign strings ***
>
>    // Note: Here you are setting the pointer TO the string pointer as 
> the foreign data value.
>    // IMO the previous method is better cause you don't need to create 
> a pointer-to-pointer like this method.
>
>   char *string_ptr = "hi there";
>   cl_object f_obj = 
> ecl_make_foreign_data(make_simple_string("CSTRING"), 
> strlen(string_ptr), (void*)&string_ptr);
>
>    cl_set(c_string_to_object("VAR3"), f_obj);
>
> ;; Note: This method will crash ECL if you try and use it for the 
> first method as it expects a pointer-to-pointer.
>
> (ffi:deref-pointer var3 :cstring)
>
>
> Hope that helps, Good luck.
>
> Cheers
> Dean.
>
> fBechmann at t-online.de wrote:
>
>> Hi everybody:
>>
>> what I want to do: call a lisp program (either precompiled or plain 
>> lisp file, given by its name) from within a C program, pasing some 
>> string args by the way, and reading  the output of the program from 
>> its stdout (and stderr). It's good enough for my purposes to perform 
>> that call synchronously.
>>
>> Any pointer would be appreciated. Does anyone also has suggestions 
>> where I could find a more explanatory of the C interface, other than 
>> in the developer's guide (and by reading the sources of course).
>>
>> Thx in advance, Frank
>>
>>
>> -------------------------------------------------------
>> SF.Net email is sponsored by:
>> Tame your development challenges with Apache's Geronimo App Server. 
>> Download
>> it for free - -and be entered to win a 42" plasma tv or your very own
>> Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
>> _______________________________________________
>> Ecls-list mailing list
>> Ecls-list at lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/ecls-list
>
>
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by:
> Tame your development challenges with Apache's Geronimo App Server. 
> Download
> it for free - -and be entered to win a 42" plasma tv or your very own
> Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
> _______________________________________________
> Ecls-list mailing list
> Ecls-list at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ecls-list
>





More information about the ecl-devel mailing list