summaryrefslogtreecommitdiffstats
path: root/akonadish/repl
diff options
context:
space:
mode:
Diffstat (limited to 'akonadish/repl')
-rw-r--r--akonadish/repl/repl.cpp91
-rw-r--r--akonadish/repl/repl.h35
-rw-r--r--akonadish/repl/replStates.cpp171
-rw-r--r--akonadish/repl/replStates.h87
4 files changed, 384 insertions, 0 deletions
diff --git a/akonadish/repl/repl.cpp b/akonadish/repl/repl.cpp
new file mode 100644
index 0000000..499a4af
--- /dev/null
+++ b/akonadish/repl/repl.cpp
@@ -0,0 +1,91 @@
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 "repl.h"
21
22#include <readline/history.h>
23
24#include <QDir>
25#include <QFile>
26#include <QFinalState>
27#include <QStandardPaths>
28#include <QTextStream>
29
30#include "replStates.h"
31#include "syntaxtree.h"
32
33Repl::Repl(QObject *parent)
34 : QStateMachine(parent)
35{
36 // readline history setup
37 using_history();
38 read_history(commandHistoryPath().toLocal8Bit());
39
40 // create all states
41 ReadState *read = new ReadState(this);
42 UnfinishedReadState *unfinishedRead = new UnfinishedReadState(this);
43 EvalState *eval = new EvalState(this);
44 PrintState *print = new PrintState(this);
45 QFinalState *final = new QFinalState(this);
46
47 // connect the transitions
48 read->addTransition(read, SIGNAL(command(QString)), eval);
49 read->addTransition(read, SIGNAL(exitRequested()), final);
50
51 unfinishedRead->addTransition(unfinishedRead, SIGNAL(command(QString)), eval);
52 unfinishedRead->addTransition(unfinishedRead, SIGNAL(exitRequested()), final);
53
54 eval->addTransition(eval, SIGNAL(completed()), read);
55 eval->addTransition(eval, SIGNAL(continueInput()), unfinishedRead);
56 eval->addTransition(eval, SIGNAL(output(QString)), print);
57
58 print->addTransition(print, SIGNAL(completed()), eval);
59
60 setInitialState(read);
61 printWelcomeBanner();
62 start();
63}
64
65Repl::~Repl()
66{
67 // readline history writing
68 write_history(commandHistoryPath().toLocal8Bit());
69}
70
71void Repl::printWelcomeBanner()
72{
73 QTextStream out(stdout);
74 out << QObject::tr("Welcome to the Akonadi2 interative shell!\n");
75 out << QObject::tr("Type `help` for information on the available commands.\n");
76 out.flush();
77}
78
79QString Repl::commandHistoryPath()
80{
81 const QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
82
83 if (!QFile::exists(path)) {
84 QDir dir;
85 dir.mkpath(path);
86 }
87
88 return path + "/repl_history";
89}
90
91#include "moc_repl.cpp"
diff --git a/akonadish/repl/repl.h b/akonadish/repl/repl.h
new file mode 100644
index 0000000..d8d2533
--- /dev/null
+++ b/akonadish/repl/repl.h
@@ -0,0 +1,35 @@
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#pragma once
21
22#include <QStateMachine>
23
24class Repl : public QStateMachine
25{
26 Q_OBJECT
27
28public:
29 Repl(QObject *parent = 0);
30 ~Repl();
31
32private:
33 static void printWelcomeBanner();
34 static QString commandHistoryPath();
35};
diff --git a/akonadish/repl/replStates.cpp b/akonadish/repl/replStates.cpp
new file mode 100644
index 0000000..62888d0
--- /dev/null
+++ b/akonadish/repl/replStates.cpp
@@ -0,0 +1,171 @@
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 "syntaxtree.h"
33
34static char *akonadi2_cli_next_tab_complete_match(const char *text, int state);
35static char ** akonadi2_cli_tab_completion(const char *text, int start, int end);
36
37ReadState::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
44void 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
68const char *ReadState::prompt() const
69{
70 return "> ";
71}
72
73UnfinishedReadState::UnfinishedReadState(QState *parent)
74 : ReadState(parent)
75{
76}
77
78const char *UnfinishedReadState::prompt() const
79{
80 return " ";
81}
82
83EvalState::EvalState(QState *parent)
84 : QState(parent)
85{
86}
87
88void EvalState::onEntry(QEvent *event)
89{
90 QStateMachine::SignalEvent *e = dynamic_cast<QStateMachine::SignalEvent*>(event);
91
92 const QString command = e ? e->arguments()[0].toString() : QString();
93
94 if (command.isEmpty()) {
95 complete();
96 return;
97 }
98
99 if (command.right(1) == "\\") {
100 m_partial += " " + command.left(command.size() - 1);
101 continueInput();
102 } else {
103 m_partial += " " + command;
104 complete();
105 }
106}
107
108void EvalState::complete()
109{
110 m_partial = m_partial.simplified();
111
112 if (!m_partial.isEmpty()) {
113 //emit output("Processing ... " + command);
114 const QStringList commands = SyntaxTree::tokenize(m_partial);
115 SyntaxTree::self()->run(commands);
116 m_partial.clear();
117 }
118
119 emit completed();
120}
121
122PrintState::PrintState(QState *parent)
123 : QState(parent)
124{
125}
126
127void PrintState::onEntry(QEvent *event)
128{
129 QStateMachine::SignalEvent *e = dynamic_cast<QStateMachine::SignalEvent*>(event);
130
131 if (e && !e->arguments().isEmpty()) {
132 const QString command = e->arguments()[0].toString();
133 QTextStream stream(stdout);
134 stream << command << "\n";
135 }
136
137 emit completed();
138}
139
140static QStringList tab_completion_full_state;
141static bool tab_completion_at_root = false;
142
143static char **akonadi2_cli_tab_completion(const char *text, int start, int end)
144{
145 tab_completion_at_root = start == 0;
146 tab_completion_full_state = QString(rl_line_buffer).remove(start, end - start).split(" ", QString::SkipEmptyParts);
147 return NULL;
148}
149
150static char *akonadi2_cli_next_tab_complete_match(const char *text, int state)
151{
152 const QString fragment(text);
153 Syntax::List nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, fragment);
154 //for (auto syntax: nearest) { qDebug() << "Nearest: " << syntax.keyword; }
155
156 if (nearest.isEmpty()) {
157 SyntaxTree::Command command = SyntaxTree::self()->match(tab_completion_full_state);
158 if (command.first && command.first->completer) {
159 QStringList commandCompletions = command.first->completer(tab_completion_full_state, fragment);
160 if (commandCompletions.size() > state) {
161 return qstrdup(commandCompletions[state].toUtf8());
162 }
163 }
164 } else if (nearest.size() > state) {
165 return qstrdup(nearest[state].keyword.toUtf8());
166 }
167
168 return rl_filename_completion_function(text, state);
169}
170
171#include "moc_replStates.cpp"
diff --git a/akonadish/repl/replStates.h b/akonadish/repl/replStates.h
new file mode 100644
index 0000000..a0d3f90
--- /dev/null
+++ b/akonadish/repl/replStates.h
@@ -0,0 +1,87 @@
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#pragma once
21
22#include <QState>
23
24class QSocketNotifier;
25
26class ReadState : public QState
27{
28 Q_OBJECT
29
30public:
31 ReadState(QState *parent = 0);
32
33Q_SIGNALS:
34 void command(const QString &command);
35 void exitRequested();
36
37protected:
38 void onEntry(QEvent *event);
39 virtual const char *prompt() const;
40};
41
42class UnfinishedReadState : public ReadState
43{
44 Q_OBJECT
45
46public:
47 UnfinishedReadState(QState *parent = 0);
48
49protected:
50 const char *prompt() const;
51};
52
53class EvalState : public QState
54{
55 Q_OBJECT
56
57public:
58 EvalState(QState *parent = 0);
59
60Q_SIGNALS:
61 void completed();
62 void continueInput();
63 void output(const QString &output);
64
65protected:
66 void onEntry(QEvent *event);
67
68private:
69 void complete();
70
71 QString m_partial;
72};
73
74class PrintState : public QState
75{
76 Q_OBJECT
77
78public:
79 PrintState(QState *parent = 0);
80
81Q_SIGNALS:
82 void completed();
83
84protected:
85 void onEntry(QEvent *event);
86};
87