Compare commits

...

3 Commits

View File

@@ -9,6 +9,26 @@
;; directory of the current user of the program
(define call-dir (make-parameter (string->path "/nobody")))
;; raised by assert-confined-to-call-dir when attemping to traverse
;; outside of (call-dir)
(struct exn:fail:contract:illegal-path exn:fail:contract ()
#:extra-constructor-name make-exn:fail:contract:illegal-path
#:transparent)
;; check if path, once resolved, is a child of base
(define (confined-path? base path)
(string-prefix?
(path->string (simplify-path (build-path base path)))
(path->string (simplify-path base))))
;; asserts that path is within (call-dir), and raises
;; exn:fail:contract:illegal-path otherwise.
(define (assert-confined-to-call-dir path)
(when (not (confined-path? (call-dir) path))
(raise (make-exn:fail:contract:illegal-path
"assert-confined-to-call-dir: traversed outside of root"
(current-continuation-marks)))))
(define (call-valid? call)
(and
(<= (string-length call) 9)
@@ -101,45 +121,50 @@
(session))))
(define (session)
(display "palps > ")
(flush-output)
(with-handlers
([exn:fail:contract:illegal-path?
(λ (exn)
(displayln "cannot traverse higher than root"))])
(match (regexp-match #px"(\\w+)\\s*(.*)"
(read-line (current-input-port) 'any))
[(list _ "ls" _)
(for-each (λ (p) (displayln (path->string p)))
(directory-list))
(flush-output)]
(display "palps > ")
(flush-output)
[(list _ "ed" name)
(let ([path (build-path (current-directory)
(string->path-element name))])
(let ([path (if (file-exists? path)
(ed (our-make-temporary-file path))
(match (regexp-match #px"(\\w+)\\s*(.*)"
(read-line (current-input-port) 'any))
[(list _ "ls" _)
(for-each (λ (p) (displayln (path->string p)))
(directory-list))
(flush-output)]
[(list _ "ed" name)
(assert-confined-to-call-dir name)
(let ([path (if (file-exists? name)
(ed (our-make-temporary-file name))
(ed))])
(copy-file path name #:exists-ok? #t)))]
(copy-file path name #:exists-ok? #t))]
[(list _ "rm" name)
(let ([path (build-path (current-directory)
(string->path-element name))])
(if (file-exists? path)
(delete-file path)
[(list _ "rm" name)
(assert-confined-to-call-dir name)
(if (file-exists? name)
(delete-file name)
(displayln "file does not exist"))
(flush-output))]
(flush-output)]
[(or (list _ "bye" _)
(list _ "b" _)
(list _ "quit" _)
(list _ "q" _))
(displayln "goodbye")
(flush-output)
(exit)]
[(or (list _ "bye" _)
(list _ "b" _)
(list _ "quit" _)
(list _ "q" _))
(displayln "goodbye")
(flush-output)
(exit)]
[else
(displayln "no such command")
(flush-output)])
[else
(displayln "no such command")
(flush-output)]))
(session))
(session-startup)
;(session-startup)