Пример #1
0
int main ( int argc, char **argv )
{
	 QLocale::setDefault(QLocale::C); // This isn't a UI, we want things to be
	                                  // consistant across compiles, environment
	                                  // independant.

	QxtCommandOptions opt;
	opt.add("output", "Where to write the output file to.", QxtCommandOptions::Required);
	opt.alias("output", "o");
	opt.add("help", "Display this text.");
	opt.alias("help", "h");
	opt.parse(argc, argv);
	if ( opt.count("help") || opt.showUnrecognizedWarning() ) usage(&opt);

	QStringList pos = opt.positional();
	if ( pos.length() < 1 )
	{
		cerr << "Error: No source files." << endl;
		usage(&opt); // No files.
	}
	if ( pos.length() > 1 )
	{
		if (opt.count("output")) // Multiple files and only one output.
		{
			cerr << "Error: Multiple source files but output file given." << endl;
			usage(&opt); // No files.
		}

		for ( QStringList::Iterator i = pos.begin(); i != pos.end(); i++ )
		{
			system((QString(argv[0])+" '"+*i+"'").toStdString().c_str());
		}
		exit(0); // Our work here is done.
	}

	QFile src(pos[0]);
	if (!src.open(QIODevice::ReadOnly))
	{
		cerr << "Error: could not open source file." << endl;
		exit(EX_IOERR);
	}
	SmartBuffer b(&src);

	if ( b.look(2) == "#!" )      // Skip the hashbang.
		while ( b.pop() != '\n' ) //
			;

	Token::List tl = Token::tokenize(&b);
	qDebug() << tl;
	AST::List ast = AST::parse(tl);
	qDebug() << ast;

	//Module *mod = Module::parse(&b);

	cerr << "SUCCESS!" << endl;

	return 0;
}
Пример #2
0
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    a.setApplicationName("Jupiter Reporter");
    a.setOrganizationName("XE-QT Solutions");
    a.setOrganizationDomain("xe-qt-solutions.com");

    QxtCommandOptions options;
    options.add("version", "show version number and build date");
    options.alias("version", "ver");
    options.add("help", "show this help text");

    options.addSection("printing and PDF");
    options.add("print", "print a report to a specific printer (blank for default)", QxtCommandOptions::ValueOptional);
    options.add("pdf", "save a report as a PDF file", QxtCommandOptions::ValueRequired);

    options.addSection("email");
    options.add("to", "list of email recipients", QxtCommandOptions::ValueRequired);
    options.add("cc", "list of email cc recipients", QxtCommandOptions::ValueRequired);
    options.add("subject", "the email subject", QxtCommandOptions::ValueRequired);
    options.add("body", "the body of the email message", QxtCommandOptions::ValueRequired);
    options.add("server", "the email smtp server", QxtCommandOptions::ValueRequired);
    options.add("port", "the smtp server port", QxtCommandOptions::ValueRequired);
    options.add("ssl", "use a secure socket when sending email");
    options.add("account", "the name of the smtp account", QxtCommandOptions::ValueRequired);
    options.add("password", "the smtp account password", QxtCommandOptions::ValueRequired);
    options.add("from", "the email address of the email sender", QxtCommandOptions::ValueRequired);

    options.addSection("licensing");
    options.add("lic", "display license details");
    options.add("name", "the licensee name", QxtCommandOptions::ValueRequired);
    options.add("email", "the licensee email address", QxtCommandOptions::ValueRequired);
    options.add("expires", "the license expiry date (YYYY-MM-DD format)", QxtCommandOptions::ValueRequired);
    options.add("key", "the activation key to unlock advanced features", QxtCommandOptions::ValueRequired);
    options.parse(a.arguments());

    if (options.count("version"))
    {
	std::cout << "Jupiter Reporter v" << VER_MAJOR << "." << VER_MINOR << ", built on " << __DATE__ << " at " << __TIME__ << std::endl;
	std::cout << "Copyright (c) 2010 XE-QT Solutions Ltd., All rights reserved." << std::endl;
	return 0;
    }

    QSettings settings(QString("%1/license.ini").arg(QDesktopServices::storageLocation(QDesktopServices::DataLocation)), QSettings::IniFormat);

    // Generate license?
    if (options.count("name") || options.count("email") || options.count("expires"))
    {
	const QString name = options.value("name").toString();
	const QString email = options.value("email").toString();
	const QString expires = options.value("expires").toString();

	if (name.isEmpty() || email.isEmpty() || expires.isEmpty())
	{
	    options.showUsage();
	    return 0;
	}

	settings.setValue("License/name", name);
	settings.setValue("License/email", email);
	settings.setValue("License/expires", expires);
	settings.setValue("License/license", NodeLockedLicense::licenseFile(name, email, QDate::fromString(expires, Qt::ISODate)));

	std::cout << "Generated " << qPrintable(settings.fileName()) << std::endl;

	return 0;
    }

    if (options.count("key"))
    {
	settings.setValue("License/key", options.value("key").toString());
	settings.remove("License/license");
	std::cout << "Updated " << qPrintable(settings.fileName()) << std::endl;
	return 0;
    }

    // Read license
    const QString name = settings.value("License/name").toString();
    const QString email = settings.value("License/email").toString();
    const QDate expires = QDate::fromString(settings.value("License/expires").toString(), Qt::ISODate);
    const QString key = settings.value("License/key").toString();

    bool isLicensed = NodeLockedLicense::isValid(name, email, expires, key);

    if (options.count("lic"))
    {
	std::cout << "License: " << qPrintable(settings.fileName()) << std::endl;

	if (!isLicensed)
	{
	    std::cout << "No valid license exists.  Advanced functionality will be disabled." << std::endl;
	}
	else
	{
	    std::cout << "Licensed to " << qPrintable(name) << " (" << qPrintable(email) << "), expires on " << qPrintable(expires.toString()) << std::endl;
	}
	return 0;
    }

    if (options.positional().isEmpty() || options.count("help") || options.showUnrecognizedWarning() ||
	(options.count("print") == 0 && options.count("pdf") == 0 && options.count("to") == 0))
    {
	options.showUsage();
	return 0;
    }

    const QString fileName = options.positional().first();
    Q_ASSERT(fileName != "");

    if (options.count("print"))
    {
	Print print;
	QObject::connect(&print, SIGNAL(finished()), &a, SLOT(quit()));
	print.print(fileName, options.value("print").toString());
	return a.exec();
    }

    if (options.count("pdf"))
    {
	if (isLicensed)
	{
	    Pdf pdf;
	    QObject::connect(&pdf, SIGNAL(finished()), &a, SLOT(quit()));
	    pdf.create(fileName, options.value("pdf").toString());
	    return a.exec();
	}
	else
	{
	    std::cout << "PDF support requires a license!" << std::endl;
	    return 0;
	}
    }

    if (options.count("to") || options.count("cc"))
    {
	if (isLicensed)
	{
	    Email email;
	    email.send(fileName, parseSendSettings(options), parseEmailSettings(options));
	    QObject::connect(&email, SIGNAL(finished()), &a, SLOT(quit()));
	    return a.exec();
	}
	else
	{
	    std::cout << "Email support requires a license!" << std::endl;
	    return 0;
	}
    }

    return 0;
}
Пример #3
0
int main(int argc, char **argv)
{
  QxtCommandOptions options;

  options.add(CL_HELP, "display this help message",
      QxtCommandOptions::NoValue);
  options.add(CL_NKEYS, "number of keys to generate",
      QxtCommandOptions::ValueRequired);
  options.add(CL_PUBDIR, "directory in which to put public keys (default=./keys/pub)",
      QxtCommandOptions::ValueRequired);
  options.add(CL_PRIVDIR, "directory in which to put private keys (default=./keys/priv)",
      QxtCommandOptions::ValueRequired);
  options.add(CL_KEYTYPE, "specify the key type (default=dsa, options=dsa|rsa)",
      QxtCommandOptions::ValueRequired);
  options.add(CL_LIB, "specify the library (default=cryptopp, options=cryptopp)",
      QxtCommandOptions::ValueRequired);
  options.add(CL_RAND, "specify the base properties for the key (default=NULL)",
      QxtCommandOptions::ValueRequired);
  options.add(CL_DEBUG, "enable debugging",
      QxtCommandOptions::NoValue);

  options.parse(argc, argv);

  if(options.count(CL_HELP) || options.showUnrecognizedWarning()) {
    options.showUsage();
    return -1;
  }

  QMultiHash<QString, QVariant> params = options.parameters();

  int key_count = params.value(CL_NKEYS, 1).toInt();
  if(key_count < 1) {
    ExitWithWarning(options, "Invalid nkeys");
  }

  QString pubdir_path = params.value(CL_PUBDIR, DEFAULT_PUBDIR).toString();
  QDir pubdir(pubdir_path);
  if(!pubdir.exists()) {
    pubdir.mkpath(".");
  }

  if(!pubdir.exists()) {
    ExitWithWarning(options, "Unable to create pubdir");
  }

  QString privdir_path = params.value(CL_PRIVDIR, DEFAULT_PRIVDIR).toString();
  QDir privdir(privdir_path);
  if(!privdir.exists()) {
    privdir.mkpath(".");
  }

  if(!privdir.exists()) {
    ExitWithWarning(options, "Unable to create privdir");
  }

  if(params.contains(CL_DEBUG)) {
    Logging::UseStderr();
  }

  QString lib_name = params.value(CL_LIB, "cryptopp").toString();
  QString key = params.value(CL_KEYTYPE, "dsa").toString();

  CryptoFactory &cf = CryptoFactory::GetInstance();
  QSharedPointer<CreateKey> ck(new CreateKey());
  if(lib_name == "cryptopp") {
    if(key == "dsa") {
      cf.SetLibrary(CryptoFactory::CryptoPPDsa);
      if(params.contains(CL_RAND)) {
        ck = QSharedPointer<CreateKey>(
            new CreateSeededDsaKey(params.value(CL_RAND).toString()));
      }
    } else if (key == "rsa") {
      cf.SetLibrary(CryptoFactory::CryptoPP);
    } else {
      ExitWithWarning(options, "Invalid key type");
    }
  } else {
    ExitWithWarning(options, "Invalid library");
  }

  Library *lib = cf.GetLibrary();
  QSharedPointer<Hash> hash(lib->GetHashAlgorithm());

  int count = 0;
  while(count < key_count) {
    QSharedPointer<AsymmetricKey> key((*ck)());
    QSharedPointer<AsymmetricKey> pubkey(key->GetPublicKey());
    QByteArray hvalue = hash->ComputeHash(pubkey->GetByteArray());
    QString id = Integer(hvalue).ToString();

    if(!key->Save(privdir_path + QDir::separator() + id)) {
      qFatal("Could not save private key");
    }

    if(!pubkey->Save(pubdir_path + QDir::separator() + id + ".pub")) {
      qFatal("Could not save private key");
    }

    count++;
  }

  return 0;
}
Пример #4
0
void ExitWithWarning(const QxtCommandOptions &options, const char* warning)
{
  std::cerr << "Error: " << warning << std::endl;
  options.showUsage();
  exit(-1);
}
Пример #5
0
bool consoleApplication(QCoreApplication *pApp, int *pRes)
{
    *pRes = 0;   // By default, everything is fine

    // Specify the type of command line options that are allowed
    // Note #1: we don't rely on the QxtCommandOptions::showUsage() method
    // Note #2: we don't distinguish between Windows and Linux / OS X when it
    //          comes to the formatting of the command line options

    QxtCommandOptions cmdLineOptions;

    cmdLineOptions.setFlagStyle(QxtCommandOptions::DoubleDash);
    cmdLineOptions.setParamStyle(QxtCommandOptions::SpaceAndEquals);

    cmdLineOptions.add("help");
    cmdLineOptions.alias("help", "h");

    cmdLineOptions.add("about");
    cmdLineOptions.alias("about", "a");

    cmdLineOptions.add("version");
    cmdLineOptions.alias("version", "v");

    // Parse the command line options

    cmdLineOptions.parse(pApp->arguments());

    // See what needs doing with the command line options, if anything

    if (cmdLineOptions.count("help")) {
        // The user wants to know how to use OpenCOR from the console, so...

        usage(pApp);

        return true;
    }
    else if (cmdLineOptions.count("about")) {
        // The user wants to know how to use OpenCOR from the console, so...

        about(pApp);

        return true;
    } else if (cmdLineOptions.count("version")) {
        // The user wants to know the version of OpenCOR this is, so...

        version(pApp);

        return true;
    } else if (cmdLineOptions.getUnrecognizedWarning().count()) {
        // The user provided OpenCOR with wrong command line options, so...

        usage(pApp);

        *pRes = -1;

        return true;
    } else {
        // The user didn't provide any command line options that requires
        // running OpenCOR as a console application, so...

        return false;
    }
}
Пример #6
0
int main(int argc, char **argv)
{
#if (QT_VERSION < 0x050200)
        #error("You need Qt 5.2.0 or later to compile Actiona Executer");
#endif

#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
    QtSingleApplication app("actiona-exec", argc, argv);
#else
    ActionTools::NativeEventFilteringApplication app("actiona-exec", argc, argv);
#endif
	app.setQuitOnLastWindowClosed(false);

	qAddPostRoutine(cleanup);

	qsrand(std::time(NULL));

#ifdef Q_OS_LINUX
    notify_init("Actiona executer");
#endif

#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
#endif

    QxtCommandOptions preOptions;

    preOptions.add("portable", QObject::tr("starts in portable mode, storing the settings in the executable folder"));
    preOptions.alias("portable", "p");
    preOptions.parse(QCoreApplication::arguments());

    if(preOptions.count("portable") > 0)
    {
        QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, QApplication::applicationDirPath() + "/userSettings");
        QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, QApplication::applicationDirPath() + "/systemSettings");
        QSettings::setDefaultFormat(QSettings::IniFormat);
    }

    QString locale = Tools::locale();

    Tools::installQtTranslator(locale);
    Tools::installTranslator("tools", locale);
    Tools::installTranslator("actiontools", locale);
    Tools::installTranslator("executer", locale);
    Tools::installTranslator("actexecuter", locale);

    const QStringList &arguments = QCoreApplication::arguments();

    QxtCommandOptions options;
    options.setFlagStyle(QxtCommandOptions::DoubleDash);
    options.setScreenWidth(0);
    options.add("code", QObject::tr("switch to code mode, may not be used with -s"));
    options.alias("code", "c");
    options.add("script", QObject::tr("switch to script mode, may not be used with -c"));
    options.alias("script", "s");
    options.add("nocodeqt", QObject::tr("do not include the Qt library into the code"));
    options.alias("nocodeqt", "Q");
    options.add("portable", QObject::tr("starts in portable mode, storing the settings in the executable folder"));
    options.alias("portable", "p");
    options.add("proxy-mode", QObject::tr("sets the proxy mode, values are \"none\", \"system\" (default) or \"custom\""));
    options.add("proxy-type", QObject::tr("sets the custom proxy type, values are \"http\" or \"socks\" (default)"));
    options.add("proxy-host", QObject::tr("sets the custom proxy host"));
    options.add("proxy-port", QObject::tr("sets the custom proxy port"));
    options.add("proxy-user", QObject::tr("sets the custom proxy user"));
    options.add("proxy-password", QObject::tr("sets the custom proxy password"));
