| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 |
| 29 #include <readline/readline.h> |
| 30 #include <readline/history.h> |
| 31 |
| 32 |
| 33 #include "d8.h" |
| 34 |
| 35 |
| 36 namespace v8 { |
| 37 |
| 38 |
| 39 class ReadLineEditor: public LineEditor { |
| 40 public: |
| 41 ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { } |
| 42 virtual i::SmartPointer<char> Prompt(const char* prompt); |
| 43 virtual bool Open(); |
| 44 virtual bool Close(); |
| 45 virtual void AddHistory(const char* str); |
| 46 private: |
| 47 static char** AttemptedCompletion(const char* text, int start, int end); |
| 48 static char* CompletionGenerator(const char* text, int state); |
| 49 static char kWordBreakCharacters[]; |
| 50 }; |
| 51 |
| 52 |
| 53 static ReadLineEditor read_line_editor; |
| 54 char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"', |
| 55 '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(', |
| 56 '\0'}; |
| 57 |
| 58 |
| 59 bool ReadLineEditor::Open() { |
| 60 rl_initialize(); |
| 61 rl_attempted_completion_function = AttemptedCompletion; |
| 62 rl_completer_word_break_characters = kWordBreakCharacters; |
| 63 rl_bind_key('\t', rl_complete); |
| 64 using_history(); |
| 65 return read_history(Shell::kHistoryFileName) == 0; |
| 66 } |
| 67 |
| 68 |
| 69 bool ReadLineEditor::Close() { |
| 70 return write_history(Shell::kHistoryFileName) == 0; |
| 71 } |
| 72 |
| 73 |
| 74 i::SmartPointer<char> ReadLineEditor::Prompt(const char* prompt) { |
| 75 char* result = readline(prompt); |
| 76 return i::SmartPointer<char>(result); |
| 77 } |
| 78 |
| 79 |
| 80 void ReadLineEditor::AddHistory(const char* str) { |
| 81 add_history(str); |
| 82 } |
| 83 |
| 84 |
| 85 char** ReadLineEditor::AttemptedCompletion(const char* text, |
| 86 int start, |
| 87 int end) { |
| 88 char** result = rl_completion_matches(text, CompletionGenerator); |
| 89 rl_attempted_completion_over = true; |
| 90 return result; |
| 91 } |
| 92 |
| 93 |
| 94 char* ReadLineEditor::CompletionGenerator(const char* text, int state) { |
| 95 static unsigned current_index; |
| 96 static Persistent<Array> current_completions; |
| 97 if (state == 0) { |
| 98 i::SmartPointer<char> full_text(strndup(rl_line_buffer, rl_point)); |
| 99 HandleScope scope; |
| 100 Handle<Array> completions = |
| 101 Shell::GetCompletions(String::New(text), String::New(*full_text)); |
| 102 current_completions = Persistent<Array>::New(completions); |
| 103 current_index = 0; |
| 104 } |
| 105 if (current_index < current_completions->Length()) { |
| 106 HandleScope scope; |
| 107 Handle<Integer> index = Integer::New(current_index); |
| 108 Handle<Value> str_obj = current_completions->Get(index); |
| 109 current_index++; |
| 110 String::Utf8Value str(str_obj); |
| 111 return strdup(*str); |
| 112 } else { |
| 113 current_completions.Dispose(); |
| 114 current_completions.Clear(); |
| 115 return NULL; |
| 116 } |
| 117 } |
| 118 } // v8 |
| OLD | NEW |