1 //===-- Editline.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "lldb/Host/Editline.h"
13 #include "lldb/Core/Error.h"
14 #include "lldb/Core/StreamString.h"
15 #include "lldb/Core/StringList.h"
16 #include "lldb/Host/Host.h"
21 using namespace lldb_private;
23 namespace lldb_private {
24 typedef std::weak_ptr<EditlineHistory> EditlineHistoryWP;
27 // EditlineHistory objects are sometimes shared between multiple
28 // Editline instances with the same program name. This class allows
29 // multiple editline instances to
35 // Use static GetHistory() function to get a EditlineHistorySP to one of these objects
36 EditlineHistory(const std::string &prefix, uint32_t size, bool unique_entries) :
42 m_history = ::history_init();
43 ::history (m_history, &m_event, H_SETSIZE, size);
45 ::history (m_history, &m_event, H_SETUNIQUE, 1);
51 if (m_path.empty() && m_history && !m_prefix.empty())
53 char history_path[PATH_MAX];
54 ::snprintf (history_path, sizeof(history_path), "~/.%s-history", m_prefix.c_str());
55 m_path = std::move(FileSpec(history_path, true).GetPath());
59 return m_path.c_str();
70 ::history_end (m_history);
75 static EditlineHistorySP
76 GetHistory (const std::string &prefix)
78 typedef std::map<std::string, EditlineHistoryWP> WeakHistoryMap;
79 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
80 static WeakHistoryMap g_weak_map;
81 Mutex::Locker locker (g_mutex);
82 WeakHistoryMap::const_iterator pos = g_weak_map.find (prefix);
83 EditlineHistorySP history_sp;
84 if (pos != g_weak_map.end())
86 history_sp = pos->second.lock();
89 g_weak_map.erase(pos);
91 history_sp.reset(new EditlineHistory(prefix, 800, true));
92 g_weak_map[prefix] = history_sp;
98 return m_history != NULL;
108 Enter (const char *line_cstr)
111 ::history (m_history, &m_event, H_ENTER, line_cstr);
119 const char *path = GetHistoryFilePath();
122 ::history (m_history, &m_event, H_LOAD, path);
134 const char *path = GetHistoryFilePath();
137 ::history (m_history, &m_event, H_SAVE, path);
145 ::History *m_history; // The history object
146 ::HistEvent m_event;// The history event needed to contain all history events
147 std::string m_prefix; // The prefix name (usually the editline program name) to use when loading/saving history
148 std::string m_path; // Path to the history file
153 static const char k_prompt_escape_char = '\1';
155 Editline::Editline (const char *prog, // prog can't be NULL
156 const char *prompt, // can be NULL for no prompt
157 bool configure_for_multiline,
165 m_getting_char (false),
166 m_completion_callback (NULL),
167 m_completion_callback_baton (NULL),
168 m_line_complete_callback (NULL),
169 m_line_complete_callback_baton (NULL),
170 m_lines_command (Command::None),
172 m_lines_curr_line (0),
173 m_lines_max_line (0),
174 m_file (fileno(fin), false),
175 m_prompt_with_line_numbers (false),
176 m_getting_line (false),
178 m_interrupted (false)
182 m_editline = ::el_init(prog, fin, fout, ferr);
184 // Get a shared history instance
185 m_history_sp = EditlineHistory::GetHistory(prog);
189 m_editline = ::el_init("lldb-tmp", fin, fout, ferr);
192 if (prompt && prompt[0])
195 //::el_set (m_editline, EL_BIND, "^[[A", NULL); // Print binding for up arrow key
196 //::el_set (m_editline, EL_BIND, "^[[B", NULL); // Print binding for up down key
199 ::el_set (m_editline, EL_CLIENTDATA, this);
201 // only defined for newer versions of editline
203 ::el_set (m_editline, EL_PROMPT_ESC, GetPromptCallback, k_prompt_escape_char);
205 // fall back on old prompt setting code
206 ::el_set (m_editline, EL_PROMPT, GetPromptCallback);
208 ::el_set (m_editline, EL_EDITOR, "emacs");
209 if (m_history_sp && m_history_sp->IsValid())
211 ::el_set (m_editline, EL_HIST, history, m_history_sp->GetHistoryPtr());
213 ::el_set (m_editline, EL_ADDFN, "lldb-complete", "Editline completion function", Editline::CallbackComplete);
214 // Keep old "lldb_complete" mapping for older clients that used this in their .editrc. editline also
215 // has a bad bug where if you have a bind command that tries to bind to a function name that doesn't
216 // exist, it will corrupt the heap and probably crash your process later.
217 ::el_set (m_editline, EL_ADDFN, "lldb_complete", "Editline completion function", Editline::CallbackComplete);
218 ::el_set (m_editline, EL_ADDFN, "lldb-edit-prev-line", "Editline edit prev line", Editline::CallbackEditPrevLine);
219 ::el_set (m_editline, EL_ADDFN, "lldb-edit-next-line", "Editline edit next line", Editline::CallbackEditNextLine);
221 ::el_set (m_editline, EL_BIND, "^r", "em-inc-search-prev", NULL); // Cycle through backwards search, entering string
222 ::el_set (m_editline, EL_BIND, "^w", "ed-delete-prev-word", NULL); // Delete previous word, behave like bash does.
223 ::el_set (m_editline, EL_BIND, "\033[3~", "ed-delete-next-char", NULL); // Fix the delete key.
224 ::el_set (m_editline, EL_BIND, "\t", "lldb-complete", NULL); // Bind TAB to be auto complete
226 if (configure_for_multiline)
228 // Use escape sequences for control characters due to bugs in editline
229 // where "-k up" and "-k down" don't always work.
230 ::el_set (m_editline, EL_BIND, "^[[A", "lldb-edit-prev-line", NULL); // Map up arrow
231 ::el_set (m_editline, EL_BIND, "^[[B", "lldb-edit-next-line", NULL); // Map down arrow
232 // Bindings for next/prev history
233 ::el_set (m_editline, EL_BIND, "^P", "ed-prev-history", NULL); // Map up arrow
234 ::el_set (m_editline, EL_BIND, "^N", "ed-next-history", NULL); // Map down arrow
238 // Use escape sequences for control characters due to bugs in editline
239 // where "-k up" and "-k down" don't always work.
240 ::el_set (m_editline, EL_BIND, "^[[A", "ed-prev-history", NULL); // Map up arrow
241 ::el_set (m_editline, EL_BIND, "^[[B", "ed-next-history", NULL); // Map down arrow
244 // Source $PWD/.editrc then $HOME/.editrc
245 ::el_source (m_editline, NULL);
247 // Always read through our callback function so we don't read
248 // stuff we aren't supposed to. This also stops the extra echoing
249 // that can happen when you have more input than editline can handle
251 SetGetCharCallback(GetCharFromInputFileCallback);
256 Editline::~Editline()
258 // EditlineHistory objects are sometimes shared between multiple
259 // Editline instances with the same program name. So just release
260 // our shared pointer and if we are the last owner, it will save the
261 // history to the history save file automatically.
262 m_history_sp.reset();
264 // Disable edit mode to stop the terminal from flushing all input
265 // during the call to el_end() since we expect to have multiple editline
266 // instances in this program.
267 ::el_set (m_editline, EL_EDITMODE, 0);
269 ::el_end(m_editline);
274 Editline::SetGetCharCallback (GetCharCallbackType callback)
276 ::el_set (m_editline, EL_GETCFN, callback);
280 Editline::LoadHistory ()
283 return m_history_sp->Load();
288 Editline::SaveHistory ()
291 return m_history_sp->Save();
297 Editline::PrivateGetLine(std::string &line)
302 error.SetErrorString("interrupted");
307 if (m_editline != NULL)
310 // Call el_gets to prompt the user and read the user's input.
311 const char *line_cstr = ::el_gets (m_editline, &line_len);
313 static int save_errno = (line_len < 0) ? errno : 0;
317 error.SetError(save_errno, eErrorTypePOSIX);
321 // Decrement the length so we don't have newline characters in "line" for when
322 // we assign the cstr into the std::string
323 llvm::StringRef line_ref (line_cstr);
324 line_ref = line_ref.rtrim("\n\r");
326 if (!line_ref.empty() && !m_interrupted)
328 // We didn't strip the newlines, we just adjusted the length, and
329 // we want to add the history item with the newlines
331 m_history_sp->Enter(line_cstr);
333 // Copy the part of the c string that we want (removing the newline chars)
334 line = std::move(line_ref.str());
340 error.SetErrorString("the EditLine instance has been deleted");
347 Editline::GetLine(std::string &line, bool &interrupted)
353 // Set arrow key bindings for up and down arrows for single line
354 // mode where up and down arrows do prev/next history
355 m_interrupted = false;
361 error.SetErrorString("already getting a line");
364 if (m_lines_curr_line > 0)
366 error.SetErrorString("already getting lines");
369 m_getting_line = true;
370 error = PrivateGetLine(line);
371 m_getting_line = false;
374 interrupted = m_interrupted;
376 if (m_got_eof && line.empty())
378 // Only set the error if we didn't get an error back from PrivateGetLine()
380 error.SetErrorString("end of file");
387 Editline::Push (const char *bytes, size_t len)
391 // Must NULL terminate the string for el_push() so we stick it
392 // into a std::string first
393 ::el_push(m_editline,
394 const_cast<char*>(std::string (bytes, len).c_str()));
402 Editline::GetLines(const std::string &end_line, StringList &lines, bool &interrupted)
408 error.SetErrorString("already getting a line");
411 if (m_lines_curr_line > 0)
413 error.SetErrorString("already getting lines");
417 // Set arrow key bindings for up and down arrows for multiple line
418 // mode where up and down arrows do edit prev/next line
419 m_interrupted = false;
421 LineStatus line_status = LineStatus::Success;
425 FILE *out_file = GetOutputFile();
426 FILE *err_file = GetErrorFile();
427 m_lines_curr_line = 1;
428 while (line_status != LineStatus::Done)
430 const uint32_t line_idx = m_lines_curr_line-1;
431 if (line_idx >= lines.GetSize())
432 lines.SetSize(m_lines_curr_line);
433 m_lines_max_line = lines.GetSize();
434 m_lines_command = Command::None;
435 assert(line_idx < m_lines_max_line);
436 std::string &line = lines[line_idx];
437 error = PrivateGetLine(line);
440 line_status = LineStatus::Error;
442 else if (m_interrupted)
445 line_status = LineStatus::Done;
449 switch (m_lines_command)
452 if (m_line_complete_callback)
454 line_status = m_line_complete_callback (this,
458 m_line_complete_callback_baton);
460 else if (line == end_line)
462 line_status = LineStatus::Done;
465 if (line_status == LineStatus::Success)
468 // If we already have content for the next line because
469 // we were editing previous lines, then populate the line
470 // with the appropriate contents
471 if (line_idx+1 < lines.GetSize() && !lines[line_idx+1].empty())
472 ::el_push (m_editline,
473 const_cast<char*>(lines[line_idx+1].c_str()));
475 else if (line_status == LineStatus::Error)
477 // Clear to end of line ("ESC[K"), then print the error,
478 // then go to the next line ("\n") and then move cursor up
479 // two lines ("ESC[2A").
480 fprintf (err_file, "\033[Kerror: %s\n\033[2A", error.AsCString());
483 case Command::EditPrevLine:
484 if (m_lines_curr_line > 1)
486 //::fprintf (out_file, "\033[1A\033[%uD\033[2K", (uint32_t)(m_lines_prompt.size() + lines[line_idx].size())); // Make cursor go up a line and clear that line
487 ::fprintf (out_file, "\033[1A\033[1000D\033[2K");
488 if (!lines[line_idx-1].empty())
489 ::el_push (m_editline,
490 const_cast<char*>(lines[line_idx-1].c_str()));
494 case Command::EditNextLine:
495 // Allow the down arrow to create a new line
497 //::fprintf (out_file, "\033[1B\033[%uD\033[2K", (uint32_t)(m_lines_prompt.size() + lines[line_idx].size()));
498 ::fprintf (out_file, "\033[1B\033[1000D\033[2K");
499 if (line_idx+1 < lines.GetSize() && !lines[line_idx+1].empty())
500 ::el_push (m_editline,
501 const_cast<char*>(lines[line_idx+1].c_str()));
506 m_lines_curr_line = 0;
507 m_lines_command = Command::None;
509 // If we have a callback, call it one more time to let the
510 // user know the lines are complete
511 if (m_line_complete_callback && !interrupted)
512 m_line_complete_callback (this,
516 m_line_complete_callback_baton);
522 Editline::HandleCompletion (int ch)
524 if (m_completion_callback == NULL)
527 const LineInfo *line_info = ::el_line(m_editline);
528 StringList completions;
531 const int num_completions = m_completion_callback (line_info->buffer,
534 0, // Don't skip any matches (start at match zero)
535 -1, // Get all the matches
537 m_completion_callback_baton);
539 FILE *out_file = GetOutputFile();
541 // if (num_completions == -1)
543 // ::el_insertstr (m_editline, m_completion_key);
544 // return CC_REDISPLAY;
547 if (num_completions == -2)
549 // Replace the entire line with the first string...
550 ::el_deletestr (m_editline, line_info->cursor - line_info->buffer);
551 ::el_insertstr (m_editline, completions.GetStringAtIndex(0));
555 // If we get a longer match display that first.
556 const char *completion_str = completions.GetStringAtIndex(0);
557 if (completion_str != NULL && *completion_str != '\0')
559 el_insertstr (m_editline, completion_str);
563 if (num_completions > 1)
565 int num_elements = num_completions + 1;
566 ::fprintf (out_file, "\nAvailable completions:");
567 if (num_completions < page_size)
569 for (int i = 1; i < num_elements; i++)
571 completion_str = completions.GetStringAtIndex(i);
572 ::fprintf (out_file, "\n\t%s", completion_str);
574 ::fprintf (out_file, "\n");
581 while (cur_pos < num_elements)
583 int endpoint = cur_pos + page_size;
584 if (endpoint > num_elements)
585 endpoint = num_elements;
586 for (; cur_pos < endpoint; cur_pos++)
588 completion_str = completions.GetStringAtIndex(cur_pos);
589 ::fprintf (out_file, "\n\t%s", completion_str);
592 if (cur_pos >= num_elements)
594 ::fprintf (out_file, "\n");
598 ::fprintf (out_file, "\nMore (Y/n/a): ");
600 got_char = el_getc(m_editline, &reply);
601 if (got_char == -1 || reply == 'n')
604 page_size = num_elements - cur_pos;
610 if (num_completions == 0)
611 return CC_REFRESH_BEEP;
617 Editline::GetClientData (::EditLine *e)
619 Editline *editline = NULL;
620 if (e && ::el_get(e, EL_CLIENTDATA, &editline) == 0)
626 Editline::GetInputFile ()
628 return GetFilePointer (m_editline, 0);
632 Editline::GetOutputFile ()
634 return GetFilePointer (m_editline, 1);
638 Editline::GetErrorFile ()
640 return GetFilePointer (m_editline, 2);
644 Editline::GetPrompt()
646 if (m_prompt_with_line_numbers && m_lines_curr_line > 0)
649 strm.Printf("%3u: ", m_lines_curr_line);
650 m_lines_prompt = std::move(strm.GetString());
651 return m_lines_prompt.c_str();
655 return m_prompt.c_str();
660 Editline::SetPrompt (const char *p)
666 size_t start_pos = 0;
668 while ((escape_pos = m_prompt.find('\033', start_pos)) != std::string::npos)
670 m_prompt.insert(escape_pos, 1, k_prompt_escape_char);
676 Editline::GetFilePointer (::EditLine *e, int fd)
678 FILE *file_ptr = NULL;
679 if (e && ::el_get(e, EL_GETFP, fd, &file_ptr) == 0)
685 Editline::CallbackEditPrevLine (::EditLine *e, int ch)
687 Editline *editline = GetClientData (e);
688 if (editline->m_lines_curr_line > 1)
690 editline->m_lines_command = Command::EditPrevLine;
696 Editline::CallbackEditNextLine (::EditLine *e, int ch)
698 Editline *editline = GetClientData (e);
699 if (editline->m_lines_curr_line < editline->m_lines_max_line)
701 editline->m_lines_command = Command::EditNextLine;
708 Editline::CallbackComplete (::EditLine *e, int ch)
710 Editline *editline = GetClientData (e);
712 return editline->HandleCompletion (ch);
717 Editline::GetPromptCallback (::EditLine *e)
719 Editline *editline = GetClientData (e);
721 return editline->GetPrompt();
726 Editline::GetCharFromInputFileCallback (EditLine *e, char *c)
728 Editline *editline = GetClientData (e);
729 if (editline && editline->m_got_eof == false)
731 FILE *f = editline->GetInputFile();
734 editline->m_got_eof = true;
741 lldb::ConnectionStatus status = eConnectionStatusSuccess;
743 // When we start to call el_gets() the editline library needs to
745 editline->m_getting_char.SetValue(true, eBroadcastAlways);
746 const size_t n = editline->m_file.Read(&ch, 1, UINT32_MAX, status, NULL);
747 editline->m_getting_char.SetValue(false, eBroadcastAlways);
752 // Only turn a CTRL+D into a EOF if we receive the
753 // CTRL+D an empty line, otherwise it will forward
754 // delete the character at the cursor
755 const LineInfo *line_info = ::el_line(e);
756 if (line_info != NULL &&
757 line_info->buffer == line_info->cursor &&
758 line_info->cursor == line_info->lastchar)
760 editline->m_got_eof = true;
765 if (status == eConnectionStatusEndOfFile)
767 editline->m_got_eof = true;
780 case eConnectionStatusInterrupted:
781 editline->m_interrupted = true;
785 case eConnectionStatusSuccess: // Success
788 case eConnectionStatusError: // Check GetError() for details
789 case eConnectionStatusTimedOut: // Request timed out
790 case eConnectionStatusEndOfFile: // End-of-file encountered
791 case eConnectionStatusNoConnection: // No connection
792 case eConnectionStatusLostConnection: // Lost connection while connected to a valid connection
793 editline->m_got_eof = true;
807 // If we are getting a line, we might have started to call el_gets() and
808 // it might be printing the prompt. Here we make sure we are actually getting
809 // a character. This way we know the entire prompt has been printed.
810 TimeValue timeout = TimeValue::Now();
811 timeout.OffsetWithSeconds(1);
812 if (m_getting_char.WaitForValueEqualTo(true, &timeout))
814 FILE *out_file = GetOutputFile();
817 const LineInfo *line_info = ::el_line(m_editline);
819 ::fprintf (out_file, "\033[%uD\033[K", (uint32_t)(strlen(GetPrompt()) + line_info->cursor - line_info->buffer));
831 // If we are getting a line, we might have started to call el_gets() and
832 // it might be printing the prompt. Here we make sure we are actually getting
833 // a character. This way we know the entire prompt has been printed.
834 TimeValue timeout = TimeValue::Now();
835 timeout.OffsetWithSeconds(1);
836 if (m_getting_char.WaitForValueEqualTo(true, &timeout))
838 ::el_set (m_editline, EL_REFRESH);
844 Editline::Interrupt ()
846 m_interrupted = true;
847 if (m_getting_line || m_lines_curr_line > 0)
848 return m_file.InterruptRead();
849 return false; // Interrupt not handled as we weren't getting a line or lines