#ifdef Q_OS_WIN
    options.add("console", QObject::tr("create a console to see debug output"));
    options.add("pause-at-end", QObject::tr("wait for user input at the end of the execution, used only with --console"));
#endif
    options.add("version", QObject::tr("show the program version"));
    options.alias("version", "v");
    options.add("help", QObject::tr("show this help text"));
    options.alias("help", "h");
    options.parse(arguments);

#ifdef Q_OS_WIN
    if(options.count("console"))
    {
        createConsole();

        if(options.count("pause-at-end"))
            qAddPostRoutine(pause);
    }
#endif

	qRegisterMetaType<ActionTools::ActionInstance>("ActionInstance");
	qRegisterMetaType<ActionTools::ActionException::Exception>("Exception");
	qRegisterMetaType<ActionTools::Parameter>("Parameter");
	qRegisterMetaType<ActionTools::SubParameter>("SubParameter");
	qRegisterMetaType<Tools::Version>("Version");

	qRegisterMetaTypeStreamOperators<ActionTools::ActionInstance>("ActionInstance");
	qRegisterMetaTypeStreamOperators<ActionTools::Parameter>("Parameter");
	qRegisterMetaTypeStreamOperators<ActionTools::SubParameter>("SubParameter");
	qRegisterMetaTypeStreamOperators<Tools::Version>("Version");

	if(options.count("version"))
	{
		QTextStream stream(stdout);
        stream << "Actiona Executer version " << Global::ACTIONA_VERSION.toString() << ", script version " << Global::SCRIPT_VERSION.toString() << "\n";
		stream.flush();
		return 0;
	}
	if(options.count("help") || options.showUnrecognizedWarning() || options.positional().count() < 1 || (options.count("code") && options.count("script")))
	{
		QTextStream stream(stdout);
		stream << QObject::tr("usage: ") << QCoreApplication::arguments().at(0) << " " << QObject::tr("[parameters]") << " " << QObject::tr("filename") << "\n";
		stream << QObject::tr("Parameters are:") << "\n";
		stream << options.getUsage();
		stream.flush();
		return -1;
	}

	app.addLibraryPath(QApplication::applicationDirPath() + "/actions");
	app.addLibraryPath(QApplication::applicationDirPath() + "/plugins");

	if(!options.count("nocodeqt"))
		app.addLibraryPath(QApplication::applicationDirPath() + "/code");

