1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QByteArray>
#include <QString>
#include <QVarLengthArray>
#include <QFile>
#include <QProcess>
#include <QMetaObject>
#include <QList>
#include <QRegExp>
#include <QCoreApplication>
#include <QLibraryInfo>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include "qdbusconnection.h" // for the Export* flags
// copied from dbus-protocol.h:
static const char docTypeHeader[] =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" "
"\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n";
// in qdbusxmlgenerator.cpp
QT_BEGIN_NAMESPACE
extern Q_DBUS_EXPORT QString qDBusGenerateMetaObjectXml(QString interface, const QMetaObject *mo,
const QMetaObject *base, int flags);
QT_END_NAMESPACE
#define PROGRAMNAME "qdbuscpp2xml"
#define PROGRAMVERSION "0.1"
#define PROGRAMCOPYRIGHT "Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)."
static QString outputFile;
static int flags;
static const char help[] =
"Usage: " PROGRAMNAME " [options...] [files...]\n"
"Parses the C++ source or header file containing a QObject-derived class and\n"
"produces the D-Bus Introspection XML."
"\n"
"Options:\n"
" -p|-s|-m Only parse scriptable Properties, Signals and Methods (slots)\n"
" -P|-S|-M Parse all Properties, Signals and Methods (slots)\n"
" -a Output all scriptable contents (equivalent to -psm)\n"
" -A Output all contents (equivalent to -PSM)\n"
" -o <filename> Write the output to file <filename>\n"
" -h Show this information\n"
" -V Show the program version and quit.\n"
"\n";
class MocParser
{
void parseError();
QByteArray readLine();
void loadIntData(uint *&data);
void loadStringData(char *&stringdata);
QIODevice *input;
const char *filename;
int lineNumber;
public:
~MocParser();
void parse(const char *filename, QIODevice *input, int lineNumber = 0);
QList<QMetaObject> objects;
};
void MocParser::parseError()
{
fprintf(stderr, PROGRAMNAME ": error parsing input file '%s' line %d \n", filename, lineNumber);
exit(1);
}
QByteArray MocParser::readLine()
{
++lineNumber;
return input->readLine();
}
void MocParser::loadIntData(uint *&data)
{
data = 0; // initialise
QVarLengthArray<uint> array;
QRegExp rx(QLatin1String("(\\d+|0x[0-9abcdef]+)"), Qt::CaseInsensitive);
while (!input->atEnd()) {
QString line = QLatin1String(readLine());
int pos = line.indexOf(QLatin1String("//"));
if (pos != -1)
line.truncate(pos); // drop comments
if (line == QLatin1String("};\n")) {
// end of data
data = new uint[array.count()];
memcpy(data, array.data(), array.count() * sizeof(*data));
return;
}
pos = 0;
while ((pos = rx.indexIn(line, pos)) != -1) {
QString num = rx.cap(1);
if (num.startsWith(QLatin1String("0x")))
array.append(num.mid(2).toUInt(0, 16));
else
array.append(num.toUInt());
pos += rx.matchedLength();
}
}
parseError();
}
void MocParser::loadStringData(char *&stringdata)
{
stringdata = 0;
QVarLengthArray<char, 1024> array;
while (!input->atEnd()) {
QByteArray line = readLine();
if (line == "};\n") {
// end of data
stringdata = new char[array.count()];
memcpy(stringdata, array.data(), array.count() * sizeof(*stringdata));
return;
}
int start = line.indexOf('"');
if (start == -1)
parseError();
int len = line.length() - 1;
line.truncate(len); // drop ending \n
if (line.at(len - 1) != '"')
parseError();
--len;
++start;
for ( ; start < len; ++start)
if (line.at(start) == '\\') {
// parse escaped sequence
++start;
if (start == len)
parseError();
QChar c(QLatin1Char(line.at(start)));
if (!c.isDigit()) {
switch (c.toLatin1()) {
case 'a':
array.append('\a');
break;
case 'b':
array.append('\b');
break;
case 'f':
array.append('\f');
break;
case 'n':
array.append('\n');
break;
case 'r':
array.append('\r');
break;
case 't':
array.append('\t');
break;
case 'v':
array.append('\v');
break;
case '\\':
case '?':
case '\'':
case '"':
array.append(c.toLatin1());
break;
case 'x':
if (start + 2 <= len)
parseError();
array.append(char(line.mid(start + 1, 2).toInt(0, 16)));
break;
default:
array.append(c.toLatin1());
fprintf(stderr, PROGRAMNAME ": warning: invalid escape sequence '\\%c' found in input",
c.toLatin1());
}
} else {
// octal
QRegExp octal(QLatin1String("([0-7]+)"));
if (octal.indexIn(QLatin1String(line), start) == -1)
parseError();
array.append(char(octal.cap(1).toInt(0, 8)));
}
} else {
array.append(line.at(start));
}
}
parseError();
}
void MocParser::parse(const char *fname, QIODevice *io, int lineNum)
{
filename = fname;
input = io;
lineNumber = lineNum;
while (!input->atEnd()) {
QByteArray line = readLine();
if (line.startsWith("static const uint qt_meta_data_")) {
// start of new class data
uint *data;
loadIntData(data);
// find the start of the string data
do {
line = readLine();
if (input->atEnd())
parseError();
} while (!line.startsWith("static const char qt_meta_stringdata_"));
char *stringdata;
loadStringData(stringdata);
QMetaObject mo;
mo.d.superdata = &QObject::staticMetaObject;
mo.d.stringdata = stringdata;
mo.d.data = data;
mo.d.extradata = 0;
objects.append(mo);
}
}
fname = 0;
input = 0;
}
MocParser::~MocParser()
{
foreach (QMetaObject mo, objects) {
delete const_cast<char *>(mo.d.stringdata);
delete const_cast<uint *>(mo.d.data);
}
}
static void showHelp()
{
printf("%s", help);
exit(0);
}
static void showVersion()
{
printf("%s version %s\n", PROGRAMNAME, PROGRAMVERSION);
printf("D-Bus QObject-to-XML converter\n");
exit(0);
}
static void parseCmdLine(QStringList &arguments)
{
for (int i = 1; i < arguments.count(); ++i) {
const QString arg = arguments.at(i);
if (arg == QLatin1String("--help"))
showHelp();
if (!arg.startsWith(QLatin1Char('-')))
continue;
char c = arg.count() == 2 ? arg.at(1).toLatin1() : char(0);
switch (c) {
case 'P':
flags |= QDBusConnection::ExportNonScriptableProperties;
// fall through
case 'p':
flags |= QDBusConnection::ExportScriptableProperties;
break;
case 'S':
flags |= QDBusConnection::ExportNonScriptableSignals;
// fall through
case 's':
flags |= QDBusConnection::ExportScriptableSignals;
break;
case 'M':
flags |= QDBusConnection::ExportNonScriptableSlots;
// fall through
case 'm':
flags |= QDBusConnection::ExportScriptableSlots;
break;
case 'A':
flags |= QDBusConnection::ExportNonScriptableContents;
// fall through
case 'a':
flags |= QDBusConnection::ExportScriptableContents;
break;
case 'o':
if (arguments.count() < i + 2 || arguments.at(i + 1).startsWith(QLatin1Char('-'))) {
printf("-o expects a filename\n");
exit(1);
}
outputFile = arguments.takeAt(i + 1);
break;
case 'h':
case '?':
showHelp();
break;
case 'V':
showVersion();
break;
default:
printf("unknown option: \"%s\"\n", qPrintable(arg));
exit(1);
}
}
if (flags == 0)
flags = QDBusConnection::ExportScriptableContents
| QDBusConnection::ExportNonScriptableContents;
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QStringList args = app.arguments();
MocParser parser;
parseCmdLine(args);
for (int i = 1; i < args.count(); ++i) {
const QString arg = args.at(i);
if (arg.startsWith(QLatin1Char('-')))
continue;
QFile f(arg);
if (!f.open(QIODevice::ReadOnly|QIODevice::Text)) {
fprintf(stderr, PROGRAMNAME ": could not open '%s': %s\n",
qPrintable(arg), qPrintable(f.errorString()));
return 1;
}
f.readLine();
QByteArray line = f.readLine();
if (line.contains("Meta object code from reading C++ file"))
// this is a moc-generated file
parser.parse(argv[i], &f, 3);
else {
// run moc on this file
QProcess proc;
proc.start(QLibraryInfo::location(QLibraryInfo::BinariesPath) + QLatin1String("/moc"), QStringList() << QFile::decodeName(argv[i]), QIODevice::ReadOnly | QIODevice::Text);
if (!proc.waitForStarted()) {
fprintf(stderr, PROGRAMNAME ": could not execute moc! Aborting.\n");
return 1;
}
proc.closeWriteChannel();
if (!proc.waitForFinished() || proc.exitStatus() != QProcess::NormalExit ||
proc.exitCode() != 0) {
// output the moc errors:
fprintf(stderr, "%s", proc.readAllStandardError().constData());
fprintf(stderr, PROGRAMNAME ": exit code %d from moc. Aborting\n", proc.exitCode());
return 1;
}
fprintf(stderr, "%s", proc.readAllStandardError().constData());
parser.parse(argv[i], &proc, 1);
}
f.close();
}
QFile output;
if (outputFile.isEmpty()) {
output.open(stdout, QIODevice::WriteOnly);
} else {
output.setFileName(outputFile);
if (!output.open(QIODevice::WriteOnly)) {
fprintf(stderr, PROGRAMNAME ": could not open output file '%s': %s",
qPrintable(outputFile), qPrintable(output.errorString()));
return 1;
}
}
output.write(docTypeHeader);
output.write("<node>\n");
foreach (QMetaObject mo, parser.objects) {
QString xml = qDBusGenerateMetaObjectXml(QString(), &mo, &QObject::staticMetaObject,
flags);
output.write(xml.toLocal8Bit());
}
output.write("</node>\n");
return 0;
}
|