1*0b57cec5SDimitry Andric //===-- LineEditor.cpp - line editor --------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "llvm/LineEditor/LineEditor.h"
10*0b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
11*0b57cec5SDimitry Andric #include "llvm/Config/config.h"
12*0b57cec5SDimitry Andric #include "llvm/Support/Path.h"
13*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
14*0b57cec5SDimitry Andric #include <algorithm>
15*0b57cec5SDimitry Andric #include <cassert>
16*0b57cec5SDimitry Andric #include <cstdio>
17*0b57cec5SDimitry Andric #ifdef HAVE_LIBEDIT
18*0b57cec5SDimitry Andric #include <histedit.h>
19*0b57cec5SDimitry Andric #endif
20*0b57cec5SDimitry Andric
21*0b57cec5SDimitry Andric using namespace llvm;
22*0b57cec5SDimitry Andric
getDefaultHistoryPath(StringRef ProgName)23*0b57cec5SDimitry Andric std::string LineEditor::getDefaultHistoryPath(StringRef ProgName) {
24*0b57cec5SDimitry Andric SmallString<32> Path;
25*0b57cec5SDimitry Andric if (sys::path::home_directory(Path)) {
26*0b57cec5SDimitry Andric sys::path::append(Path, "." + ProgName + "-history");
27*0b57cec5SDimitry Andric return std::string(Path.str());
28*0b57cec5SDimitry Andric }
29*0b57cec5SDimitry Andric return std::string();
30*0b57cec5SDimitry Andric }
31*0b57cec5SDimitry Andric
~CompleterConcept()32*0b57cec5SDimitry Andric LineEditor::CompleterConcept::~CompleterConcept() {}
~ListCompleterConcept()33*0b57cec5SDimitry Andric LineEditor::ListCompleterConcept::~ListCompleterConcept() {}
34*0b57cec5SDimitry Andric
getCommonPrefix(const std::vector<Completion> & Comps)35*0b57cec5SDimitry Andric std::string LineEditor::ListCompleterConcept::getCommonPrefix(
36*0b57cec5SDimitry Andric const std::vector<Completion> &Comps) {
37*0b57cec5SDimitry Andric assert(!Comps.empty());
38*0b57cec5SDimitry Andric
39*0b57cec5SDimitry Andric std::string CommonPrefix = Comps[0].TypedText;
40*0b57cec5SDimitry Andric for (std::vector<Completion>::const_iterator I = Comps.begin() + 1,
41*0b57cec5SDimitry Andric E = Comps.end();
42*0b57cec5SDimitry Andric I != E; ++I) {
43*0b57cec5SDimitry Andric size_t Len = std::min(CommonPrefix.size(), I->TypedText.size());
44*0b57cec5SDimitry Andric size_t CommonLen = 0;
45*0b57cec5SDimitry Andric for (; CommonLen != Len; ++CommonLen) {
46*0b57cec5SDimitry Andric if (CommonPrefix[CommonLen] != I->TypedText[CommonLen])
47*0b57cec5SDimitry Andric break;
48*0b57cec5SDimitry Andric }
49*0b57cec5SDimitry Andric CommonPrefix.resize(CommonLen);
50*0b57cec5SDimitry Andric }
51*0b57cec5SDimitry Andric return CommonPrefix;
52*0b57cec5SDimitry Andric }
53*0b57cec5SDimitry Andric
54*0b57cec5SDimitry Andric LineEditor::CompletionAction
complete(StringRef Buffer,size_t Pos) const55*0b57cec5SDimitry Andric LineEditor::ListCompleterConcept::complete(StringRef Buffer, size_t Pos) const {
56*0b57cec5SDimitry Andric CompletionAction Action;
57*0b57cec5SDimitry Andric std::vector<Completion> Comps = getCompletions(Buffer, Pos);
58*0b57cec5SDimitry Andric if (Comps.empty()) {
59*0b57cec5SDimitry Andric Action.Kind = CompletionAction::AK_ShowCompletions;
60*0b57cec5SDimitry Andric return Action;
61*0b57cec5SDimitry Andric }
62*0b57cec5SDimitry Andric
63*0b57cec5SDimitry Andric std::string CommonPrefix = getCommonPrefix(Comps);
64*0b57cec5SDimitry Andric
65*0b57cec5SDimitry Andric // If the common prefix is non-empty we can simply insert it. If there is a
66*0b57cec5SDimitry Andric // single completion, this will insert the full completion. If there is more
67*0b57cec5SDimitry Andric // than one, this might be enough information to jog the user's memory but if
68*0b57cec5SDimitry Andric // not the user can also hit tab again to see the completions because the
69*0b57cec5SDimitry Andric // common prefix will then be empty.
70*0b57cec5SDimitry Andric if (CommonPrefix.empty()) {
71*0b57cec5SDimitry Andric Action.Kind = CompletionAction::AK_ShowCompletions;
72*0b57cec5SDimitry Andric for (std::vector<Completion>::iterator I = Comps.begin(), E = Comps.end();
73*0b57cec5SDimitry Andric I != E; ++I)
74*0b57cec5SDimitry Andric Action.Completions.push_back(I->DisplayText);
75*0b57cec5SDimitry Andric } else {
76*0b57cec5SDimitry Andric Action.Kind = CompletionAction::AK_Insert;
77*0b57cec5SDimitry Andric Action.Text = CommonPrefix;
78*0b57cec5SDimitry Andric }
79*0b57cec5SDimitry Andric
80*0b57cec5SDimitry Andric return Action;
81*0b57cec5SDimitry Andric }
82*0b57cec5SDimitry Andric
getCompletionAction(StringRef Buffer,size_t Pos) const83*0b57cec5SDimitry Andric LineEditor::CompletionAction LineEditor::getCompletionAction(StringRef Buffer,
84*0b57cec5SDimitry Andric size_t Pos) const {
85*0b57cec5SDimitry Andric if (!Completer) {
86*0b57cec5SDimitry Andric CompletionAction Action;
87*0b57cec5SDimitry Andric Action.Kind = CompletionAction::AK_ShowCompletions;
88*0b57cec5SDimitry Andric return Action;
89*0b57cec5SDimitry Andric }
90*0b57cec5SDimitry Andric
91*0b57cec5SDimitry Andric return Completer->complete(Buffer, Pos);
92*0b57cec5SDimitry Andric }
93*0b57cec5SDimitry Andric
94*0b57cec5SDimitry Andric #ifdef HAVE_LIBEDIT
95*0b57cec5SDimitry Andric
96*0b57cec5SDimitry Andric // libedit-based implementation.
97*0b57cec5SDimitry Andric
98*0b57cec5SDimitry Andric struct LineEditor::InternalData {
99*0b57cec5SDimitry Andric LineEditor *LE;
100*0b57cec5SDimitry Andric
101*0b57cec5SDimitry Andric History *Hist;
102*0b57cec5SDimitry Andric EditLine *EL;
103*0b57cec5SDimitry Andric
104*0b57cec5SDimitry Andric unsigned PrevCount;
105*0b57cec5SDimitry Andric std::string ContinuationOutput;
106*0b57cec5SDimitry Andric
107*0b57cec5SDimitry Andric FILE *Out;
108*0b57cec5SDimitry Andric };
109*0b57cec5SDimitry Andric
110*0b57cec5SDimitry Andric namespace {
111*0b57cec5SDimitry Andric
ElGetPromptFn(EditLine * EL)112*0b57cec5SDimitry Andric const char *ElGetPromptFn(EditLine *EL) {
113*0b57cec5SDimitry Andric LineEditor::InternalData *Data;
114*0b57cec5SDimitry Andric if (el_get(EL, EL_CLIENTDATA, &Data) == 0)
115*0b57cec5SDimitry Andric return Data->LE->getPrompt().c_str();
116*0b57cec5SDimitry Andric return "> ";
117*0b57cec5SDimitry Andric }
118*0b57cec5SDimitry Andric
119*0b57cec5SDimitry Andric // Handles tab completion.
120*0b57cec5SDimitry Andric //
121*0b57cec5SDimitry Andric // This function is really horrible. But since the alternative is to get into
122*0b57cec5SDimitry Andric // the line editor business, here we are.
ElCompletionFn(EditLine * EL,int ch)123*0b57cec5SDimitry Andric unsigned char ElCompletionFn(EditLine *EL, int ch) {
124*0b57cec5SDimitry Andric LineEditor::InternalData *Data;
125*0b57cec5SDimitry Andric if (el_get(EL, EL_CLIENTDATA, &Data) == 0) {
126*0b57cec5SDimitry Andric if (!Data->ContinuationOutput.empty()) {
127*0b57cec5SDimitry Andric // This is the continuation of the AK_ShowCompletions branch below.
128*0b57cec5SDimitry Andric FILE *Out = Data->Out;
129*0b57cec5SDimitry Andric
130*0b57cec5SDimitry Andric // Print the required output (see below).
131*0b57cec5SDimitry Andric ::fwrite(Data->ContinuationOutput.c_str(),
132*0b57cec5SDimitry Andric Data->ContinuationOutput.size(), 1, Out);
133*0b57cec5SDimitry Andric
134*0b57cec5SDimitry Andric // Push a sequence of Ctrl-B characters to move the cursor back to its
135*0b57cec5SDimitry Andric // original position.
136*0b57cec5SDimitry Andric std::string Prevs(Data->PrevCount, '\02');
137*0b57cec5SDimitry Andric ::el_push(EL, const_cast<char *>(Prevs.c_str()));
138*0b57cec5SDimitry Andric
139*0b57cec5SDimitry Andric Data->ContinuationOutput.clear();
140*0b57cec5SDimitry Andric
141*0b57cec5SDimitry Andric return CC_REFRESH;
142*0b57cec5SDimitry Andric }
143*0b57cec5SDimitry Andric
144*0b57cec5SDimitry Andric const LineInfo *LI = ::el_line(EL);
145*0b57cec5SDimitry Andric LineEditor::CompletionAction Action = Data->LE->getCompletionAction(
146*0b57cec5SDimitry Andric StringRef(LI->buffer, LI->lastchar - LI->buffer),
147*0b57cec5SDimitry Andric LI->cursor - LI->buffer);
148*0b57cec5SDimitry Andric switch (Action.Kind) {
149*0b57cec5SDimitry Andric case LineEditor::CompletionAction::AK_Insert:
150*0b57cec5SDimitry Andric ::el_insertstr(EL, Action.Text.c_str());
151*0b57cec5SDimitry Andric return CC_REFRESH;
152*0b57cec5SDimitry Andric
153*0b57cec5SDimitry Andric case LineEditor::CompletionAction::AK_ShowCompletions:
154*0b57cec5SDimitry Andric if (Action.Completions.empty()) {
155*0b57cec5SDimitry Andric return CC_REFRESH_BEEP;
156*0b57cec5SDimitry Andric } else {
157*0b57cec5SDimitry Andric // Push a Ctrl-E and a tab. The Ctrl-E causes libedit to move the cursor
158*0b57cec5SDimitry Andric // to the end of the line, so that when we emit a newline we will be on
159*0b57cec5SDimitry Andric // a new blank line. The tab causes libedit to call this function again
160*0b57cec5SDimitry Andric // after moving the cursor. There doesn't seem to be anything we can do
161*0b57cec5SDimitry Andric // from here to cause libedit to move the cursor immediately. This will
162*0b57cec5SDimitry Andric // break horribly if the user has rebound their keys, so for now we do
163*0b57cec5SDimitry Andric // not permit user rebinding.
164*0b57cec5SDimitry Andric ::el_push(EL, const_cast<char *>("\05\t"));
165*0b57cec5SDimitry Andric
166*0b57cec5SDimitry Andric // This assembles the output for the continuation block above.
167*0b57cec5SDimitry Andric raw_string_ostream OS(Data->ContinuationOutput);
168*0b57cec5SDimitry Andric
169*0b57cec5SDimitry Andric // Move cursor to a blank line.
170*0b57cec5SDimitry Andric OS << "\n";
171*0b57cec5SDimitry Andric
172*0b57cec5SDimitry Andric // Emit the completions.
173*0b57cec5SDimitry Andric for (std::vector<std::string>::iterator I = Action.Completions.begin(),
174*0b57cec5SDimitry Andric E = Action.Completions.end();
175*0b57cec5SDimitry Andric I != E; ++I) {
176*0b57cec5SDimitry Andric OS << *I << "\n";
177*0b57cec5SDimitry Andric }
178*0b57cec5SDimitry Andric
179*0b57cec5SDimitry Andric // Fool libedit into thinking nothing has changed. Reprint its prompt
180*0b57cec5SDimitry Andric // and the user input. Note that the cursor will remain at the end of
181*0b57cec5SDimitry Andric // the line after this.
182*0b57cec5SDimitry Andric OS << Data->LE->getPrompt()
183*0b57cec5SDimitry Andric << StringRef(LI->buffer, LI->lastchar - LI->buffer);
184*0b57cec5SDimitry Andric
185*0b57cec5SDimitry Andric // This is the number of characters we need to tell libedit to go back:
186*0b57cec5SDimitry Andric // the distance between end of line and the original cursor position.
187*0b57cec5SDimitry Andric Data->PrevCount = LI->lastchar - LI->cursor;
188*0b57cec5SDimitry Andric
189*0b57cec5SDimitry Andric return CC_REFRESH;
190*0b57cec5SDimitry Andric }
191*0b57cec5SDimitry Andric }
192*0b57cec5SDimitry Andric }
193*0b57cec5SDimitry Andric return CC_ERROR;
194*0b57cec5SDimitry Andric }
195*0b57cec5SDimitry Andric
196*0b57cec5SDimitry Andric } // end anonymous namespace
197*0b57cec5SDimitry Andric
LineEditor(StringRef ProgName,StringRef HistoryPath,FILE * In,FILE * Out,FILE * Err)198*0b57cec5SDimitry Andric LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In,
199*0b57cec5SDimitry Andric FILE *Out, FILE *Err)
200*0b57cec5SDimitry Andric : Prompt((ProgName + "> ").str()), HistoryPath(std::string(HistoryPath)),
201*0b57cec5SDimitry Andric Data(new InternalData) {
202*0b57cec5SDimitry Andric if (HistoryPath.empty())
203*0b57cec5SDimitry Andric this->HistoryPath = getDefaultHistoryPath(ProgName);
204*0b57cec5SDimitry Andric
205*0b57cec5SDimitry Andric Data->LE = this;
206*0b57cec5SDimitry Andric Data->Out = Out;
207*0b57cec5SDimitry Andric
208*0b57cec5SDimitry Andric Data->Hist = ::history_init();
209*0b57cec5SDimitry Andric assert(Data->Hist);
210*0b57cec5SDimitry Andric
211*0b57cec5SDimitry Andric Data->EL = ::el_init(ProgName.str().c_str(), In, Out, Err);
212*0b57cec5SDimitry Andric assert(Data->EL);
213*0b57cec5SDimitry Andric
214*0b57cec5SDimitry Andric ::el_set(Data->EL, EL_PROMPT, ElGetPromptFn);
215*0b57cec5SDimitry Andric ::el_set(Data->EL, EL_EDITOR, "emacs");
216*0b57cec5SDimitry Andric ::el_set(Data->EL, EL_HIST, history, Data->Hist);
217*0b57cec5SDimitry Andric ::el_set(Data->EL, EL_ADDFN, "tab_complete", "Tab completion function",
218*0b57cec5SDimitry Andric ElCompletionFn);
219*0b57cec5SDimitry Andric ::el_set(Data->EL, EL_BIND, "\t", "tab_complete", NULL);
220*0b57cec5SDimitry Andric ::el_set(Data->EL, EL_BIND, "^r", "em-inc-search-prev",
221*0b57cec5SDimitry Andric NULL); // Cycle through backwards search, entering string
222*0b57cec5SDimitry Andric ::el_set(Data->EL, EL_BIND, "^w", "ed-delete-prev-word",
223*0b57cec5SDimitry Andric NULL); // Delete previous word, behave like bash does.
224*0b57cec5SDimitry Andric ::el_set(Data->EL, EL_BIND, "\033[3~", "ed-delete-next-char",
225*0b57cec5SDimitry Andric NULL); // Fix the delete key.
226*0b57cec5SDimitry Andric ::el_set(Data->EL, EL_CLIENTDATA, Data.get());
227*0b57cec5SDimitry Andric
228*0b57cec5SDimitry Andric HistEvent HE;
229*0b57cec5SDimitry Andric ::history(Data->Hist, &HE, H_SETSIZE, 800);
230*0b57cec5SDimitry Andric ::history(Data->Hist, &HE, H_SETUNIQUE, 1);
231*0b57cec5SDimitry Andric loadHistory();
232*0b57cec5SDimitry Andric }
233*0b57cec5SDimitry Andric
~LineEditor()234*0b57cec5SDimitry Andric LineEditor::~LineEditor() {
235*0b57cec5SDimitry Andric saveHistory();
236*0b57cec5SDimitry Andric
237*0b57cec5SDimitry Andric ::history_end(Data->Hist);
238*0b57cec5SDimitry Andric ::el_end(Data->EL);
239*0b57cec5SDimitry Andric ::fwrite("\n", 1, 1, Data->Out);
240*0b57cec5SDimitry Andric }
241*0b57cec5SDimitry Andric
saveHistory()242*0b57cec5SDimitry Andric void LineEditor::saveHistory() {
243*0b57cec5SDimitry Andric if (!HistoryPath.empty()) {
244*0b57cec5SDimitry Andric HistEvent HE;
245*0b57cec5SDimitry Andric ::history(Data->Hist, &HE, H_SAVE, HistoryPath.c_str());
246*0b57cec5SDimitry Andric }
247*0b57cec5SDimitry Andric }
248*0b57cec5SDimitry Andric
loadHistory()249*0b57cec5SDimitry Andric void LineEditor::loadHistory() {
250*0b57cec5SDimitry Andric if (!HistoryPath.empty()) {
251*0b57cec5SDimitry Andric HistEvent HE;
252*0b57cec5SDimitry Andric ::history(Data->Hist, &HE, H_LOAD, HistoryPath.c_str());
253*0b57cec5SDimitry Andric }
254*0b57cec5SDimitry Andric }
255*0b57cec5SDimitry Andric
readLine() const256*0b57cec5SDimitry Andric Optional<std::string> LineEditor::readLine() const {
257*0b57cec5SDimitry Andric // Call el_gets to prompt the user and read the user's input.
258*0b57cec5SDimitry Andric int LineLen = 0;
259*0b57cec5SDimitry Andric const char *Line = ::el_gets(Data->EL, &LineLen);
260*0b57cec5SDimitry Andric
261*0b57cec5SDimitry Andric // Either of these may mean end-of-file.
262*0b57cec5SDimitry Andric if (!Line || LineLen == 0)
263*0b57cec5SDimitry Andric return Optional<std::string>();
264*0b57cec5SDimitry Andric
265*0b57cec5SDimitry Andric // Strip any newlines off the end of the string.
266*0b57cec5SDimitry Andric while (LineLen > 0 &&
267*0b57cec5SDimitry Andric (Line[LineLen - 1] == '\n' || Line[LineLen - 1] == '\r'))
268*0b57cec5SDimitry Andric --LineLen;
269*0b57cec5SDimitry Andric
270*0b57cec5SDimitry Andric HistEvent HE;
271*0b57cec5SDimitry Andric if (LineLen > 0)
272*0b57cec5SDimitry Andric ::history(Data->Hist, &HE, H_ENTER, Line);
273*0b57cec5SDimitry Andric
274*0b57cec5SDimitry Andric return std::string(Line, LineLen);
275*0b57cec5SDimitry Andric }
276*0b57cec5SDimitry Andric
277*0b57cec5SDimitry Andric #else // HAVE_LIBEDIT
278*0b57cec5SDimitry Andric
279*0b57cec5SDimitry Andric // Simple fgets-based implementation.
280*0b57cec5SDimitry Andric
281*0b57cec5SDimitry Andric struct LineEditor::InternalData {
282*0b57cec5SDimitry Andric FILE *In;
283*0b57cec5SDimitry Andric FILE *Out;
284*0b57cec5SDimitry Andric };
285*0b57cec5SDimitry Andric
LineEditor(StringRef ProgName,StringRef HistoryPath,FILE * In,FILE * Out,FILE * Err)286*0b57cec5SDimitry Andric LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In,
287*0b57cec5SDimitry Andric FILE *Out, FILE *Err)
288*0b57cec5SDimitry Andric : Prompt((ProgName + "> ").str()), Data(new InternalData) {
289*0b57cec5SDimitry Andric Data->In = In;
290*0b57cec5SDimitry Andric Data->Out = Out;
291*0b57cec5SDimitry Andric }
292*0b57cec5SDimitry Andric
~LineEditor()293*0b57cec5SDimitry Andric LineEditor::~LineEditor() {
294*0b57cec5SDimitry Andric ::fwrite("\n", 1, 1, Data->Out);
295*0b57cec5SDimitry Andric }
296*0b57cec5SDimitry Andric
saveHistory()297*0b57cec5SDimitry Andric void LineEditor::saveHistory() {}
loadHistory()298*0b57cec5SDimitry Andric void LineEditor::loadHistory() {}
299*0b57cec5SDimitry Andric
readLine() const300*0b57cec5SDimitry Andric Optional<std::string> LineEditor::readLine() const {
301*0b57cec5SDimitry Andric ::fprintf(Data->Out, "%s", Prompt.c_str());
302*0b57cec5SDimitry Andric
303*0b57cec5SDimitry Andric std::string Line;
304*0b57cec5SDimitry Andric do {
305*0b57cec5SDimitry Andric char Buf[64];
306*0b57cec5SDimitry Andric char *Res = ::fgets(Buf, sizeof(Buf), Data->In);
307*0b57cec5SDimitry Andric if (!Res) {
308*0b57cec5SDimitry Andric if (Line.empty())
309*0b57cec5SDimitry Andric return Optional<std::string>();
310*0b57cec5SDimitry Andric else
311*0b57cec5SDimitry Andric return Line;
312*0b57cec5SDimitry Andric }
313*0b57cec5SDimitry Andric Line.append(Buf);
314*0b57cec5SDimitry Andric } while (Line.empty() ||
315*0b57cec5SDimitry Andric (Line[Line.size() - 1] != '\n' && Line[Line.size() - 1] != '\r'));
316*0b57cec5SDimitry Andric
317*0b57cec5SDimitry Andric while (!Line.empty() &&
318*0b57cec5SDimitry Andric (Line[Line.size() - 1] == '\n' || Line[Line.size() - 1] == '\r'))
319*0b57cec5SDimitry Andric Line.resize(Line.size() - 1);
320*0b57cec5SDimitry Andric
321*0b57cec5SDimitry Andric return Line;
322*0b57cec5SDimitry Andric }
323*0b57cec5SDimitry Andric
324*0b57cec5SDimitry Andric #endif // HAVE_LIBEDIT
325