;;;; ;;;; readline.stk -- Access to the GNU-readline lib ;;;; (or libedit if absent :-<) ;;;; ;;;; Copyright © 2007-2010 Erick Gallesio - I3S-CNRS/ESSI ;;;; ;;;; ;;;; This program is free software; you can redistribute it and/or modify ;;;; it under the terms of the GNU General Public License as published by ;;;; the Free Software Foundation; either version 2 of the License, or ;;;; (at your option) any later version. ;;;; ;;;; This program is distributed in the hope that it will be useful, ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;;; GNU General Public License for more details. ;;;; ;;;; You should have received a copy of the GNU General Public License ;;;; along with this program; if not, write to the Free Software ;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, ;;;; USA. ;;;; ;;;; Author: Erick Gallesio [eg@essi.fr] ;;;; Creation date: 25-Jun-2007 11:48 (eg) ;;;; Last file update: 5-Aug-2010 18:52 (eg) ;;;; (define-module READLINE (export try-initialize-readline readline add-history read-history write-history read-with-history) ;;;; In module READLINE (define readline #f) (define add-history #f) (define read-history #f) (define write-history #f) (define (read-with-history :optional (prompt "> ")) (let ((line (readline prompt))) (unless (or (eof-object? line) (equal? line "")) (add-history line)) line)) (define (try-initialize libname) ;; Return #t if we were able to find a readline library (with-handler (lambda (c) ;; If we are here, this probably because library is not present #f) (let ((%readline (make-external-function "readline" '(:string) :pointer libname))) (set! readline (lambda (prompt) (let ((ptr (%readline prompt))) (if (eq? ptr (void)) #eof (let ((str (cpointer->string ptr))) ;; GNU/Readline always returns a string which is ;; dynamically allocated. Take care of that. (free-bytes ptr) str))))) (set! add-history (make-external-function "add_history" '(:string) :void libname)) (set! read-history (make-external-function "read_history" '(:string) :int libname)) (set! write-history (make-external-function "write_history" '(:string) :int libname)) (let ((support (if (%get-symbol-address "el_set" "") ;; We have found a realine library which is in fact ;; libedit (macos) 'libedit 'readline))) ;; Retain that the kind of readline support we have (key-set! *%system-state-plist* :readline support))) ;; return #t to tell that everything is alright #t)) (define (try-initialize-readline) (let ((suffix (%shared-library-suffix))) ;; Try in sequence the GNU readline and BSD libedit (which has readline support) (or (try-initialize (string-append "libreadline." suffix)) (try-initialize (string-append "libedit." suffix))))) ;; End of module READLINE ) (provide "readline")