command.scm 20 KB

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