2005-08-15 08:41:24 -04:00
|
|
|
;; ,open conditions extended-ports define-record-types srfi-1 srfi-6 srfi-8 srfi-13 srfi-14 silly signals handle
|
2005-08-07 10:46:57 -04:00
|
|
|
|
|
|
|
;; command line language:
|
|
|
|
;;
|
|
|
|
;; <cmdline> ::= (<cmd> (<sep> <cmd>)* <jobctrl>?
|
|
|
|
;; <cmd> ::= <prog> <arg>* <redir>*
|
|
|
|
;; <redir> ::= <redirop> <fname>
|
|
|
|
;; <redirop> ::= ">" | ">>" | "<<" | "<"
|
|
|
|
;; <fname> ::= <string> | <unquote>
|
|
|
|
;; <prog> ::= <string> | <unquote>
|
|
|
|
;; <string> ::= "..." | char+ with char = ASCII\{"&","|","<",">",","}
|
|
|
|
;; <jobctrl> ::= "&" | "&*"
|
|
|
|
;; <sep> ::= "|" | "&&" | "||" | ";"
|
|
|
|
;; <unquote> ::= "," <s-expr> | ",@" <s-expr>
|
|
|
|
|
|
|
|
;; lexer stuff
|
|
|
|
|
|
|
|
(define-record-type token :token
|
2005-08-15 08:41:24 -04:00
|
|
|
(really-make-token token type start-pos cursor-pos)
|
2005-08-07 10:46:57 -04:00
|
|
|
token?
|
|
|
|
(token token-token)
|
|
|
|
(type token-type)
|
2005-08-15 08:41:24 -04:00
|
|
|
(start-pos token-start-pos)
|
|
|
|
(cursor-pos token-cursor-pos))
|
|
|
|
|
|
|
|
;; all indices and positions are zero-based
|
2005-08-07 10:46:57 -04:00
|
|
|
|
|
|
|
(define-record-discloser :token
|
|
|
|
(lambda (r)
|
2005-08-15 08:41:24 -04:00
|
|
|
`(token ,(token-type r) ,(token-token r) ,(token-cursor-pos r))))
|
|
|
|
|
|
|
|
(define (make-token token type start-pos cursor-index port)
|
|
|
|
(really-make-token
|
|
|
|
token type start-pos
|
|
|
|
(and cursor-index
|
|
|
|
(<= start-pos cursor-index (current-column port))
|
|
|
|
(- cursor-index start-pos))))
|
2005-08-07 10:46:57 -04:00
|
|
|
|
2005-08-15 08:41:24 -04:00
|
|
|
(define (make-string-token data start-pos cursor-index port)
|
|
|
|
(make-token data 'string start-pos cursor-index port))
|
2005-08-07 10:46:57 -04:00
|
|
|
|
|
|
|
(define (string-token? thing)
|
|
|
|
(and (token? thing) (eq? (token-type thing) 'string)))
|
|
|
|
|
2005-08-15 08:41:24 -04:00
|
|
|
(define (make-s-expr-token data start-pos cursor-index port)
|
|
|
|
(make-token data 's-expr start-pos cursor-index port))
|
2005-08-07 10:46:57 -04:00
|
|
|
|
|
|
|
(define (s-expr-token? thing)
|
|
|
|
(and (token? thing) (eq? (token-type thing) 's-expr)))
|
|
|
|
|
2005-08-15 08:41:24 -04:00
|
|
|
(define (make-operator-token data start-pos cursor-index port)
|
|
|
|
(make-token data 'operator start-pos cursor-index port))
|
2005-08-07 10:46:57 -04:00
|
|
|
|
|
|
|
(define (operator-token? thing)
|
|
|
|
(and (token? thing) (eq? (token-type thing) 'operator)))
|
|
|
|
|
2005-08-15 08:41:24 -04:00
|
|
|
(define (cursor-on-token? token)
|
|
|
|
(or (to-complete? token)
|
|
|
|
(integer? (token-cursor-pos token))))
|
|
|
|
|
|
|
|
(define-record-type to-complete :to-complete
|
2005-08-17 09:29:55 -04:00
|
|
|
(make-to-complete prefix pos)
|
2005-08-15 08:41:24 -04:00
|
|
|
to-complete?
|
2005-08-17 09:29:55 -04:00
|
|
|
(prefix to-complete-prefix)
|
|
|
|
(pos to-complete-pos))
|
2005-08-15 08:41:24 -04:00
|
|
|
|
|
|
|
(define-record-discloser :to-complete
|
|
|
|
(lambda (r)
|
2005-08-17 09:29:55 -04:00
|
|
|
`(to-complete ,(to-complete-prefix r) ,(to-complete-pos r))))
|
2005-08-15 08:41:24 -04:00
|
|
|
|
|
|
|
(define to-complete-without-prefix?
|
|
|
|
to-complete-prefix)
|
|
|
|
|
2005-08-17 09:29:55 -04:00
|
|
|
(define (make-empty-to-complete pos)
|
|
|
|
(make-to-complete #f pos))
|
2005-08-15 08:41:24 -04:00
|
|
|
|
2005-08-07 10:46:57 -04:00
|
|
|
(define operator-chars (char-set #\& #\| #\< #\> #\, #\;))
|
|
|
|
|
|
|
|
(define composed-operator-derivations
|
|
|
|
(char-set #\& #\* #\| #\> #\<))
|
|
|
|
|
|
|
|
(define (valid-operator? str)
|
|
|
|
(member str '("&" "&&" "&*" "|" "||" "<" "<<" ">" ">>" "," ",@" ";")))
|
|
|
|
|
|
|
|
(define token-terminating-chars
|
|
|
|
(char-set-union operator-chars char-set:whitespace))
|
|
|
|
|
2005-08-15 08:41:24 -04:00
|
|
|
(define (lex-token cursor-index port)
|
2005-08-07 10:46:57 -04:00
|
|
|
(let ((start-pos (current-column port)))
|
|
|
|
(let lp ((c (peek-char port)) (chars '()))
|
|
|
|
(cond
|
|
|
|
((or (eof-object? c)
|
|
|
|
(char-set-contains? token-terminating-chars c))
|
2005-08-15 08:41:24 -04:00
|
|
|
(make-string-token (reverse-list->string chars (length chars))
|
|
|
|
start-pos cursor-index port))
|
2005-08-07 10:46:57 -04:00
|
|
|
(else
|
|
|
|
(read-char port)
|
|
|
|
(lp (peek-char port) (cons c chars)))))))
|
|
|
|
|
2005-08-15 08:41:24 -04:00
|
|
|
(define (lex-operator cursor-index port)
|
|
|
|
(let ((start-pos (current-column port))
|
2005-08-07 10:46:57 -04:00
|
|
|
(c-1 (read-char port))
|
|
|
|
(c-2 (peek-char port)))
|
|
|
|
(if (eof-object? c-2)
|
2005-08-15 08:41:24 -04:00
|
|
|
(make-operator-token (string->symbol (string c-1))
|
|
|
|
start-pos cursor-index port)
|
2005-08-07 10:46:57 -04:00
|
|
|
(let ((c (if (char-set-contains? composed-operator-derivations c-2)
|
|
|
|
(begin
|
|
|
|
(read-char port)
|
2005-08-15 08:41:24 -04:00
|
|
|
(string-append (string c-1) (string c-2)))
|
2005-08-07 10:46:57 -04:00
|
|
|
(string c-1))))
|
|
|
|
(cond
|
|
|
|
((valid-operator? c)
|
2005-08-15 08:41:24 -04:00
|
|
|
(make-operator-token (string->symbol c)
|
|
|
|
start-pos cursor-index port))
|
2005-08-07 10:46:57 -04:00
|
|
|
(else
|
|
|
|
(error "Invalid operator in command line" c start-pos)))))))
|
|
|
|
|
2005-08-15 08:41:24 -04:00
|
|
|
(define (lex-port cursor-index port)
|
2005-08-07 10:46:57 -04:00
|
|
|
(let lp ((c (peek-char port)) (tokens '()))
|
|
|
|
(cond
|
|
|
|
((eof-object? c)
|
|
|
|
(reverse tokens))
|
|
|
|
((or (char=? c #\") (char=? c #\,))
|
2005-08-15 08:41:24 -04:00
|
|
|
(let* ((start-pos (current-column port))
|
|
|
|
(s-expr (read port))
|
|
|
|
(token
|
|
|
|
(make-s-expr-token
|
|
|
|
s-expr start-pos cursor-index port)))
|
2005-08-07 10:46:57 -04:00
|
|
|
(lp (peek-char port) (cons token tokens))))
|
|
|
|
((char-set-contains? operator-chars c)
|
2005-08-15 08:41:24 -04:00
|
|
|
(let ((token (lex-operator cursor-index port)))
|
2005-08-07 10:46:57 -04:00
|
|
|
(lp (peek-char port) (cons token tokens))))
|
|
|
|
((char-set-contains? char-set:whitespace c)
|
2005-08-18 05:23:19 -04:00
|
|
|
(let ((start-pos (current-column port)))
|
2005-08-15 08:41:24 -04:00
|
|
|
(read-char port)
|
2005-08-18 05:23:19 -04:00
|
|
|
(let read-whitspaces ((c (peek-char port))
|
2005-08-20 03:48:51 -04:00
|
|
|
(cursor? #f))
|
2005-08-18 05:23:19 -04:00
|
|
|
(cond
|
|
|
|
((eof-object? c)
|
|
|
|
(lp c (if (or cursor?
|
2005-08-21 12:53:23 -04:00
|
|
|
(and cursor-index
|
|
|
|
(= (current-column port) cursor-index)))
|
2005-08-18 05:23:19 -04:00
|
|
|
(cons (make-empty-to-complete cursor-index)
|
|
|
|
tokens)
|
|
|
|
tokens)))
|
|
|
|
((char-set-contains? char-set:whitespace c)
|
|
|
|
(read-char port)
|
|
|
|
(read-whitspaces (peek-char port)
|
2005-08-19 08:28:53 -04:00
|
|
|
(or cursor?
|
|
|
|
(and cursor-index
|
|
|
|
(= (current-column port) cursor-index)))))
|
2005-08-18 05:23:19 -04:00
|
|
|
(else (lp c tokens))))))
|
2005-08-07 10:46:57 -04:00
|
|
|
(else
|
2005-08-15 08:41:24 -04:00
|
|
|
(let ((token (lex-token cursor-index port)))
|
2005-08-07 10:46:57 -04:00
|
|
|
(lp (peek-char port) (cons token tokens)))))))
|
|
|
|
|
2005-08-15 08:41:24 -04:00
|
|
|
(define (lex-command-line cmd-line . args)
|
|
|
|
(let-optionals args
|
|
|
|
((cursor-index #f))
|
2005-08-20 03:48:51 -04:00
|
|
|
(with-handler
|
|
|
|
(lambda (c more)
|
|
|
|
(if (read-error? c)
|
|
|
|
(signal-syntax-error (condition-stuff c))
|
|
|
|
(more)))
|
|
|
|
(lambda ()
|
|
|
|
(lex-port
|
|
|
|
cursor-index
|
|
|
|
(make-tracking-input-port
|
|
|
|
(make-string-input-port cmd-line)))))))
|
2005-08-07 10:46:57 -04:00
|
|
|
|
|
|
|
;; abstract syntax
|
|
|
|
|
|
|
|
(define-record-type command-line :command-line
|
|
|
|
(make-command-line first-cmd combinator/cmds job-ctrl)
|
|
|
|
command-line?
|
|
|
|
(first-cmd command-line-first-cmd)
|
|
|
|
(combinator/cmds command-line-combinator/cmds)
|
|
|
|
(job-ctrl command-line-job-ctrl))
|
|
|
|
|
|
|
|
(define-record-discloser :command-line
|
|
|
|
(lambda (r)
|
|
|
|
`(command-line ,(command-line-first-cmd r)
|
|
|
|
,(command-line-combinator/cmds r)
|
|
|
|
,(command-line-job-ctrl r))))
|
|
|
|
|
|
|
|
(define-record-type command :command
|
|
|
|
(make-command executable args redirections)
|
|
|
|
command?
|
|
|
|
(executable command-executable)
|
|
|
|
(args command-args)
|
|
|
|
(redirections command-redirections))
|
|
|
|
|
|
|
|
(define-record-discloser :command
|
|
|
|
(lambda (r)
|
|
|
|
`(command ,(command-executable r)
|
|
|
|
,(command-args r) ,(command-redirections r))))
|
|
|
|
|
|
|
|
(define-record-type redirection :redirection
|
|
|
|
(make-redirection op dest)
|
|
|
|
redirection?
|
|
|
|
(op redirection-op)
|
|
|
|
(dest redirection-dest))
|
|
|
|
|
|
|
|
(define-record-discloser :redirection
|
|
|
|
(lambda (r)
|
|
|
|
`(redir ,(redirection-op r) ,(redirection-dest r))))
|
|
|
|
|
2005-08-15 08:41:24 -04:00
|
|
|
;; condition raised during parsing
|
|
|
|
|
|
|
|
(define-condition-type 'parser-error '(error))
|
|
|
|
(define parser-error? (condition-predicate 'parser-error))
|
|
|
|
|
|
|
|
(define-condition-type 'parser-syntax-error '(parser-error))
|
|
|
|
(define parser-syntax-error?
|
|
|
|
(condition-predicate 'parser-syntax-error))
|
|
|
|
|
|
|
|
(define-condition-type 'parser-unexpected-eof '(parser-error))
|
|
|
|
(define parser-unexpected-eof?
|
|
|
|
(condition-predicate 'parser-unexpected-eof))
|
|
|
|
|
|
|
|
(define (signal-syntax-error info)
|
|
|
|
(signal 'parser-syntax-error info))
|
|
|
|
|
|
|
|
(define (signal-unexpected-eof info)
|
|
|
|
(signal 'parser-unexpected-eof info))
|
|
|
|
|
2005-08-07 10:46:57 -04:00
|
|
|
;; a simple recursive descent parser
|
|
|
|
|
2005-08-15 08:41:24 -04:00
|
|
|
(define (extract-token t)
|
|
|
|
(cond
|
|
|
|
((to-complete? t)
|
|
|
|
t)
|
|
|
|
((cursor-on-token? t)
|
2005-08-19 08:28:53 -04:00
|
|
|
(make-to-complete (token-token t) (token-cursor-pos t)))
|
2005-08-15 08:41:24 -04:00
|
|
|
(else
|
|
|
|
(token-token t))))
|
2005-08-07 10:46:57 -04:00
|
|
|
|
2005-08-15 08:41:24 -04:00
|
|
|
(define (string-or-s-expr? t)
|
|
|
|
(or (string-token? t)
|
|
|
|
(s-expr-token? t)
|
|
|
|
(to-complete? t)))
|
2005-08-07 10:46:57 -04:00
|
|
|
|
|
|
|
(define (parse-string/s-expr tokens)
|
|
|
|
(if (null? tokens)
|
2005-08-15 08:41:24 -04:00
|
|
|
(signal-unexpected-eof 'parse-string/s-expr)
|
2005-08-07 10:46:57 -04:00
|
|
|
(let ((t (car tokens)))
|
|
|
|
(if (or (string-token? t) (s-expr-token? t))
|
2005-08-15 08:41:24 -04:00
|
|
|
(values (extract-token t) (cdr tokens))
|
|
|
|
(signal-syntax-error `(parse-string/s-expr ,t ,tokens))))))
|
2005-08-07 10:46:57 -04:00
|
|
|
|
|
|
|
(define (parse-many-satisfying predicate tokens)
|
|
|
|
(let lp ((tokens tokens) (found '()))
|
|
|
|
(if (or (null? tokens) (not (predicate (car tokens))))
|
|
|
|
(values (reverse found) tokens)
|
|
|
|
(lp (cdr tokens) (cons (car tokens) found)))))
|
|
|
|
|
|
|
|
(define (parse-redirection tokens)
|
|
|
|
(if (< (length tokens) 2)
|
|
|
|
(values #f tokens)
|
|
|
|
(let ((op (car tokens))
|
|
|
|
(obj (cadr tokens)))
|
|
|
|
(cond
|
|
|
|
((and (operator-token? op)
|
|
|
|
(member (token-token op) '(> < >>))
|
|
|
|
(string-or-s-expr? obj))
|
|
|
|
(values
|
2005-08-15 08:41:24 -04:00
|
|
|
(make-redirection (token-token op) (extract-token obj))
|
2005-08-07 10:46:57 -04:00
|
|
|
(cddr tokens)))
|
|
|
|
((and (operator-token? op)
|
|
|
|
(eq? (token-token op) '<<)
|
|
|
|
(s-expr-token? obj))
|
|
|
|
(values
|
2005-08-15 08:41:24 -04:00
|
|
|
(make-redirection (token-token op) (extract-token obj))
|
2005-08-07 10:46:57 -04:00
|
|
|
(cddr tokens)))
|
|
|
|
(else
|
|
|
|
(values #f tokens))))))
|
|
|
|
|
|
|
|
(define (parse-redirections tokens)
|
|
|
|
(let lp ((tokens tokens) (redirs '()))
|
|
|
|
(receive (redir tokens)
|
|
|
|
(parse-redirection tokens)
|
|
|
|
(if redir
|
|
|
|
(lp tokens (cons redir redirs))
|
|
|
|
(values (reverse redirs) tokens)))))
|
|
|
|
|
|
|
|
(define (parse-command tokens)
|
|
|
|
(receive (executable tokens)
|
|
|
|
(parse-string/s-expr tokens)
|
|
|
|
(receive (args tokens)
|
|
|
|
(parse-many-satisfying string-or-s-expr? tokens)
|
|
|
|
(receive (redirs tokens)
|
|
|
|
(parse-redirections tokens)
|
|
|
|
(values (make-command executable
|
2005-08-15 08:41:24 -04:00
|
|
|
(map extract-token args) redirs)
|
2005-08-07 10:46:57 -04:00
|
|
|
tokens)))))
|
|
|
|
|
|
|
|
(define combinator-tokens
|
|
|
|
(map string->symbol '(";" "|" "&&" "||")))
|
|
|
|
|
|
|
|
(define (combinator-token? token)
|
|
|
|
(and (operator-token? token)
|
|
|
|
(member (token-token token) combinator-tokens)))
|
|
|
|
|
|
|
|
(define (parse-combinator/command tokens)
|
|
|
|
(if (< (length tokens) 2)
|
|
|
|
(values #f tokens)
|
|
|
|
(let ((comb (car tokens))
|
|
|
|
(cmd (cdr tokens)))
|
|
|
|
(cond
|
|
|
|
((combinator-token? comb)
|
|
|
|
(receive (command tokens)
|
|
|
|
(parse-command cmd)
|
2005-08-15 08:41:24 -04:00
|
|
|
(values (cons (extract-token comb) command)
|
2005-08-07 10:46:57 -04:00
|
|
|
tokens)))
|
|
|
|
(else
|
|
|
|
(values #f tokens))))))
|
|
|
|
|
|
|
|
(define (parse-combinators/commands-list tokens)
|
|
|
|
(let lp ((tokens tokens) (combs/cmds '()))
|
|
|
|
(receive (comb/cmd tokens)
|
|
|
|
(parse-combinator/command tokens)
|
|
|
|
(if comb/cmd
|
|
|
|
(lp tokens (cons comb/cmd combs/cmds))
|
|
|
|
(values (reverse combs/cmds) tokens)))))
|
|
|
|
|
|
|
|
(define (parse-job-control tokens)
|
|
|
|
(if (null? tokens)
|
|
|
|
(values #f tokens)
|
|
|
|
(let ((t (car tokens)))
|
|
|
|
(if (and (operator-token? t)
|
2005-08-20 03:48:51 -04:00
|
|
|
(member (token-token t) '(&* &)))
|
2005-08-07 10:46:57 -04:00
|
|
|
(values (token-token t) (cdr tokens))
|
|
|
|
(values #f tokens)))))
|
|
|
|
|
|
|
|
(define (parse-command-line tokens)
|
|
|
|
(receive (command tokens)
|
|
|
|
(parse-command tokens)
|
|
|
|
(receive (combinator/command-list tokens)
|
|
|
|
(parse-combinators/commands-list tokens)
|
|
|
|
(receive (job-control tokens)
|
|
|
|
(parse-job-control tokens)
|
|
|
|
(if (null? tokens)
|
|
|
|
(make-command-line command combinator/command-list
|
|
|
|
job-control)
|
2005-08-15 08:41:24 -04:00
|
|
|
(signal-syntax-error `(parse-command-line ,tokens)))))))
|
|
|
|
|
|
|
|
;; for use with tab completion
|
|
|
|
(define (parse-command-line-carefully tokens)
|
|
|
|
(call-with-current-continuation
|
|
|
|
(lambda (esc)
|
|
|
|
(with-handler
|
|
|
|
(lambda (condition . more)
|
|
|
|
(if (parser-error? condition)
|
|
|
|
(esc #f)
|
|
|
|
(more)))
|
|
|
|
(lambda ()
|
|
|
|
(parse-command-line tokens))))))
|
|
|
|
|
|
|
|
(define (lex/parse-partial-command-line cmdln cursor-index)
|
|
|
|
(let ((tokens (lex-command-line cmdln cursor-index)))
|
|
|
|
(let lp ((tokens tokens))
|
|
|
|
(cond
|
|
|
|
((null? tokens)
|
2005-08-17 09:29:55 -04:00
|
|
|
(make-command-line
|
|
|
|
(make-command (make-to-complete #f 0) '() '())
|
|
|
|
'() #f))
|
2005-08-15 08:41:24 -04:00
|
|
|
((parse-command-line-carefully tokens)
|
|
|
|
=> (lambda (v) v))
|
|
|
|
(else
|
|
|
|
(lp (drop-right tokens 1)))))))
|
2005-08-07 10:46:57 -04:00
|
|
|
|
|
|
|
;; unparser
|
|
|
|
|
2006-04-05 03:21:41 -04:00
|
|
|
(define (unparse-string/s-expr completion pos v start-of-line?)
|
|
|
|
(if (not start-of-line?)
|
|
|
|
(display " "))
|
2005-08-07 10:46:57 -04:00
|
|
|
(cond
|
2005-08-19 08:28:53 -04:00
|
|
|
((and completion (to-complete? v))
|
|
|
|
(completion v)
|
|
|
|
(cell-set! pos (current-column (current-output-port))))
|
|
|
|
((string? v)
|
|
|
|
(display v))
|
2005-08-07 10:46:57 -04:00
|
|
|
((and (pair? v) (eq? (car v) 'unquote))
|
2005-08-19 08:28:53 -04:00
|
|
|
(display ",")
|
|
|
|
(display (cadr v)))
|
2005-08-07 10:46:57 -04:00
|
|
|
((and (pair? v) (eq? (car v) 'unquote-splicing))
|
2005-08-19 08:28:53 -04:00
|
|
|
(display ",@")
|
|
|
|
(display (cadr v)))
|
2005-08-07 10:46:57 -04:00
|
|
|
(else
|
2005-08-19 08:28:53 -04:00
|
|
|
(error "Don't know how to unparse this." v))))
|
|
|
|
|
|
|
|
(define (unparse-redirection completion pos rd)
|
|
|
|
(display " ")
|
2006-04-05 03:21:41 -04:00
|
|
|
(display (symbol->string (redirection-op rd)))
|
2005-08-19 08:28:53 -04:00
|
|
|
(unparse-string/s-expr completion pos
|
2006-04-05 03:21:41 -04:00
|
|
|
(redirection-dest rd) #f))
|
2005-08-19 08:28:53 -04:00
|
|
|
|
2006-04-05 03:21:41 -04:00
|
|
|
(define (unparse-command completion pos cmd start-of-line?)
|
|
|
|
(unparse-string/s-expr completion pos (command-executable cmd) start-of-line?)
|
2005-08-19 08:28:53 -04:00
|
|
|
(for-each (lambda (arg)
|
2006-04-05 03:21:41 -04:00
|
|
|
(unparse-string/s-expr completion pos arg #f))
|
2005-08-19 08:28:53 -04:00
|
|
|
(command-args cmd))
|
|
|
|
(for-each (lambda (arg)
|
2006-04-05 03:21:41 -04:00
|
|
|
(display " ")
|
|
|
|
(unparse-redirection completion pos arg))
|
2005-08-19 08:28:53 -04:00
|
|
|
(command-redirections cmd)))
|
|
|
|
|
|
|
|
(define (unparse-command-line cmdln . arg)
|
|
|
|
(let-optionals arg
|
|
|
|
((completion #f))
|
|
|
|
(let* ((pos (make-cell #f))
|
|
|
|
(string-port (make-string-output-port))
|
|
|
|
(track-port (make-tracking-output-port string-port)))
|
|
|
|
(with-current-output-port track-port
|
|
|
|
(unparse-command completion pos
|
2006-04-05 03:21:41 -04:00
|
|
|
(command-line-first-cmd cmdln) #t)
|
2005-08-19 08:28:53 -04:00
|
|
|
(for-each
|
|
|
|
(lambda (comb.cmd)
|
2006-04-05 03:21:41 -04:00
|
|
|
(display " ")
|
|
|
|
(display (symbol->string (car comb.cmd)))
|
|
|
|
(unparse-command completion pos (cdr comb.cmd) #f))
|
2005-08-19 08:28:53 -04:00
|
|
|
(command-line-combinator/cmds cmdln))
|
|
|
|
(cond
|
|
|
|
((command-line-job-ctrl cmdln)
|
|
|
|
=> (lambda (sym)
|
|
|
|
(display " ")
|
|
|
|
(display (symbol->string sym)))))
|
|
|
|
;; we're done
|
|
|
|
(close-output-port track-port)
|
|
|
|
(values (string-output-port-output string-port)
|
|
|
|
(cell-ref pos))))))
|
|
|
|
|
2006-04-18 11:46:57 -04:00
|
|
|
;;; cmdline expansion
|
|
|
|
|
|
|
|
;; helper functions for globbing
|
|
|
|
|
|
|
|
(define (contains-glob-enumerator? arg)
|
|
|
|
(if-match
|
|
|
|
(regexp-search
|
|
|
|
(rx (: (submatch (* any)) ("{[") (* any) (submatch (* any)) ("]}")))
|
|
|
|
arg)
|
|
|
|
(whole-arg submatch-before submatch-after)
|
|
|
|
(not (or (string-suffix? "\\" submatch-before)
|
|
|
|
(string-suffix? "\\" submatch-after)))
|
|
|
|
#f))
|
|
|
|
|
|
|
|
(define (contains-glob-wildcard? arg)
|
|
|
|
(if-match
|
|
|
|
(regexp-search (rx (: (submatch (* any)) ("*?"))) arg)
|
|
|
|
(whole-arg submatch-before)
|
|
|
|
(not (string-suffix? "\\" submatch-before))
|
|
|
|
#f))
|
|
|
|
|
|
|
|
(define (contains-glob-expression? arg)
|
|
|
|
(or (contains-glob-wildcard? arg)
|
|
|
|
(contains-glob-enumerator? arg)))
|
|
|
|
|
|
|
|
(define (glob-argument arg)
|
|
|
|
(let ((files (glob arg)))
|
|
|
|
(if (null? files)
|
|
|
|
(error "no files match this glob expression" arg (cwd))
|
|
|
|
files)))
|
|
|
|
|
|
|
|
;; expand command list:
|
|
|
|
;; - substiute environment vars in strings with their values
|
|
|
|
;; - globbing
|
|
|
|
;; - tilde expansion
|
|
|
|
|
|
|
|
|
|
|
|
(define (substitute-env-vars str)
|
|
|
|
(regexp-substitute/global
|
|
|
|
#f
|
|
|
|
(rx (: #\$ (? #\{) (submatch (+ alphanum)) (? #\})))
|
|
|
|
str
|
|
|
|
'pre
|
|
|
|
(lambda (m)
|
|
|
|
(or (lookup-env-var (match:substring m 1)) (match:substring m 1)))
|
|
|
|
'post))
|
|
|
|
|
|
|
|
|
|
|
|
(define (expand-filename-string s)
|
|
|
|
(debug-message "expand-filename-string " s)
|
|
|
|
(resolve-file-name (substitute-env-vars s)))
|
|
|
|
|
|
|
|
(define (expand-string/s-expr v)
|
|
|
|
(if (string? v)
|
|
|
|
(expand-filename-string v)
|
|
|
|
v))
|
|
|
|
|
|
|
|
(define (expand/glob-arguments args)
|
|
|
|
(fold-right
|
|
|
|
(lambda (arg args)
|
|
|
|
(let ((expanded (expand-string/s-expr arg)))
|
|
|
|
(if (and (string? expanded) (contains-glob-expression? expanded))
|
|
|
|
(append (glob-argument expanded) args)
|
|
|
|
(cons expanded args))))
|
|
|
|
'() args))
|
|
|
|
|
|
|
|
(define (expand-redirection redirection)
|
|
|
|
(make-redirection
|
|
|
|
(redirection-op redirection)
|
|
|
|
(expand-string/s-expr (redirection-dest redirection))))
|
|
|
|
|
|
|
|
(define (expand-command command)
|
|
|
|
(make-command
|
|
|
|
(expand-string/s-expr (command-executable command))
|
|
|
|
(expand/glob-arguments (command-args command))
|
|
|
|
(map expand-redirection (command-redirections command))))
|
|
|
|
|
|
|
|
(define (expand-command-line command-line)
|
|
|
|
(make-command-line
|
|
|
|
(expand-command (command-line-first-cmd command-line))
|
|
|
|
(map (lambda (combinator.command)
|
|
|
|
(cons (car combinator.command)
|
|
|
|
(expand-command (cdr combinator.command))))
|
|
|
|
(command-line-combinator/cmds command-line))
|
|
|
|
(command-line-job-ctrl command-line)))
|