From: Zafrir E. Gan
Subject: returning a string from c code in CL
Date: 
Message-ID: <EeInley00Vom4Atm9E@andrew.cmu.edu>
Hi, I'm using the foreign functions interface in allegro common lisp to
run some c code.
What I can't do is return a string from the c function.  I tried using
the "lisp_value()"
function suggested in the docs, but I can't find a c library to define
it. HELP...

thanks in advance,


    \|/Zap\|/  ==>Happy Happy Joy Joy<==

From: Joe Konstan
Subject: Re: returning a string from c code in CL
Date: 
Message-ID: <12vud6INNhu4@agate.berkeley.edu>
In article <··················@andrew.cmu.edu>, ·····@andrew.cmu.edu (Zafrir E. Gan) writes:
|> Hi, I'm using the foreign functions interface in allegro common lisp to
|> run some c code.
|> What I can't do is return a string from the c function.  I tried using
|> the "lisp_value()"
|> function suggested in the docs, but I can't find a c library to define
|> it. HELP...

Well, I posted one answer a week or two ago in my socket code.  Here are the
relevant parts:

This code calls a C function that always returns a Lisp string:

;; define the lisp string and a pointer to it

(defvar *sockagent-string-buffer* (make-string 2048))

(defvar *sockagent-string-buffer-index*
           (register-value '*sockagent-string-buffer*))

;; define a function as an interface

(defun get-socketagent-string ()
  (get-socketagent-string-allegro *sockagent-string-buffer-index*))

;; the foreign function returns a lisp object

(defforeign 'get-socketagent-string-allegro
  :entry-point "_get_socketagent_string_allegro"
  :arguments '(integer)
  :return-type :lisp)

/* now the C code - I leave out get_socketagent_string() which just returns
   an ordinary C string */

#include <stdio.h>
#include "lisp.h"

char *get_socketagent_string();

/* this takes a lisp string, gets the C string, and copies it into the lisp
   string followed by a space (important for my use) */
  
char *get_socketagent_string_allegro(stbuf_index)
int stbuf_index;
{
    strcpy ((char *)Vecdata(SymbolValue(lisp_value(stbuf_index))),
            get_socketagent_string());
    strcat ((char *)Vecdata(SymbolValue(lisp_value(stbuf_index)))," ");
    return (char *) SymbolValue(lisp_value(stbuf_index));
}



Good luck.  This was pretty clearly laid out in the Allegro manual
under passing strings from C to Lisp.

Joe Konstan
·······@cs.berkeley.edu
From: Gerard Kim
Subject: Re: returning a string from c code in CL
Date: 
Message-ID: <l59k3cINNpso@mensa.usc.edu>
Hello, every Lisp wizards out there,

I, too, am trying to call a C program from Lisp.  The only function I am aware of is
the (run-program ...).  Can some one point me to literatures or documents which
describe how to interface other programs to common lisp, if they exist.  I could not
find any description for, for example (run-program ...),  neither from "Common Lisp" 
(Franz Inc) nor "Common Lisp" (Guy Steel).  Your own help would be appreciated too.

Thanks in advance,

Gerard J. Kim
CSD USC

PS: Actually the specific thing I am trying to do is call a C program and run it
concurrently with the LISP interpreter I called the program from.
From: Charles A. Cox
Subject: Re: returning a string from c code in CL
Date: 
Message-ID: <COX.92Jul5233419@crisp.Franz.COM>
In article <··················@andrew.cmu.edu> ·····@andrew.cmu.edu (Zafrir E. Gan) writes:

> Hi, I'm using the foreign functions interface in allegro common lisp to
> run some c code.
> What I can't do is return a string from the c function.

  Allegro provides functions ff:char*-to-string and ff:string-to-char*
which can be used for converting between integers, representing
addresses to C-style strings, and LISP strings.  Here's an example
from Allegro CL 4.1:

 USER(1): (shell "cat xx.c")
 char s[50];

 char *
 rstring()
 {
     strcpy(s, "Hello from C");
     return (s);
 }
 0
 USER(2): (shell "cc -c xx.c")
 0
 USER(3): (load "xx.o")
 ; Foreign loading /usr/tmp/xx.o.
 T
 USER(4): (ff:defforeign 'rstring :return-type :integer)
 T
 USER(5): (ff:char*-to-string (rstring))
 "Hello from C"
 USER(6): 

	Charley
--
---
Charles A. Cox, Franz Inc.        1995 University Avenue, Suite 275
Internet: ···@franz.com           Berkeley, CA  94704
uucp:     uunet!franz!cox         Phone: (510) 548-3600; FAX: (510) 548-8253
                                  > Please Note:  Our new area code is 510.