2017-03-13 08:41:01 +08:00
|
|
|
//===--------------------- TildeExpressionResolver.cpp ----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Utility/TildeExpressionResolver.h"
|
|
|
|
|
2017-04-07 02:12:24 +08:00
|
|
|
#include <assert.h> // for assert
|
|
|
|
#include <system_error> // for error_code
|
|
|
|
|
|
|
|
#include "llvm/ADT/STLExtras.h" // for any_of
|
|
|
|
#include "llvm/ADT/SmallVector.h" // for SmallVectorImpl
|
2017-03-13 08:41:01 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2017-04-07 02:12:24 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h" // for fs
|
2017-03-13 08:41:01 +08:00
|
|
|
|
2018-04-10 21:33:45 +08:00
|
|
|
#if !defined(_WIN32)
|
2017-03-13 10:44:39 +08:00
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
|
2017-03-13 08:41:01 +08:00
|
|
|
using namespace lldb_private;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace fs = llvm::sys::fs;
|
|
|
|
namespace path = llvm::sys::path;
|
|
|
|
|
|
|
|
TildeExpressionResolver::~TildeExpressionResolver() {}
|
|
|
|
|
|
|
|
bool StandardTildeExpressionResolver::ResolveExact(
|
|
|
|
StringRef Expr, SmallVectorImpl<char> &Output) {
|
|
|
|
// We expect the tilde expression to be ONLY the expression itself, and
|
2017-03-17 06:28:04 +08:00
|
|
|
// contain no separators.
|
|
|
|
assert(!llvm::any_of(Expr, [](char c) { return path::is_separator(c); }));
|
2017-03-13 08:41:01 +08:00
|
|
|
assert(Expr.empty() || Expr[0] == '~');
|
|
|
|
|
|
|
|
return !fs::real_path(Expr, Output, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StandardTildeExpressionResolver::ResolvePartial(StringRef Expr,
|
|
|
|
StringSet<> &Output) {
|
|
|
|
// We expect the tilde expression to be ONLY the expression itself, and
|
|
|
|
// contain no separators.
|
2017-03-17 06:28:04 +08:00
|
|
|
assert(!llvm::any_of(Expr, [](char c) { return path::is_separator(c); }));
|
2017-03-13 08:41:01 +08:00
|
|
|
assert(Expr.empty() || Expr[0] == '~');
|
|
|
|
|
|
|
|
Output.clear();
|
2018-04-10 21:33:45 +08:00
|
|
|
#if defined(_WIN32) || defined(__ANDROID__)
|
2017-03-13 08:41:01 +08:00
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
if (Expr.empty())
|
|
|
|
return false;
|
|
|
|
|
2017-03-13 10:44:39 +08:00
|
|
|
SmallString<32> Buffer("~");
|
2017-03-13 08:41:01 +08:00
|
|
|
setpwent();
|
|
|
|
struct passwd *user_entry;
|
|
|
|
Expr = Expr.drop_front();
|
|
|
|
|
|
|
|
while ((user_entry = getpwent()) != NULL) {
|
|
|
|
StringRef ThisName(user_entry->pw_name);
|
|
|
|
if (!ThisName.startswith(Expr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Buffer.resize(1);
|
|
|
|
Buffer.append(ThisName);
|
2017-03-13 17:46:15 +08:00
|
|
|
Buffer.append(path::get_separator());
|
|
|
|
Output.insert(Buffer);
|
2017-03-13 08:41:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
2017-03-23 01:33:23 +08:00
|
|
|
|
|
|
|
bool TildeExpressionResolver::ResolveFullPath(
|
|
|
|
StringRef Expr, llvm::SmallVectorImpl<char> &Output) {
|
|
|
|
Output.clear();
|
|
|
|
if (!Expr.startswith("~")) {
|
|
|
|
Output.append(Expr.begin(), Expr.end());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace path = llvm::sys::path;
|
|
|
|
StringRef Left =
|
|
|
|
Expr.take_until([](char c) { return path::is_separator(c); });
|
|
|
|
|
|
|
|
if (!ResolveExact(Left, Output))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Output.append(Expr.begin() + Left.size(), Expr.end());
|
|
|
|
return true;
|
|
|
|
}
|