summaryrefslogtreecommitdiffstats
path: root/akonadi2_cli/repl
diff options
context:
space:
mode:
Diffstat (limited to 'akonadi2_cli/repl')
-rw-r--r--akonadi2_cli/repl/repl.cpp78
-rw-r--r--akonadi2_cli/repl/repl.h34
-rw-r--r--akonadi2_cli/repl/replStates.cpp155
-rw-r--r--akonadi2_cli/repl/replStates.h85
4 files changed, 352 insertions, 0 deletions
diff --git a/akonadi2_cli/repl/repl.cpp b/akonadi2_cli/repl/repl.cpp
new file mode 100644
index 0000000..395661e
--- /dev/null
+++ b/akonadi2_cli/repl/repl.cpp
@@ -0,0 +1,78 @@
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
29#include "replStates.h"
30
31Repl::Repl(QObject *parent)
32 : QStateMachine(parent)
33{
34 // readline history setup
35 using_history();
36 read_history(commandHistoryPath().toLocal8Bit());
37
38 // create all states
39 ReadState *read = new ReadState(this);
40 UnfinishedReadState *unfinishedRead = new UnfinishedReadState(this);
41 EvalState *eval = new EvalState(this);
42 PrintState *print = new PrintState(this);
43 QFinalState *final = new QFinalState(this);
44
45 // connect the transitions
46 read->addTransition(read, SIGNAL(command(QString)), eval);
47 read->addTransition(read, SIGNAL(exitRequested()), final);
48
49 unfinishedRead->addTransition(unfinishedRead, SIGNAL(command(QString)), eval);
50 unfinishedRead->addTransition(unfinishedRead, SIGNAL(exitRequested()), final);
51
52 eval->addTransition(eval, SIGNAL(completed()), read);
53 eval->addTransition(eval, SIGNAL(continueInput()), unfinishedRead);
54 eval->addTransition(eval, SIGNAL(output(QString)), print);
55
56 print->addTransition(print, SIGNAL(completed()), eval);
57
58 setInitialState(read);
59 start();
60}
61
62Repl::~Repl()
63{
64 // readline history writing
65 write_history(commandHistoryPath().toLocal8Bit());
66}
67
68QString Repl::commandHistoryPath()
69{
70 const QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
71 if (!QFile::exists(path)) {
72 QDir dir;
73 dir.mkpath(path);
74 }
75 return path + "/repl_history";
76}
77
78#include "moc_repl.cpp"
diff --git a/akonadi2_cli/repl/repl.h b/akonadi2_cli/repl/repl.h
new file mode 100644
index 0000000..b76c66b
--- /dev/null
+++ b/akonadi2_cli/repl/repl.h
@@ -0,0 +1,34 @@
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 QString commandHistoryPath();
34};
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
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 m_complete(false)
86{
87}
88
89void 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
116PrintState::PrintState(QState *parent)
117 : QState(parent)
118{
119}
120
121void 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
134static QStringList tab_completion_full_state;
135static bool tab_completion_at_root = false;
136
137static 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
144static 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"
diff --git a/akonadi2_cli/repl/replStates.h b/akonadi2_cli/repl/replStates.h
new file mode 100644
index 0000000..a019a37
--- /dev/null
+++ b/akonadi2_cli/repl/replStates.h
@@ -0,0 +1,85 @@
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 bool m_complete;
70};
71
72class PrintState : public QState
73{
74 Q_OBJECT
75
76public:
77 PrintState(QState *parent = 0);
78
79Q_SIGNALS:
80 void completed();
81
82protected:
83 void onEntry(QEvent *event);
84};
85