mini-command.scm 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber
  3. ; Miniature command processor.
  4. (define (command-processor ignore args)
  5. (let ((in (current-input-port))
  6. (out (current-output-port))
  7. (err (current-error-port))
  8. (batch? (member "batch" (map os-string->string args))))
  9. (let loop ()
  10. ((call-with-current-continuation
  11. (lambda (go)
  12. (with-handler
  13. (lambda (c punt)
  14. (cond ((or (serious-condition? c) (interrupt-condition? c))
  15. (display-condition c err)
  16. (go (if batch?
  17. (lambda () 1)
  18. loop)))
  19. ((warning? c)
  20. (display-condition c err))
  21. (else (punt))))
  22. (lambda ()
  23. (if (not batch?)
  24. (display "- " out))
  25. (let ((form (read in)))
  26. (cond ((eof-object? form)
  27. (newline out)
  28. (go (lambda () 0)))
  29. ((and (pair? form) (eq? (car form) 'unquote))
  30. (case (cadr form)
  31. ((load)
  32. (mini-load in)
  33. (go loop))
  34. ((go)
  35. (let ((form (read in)))
  36. (go (lambda ()
  37. (eval form (interaction-environment))))))
  38. (else (error 'command-processor
  39. "unknown command" (cadr form) 'go 'load (eq? (cadr form) 'load)))))
  40. (else
  41. (call-with-values
  42. (lambda () (eval form (interaction-environment)))
  43. (lambda results
  44. (for-each (lambda (result)
  45. (write result out)
  46. (newline out))
  47. results)
  48. (go loop))))))))))))))
  49. (define (mini-load in)
  50. (let ((c (peek-char in)))
  51. (cond ((char=? c #\newline) (read-char in) #t)
  52. ((char-whitespace? c) (read-char in) (mini-load in))
  53. ((char=? c #\")
  54. (let ((filename (read in)))
  55. (load filename)
  56. (mini-load in)))
  57. (else
  58. (let ((filename (read-string in char-whitespace?)))
  59. (load filename)
  60. (mini-load in))))))
  61. (define (read-string port delimiter?)
  62. (let loop ((l '()) (n 0))
  63. (let ((c (peek-char port)))
  64. (cond ((or (eof-object? c)
  65. (delimiter? c))
  66. (list->string (reverse l)))
  67. (else
  68. (loop (cons (read-char port) l) (+ n 1)))))))
  69. (define (byte-vector->string b)
  70. (let ((size (- (byte-vector-length b) 1)))
  71. (do ((s (make-string size))
  72. (i 0 (+ 1 i)))
  73. ((= i size)
  74. s)
  75. (string-set! s i (ascii->char (vector-ref b i))))))