From 1cd5d8bacb9dcdd5041453b26ed6119ccf69fbb7 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 24 Nov 2007 18:09:30 -0500 Subject: [PATCH] io.unix.launcher no longer depends on /bin/sh! Down with the GNU establishment! --- extra/io/unix/launcher/launcher-tests.factor | 19 +++++++++++ extra/io/unix/launcher/launcher.factor | 34 ++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100755 extra/io/unix/launcher/launcher-tests.factor mode change 100644 => 100755 extra/io/unix/launcher/launcher.factor diff --git a/extra/io/unix/launcher/launcher-tests.factor b/extra/io/unix/launcher/launcher-tests.factor new file mode 100755 index 0000000000..d07ab24da5 --- /dev/null +++ b/extra/io/unix/launcher/launcher-tests.factor @@ -0,0 +1,19 @@ +IN: temporary +USING: io.unix.launcher tools.test ; + +[ { } ] [ "" tokenize-command ] unit-test +[ { } ] [ " " tokenize-command ] unit-test +[ { "a" } ] [ "a" tokenize-command ] unit-test +[ { "abc" } ] [ "abc" tokenize-command ] unit-test +[ { "abc" } ] [ "abc " tokenize-command ] unit-test +[ { "abc" } ] [ " abc" tokenize-command ] unit-test +[ { "abc" "def" } ] [ "abc def" tokenize-command ] unit-test +[ { "abc def" } ] [ "abc\\ def" tokenize-command ] unit-test +[ { "abc\\" "def" } ] [ "abc\\\\ def" tokenize-command ] unit-test +[ { "abc\\ def" } ] [ "'abc\\\\ def'" tokenize-command ] unit-test +[ { "abc\\ def" } ] [ " 'abc\\\\ def'" tokenize-command ] unit-test +[ { "abc\\ def" "hey" } ] [ "'abc\\\\ def' hey" tokenize-command ] unit-test +[ { "abc def" "hey" } ] [ "'abc def' \"hey\"" tokenize-command ] unit-test +[ "'abc def' \"hey" tokenize-command ] unit-test-fails +[ "'abc def" tokenize-command ] unit-test-fails +[ { "abc def" "h\"ey" } ] [ "'abc def' \"h\"ey\" " tokenize-command ] unit-test diff --git a/extra/io/unix/launcher/launcher.factor b/extra/io/unix/launcher/launcher.factor old mode 100644 new mode 100755 index 0e7ec9ad16..2ca5b748fc --- a/extra/io/unix/launcher/launcher.factor +++ b/extra/io/unix/launcher/launcher.factor @@ -2,15 +2,43 @@ ! See http://factorcode.org/license.txt for BSD license. USING: io io.launcher io.unix.backend io.nonblocking sequences kernel namespaces math system alien.c-types -debugger continuations arrays assocs combinators unix.process ; +debugger continuations arrays assocs combinators unix.process +parser-combinators memoize ; IN: io.unix.launcher ! Search unix first USE: unix +! Our command line parser. Supported syntax: +! foo bar baz -- simple tokens +! foo\ bar -- escaping the space +! 'foo bar' -- quotation +! "foo bar" -- quotation +LAZY: 'escaped-char' "\\" token any-char-parser &> ; + +LAZY: 'chars' 'escaped-char' any-char-parser <|> <*> ; + +LAZY: 'non-space-char' + 'escaped-char' [ CHAR: \s = not ] satisfy <|> ; + +LAZY: 'quoted-1' 'chars' "\"" "\"" surrounded-by ; + +LAZY: 'quoted-2' 'chars' "'" "'" surrounded-by ; + +LAZY: 'unquoted' 'non-space-char' <+> ; + +LAZY: 'argument' + 'quoted-1' 'quoted-2' 'unquoted' <|> <|> + [ >string ] <@ ; + +MEMO: 'arguments' ( -- parser ) + 'argument' " " token <+> list-of ; + +: tokenize-command ( command -- arguments ) + 'arguments' parse-1 ; + : get-arguments ( -- seq ) - +command+ get - [ "/bin/sh" "-c" rot 3array ] [ +arguments+ get ] if* ; + +command+ get [ tokenize-command ] [ +arguments+ get ] if* ; : assoc>env ( assoc -- env ) [ "=" swap 3append ] { } assoc>map ;