1/****************************************************************************
2**
3** Copyright (C) 2018 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Linguist of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "runqttool.h"
30
31#include "profileutils.h"
32
33#include <QtCore/qcoreapplication.h>
34#include <QtCore/qdir.h>
35#include <QtCore/qregexp.h>
36
37#include <cstdlib>
38#include <iostream>
39
40#ifdef Q_OS_UNIX
41#include <sys/wait.h>
42#endif
43
44class FMT {
45 Q_DECLARE_TR_FUNCTIONS(Linguist)
46};
47
48static QString qtToolFilePath(const QString &toolName)
49{
50 QString filePath = QCoreApplication::instance()->applicationDirPath()
51 + QLatin1Char('/') + toolName;
52#ifdef Q_OS_WIN
53 filePath.append(QLatin1String(".exe"));
54#endif
55 return QDir::cleanPath(path: filePath);
56}
57
58static void printErr(const QString &out)
59{
60 std::cerr << qUtf8Printable(out);
61}
62
63static QString shellQuoted(const QString &str)
64{
65 static QRegExp rx(QStringLiteral("\\s"));
66 QString result = str;
67 if (result.contains(rx)) {
68 const QLatin1Char dblqt = QLatin1Char('"');
69 result.prepend(c: dblqt);
70 result.append(c: dblqt);
71 }
72 return result;
73}
74
75static QStringList shellQuoted(const QStringList &strs)
76{
77 QStringList result;
78 result.reserve(alloc: strs.size());
79 std::transform(first: strs.begin(), last: strs.end(), result: std::back_inserter(x&: result),
80 unary_op: static_cast<QString (*)(const QString &)>(&shellQuoted));
81 return result;
82}
83
84static QString commandLineForSystem(const QString &program,
85 const QStringList &arguments)
86{
87 return shellQuoted(str: program)
88 + QLatin1Char(' ')
89 + shellQuoted(strs: arguments).join(sep: QLatin1Char(' '));
90}
91
92void runQtTool(const QString &toolName, const QStringList &arguments)
93{
94 int exitCode = 0;
95 const QString commandLine = commandLineForSystem(program: qtToolFilePath(toolName), arguments);
96#if defined(Q_OS_WIN)
97 exitCode = _wsystem(reinterpret_cast<const wchar_t *>(commandLine.utf16()));
98#elif defined(Q_OS_UNIX)
99 int ret = std::system(qPrintable(commandLine));
100 exitCode = WEXITSTATUS(ret);
101#else
102 exitCode = std::system(qPrintable(commandLine));
103#endif
104 if (exitCode != 0)
105 exit(status: exitCode);
106}
107
108std::unique_ptr<QTemporaryFile> createProjectDescription(QStringList args)
109{
110 std::unique_ptr<QTemporaryFile> file(new QTemporaryFile(QStringLiteral("XXXXXX.json")));
111 if (!file->open()) {
112 printErr(out: FMT::tr(sourceText: "Cannot create temporary file: %1\n").arg(a: file->errorString()));
113 exit(status: 1);
114 }
115 file->close();
116 args << QStringLiteral("-out") << file->fileName();
117 runQtTool(QStringLiteral("lprodump"), arguments: args);
118 return file;
119}
120

source code of qttools/src/linguist/shared/runqttool.cpp