10b57cec5SDimitry Andric //===-- LineEditor.cpp - line editor --------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "llvm/LineEditor/LineEditor.h" 10bdd1243dSDimitry Andric #include "llvm/ADT/STLExtras.h" 110b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 120b57cec5SDimitry Andric #include "llvm/Config/config.h" 130b57cec5SDimitry Andric #include "llvm/Support/Path.h" 140b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 150b57cec5SDimitry Andric #include <algorithm> 160b57cec5SDimitry Andric #include <cassert> 170b57cec5SDimitry Andric #include <cstdio> 180b57cec5SDimitry Andric #ifdef HAVE_LIBEDIT 190b57cec5SDimitry Andric #include <histedit.h> 200b57cec5SDimitry Andric #endif 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric using namespace llvm; 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric std::string LineEditor::getDefaultHistoryPath(StringRef ProgName) { 250b57cec5SDimitry Andric SmallString<32> Path; 260b57cec5SDimitry Andric if (sys::path::home_directory(Path)) { 270b57cec5SDimitry Andric sys::path::append(Path, "." + ProgName + "-history"); 28*7a6dacacSDimitry Andric return std::string(Path); 290b57cec5SDimitry Andric } 300b57cec5SDimitry Andric return std::string(); 310b57cec5SDimitry Andric } 320b57cec5SDimitry Andric 3381ad6265SDimitry Andric LineEditor::CompleterConcept::~CompleterConcept() = default; 3481ad6265SDimitry Andric LineEditor::ListCompleterConcept::~ListCompleterConcept() = default; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric std::string LineEditor::ListCompleterConcept::getCommonPrefix( 370b57cec5SDimitry Andric const std::vector<Completion> &Comps) { 380b57cec5SDimitry Andric assert(!Comps.empty()); 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric std::string CommonPrefix = Comps[0].TypedText; 41bdd1243dSDimitry Andric for (const Completion &C : llvm::drop_begin(Comps)) { 42bdd1243dSDimitry Andric size_t Len = std::min(CommonPrefix.size(), C.TypedText.size()); 430b57cec5SDimitry Andric size_t CommonLen = 0; 440b57cec5SDimitry Andric for (; CommonLen != Len; ++CommonLen) { 45bdd1243dSDimitry Andric if (CommonPrefix[CommonLen] != C.TypedText[CommonLen]) 460b57cec5SDimitry Andric break; 470b57cec5SDimitry Andric } 480b57cec5SDimitry Andric CommonPrefix.resize(CommonLen); 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric return CommonPrefix; 510b57cec5SDimitry Andric } 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric LineEditor::CompletionAction 540b57cec5SDimitry Andric LineEditor::ListCompleterConcept::complete(StringRef Buffer, size_t Pos) const { 550b57cec5SDimitry Andric CompletionAction Action; 560b57cec5SDimitry Andric std::vector<Completion> Comps = getCompletions(Buffer, Pos); 570b57cec5SDimitry Andric if (Comps.empty()) { 580b57cec5SDimitry Andric Action.Kind = CompletionAction::AK_ShowCompletions; 590b57cec5SDimitry Andric return Action; 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric std::string CommonPrefix = getCommonPrefix(Comps); 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric // If the common prefix is non-empty we can simply insert it. If there is a 650b57cec5SDimitry Andric // single completion, this will insert the full completion. If there is more 660b57cec5SDimitry Andric // than one, this might be enough information to jog the user's memory but if 670b57cec5SDimitry Andric // not the user can also hit tab again to see the completions because the 680b57cec5SDimitry Andric // common prefix will then be empty. 690b57cec5SDimitry Andric if (CommonPrefix.empty()) { 700b57cec5SDimitry Andric Action.Kind = CompletionAction::AK_ShowCompletions; 710eae32dcSDimitry Andric for (const Completion &Comp : Comps) 720eae32dcSDimitry Andric Action.Completions.push_back(Comp.DisplayText); 730b57cec5SDimitry Andric } else { 740b57cec5SDimitry Andric Action.Kind = CompletionAction::AK_Insert; 750b57cec5SDimitry Andric Action.Text = CommonPrefix; 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric return Action; 790b57cec5SDimitry Andric } 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric LineEditor::CompletionAction LineEditor::getCompletionAction(StringRef Buffer, 820b57cec5SDimitry Andric size_t Pos) const { 830b57cec5SDimitry Andric if (!Completer) { 840b57cec5SDimitry Andric CompletionAction Action; 850b57cec5SDimitry Andric Action.Kind = CompletionAction::AK_ShowCompletions; 860b57cec5SDimitry Andric return Action; 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric return Completer->complete(Buffer, Pos); 900b57cec5SDimitry Andric } 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric #ifdef HAVE_LIBEDIT 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric // libedit-based implementation. 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric struct LineEditor::InternalData { 970b57cec5SDimitry Andric LineEditor *LE; 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric History *Hist; 1000b57cec5SDimitry Andric EditLine *EL; 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric unsigned PrevCount; 1030b57cec5SDimitry Andric std::string ContinuationOutput; 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric FILE *Out; 1060b57cec5SDimitry Andric }; 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric namespace { 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric const char *ElGetPromptFn(EditLine *EL) { 1110b57cec5SDimitry Andric LineEditor::InternalData *Data; 1120b57cec5SDimitry Andric if (el_get(EL, EL_CLIENTDATA, &Data) == 0) 1130b57cec5SDimitry Andric return Data->LE->getPrompt().c_str(); 1140b57cec5SDimitry Andric return "> "; 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric // Handles tab completion. 1180b57cec5SDimitry Andric // 1190b57cec5SDimitry Andric // This function is really horrible. But since the alternative is to get into 1200b57cec5SDimitry Andric // the line editor business, here we are. 1210b57cec5SDimitry Andric unsigned char ElCompletionFn(EditLine *EL, int ch) { 1220b57cec5SDimitry Andric LineEditor::InternalData *Data; 1230b57cec5SDimitry Andric if (el_get(EL, EL_CLIENTDATA, &Data) == 0) { 1240b57cec5SDimitry Andric if (!Data->ContinuationOutput.empty()) { 1250b57cec5SDimitry Andric // This is the continuation of the AK_ShowCompletions branch below. 1260b57cec5SDimitry Andric FILE *Out = Data->Out; 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric // Print the required output (see below). 1290b57cec5SDimitry Andric ::fwrite(Data->ContinuationOutput.c_str(), 1300b57cec5SDimitry Andric Data->ContinuationOutput.size(), 1, Out); 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric // Push a sequence of Ctrl-B characters to move the cursor back to its 1330b57cec5SDimitry Andric // original position. 1340b57cec5SDimitry Andric std::string Prevs(Data->PrevCount, '\02'); 1350b57cec5SDimitry Andric ::el_push(EL, const_cast<char *>(Prevs.c_str())); 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric Data->ContinuationOutput.clear(); 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric return CC_REFRESH; 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric const LineInfo *LI = ::el_line(EL); 1430b57cec5SDimitry Andric LineEditor::CompletionAction Action = Data->LE->getCompletionAction( 1440b57cec5SDimitry Andric StringRef(LI->buffer, LI->lastchar - LI->buffer), 1450b57cec5SDimitry Andric LI->cursor - LI->buffer); 1460b57cec5SDimitry Andric switch (Action.Kind) { 1470b57cec5SDimitry Andric case LineEditor::CompletionAction::AK_Insert: 1480b57cec5SDimitry Andric ::el_insertstr(EL, Action.Text.c_str()); 1490b57cec5SDimitry Andric return CC_REFRESH; 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric case LineEditor::CompletionAction::AK_ShowCompletions: 1520b57cec5SDimitry Andric if (Action.Completions.empty()) { 1530b57cec5SDimitry Andric return CC_REFRESH_BEEP; 1540b57cec5SDimitry Andric } else { 1550b57cec5SDimitry Andric // Push a Ctrl-E and a tab. The Ctrl-E causes libedit to move the cursor 1560b57cec5SDimitry Andric // to the end of the line, so that when we emit a newline we will be on 1570b57cec5SDimitry Andric // a new blank line. The tab causes libedit to call this function again 1580b57cec5SDimitry Andric // after moving the cursor. There doesn't seem to be anything we can do 1590b57cec5SDimitry Andric // from here to cause libedit to move the cursor immediately. This will 1600b57cec5SDimitry Andric // break horribly if the user has rebound their keys, so for now we do 1610b57cec5SDimitry Andric // not permit user rebinding. 1620b57cec5SDimitry Andric ::el_push(EL, const_cast<char *>("\05\t")); 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric // This assembles the output for the continuation block above. 1650b57cec5SDimitry Andric raw_string_ostream OS(Data->ContinuationOutput); 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric // Move cursor to a blank line. 1680b57cec5SDimitry Andric OS << "\n"; 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric // Emit the completions. 171bdd1243dSDimitry Andric for (const std::string &Completion : Action.Completions) 172bdd1243dSDimitry Andric OS << Completion << "\n"; 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric // Fool libedit into thinking nothing has changed. Reprint its prompt 1750b57cec5SDimitry Andric // and the user input. Note that the cursor will remain at the end of 1760b57cec5SDimitry Andric // the line after this. 1770b57cec5SDimitry Andric OS << Data->LE->getPrompt() 1780b57cec5SDimitry Andric << StringRef(LI->buffer, LI->lastchar - LI->buffer); 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric // This is the number of characters we need to tell libedit to go back: 1810b57cec5SDimitry Andric // the distance between end of line and the original cursor position. 1820b57cec5SDimitry Andric Data->PrevCount = LI->lastchar - LI->cursor; 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric return CC_REFRESH; 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric } 1880b57cec5SDimitry Andric return CC_ERROR; 1890b57cec5SDimitry Andric } 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric } // end anonymous namespace 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In, 1940b57cec5SDimitry Andric FILE *Out, FILE *Err) 1955ffd83dbSDimitry Andric : Prompt((ProgName + "> ").str()), HistoryPath(std::string(HistoryPath)), 1960b57cec5SDimitry Andric Data(new InternalData) { 1970b57cec5SDimitry Andric if (HistoryPath.empty()) 1980b57cec5SDimitry Andric this->HistoryPath = getDefaultHistoryPath(ProgName); 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric Data->LE = this; 2010b57cec5SDimitry Andric Data->Out = Out; 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric Data->Hist = ::history_init(); 2040b57cec5SDimitry Andric assert(Data->Hist); 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric Data->EL = ::el_init(ProgName.str().c_str(), In, Out, Err); 2070b57cec5SDimitry Andric assert(Data->EL); 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric ::el_set(Data->EL, EL_PROMPT, ElGetPromptFn); 2100b57cec5SDimitry Andric ::el_set(Data->EL, EL_EDITOR, "emacs"); 2110b57cec5SDimitry Andric ::el_set(Data->EL, EL_HIST, history, Data->Hist); 2120b57cec5SDimitry Andric ::el_set(Data->EL, EL_ADDFN, "tab_complete", "Tab completion function", 2130b57cec5SDimitry Andric ElCompletionFn); 2140b57cec5SDimitry Andric ::el_set(Data->EL, EL_BIND, "\t", "tab_complete", NULL); 2150b57cec5SDimitry Andric ::el_set(Data->EL, EL_BIND, "^r", "em-inc-search-prev", 2160b57cec5SDimitry Andric NULL); // Cycle through backwards search, entering string 2170b57cec5SDimitry Andric ::el_set(Data->EL, EL_BIND, "^w", "ed-delete-prev-word", 2180b57cec5SDimitry Andric NULL); // Delete previous word, behave like bash does. 2190b57cec5SDimitry Andric ::el_set(Data->EL, EL_BIND, "\033[3~", "ed-delete-next-char", 2200b57cec5SDimitry Andric NULL); // Fix the delete key. 2210b57cec5SDimitry Andric ::el_set(Data->EL, EL_CLIENTDATA, Data.get()); 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric HistEvent HE; 2240b57cec5SDimitry Andric ::history(Data->Hist, &HE, H_SETSIZE, 800); 2250b57cec5SDimitry Andric ::history(Data->Hist, &HE, H_SETUNIQUE, 1); 2260b57cec5SDimitry Andric loadHistory(); 2270b57cec5SDimitry Andric } 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric LineEditor::~LineEditor() { 2300b57cec5SDimitry Andric saveHistory(); 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric ::history_end(Data->Hist); 2330b57cec5SDimitry Andric ::el_end(Data->EL); 2340b57cec5SDimitry Andric ::fwrite("\n", 1, 1, Data->Out); 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric void LineEditor::saveHistory() { 2380b57cec5SDimitry Andric if (!HistoryPath.empty()) { 2390b57cec5SDimitry Andric HistEvent HE; 2400b57cec5SDimitry Andric ::history(Data->Hist, &HE, H_SAVE, HistoryPath.c_str()); 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric } 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric void LineEditor::loadHistory() { 2450b57cec5SDimitry Andric if (!HistoryPath.empty()) { 2460b57cec5SDimitry Andric HistEvent HE; 2470b57cec5SDimitry Andric ::history(Data->Hist, &HE, H_LOAD, HistoryPath.c_str()); 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 251bdd1243dSDimitry Andric std::optional<std::string> LineEditor::readLine() const { 2520b57cec5SDimitry Andric // Call el_gets to prompt the user and read the user's input. 2530b57cec5SDimitry Andric int LineLen = 0; 2540b57cec5SDimitry Andric const char *Line = ::el_gets(Data->EL, &LineLen); 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric // Either of these may mean end-of-file. 2570b57cec5SDimitry Andric if (!Line || LineLen == 0) 258bdd1243dSDimitry Andric return std::nullopt; 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric // Strip any newlines off the end of the string. 2610b57cec5SDimitry Andric while (LineLen > 0 && 2620b57cec5SDimitry Andric (Line[LineLen - 1] == '\n' || Line[LineLen - 1] == '\r')) 2630b57cec5SDimitry Andric --LineLen; 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric HistEvent HE; 2660b57cec5SDimitry Andric if (LineLen > 0) 2670b57cec5SDimitry Andric ::history(Data->Hist, &HE, H_ENTER, Line); 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric return std::string(Line, LineLen); 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric #else // HAVE_LIBEDIT 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric // Simple fgets-based implementation. 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric struct LineEditor::InternalData { 2770b57cec5SDimitry Andric FILE *In; 2780b57cec5SDimitry Andric FILE *Out; 2790b57cec5SDimitry Andric }; 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In, 2820b57cec5SDimitry Andric FILE *Out, FILE *Err) 2830b57cec5SDimitry Andric : Prompt((ProgName + "> ").str()), Data(new InternalData) { 2840b57cec5SDimitry Andric Data->In = In; 2850b57cec5SDimitry Andric Data->Out = Out; 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric LineEditor::~LineEditor() { 2890b57cec5SDimitry Andric ::fwrite("\n", 1, 1, Data->Out); 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric void LineEditor::saveHistory() {} 2930b57cec5SDimitry Andric void LineEditor::loadHistory() {} 2940b57cec5SDimitry Andric 295bdd1243dSDimitry Andric std::optional<std::string> LineEditor::readLine() const { 2960b57cec5SDimitry Andric ::fprintf(Data->Out, "%s", Prompt.c_str()); 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric std::string Line; 2990b57cec5SDimitry Andric do { 3000b57cec5SDimitry Andric char Buf[64]; 3010b57cec5SDimitry Andric char *Res = ::fgets(Buf, sizeof(Buf), Data->In); 3020b57cec5SDimitry Andric if (!Res) { 3030b57cec5SDimitry Andric if (Line.empty()) 304bdd1243dSDimitry Andric return std::nullopt; 3050b57cec5SDimitry Andric else 3060b57cec5SDimitry Andric return Line; 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric Line.append(Buf); 3090b57cec5SDimitry Andric } while (Line.empty() || 3100b57cec5SDimitry Andric (Line[Line.size() - 1] != '\n' && Line[Line.size() - 1] != '\r')); 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric while (!Line.empty() && 3130b57cec5SDimitry Andric (Line[Line.size() - 1] == '\n' || Line[Line.size() - 1] == '\r')) 3140b57cec5SDimitry Andric Line.resize(Line.size() - 1); 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric return Line; 3170b57cec5SDimitry Andric } 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric #endif // HAVE_LIBEDIT 320