Utils/Console: Add support for quoted path components
g0dil [Wed, 1 Sep 2010 21:03:57 +0000 (21:03 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1706 270642c3-0616-0410-b53a-bc976706d245

senf/Utils/Console/Executor.test.cc
senf/Utils/Console/Parse.ih
senf/Utils/Console/Parse.test.cc

index 24bd307..8e56265 100644 (file)
@@ -51,7 +51,7 @@ SENF_AUTO_UNIT_TEST(executor)
 {
     namespace fty = senf::console::factory;
 
-    senf::console::root().add("dir1",fty::Directory()).add("dir3",fty::Directory());
+    senf::console::root().add("dir 1",fty::Directory()).add("dir3",fty::Directory());
     senf::console::root().add("dir2",fty::Directory()).doc("Helptext").add("test",fty::Command(&testCommand));
 
     senf::console::Executor executor;
@@ -61,11 +61,11 @@ SENF_AUTO_UNIT_TEST(executor)
 
     {
         std::stringstream os;
-        parser.parse("cd dir1", &setCommand);
+        parser.parse("cd \"dir 1\"", &setCommand);
         executor(os, commands.back());
         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinCD );
-        BOOST_CHECK( executor.cwd() == senf::console::root()["dir1"] );
-        BOOST_CHECK_EQUAL( executor.cwdPath(), "/dir1" );
+        BOOST_CHECK( executor.cwd() == senf::console::root()["dir 1"] );
+        BOOST_CHECK_EQUAL( executor.cwdPath(), "/dir 1" );
         BOOST_CHECK_EQUAL( os.str(), "" );
     }
 
@@ -80,7 +80,7 @@ SENF_AUTO_UNIT_TEST(executor)
 
     {
         std::stringstream os;
-        parser.parse("cd dir1", &setCommand);
+        parser.parse("cd \"dir 1\"", &setCommand);
         BOOST_CHECK_THROW( executor(os, commands.back()), senf::console::SyntaxErrorException );
         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinCD );
         BOOST_CHECK( &executor.cwd() == &senf::console::root()["dir2"] );
@@ -102,14 +102,14 @@ SENF_AUTO_UNIT_TEST(executor)
         executor(os, commands.back());
         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinLL );
         BOOST_CHECK_EQUAL( os.str(),
-                           "dir1/                       \n"
+                           "dir 1/                      \n"
                            "dir2/                       Helptext\n"
                            "sys/                        \n" );
     }
 
     {
         std::stringstream os;
-        parser.parse("ll dir1", &setCommand);
+        parser.parse("ll \"dir 1\"", &setCommand);
         executor(os, commands.back());
         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinLL );
         BOOST_CHECK_EQUAL( os.str(), "dir3/                       \n" );
@@ -129,7 +129,7 @@ SENF_AUTO_UNIT_TEST(executor)
         executor(os, commands.back());
         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinLR );
         BOOST_CHECK_EQUAL( os.str().substr(0,213),
-                           "dir1/                                   \n"
+                           "dir 1/                                  \n"
                            "  dir3/                                 \n"
                            "dir2/                                   Helptext\n"
                            "  test                                  \n"
@@ -138,10 +138,10 @@ SENF_AUTO_UNIT_TEST(executor)
 
     {
         std::stringstream os;
-        parser.parse("dir1/dir3 { }", &setCommand);
+        parser.parse("\"dir 1\"/dir3 { }", &setCommand);
         executor(os, commands.rbegin()[1]);
         BOOST_CHECK_EQUAL( commands.rbegin()[1].builtin(), senf::console::ParseCommandInfo::BuiltinPUSHD );
-        BOOST_CHECK( &executor.cwd() == &senf::console::root()["dir1"]["dir3"] );
+        BOOST_CHECK( &executor.cwd() == &senf::console::root()["dir 1"]["dir3"] );
         BOOST_CHECK_EQUAL( os.str(), "" );
     }
 
@@ -178,7 +178,7 @@ SENF_AUTO_UNIT_TEST(executor)
     }
 
     commands.clear();
