1cfca06d7SDimitry Andric //===-- TildeExpressionResolver.cpp ---------------------------------------===//
274a628f7SDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
674a628f7SDimitry Andric //
774a628f7SDimitry Andric //===----------------------------------------------------------------------===//
874a628f7SDimitry Andric
974a628f7SDimitry Andric #include "lldb/Utility/TildeExpressionResolver.h"
1074a628f7SDimitry Andric
11344a3780SDimitry Andric #include <cassert>
1294994d37SDimitry Andric #include <system_error>
1374a628f7SDimitry Andric
1494994d37SDimitry Andric #include "llvm/ADT/STLExtras.h"
1594994d37SDimitry Andric #include "llvm/ADT/SmallVector.h"
1674a628f7SDimitry Andric #include "llvm/Support/FileSystem.h"
1774a628f7SDimitry Andric #include "llvm/Support/Path.h"
1894994d37SDimitry Andric #include "llvm/Support/raw_ostream.h"
1974a628f7SDimitry Andric
20f73363f1SDimitry Andric #if !defined(_WIN32)
2174a628f7SDimitry Andric #include <pwd.h>
2274a628f7SDimitry Andric #endif
2374a628f7SDimitry Andric
2474a628f7SDimitry Andric using namespace lldb_private;
2574a628f7SDimitry Andric using namespace llvm;
2674a628f7SDimitry Andric
2774a628f7SDimitry Andric namespace fs = llvm::sys::fs;
2874a628f7SDimitry Andric namespace path = llvm::sys::path;
2974a628f7SDimitry Andric
30344a3780SDimitry Andric TildeExpressionResolver::~TildeExpressionResolver() = default;
3174a628f7SDimitry Andric
ResolveExact(StringRef Expr,SmallVectorImpl<char> & Output)3274a628f7SDimitry Andric bool StandardTildeExpressionResolver::ResolveExact(
3374a628f7SDimitry Andric StringRef Expr, SmallVectorImpl<char> &Output) {
3474a628f7SDimitry Andric // We expect the tilde expression to be ONLY the expression itself, and
3574a628f7SDimitry Andric // contain no separators.
3674a628f7SDimitry Andric assert(!llvm::any_of(Expr, [](char c) { return path::is_separator(c); }));
3774a628f7SDimitry Andric assert(Expr.empty() || Expr[0] == '~');
3874a628f7SDimitry Andric
3974a628f7SDimitry Andric return !fs::real_path(Expr, Output, true);
4074a628f7SDimitry Andric }
4174a628f7SDimitry Andric
ResolvePartial(StringRef Expr,StringSet<> & Output)4274a628f7SDimitry Andric bool StandardTildeExpressionResolver::ResolvePartial(StringRef Expr,
4374a628f7SDimitry Andric StringSet<> &Output) {
4474a628f7SDimitry Andric // We expect the tilde expression to be ONLY the expression itself, and
4574a628f7SDimitry Andric // contain no separators.
4674a628f7SDimitry Andric assert(!llvm::any_of(Expr, [](char c) { return path::is_separator(c); }));
4774a628f7SDimitry Andric assert(Expr.empty() || Expr[0] == '~');
4874a628f7SDimitry Andric
4974a628f7SDimitry Andric Output.clear();
50f73363f1SDimitry Andric #if defined(_WIN32) || defined(__ANDROID__)
5174a628f7SDimitry Andric return false;
5274a628f7SDimitry Andric #else
5374a628f7SDimitry Andric if (Expr.empty())
5474a628f7SDimitry Andric return false;
5574a628f7SDimitry Andric
5674a628f7SDimitry Andric SmallString<32> Buffer("~");
5774a628f7SDimitry Andric setpwent();
5874a628f7SDimitry Andric struct passwd *user_entry;
5974a628f7SDimitry Andric Expr = Expr.drop_front();
6074a628f7SDimitry Andric
6194994d37SDimitry Andric while ((user_entry = getpwent()) != nullptr) {
6274a628f7SDimitry Andric StringRef ThisName(user_entry->pw_name);
63312c0ed1SDimitry Andric if (!ThisName.starts_with(Expr))
6474a628f7SDimitry Andric continue;
6574a628f7SDimitry Andric
6674a628f7SDimitry Andric Buffer.resize(1);
6774a628f7SDimitry Andric Buffer.append(ThisName);
6874a628f7SDimitry Andric Buffer.append(path::get_separator());
6974a628f7SDimitry Andric Output.insert(Buffer);
7074a628f7SDimitry Andric }
7174a628f7SDimitry Andric
7274a628f7SDimitry Andric return true;
7374a628f7SDimitry Andric #endif
7474a628f7SDimitry Andric }
7574a628f7SDimitry Andric
ResolveFullPath(StringRef Expr,llvm::SmallVectorImpl<char> & Output)7674a628f7SDimitry Andric bool TildeExpressionResolver::ResolveFullPath(
7774a628f7SDimitry Andric StringRef Expr, llvm::SmallVectorImpl<char> &Output) {
78312c0ed1SDimitry Andric if (!Expr.starts_with("~")) {
79b60736ecSDimitry Andric Output.assign(Expr.begin(), Expr.end());
8074a628f7SDimitry Andric return false;
8174a628f7SDimitry Andric }
8274a628f7SDimitry Andric
8374a628f7SDimitry Andric namespace path = llvm::sys::path;
8474a628f7SDimitry Andric StringRef Left =
8574a628f7SDimitry Andric Expr.take_until([](char c) { return path::is_separator(c); });
8674a628f7SDimitry Andric
87b60736ecSDimitry Andric if (!ResolveExact(Left, Output)) {
88b60736ecSDimitry Andric Output.assign(Expr.begin(), Expr.end());
8974a628f7SDimitry Andric return false;
90b60736ecSDimitry Andric }
9174a628f7SDimitry Andric
9274a628f7SDimitry Andric Output.append(Expr.begin() + Left.size(), Expr.end());
9374a628f7SDimitry Andric return true;
9474a628f7SDimitry Andric }
95