ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 29 | #include <cstdio> // NOLINT |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 30 | #include <readline/readline.h> // NOLINT |
| 31 | #include <readline/history.h> // NOLINT |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 32 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 33 | // The readline includes leaves RETURN defined which breaks V8 compilation. |
| 34 | #undef RETURN |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 35 | |
| 36 | #include "d8.h" |
| 37 | |
| 38 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 39 | // There are incompatibilities between different versions and different |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 40 | // implementations of readline. This smooths out one known incompatibility. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 41 | #if RL_READLINE_VERSION >= 0x0500 |
| 42 | #define completion_matches rl_completion_matches |
| 43 | #endif |
| 44 | |
| 45 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 46 | namespace v8 { |
| 47 | |
| 48 | |
| 49 | class ReadLineEditor: public LineEditor { |
| 50 | public: |
| 51 | ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 52 | virtual Handle<String> Prompt(const char* prompt); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 53 | virtual bool Open(); |
| 54 | virtual bool Close(); |
| 55 | virtual void AddHistory(const char* str); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 56 | |
| 57 | static const char* kHistoryFileName; |
| 58 | static const int kMaxHistoryEntries; |
| 59 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 60 | private: |
| 61 | static char** AttemptedCompletion(const char* text, int start, int end); |
| 62 | static char* CompletionGenerator(const char* text, int state); |
| 63 | static char kWordBreakCharacters[]; |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | static ReadLineEditor read_line_editor; |
| 68 | char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"', |
| 69 | '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(', |
| 70 | '\0'}; |
| 71 | |
| 72 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 73 | const char* ReadLineEditor::kHistoryFileName = ".d8_history"; |
| 74 | const int ReadLineEditor::kMaxHistoryEntries = 1000; |
| 75 | |
| 76 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 77 | bool ReadLineEditor::Open() { |
| 78 | rl_initialize(); |
| 79 | rl_attempted_completion_function = AttemptedCompletion; |
| 80 | rl_completer_word_break_characters = kWordBreakCharacters; |
| 81 | rl_bind_key('\t', rl_complete); |
| 82 | using_history(); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 83 | stifle_history(kMaxHistoryEntries); |
| 84 | return read_history(kHistoryFileName) == 0; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | bool ReadLineEditor::Close() { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 89 | return write_history(kHistoryFileName) == 0; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 93 | Handle<String> ReadLineEditor::Prompt(const char* prompt) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 94 | char* result = NULL; |
| 95 | { // Release lock for blocking input. |
| 96 | Unlocker unlock(Isolate::GetCurrent()); |
| 97 | result = readline(prompt); |
| 98 | } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 99 | if (result != NULL) { |
| 100 | AddHistory(result); |
| 101 | } else { |
| 102 | return Handle<String>(); |
| 103 | } |
| 104 | return String::New(result); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | |
| 108 | void ReadLineEditor::AddHistory(const char* str) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 109 | // Do not record empty input. |
| 110 | if (strlen(str) == 0) return; |
| 111 | // Remove duplicate history entry. |
| 112 | history_set_pos(history_length-1); |
| 113 | if (current_history()) { |
| 114 | do { |
| 115 | if (strcmp(current_history()->line, str) == 0) { |
| 116 | remove_history(where_history()); |
| 117 | break; |
| 118 | } |
| 119 | } while (previous_history()); |
| 120 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 121 | add_history(str); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | char** ReadLineEditor::AttemptedCompletion(const char* text, |
| 126 | int start, |
| 127 | int end) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 128 | char** result = completion_matches(text, CompletionGenerator); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 129 | rl_attempted_completion_over = true; |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | char* ReadLineEditor::CompletionGenerator(const char* text, int state) { |
| 135 | static unsigned current_index; |
| 136 | static Persistent<Array> current_completions; |
| 137 | if (state == 0) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 138 | HandleScope scope; |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 139 | Local<String> full_text = String::New(rl_line_buffer, rl_point); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 140 | Handle<Array> completions = |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 141 | Shell::GetCompletions(String::New(text), full_text); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 142 | current_completions = Persistent<Array>::New(completions); |
| 143 | current_index = 0; |
| 144 | } |
| 145 | if (current_index < current_completions->Length()) { |
| 146 | HandleScope scope; |
| 147 | Handle<Integer> index = Integer::New(current_index); |
| 148 | Handle<Value> str_obj = current_completions->Get(index); |
| 149 | current_index++; |
| 150 | String::Utf8Value str(str_obj); |
| 151 | return strdup(*str); |
| 152 | } else { |
| 153 | current_completions.Dispose(); |
| 154 | current_completions.Clear(); |
| 155 | return NULL; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | } // namespace v8 |