+(define (serve-info-records string)
+ (for-each
+ (lambda (line)
+ (print* "i")
+ (for-each (lambda (char)
+ (print* (if (eq? char #\tab)
+ " "
+ char)))
+ (string->list line))
+ (print "\tfake\tfake\t1\r"))
+ (string-split string "\n" #t)))
+
+(define (serve-record record path config)
+ (match record
+ (('shell command) (serve-shell-command command))
+ (('eval expression) (serve-expression expression))
+ (('url display-string url)
+ (print #\h display-string "\tURL:" url "\tfake\t80\r"))
+ ((type display-string selector host port)
+ (print type display-string "\t" selector "\t" host "\t" port "\r"))
+ ((type display-string selector host)
+ (serve-record (list type display-string selector host 70)
+ path config))
+ ((type display-string selector)
+ (serve-record (list type display-string selector
+ (config-host config) (config-port config))
+ path config))
+ ((display-string selector)
+ (serve-record (list (infer-selector-type selector) display-string selector)
+ path config))
+ ((selector)
+ (serve-record (list (infer-selecto-type selector) selector)
+ path config))
+ (else (error "Unknown record type."))))
+
+(define (serve-shell-command command)
+ (let-values (((in-port out-port id) (process command)))
+ (serve-info-records (string-chomp (read-string #f in-port) "\n"))))
+
+(define (serve-expression expression)
+ (serve-info-records (conc (eval expression))))
+
+
+;;; Utility methods
+
+(define (with-current-working-directory directory thunk)
+ (let ((old-wd (current-directory)))
+ (change-directory directory)
+ (thunk)
+ (change-directory old-wd)))
+
+(define (with-selector-dir selector thunk)
+ (with-current-working-directory (pathname-directory selector) thunk))