new mac path expansion

This commit is contained in:
Kelvin Sherlock 2013-08-01 13:53:34 -04:00
parent 7a08004542
commit e1e3f3f4a2
3 changed files with 260 additions and 2 deletions

View File

@ -4,7 +4,8 @@ set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ -Wall -g")
add_definitions(-I ${CMAKE_SOURCE_DIR}/)
set(MPW_SRC mpw.cpp mpw_io.cpp mpw_close.cpp mpw_access.cpp mpw_ioctl.cpp environ.cpp)
set(MPW_SRC mpw.cpp mpw_io.cpp mpw_close.cpp mpw_access.cpp mpw_ioctl.cpp
environ.cpp epv.cpp ep.cpp)
add_custom_command(
OUTPUT environ.cpp
@ -12,8 +13,25 @@ add_custom_command(
MAIN_DEPENDENCY environ.rl
)
add_custom_command(
OUTPUT epv.cpp
COMMAND ragel -p -G2 -o epv.cpp "${CMAKE_CURRENT_SOURCE_DIR}/epv.rl"
MAIN_DEPENDENCY epv.rl
)
add_custom_command(
OUTPUT ep.cpp
COMMAND ragel -p -G2 -o ep.cpp "${CMAKE_CURRENT_SOURCE_DIR}/ep.rl"
MAIN_DEPENDENCY ep.rl
)
set_source_files_properties(
environ.cpp
environ.cpp ep.cpp epv.cpp
PROPERTIES
COMPILE_FLAGS
"${CMAKE_CXX_FLAGS} -Wno-unused-variable"

158
mpw/ep.rl Normal file
View File

@ -0,0 +1,158 @@
/*
* Copyright (c) 2013, Kelvin W Sherlock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
*
* file -> file
* :directory:file -> directory/file
* volume:directory -> /volume/directory
* :: -> ../
* ::: -> ../../
*/
%%{
machine m;
main := |*
':' {2,} {
// :: = ..
// ::: = ../..
// etc
unsigned count = te - ts;
if (ts != begin)
{
rv.push_back('/');
}
for (unsigned i = 1; i < count; ++i)
{
rv.append("../");
}
};
':' {
if (ts != begin)
rv.push_back('/');
};
any {
rv.push_back(*ts);
};
*|;
}%%
#include <string>
#include "mpw.h"
namespace {
%%write data;
}
namespace MPW {
std::string ExpandPathVariables(std::string);
std::string ExpandPath(std::string s)
{
std::string rv;
// 1. expand any variables.
#ifndef TESTING
if (s.find('{') != s.npos)
{
s = ExpandPathVariables(s);
}
#endif
// 2. does it even need it?
unsigned sep = 0;
for (char c : s)
{
if (c == ':' || c == '/')
{
sep = c;
break;
}
}
if (!sep) return s;
// convert volume:path to /volume/path
// leading / manually placed.
if (sep == ':' && s.front() != ':')
rv.push_back('/');
auto begin = s.c_str();
auto p = begin;
auto pe = p + s.length();
auto eof = pe;
const char *ts;
const char *te;
int cs;
int act;
%%write init;
%%write exec;
return rv;
}
}
#if TESTING
#include <cstdio>
int main(int argc, char **argv)
{
for (int i = 1; i < argc; ++i)
{
std::string s(argv[i]);
printf("%s -> ", s.c_str());
s = MPW::ExpandPath(s);
printf("%s\n", s.c_str());
}
return 0;
}
#endif

82
mpw/epv.rl Normal file
View File

@ -0,0 +1,82 @@
/*
* Copyright (c) 2013, Kelvin W Sherlock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
%%{
machine m;
main := |*
'{' [A-Za-z0-9_]+ '}' {
std::string name(ts + 1, te - 1);
rv.append(GetEnv(name));
};
any {
rv.push_back(*ts);
};
*|;
}%%
#include <string>
#include "mpw.h"
namespace {
%%write data;
}
namespace MPW {
std::string ExpandPathVariables(std::string s)
{
std::string rv;
auto p = s.c_str();
auto pe = p + s.length();
auto eof = pe;
const char *ts;
const char *te;
int cs;
int act;
%%write init;
%%write exec;
return rv;
}
}