-    senf::console::root().remove("dir1");
+    senf::console::root().remove("dir 1");
     senf::console::root().remove("dir2");
 }
 
index 1fd7d2d..fffaec2 100644 (file)
@@ -150,10 +150,10 @@ namespace detail {
                                                  boost_spirit::rule<Scanner>,
                                                  boost_spirit::rule<Scanner> >
         {
-            boost_spirit::rule<Scanner> command, path, argument, word, string, hexstring, token,
-                punctuation, hexbyte, balanced_tokens, simple_argument, complex_argument, builtin,
-                skip, statement, relpath, abspath, arguments, group_start, group_close,
-                statement_end, opt_path;
+            boost_spirit::rule<Scanner> command, path, argument, word, string, hexstring,
+                word_or_string, token, punctuation, hexbyte, balanced_tokens, simple_argument,
+                complex_argument, builtin, skip, statement, relpath, abspath, arguments,
+                group_start, group_close, statement_end, opt_path;
 
             definition(CommandGrammar const & self)
             {
@@ -307,7 +307,7 @@ namespace detail {
                     ;
 
                 relpath
-                    =    (   word                 [ push_back(path_, token_) ]
+                    =    (   word_or_string       [ push_back(path_, token_) ]
                            % +ch_p('/') )
                       >> ( ! (+ch_p('/') )        [ push_back(path_, construct_<Token>()) ] )
                     ;
@@ -375,6 +375,11 @@ namespace detail {
                                                         pos_) ]
                     ;
 
+                word_or_string
+                    =    word
+                    |    string
+                    ;
+
                 hexbyte
                     =    uint_parser<char, 16, 2, 2>()
                                                   [ push_back(str_, arg1) ]
index a214186..3ab948b 100644 (file)
@@ -128,10 +128,10 @@ SENF_AUTO_UNIT_TEST(commandGrammar)
     {
         ss.str("");
         BOOST_CHECK( senf::console::detail::boost_spirit::parse(
-                         "ls //foo/bar;",
+                         "ls //foo/\"bar\";",
                          grammar.use_parser<Grammar::CommandParser>(),
                          grammar.use_parser<Grammar::SkipParser>() ) . full );
-        BOOST_CHECK_EQUAL( ss.str(), "builtin_ls( None('')/Word('foo')/Word('bar') )\n" );
+        BOOST_CHECK_EQUAL( ss.str(), "builtin_ls( None('')/Word('foo')/BasicString('bar') )\n" );
     }
 
     {
@@ -164,11 +164,11 @@ SENF_AUTO_UNIT_TEST(commandGrammar)
     {
         ss.str("");
         BOOST_CHECK( senf::console::detail::boost_spirit::parse(
-                         "foo/bar// {",
+                         "foo/\"bar baz\"// {",
                          grammar.use_parser<Grammar::CommandParser>(),
                          grammar.use_parser<Grammar::SkipParser>() ) . full );
         BOOST_CHECK_EQUAL( ss.str(),
-                           "beginCommand( Word('foo')/Word('bar')/None('') )\n"
+                           "beginCommand( Word('foo')/BasicString('bar baz')/None('') )\n"
                            "pushDirectory()\n"
                            "endCommand()\n" );
     }
@@ -355,7 +355,7 @@ SENF_AUTO_UNIT_TEST(parseExceptions)
     CheckParseEx( "/foo/bar;\n  ()", "path expected\nat <unknown>:2:3" );
     CheckParseEx( "cd /foo/bar foo/bar", "end of statement expected\nat <unknown>:1:13" );
     CheckParseEx( "/foo/bar foo /", "end of statement expected\nat <unknown>:1:14" );
-    CheckParseEx( "cd \"foo\"", "path expected\nat <unknown>:1:4" );
+    CheckParseEx( "cd (foo)", "path expected\nat <unknown>:1:4" );
     CheckParseEx( "/foo/bar \"string", "'\"' expected\nat <unknown>:1:17" );
     CheckParseEx( "/foo/bar x\"hi\"", "'\"' expected\nat <unknown>:1:12" );
     CheckParseEx( "/foo/bar (", "')' expected\nat <unknown>:1:11" );