[go: nahoru, domu]

blob: 71be933109be193c6a80ef5f5a613fcd325dd830 [file] [log] [blame]
Ben Murdoch589d6972011-11-30 16:04:58 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 <cstdio> // NOLINT
Andrei Popescu402d9372010-02-26 13:31:12 +000030#include <readline/readline.h> // NOLINT
31#include <readline/history.h> // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +000032
Ben Murdoch257744e2011-11-30 15:57:28 +000033// The readline includes leaves RETURN defined which breaks V8 compilation.
34#undef RETURN
Steve Blocka7e24c12009-10-30 11:49:00 +000035
36#include "d8.h"
37
38
39// There are incompatibilities between different versions and different
40// implementations of readline. This smooths out one known incompatibility.
41#if RL_READLINE_VERSION >= 0x0500
42#define completion_matches rl_completion_matches
43#endif
44
45
46namespace v8 {
47
48
49class ReadLineEditor: public LineEditor {
50 public:
51 ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { }
Ben Murdoch589d6972011-11-30 16:04:58 +000052 virtual i::SmartArrayPointer<char> Prompt(const char* prompt);
Steve Blocka7e24c12009-10-30 11:49:00 +000053 virtual bool Open();
54 virtual bool Close();
55 virtual void AddHistory(const char* str);
56 private:
57 static char** AttemptedCompletion(const char* text, int start, int end);
58 static char* CompletionGenerator(const char* text, int state);
59 static char kWordBreakCharacters[];
60};
61
62
63static ReadLineEditor read_line_editor;
64char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"',
65 '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(',
66 '\0'};
67
68
69bool ReadLineEditor::Open() {
70 rl_initialize();
71 rl_attempted_completion_function = AttemptedCompletion;
72 rl_completer_word_break_characters = kWordBreakCharacters;
73 rl_bind_key('\t', rl_complete);
74 using_history();
Ben Murdoch589d6972011-11-30 16:04:58 +000075 stifle_history(Shell::kMaxHistoryEntries);
Steve Blocka7e24c12009-10-30 11:49:00 +000076 return read_history(Shell::kHistoryFileName) == 0;
77}
78
79
80bool ReadLineEditor::Close() {
81 return write_history(Shell::kHistoryFileName) == 0;
82}
83
84
Ben Murdoch589d6972011-11-30 16:04:58 +000085i::SmartArrayPointer<char> ReadLineEditor::Prompt(const char* prompt) {
Steve Blocka7e24c12009-10-30 11:49:00 +000086 char* result = readline(prompt);
Ben Murdoch589d6972011-11-30 16:04:58 +000087 return i::SmartArrayPointer<char>(result);
Steve Blocka7e24c12009-10-30 11:49:00 +000088}
89
90
91void ReadLineEditor::AddHistory(const char* str) {
Ben Murdoch589d6972011-11-30 16:04:58 +000092 // Do not record empty input.
93 if (strlen(str) == 0) return;
94 // Remove duplicate history entry.
95 history_set_pos(history_length-1);
96 if (current_history()) {
97 do {
98 if (strcmp(current_history()->line, str) == 0) {
99 remove_history(where_history());
100 break;
101 }
102 } while (previous_history());
103 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000104 add_history(str);
105}
106
107
108char** ReadLineEditor::AttemptedCompletion(const char* text,
109 int start,
110 int end) {
111 char** result = completion_matches(text, CompletionGenerator);
112 rl_attempted_completion_over = true;
113 return result;
114}
115
116
117char* ReadLineEditor::CompletionGenerator(const char* text, int state) {
118 static unsigned current_index;
119 static Persistent<Array> current_completions;
120 if (state == 0) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000121 i::SmartArrayPointer<char> full_text(i::StrNDup(rl_line_buffer, rl_point));
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 HandleScope scope;
123 Handle<Array> completions =
124 Shell::GetCompletions(String::New(text), String::New(*full_text));
125 current_completions = Persistent<Array>::New(completions);
126 current_index = 0;
127 }
128 if (current_index < current_completions->Length()) {
129 HandleScope scope;
130 Handle<Integer> index = Integer::New(current_index);
131 Handle<Value> str_obj = current_completions->Get(index);
132 current_index++;
133 String::Utf8Value str(str_obj);
134 return strdup(*str);
135 } else {
136 current_completions.Dispose();
137 current_completions.Clear();
138 return NULL;
139 }
140}
141
142
143} // namespace v8