1 ; File: "server.scm", Time-stamp: <2007-04-04 11:40:46 feeley>
3 ; Copyright (c) 1996-2007 by Marc Feeley, All Rights Reserved.
5 ; This file shows how to use the C-interface to implement a "Scheme
6 ; server", that is a program which is mainly written in C and that
7 ; makes calls to Scheme functions to access certain services.
9 ;------------------------------------------------------------------------------
11 ; This is a simple server that can evaluate a string of Scheme code.
13 (define (catch-all-errors thunk)
14 (with-exception-catcher
16 (write-to-string exc))
19 (define (write-to-string obj)
20 (with-output-to-string
22 (lambda () (write obj))))
24 (define (read-from-string str)
25 (with-input-from-string str read))
27 ; The following "c-define" form will define the function "eval_string"
28 ; which can be called from C just like an ordinary C function. The
29 ; single argument is a character string (C type "char*") and the
30 ; result is also a character string.
32 (c-define (eval-string str) (char-string) char-string "eval_string" "extern"
34 (lambda () (write-to-string (eval (read-from-string str))))))
36 ;------------------------------------------------------------------------------