pathname conversion -- handle dev:null, dev:stdout, dev:stdin, dev:stderr

This commit is contained in:
Kelvin Sherlock 2016-02-10 23:51:37 -05:00
parent 1339c0891f
commit a8920e2f53
2 changed files with 39 additions and 4 deletions

View File

@ -13,7 +13,10 @@
namespace ToolBox {
std::string MacToUnix(const std::string path);
std::string UnixToMac(const std::string path);
}
template<class T>
T pop(std::vector<T> &v) {
@ -42,7 +45,9 @@ int open(const std::string &name, int flags) {
// dup2 does not copy the O_CLOEXEC flag so it's safe to use.
int fd = ::open(name.c_str(), flags | O_CLOEXEC, 0666);
std::string uname = ToolBox::MacToUnix(name);
int fd = ::open(uname.c_str(), flags | O_CLOEXEC, 0666);
if (fd < 0) {
open_error(name);
return -1;

View File

@ -117,11 +117,42 @@ namespace {
write data;
}%%
%%{
machine dev;
main :=
'/dev/null'i $eof{ return "/dev/null"; }
|
'/dev/stderr'i $eof{ return "/dev/stderr"; }
|
'/dev/stdout'i $eof{ return "/dev/stdout"; }
;
write data;
}%%
}
namespace ToolBox
{
std::string check_dev(std::string &&str) {
const char *p = str.c_str();
const char *pe = p + str.length();
const char *eof = pe;
int cs;
%%{
machine dev;
write init;
write exec;
}%%
return str;
}
std::string MacToUnix(const std::string path)
{
@ -173,8 +204,7 @@ namespace ToolBox
write exec;
}%%
return rv;
return check_dev(std::move(rv));
}