checkpoint: fg

This commit is contained in:
Rutger van Beusekom 2016-10-26 00:45:12 +02:00
parent 628f111ced
commit 5c724a023a
2 changed files with 34 additions and 13 deletions

View File

@ -148,7 +148,9 @@ copyleft.
;;(format (current-error-port) "builtin: ~s\n" ast)
(match ast
(('append ('glob "cd") arg) `(apply chdir ,arg))
(('append ('glob "fg") ('glob arg)) `(fg ,(string->number arg)))
(('append ('glob "echo") args ...) `(apply stdout ,@args))
(('glob "fg") `(fg 1))
(('glob "jobs") `(jobs))
(('for-each rest ...) ast)
(('if rest ...) ast)

View File

@ -8,7 +8,7 @@
:use-module (srfi srfi-9)
:use-module (srfi srfi-26)
:export (pipeline job-control-init jobs))
:export (pipeline job-control-init jobs fg))
(define (stdout . o)
(map display o)
@ -34,6 +34,12 @@
(define job-table '()) ;; list of <job>
(define (job-index index)
(let ((index (- (length job-table) index)))
(if (<= 0 index)
(list-ref job-table index)
#f)))
(define (status->state status)
(cond ((status:exit-val status) 'Completed)
((status:term-sig status) 'Terminated)
@ -143,8 +149,9 @@
;; remove reported terminated or completed jobs
(define (pipeline . commands)
(let ((interactive? (isatty? (current-error-port)))
(job (make-job (length job-table) #f '())))
(let* ((interactive? (isatty? (current-error-port)))
(index (+ 1 (length job-table)))
(job (make-job index #f '())))
(set! job-table (cons job job-table))
(if (> (length commands) 1)
(let loop ((src (spawn-source job interactive? (car commands)))
@ -154,16 +161,7 @@
(loop (spawn-filter job interactive? src (car commands))
(cdr commands))))
(spawn-sink job interactive? #f (car commands)))
(let loop ()
(let* ((pid-status (waitpid WAIT_ANY WUNTRACED))
(pid (car pid-status))
(status (cdr pid-status)))
(job-update job pid status)
(if (job-running? job) (loop))))
(tcsetpgrp (current-error-port) (getpid))
;;(pretty-print job-table)
(job-status job)
(reap-jobs)))
(wait job)))
(define (disjoin . predicates)
(lambda (. arguments)
@ -172,5 +170,26 @@
(define (reap-jobs)
(set! job-table (filter (disjoin job-running? job-stopped?) job-table)))
(define (wait job)
(let ((index (job-id job)))
(let loop ()
(let* ((pid-status (waitpid WAIT_ANY WUNTRACED))
(pid (car pid-status))
(status (cdr pid-status)))
(job-update job pid status)
(if (job-running? job) (loop))))
(tcsetpgrp (current-error-port) (getpid))
(stdout "\n[" index "] " (status->state (job-status job)) "\t\t" (string-join (append-map process-command (job-processes job))))
(reap-jobs)))
(define (fg index)
(let ((job (job-index index)))
(cond (job
(tcsetpgrp (current-error-port) (job-pgid job))
(kill (- (job-pgid job)) SIGCONT)
(wait job))
(#t
(stderr "fg: no such job " index)))))
;;(pipeline (list "ls" "/")
;;(pipeline (list "ls" "/") (list "grep" "o") (list "tr" "o" "e"))