File indexing completed on 2024-11-24 04:52:45

0001 /*
0002  * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 
0020 #include "repl.h"
0021 
0022 #include <QDir>
0023 #include <QFile>
0024 #include <QFinalState>
0025 #include <QStandardPaths>
0026 #include <QTextStream>
0027 
0028 #include "replStates.h"
0029 #include "syntaxtree.h"
0030 #include "commandline.h"
0031 
0032 Repl::Repl(QObject *parent)
0033     : QStateMachine(parent)
0034 {
0035     Commandline::loadHistory(commandHistoryPath());
0036 
0037     // create all states
0038     ReadState *read = new ReadState(this);
0039     UnfinishedReadState *unfinishedRead = new UnfinishedReadState(this);
0040     EvalState *eval = new EvalState(this);
0041     PrintState *print = new PrintState(this);
0042     QFinalState *final = new QFinalState(this);
0043 
0044     // connect the transitions
0045     read->addTransition(read, SIGNAL(command(QString)), eval);
0046     read->addTransition(read, SIGNAL(exitRequested()), final);
0047 
0048     unfinishedRead->addTransition(unfinishedRead, SIGNAL(command(QString)), eval);
0049     unfinishedRead->addTransition(unfinishedRead, SIGNAL(exitRequested()), final);
0050 
0051     eval->addTransition(eval, SIGNAL(completed()), read);
0052     eval->addTransition(eval, SIGNAL(continueInput()), unfinishedRead);
0053     eval->addTransition(eval, SIGNAL(output(QString)), print);
0054 
0055     print->addTransition(print, SIGNAL(completed()), eval);
0056 
0057     setInitialState(read);
0058     printWelcomeBanner();
0059     start();
0060 }
0061 
0062 Repl::~Repl()
0063 {
0064     Commandline::saveHistory(commandHistoryPath());
0065 }
0066 
0067 void Repl::printWelcomeBanner()
0068 {
0069     QTextStream out(stdout);
0070     out << QObject::tr("Welcome to the Sink interative shell!\n");
0071     out << QObject::tr("Type `help` for information on the available commands.\n");
0072     out.flush();
0073 }
0074 
0075 QString Repl::commandHistoryPath()
0076 {
0077     const QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
0078 
0079     if (!QFile::exists(path)) {
0080         QDir dir;
0081         dir.mkpath(path);
0082     }
0083 
0084     return  path + "/repl_history";
0085 }
0086 
0087 #include "moc_repl.cpp"