top-repl.scm 2.6 KB

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