| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 42 #define completion_matches rl_completion_matches | 42 #define completion_matches rl_completion_matches |
| 43 #endif | 43 #endif |
| 44 | 44 |
| 45 | 45 |
| 46 namespace v8 { | 46 namespace v8 { |
| 47 | 47 |
| 48 | 48 |
| 49 class ReadLineEditor: public LineEditor { | 49 class ReadLineEditor: public LineEditor { |
| 50 public: | 50 public: |
| 51 ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { } | 51 ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { } |
| 52 virtual i::SmartArrayPointer<char> Prompt(const char* prompt); | 52 virtual Handle<String> Prompt(const char* prompt); |
| 53 virtual bool Open(); | 53 virtual bool Open(); |
| 54 virtual bool Close(); | 54 virtual bool Close(); |
| 55 virtual void AddHistory(const char* str); | 55 virtual void AddHistory(const char* str); |
| 56 |
| 57 static const char* kHistoryFileName; |
| 58 static const int kMaxHistoryEntries; |
| 59 |
| 56 private: | 60 private: |
| 57 static char** AttemptedCompletion(const char* text, int start, int end); | 61 static char** AttemptedCompletion(const char* text, int start, int end); |
| 58 static char* CompletionGenerator(const char* text, int state); | 62 static char* CompletionGenerator(const char* text, int state); |
| 59 static char kWordBreakCharacters[]; | 63 static char kWordBreakCharacters[]; |
| 60 }; | 64 }; |
| 61 | 65 |
| 62 | 66 |
| 63 static ReadLineEditor read_line_editor; | 67 static ReadLineEditor read_line_editor; |
| 64 char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"', | 68 char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"', |
| 65 '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(', | 69 '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(', |
| 66 '\0'}; | 70 '\0'}; |
| 67 | 71 |
| 68 | 72 |
| 73 const char* ReadLineEditor::kHistoryFileName = ".d8_history"; |
| 74 const int ReadLineEditor::kMaxHistoryEntries = 1000; |
| 75 |
| 76 |
| 69 bool ReadLineEditor::Open() { | 77 bool ReadLineEditor::Open() { |
| 70 rl_initialize(); | 78 rl_initialize(); |
| 71 rl_attempted_completion_function = AttemptedCompletion; | 79 rl_attempted_completion_function = AttemptedCompletion; |
| 72 rl_completer_word_break_characters = kWordBreakCharacters; | 80 rl_completer_word_break_characters = kWordBreakCharacters; |
| 73 rl_bind_key('\t', rl_complete); | 81 rl_bind_key('\t', rl_complete); |
| 74 using_history(); | 82 using_history(); |
| 75 stifle_history(Shell::kMaxHistoryEntries); | 83 stifle_history(kMaxHistoryEntries); |
| 76 return read_history(Shell::kHistoryFileName) == 0; | 84 return read_history(kHistoryFileName) == 0; |
| 77 } | 85 } |
| 78 | 86 |
| 79 | 87 |
| 80 bool ReadLineEditor::Close() { | 88 bool ReadLineEditor::Close() { |
| 81 return write_history(Shell::kHistoryFileName) == 0; | 89 return write_history(kHistoryFileName) == 0; |
| 82 } | 90 } |
| 83 | 91 |
| 84 | 92 |
| 85 i::SmartArrayPointer<char> ReadLineEditor::Prompt(const char* prompt) { | 93 Handle<String> ReadLineEditor::Prompt(const char* prompt) { |
| 86 char* result = readline(prompt); | 94 char* result = readline(prompt); |
| 87 return i::SmartArrayPointer<char>(result); | 95 if (result != NULL) { |
| 96 AddHistory(result); |
| 97 } else { |
| 98 return Handle<String>(); |
| 99 } |
| 100 return String::New(result); |
| 88 } | 101 } |
| 89 | 102 |
| 90 | 103 |
| 91 void ReadLineEditor::AddHistory(const char* str) { | 104 void ReadLineEditor::AddHistory(const char* str) { |
| 92 // Do not record empty input. | 105 // Do not record empty input. |
| 93 if (strlen(str) == 0) return; | 106 if (strlen(str) == 0) return; |
| 94 // Remove duplicate history entry. | 107 // Remove duplicate history entry. |
| 95 history_set_pos(history_length-1); | 108 history_set_pos(history_length-1); |
| 96 if (current_history()) { | 109 if (current_history()) { |
| 97 do { | 110 do { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 111 char** result = completion_matches(text, CompletionGenerator); | 124 char** result = completion_matches(text, CompletionGenerator); |
| 112 rl_attempted_completion_over = true; | 125 rl_attempted_completion_over = true; |
| 113 return result; | 126 return result; |
| 114 } | 127 } |
| 115 | 128 |
| 116 | 129 |
| 117 char* ReadLineEditor::CompletionGenerator(const char* text, int state) { | 130 char* ReadLineEditor::CompletionGenerator(const char* text, int state) { |
| 118 static unsigned current_index; | 131 static unsigned current_index; |
| 119 static Persistent<Array> current_completions; | 132 static Persistent<Array> current_completions; |
| 120 if (state == 0) { | 133 if (state == 0) { |
| 121 i::SmartArrayPointer<char> full_text(i::StrNDup(rl_line_buffer, rl_point)); | |
| 122 HandleScope scope; | 134 HandleScope scope; |
| 135 Local<String> full_text = String::New(rl_line_buffer, rl_point); |
| 123 Handle<Array> completions = | 136 Handle<Array> completions = |
| 124 Shell::GetCompletions(String::New(text), String::New(*full_text)); | 137 Shell::GetCompletions(String::New(text), full_text); |
| 125 current_completions = Persistent<Array>::New(completions); | 138 current_completions = Persistent<Array>::New(completions); |
| 126 current_index = 0; | 139 current_index = 0; |
| 127 } | 140 } |
| 128 if (current_index < current_completions->Length()) { | 141 if (current_index < current_completions->Length()) { |
| 129 HandleScope scope; | 142 HandleScope scope; |
| 130 Handle<Integer> index = Integer::New(current_index); | 143 Handle<Integer> index = Integer::New(current_index); |
| 131 Handle<Value> str_obj = current_completions->Get(index); | 144 Handle<Value> str_obj = current_completions->Get(index); |
| 132 current_index++; | 145 current_index++; |
| 133 String::Utf8Value str(str_obj); | 146 String::Utf8Value str(str_obj); |
| 134 return strdup(*str); | 147 return strdup(*str); |
| 135 } else { | 148 } else { |
| 136 current_completions.Dispose(); | 149 current_completions.Dispose(); |
| 137 current_completions.Clear(); | 150 current_completions.Clear(); |
| 138 return NULL; | 151 return NULL; |
| 139 } | 152 } |
| 140 } | 153 } |
| 141 | 154 |
| 142 | 155 |
| 143 } // namespace v8 | 156 } // namespace v8 |
| OLD | NEW |