diff options
-rw-r--r-- | akonadish/main.cpp | 99 | ||||
-rw-r--r-- | akonadish/state.cpp | 5 | ||||
-rw-r--r-- | akonadish/state.h | 1 |
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 | ||
40 | int 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 | |||
58 | bool goInteractive(const QStringList &, State &) | ||
59 | { | ||
60 | enterRepl(); | ||
61 | return true; | ||
62 | } | ||
63 | |||
64 | Syntax::List goInteractiveSyntax() | ||
65 | { | ||
66 | Syntax interactive("go_interactive", QString(), &goInteractive); | ||
67 | return Syntax::List() << interactive; | ||
68 | } | ||
69 | |||
70 | void 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 | |||
39 | int main(int argc, char *argv[]) | 85 | int 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 f3f5975..85cbe10 100644 --- a/akonadish/state.cpp +++ b/akonadish/state.cpp | |||
@@ -112,6 +112,11 @@ void State::setHasEventLoop(bool evented) | |||
112 | s_hasEventLoop = evented; | 112 | s_hasEventLoop = evented; |
113 | } | 113 | } |
114 | 114 | ||
115 | bool State::hasEventLoop() | ||
116 | { | ||
117 | return s_hasEventLoop; | ||
118 | } | ||
119 | |||
115 | void State::setCommandTiming(bool time) | 120 | void State::setCommandTiming(bool time) |
116 | { | 121 | { |
117 | d->timing = time; | 122 | d->timing = time; |
diff --git a/akonadish/state.h b/akonadish/state.h index 9c1ab6f..b1ca358 100644 --- a/akonadish/state.h +++ b/akonadish/state.h | |||
@@ -40,6 +40,7 @@ public: | |||
40 | void commandFinished(int returnCode = 0) const; | 40 | void commandFinished(int returnCode = 0) const; |
41 | 41 | ||
42 | static void setHasEventLoop(bool evented); | 42 | static void setHasEventLoop(bool evented); |
43 | static bool hasEventLoop(); | ||
43 | 44 | ||
44 | private: | 45 | private: |
45 | class Private; | 46 | class Private; |