diff options
Diffstat (limited to 'akonadi2_cli/repl/replStates.cpp')
-rw-r--r-- | akonadi2_cli/repl/replStates.cpp | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/akonadi2_cli/repl/replStates.cpp b/akonadi2_cli/repl/replStates.cpp new file mode 100644 index 0000000..e87dd5f --- /dev/null +++ b/akonadi2_cli/repl/replStates.cpp | |||
@@ -0,0 +1,155 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | |||
20 | #include "replStates.h" | ||
21 | |||
22 | #include <unistd.h> | ||
23 | #include <iostream> | ||
24 | |||
25 | #include <readline/readline.h> | ||
26 | #include <readline/history.h> | ||
27 | |||
28 | #include <QDebug> | ||
29 | #include <QEvent> | ||
30 | #include <QStateMachine> | ||
31 | |||
32 | #include "module.h" | ||
33 | |||
34 | static char *akonadi2_cli_next_tab_complete_match(const char *text, int state); | ||
35 | static char ** akonadi2_cli_tab_completion(const char *text, int start, int end); | ||
36 | |||
37 | ReadState::ReadState(QState *parent) | ||
38 | : QState(parent) | ||
39 | { | ||
40 | rl_completion_entry_function = akonadi2_cli_next_tab_complete_match; | ||
41 | rl_attempted_completion_function = akonadi2_cli_tab_completion; | ||
42 | } | ||
43 | |||
44 | void ReadState::onEntry(QEvent *event) | ||
45 | { | ||
46 | Q_UNUSED(event) | ||
47 | char *line = readline(prompt()); | ||
48 | |||
49 | if (!line) { | ||
50 | std::cout << std::endl; | ||
51 | emit exitRequested(); | ||
52 | return; | ||
53 | } | ||
54 | |||
55 | // we have actual data, so let's wait for a full line of text | ||
56 | QByteArray input(line); | ||
57 | const QString text = QString(line).simplified(); | ||
58 | //qDebug() << "Line is ... " << text; | ||
59 | |||
60 | if (text.length() > 0) { | ||
61 | add_history(line); | ||
62 | } | ||
63 | |||
64 | free(line); | ||
65 | emit command(text); | ||
66 | } | ||
67 | |||
68 | const char *ReadState::prompt() const | ||
69 | { | ||
70 | return "> "; | ||
71 | } | ||
72 | |||
73 | UnfinishedReadState::UnfinishedReadState(QState *parent) | ||
74 | : ReadState(parent) | ||
75 | { | ||
76 | } | ||
77 | |||
78 | const char *UnfinishedReadState::prompt() const | ||
79 | { | ||
80 | return " "; | ||
81 | } | ||
82 | |||
83 | EvalState::EvalState(QState *parent) | ||
84 | : QState(parent), | ||
85 | m_complete(false) | ||
86 | { | ||
87 | } | ||
88 | |||
89 | void EvalState::onEntry(QEvent *event) | ||
90 | { | ||
91 | QStateMachine::SignalEvent *e = dynamic_cast<QStateMachine::SignalEvent*>(event); | ||
92 | |||
93 | if (!e || e->arguments().isEmpty()) { | ||
94 | if (m_complete) { | ||
95 | emit completed(); | ||
96 | } else { | ||
97 | emit continueInput(); | ||
98 | } | ||
99 | return; | ||
100 | } | ||
101 | |||
102 | //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"'] | ||
103 | const QString command = e->arguments()[0].toString(); | ||
104 | |||
105 | if (!command.isEmpty()) { | ||
106 | m_complete = command.right(1) != "\\"; | ||
107 | if (m_complete) { | ||
108 | //emit output("Processing ... " + command); | ||
109 | const QStringList commands = command.split(" "); | ||
110 | Module::run(commands); | ||
111 | emit completed(); | ||
112 | } | ||
113 | } | ||
114 | } | ||
115 | |||
116 | PrintState::PrintState(QState *parent) | ||
117 | : QState(parent) | ||
118 | { | ||
119 | } | ||
120 | |||
121 | void PrintState::onEntry(QEvent *event) | ||
122 | { | ||
123 | QStateMachine::SignalEvent *e = dynamic_cast<QStateMachine::SignalEvent*>(event); | ||
124 | |||
125 | if (e && !e->arguments().isEmpty()) { | ||
126 | const QString command = e->arguments()[0].toString(); | ||
127 | QTextStream stream(stdout); | ||
128 | stream << command << "\n"; | ||
129 | } | ||
130 | |||
131 | emit completed(); | ||
132 | } | ||
133 | |||
134 | static QStringList tab_completion_full_state; | ||
135 | static bool tab_completion_at_root = false; | ||
136 | |||
137 | static char **akonadi2_cli_tab_completion(const char *text, int start, int end) | ||
138 | { | ||
139 | tab_completion_at_root = start == 0; | ||
140 | tab_completion_full_state = QString(rl_line_buffer).remove(start, end - start).split(" ", QString::SkipEmptyParts); | ||
141 | return NULL; | ||
142 | } | ||
143 | |||
144 | static char *akonadi2_cli_next_tab_complete_match(const char *text, int state) | ||
145 | { | ||
146 | QVector<Module::Syntax> nearest = Module::nearestSyntax(tab_completion_full_state, QString(text)); | ||
147 | |||
148 | if (nearest.size() > state) { | ||
149 | return qstrdup(nearest[state].keyword.toUtf8()); | ||
150 | } | ||
151 | |||
152 | return rl_filename_completion_function(text, state); | ||
153 | } | ||
154 | |||
155 | #include "moc_replStates.cpp" | ||