2005-06-03 07:44:53 -04:00
|
|
|
(define-record-type job :job
|
2005-06-07 14:24:05 -04:00
|
|
|
(really-make-job name console
|
|
|
|
proc status
|
|
|
|
start-time end-time run-status)
|
2005-06-03 07:44:53 -04:00
|
|
|
job?
|
|
|
|
(name job-name)
|
2005-06-07 14:24:05 -04:00
|
|
|
(console job-console)
|
2005-06-03 07:44:53 -04:00
|
|
|
(proc job-proc)
|
2005-06-14 07:20:30 -04:00
|
|
|
(status really-job-status set-job-status!)
|
2005-06-04 07:22:44 -04:00
|
|
|
(start-time job-start-time)
|
|
|
|
(end-time job-end-time set-job-end-time!)
|
|
|
|
(run-status job-run-status set-job-run-status!))
|
2005-06-03 07:44:53 -04:00
|
|
|
|
2005-06-04 05:42:45 -04:00
|
|
|
(define-record-discloser :job
|
|
|
|
(lambda (r)
|
2005-06-04 07:22:44 -04:00
|
|
|
`(job ,(job-name r) ,(job-run-status r))))
|
2005-06-04 05:42:45 -04:00
|
|
|
|
2005-06-14 07:20:30 -04:00
|
|
|
(define (make-job-with-console name proc pty-in pty-out terminal-buffer)
|
2005-06-04 07:22:44 -04:00
|
|
|
(let ((job (really-make-job
|
2005-06-07 14:24:05 -04:00
|
|
|
name
|
|
|
|
(make-console pty-in pty-out
|
|
|
|
(app-window-curses-win (result-window))
|
|
|
|
terminal-buffer)
|
|
|
|
proc (make-placeholder)
|
2005-06-04 07:22:44 -04:00
|
|
|
(date) #f 'running)))
|
2005-06-03 07:44:53 -04:00
|
|
|
(spawn-job-status-surveillant job)
|
|
|
|
(add-job! job)
|
|
|
|
job))
|
|
|
|
|
2005-08-12 09:27:27 -04:00
|
|
|
(define (make-job-sans-console name proc)
|
|
|
|
(let ((job (really-make-job
|
|
|
|
name #f proc (make-placeholder)
|
2005-06-14 07:20:30 -04:00
|
|
|
(date) #f 'running)))
|
2005-08-12 09:27:27 -04:00
|
|
|
(spawn-job-status-surveillant job)
|
|
|
|
(add-job! job)
|
|
|
|
job))
|
2005-06-14 07:20:30 -04:00
|
|
|
|
|
|
|
(define (job-with-console? v)
|
|
|
|
(and (job? v) (job-console v)))
|
|
|
|
|
|
|
|
(define (job-sans-console? v)
|
|
|
|
(not (job-with-console? v)))
|
|
|
|
|
2005-06-03 07:44:53 -04:00
|
|
|
(define (job-status-rv job)
|
|
|
|
(placeholder-value-rv (really-job-status job)))
|
|
|
|
|
|
|
|
(define (job-status job)
|
|
|
|
(sync (job-status-rv job)))
|
|
|
|
|
2005-08-12 09:27:27 -04:00
|
|
|
(define (spawn-job-status-surveillant job)
|
2005-06-14 09:44:09 -04:00
|
|
|
(spawn
|
|
|
|
(lambda ()
|
2005-08-12 09:27:27 -04:00
|
|
|
(let ((status (wait (job-proc job) wait/stopped-children)))
|
2005-06-14 09:44:09 -04:00
|
|
|
(cond
|
|
|
|
((status:exit-val status)
|
|
|
|
=> (lambda (i)
|
2005-08-11 11:03:15 -04:00
|
|
|
(debug-message "spawn-job-status-surveillant exit-val " i)
|
2005-06-14 09:44:09 -04:00
|
|
|
(set-job-run-status! job 'ready)
|
|
|
|
(set-job-end-time! job (date))))
|
|
|
|
((status:stop-sig status)
|
|
|
|
=> (lambda (signal)
|
2005-08-11 11:03:15 -04:00
|
|
|
(debug-message "spawn-job-status-surveillant stop-sig " signal)
|
2005-06-14 09:44:09 -04:00
|
|
|
(cond
|
|
|
|
((= signal signal/ttin)
|
|
|
|
(set-job-run-status! job 'waiting-for-input))
|
|
|
|
((= signal signal/ttou)
|
|
|
|
(set-job-run-status! job 'new-output))
|
|
|
|
(else
|
|
|
|
(set-job-run-status! job 'stopped)))))
|
|
|
|
((status:term-sig status)
|
|
|
|
=> (lambda (i)
|
2005-08-11 11:03:15 -04:00
|
|
|
(debug-message "spawn-job-status-surveillant term-sig " i)
|
2005-06-14 09:44:09 -04:00
|
|
|
(set-job-run-status! job 'stopped))))
|
|
|
|
(placeholder-set!
|
|
|
|
(really-job-status job) status)))))
|
2005-06-04 07:22:44 -04:00
|
|
|
|
|
|
|
(define (job-running? job)
|
|
|
|
(eq? (job-run-status job) 'running))
|
|
|
|
|
|
|
|
(define (job-ready? job)
|
|
|
|
(eq? (job-run-status job) 'ready))
|
|
|
|
|
|
|
|
(define (job-waiting-for-input? job)
|
|
|
|
(eq? (job-run-status job) 'waiting-for-input))
|
|
|
|
|
|
|
|
(define (job-has-new-output? job)
|
|
|
|
(eq? (job-run-status job) 'new-output))
|
2005-06-03 07:44:53 -04:00
|
|
|
|
2005-06-14 07:20:30 -04:00
|
|
|
(define (job-stopped? job)
|
|
|
|
(eq? (job-run-status job) 'stopped))
|
|
|
|
|
2005-06-03 07:44:53 -04:00
|
|
|
(define (signal-job signal job)
|
2005-09-13 09:20:30 -04:00
|
|
|
(signal-process-group (job-proc job) signal))
|
2005-06-03 07:44:53 -04:00
|
|
|
|
|
|
|
(define (stop-job job)
|
2005-09-13 09:20:30 -04:00
|
|
|
(signal-job signal/stop job))
|
2005-06-03 07:44:53 -04:00
|
|
|
|
|
|
|
(define (continue-job job)
|
2005-06-14 07:20:30 -04:00
|
|
|
(set-job-status! job (make-placeholder))
|
|
|
|
(set-job-run-status! job 'running)
|
|
|
|
(signal-process-group
|
|
|
|
(proc:pid (job-proc job)) signal/cont)
|
2005-06-14 09:44:09 -04:00
|
|
|
(spawn-job-status-surveillant job)
|
2005-09-13 09:20:30 -04:00
|
|
|
(send notify-continue-channel job))
|
2005-06-03 07:44:53 -04:00
|
|
|
|
2005-06-07 14:24:05 -04:00
|
|
|
(define (pause-job-output job)
|
|
|
|
(pause-console-output (job-console job)))
|
|
|
|
|
|
|
|
(define (resume-job-output job)
|
|
|
|
(resume-console-output (job-console job)))
|
|
|
|
|
2005-06-14 07:20:30 -04:00
|
|
|
(define (continue-job-in-foreground job)
|
|
|
|
(if (job-sans-console? job)
|
|
|
|
(begin
|
|
|
|
(drain-tty (current-output-port))
|
|
|
|
(def-prog-mode)
|
|
|
|
(endwin)
|
|
|
|
(newline)
|
|
|
|
(drain-tty (current-output-port))
|
|
|
|
(obtain-lock paint-lock)
|
|
|
|
(set-tty-process-group
|
|
|
|
(current-output-port) (proc:pid (job-proc job)))
|
|
|
|
(continue-job job)
|
|
|
|
(job-status job)
|
|
|
|
(set-tty-process-group (current-output-port) (pid))
|
|
|
|
(display "Press any key to return to Commander S...")
|
|
|
|
(wait-for-key)
|
|
|
|
(release-lock paint-lock))))
|
|
|
|
|
2005-09-13 09:20:30 -04:00
|
|
|
(define (continue-job-in-background job)
|
|
|
|
(continue-job job))
|
|
|
|
|
2005-06-03 07:44:53 -04:00
|
|
|
;; channels for communicating with the joblist surveillant
|
|
|
|
|
|
|
|
(define add-job-channel
|
|
|
|
(make-channel))
|
|
|
|
|
|
|
|
(define get-job-list-channel
|
|
|
|
(make-channel))
|
|
|
|
|
2005-06-07 14:24:05 -04:00
|
|
|
(define clear-ready-jobs-channel
|
|
|
|
(make-channel))
|
|
|
|
|
2005-09-13 09:20:30 -04:00
|
|
|
(define notify-continue-channel
|
2005-06-14 09:44:09 -04:00
|
|
|
(make-channel))
|
|
|
|
|
2005-06-03 07:44:53 -04:00
|
|
|
(define (add-job! job)
|
|
|
|
(send add-job-channel job))
|
|
|
|
|
|
|
|
(define (running-jobs)
|
|
|
|
(let ((answer-channel (make-channel)))
|
|
|
|
(send get-job-list-channel (cons 'running answer-channel))
|
|
|
|
(receive answer-channel)))
|
|
|
|
|
|
|
|
(define (ready-jobs)
|
|
|
|
(let ((answer-channel (make-channel)))
|
|
|
|
(send get-job-list-channel (cons 'ready answer-channel))
|
|
|
|
(receive answer-channel)))
|
|
|
|
|
2005-07-06 04:57:44 -04:00
|
|
|
(define (stopped-jobs)
|
|
|
|
(let ((answer-channel (make-channel)))
|
|
|
|
(send get-job-list-channel (cons 'stopped answer-channel))
|
|
|
|
(receive answer-channel)))
|
|
|
|
|
2005-06-07 14:24:05 -04:00
|
|
|
(define (clear-ready-jobs!)
|
|
|
|
(send clear-ready-jobs-channel 'ignored))
|
|
|
|
|
2005-06-03 07:44:53 -04:00
|
|
|
(define (jobs-with-new-output)
|
|
|
|
(let ((answer-channel (make-channel)))
|
|
|
|
(send get-job-list-channel (cons 'new-output answer-channel))
|
|
|
|
(receive answer-channel)))
|
|
|
|
|
|
|
|
(define (jobs-waiting-for-input)
|
|
|
|
(let ((answer-channel (make-channel)))
|
|
|
|
(send get-job-list-channel (cons 'waiting-for-input answer-channel))
|
|
|
|
(receive answer-channel)))
|
|
|
|
|
|
|
|
(define (spawn-joblist-surveillant)
|
|
|
|
(let ((statistics-channel (make-channel)))
|
|
|
|
(spawn
|
|
|
|
(lambda ()
|
|
|
|
(let lp ((running '())
|
|
|
|
(ready '())
|
2005-06-14 07:20:30 -04:00
|
|
|
(stopped '())
|
2005-06-03 07:44:53 -04:00
|
|
|
(new-output '())
|
|
|
|
(waiting-for-input '())
|
|
|
|
(notify? #f))
|
2005-06-14 07:20:30 -04:00
|
|
|
(debug-message "spawn-joblist-surveillant "
|
|
|
|
running " " ready " " stopped " "
|
|
|
|
new-output " " waiting-for-input " " notify?)
|
2005-06-03 07:44:53 -04:00
|
|
|
(cond
|
|
|
|
(notify?
|
|
|
|
(send statistics-channel
|
|
|
|
(list (cons 'running (length running))
|
|
|
|
(cons 'ready (length ready))
|
2005-06-14 07:20:30 -04:00
|
|
|
(cons 'stopped (length stopped))
|
2005-06-03 07:44:53 -04:00
|
|
|
(cons 'new-output (length new-output))
|
|
|
|
(cons 'waiting-for-input (length waiting-for-input))))
|
2005-06-14 07:20:30 -04:00
|
|
|
(lp running ready stopped new-output waiting-for-input #f))
|
2005-06-03 07:44:53 -04:00
|
|
|
(else
|
|
|
|
(apply
|
|
|
|
select
|
|
|
|
(append
|
|
|
|
(list
|
|
|
|
(wrap (receive-rv add-job-channel)
|
|
|
|
(lambda (new-job)
|
2005-06-14 07:20:30 -04:00
|
|
|
(lp (cons new-job running)
|
|
|
|
ready stopped new-output waiting-for-input #t)))
|
2005-06-07 14:24:05 -04:00
|
|
|
|
2005-09-13 09:20:30 -04:00
|
|
|
(wrap (receive-rv notify-continue-channel)
|
2005-06-14 09:44:09 -04:00
|
|
|
(lambda (job)
|
|
|
|
(lp (cons job running)
|
|
|
|
ready
|
|
|
|
(delete job stopped)
|
|
|
|
(delete job new-output)
|
|
|
|
(delete job waiting-for-input) #t)))
|
|
|
|
|
2005-06-07 14:24:05 -04:00
|
|
|
(wrap (receive-rv clear-ready-jobs-channel)
|
|
|
|
(lambda (ignore)
|
2005-06-14 07:20:30 -04:00
|
|
|
(lp running '() stopped new-output waiting-for-input #t)))
|
2005-06-03 07:44:53 -04:00
|
|
|
|
|
|
|
(wrap (receive-rv get-job-list-channel)
|
|
|
|
(lambda (state.channel)
|
|
|
|
(send (cdr state.channel)
|
|
|
|
(case (car state.channel)
|
|
|
|
((running) running)
|
|
|
|
((ready) ready)
|
2005-06-14 07:20:30 -04:00
|
|
|
((stopped) stopped)
|
2005-06-03 07:44:53 -04:00
|
|
|
((new-output) new-output)
|
|
|
|
((waiting-for-input) waiting-for-input)
|
|
|
|
(else
|
|
|
|
(error "joblist-surveillant" state.channel))))
|
2005-06-14 07:20:30 -04:00
|
|
|
(lp running ready stopped new-output waiting-for-input #f))))
|
2005-06-03 07:44:53 -04:00
|
|
|
|
|
|
|
(map (lambda (job)
|
|
|
|
(wrap (job-status-rv job)
|
2005-06-14 07:20:30 -04:00
|
|
|
(lambda (status)
|
|
|
|
(cond
|
|
|
|
((status:exit-val status)
|
|
|
|
=> (lambda (ignore)
|
|
|
|
(lp (delete job running)
|
|
|
|
(cons job ready) stopped
|
|
|
|
new-output waiting-for-input #t)))
|
|
|
|
((status:stop-sig status)
|
|
|
|
=> (lambda (signal)
|
|
|
|
(cond
|
|
|
|
((= signal signal/ttin)
|
|
|
|
(lp (delete job running)
|
|
|
|
ready stopped new-output
|
|
|
|
(cons job waiting-for-input) #t))
|
|
|
|
((= signal signal/ttou)
|
|
|
|
(lp (delete job running)
|
|
|
|
ready stopped
|
|
|
|
(cons job new-output)
|
|
|
|
waiting-for-input #t))
|
2005-09-13 09:20:30 -04:00
|
|
|
((or (= signal signal/tstp)
|
|
|
|
(= signal signal/stop))
|
|
|
|
;; TODO catch any other signal here
|
2005-06-15 07:42:29 -04:00
|
|
|
(stop-job job)
|
|
|
|
(lp (delete job running)
|
|
|
|
ready (cons job stopped)
|
|
|
|
new-output waiting-for-input #t))
|
2005-06-14 07:20:30 -04:00
|
|
|
(else
|
|
|
|
(error "Unhandled signal" signal)))))
|
|
|
|
((status:term-sig status)
|
|
|
|
=> (lambda (signal)
|
|
|
|
(lp (delete job running)
|
|
|
|
ready (cons job stopped)
|
|
|
|
new-output waiting-for-input #t)))))))
|
2005-06-03 07:44:53 -04:00
|
|
|
running))))))))
|
|
|
|
statistics-channel))
|
|
|
|
|
|
|
|
(define (initial-job-statistics)
|
|
|
|
(list (cons 'running 0)
|
|
|
|
(cons 'ready 0)
|
|
|
|
(cons 'new-output 0)
|
|
|
|
(cons 'waiting-for-input 0)))
|
|
|
|
|
|
|
|
;; #### unfinished
|
|
|
|
(define (install-terminal/stop-handler)
|
|
|
|
(set-interrupt-handler
|
|
|
|
interrupt/tstp
|
|
|
|
(lambda args
|
|
|
|
(display args))))
|
|
|
|
|
2005-06-14 07:20:30 -04:00
|
|
|
(define (save-tty-excursion port thunk)
|
|
|
|
(let ((settings (tty-info port)))
|
2005-08-11 05:36:10 -04:00
|
|
|
(let ((val (thunk)))
|
|
|
|
(set-tty-info/now port settings)
|
|
|
|
val)))
|
2005-06-14 07:20:30 -04:00
|
|
|
|
2005-08-10 14:03:59 -04:00
|
|
|
;; run a job by running the program form
|
|
|
|
|
|
|
|
|
|
|
|
;; for use in command mode (used by command-line-compiler)
|
|
|
|
(define (run/console* s-expr)
|
|
|
|
(call-with-values
|
|
|
|
(lambda ()
|
|
|
|
(fork-pty-session
|
|
|
|
(lambda ()
|
2005-09-13 09:20:30 -04:00
|
|
|
(handle-signal-default signal/ttou)
|
2005-08-10 14:03:59 -04:00
|
|
|
(eval-s-expr s-expr))))
|
|
|
|
(lambda (proc pty-in pty-out tty-name)
|
|
|
|
(make-job-with-console
|
|
|
|
s-expr proc pty-in pty-out
|
|
|
|
(make-terminal-buffer
|
2005-09-27 04:59:55 -04:00
|
|
|
(result-buffer-num-cols (result-buffer))
|
|
|
|
(result-buffer-num-lines (result-buffer)))))))
|
2005-08-10 14:03:59 -04:00
|
|
|
|
|
|
|
;; for use in Scheme mode
|
2005-07-06 04:57:44 -04:00
|
|
|
(define-syntax run/console
|
2005-06-03 07:44:53 -04:00
|
|
|
(syntax-rules ()
|
2005-08-12 09:27:27 -04:00
|
|
|
((_ epf)
|
|
|
|
(run/console* '(exec-epf epf)))))
|
2005-08-10 14:03:59 -04:00
|
|
|
|
|
|
|
;; for use in command mode (used by command-line-compiler)
|
|
|
|
(define (run/fg* s-expr)
|
|
|
|
(debug-message "run/fg* " s-expr)
|
|
|
|
(save-tty-excursion
|
|
|
|
(current-input-port)
|
|
|
|
(lambda ()
|
2005-08-12 09:27:27 -04:00
|
|
|
(def-prog-mode)
|
|
|
|
(clear)
|
|
|
|
(endwin)
|
|
|
|
(restore-initial-tty-info! (current-input-port))
|
|
|
|
(drain-tty (current-output-port))
|
|
|
|
(obtain-lock paint-lock)
|
|
|
|
(let ((foreground-pgrp (tty-process-group (current-output-port)))
|
|
|
|
(proc
|
|
|
|
(fork
|
|
|
|
(lambda ()
|
2005-09-13 09:20:30 -04:00
|
|
|
(let ((child-pid (pid)))
|
|
|
|
(set-process-group child-pid)
|
|
|
|
(set-tty-process-group (current-output-port) child-pid)
|
|
|
|
(handle-signal-default signal/ttou)
|
|
|
|
(eval-s-expr s-expr))))))
|
2005-08-12 09:27:27 -04:00
|
|
|
(let* ((job (make-job-sans-console s-expr proc))
|
|
|
|
(status (job-status job)))
|
|
|
|
(set-tty-process-group (current-output-port) foreground-pgrp)
|
|
|
|
(newline)
|
|
|
|
(display "Press any key to return to Commander S...")
|
|
|
|
(wait-for-key)
|
|
|
|
(release-lock paint-lock)
|
|
|
|
job)))))
|
2005-06-14 07:20:30 -04:00
|
|
|
|
2005-07-06 04:57:44 -04:00
|
|
|
(define-syntax run/fg
|
2005-06-14 07:20:30 -04:00
|
|
|
(syntax-rules ()
|
|
|
|
((_ epf)
|
2005-08-12 09:27:27 -04:00
|
|
|
(run/fg* '(exec-epf epf)))))
|
2005-08-10 14:03:59 -04:00
|
|
|
|
|
|
|
;; for use in command mode (used by command-line-compiler)
|
|
|
|
(define (run/bg* s-expr)
|
|
|
|
(obtain-lock paint-lock)
|
|
|
|
(drain-tty (current-output-port))
|
|
|
|
(set-tty-process-group (current-output-port) (pid))
|
|
|
|
(let ((proc
|
|
|
|
(fork
|
|
|
|
(lambda ()
|
|
|
|
(set-process-group (pid) (pid))
|
2005-09-13 09:20:30 -04:00
|
|
|
(handle-signal-default signal/ttou)
|
2005-08-10 14:03:59 -04:00
|
|
|
(eval-s-expr s-expr)))))
|
2005-08-12 09:27:27 -04:00
|
|
|
(let ((job (make-job-sans-console s-expr proc)))
|
2005-08-10 14:03:59 -04:00
|
|
|
(release-lock paint-lock)
|
|
|
|
job)))
|
|
|
|
|
|
|
|
;; for use in Scheme mode
|
2005-07-06 04:57:44 -04:00
|
|
|
(define-syntax run/bg
|
2005-06-14 07:20:30 -04:00
|
|
|
(syntax-rules ()
|
2005-08-12 09:27:27 -04:00
|
|
|
((_ epf)
|
|
|
|
(run/bg* '(exec-epf epf)))))
|
2005-08-10 14:03:59 -04:00
|
|
|
|
2005-07-06 04:57:44 -04:00
|
|
|
;;; EOF
|