top-repl.scm 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ;;; -*- mode: scheme; coding: utf-8; -*-
  2. ;;;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  3. ;;;; 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;;
  19. (define-module (ice-9 top-repl)
  20. #:use-module (ice-9 top-repl)
  21. #:use-module ((system repl repl) #:select (start-repl))
  22. ;; #:replace, as with deprecated code enabled these will be in the root env
  23. #:replace (top-repl))
  24. (define call-with-sigint
  25. (if (not (provided? 'posix))
  26. (lambda (thunk) (thunk))
  27. (lambda (thunk)
  28. (let ((handler #f))
  29. (dynamic-wind
  30. (lambda ()
  31. (set! handler
  32. (sigaction SIGINT
  33. (lambda (sig)
  34. (scm-error 'signal #f "User interrupt" '()
  35. (list sig))))))
  36. thunk
  37. (lambda ()
  38. (if handler
  39. ;; restore Scheme handler, SIG_IGN or SIG_DFL.
  40. (sigaction SIGINT (car handler) (cdr handler))
  41. ;; restore original C handler.
  42. (sigaction SIGINT #f))))))))
  43. (define (top-repl)
  44. (let ((guile-user-module (resolve-module '(guile-user))))
  45. ;; Use some convenient modules (in reverse order)
  46. (set-current-module guile-user-module)
  47. (process-use-modules
  48. (append
  49. '(((ice-9 r5rs))
  50. ((ice-9 session)))
  51. (if (provided? 'regex)
  52. '(((ice-9 regex)))
  53. '())
  54. (if (provided? 'threads)
  55. '(((ice-9 threads)))
  56. '())))
  57. (call-with-sigint
  58. (lambda ()
  59. (and (defined? 'setlocale)
  60. (catch 'system-error
  61. (lambda ()
  62. (setlocale LC_ALL ""))
  63. (lambda (key subr fmt args errno)
  64. (format (current-error-port)
  65. "warning: failed to install locale: ~a~%"
  66. (strerror (car errno))))))
  67. (let ((status (start-repl (current-language))))
  68. (run-hook exit-hook)
  69. status)))))