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 Assistant 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 <stddef.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34#ifdef _WIN32
35#include <process.h>
36#else
37#include <unistd.h>
38#endif
39
40static const char collectionGeneratorName[] = "qcollectiongenerator";
41static const char helpGeneratorName[] = "qhelpgenerator";
42
43#ifdef _WIN32
44static const char separator = '\\';
45#else
46static const char separator = '/';
47#endif
48
49int main(int argc, char *argv[])
50{
51 (void)argc;
52 printf(format: "The \"%s\" tool is deprecated, use \"%s\" instead.\n\n",
53 collectionGeneratorName, helpGeneratorName);
54
55 // Replace the "qcollectiongenerator" with "qhelpgenerator"
56 // in passed argv[0], keeping the path.
57
58 const size_t currentNameSize = strlen(s: argv[0]);
59 const size_t collectionGeneratorNameSize = strlen(s: collectionGeneratorName);
60 const ptrdiff_t maxPathOffset = currentNameSize - collectionGeneratorNameSize;
61 ptrdiff_t pathOffset = maxPathOffset;
62
63 if (maxPathOffset >= 0 && strchr(s: argv[0] + maxPathOffset, c: separator))
64 pathOffset = -1; // Separator detected. Wrong filename.
65
66 while (pathOffset >= 0) {
67 const char *fileName = argv[0] + pathOffset;
68
69 if (fileName[0] == separator) { // Separator detected. Wrong filename.
70 pathOffset = -1;
71 break;
72 }
73
74 if (!strncmp(s1: fileName, s2: collectionGeneratorName, n: collectionGeneratorNameSize))
75 break;
76
77 --pathOffset;
78 }
79
80 if (pathOffset < 0) {
81 fprintf(stderr, format: "Wrong tool name. "
82 "The tool name is expected to contain: \"%s\", got: \"%s\" instead.\n",
83 collectionGeneratorName, argv[0]);
84 return 3;
85 }
86
87 const size_t helpGeneratorNameSize = strlen(s: helpGeneratorName);
88 // Allocate a buffer for the new full path, consisting of the pathSize + new name
89 char *newPath = (char *) malloc(size: (maxPathOffset + helpGeneratorNameSize + 1) * sizeof(char));
90 // Copy the path
91 memcpy(dest: newPath, src: argv[0], n: pathOffset);
92 // Copy the new name
93 memcpy(dest: newPath + pathOffset, src: helpGeneratorName, n: helpGeneratorNameSize);
94 // Copy the remaining part
95 memcpy(dest: newPath + pathOffset + helpGeneratorNameSize,
96 src: argv[0] + pathOffset + collectionGeneratorNameSize,
97 n: currentNameSize - pathOffset - collectionGeneratorNameSize + 1);
98
99 argv[0] = newPath;
100#ifdef _WIN32
101 const intptr_t ret = _spawnvp(_P_WAIT, newPath, argv);
102 if (ret == -1) {
103 fprintf(stderr, "Error while executing \"%s\" tool.\n", newPath);
104 return 3;
105 }
106 return ret;
107#else
108 execvp(file: newPath, argv: argv);
109 fprintf(stderr, format: "Error while executing \"%s\" tool.\n", newPath);
110 return 3;
111#endif
112}
113
114

source code of qttools/src/assistant/qcollectiongenerator/main.c