-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjcommand.scm
49 lines (42 loc) · 1.27 KB
/
jcommand.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(require 'xmlp)
(define newline-string (string #\newline))
(define (jc:do-help)
(format "help: print this help~neval: eval a sexp"))
;; escape < > to entity
(define (escape-special str)
(define specials '((#\< "<")
(#\> ">")))
(let loop ((strings (map (lambda (c)
(let ((a (assoc c specials)))
(if a (cadr a) (string c))))
(string->list str)))
(result ""))
(if (null? strings) result (loop (cdr strings) (string-append result (car strings))))))
(define (jc:do-eval sexp)
(lambda ()
(let* ((cmdline (format "csi -p '~a' 2>&1" sexp))
(port (open-input-pipe cmdline)))
(printf "CMDLINE: ~a~n" cmdline)
(let loop ((l (read-line port))
(a ""))
(if (eof-object? l)
(escape-special a)
(loop (read-line port) (string-append a l newline-string)))))))
(define (jc:help ctx)
(>>=
(<- comm xp:qname)
(if (string=? comm "help")
(m:return jc:do-help)
(m:fail (format "help expected. got ~s" comm)))))
(define (jc:eval ctx)
(>>=
(<- comm xp:qname)
(if (string=? comm "eval")
(>>=
(<- sexp (mp:+/s (mp:char-parser char? "")))
(m:return (jc:do-eval sexp)))
(m:fail (format "eval expected. got ~s" comm)))))
(define (jc:command ctx)
(mp:any
(jc:help ctx)
(jc:eval ctx)))