I am trying to use a package function in my lisp library that will
later be called from a C application. To be precise, it is shorten
function from the str package.

Here is my lisp library text:

;; (require :str)
;; (use-package :str)

(defun echo-string (s)
  (coerce (format nil "Recieved input: ~10A" (shorten-string s)) 'base-string))

(defun shorten-string (s)
  (declare (string s))
  (let ((l (length s)))
    (if (> l 10)
        (concat (shorten 7 s) (substring (- l 3) nil s))
        s)))

First lines are commented out because i do not know their necessity in
the library. That's part of the question.

However, all i get when trying to use any function from this library
is a number of undecipherable errors, like this:

https://pastebin.com/Sn8jgE9f

Here is my C code for a minimal case:

#include <stdlib.h>
#include <stdio.h>
#include <ecl/ecl.h>

// utility functions

cl_object make_cl_string(const char * pstring)
{
    return ecl_make_constant_base_string(pstring, strlen(pstring));
}

int main(int argc, const char* argv[])
{
   /* Initialize ECL */
   ecl_set_option(ECL_OPT_TRAP_SIGSEGV, false);
   cl_boot(argc, argv);

   extern void init_lib_ASDF(cl_object);
   ecl_init_module(NULL, init_lib_ASDF);

   // is this necessary?
   extern void init_lib_ECL_QUICKLISP(cl_object);
   ecl_init_module(NULL, init_lib_ECL_QUICKLISP);
   cl_object ecl_module_name = make_cl_string("ecl-quicklisp");
   cl_require(1, ecl_module_name);
   cl_object str_module_name = make_cl_string("str");
   cl_require(1, str_module_name);
   // this code fails, not clear what use as package designator
   /* cl_object str_package_name = make_cl_string(":str"); */
   /* cl_use_package(1, str_package_name); */
   extern void init_embedded_console(cl_object);
   ecl_init_module(NULL, init_embedded_console);

   // library calls
   cl_object fname = make_cl_string("ECHO-STRING");
   cl_object echo_func = cl_find_symbol(1, fname);
   assert(ECL_SYMBOLP(pstate->echo_func));

   const char * line = "hello";
   cl_object call_result = cl_funcall(2, echo_func, make_cl_string(line));
   char * msg_str = malloc(call_result->string.fillp + 1);
   memcpy(msg_str, call_result->string.self, call_result->string.fillp + 1);
   printf("Output: %s\n", msg_str);
   free(msg_str);

   const char * line2 = "hello";
   call_result = cl_funcall(2, echo_func, make_cl_string(line2));
   msg_str = malloc(call_result->string.fillp + 1);
   memcpy(msg_str, call_result->string.self, call_result->string.fillp + 1);
   printf("Output: %s\n", msg_str);
   free(msg_str);

   return 0;
}


What should be done for me to be able to use some package (installed
with quicklisp) from my ECL library? How should the initialisation be
done?

Reply via email to