From: Tim Vaughan Date: Thu, 30 May 2019 08:57:37 +0000 (+0200) Subject: Settled on way to serve script results. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=307887439e723047cab1bc5a2572cb89e2f06630;p=scratchy.git Settled on way to serve script results. --- diff --git a/burrower.scm b/burrower.scm index b7b2754..f9604a7 100644 --- a/burrower.scm +++ b/burrower.scm @@ -105,17 +105,16 @@ (let* ((selector-list (string-split raw-selector "\t")) (selector (car selector-list)) (arguments (cdr selector-list))) - (case (infer-selector-type selector) - ((1) (if (string-contains selector ":") - (let ((l (string-split selector ":"))) - (serve-directory-script (car l) (cdr l) - config)) - (serve-directory-file selector config))) - ((7) (let ((l (string-split selector "?"))) - (serve-directory-script (car l) arguments config))) - ((0) (serve-text-file selector config)) - ((h) (serve-url selector config)) - (else (serve-binary-file selector config))))) + (if (string-contains selector ":") + (let ((l (string-split selector ":"))) + (serve-script (car l) (cdr l) config)) + (case (infer-selector-type selector) + ((1) (serve-directory-file selector config)) + ((7) (let ((l (string-split selector "?"))) + (serve-script (car l) arguments config))) + ((0) (serve-text-file selector config)) + ((h) (serve-url selector config)) + (else (serve-binary-file selector config)))))) (define (legal-filename? filename config) (and (string-prefix? (config-root-dir config) @@ -129,19 +128,6 @@ (string-suffix? ".scm" filename) (file-executable? filename))) -(define (serve-directory-script selector arguments config) - (let ((filename (make-pathname (config-root-dir config) selector))) - (if (legal-script-filename? filename config) - (let* ((sexp (with-input-from-file filename read)) - (selector-dir (pathname-directory selector))) - (serve-records (with-selector-dir - selector config - (lambda () - (apply (eval sexp) arguments))) - selector-dir config) - (print ".\r")) - (error "No legal index script not found." filename)))) - (define (serve-directory-file selector config) (let ((filename (make-pathname (list (config-root-dir config) selector) gopher-index-filename))) @@ -207,6 +193,20 @@ "" url "\n" ""))) +(define (serve-script selector arguments config) + (let ((filename (make-pathname (config-root-dir config) selector))) + (if (legal-script-filename? filename config) + (let* ((sexp (with-input-from-file filename read)) + (script-result (with-selector-dir + selector config + (lambda () + (apply (eval sexp) arguments))))) + (when (pair? script-result) + (serve-records script-result + (pathname-directory selector) config) + (print ".\r"))) + (error "No legal index script not found." filename)))) + ;;; Index rendering @@ -263,7 +263,9 @@ (let ((string (read-string #f in-port))) (if (and (not (eof-object? string)) (> (string-length string) 0)) - (serve-info-records (string-chomp string "\n")))))))) + (serve-info-records (string-chomp string "\n"))) + (close-input-port in-port) + (close-output-port out-port)))))) (define (serve-expression expression dir-selector config) (with-selector-dir