summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPeter Collingbourne <peter@pcc.me.uk>2014-01-31 23:46:14 +0000
committerPeter Collingbourne <peter@pcc.me.uk>2014-01-31 23:46:14 +0000
commitcb6684b63b3c4c5a90e194c5719bc82690180f30 (patch)
treeb436db0df9f0c8d4f88b046f928a951fbf23cfff /lib
parentbef2236283c333f17613b2ea4904878228fedb6e (diff)
downloadllvm-cb6684b63b3c4c5a90e194c5719bc82690180f30.tar.gz
llvm-cb6684b63b3c4c5a90e194c5719bc82690180f30.tar.bz2
llvm-cb6684b63b3c4c5a90e194c5719bc82690180f30.tar.xz
Introduce line editor library.
This library will be used by clang-query. I can imagine LLDB becoming another client of this library, so I think LLVM is a sensible place for it to live. It wraps libedit, and adds tab completion support. The code is loosely based on the line editor bits in LLDB, with a few improvements: - Polymorphism for retrieving the list of tab completions, based on the concept pattern from the new pass manager. - Tab completion doesn't corrupt terminal output if the input covers multiple lines. Unfortunately this can only be done in a truly horrible way, as far as I can tell. But since the alternative is to implement our own line editor (which I don't think LLVM should be in the business of doing, at least for now) I think it may be acceptable. - Includes a fallback for the case where the user doesn't have libedit installed. Note that this uses C stdio, mainly because libedit also uses C stdio. Differential Revision: http://llvm-reviews.chandlerc.com/D2200 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200595 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/CMakeLists.txt1
-rw-r--r--lib/LLVMBuild.txt2
-rw-r--r--lib/LineEditor/CMakeLists.txt7
-rw-r--r--lib/LineEditor/LLVMBuild.txt22
-rw-r--r--lib/LineEditor/LineEditor.cpp325
-rw-r--r--lib/LineEditor/Makefile15
-rw-r--r--lib/Makefile2
7 files changed, 372 insertions, 2 deletions
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 7fbf12339d..9367f55313 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -15,3 +15,4 @@ add_subdirectory(DebugInfo)
add_subdirectory(ExecutionEngine)
add_subdirectory(Target)
add_subdirectory(AsmParser)
+add_subdirectory(LineEditor)
diff --git a/lib/LLVMBuild.txt b/lib/LLVMBuild.txt
index 00280c8603..a0984d410c 100644
--- a/lib/LLVMBuild.txt
+++ b/lib/LLVMBuild.txt
@@ -16,7 +16,7 @@
;===------------------------------------------------------------------------===;
[common]
-subdirectories = Analysis AsmParser Bitcode CodeGen DebugInfo ExecutionEngine Linker IR IRReader LTO MC Object Option Support TableGen Target Transforms
+subdirectories = Analysis AsmParser Bitcode CodeGen DebugInfo ExecutionEngine LineEditor Linker IR IRReader LTO MC Object Option Support TableGen Target Transforms
[component_0]
type = Group
diff --git a/lib/LineEditor/CMakeLists.txt b/lib/LineEditor/CMakeLists.txt
new file mode 100644
index 0000000000..cef36a40b4
--- /dev/null
+++ b/lib/LineEditor/CMakeLists.txt
@@ -0,0 +1,7 @@
+add_llvm_library(LLVMLineEditor
+ LineEditor.cpp
+ )
+
+if(HAVE_LIBEDIT)
+ target_link_libraries(LLVMLineEditor edit)
+endif()
diff --git a/lib/LineEditor/LLVMBuild.txt b/lib/LineEditor/LLVMBuild.txt
new file mode 100644
index 0000000000..35f4361e06
--- /dev/null
+++ b/lib/LineEditor/LLVMBuild.txt
@@ -0,0 +1,22 @@
+;===- ./lib/LineEditor/LLVMBuild.txt ---------------------------*- Conf -*--===;
+;
+; The LLVM Compiler Infrastructure
+;
+; This file is distributed under the University of Illinois Open Source
+; License. See LICENSE.TXT for details.
+;
+;===------------------------------------------------------------------------===;
+;
+; This is an LLVMBuild description file for the components in this subdirectory.
+;
+; For more information on the LLVMBuild system, please see:
+;
+; http://llvm.org/docs/LLVMBuild.html
+;
+;===------------------------------------------------------------------------===;
+
+[component_0]
+type = Library
+name = LineEditor
+parent = Libraries
+required_libraries = Support
diff --git a/lib/LineEditor/LineEditor.cpp b/lib/LineEditor/LineEditor.cpp
new file mode 100644
index 0000000000..8b7c12a0fc
--- /dev/null
+++ b/lib/LineEditor/LineEditor.cpp
@@ -0,0 +1,325 @@
+//===-- LineEditor.cpp - line editor --------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/LineEditor/LineEditor.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Config/config.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/raw_ostream.h"
+#include <stdio.h>
+#ifdef HAVE_LIBEDIT
+#include <histedit.h>
+#endif
+
+using namespace llvm;
+
+std::string LineEditor::getDefaultHistoryPath(StringRef ProgName) {
+ SmallString<32> Path;
+ if (sys::path::home_directory(Path)) {
+ sys::path::append(Path, "." + ProgName + "-history");
+ return Path.str();
+ }
+ return std::string();
+}
+
+LineEditor::CompleterConcept::~CompleterConcept() {}
+LineEditor::ListCompleterConcept::~ListCompleterConcept() {}
+
+std::string LineEditor::ListCompleterConcept::getCommonPrefix(
+ const std::vector<Completion> &Comps) {
+ assert(!Comps.empty());
+
+ std::string CommonPrefix = Comps[0].TypedText;
+ for (std::vector<Completion>::const_iterator I = Comps.begin() + 1,
+ E = Comps.end();
+ I != E; ++I) {
+ size_t Len = std::min(CommonPrefix.size(), I->TypedText.size());
+ size_t CommonLen = 0;
+ for (; CommonLen != Len; ++CommonLen) {
+ if (CommonPrefix[CommonLen] != I->TypedText[CommonLen])
+ break;
+ }
+ CommonPrefix.resize(CommonLen);
+ }
+ return CommonPrefix;
+}
+
+LineEditor::CompletionAction
+LineEditor::ListCompleterConcept::complete(StringRef Buffer, size_t Pos) const {
+ CompletionAction Action;
+ std::vector<Completion> Comps = getCompletions(Buffer, Pos);
+ if (Comps.empty()) {
+ Action.Kind = CompletionAction::AK_ShowCompletions;
+ return Action;
+ }
+
+ std::string CommonPrefix = getCommonPrefix(Comps);
+
+ // If the common prefix is non-empty we can simply insert it. If there is a
+ // single completion, this will insert the full completion. If there is more
+ // than one, this might be enough information to jog the user's memory but if
+ // not the user can also hit tab again to see the completions because the
+ // common prefix will then be empty.
+ if (CommonPrefix.empty()) {
+ Action.Kind = CompletionAction::AK_ShowCompletions;
+ for (std::vector<Completion>::iterator I = Comps.begin(), E = Comps.end();
+ I != E; ++I)
+ Action.Completions.push_back(I->DisplayText);
+ } else {
+ Action.Kind = CompletionAction::AK_Insert;
+ Action.Text = CommonPrefix;
+ }
+
+ return Action;
+}
+
+LineEditor::CompletionAction LineEditor::getCompletionAction(StringRef Buffer,
+ size_t Pos) const {
+ if (!Completer) {
+ CompletionAction Action;
+ Action.Kind = CompletionAction::AK_ShowCompletions;
+ return Action;
+ }
+
+ return Completer->complete(Buffer, Pos);
+}
+
+#ifdef HAVE_LIBEDIT
+
+// libedit-based implementation.
+
+struct LineEditor::InternalData {
+ LineEditor *LE;
+
+ History *Hist;
+ EditLine *EL;
+
+ unsigned PrevCount;
+ std::string ContinuationOutput;
+};
+
+static const char *ElGetPromptFn(EditLine *EL) {
+ LineEditor::InternalData *Data;
+ if (el_get(EL, EL_CLIENTDATA, &Data) == 0)
+ return Data->LE->getPrompt().c_str();
+ return "> ";
+}
+
+// Handles tab completion.
+//
+// This function is really horrible. But since the alternative is to get into
+// the line editor business, here we are.
+static unsigned char ElCompletionFn(EditLine *EL, int ch) {
+ LineEditor::InternalData *Data;
+ if (el_get(EL, EL_CLIENTDATA, &Data) == 0) {
+ if (!Data->ContinuationOutput.empty()) {
+ // This is the continuation of the AK_ShowCompletions branch below.
+ FILE *Out;
+ if (::el_get(EL, EL_GETFP, 1, &Out) != 0)
+ return CC_ERROR;
+
+ // Print the required output (see below).
+ ::fwrite(Data->ContinuationOutput.c_str(),
+ Data->ContinuationOutput.size(), 1, Out);
+
+ // Push a sequence of Ctrl-B characters to move the cursor back to its
+ // original position.
+ std::string Prevs(Data->PrevCount, '\02');
+ ::el_push(EL, (char *)Prevs.c_str());
+
+ Data->ContinuationOutput.clear();
+
+ return CC_REFRESH;
+ }
+
+ const LineInfo *LI = ::el_line(EL);
+ LineEditor::CompletionAction Action = Data->LE->getCompletionAction(
+ StringRef(LI->buffer, LI->lastchar - LI->buffer),
+ LI->cursor - LI->buffer);
+ switch (Action.Kind) {
+ case LineEditor::CompletionAction::AK_Insert:
+ ::el_insertstr(EL, Action.Text.c_str());
+ return CC_REFRESH;
+
+ case LineEditor::CompletionAction::AK_ShowCompletions:
+ if (Action.Completions.empty()) {
+ return CC_REFRESH_BEEP;
+ } else {
+ // Push a Ctrl-E and a tab. The Ctrl-E causes libedit to move the cursor
+ // to the end of the line, so that when we emit a newline we will be on
+ // a new blank line. The tab causes libedit to call this function again
+ // after moving the cursor. There doesn't seem to be anything we can do
+ // from here to cause libedit to move the cursor immediately. This will
+ // break horribly if the user has rebound their keys, so for now we do
+ // not permit user rebinding.
+ ::el_push(EL, (char *)"\05\t");
+
+ // This assembles the output for the continuation block above.
+ raw_string_ostream OS(Data->ContinuationOutput);
+
+ // Move cursor to a blank line.
+ OS << "\n";
+
+ // Emit the completions.
+ for (std::vector<std::string>::iterator I = Action.Completions.begin(),
+ E = Action.Completions.end();
+ I != E; ++I) {
+ OS << *I << "\n";
+ }
+
+ // Fool libedit into thinking nothing has changed. Reprint its prompt
+ // and the user input. Note that the cursor will remain at the end of
+ // the line after this.
+ OS << Data->LE->getPrompt()
+ << StringRef(LI->buffer, LI->lastchar - LI->buffer);
+
+ // This is the number of characters we need to tell libedit to go back:
+ // the distance between end of line and the original cursor position.
+ Data->PrevCount = LI->lastchar - LI->cursor;
+
+ return CC_REFRESH;
+ }
+ }
+ } else {
+ return CC_ERROR;
+ }
+}
+
+LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In,
+ FILE *Out, FILE *Err)
+ : Prompt((ProgName + "> ").str()), HistoryPath(HistoryPath),
+ Data(new InternalData) {
+ if (HistoryPath.empty())
+ this->HistoryPath = getDefaultHistoryPath(ProgName);
+
+ Data->LE = this;
+
+ Data->Hist = ::history_init();
+ assert(Data->Hist);
+
+ Data->EL = ::el_init(ProgName.str().c_str(), In, Out, Err);
+ assert(Data->EL);
+
+ ::el_set(Data->EL, EL_PROMPT, ElGetPromptFn);
+ ::el_set(Data->EL, EL_EDITOR, "emacs");
+ ::el_set(Data->EL, EL_HIST, history, Data->Hist);
+ ::el_set(Data->EL, EL_ADDFN, "tab_complete", "Tab completion function",
+ ElCompletionFn);
+ ::el_set(Data->EL, EL_BIND, "\t", "tab_complete", NULL);
+ ::el_set(Data->EL, EL_BIND, "^r", "em-inc-search-prev",
+ NULL); // Cycle through backwards search, entering string
+ ::el_set(Data->EL, EL_BIND, "^w", "ed-delete-prev-word",
+ NULL); // Delete previous word, behave like bash does.
+ ::el_set(Data->EL, EL_BIND, "\033[3~", "ed-delete-next-char",
+ NULL); // Fix the delete key.
+ ::el_set(Data->EL, EL_CLIENTDATA, Data.get());
+
+ HistEvent HE;
+ ::history(Data->Hist, &HE, H_SETSIZE, 800);
+ ::history(Data->Hist, &HE, H_SETUNIQUE, 1);
+ loadHistory();
+}
+
+LineEditor::~LineEditor() {
+ saveHistory();
+
+ FILE *Out;
+ if (::el_get(Data->EL, EL_GETFP, 1, &Out) != 0)
+ Out = 0;
+
+ ::history_end(Data->Hist);
+ ::el_end(Data->EL);
+
+ if (Out)
+ ::fwrite("\n", 1, 1, Out);
+}
+
+void LineEditor::saveHistory() {
+ if (!HistoryPath.empty()) {
+ HistEvent HE;
+ ::history(Data->Hist, &HE, H_SAVE, HistoryPath.c_str());
+ }
+}
+
+void LineEditor::loadHistory() {
+ if (!HistoryPath.empty()) {
+ HistEvent HE;
+ ::history(Data->Hist, &HE, H_LOAD, HistoryPath.c_str());
+ }
+}
+
+Optional<std::string> LineEditor::readLine() const {
+ // Call el_gets to prompt the user and read the user's input.
+ int LineLen = 0;
+ const char *Line = ::el_gets(Data->EL, &LineLen);
+
+ // Either of these may mean end-of-file.
+ if (!Line || LineLen == 0)
+ return Optional<std::string>();
+
+ // Strip any newlines off the end of the string.
+ while (LineLen > 0 &&
+ (Line[LineLen - 1] == '\n' || Line[LineLen - 1] == '\r'))
+ --LineLen;
+
+ HistEvent HE;
+ if (LineLen > 0)
+ ::history(Data->Hist, &HE, H_ENTER, Line);
+
+ return std::string(Line, LineLen);
+}
+
+#else
+
+// Simple fgets-based implementation.
+
+struct LineEditor::InternalData {
+ FILE *In;
+ FILE *Out;
+};
+
+LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In,
+ FILE *Out, FILE *Err)
+ : Prompt((ProgName + "> ").str()), Data(new InternalData) {
+ Data->In = In;
+ Data->Out = Out;
+}
+
+LineEditor::~LineEditor() {
+ ::fwrite("\n", 1, 1, Data->Out);
+}
+
+void LineEditor::saveHistory() {}
+void LineEditor::loadHistory() {}
+
+Optional<std::string> LineEditor::readLine() const {
+ ::fprintf(Data->Out, "%s", Prompt.c_str());
+
+ std::string Line;
+ do {
+ char Buf[64];
+ char *Res = ::fgets(Buf, sizeof(Buf), Data->In);
+ if (!Res) {
+ if (Line.empty())
+ return Optional<std::string>();
+ else
+ return Line;
+ }
+ Line.append(Buf);
+ } while (Line.empty() ||
+ (Line[Line.size() - 1] != '\n' && Line[Line.size() - 1] != '\r'));
+
+ while (!Line.empty() &&
+ (Line[Line.size() - 1] == '\n' || Line[Line.size() - 1] == '\r'))
+ Line.resize(Line.size() - 1);
+
+ return Line;
+}
+
+#endif
diff --git a/lib/LineEditor/Makefile b/lib/LineEditor/Makefile
new file mode 100644
index 0000000000..c7ff6d8eaa
--- /dev/null
+++ b/lib/LineEditor/Makefile
@@ -0,0 +1,15 @@
+##===- lib/LineEditor/Makefile -----------------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../..
+LIBRARYNAME = LLVMLineEditor
+BUILD_ARCHIVE := 1
+
+include $(LEVEL)/Makefile.common
+
diff --git a/lib/Makefile b/lib/Makefile
index 2ed0636cfd..a97f71aded 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -12,6 +12,6 @@ include $(LEVEL)/Makefile.config
PARALLEL_DIRS := IR AsmParser Bitcode Analysis Transforms CodeGen Target \
ExecutionEngine Linker LTO MC Object Option DebugInfo \
- IRReader
+ IRReader LineEditor
include $(LEVEL)/Makefile.common