From ec380cc4ac09714c91a0b506563fac79c8b6b339 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Sun, 10 Jan 2016 11:55:38 +0100 Subject: factor out the common multiline input routine --- akonadish/main.cpp | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) (limited to 'akonadish/main.cpp') diff --git a/akonadish/main.cpp b/akonadish/main.cpp index f3cbcac..ba8be96 100644 --- a/akonadish/main.cpp +++ b/akonadish/main.cpp @@ -36,6 +36,20 @@ * 3. called with commands: try to match to syntx */ +void processCommandStream(QTextStream &stream) +{ + QString line = stream.readLine(); + while (!line.isEmpty()) { + line = line.trimmed(); + + if (!line.isEmpty() && !line.startsWith('#')) { + SyntaxTree::self()->run(SyntaxTree::tokenize(line)); + } + + line = stream.readLine(); + } +} + int main(int argc, char *argv[]) { const bool interactive = isatty(fileno(stdin)); @@ -72,30 +86,11 @@ int main(int argc, char *argv[]) return 1; } - QString line = f.readLine(); - while (!line.isEmpty()) { - line = line.trimmed(); - - if (!line.isEmpty() && !line.startsWith('#')) { - SyntaxTree::self()->run(SyntaxTree::tokenize(line)); - } - - line = f.readLine(); - } - exit(0); + QTextStream inputStream(&f); + processCommandStream(inputStream); } else if (!interactive) { QTextStream inputStream(stdin); - - QString line = inputStream.readLine(); - while (!line.isEmpty()) { - line = line.trimmed(); - - if (!line.isEmpty() && !line.startsWith('#')) { - SyntaxTree::self()->run(SyntaxTree::tokenize(line)); - } - - line = inputStream.readLine(); - } + processCommandStream(inputStream); } else { QStringList commands = app.arguments(); commands.removeFirst(); -- cgit v1.2.3 From 3a34c76f5c4df491b7b6bf1100690284c6b27985 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Sun, 10 Jan 2016 11:59:38 +0100 Subject: factor out and simplify the repl entrance branch --- akonadish/main.cpp | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'akonadish/main.cpp') diff --git a/akonadish/main.cpp b/akonadish/main.cpp index ba8be96..139b28f 100644 --- a/akonadish/main.cpp +++ b/akonadish/main.cpp @@ -36,6 +36,20 @@ * 3. called with commands: try to match to syntx */ +int enterRepl() +{ + Repl *repl = new Repl; + QObject::connect(repl, &QStateMachine::finished, + repl, &QObject::deleteLater); + QObject::connect(repl, &QStateMachine::finished, + QCoreApplication::instance(), &QCoreApplication::quit); + + State::setHasEventLoop(true); + int rv = QCoreApplication::instance()->exec(); + State::setHasEventLoop(false); + return rv; +} + void processCommandStream(QTextStream &stream) { QString line = stream.readLine(); @@ -55,31 +69,15 @@ int main(int argc, char *argv[]) const bool interactive = isatty(fileno(stdin)); const bool startRepl = (argc == 1) && interactive; //TODO: make a json command parse cause that would be awesomesauce - const bool startJsonListener = !startRepl && - (argc == 2 && qstrcmp(argv[1], "-") == 0); const bool fromScript = !startRepl && QFile::exists(argv[1]); - //qDebug() << "state at startup is" << interactive << startRepl << startJsonListener << fromScript; + //qDebug() << "state at startup is" << interactive << startRepl << fromScript; QCoreApplication app(argc, argv); app.setApplicationName(fromScript ? "interactive-app-shell" : argv[0]); - //app.setApplicationName(argv[0]); - - if (startRepl || startJsonListener) { - if (startRepl) { - Repl *repl = new Repl; - QObject::connect(repl, &QStateMachine::finished, - repl, &QObject::deleteLater); - QObject::connect(repl, &QStateMachine::finished, - &app, &QCoreApplication::quit); - } - - if (startJsonListener) { -// JsonListener listener(syntax); - } - State::setHasEventLoop(true); - return app.exec(); + if (startRepl) { + return enterRepl(); } else if (fromScript) { QFile f(argv[1]); if (!f.open(QIODevice::ReadOnly)) { -- cgit v1.2.3 From 430672a394c563d23c6ef4fec47accf8de1bccab Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Sun, 10 Jan 2016 12:00:36 +0100 Subject: update for accuracy --- akonadish/main.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'akonadish/main.cpp') diff --git a/akonadish/main.cpp b/akonadish/main.cpp index 139b28f..af85a94 100644 --- a/akonadish/main.cpp +++ b/akonadish/main.cpp @@ -31,9 +31,10 @@ /* * modes of operation: * - * 1. called with no commands: start the REPL and listen for JSON on stin - * 2. called with -: listen for JSON on stdin - * 3. called with commands: try to match to syntx + * 1. called with no commands: start the REPL + * 2. called with -: listen for commands on stdin + * 3. called with a filename: try to run it as a script + * 4. called with commands: try to match to syntax and run the result */ int enterRepl() -- cgit v1.2.3 From 378230eaa955d6915ad2a1b7792855595b598e05 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Sun, 10 Jan 2016 12:09:30 +0100 Subject: make it possible to enter itneractive mode from a script use case -> you want to set the default log and debug levels away from the defaults and then go into interactive mode: set logging warning go_interactive --- akonadish/main.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'akonadish/main.cpp') diff --git a/akonadish/main.cpp b/akonadish/main.cpp index af85a94..4c00b9b 100644 --- a/akonadish/main.cpp +++ b/akonadish/main.cpp @@ -39,6 +39,10 @@ int enterRepl() { + if (State::hasEventLoop()) { + return 0; + } + Repl *repl = new Repl; QObject::connect(repl, &QStateMachine::finished, repl, &QObject::deleteLater); @@ -51,8 +55,21 @@ int enterRepl() return rv; } +bool goInteractive(const QStringList &, State &) +{ + enterRepl(); + return true; +} + +Syntax::List goInteractiveSyntax() +{ + Syntax interactive("go_interactive", QString(), &goInteractive); + return Syntax::List() << interactive; +} + void processCommandStream(QTextStream &stream) { + SyntaxTree::self()->registerSyntax(&goInteractiveSyntax); QString line = stream.readLine(); while (!line.isEmpty()) { line = line.trimmed(); -- cgit v1.2.3