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