diff options
Diffstat (limited to 'sinksh/repl/repl.cpp')
-rw-r--r-- | sinksh/repl/repl.cpp | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/sinksh/repl/repl.cpp b/sinksh/repl/repl.cpp new file mode 100644 index 0000000..21209fc --- /dev/null +++ b/sinksh/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 | |||
33 | Repl::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 | |||
65 | Repl::~Repl() | ||
66 | { | ||
67 | // readline history writing | ||
68 | write_history(commandHistoryPath().toLocal8Bit()); | ||
69 | } | ||
70 | |||
71 | void Repl::printWelcomeBanner() | ||
72 | { | ||
73 | QTextStream out(stdout); | ||
74 | out << QObject::tr("Welcome to the Sink interative shell!\n"); | ||
75 | out << QObject::tr("Type `help` for information on the available commands.\n"); | ||
76 | out.flush(); | ||
77 | } | ||
78 | |||
79 | QString 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" | ||