]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/common/Editline.cpp
Merge ^/head r317281 through r317502.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / common / Editline.cpp
1 //===-- Editline.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
10 #include <iomanip>
11 #include <iostream>
12 #include <limits.h>
13
14 #include "lldb/Host/ConnectionFileDescriptor.h"
15 #include "lldb/Host/Editline.h"
16 #include "lldb/Host/Host.h"
17 #include "lldb/Utility/Error.h"
18 #include "lldb/Utility/FileSpec.h"
19 #include "lldb/Utility/LLDBAssert.h"
20 #include "lldb/Utility/SelectHelper.h"
21 #include "lldb/Utility/StreamString.h"
22 #include "lldb/Utility/StringList.h"
23 #include "lldb/Utility/Timeout.h"
24
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Threading.h"
27
28 using namespace lldb_private;
29 using namespace lldb_private::line_editor;
30
31 // Workaround for what looks like an OS X-specific issue, but other platforms
32 // may benefit from something similar if issues arise.  The libedit library
33 // doesn't explicitly initialize the curses termcap library, which it gets away
34 // with until TERM is set to VT100 where it stumbles over an implementation
35 // assumption that may not exist on other platforms.  The setupterm() function
36 // would normally require headers that don't work gracefully in this context, so
37 // the function declaraction has been hoisted here.
38 #if defined(__APPLE__)
39 extern "C" {
40 int setupterm(char *term, int fildes, int *errret);
41 }
42 #define USE_SETUPTERM_WORKAROUND
43 #endif
44
45 // Editline uses careful cursor management to achieve the illusion of editing a
46 // multi-line block of text
47 // with a single line editor.  Preserving this illusion requires fairly careful
48 // management of cursor
49 // state.  Read and understand the relationship between DisplayInput(),
50 // MoveCursor(), SetCurrentLine(),
51 // and SaveEditedLine() before making changes.
52
53 #define ESCAPE "\x1b"
54 #define ANSI_FAINT ESCAPE "[2m"
55 #define ANSI_UNFAINT ESCAPE "[22m"
56 #define ANSI_CLEAR_BELOW ESCAPE "[J"
57 #define ANSI_CLEAR_RIGHT ESCAPE "[K"
58 #define ANSI_SET_COLUMN_N ESCAPE "[%dG"
59 #define ANSI_UP_N_ROWS ESCAPE "[%dA"
60 #define ANSI_DOWN_N_ROWS ESCAPE "[%dB"
61
62 #if LLDB_EDITLINE_USE_WCHAR
63
64 #define EditLineConstString(str) L##str
65 #define EditLineStringFormatSpec "%ls"
66
67 #else
68
69 #define EditLineConstString(str) str
70 #define EditLineStringFormatSpec "%s"
71
72 // use #defines so wide version functions and structs will resolve to old
73 // versions
74 // for case of libedit not built with wide char support
75 #define history_w history
76 #define history_winit history_init
77 #define history_wend history_end
78 #define HistoryW History
79 #define HistEventW HistEvent
80 #define LineInfoW LineInfo
81
82 #define el_wgets el_gets
83 #define el_wgetc el_getc
84 #define el_wpush el_push
85 #define el_wparse el_parse
86 #define el_wset el_set
87 #define el_wget el_get
88 #define el_wline el_line
89 #define el_winsertstr el_insertstr
90 #define el_wdeletestr el_deletestr
91
92 #endif // #if LLDB_EDITLINE_USE_WCHAR
93
94 bool IsOnlySpaces(const EditLineStringType &content) {
95   for (wchar_t ch : content) {
96     if (ch != EditLineCharType(' '))
97       return false;
98   }
99   return true;
100 }
101
102 EditLineStringType CombineLines(const std::vector<EditLineStringType> &lines) {
103   EditLineStringStreamType combined_stream;
104   for (EditLineStringType line : lines) {
105     combined_stream << line.c_str() << "\n";
106   }
107   return combined_stream.str();
108 }
109
110 std::vector<EditLineStringType> SplitLines(const EditLineStringType &input) {
111   std::vector<EditLineStringType> result;
112   size_t start = 0;
113   while (start < input.length()) {
114     size_t end = input.find('\n', start);
115     if (end == std::string::npos) {
116       result.insert(result.end(), input.substr(start));
117       break;
118     }
119     result.insert(result.end(), input.substr(start, end - start));
120     start = end + 1;
121   }
122   return result;
123 }
124
125 EditLineStringType FixIndentation(const EditLineStringType &line,
126                                   int indent_correction) {
127   if (indent_correction == 0)
128     return line;
129   if (indent_correction < 0)
130     return line.substr(-indent_correction);
131   return EditLineStringType(indent_correction, EditLineCharType(' ')) + line;
132 }
133
134 int GetIndentation(const EditLineStringType &line) {
135   int space_count = 0;
136   for (EditLineCharType ch : line) {
137     if (ch != EditLineCharType(' '))
138       break;
139     ++space_count;
140   }
141   return space_count;
142 }
143
144 bool IsInputPending(FILE *file) {
145   // FIXME: This will be broken on Windows if we ever re-enable Editline.  You
146   // can't use select
147   // on something that isn't a socket.  This will have to be re-written to not
148   // use a FILE*, but
149   // instead use some kind of yet-to-be-created abstraction that select-like
150   // functionality on
151   // non-socket objects.
152   const int fd = fileno(file);
153   SelectHelper select_helper;
154   select_helper.SetTimeout(std::chrono::microseconds(0));
155   select_helper.FDSetRead(fd);
156   return select_helper.Select().Success();
157 }
158
159 namespace lldb_private {
160 namespace line_editor {
161 typedef std::weak_ptr<EditlineHistory> EditlineHistoryWP;
162
163 // EditlineHistory objects are sometimes shared between multiple
164 // Editline instances with the same program name.
165
166 class EditlineHistory {
167 private:
168   // Use static GetHistory() function to get a EditlineHistorySP to one of these
169   // objects
170   EditlineHistory(const std::string &prefix, uint32_t size, bool unique_entries)
171       : m_history(NULL), m_event(), m_prefix(prefix), m_path() {
172     m_history = history_winit();
173     history_w(m_history, &m_event, H_SETSIZE, size);
174     if (unique_entries)
175       history_w(m_history, &m_event, H_SETUNIQUE, 1);
176   }
177
178   const char *GetHistoryFilePath() {
179     if (m_path.empty() && m_history && !m_prefix.empty()) {
180       FileSpec parent_path{"~/.lldb", true};
181       char history_path[PATH_MAX];
182       if (!llvm::sys::fs::create_directory(parent_path.GetPath())) {
183         snprintf(history_path, sizeof(history_path), "~/.lldb/%s-history",
184                  m_prefix.c_str());
185       } else {
186         snprintf(history_path, sizeof(history_path), "~/%s-widehistory",
187                  m_prefix.c_str());
188       }
189       m_path = FileSpec(history_path, true).GetPath();
190     }
191     if (m_path.empty())
192       return NULL;
193     return m_path.c_str();
194   }
195
196 public:
197   ~EditlineHistory() {
198     Save();
199
200     if (m_history) {
201       history_wend(m_history);
202       m_history = NULL;
203     }
204   }
205
206   static EditlineHistorySP GetHistory(const std::string &prefix) {
207     typedef std::map<std::string, EditlineHistoryWP> WeakHistoryMap;
208     static std::recursive_mutex g_mutex;
209     static WeakHistoryMap g_weak_map;
210     std::lock_guard<std::recursive_mutex> guard(g_mutex);
211     WeakHistoryMap::const_iterator pos = g_weak_map.find(prefix);
212     EditlineHistorySP history_sp;
213     if (pos != g_weak_map.end()) {
214       history_sp = pos->second.lock();
215       if (history_sp)
216         return history_sp;
217       g_weak_map.erase(pos);
218     }
219     history_sp.reset(new EditlineHistory(prefix, 800, true));
220     g_weak_map[prefix] = history_sp;
221     return history_sp;
222   }
223
224   bool IsValid() const { return m_history != NULL; }
225
226   HistoryW *GetHistoryPtr() { return m_history; }
227
228   void Enter(const EditLineCharType *line_cstr) {
229     if (m_history)
230       history_w(m_history, &m_event, H_ENTER, line_cstr);
231   }
232
233   bool Load() {
234     if (m_history) {
235       const char *path = GetHistoryFilePath();
236       if (path) {
237         history_w(m_history, &m_event, H_LOAD, path);
238         return true;
239       }
240     }
241     return false;
242   }
243
244   bool Save() {
245     if (m_history) {
246       const char *path = GetHistoryFilePath();
247       if (path) {
248         history_w(m_history, &m_event, H_SAVE, path);
249         return true;
250       }
251     }
252     return false;
253   }
254
255 protected:
256   HistoryW *m_history; // The history object
257   HistEventW m_event;  // The history event needed to contain all history events
258   std::string m_prefix; // The prefix name (usually the editline program name)
259                         // to use when loading/saving history
260   std::string m_path;   // Path to the history file
261 };
262 }
263 }
264
265 //------------------------------------------------------------------
266 // Editline private methods
267 //------------------------------------------------------------------
268
269 void Editline::SetBaseLineNumber(int line_number) {
270   std::stringstream line_number_stream;
271   line_number_stream << line_number;
272   m_base_line_number = line_number;
273   m_line_number_digits =
274       std::max(3, (int)line_number_stream.str().length() + 1);
275 }
276
277 std::string Editline::PromptForIndex(int line_index) {
278   bool use_line_numbers = m_multiline_enabled && m_base_line_number > 0;
279   std::string prompt = m_set_prompt;
280   if (use_line_numbers && prompt.length() == 0) {
281     prompt = ": ";
282   }
283   std::string continuation_prompt = prompt;
284   if (m_set_continuation_prompt.length() > 0) {
285     continuation_prompt = m_set_continuation_prompt;
286
287     // Ensure that both prompts are the same length through space padding
288     while (continuation_prompt.length() < prompt.length()) {
289       continuation_prompt += ' ';
290     }
291     while (prompt.length() < continuation_prompt.length()) {
292       prompt += ' ';
293     }
294   }
295
296   if (use_line_numbers) {
297     StreamString prompt_stream;
298     prompt_stream.Printf(
299         "%*d%s", m_line_number_digits, m_base_line_number + line_index,
300         (line_index == 0) ? prompt.c_str() : continuation_prompt.c_str());
301     return std::move(prompt_stream.GetString());
302   }
303   return (line_index == 0) ? prompt : continuation_prompt;
304 }
305
306 void Editline::SetCurrentLine(int line_index) {
307   m_current_line_index = line_index;
308   m_current_prompt = PromptForIndex(line_index);
309 }
310
311 int Editline::GetPromptWidth() { return (int)PromptForIndex(0).length(); }
312
313 bool Editline::IsEmacs() {
314   const char *editor;
315   el_get(m_editline, EL_EDITOR, &editor);
316   return editor[0] == 'e';
317 }
318
319 bool Editline::IsOnlySpaces() {
320   const LineInfoW *info = el_wline(m_editline);
321   for (const EditLineCharType *character = info->buffer;
322        character < info->lastchar; character++) {
323     if (*character != ' ')
324       return false;
325   }
326   return true;
327 }
328
329 int Editline::GetLineIndexForLocation(CursorLocation location, int cursor_row) {
330   int line = 0;
331   if (location == CursorLocation::EditingPrompt ||
332       location == CursorLocation::BlockEnd ||
333       location == CursorLocation::EditingCursor) {
334     for (unsigned index = 0; index < m_current_line_index; index++) {
335       line += CountRowsForLine(m_input_lines[index]);
336     }
337     if (location == CursorLocation::EditingCursor) {
338       line += cursor_row;
339     } else if (location == CursorLocation::BlockEnd) {
340       for (unsigned index = m_current_line_index; index < m_input_lines.size();
341            index++) {
342         line += CountRowsForLine(m_input_lines[index]);
343       }
344       --line;
345     }
346   }
347   return line;
348 }
349
350 void Editline::MoveCursor(CursorLocation from, CursorLocation to) {
351   const LineInfoW *info = el_wline(m_editline);
352   int editline_cursor_position =
353       (int)((info->cursor - info->buffer) + GetPromptWidth());
354   int editline_cursor_row = editline_cursor_position / m_terminal_width;
355
356   // Determine relative starting and ending lines
357   int fromLine = GetLineIndexForLocation(from, editline_cursor_row);
358   int toLine = GetLineIndexForLocation(to, editline_cursor_row);
359   if (toLine != fromLine) {
360     fprintf(m_output_file,
361             (toLine > fromLine) ? ANSI_DOWN_N_ROWS : ANSI_UP_N_ROWS,
362             std::abs(toLine - fromLine));
363   }
364
365   // Determine target column
366   int toColumn = 1;
367   if (to == CursorLocation::EditingCursor) {
368     toColumn =
369         editline_cursor_position - (editline_cursor_row * m_terminal_width) + 1;
370   } else if (to == CursorLocation::BlockEnd) {
371     toColumn =
372         ((m_input_lines[m_input_lines.size() - 1].length() + GetPromptWidth()) %
373          80) +
374         1;
375   }
376   fprintf(m_output_file, ANSI_SET_COLUMN_N, toColumn);
377 }
378
379 void Editline::DisplayInput(int firstIndex) {
380   fprintf(m_output_file, ANSI_SET_COLUMN_N ANSI_CLEAR_BELOW, 1);
381   int line_count = (int)m_input_lines.size();
382   const char *faint = m_color_prompts ? ANSI_FAINT : "";
383   const char *unfaint = m_color_prompts ? ANSI_UNFAINT : "";
384
385   for (int index = firstIndex; index < line_count; index++) {
386     fprintf(m_output_file, "%s"
387                            "%s"
388                            "%s" EditLineStringFormatSpec " ",
389             faint, PromptForIndex(index).c_str(), unfaint,
390             m_input_lines[index].c_str());
391     if (index < line_count - 1)
392       fprintf(m_output_file, "\n");
393   }
394 }
395
396 int Editline::CountRowsForLine(const EditLineStringType &content) {
397   auto prompt =
398       PromptForIndex(0); // Prompt width is constant during an edit session
399   int line_length = (int)(content.length() + prompt.length());
400   return (line_length / m_terminal_width) + 1;
401 }
402
403 void Editline::SaveEditedLine() {
404   const LineInfoW *info = el_wline(m_editline);
405   m_input_lines[m_current_line_index] =
406       EditLineStringType(info->buffer, info->lastchar - info->buffer);
407 }
408
409 StringList Editline::GetInputAsStringList(int line_count) {
410   StringList lines;
411   for (EditLineStringType line : m_input_lines) {
412     if (line_count == 0)
413       break;
414 #if LLDB_EDITLINE_USE_WCHAR
415     lines.AppendString(m_utf8conv.to_bytes(line));
416 #else
417     lines.AppendString(line);
418 #endif
419     --line_count;
420   }
421   return lines;
422 }
423
424 unsigned char Editline::RecallHistory(bool earlier) {
425   if (!m_history_sp || !m_history_sp->IsValid())
426     return CC_ERROR;
427
428   HistoryW *pHistory = m_history_sp->GetHistoryPtr();
429   HistEventW history_event;
430   std::vector<EditLineStringType> new_input_lines;
431
432   // Treat moving from the "live" entry differently
433   if (!m_in_history) {
434     if (earlier == false)
435       return CC_ERROR; // Can't go newer than the "live" entry
436     if (history_w(pHistory, &history_event, H_FIRST) == -1)
437       return CC_ERROR;
438
439     // Save any edits to the "live" entry in case we return by moving forward in
440     // history
441     // (it would be more bash-like to save over any current entry, but libedit
442     // doesn't
443     // offer the ability to add entries anywhere except the end.)
444     SaveEditedLine();
445     m_live_history_lines = m_input_lines;
446     m_in_history = true;
447   } else {
448     if (history_w(pHistory, &history_event, earlier ? H_NEXT : H_PREV) == -1) {
449       // Can't move earlier than the earliest entry
450       if (earlier)
451         return CC_ERROR;
452
453       // ... but moving to newer than the newest yields the "live" entry
454       new_input_lines = m_live_history_lines;
455       m_in_history = false;
456     }
457   }
458
459   // If we're pulling the lines from history, split them apart
460   if (m_in_history)
461     new_input_lines = SplitLines(history_event.str);
462
463   // Erase the current edit session and replace it with a new one
464   MoveCursor(CursorLocation::EditingCursor, CursorLocation::BlockStart);
465   m_input_lines = new_input_lines;
466   DisplayInput();
467
468   // Prepare to edit the last line when moving to previous entry, or the first
469   // line
470   // when moving to next entry
471   SetCurrentLine(m_current_line_index =
472                      earlier ? (int)m_input_lines.size() - 1 : 0);
473   MoveCursor(CursorLocation::BlockEnd, CursorLocation::EditingPrompt);
474   return CC_NEWLINE;
475 }
476
477 int Editline::GetCharacter(EditLineCharType *c) {
478   const LineInfoW *info = el_wline(m_editline);
479
480   // Paint a faint version of the desired prompt over the version libedit draws
481   // (will only be requested if colors are supported)
482   if (m_needs_prompt_repaint) {
483     MoveCursor(CursorLocation::EditingCursor, CursorLocation::EditingPrompt);
484     fprintf(m_output_file, "%s"
485                            "%s"
486                            "%s",
487             ANSI_FAINT, Prompt(), ANSI_UNFAINT);
488     MoveCursor(CursorLocation::EditingPrompt, CursorLocation::EditingCursor);
489     m_needs_prompt_repaint = false;
490   }
491
492   if (m_multiline_enabled) {
493     // Detect when the number of rows used for this input line changes due to an
494     // edit
495     int lineLength = (int)((info->lastchar - info->buffer) + GetPromptWidth());
496     int new_line_rows = (lineLength / m_terminal_width) + 1;
497     if (m_current_line_rows != -1 && new_line_rows != m_current_line_rows) {
498       // Respond by repainting the current state from this line on
499       MoveCursor(CursorLocation::EditingCursor, CursorLocation::EditingPrompt);
500       SaveEditedLine();
501       DisplayInput(m_current_line_index);
502       MoveCursor(CursorLocation::BlockEnd, CursorLocation::EditingCursor);
503     }
504     m_current_line_rows = new_line_rows;
505   }
506
507   // Read an actual character
508   while (true) {
509     lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess;
510     char ch = 0;
511
512     // This mutex is locked by our caller (GetLine). Unlock it while we read a
513     // character
514     // (blocking operation), so we do not hold the mutex indefinitely. This
515     // gives a chance
516     // for someone to interrupt us. After Read returns, immediately lock the
517     // mutex again and
518     // check if we were interrupted.
519     m_output_mutex.unlock();
520     int read_count = m_input_connection.Read(&ch, 1, llvm::None, status, NULL);
521     m_output_mutex.lock();
522     if (m_editor_status == EditorStatus::Interrupted) {
523       while (read_count > 0 && status == lldb::eConnectionStatusSuccess)
524         read_count = m_input_connection.Read(&ch, 1, llvm::None, status, NULL);
525       lldbassert(status == lldb::eConnectionStatusInterrupted);
526       return 0;
527     }
528
529     if (read_count) {
530       if (CompleteCharacter(ch, *c))
531         return 1;
532     } else {
533       switch (status) {
534       case lldb::eConnectionStatusSuccess: // Success
535         break;
536
537       case lldb::eConnectionStatusInterrupted:
538         lldbassert(0 && "Interrupts should have been handled above.");
539
540       case lldb::eConnectionStatusError:        // Check GetError() for details
541       case lldb::eConnectionStatusTimedOut:     // Request timed out
542       case lldb::eConnectionStatusEndOfFile:    // End-of-file encountered
543       case lldb::eConnectionStatusNoConnection: // No connection
544       case lldb::eConnectionStatusLostConnection: // Lost connection while
545                                                   // connected to a valid
546                                                   // connection
547         m_editor_status = EditorStatus::EndOfInput;
548         return 0;
549       }
550     }
551   }
552 }
553
554 const char *Editline::Prompt() {
555   if (m_color_prompts)
556     m_needs_prompt_repaint = true;
557   return m_current_prompt.c_str();
558 }
559
560 unsigned char Editline::BreakLineCommand(int ch) {
561   // Preserve any content beyond the cursor, truncate and save the current line
562   const LineInfoW *info = el_wline(m_editline);
563   auto current_line =
564       EditLineStringType(info->buffer, info->cursor - info->buffer);
565   auto new_line_fragment =
566       EditLineStringType(info->cursor, info->lastchar - info->cursor);
567   m_input_lines[m_current_line_index] = current_line;
568
569   // Ignore whitespace-only extra fragments when breaking a line
570   if (::IsOnlySpaces(new_line_fragment))
571     new_line_fragment = EditLineConstString("");
572
573   // Establish the new cursor position at the start of a line when inserting a
574   // line break
575   m_revert_cursor_index = 0;
576
577   // Don't perform automatic formatting when pasting
578   if (!IsInputPending(m_input_file)) {
579     // Apply smart indentation
580     if (m_fix_indentation_callback) {
581       StringList lines = GetInputAsStringList(m_current_line_index + 1);
582 #if LLDB_EDITLINE_USE_WCHAR
583       lines.AppendString(m_utf8conv.to_bytes(new_line_fragment));
584 #else
585       lines.AppendString(new_line_fragment);
586 #endif
587
588       int indent_correction = m_fix_indentation_callback(
589           this, lines, 0, m_fix_indentation_callback_baton);
590       new_line_fragment = FixIndentation(new_line_fragment, indent_correction);
591       m_revert_cursor_index = GetIndentation(new_line_fragment);
592     }
593   }
594
595   // Insert the new line and repaint everything from the split line on down
596   m_input_lines.insert(m_input_lines.begin() + m_current_line_index + 1,
597                        new_line_fragment);
598   MoveCursor(CursorLocation::EditingCursor, CursorLocation::EditingPrompt);
599   DisplayInput(m_current_line_index);
600
601   // Reposition the cursor to the right line and prepare to edit the new line
602   SetCurrentLine(m_current_line_index + 1);
603   MoveCursor(CursorLocation::BlockEnd, CursorLocation::EditingPrompt);
604   return CC_NEWLINE;
605 }
606
607 unsigned char Editline::EndOrAddLineCommand(int ch) {
608   // Don't perform end of input detection when pasting, always treat this as a
609   // line break
610   if (IsInputPending(m_input_file)) {
611     return BreakLineCommand(ch);
612   }
613
614   // Save any edits to this line
615   SaveEditedLine();
616
617   // If this is the end of the last line, consider whether to add a line instead
618   const LineInfoW *info = el_wline(m_editline);
619   if (m_current_line_index == m_input_lines.size() - 1 &&
620       info->cursor == info->lastchar) {
621     if (m_is_input_complete_callback) {
622       auto lines = GetInputAsStringList();
623       if (!m_is_input_complete_callback(this, lines,
624                                         m_is_input_complete_callback_baton)) {
625         return BreakLineCommand(ch);
626       }
627
628       // The completion test is allowed to change the input lines when complete
629       m_input_lines.clear();
630       for (unsigned index = 0; index < lines.GetSize(); index++) {
631 #if LLDB_EDITLINE_USE_WCHAR
632         m_input_lines.insert(m_input_lines.end(),
633                              m_utf8conv.from_bytes(lines[index]));
634 #else
635         m_input_lines.insert(m_input_lines.end(), lines[index]);
636 #endif
637       }
638     }
639   }
640   MoveCursor(CursorLocation::EditingCursor, CursorLocation::BlockEnd);
641   fprintf(m_output_file, "\n");
642   m_editor_status = EditorStatus::Complete;
643   return CC_NEWLINE;
644 }
645
646 unsigned char Editline::DeleteNextCharCommand(int ch) {
647   LineInfoW *info = const_cast<LineInfoW *>(el_wline(m_editline));
648
649   // Just delete the next character normally if possible
650   if (info->cursor < info->lastchar) {
651     info->cursor++;
652     el_deletestr(m_editline, 1);
653     return CC_REFRESH;
654   }
655
656   // Fail when at the end of the last line, except when ^D is pressed on
657   // the line is empty, in which case it is treated as EOF
658   if (m_current_line_index == m_input_lines.size() - 1) {
659     if (ch == 4 && info->buffer == info->lastchar) {
660       fprintf(m_output_file, "^D\n");
661       m_editor_status = EditorStatus::EndOfInput;
662       return CC_EOF;
663     }
664     return CC_ERROR;
665   }
666
667   // Prepare to combine this line with the one below
668   MoveCursor(CursorLocation::EditingCursor, CursorLocation::EditingPrompt);
669
670   // Insert the next line of text at the cursor and restore the cursor position
671   const EditLineCharType *cursor = info->cursor;
672   el_winsertstr(m_editline, m_input_lines[m_current_line_index + 1].c_str());
673   info->cursor = cursor;
674   SaveEditedLine();
675
676   // Delete the extra line
677   m_input_lines.erase(m_input_lines.begin() + m_current_line_index + 1);
678
679   // Clear and repaint from this line on down
680   DisplayInput(m_current_line_index);
681   MoveCursor(CursorLocation::BlockEnd, CursorLocation::EditingCursor);
682   return CC_REFRESH;
683 }
684
685 unsigned char Editline::DeletePreviousCharCommand(int ch) {
686   LineInfoW *info = const_cast<LineInfoW *>(el_wline(m_editline));
687
688   // Just delete the previous character normally when not at the start of a line
689   if (info->cursor > info->buffer) {
690     el_deletestr(m_editline, 1);
691     return CC_REFRESH;
692   }
693
694   // No prior line and no prior character?  Let the user know
695   if (m_current_line_index == 0)
696     return CC_ERROR;
697
698   // No prior character, but prior line?  Combine with the line above
699   SaveEditedLine();
700   SetCurrentLine(m_current_line_index - 1);
701   auto priorLine = m_input_lines[m_current_line_index];
702   m_input_lines.erase(m_input_lines.begin() + m_current_line_index);
703   m_input_lines[m_current_line_index] =
704       priorLine + m_input_lines[m_current_line_index];
705
706   // Repaint from the new line down
707   fprintf(m_output_file, ANSI_UP_N_ROWS ANSI_SET_COLUMN_N,
708           CountRowsForLine(priorLine), 1);
709   DisplayInput(m_current_line_index);
710
711   // Put the cursor back where libedit expects it to be before returning to
712   // editing
713   // by telling libedit about the newly inserted text
714   MoveCursor(CursorLocation::BlockEnd, CursorLocation::EditingPrompt);
715   el_winsertstr(m_editline, priorLine.c_str());
716   return CC_REDISPLAY;
717 }
718
719 unsigned char Editline::PreviousLineCommand(int ch) {
720   SaveEditedLine();
721
722   if (m_current_line_index == 0) {
723     return RecallHistory(true);
724   }
725
726   // Start from a known location
727   MoveCursor(CursorLocation::EditingCursor, CursorLocation::EditingPrompt);
728
729   // Treat moving up from a blank last line as a deletion of that line
730   if (m_current_line_index == m_input_lines.size() - 1 && IsOnlySpaces()) {
731     m_input_lines.erase(m_input_lines.begin() + m_current_line_index);
732     fprintf(m_output_file, ANSI_CLEAR_BELOW);
733   }
734
735   SetCurrentLine(m_current_line_index - 1);
736   fprintf(m_output_file, ANSI_UP_N_ROWS ANSI_SET_COLUMN_N,
737           CountRowsForLine(m_input_lines[m_current_line_index]), 1);
738   return CC_NEWLINE;
739 }
740
741 unsigned char Editline::NextLineCommand(int ch) {
742   SaveEditedLine();
743
744   // Handle attempts to move down from the last line
745   if (m_current_line_index == m_input_lines.size() - 1) {
746     // Don't add an extra line if the existing last line is blank, move through
747     // history instead
748     if (IsOnlySpaces()) {
749       return RecallHistory(false);
750     }
751
752     // Determine indentation for the new line
753     int indentation = 0;
754     if (m_fix_indentation_callback) {
755       StringList lines = GetInputAsStringList();
756       lines.AppendString("");
757       indentation = m_fix_indentation_callback(
758           this, lines, 0, m_fix_indentation_callback_baton);
759     }
760     m_input_lines.insert(
761         m_input_lines.end(),
762         EditLineStringType(indentation, EditLineCharType(' ')));
763   }
764
765   // Move down past the current line using newlines to force scrolling if needed
766   SetCurrentLine(m_current_line_index + 1);
767   const LineInfoW *info = el_wline(m_editline);
768   int cursor_position = (int)((info->cursor - info->buffer) + GetPromptWidth());
769   int cursor_row = cursor_position / m_terminal_width;
770   for (int line_count = 0; line_count < m_current_line_rows - cursor_row;
771        line_count++) {
772     fprintf(m_output_file, "\n");
773   }
774   return CC_NEWLINE;
775 }
776
777 unsigned char Editline::PreviousHistoryCommand(int ch) {
778   SaveEditedLine();
779
780   return RecallHistory(true);
781 }
782
783 unsigned char Editline::NextHistoryCommand(int ch) {
784   SaveEditedLine();
785
786   return RecallHistory(false);
787 }
788
789 unsigned char Editline::FixIndentationCommand(int ch) {
790   if (!m_fix_indentation_callback)
791     return CC_NORM;
792
793   // Insert the character typed before proceeding
794   EditLineCharType inserted[] = {(EditLineCharType)ch, 0};
795   el_winsertstr(m_editline, inserted);
796   LineInfoW *info = const_cast<LineInfoW *>(el_wline(m_editline));
797   int cursor_position = info->cursor - info->buffer;
798
799   // Save the edits and determine the correct indentation level
800   SaveEditedLine();
801   StringList lines = GetInputAsStringList(m_current_line_index + 1);
802   int indent_correction = m_fix_indentation_callback(
803       this, lines, cursor_position, m_fix_indentation_callback_baton);
804
805   // If it is already correct no special work is needed
806   if (indent_correction == 0)
807     return CC_REFRESH;
808
809   // Change the indentation level of the line
810   std::string currentLine = lines.GetStringAtIndex(m_current_line_index);
811   if (indent_correction > 0) {
812     currentLine = currentLine.insert(0, indent_correction, ' ');
813   } else {
814     currentLine = currentLine.erase(0, -indent_correction);
815   }
816 #if LLDB_EDITLINE_USE_WCHAR
817   m_input_lines[m_current_line_index] = m_utf8conv.from_bytes(currentLine);
818 #else
819   m_input_lines[m_current_line_index] = currentLine;
820 #endif
821
822   // Update the display to reflect the change
823   MoveCursor(CursorLocation::EditingCursor, CursorLocation::EditingPrompt);
824   DisplayInput(m_current_line_index);
825
826   // Reposition the cursor back on the original line and prepare to restart
827   // editing
828   // with a new cursor position
829   SetCurrentLine(m_current_line_index);
830   MoveCursor(CursorLocation::BlockEnd, CursorLocation::EditingPrompt);
831   m_revert_cursor_index = cursor_position + indent_correction;
832   return CC_NEWLINE;
833 }
834
835 unsigned char Editline::RevertLineCommand(int ch) {
836   el_winsertstr(m_editline, m_input_lines[m_current_line_index].c_str());
837   if (m_revert_cursor_index >= 0) {
838     LineInfoW *info = const_cast<LineInfoW *>(el_wline(m_editline));
839     info->cursor = info->buffer + m_revert_cursor_index;
840     if (info->cursor > info->lastchar) {
841       info->cursor = info->lastchar;
842     }
843     m_revert_cursor_index = -1;
844   }
845   return CC_REFRESH;
846 }
847
848 unsigned char Editline::BufferStartCommand(int ch) {
849   SaveEditedLine();
850   MoveCursor(CursorLocation::EditingCursor, CursorLocation::BlockStart);
851   SetCurrentLine(0);
852   m_revert_cursor_index = 0;
853   return CC_NEWLINE;
854 }
855
856 unsigned char Editline::BufferEndCommand(int ch) {
857   SaveEditedLine();
858   MoveCursor(CursorLocation::EditingCursor, CursorLocation::BlockEnd);
859   SetCurrentLine((int)m_input_lines.size() - 1);
860   MoveCursor(CursorLocation::BlockEnd, CursorLocation::EditingPrompt);
861   return CC_NEWLINE;
862 }
863
864 unsigned char Editline::TabCommand(int ch) {
865   if (m_completion_callback == nullptr)
866     return CC_ERROR;
867
868   const LineInfo *line_info = el_line(m_editline);
869   StringList completions;
870   int page_size = 40;
871
872   const int num_completions = m_completion_callback(
873       line_info->buffer, line_info->cursor, line_info->lastchar,
874       0,  // Don't skip any matches (start at match zero)
875       -1, // Get all the matches
876       completions, m_completion_callback_baton);
877
878   if (num_completions == 0)
879     return CC_ERROR;
880   //    if (num_completions == -1)
881   //    {
882   //        el_insertstr (m_editline, m_completion_key);
883   //        return CC_REDISPLAY;
884   //    }
885   //    else
886   if (num_completions == -2) {
887     // Replace the entire line with the first string...
888     el_deletestr(m_editline, line_info->cursor - line_info->buffer);
889     el_insertstr(m_editline, completions.GetStringAtIndex(0));
890     return CC_REDISPLAY;
891   }
892
893   // If we get a longer match display that first.
894   const char *completion_str = completions.GetStringAtIndex(0);
895   if (completion_str != nullptr && *completion_str != '\0') {
896     el_insertstr(m_editline, completion_str);
897     return CC_REDISPLAY;
898   }
899
900   if (num_completions > 1) {
901     int num_elements = num_completions + 1;
902     fprintf(m_output_file, "\n" ANSI_CLEAR_BELOW "Available completions:");
903     if (num_completions < page_size) {
904       for (int i = 1; i < num_elements; i++) {
905         completion_str = completions.GetStringAtIndex(i);
906         fprintf(m_output_file, "\n\t%s", completion_str);
907       }
908       fprintf(m_output_file, "\n");
909     } else {
910       int cur_pos = 1;
911       char reply;
912       int got_char;
913       while (cur_pos < num_elements) {
914         int endpoint = cur_pos + page_size;
915         if (endpoint > num_elements)
916           endpoint = num_elements;
917         for (; cur_pos < endpoint; cur_pos++) {
918           completion_str = completions.GetStringAtIndex(cur_pos);
919           fprintf(m_output_file, "\n\t%s", completion_str);
920         }
921
922         if (cur_pos >= num_elements) {
923           fprintf(m_output_file, "\n");
924           break;
925         }
926
927         fprintf(m_output_file, "\nMore (Y/n/a): ");
928         reply = 'n';
929         got_char = el_getc(m_editline, &reply);
930         if (got_char == -1 || reply == 'n')
931           break;
932         if (reply == 'a')
933           page_size = num_elements - cur_pos;
934       }
935     }
936     DisplayInput();
937     MoveCursor(CursorLocation::BlockEnd, CursorLocation::EditingCursor);
938   }
939   return CC_REDISPLAY;
940 }
941
942 void Editline::ConfigureEditor(bool multiline) {
943   if (m_editline && m_multiline_enabled == multiline)
944     return;
945   m_multiline_enabled = multiline;
946
947   if (m_editline) {
948     // Disable edit mode to stop the terminal from flushing all input
949     // during the call to el_end() since we expect to have multiple editline
950     // instances in this program.
951     el_set(m_editline, EL_EDITMODE, 0);
952     el_end(m_editline);
953   }
954
955   m_editline =
956       el_init(m_editor_name.c_str(), m_input_file, m_output_file, m_error_file);
957   TerminalSizeChanged();
958
959   if (m_history_sp && m_history_sp->IsValid()) {
960     m_history_sp->Load();
961     el_wset(m_editline, EL_HIST, history, m_history_sp->GetHistoryPtr());
962   }
963   el_set(m_editline, EL_CLIENTDATA, this);
964   el_set(m_editline, EL_SIGNAL, 0);
965   el_set(m_editline, EL_EDITOR, "emacs");
966   el_set(m_editline, EL_PROMPT,
967          (EditlinePromptCallbackType)([](EditLine *editline) {
968            return Editline::InstanceFor(editline)->Prompt();
969          }));
970
971   el_wset(m_editline, EL_GETCFN, (EditlineGetCharCallbackType)([](
972                                      EditLine *editline, EditLineCharType *c) {
973             return Editline::InstanceFor(editline)->GetCharacter(c);
974           }));
975
976   // Commands used for multiline support, registered whether or not they're used
977   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-break-line"),
978           EditLineConstString("Insert a line break"),
979           (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
980             return Editline::InstanceFor(editline)->BreakLineCommand(ch);
981           }));
982   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-end-or-add-line"),
983           EditLineConstString("End editing or continue when incomplete"),
984           (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
985             return Editline::InstanceFor(editline)->EndOrAddLineCommand(ch);
986           }));
987   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-delete-next-char"),
988           EditLineConstString("Delete next character"),
989           (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
990             return Editline::InstanceFor(editline)->DeleteNextCharCommand(ch);
991           }));
992   el_wset(
993       m_editline, EL_ADDFN, EditLineConstString("lldb-delete-previous-char"),
994       EditLineConstString("Delete previous character"),
995       (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
996         return Editline::InstanceFor(editline)->DeletePreviousCharCommand(ch);
997       }));
998   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-previous-line"),
999           EditLineConstString("Move to previous line"),
1000           (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
1001             return Editline::InstanceFor(editline)->PreviousLineCommand(ch);
1002           }));
1003   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-next-line"),
1004           EditLineConstString("Move to next line"),
1005           (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
1006             return Editline::InstanceFor(editline)->NextLineCommand(ch);
1007           }));
1008   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-previous-history"),
1009           EditLineConstString("Move to previous history"),
1010           (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
1011             return Editline::InstanceFor(editline)->PreviousHistoryCommand(ch);
1012           }));
1013   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-next-history"),
1014           EditLineConstString("Move to next history"),
1015           (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
1016             return Editline::InstanceFor(editline)->NextHistoryCommand(ch);
1017           }));
1018   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-buffer-start"),
1019           EditLineConstString("Move to start of buffer"),
1020           (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
1021             return Editline::InstanceFor(editline)->BufferStartCommand(ch);
1022           }));
1023   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-buffer-end"),
1024           EditLineConstString("Move to end of buffer"),
1025           (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
1026             return Editline::InstanceFor(editline)->BufferEndCommand(ch);
1027           }));
1028   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-fix-indentation"),
1029           EditLineConstString("Fix line indentation"),
1030           (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
1031             return Editline::InstanceFor(editline)->FixIndentationCommand(ch);
1032           }));
1033
1034   // Register the complete callback under two names for compatibility with older
1035   // clients using
1036   // custom .editrc files (largely because libedit has a bad bug where if you
1037   // have a bind command
1038   // that tries to bind to a function name that doesn't exist, it can corrupt
1039   // the heap and
1040   // crash your process later.)
1041   EditlineCommandCallbackType complete_callback = [](EditLine *editline,
1042                                                      int ch) {
1043     return Editline::InstanceFor(editline)->TabCommand(ch);
1044   };
1045   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-complete"),
1046           EditLineConstString("Invoke completion"), complete_callback);
1047   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb_complete"),
1048           EditLineConstString("Invoke completion"), complete_callback);
1049
1050   // General bindings we don't mind being overridden
1051   if (!multiline) {
1052     el_set(m_editline, EL_BIND, "^r", "em-inc-search-prev",
1053            NULL); // Cycle through backwards search, entering string
1054   }
1055   el_set(m_editline, EL_BIND, "^w", "ed-delete-prev-word",
1056          NULL); // Delete previous word, behave like bash in emacs mode
1057   el_set(m_editline, EL_BIND, "\t", "lldb-complete",
1058          NULL); // Bind TAB to auto complete
1059
1060   // Allow user-specific customization prior to registering bindings we
1061   // absolutely require
1062   el_source(m_editline, NULL);
1063
1064   // Register an internal binding that external developers shouldn't use
1065   el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-revert-line"),
1066           EditLineConstString("Revert line to saved state"),
1067           (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
1068             return Editline::InstanceFor(editline)->RevertLineCommand(ch);
1069           }));
1070
1071   // Register keys that perform auto-indent correction
1072   if (m_fix_indentation_callback && m_fix_indentation_callback_chars) {
1073     char bind_key[2] = {0, 0};
1074     const char *indent_chars = m_fix_indentation_callback_chars;
1075     while (*indent_chars) {
1076       bind_key[0] = *indent_chars;
1077       el_set(m_editline, EL_BIND, bind_key, "lldb-fix-indentation", NULL);
1078       ++indent_chars;
1079     }
1080   }
1081
1082   // Multi-line editor bindings
1083   if (multiline) {
1084     el_set(m_editline, EL_BIND, "\n", "lldb-end-or-add-line", NULL);
1085     el_set(m_editline, EL_BIND, "\r", "lldb-end-or-add-line", NULL);
1086     el_set(m_editline, EL_BIND, ESCAPE "\n", "lldb-break-line", NULL);
1087     el_set(m_editline, EL_BIND, ESCAPE "\r", "lldb-break-line", NULL);
1088     el_set(m_editline, EL_BIND, "^p", "lldb-previous-line", NULL);
1089     el_set(m_editline, EL_BIND, "^n", "lldb-next-line", NULL);
1090     el_set(m_editline, EL_BIND, "^?", "lldb-delete-previous-char", NULL);
1091     el_set(m_editline, EL_BIND, "^d", "lldb-delete-next-char", NULL);
1092     el_set(m_editline, EL_BIND, ESCAPE "[3~", "lldb-delete-next-char", NULL);
1093     el_set(m_editline, EL_BIND, ESCAPE "[\\^", "lldb-revert-line", NULL);
1094
1095     // Editor-specific bindings
1096     if (IsEmacs()) {
1097       el_set(m_editline, EL_BIND, ESCAPE "<", "lldb-buffer-start", NULL);
1098       el_set(m_editline, EL_BIND, ESCAPE ">", "lldb-buffer-end", NULL);
1099       el_set(m_editline, EL_BIND, ESCAPE "[A", "lldb-previous-line", NULL);
1100       el_set(m_editline, EL_BIND, ESCAPE "[B", "lldb-next-line", NULL);
1101       el_set(m_editline, EL_BIND, ESCAPE ESCAPE "[A", "lldb-previous-history",
1102              NULL);
1103       el_set(m_editline, EL_BIND, ESCAPE ESCAPE "[B", "lldb-next-history",
1104              NULL);
1105       el_set(m_editline, EL_BIND, ESCAPE "[1;3A", "lldb-previous-history",
1106              NULL);
1107       el_set(m_editline, EL_BIND, ESCAPE "[1;3B", "lldb-next-history", NULL);
1108     } else {
1109       el_set(m_editline, EL_BIND, "^H", "lldb-delete-previous-char", NULL);
1110
1111       el_set(m_editline, EL_BIND, "-a", ESCAPE "[A", "lldb-previous-line",
1112              NULL);
1113       el_set(m_editline, EL_BIND, "-a", ESCAPE "[B", "lldb-next-line", NULL);
1114       el_set(m_editline, EL_BIND, "-a", "x", "lldb-delete-next-char", NULL);
1115       el_set(m_editline, EL_BIND, "-a", "^H", "lldb-delete-previous-char",
1116              NULL);
1117       el_set(m_editline, EL_BIND, "-a", "^?", "lldb-delete-previous-char",
1118              NULL);
1119
1120       // Escape is absorbed exiting edit mode, so re-register important
1121       // sequences
1122       // without the prefix
1123       el_set(m_editline, EL_BIND, "-a", "[A", "lldb-previous-line", NULL);
1124       el_set(m_editline, EL_BIND, "-a", "[B", "lldb-next-line", NULL);
1125       el_set(m_editline, EL_BIND, "-a", "[\\^", "lldb-revert-line", NULL);
1126     }
1127   }
1128 }
1129
1130 //------------------------------------------------------------------
1131 // Editline public methods
1132 //------------------------------------------------------------------
1133
1134 Editline *Editline::InstanceFor(EditLine *editline) {
1135   Editline *editor;
1136   el_get(editline, EL_CLIENTDATA, &editor);
1137   return editor;
1138 }
1139
1140 Editline::Editline(const char *editline_name, FILE *input_file,
1141                    FILE *output_file, FILE *error_file, bool color_prompts)
1142     : m_editor_status(EditorStatus::Complete), m_color_prompts(color_prompts),
1143       m_input_file(input_file), m_output_file(output_file),
1144       m_error_file(error_file), m_input_connection(fileno(input_file), false) {
1145   // Get a shared history instance
1146   m_editor_name = (editline_name == nullptr) ? "lldb-tmp" : editline_name;
1147   m_history_sp = EditlineHistory::GetHistory(m_editor_name);
1148
1149 #ifdef USE_SETUPTERM_WORKAROUND
1150   if (m_output_file) {
1151     const int term_fd = fileno(m_output_file);
1152     if (term_fd != -1) {
1153       static std::mutex *g_init_terminal_fds_mutex_ptr = nullptr;
1154       static std::set<int> *g_init_terminal_fds_ptr = nullptr;
1155       static llvm::once_flag g_once_flag;
1156       llvm::call_once(g_once_flag, [&]() {
1157         g_init_terminal_fds_mutex_ptr =
1158             new std::mutex(); // NOTE: Leak to avoid C++ destructor chain issues
1159         g_init_terminal_fds_ptr = new std::set<int>(); // NOTE: Leak to avoid
1160                                                        // C++ destructor chain
1161                                                        // issues
1162       });
1163
1164       // We must make sure to initialize the terminal a given file descriptor
1165       // only once. If we do this multiple times, we start leaking memory.
1166       std::lock_guard<std::mutex> guard(*g_init_terminal_fds_mutex_ptr);
1167       if (g_init_terminal_fds_ptr->find(term_fd) ==
1168           g_init_terminal_fds_ptr->end()) {
1169         g_init_terminal_fds_ptr->insert(term_fd);
1170         setupterm((char *)0, term_fd, (int *)0);
1171       }
1172     }
1173   }
1174 #endif
1175 }
1176
1177 Editline::~Editline() {
1178   if (m_editline) {
1179     // Disable edit mode to stop the terminal from flushing all input
1180     // during the call to el_end() since we expect to have multiple editline
1181     // instances in this program.
1182     el_set(m_editline, EL_EDITMODE, 0);
1183     el_end(m_editline);
1184     m_editline = nullptr;
1185   }
1186
1187   // EditlineHistory objects are sometimes shared between multiple
1188   // Editline instances with the same program name. So just release
1189   // our shared pointer and if we are the last owner, it will save the
1190   // history to the history save file automatically.
1191   m_history_sp.reset();
1192 }
1193
1194 void Editline::SetPrompt(const char *prompt) {
1195   m_set_prompt = prompt == nullptr ? "" : prompt;
1196 }
1197
1198 void Editline::SetContinuationPrompt(const char *continuation_prompt) {
1199   m_set_continuation_prompt =
1200       continuation_prompt == nullptr ? "" : continuation_prompt;
1201 }
1202
1203 void Editline::TerminalSizeChanged() {
1204   if (m_editline != nullptr) {
1205     el_resize(m_editline);
1206     int columns;
1207     // Despite the man page claiming non-zero indicates success, it's actually
1208     // zero
1209     if (el_get(m_editline, EL_GETTC, "co", &columns) == 0) {
1210       m_terminal_width = columns;
1211       if (m_current_line_rows != -1) {
1212         const LineInfoW *info = el_wline(m_editline);
1213         int lineLength =
1214             (int)((info->lastchar - info->buffer) + GetPromptWidth());
1215         m_current_line_rows = (lineLength / columns) + 1;
1216       }
1217     } else {
1218       m_terminal_width = INT_MAX;
1219       m_current_line_rows = 1;
1220     }
1221   }
1222 }
1223
1224 const char *Editline::GetPrompt() { return m_set_prompt.c_str(); }
1225
1226 uint32_t Editline::GetCurrentLine() { return m_current_line_index; }
1227
1228 bool Editline::Interrupt() {
1229   bool result = true;
1230   std::lock_guard<std::mutex> guard(m_output_mutex);
1231   if (m_editor_status == EditorStatus::Editing) {
1232     fprintf(m_output_file, "^C\n");
1233     result = m_input_connection.InterruptRead();
1234   }
1235   m_editor_status = EditorStatus::Interrupted;
1236   return result;
1237 }
1238
1239 bool Editline::Cancel() {
1240   bool result = true;
1241   std::lock_guard<std::mutex> guard(m_output_mutex);
1242   if (m_editor_status == EditorStatus::Editing) {
1243     MoveCursor(CursorLocation::EditingCursor, CursorLocation::BlockStart);
1244     fprintf(m_output_file, ANSI_CLEAR_BELOW);
1245     result = m_input_connection.InterruptRead();
1246   }
1247   m_editor_status = EditorStatus::Interrupted;
1248   return result;
1249 }
1250
1251 void Editline::SetAutoCompleteCallback(CompleteCallbackType callback,
1252                                        void *baton) {
1253   m_completion_callback = callback;
1254   m_completion_callback_baton = baton;
1255 }
1256
1257 void Editline::SetIsInputCompleteCallback(IsInputCompleteCallbackType callback,
1258                                           void *baton) {
1259   m_is_input_complete_callback = callback;
1260   m_is_input_complete_callback_baton = baton;
1261 }
1262
1263 bool Editline::SetFixIndentationCallback(FixIndentationCallbackType callback,
1264                                          void *baton,
1265                                          const char *indent_chars) {
1266   m_fix_indentation_callback = callback;
1267   m_fix_indentation_callback_baton = baton;
1268   m_fix_indentation_callback_chars = indent_chars;
1269   return false;
1270 }
1271
1272 bool Editline::GetLine(std::string &line, bool &interrupted) {
1273   ConfigureEditor(false);
1274   m_input_lines = std::vector<EditLineStringType>();
1275   m_input_lines.insert(m_input_lines.begin(), EditLineConstString(""));
1276
1277   std::lock_guard<std::mutex> guard(m_output_mutex);
1278
1279   lldbassert(m_editor_status != EditorStatus::Editing);
1280   if (m_editor_status == EditorStatus::Interrupted) {
1281     m_editor_status = EditorStatus::Complete;
1282     interrupted = true;
1283     return true;
1284   }
1285
1286   SetCurrentLine(0);
1287   m_in_history = false;
1288   m_editor_status = EditorStatus::Editing;
1289   m_revert_cursor_index = -1;
1290
1291   int count;
1292   auto input = el_wgets(m_editline, &count);
1293
1294   interrupted = m_editor_status == EditorStatus::Interrupted;
1295   if (!interrupted) {
1296     if (input == nullptr) {
1297       fprintf(m_output_file, "\n");
1298       m_editor_status = EditorStatus::EndOfInput;
1299     } else {
1300       m_history_sp->Enter(input);
1301 #if LLDB_EDITLINE_USE_WCHAR
1302       line = m_utf8conv.to_bytes(SplitLines(input)[0]);
1303 #else
1304       line = SplitLines(input)[0];
1305 #endif
1306       m_editor_status = EditorStatus::Complete;
1307     }
1308   }
1309   return m_editor_status != EditorStatus::EndOfInput;
1310 }
1311
1312 bool Editline::GetLines(int first_line_number, StringList &lines,
1313                         bool &interrupted) {
1314   ConfigureEditor(true);
1315
1316   // Print the initial input lines, then move the cursor back up to the start of
1317   // input
1318   SetBaseLineNumber(first_line_number);
1319   m_input_lines = std::vector<EditLineStringType>();
1320   m_input_lines.insert(m_input_lines.begin(), EditLineConstString(""));
1321
1322   std::lock_guard<std::mutex> guard(m_output_mutex);
1323   // Begin the line editing loop
1324   DisplayInput();
1325   SetCurrentLine(0);
1326   MoveCursor(CursorLocation::BlockEnd, CursorLocation::BlockStart);
1327   m_editor_status = EditorStatus::Editing;
1328   m_in_history = false;
1329
1330   m_revert_cursor_index = -1;
1331   while (m_editor_status == EditorStatus::Editing) {
1332     int count;
1333     m_current_line_rows = -1;
1334     el_wpush(m_editline, EditLineConstString(
1335                              "\x1b[^")); // Revert to the existing line content
1336     el_wgets(m_editline, &count);
1337   }
1338
1339   interrupted = m_editor_status == EditorStatus::Interrupted;
1340   if (!interrupted) {
1341     // Save the completed entry in history before returning
1342     m_history_sp->Enter(CombineLines(m_input_lines).c_str());
1343
1344     lines = GetInputAsStringList();
1345   }
1346   return m_editor_status != EditorStatus::EndOfInput;
1347 }
1348
1349 void Editline::PrintAsync(Stream *stream, const char *s, size_t len) {
1350   std::lock_guard<std::mutex> guard(m_output_mutex);
1351   if (m_editor_status == EditorStatus::Editing) {
1352     MoveCursor(CursorLocation::EditingCursor, CursorLocation::BlockStart);
1353     fprintf(m_output_file, ANSI_CLEAR_BELOW);
1354   }
1355   stream->Write(s, len);
1356   stream->Flush();
1357   if (m_editor_status == EditorStatus::Editing) {
1358     DisplayInput();
1359     MoveCursor(CursorLocation::BlockEnd, CursorLocation::EditingCursor);
1360   }
1361 }
1362
1363 bool Editline::CompleteCharacter(char ch, EditLineCharType &out) {
1364 #if !LLDB_EDITLINE_USE_WCHAR
1365   if (ch == (char)EOF)
1366     return false;
1367
1368   out = ch;
1369   return true;
1370 #else
1371   std::codecvt_utf8<wchar_t> cvt;
1372   llvm::SmallString<4> input;
1373   for (;;) {
1374     const char *from_next;
1375     wchar_t *to_next;
1376     std::mbstate_t state = std::mbstate_t();
1377     input.push_back(ch);
1378     switch (cvt.in(state, input.begin(), input.end(), from_next, &out, &out + 1,
1379                    to_next)) {
1380     case std::codecvt_base::ok:
1381       return out != WEOF;
1382
1383     case std::codecvt_base::error:
1384     case std::codecvt_base::noconv:
1385       return false;
1386
1387     case std::codecvt_base::partial:
1388       lldb::ConnectionStatus status;
1389       size_t read_count = m_input_connection.Read(
1390           &ch, 1, std::chrono::seconds(0), status, nullptr);
1391       if (read_count == 0)
1392         return false;
1393       break;
1394     }
1395   }
1396 #endif
1397 }