summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--akonadish/main.cpp99
-rw-r--r--akonadish/state.cpp5
-rw-r--r--akonadish/state.h1
3 files changed, 61 insertions, 44 deletions
diff --git a/akonadish/main.cpp b/akonadish/main.cpp
index f3cbcac..4c00b9b 100644
--- a/akonadish/main.cpp
+++ b/akonadish/main.cpp
@@ -31,71 +31,82 @@
31/* 31/*
32 * modes of operation: 32 * modes of operation:
33 * 33 *
34 * 1. called with no commands: start the REPL and listen for JSON on stin 34 * 1. called with no commands: start the REPL
35 * 2. called with -: listen for JSON on stdin 35 * 2. called with -: listen for commands on stdin
36 * 3. called with commands: try to match to syntx 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
37 */ 38 */
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
39int main(int argc, char *argv[]) 85int main(int argc, char *argv[])
40{ 86{
41 const bool interactive = isatty(fileno(stdin)); 87 const bool interactive = isatty(fileno(stdin));
42 const bool startRepl = (argc == 1) && interactive; 88 const bool startRepl = (argc == 1) && interactive;
43 //TODO: make a json command parse cause that would be awesomesauce 89 //TODO: make a json command parse cause that would be awesomesauce
44 const bool startJsonListener = !startRepl &&
45 (argc == 2 && qstrcmp(argv[1], "-") == 0);
46 const bool fromScript = !startRepl && QFile::exists(argv[1]); 90 const bool fromScript = !startRepl && QFile::exists(argv[1]);
47 91
48 //qDebug() << "state at startup is" << interactive << startRepl << startJsonListener << fromScript; 92 //qDebug() << "state at startup is" << interactive << startRepl << fromScript;
49 93
50 QCoreApplication app(argc, argv); 94 QCoreApplication app(argc, argv);
51 app.setApplicationName(fromScript ? "interactive-app-shell" : argv[0]); 95 app.setApplicationName(fromScript ? "interactive-app-shell" : argv[0]);
52 //app.setApplicationName(argv[0]);
53
54 if (startRepl || startJsonListener) {
55 if (startRepl) {
56 Repl *repl = new Repl;
57 QObject::connect(repl, &QStateMachine::finished,
58 repl, &QObject::deleteLater);
59 QObject::connect(repl, &QStateMachine::finished,
60 &app, &QCoreApplication::quit);
61 }
62
63 if (startJsonListener) {
64// JsonListener listener(syntax);
65 }
66 96
67 State::setHasEventLoop(true); 97 if (startRepl) {
68 return app.exec(); 98 return enterRepl();
69 } else if (fromScript) { 99 } else if (fromScript) {
70 QFile f(argv[1]); 100 QFile f(argv[1]);
71 if (!f.open(QIODevice::ReadOnly)) { 101 if (!f.open(QIODevice::ReadOnly)) {
72 return 1; 102 return 1;
73 } 103 }
74 104
75 QString line = f.readLine(); 105 QTextStream inputStream(&f);
76 while (!line.isEmpty()) { 106 processCommandStream(inputStream);
77 line = line.trimmed();
78
79 if (!line.isEmpty() && !line.startsWith('#')) {
80 SyntaxTree::self()->run(SyntaxTree::tokenize(line));
81 }
82
83 line = f.readLine();
84 }
85 exit(0);
86 } else if (!interactive) { 107 } else if (!interactive) {
87 QTextStream inputStream(stdin); 108 QTextStream inputStream(stdin);
88 109 processCommandStream(inputStream);
89 QString line = inputStream.readLine();
90 while (!line.isEmpty()) {
91 line = line.trimmed();
92
93 if (!line.isEmpty() && !line.startsWith('#')) {
94 SyntaxTree::self()->run(SyntaxTree::tokenize(line));
95 }
96
97 line = inputStream.readLine();
98 }
99 } else { 110 } else {
100 QStringList commands = app.arguments(); 111 QStringList commands = app.arguments();
101 commands.removeFirst(); 112 commands.removeFirst();
diff --git a/akonadish/state.cpp b/akonadish/state.cpp
index dbd5952..9fb5bcc 100644
--- a/akonadish/state.cpp
+++ b/akonadish/state.cpp
@@ -114,6 +114,11 @@ void State::setHasEventLoop(bool evented)
114 s_hasEventLoop = evented; 114 s_hasEventLoop = evented;
115} 115}
116 116
117bool State::hasEventLoop()
118{
119 return s_hasEventLoop;
120}
121
117void State::setCommandTiming(bool time) 122void State::setCommandTiming(bool time)
118{ 123{
119 d->timing = time; 124 d->timing = time;
diff --git a/akonadish/state.h b/akonadish/state.h
index 543b063..3c4c2c7 100644
--- a/akonadish/state.h
+++ b/akonadish/state.h
@@ -43,6 +43,7 @@ public:
43 QString loggingLevel() const; 43 QString loggingLevel() const;
44 44
45 static void setHasEventLoop(bool evented); 45 static void setHasEventLoop(bool evented);
46 static bool hasEventLoop();
46 47
47private: 48private:
48 class Private; 49 class Private;