blob: 0fa25ed806ff7b8ffa942f1bba3126cbdf7254b4 [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
Kate Stoneb9c1b512016-09-06 20:57:50 +000012#include "lldb/Interpreter/CommandHistory.h"
Enrico Granata7594f142013-06-17 22:51:50 +000013
14using namespace lldb;
15using namespace lldb_private;
16
Kate Stoneb9c1b512016-09-06 20:57:50 +000017CommandHistory::CommandHistory() : m_mutex(), m_history() {}
Enrico Granata7594f142013-06-17 22:51:50 +000018
Kate Stoneb9c1b512016-09-06 20:57:50 +000019CommandHistory::~CommandHistory() {}
Enrico Granata7594f142013-06-17 22:51:50 +000020
Kate Stoneb9c1b512016-09-06 20:57:50 +000021size_t CommandHistory::GetSize() const {
22 std::lock_guard<std::recursive_mutex> guard(m_mutex);
23 return m_history.size();
Enrico Granata7594f142013-06-17 22:51:50 +000024}
25
Kate Stoneb9c1b512016-09-06 20:57:50 +000026bool CommandHistory::IsEmpty() const {
27 std::lock_guard<std::recursive_mutex> guard(m_mutex);
28 return m_history.empty();
Enrico Granata7594f142013-06-17 22:51:50 +000029}
30
Zachary Turner53877af2016-11-18 23:22:42 +000031llvm::Optional<llvm::StringRef>
32CommandHistory::FindString(llvm::StringRef input_str) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Zachary Turner53877af2016-11-18 23:22:42 +000034 if (input_str.size() < 2)
35 return llvm::None;
Enrico Granata7594f142013-06-17 22:51:50 +000036
Zachary Turner53877af2016-11-18 23:22:42 +000037 if (input_str[0] != g_repeat_char)
38 return llvm::None;
39
40 if (input_str[1] == g_repeat_char) {
Enrico Granata7594f142013-06-17 22:51:50 +000041 if (m_history.empty())
Zachary Turner53877af2016-11-18 23:22:42 +000042 return llvm::None;
Zachary Turnereb4a0cb2016-11-18 23:32:37 +000043 return llvm::StringRef(m_history.back());
Zachary Turner53877af2016-11-18 23:22:42 +000044 }
45
46 input_str = input_str.drop_front();
47
48 size_t idx = 0;
49 if (input_str.front() == '-') {
50 if (input_str.drop_front(2).getAsInteger(0, idx))
51 return llvm::None;
Zachary Turnereb4a0cb2016-11-18 23:32:37 +000052 if (idx >= m_history.size())
Zachary Turner53877af2016-11-18 23:22:42 +000053 return llvm::None;
54 idx = m_history.size() - idx;
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 } else {
Zachary Turner53877af2016-11-18 23:22:42 +000056 if (input_str.drop_front().getAsInteger(0, idx))
57 return llvm::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 if (idx >= m_history.size())
Zachary Turner53877af2016-11-18 23:22:42 +000059 return llvm::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 }
Zachary Turnereb4a0cb2016-11-18 23:32:37 +000061
62 return llvm::StringRef(m_history[idx]);
Enrico Granata7594f142013-06-17 22:51:50 +000063}
64
Zachary Turner53877af2016-11-18 23:22:42 +000065llvm::StringRef CommandHistory::GetStringAtIndex(size_t idx) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 std::lock_guard<std::recursive_mutex> guard(m_mutex);
67 if (idx < m_history.size())
Zachary Turner53877af2016-11-18 23:22:42 +000068 return m_history[idx];
69 return "";
Kate Stoneb9c1b512016-09-06 20:57:50 +000070}
71
Zachary Turner53877af2016-11-18 23:22:42 +000072llvm::StringRef CommandHistory::operator[](size_t idx) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 return GetStringAtIndex(idx);
74}
75
Zachary Turner53877af2016-11-18 23:22:42 +000076llvm::StringRef CommandHistory::GetRecentmostString() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 std::lock_guard<std::recursive_mutex> guard(m_mutex);
78 if (m_history.empty())
Zachary Turner53877af2016-11-18 23:22:42 +000079 return "";
80 return m_history.back();
Kate Stoneb9c1b512016-09-06 20:57:50 +000081}
82
Zachary Turner53877af2016-11-18 23:22:42 +000083void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 std::lock_guard<std::recursive_mutex> guard(m_mutex);
85 if (reject_if_dupe) {
86 if (!m_history.empty()) {
87 if (str == m_history.back())
88 return;
Enrico Granata7594f142013-06-17 22:51:50 +000089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 }
Zachary Turner53877af2016-11-18 23:22:42 +000091 m_history.push_back(str);
Enrico Granata7594f142013-06-17 22:51:50 +000092}
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094void CommandHistory::Clear() {
95 std::lock_guard<std::recursive_mutex> guard(m_mutex);
96 m_history.clear();
Enrico Granata7594f142013-06-17 22:51:50 +000097}
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099void CommandHistory::Dump(Stream &stream, size_t start_idx,
100 size_t stop_idx) const {
101 std::lock_guard<std::recursive_mutex> guard(m_mutex);
102 stop_idx = std::min(stop_idx + 1, m_history.size());
103 for (size_t counter = start_idx; counter < stop_idx; counter++) {
104 const std::string hist_item = m_history[counter];
105 if (!hist_item.empty()) {
106 stream.Indent();
107 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());
Enrico Granata7594f142013-06-17 22:51:50 +0000108 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 }
Enrico Granata7594f142013-06-17 22:51:50 +0000110}