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