Skip to content

Commit

Permalink
Support reading input files from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed Jul 26, 2023
1 parent dd03221 commit 5d7eabb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
8 changes: 7 additions & 1 deletion source/text/Glob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ GlobRank svGlobInternal(const fs::path& basePath, std::string_view pattern, Glob
SLANG_EXPORT GlobRank svGlob(const fs::path& basePath, std::string_view pattern, GlobMode mode,
SmallVector<fs::path>& results, bool expandEnvVars,
std::error_code& ec) {
ec.clear();
if (pattern == "-"sv && mode == GlobMode::Files) {
// This is interpretted as trying to read stdin.
results.emplace_back("-");
return GlobRank::ExactPath;
}

fs::path patternPath;
if (expandEnvVars) {
std::string patternStr;
Expand Down Expand Up @@ -243,7 +250,6 @@ SLANG_EXPORT GlobRank svGlob(const fs::path& basePath, std::string_view pattern,
results.emplace_back(std::move(canonical));
}

ec.clear();
if (!anyWildcards && rank == GlobRank::SimpleName) {
// If there were no wildcards at all and we had a simple name match,
// promote the rank to an exact path match.
Expand Down
66 changes: 44 additions & 22 deletions source/util/OS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,28 @@ bool OS::fileSupportsColors(FILE* file) {
}

std::error_code OS::readFile(const fs::path& path, SmallVector<char>& buffer) {
HANDLE handle = ::CreateFileW(path.native().c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if (handle == INVALID_HANDLE_VALUE) {
// Provide a better error when trying to open directories.
std::error_code ec;
DWORD lastErr = ::GetLastError();
if (lastErr == ERROR_ACCESS_DENIED && fs::is_directory(path, ec))
return make_error_code(std::errc::is_a_directory);

return std::error_code(lastErr, std::system_category());
HANDLE handle;
auto& pathStr = path.native();
const bool isStdin = pathStr == L"-";

if (isStdin) {
_setmode(_fileno(stdin), _O_BINARY);
handle = ::GetStdHandle(STD_INPUT_HANDLE);
}
else {
handle = ::CreateFileW(pathStr.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if (handle == INVALID_HANDLE_VALUE) {
// Provide a better error when trying to open directories.
std::error_code ec;
DWORD lastErr = ::GetLastError();
if (lastErr == ERROR_ACCESS_DENIED && fs::is_directory(path, ec))
return make_error_code(std::errc::is_a_directory);

return std::error_code(lastErr, std::system_category());
}
}

std::error_code ec;
Expand Down Expand Up @@ -144,8 +154,10 @@ std::error_code OS::readFile(const fs::path& path, SmallVector<char>& buffer) {
buffer.back() = '\0';
}

if (!::CloseHandle(handle) && !ec)
ec.assign(::GetLastError(), std::system_category());
if (!isStdin) {
if (!::CloseHandle(handle) && !ec)
ec.assign(::GetLastError(), std::system_category());
}

return ec;
}
Expand All @@ -170,13 +182,21 @@ bool OS::fileSupportsColors(FILE* file) {

std::error_code OS::readFile(const fs::path& path, SmallVector<char>& buffer) {
int fd;
while (true) {
fd = ::open(path.native().c_str(), O_RDONLY | O_CLOEXEC);
if (fd >= 0)
break;
auto& pathStr = path.native();
const bool isStdin = pathStr == "-";

if (isStdin) {
fd = STDIN_FILENO;
}
else {
while (true) {
fd = ::open(pathStr.c_str(), O_RDONLY | O_CLOEXEC);
if (fd >= 0)
break;

if (errno != EINTR)
return std::error_code(errno, std::generic_category());
if (errno != EINTR)
return std::error_code(errno, std::generic_category());
}
}

std::error_code ec;
Expand Down Expand Up @@ -237,8 +257,10 @@ std::error_code OS::readFile(const fs::path& path, SmallVector<char>& buffer) {
buffer.back() = '\0';
}

if (::close(fd) < 0 && !ec)
ec.assign(errno, std::generic_category());
if (!isStdin) {
if (::close(fd) < 0 && !ec)
ec.assign(errno, std::generic_category());
}

return ec;
}
Expand Down

0 comments on commit 5d7eabb

Please sign in to comment.