#ifdef Q_OS_LINUX
	{
#ifdef ACT_PROFILE
		Tools::HighResolutionTimer timer("Load key codes");
#endif
		ActionTools::KeySymHelper::loadKeyCodes();
	}
#endif

	// Proxy settings
	int proxyMode = ActionTools::Settings::PROXY_SYSTEM;
	if(options.value("proxy-mode").toString() == "none")
		proxyMode = ActionTools::Settings::PROXY_NONE;
	else if(options.value("proxy-mode").toString() == "custom")
		proxyMode = ActionTools::Settings::PROXY_CUSTOM;
	else if(options.value("proxy-mode").toString() == "system")
		proxyMode = ActionTools::Settings::PROXY_SYSTEM;
	else if(!options.value("proxy-mode").toString().isEmpty())
	{
		QTextStream stream(stdout);
		stream << QObject::tr("Unknown proxy mode, values are \"none\", \"system\" (default) or \"custom\"") << "\n";
		stream.flush();
		return -1;
	}

	QNetworkProxy proxy;

	switch(proxyMode)
	{
	case ActionTools::Settings::PROXY_NONE:
		proxy.setType(QNetworkProxy::NoProxy);
		break;
	case ActionTools::Settings::PROXY_SYSTEM:
		{
			QUrl url(Global::CONNECTIVITY_URL);
			QNetworkProxyQuery networkProxyQuery(url);
			QList<QNetworkProxy> listOfProxies = QNetworkProxyFactory::systemProxyForQuery(networkProxyQuery);
			if(!listOfProxies.isEmpty())
				proxy = listOfProxies.first();
			else
				proxy.setType(QNetworkProxy::NoProxy);
		}
		break;
	case ActionTools::Settings::PROXY_CUSTOM:
		{
			int type = ActionTools::Settings::PROXY_TYPE_SOCKS5;
			if(options.value("proxy-type").toString() == "http")
				type = ActionTools::Settings::PROXY_TYPE_HTTP;
			else if(options.value("proxy-type").toString() == "socks")
				type = ActionTools::Settings::PROXY_TYPE_SOCKS5;
			else if(!options.value("proxy-type").toString().isEmpty())
			{
				QTextStream stream(stdout);
				stream << QObject::tr("Unknown proxy type, values are \"http\" or \"socks\" (default)") << "\n";
				stream.flush();
				return -1;
			}

			QNetworkProxy proxy;

			if(type == ActionTools::Settings::PROXY_TYPE_HTTP)
				proxy.setType(QNetworkProxy::HttpProxy);
			else
				proxy.setType(QNetworkProxy::Socks5Proxy);

			proxy.setHostName(options.value("proxy-host").toString());
			proxy.setPort(options.value("proxy-port").toInt());
			proxy.setUser(options.value("proxy-user").toString());
			proxy.setPassword(options.value("proxy-password").toString());
		}
		break;
	}

	QNetworkProxy::setApplicationProxy(proxy);

	QUrl protocolUrl = QUrl::fromEncoded(arguments.at(1).toUtf8());
    if(protocolUrl.isValid() && protocolUrl.scheme() != "actiona")
		protocolUrl = QUrl();

	MainClass::ExecutionMode executionMode = MainClass::Unknown;
	MainClass mainClass;

	if(protocolUrl.isValid())
	{
		QString mode;
        using QStringPair = QPair<QString, QString>;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
        for(const QStringPair &queryItem: QUrlQuery(protocolUrl.query()).queryItems())
#else
        for(const QStringPair &queryItem: protocolUrl.queryItems())
#endif
		{
			if(queryItem.first == "mode")
			{
				mode = queryItem.second;
				break;
			}
		}

		if(mode == "code")
			executionMode = MainClass::Code;
		else if(mode == "script")
			executionMode = MainClass::Script;
		else
		{
			if(protocolUrl.path().endsWith(".ascr"))
				executionMode = MainClass::Script;
			else if(protocolUrl.path().endsWith(".acod"))
				executionMode = MainClass::Code;
			else
			{
				QTextStream stream(stdout);
				stream << QObject::tr("Unknown execution mode, please specify mode=script or mode=code") << "\n";
				stream.flush();
				return -1;
			}
		}

		if(!mainClass.start(executionMode, protocolUrl))
			return -1;
	}
	else
	{
		QString filename = options.positional().at(0);

		if(options.count("code"))
			executionMode = MainClass::Code;
		else if(options.count("script"))
			executionMode = MainClass::Script;
		else
		{
			if(filename.endsWith(".ascr"))
				executionMode = MainClass::Script;
			else if(filename.endsWith(".acod"))
				executionMode = MainClass::Code;
			else
			{
				QTextStream stream(stdout);
				stream << QObject::tr("Unknown execution mode, please specify -s (script) or -c (code)") << "\n";
				stream.flush();
				return -1;
			}
		}

		QFile file(filename);
		if(!file.open(QIODevice::ReadOnly))
		{
			QTextStream stream(stdout);
			stream << QObject::tr("Unable to read input file") << "\n";
			stream.flush();
			return -1;
		}

		if(!mainClass.start(executionMode, &file, file.fileName()))
		{
			file.close();

			return -1;
		}
	}

	return app.exec();
}