summaryrefslogtreecommitdiffstats
path: root/sinksh/main.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-20 19:07:07 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-20 19:07:07 +0100
commitbdb01c2c068df326f5a8328ed1492ab1bea388c5 (patch)
tree25c2ee1b29bc481b6914c244ed9ca194b1415d16 /sinksh/main.cpp
parent17e7ee40c9185c0505883853345fd6024c675b1a (diff)
downloadsink-bdb01c2c068df326f5a8328ed1492ab1bea388c5.tar.gz
sink-bdb01c2c068df326f5a8328ed1492ab1bea388c5.zip
Renamed Akonadi2 to Sink
(except for documentation).
Diffstat (limited to 'sinksh/main.cpp')
-rw-r--r--sinksh/main.cpp115
1 files changed, 115 insertions, 0 deletions
diff --git a/sinksh/main.cpp b/sinksh/main.cpp
new file mode 100644
index 0000000..4c00b9b
--- /dev/null
+++ b/sinksh/main.cpp
@@ -0,0 +1,115 @@
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 <unistd.h>
21
22#include <QCoreApplication>
23#include <QDebug>
24#include <QFile>
25#include <QTextStream>
26
27#include "syntaxtree.h"
28// #include "jsonlistener.h"
29#include "repl/repl.h"
30
31/*
32 * modes of operation:
33 *
34 * 1. called with no commands: start the REPL
35 * 2. called with -: listen for commands on stdin
36 * 3. called with a filename: try to run it as a script
37 * 4. called with commands: try to match to syntax and run the result
38 */
39
40int enterRepl()
41{
42 if (State::hasEventLoop()) {
43 return 0;
44 }
45
46 Repl *repl = new Repl;
47 QObject::connect(repl, &QStateMachine::finished,
48 repl, &QObject::deleteLater);
49 QObject::connect(repl, &QStateMachine::finished,
50 QCoreApplication::instance(), &QCoreApplication::quit);
51
52 State::setHasEventLoop(true);
53 int rv = QCoreApplication::instance()->exec();
54 State::setHasEventLoop(false);
55 return rv;
56}
57
58bool goInteractive(const QStringList &, State &)
59{
60 enterRepl();
61 return true;
62}
63
64Syntax::List goInteractiveSyntax()
65{
66 Syntax interactive("go_interactive", QString(), &goInteractive);
67 return Syntax::List() << interactive;
68}
69
70void processCommandStream(QTextStream &stream)
71{
72 SyntaxTree::self()->registerSyntax(&goInteractiveSyntax);
73 QString line = stream.readLine();
74 while (!line.isEmpty()) {
75 line = line.trimmed();
76
77 if (!line.isEmpty() && !line.startsWith('#')) {
78 SyntaxTree::self()->run(SyntaxTree::tokenize(line));
79 }
80
81 line = stream.readLine();
82 }
83}
84
85int main(int argc, char *argv[])
86{
87 const bool interactive = isatty(fileno(stdin));
88 const bool startRepl = (argc == 1) && interactive;
89 //TODO: make a json command parse cause that would be awesomesauce
90 const bool fromScript = !startRepl && QFile::exists(argv[1]);
91
92 //qDebug() << "state at startup is" << interactive << startRepl << fromScript;
93
94 QCoreApplication app(argc, argv);
95 app.setApplicationName(fromScript ? "interactive-app-shell" : argv[0]);
96
97 if (startRepl) {
98 return enterRepl();
99 } else if (fromScript) {
100 QFile f(argv[1]);
101 if (!f.open(QIODevice::ReadOnly)) {
102 return 1;
103 }
104
105 QTextStream inputStream(&f);
106 processCommandStream(inputStream);
107 } else if (!interactive) {
108 QTextStream inputStream(stdin);
109 processCommandStream(inputStream);
110 } else {
111 QStringList commands = app.arguments();
112 commands.removeFirst();
113 return SyntaxTree::self()->run(commands);
114 }
115}