blob: fda7a1a3e58edb3fd644449ae8f403661a2436f0 [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;
44 return m_history.back();
45 }
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;
53 if (idx > m_history.size())
54 return llvm::None;
55 idx = m_history.size() - idx;
56 return m_history[idx];
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 } else {
Zachary Turner53877af2016-11-18 23:22:42 +000059 if (input_str.drop_front().getAsInteger(0, idx))
60 return llvm::None;
61 if (idx > m_history.size())
62 return llvm::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 if (idx >= m_history.size())
Zachary Turner53877af2016-11-18 23:22:42 +000064 return llvm::None;
65 return m_history[idx];
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 }
Enrico Granata7594f142013-06-17 22:51:50 +000067}
68
Zachary Turner53877af2016-11-18 23:22:42 +000069llvm::StringRef CommandHistory::GetStringAtIndex(size_t idx) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 std::lock_guard<std::recursive_mutex> guard(m_mutex);
71 if (idx < m_history.size())
Zachary Turner53877af2016-11-18 23:22:42 +000072 return m_history[idx];
73 return "";
Kate Stoneb9c1b512016-09-06 20:57:50 +000074}
75
Zachary Turner53877af2016-11-18 23:22:42 +000076llvm::StringRef CommandHistory::operator[](size_t idx) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 return GetStringAtIndex(idx);
78}
79
Zachary Turner53877af2016-11-18 23:22:42 +000080llvm::StringRef CommandHistory::GetRecentmostString() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 std::lock_guard<std::recursive_mutex> guard(m_mutex);
82 if (m_history.empty())
Zachary Turner53877af2016-11-18 23:22:42 +000083 return "";
84 return m_history.back();
Kate Stoneb9c1b512016-09-06 20:57:50 +000085}
86
Zachary Turner53877af2016-11-18 23:22:42 +000087void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 std::lock_guard<std::recursive_mutex> guard(m_mutex);
89 if (reject_if_dupe) {
90 if (!m_history.empty()) {
91 if (str == m_history.back())
92 return;
Enrico Granata7594f142013-06-17 22:51:50 +000093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 }
Zachary Turner53877af2016-11-18 23:22:42 +000095 m_history.push_back(str);
Enrico Granata7594f142013-06-17 22:51:50 +000096}
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098void CommandHistory::Clear() {
99 std::lock_guard<std::recursive_mutex> guard(m_mutex);
100 m_history.clear();
Enrico Granata7594f142013-06-17 22:51:50 +0000101}
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103void CommandHistory::Dump(Stream &stream, size_t start_idx,
104 size_t stop_idx) const {
105 std::lock_guard<std::recursive_mutex> guard(m_mutex);
106 stop_idx = std::min(stop_idx + 1, m_history.size());
107 for (size_t counter = start_idx; counter < stop_idx; counter++) {
108 const std::string hist_item = m_history[counter];
109 if (!hist_item.empty()) {
110 stream.Indent();
111 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());
Enrico Granata7594f142013-06-17 22:51:50 +0000112 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 }
Enrico Granata7594f142013-06-17 22:51:50 +0000114}