command.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Interpreting commands.
  3. ; Commands begin with a comma because it's an unshifted key and because
  4. ; someone else (I can't remember who) was already using it as a command
  5. ; prefix.
  6. (define command-prefix #\,)
  7. ; Fire up the processor.
  8. ;
  9. ; The double-paren around the WITH-HANDLER is because it returns a
  10. ; thunk which is the thing to do after the command-processor exits.
  11. ;
  12. ; There are two version, one for an initial start and the other for restarting
  13. ; with an existing user context.
  14. (define (start-command-processor resume-args greeting-thunk start-thunk)
  15. (restart-command-processor resume-args #f greeting-thunk start-thunk))
  16. (define (restart-command-processor resume-args context greeting-thunk start-thunk)
  17. ((with-handler command-loop-condition-handler
  18. (lambda ()
  19. (start-command-levels resume-args
  20. context
  21. greeting-thunk
  22. start-thunk
  23. script-runner
  24. real-command-loop
  25. #f ; no condition
  26. #f ; not inspecting
  27. (current-input-port)
  28. (current-output-port)
  29. (current-error-port))))))
  30. ; Entry for initialization & testing.
  31. (define (command-processor command-env resume-args)
  32. (start-command-processor resume-args
  33. values
  34. (lambda ()
  35. (set-user-command-environment! command-env)
  36. unspecific)))
  37. ;----------------
  38. ; Command loop
  39. ; Called from:
  40. ; 1. condition handler, 2. abort-to-level, 3. breakpoint
  41. ;
  42. ; The condition is either #F or whatever caused a new command loop to be
  43. ; started.
  44. (define (command-loop condition)
  45. ;; The handler may have gotten unwound by `raise'
  46. (with-handler command-loop-condition-handler
  47. (lambda ()
  48. (push-command-level condition #f))))
  49. ; Install the handler, bind $NOTE-UNDEFINED to keep from annoying the user,
  50. ; bind $FOCUS-BEFORE to avoid keeping state on the stack where it can be
  51. ; captured by CALL/CC, display the condition and start reading commands.
  52. (define (real-command-loop)
  53. (let ((results-cell (make-cell #f)))
  54. (let-fluids $note-undefined #f ;useful
  55. $command-results results-cell
  56. (lambda ()
  57. (display-command-level-condition
  58. (command-level-condition (command-level)))
  59. (let command-loop ()
  60. (let ((command (read-command-carefully (command-prompt)
  61. (form-preferred?)
  62. (command-input))))
  63. (cell-set! results-cell #f)
  64. (execute-command command)
  65. (let ((results (cell-ref results-cell)))
  66. (if results
  67. (show-command-results results)))
  68. (command-loop)))))))
  69. ; For saving the results returned by a command.
  70. (define $command-results (make-fluid #f))
  71. (define (set-command-results! results . maybe-set-focus-object?)
  72. (fluid-cell-set! $command-results results)
  73. (if (or (null? maybe-set-focus-object?)
  74. (car maybe-set-focus-object?))
  75. (case (length results)
  76. ((0)
  77. (values))
  78. ((1)
  79. (if (not (eq? (car results)
  80. (unspecific)))
  81. (set-focus-object! (car results))))
  82. (else
  83. (set-focus-object! results)))))
  84. (define (display-command-level-condition condition)
  85. (if condition
  86. (display-condition condition (command-output)
  87. (condition-writing-depth) (condition-writing-length))))
  88. ; If #T anything that doesn't start with the command prefix (a comma) is
  89. ; treated as an argument to RUN. If #F no commas are needed and RUN
  90. ; commands must be explicit.
  91. (define (form-preferred?)
  92. (not (value-stack)))
  93. ; If true then print a menu when showing results.
  94. (define (inspect-mode?)
  95. (value-stack))
  96. ; Go up to the previous level or exit if there are no more levels.
  97. (define (pop-command-level)
  98. (let ((levels (command-levels)))
  99. (if (null? (cdr levels))
  100. (cond ((batch-mode?)
  101. ; perhaps this should use scheme-exit-now, but I'm
  102. ; worried that it is what handles normal EOF. (HCC)
  103. (exit-command-processor (lambda () 0)))
  104. ((y-or-n? "Exit Scheme 48" #t)
  105. (exit-command-processor (lambda () 1)))
  106. (else
  107. (abort-to-command-level (car levels))))
  108. (let ((level (cadr (command-levels))))
  109. (if (command-level-paused-thread level)
  110. (kill-paused-thread! level))
  111. (proceed-with-command-level level)))))
  112. (define (exit-command-processor thunk)
  113. (throw-to-command-level (top-command-level)
  114. (lambda () thunk)))
  115. ; Condition handler.
  116. ; For warnings and notes we go stop the current level or continue, for
  117. ; errors and interrupts we stop the level or exit. We always continue for
  118. ; warnings on the command level thread to avoid circularity problems.
  119. (define (command-loop-condition-handler c next-handler)
  120. (cond ((or (warning? c)
  121. (note? c))
  122. (if (break-on-warnings?)
  123. (deal-with-condition c)
  124. (begin (force-output (current-output-port)) ; keep synchronous
  125. (display-condition c (current-error-port)
  126. (condition-writing-depth) (condition-writing-length))
  127. (unspecific)))) ;proceed
  128. ((or (serious-condition? c) (interrupt-condition? c))
  129. (if (batch-mode?)
  130. (begin (force-output (current-output-port)) ; keep synchronous
  131. (display-condition c (current-error-port)
  132. (condition-writing-depth) (condition-writing-length))
  133. (let ((status
  134. (cond
  135. ((error? c) 1)
  136. ((violation? c) 3) ; historical, probably nonsense
  137. (else 2))))
  138. (scheme-exit-now status)))
  139. (deal-with-condition c)))
  140. ((reset-command-input-condition? c)
  141. (unspecific)) ;proceed
  142. (else
  143. (next-handler))))
  144. ; Stop the current level either by pushing a new one or restarting it.
  145. ; If we restart the current level we save it as the focus object to give
  146. ; the user a chance to figure out what happened.
  147. (define (deal-with-condition c)
  148. (if (push-command-levels?)
  149. (command-loop c)
  150. (let ((level (car (command-levels))))
  151. (set-focus-object! level)
  152. (display-condition c (command-output)
  153. (condition-writing-depth) (condition-writing-length))
  154. (restart-command-level level))))
  155. (define (abort-to-command-level level)
  156. (cond ((eq? level (car (reverse (command-levels))))
  157. (newline (command-output))
  158. (write-line "Top level" (command-output)))
  159. (else
  160. (display "Back to " (command-output))))
  161. (restart-command-level level))
  162. ; The prompt is "level-number environment-id-string> " or just
  163. ; "environment-id-string> " at top level. The id-string is empty for the
  164. ; current user package and the name of the package otherwise.
  165. ; The ">" changes to ":" in command-preferred mode.
  166. (define (command-prompt)
  167. (let ((level (- (length (command-levels)) 1))
  168. (id (environment-id-string (environment-for-commands))))
  169. (string-append (if (= level 0)
  170. ""
  171. (number->string level))
  172. (if (or (= level 0) (= (string-length id) 0))
  173. ""
  174. " ")
  175. id
  176. (if (form-preferred?)
  177. "> "
  178. ": "))))
  179. (define-generic environment-id-string &environment-id-string (env))
  180. (define-method &environment-id-string (env) "")
  181. ;----------------
  182. ; Loading scripts
  183. (define (script-runner)
  184. (run-script (cdr (focus-object))))
  185. ; This loads a script SRFI-22-style
  186. ; ARG is a list of command-line arguments after "run-script"
  187. (define (run-script arg)
  188. (run-script-handler (os-string->string (car arg)) (cdr arg)))
  189. (define *script-handler-alist* '())
  190. (define (define-script-handler tag proc)
  191. (set! *script-handler-alist*
  192. (cons (cons tag proc) *script-handler-alist*)))
  193. (define (run-script-handler tag args)
  194. (cond
  195. ((assoc tag *script-handler-alist*)
  196. => (lambda (pair)
  197. (silently
  198. (lambda ()
  199. ((cdr pair) args)))))
  200. (else
  201. (display "invalid argument to run-script-handler: " (current-error-port))
  202. (display tag (current-error-port))
  203. (newline (current-error-port))
  204. 1)))
  205. (define (EX_SOFTWARE) (shared-binding-ref (lookup-imported-binding "EX_SOFTWARE")))
  206. (define (with-srfi-22-error-handling thunk)
  207. (call-with-current-continuation
  208. (lambda (k)
  209. (with-handler
  210. (lambda (c punt)
  211. (if (serious-condition? c)
  212. (begin
  213. (display-condition c (current-error-port)
  214. (condition-writing-depth) (condition-writing-length))
  215. (k (EX_SOFTWARE)))
  216. (punt)))
  217. (lambda ()
  218. (thunk)
  219. 0)))))
  220. (define-script-handler "r5rs"
  221. (lambda (args)
  222. (with-srfi-22-error-handling
  223. (lambda ()
  224. (load-script-into (os-string->string (car args)) (interaction-environment))
  225. ((environment-ref (interaction-environment) 'main) (map os-string->string args))))))
  226. (define-script-handler "srfi-7"
  227. (lambda (args)
  228. (with-srfi-22-error-handling
  229. (lambda ()
  230. (eval '(load-package 'srfi-7) (user-command-environment))
  231. (eval `(load-srfi-7-script 'srfi-7-script ,(os-string->string (car args)))
  232. (user-command-environment))
  233. (let ((cell (make-cell #f))) ; kludge
  234. (let-fluid $command-results cell
  235. (lambda ()
  236. (eval '(in 'srfi-7-script '(run main))
  237. (user-command-environment))))
  238. ((car (cell-ref cell)) (map os-string->string args)))))))
  239. ;----------------
  240. ; Evaluate a form and save its result as the current focus values.
  241. ; The unspecific object is discarded.
  242. (define (evaluate-and-select form env)
  243. (call-with-values (lambda ()
  244. (eval form env))
  245. (lambda results
  246. (set-command-results! results)
  247. (apply values results))))
  248. ;----------------
  249. ; Printing command results. The results are also saved as the current
  250. ; focus object.
  251. (define (show-command-results results)
  252. (let ((out (command-output)))
  253. (case (length results)
  254. ((0)
  255. (display "; no values returned" out)
  256. (newline out))
  257. ((1)
  258. (show-command-result (car results))
  259. (if (inspect-mode?)
  260. (present-menu)))
  261. (else
  262. (display "; " out)
  263. (write (length results) out)
  264. (display " values returned" out)
  265. (if (inspect-mode?)
  266. (present-menu)
  267. (begin
  268. (newline out)
  269. (for-each show-command-result results)))))))
  270. (define (show-command-result result)
  271. (let ((out (command-output)))
  272. ((if (inspect-mode?)
  273. with-limited-output
  274. (lambda (p) (p)))
  275. (lambda ()
  276. (write-carefully result out)
  277. (newline out)))))
  278. ;----------------
  279. ; Sentinels - run after every command.
  280. (define *sentinels* '())
  281. (define (run-sentinels)
  282. (for-each (lambda (sentinel) (sentinel)) *sentinels*))
  283. (define (add-sentinel! sentinel)
  284. (if (not (memq sentinel *sentinels*))
  285. (set! *sentinels* (cons sentinel *sentinels*))))
  286. ;----------------
  287. ; Commands.
  288. (define environment-for-commands interaction-environment)
  289. (define command-environment
  290. (user-context-accessor 'command-environment interaction-environment))
  291. ;(define *command-structure* (unspecific))
  292. ;
  293. ;(define (command-structure)
  294. ; *command-structure*)
  295. ;
  296. ;(define (set-command-structure! structure) ; called on initial startup
  297. ; (set! *command-structure* structure))
  298. (define command-syntax-table (make-table))
  299. (define *command-help* '())
  300. (define (get-command-syntax name)
  301. (or (table-ref (user-command-syntax-table) name)
  302. (table-ref command-syntax-table name)))
  303. (define (define-command-syntax name help1 help2 arg-descriptions)
  304. (table-set! command-syntax-table name arg-descriptions)
  305. (if help1
  306. (set! *command-help* (add-help *command-help* name help1 help2))))
  307. (define (add-help help name help1 help2)
  308. (insert (list name
  309. (string-append (symbol->string name) " " help1)
  310. help2)
  311. help
  312. (lambda (z1 z2)
  313. (string<=? (cadr z1) (cadr z2)))))
  314. (define user-command-syntax-table
  315. (user-context-accessor 'user-command-syntax-table (lambda () (make-table))))
  316. (define user-command-environment
  317. (user-context-accessor 'user-command-environment (lambda () #f)))
  318. (define set-user-command-environment!
  319. (user-context-modifier 'user-command-environment))
  320. (define user-command-help
  321. (user-context-accessor 'user-command-help (lambda () *command-help*)))
  322. (define set-user-command-help!
  323. (user-context-modifier 'user-command-help))
  324. (define (define-user-command-syntax name help1 help2 arg-descriptions)
  325. (table-set! (user-command-syntax-table) name arg-descriptions)
  326. (if help1
  327. (set-user-command-help!
  328. (add-help (user-command-help) name help1 help2))))
  329. (define (execute-command command)
  330. (run-sentinels)
  331. (cond ((eof-object? command)
  332. (newline (command-output))
  333. (pop-command-level))
  334. ((not command)) ; error while reading
  335. (else
  336. (let* ((name (car command))
  337. (proc (eval name (user-command-environment))))
  338. (apply proc (cdr command))))))
  339. ;----------------
  340. ; Settings - these are cells for controlling the behavior
  341. ; of the command interpreter.
  342. ;
  343. ; This code is here so that the help listing can print out the settings
  344. ; and their current values.
  345. (define-record-type setting :setting
  346. (make-setting name type get set on-doc off-doc)
  347. setting?
  348. (name setting-name)
  349. ;; is #t for boolean or a predicate
  350. (type setting-type)
  351. (get setting-get)
  352. (set setting-set)
  353. ;; We have two documentation strings, one for `on' and one for `off'.
  354. (on-doc setting-on-doc)
  355. (off-doc setting-off-doc))
  356. (define (setting-boolean? setting)
  357. (eqv? #t (setting-type setting)))
  358. ; alist mapping names to :SETTING records
  359. (define *settings-alist* '())
  360. (define (lookup-setting name)
  361. (cond
  362. ((assq name *settings-alist*) => cdr)
  363. (else #f)))
  364. (define (add-setting name boolean? get set on-doc . maybe-off-doc)
  365. (set! *settings-alist*
  366. (insert (cons name
  367. (make-setting name boolean? get set on-doc
  368. (if (null? maybe-off-doc)
  369. #f
  370. (car maybe-off-doc))))
  371. *settings-alist*
  372. (lambda (z1 z2)
  373. (string<=? (symbol->string (car z1))
  374. (symbol->string (car z2)))))))
  375. (define (setting-value setting)
  376. ((setting-get setting)))
  377. (define (setting-set! setting value)
  378. (if (if (setting-boolean? setting)
  379. (and (not (eqv? value #t))
  380. (not (eqv? value #f)))
  381. (not ((setting-type setting) value)))
  382. (error 'setting-set!
  383. "invalid value for setting" (setting-name setting) value))
  384. ((setting-set setting) value))
  385. (define (setting-doc setting)
  386. (cond
  387. ((not (setting-boolean? setting))
  388. (setting-on-doc setting))
  389. ((setting-value setting)
  390. (setting-on-doc setting))
  391. (else
  392. (setting-off-doc setting))))
  393. ; Print out a list of the settings and their current values.
  394. (define (list-settings)
  395. (let ((o-port (command-output))
  396. (size (apply max
  397. (map (lambda (z)
  398. (string-length (symbol->string (setting-name (cdr z)))))
  399. *settings-alist*))))
  400. (for-each (lambda (z)
  401. (let* ((setting (cdr z))
  402. (name (symbol->string (setting-name setting))))
  403. (display #\space o-port)
  404. (display name o-port)
  405. (display #\space o-port)
  406. (write-spaces (- size (string-length name)) o-port)
  407. (display #\( o-port)
  408. (cond
  409. ((not (setting-boolean? setting))
  410. (write (setting-value setting) o-port)
  411. (display ", " o-port)
  412. (display (setting-on-doc setting) o-port))
  413. ((setting-value setting)
  414. (display "on, " o-port)
  415. (display (setting-on-doc setting) o-port))
  416. (else
  417. (display "off, " o-port)
  418. (display (setting-off-doc setting) o-port)))
  419. (display #\) o-port)
  420. (newline o-port)))
  421. *settings-alist*)))
  422. ;----------------
  423. ; help
  424. (define (help . maybe-id)
  425. (if (null? maybe-id)
  426. (list-commands)
  427. (print-command-help (car maybe-id))))
  428. (define (print-command-help id)
  429. (let ((o-port (command-output)))
  430. (display #\space o-port)
  431. (cond ((assq id (user-command-help))
  432. => (lambda (data)
  433. (if (form-preferred?) (display command-prefix o-port))
  434. (display (cadr data) o-port)
  435. (display " " o-port)
  436. (display (caddr data) o-port)))
  437. (else
  438. (display #\" o-port)
  439. (display id o-port)
  440. (display #\" o-port)
  441. (display #\space o-port)
  442. (display "is not a command.")))
  443. (newline o-port)))
  444. (define (list-commands)
  445. (let ((o-port (command-output))
  446. (widest 28)
  447. (f? (form-preferred?)))
  448. (for-each (lambda (s)
  449. (write-line s o-port))
  450. '(
  451. "This is Scheme 48. You are interacting with the command processor."
  452. "A command is either a Scheme form to evaluate or one of the following:"
  453. ""))
  454. (list-command-help (user-command-help) f? o-port)
  455. (for-each (lambda (s)
  456. (write-line s o-port))
  457. '(
  458. ""
  459. "Square brackets [...] indicate optional arguments."
  460. ""
  461. "The following settings are set by the `set' and `unset' commands:"
  462. ""
  463. ))
  464. (list-settings)
  465. (for-each (lambda (s)
  466. (write-line s o-port))
  467. '(
  468. ""
  469. "The expression ## evaluates to the last value displayed by the command"
  470. "processor."
  471. ))))
  472. (define (list-command-help data prefix? o-port)
  473. (let* ((strings (map (if prefix?
  474. (lambda (d)
  475. (string-append (command-prefix-string
  476. command-prefix)
  477. (cadr d)))
  478. cadr)
  479. data))
  480. (count (length strings))
  481. (back-half (list-tail strings (quotient (+ 1 count) 2))))
  482. (let loop ((s1 strings) (s2 back-half))
  483. (cond ((not (eq? s1 back-half))
  484. (display #\space o-port)
  485. (display (car s1) o-port)
  486. (write-spaces (max 1 (- 32 (string-length (car s1)))) o-port)
  487. (if (not (null? s2))
  488. (display (car s2) o-port))
  489. (newline o-port)
  490. (loop (cdr s1) (if (null? s2) s2 (cdr s2))))))))
  491. (define (write-spaces count o-port)
  492. (do ((count count (- count 1)))
  493. ((<= count 0))
  494. (display #\space o-port)))
  495. (define (command-prefix-string prefix)
  496. (cond ((string? prefix) prefix)
  497. ((char? prefix) (string prefix))
  498. ((symbol? prefix) (symbol->string prefix))))
  499. (define (y-or-n? question eof-value)
  500. (let ((i-port (command-input))
  501. (o-port (command-output)))
  502. (let loop ((count *y-or-n-eof-count*))
  503. (display question o-port)
  504. (display " (y/n)? " o-port)
  505. (let ((line (read-line i-port)))
  506. (cond ((eof-object? line)
  507. (newline o-port)
  508. (if (= count 0)
  509. eof-value
  510. (begin (display "I'll only ask another " o-port)
  511. (write count o-port)
  512. (display " times." o-port)
  513. (newline o-port)
  514. (loop (- count 1)))))
  515. ((< (string-length line) 1) (loop count))
  516. ((char=? (string-ref line 0) #\y) #t)
  517. ((char=? (string-ref line 0) #\n) #f)
  518. (else (loop count)))))))
  519. (define *y-or-n-eof-count* 100)
  520. (define (read-line port)
  521. (let loop ((l '())
  522. (llen 0))
  523. (let ((c (read-char port)))
  524. (if (eof-object? c)
  525. c
  526. (if (char=? c #\newline)
  527. (reverse-list->string l llen)
  528. (loop (cons c l) (+ 1 llen)))))))
  529. (define (greet-user info)
  530. (let ((port (command-output)))
  531. (display "Welcome to Scheme 48 " port)
  532. (display version-info port)
  533. (if info
  534. (begin (write-char #\space port)
  535. (display info port)))
  536. (newline port)
  537. (write-line "Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees."
  538. port)
  539. (write-line "Please report bugs to scheme-48-bugs@s48.org."
  540. port)
  541. (write-line "Get more information at http://www.s48.org/."
  542. port)
  543. (if (not (batch-mode?))
  544. (write-line "Type ,? (comma question-mark) for help." port))))
  545. (define (command-continuation) ;utility for debugger
  546. (let ((obj (focus-object)))
  547. (cond ((debug-command-level)
  548. => (lambda (level)
  549. (if (command-level-paused-thread level)
  550. (thread-continuation (command-level-paused-thread level))
  551. (let ((threads (command-level-threads level)))
  552. (if (= 1 (length threads))
  553. (thread-continuation (car threads))
  554. #f)))))
  555. ((continuation? obj)
  556. obj)
  557. ((thread? obj)
  558. (thread-continuation obj))
  559. (else #f))))
  560. (define (command-threads) ;utility for debugger
  561. (let ((level (debug-command-level)))
  562. (if level
  563. (command-level-threads level)
  564. #f)))
  565. (define (debug-command-level)
  566. (let* ((obj (focus-object)))
  567. (if (command-level? obj)
  568. obj
  569. (let ((levels (command-levels)))
  570. (if (null? (cdr levels))
  571. #f
  572. (cadr levels))))))