blob: c16f71374c7edb4a8c3f073e0c72d2670944e8f4 [file] [log] [blame]
Enrico Granata7594f142013-06-17 22:51:50 +00001//===-- CommandHistory.cpp --------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Vince Harron5275aaa2015-01-15 20:08:35 +000010#include <inttypes.h>
11
Vince Harron5275aaa2015-01-15 20:08:35 +000012#include "lldb/Host/StringConvert.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000013#include "lldb/Interpreter/CommandHistory.h"
Enrico Granata7594f142013-06-17 22:51:50 +000014
15using namespace lldb;
16using namespace lldb_private;
17
Kate Stoneb9c1b512016-09-06 20:57:50 +000018CommandHistory::CommandHistory() : m_mutex(), m_history() {}
Enrico Granata7594f142013-06-17 22:51:50 +000019
Kate Stoneb9c1b512016-09-06 20:57:50 +000020CommandHistory::~CommandHistory() {}
Enrico Granata7594f142013-06-17 22:51:50 +000021
Kate Stoneb9c1b512016-09-06 20:57:50 +000022size_t CommandHistory::GetSize() const {
23 std::lock_guard<std::recursive_mutex> guard(m_mutex);
24 return m_history.size();
Enrico Granata7594f142013-06-17 22:51:50 +000025}
26
Kate Stoneb9c1b512016-09-06 20:57:50 +000027bool CommandHistory::IsEmpty() const {
28 std::lock_guard<std::recursive_mutex> guard(m_mutex);
29 return m_history.empty();
Enrico Granata7594f142013-06-17 22:51:50 +000030}
31
Zachary Turner53877af2016-11-18 23:22:42 +000032llvm::Optional<llvm::StringRef>
33CommandHistory::FindString(llvm::StringRef input_str) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000034 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Zachary Turner53877af2016-11-18 23:22:42 +000035 if (input_str.size() < 2)
36 return llvm::None;
Enrico Granata7594f142013-06-17 22:51:50 +000037
Zachary Turner53877af2016-11-18 23:22:42 +000038 if (input_str[0] != g_repeat_char)
39 return llvm::None;
40
41 if (input_str[1] == g_repeat_char) {
Enrico Granata7594f142013-06-17 22:51:50 +000042 if (m_history.empty())
Zachary Turner53877af2016-11-18 23:22:42 +000043 return llvm::None;
Zachary Turnereb4a0cb2016-11-18 23:32:37 +000044 return llvm::StringRef(m_history.back());
Zachary Turner53877af2016-11-18 23:22:42 +000045 }
46
47 input_str = input_str.drop_front();
48
49 size_t idx = 0;
50 if (input_str.front() == '-') {
51 if (input_str.drop_front(2).getAsInteger(0, idx))
52 return llvm::None;
Zachary Turnereb4a0cb2016-11-18 23:32:37 +000053 if (idx >= m_history.size())
Zachary Turner53877af2016-11-18 23:22:42 +000054 return llvm::None;
55 idx = m_history.size() - idx;
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 } else {
Zachary Turner53877af2016-11-18 23:22:42 +000057 if (input_str.drop_front().getAsInteger(0, idx))
58 return llvm::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 if (idx >= m_history.size())
Zachary Turner53877af2016-11-18 23:22:42 +000060 return llvm::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 }
Zachary Turnereb4a0cb2016-11-18 23:32:37 +000062
63 return llvm::StringRef(m_history[idx]);
Enrico Granata7594f142013-06-17 22:51:50 +000064}
65
Zachary Turner53877af2016-11-18 23:22:42 +000066llvm::StringRef CommandHistory::GetStringAtIndex(size_t idx) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 std::lock_guard<std::recursive_mutex> guard(m_mutex);
68 if (idx < m_history.size())
Zachary Turner53877af2016-11-18 23:22:42 +000069 return m_history[idx];
70 return "";
Kate Stoneb9c1b512016-09-06 20:57:50 +000071}
72
Zachary Turner53877af2016-11-18 23:22:42 +000073llvm::StringRef CommandHistory::operator[](size_t idx) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 return GetStringAtIndex(idx);
75}
76
Zachary Turner53877af2016-11-18 23:22:42 +000077llvm::StringRef CommandHistory::GetRecentmostString() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 std::lock_guard<std::recursive_mutex> guard(m_mutex);
79 if (m_history.empty())
Zachary Turner53877af2016-11-18 23:22:42 +000080 return "";
81 return m_history.back();
Kate Stoneb9c1b512016-09-06 20:57:50 +000082}
83
Zachary Turner53877af2016-11-18 23:22:42 +000084void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 std::lock_guard<std::recursive_mutex> guard(m_mutex);
86 if (reject_if_dupe) {
87 if (!m_history.empty()) {
88 if (str == m_history.back())
89 return;
Enrico Granata7594f142013-06-17 22:51:50 +000090 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 }
Zachary Turner53877af2016-11-18 23:22:42 +000092 m_history.push_back(str);
Enrico Granata7594f142013-06-17 22:51:50 +000093}
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095void CommandHistory::Clear() {
96 std::lock_guard<std::recursive_mutex> guard(m_mutex);
97 m_history.clear();
Enrico Granata7594f142013-06-17 22:51:50 +000098}
99
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100void CommandHistory::Dump(Stream &stream, size_t start_idx,
101 size_t stop_idx) const {
102 std::lock_guard<std::recursive_mutex> guard(m_mutex);
103 stop_idx = std::min(stop_idx + 1, m_history.size());
104 for (size_t counter = start_idx; counter < stop_idx; counter++) {
105 const std::string hist_item = m_history[counter];
106 if (!hist_item.empty()) {
107 stream.Indent();
108 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());
Enrico Granata7594f142013-06-17 22:51:50 +0000109 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 }
Enrico Granata7594f142013-06-17 22:51:50 +0000111}