10#include <qscopedvaluerollback.h>
15#include <qbytearray.h>
16#include <qdeadlinetimer.h>
17#include <qcoreapplication.h>
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
69QStringList QProcessEnvironmentPrivate::toList()
const
72 result.reserve(vars.size());
73 for (
auto it = vars.cbegin(), end = vars.cend(); it != end; ++it)
74 result << nameToString(it.key()) + u'=' + valueToString(it.value());
81 QStringList::ConstIterator it = list.constBegin(),
82 end = list.constEnd();
83 for ( ; it != end; ++it) {
84 const qsizetype pos = it->indexOf(u'=', 1);
88 QString value = it->mid(pos + 1);
91 env.insert(name, value);
99 result.reserve(vars.size());
100 auto it = vars.constBegin();
101 const auto end = vars.constEnd();
102 for ( ; it != end; ++it)
103 result << nameToString(it.key());
109 auto it = other.vars.constBegin();
110 const auto end = other.vars.constEnd();
111 for ( ; it != end; ++it)
112 vars.insert(it.key(), it.value());
115 auto nit = other.nameMap.constBegin();
116 const auto nend = other.nameMap.constEnd();
117 for ( ; nit != nend; ++nit)
118 nameMap.insert(nit.key(), nit.value());
123
124
125
126
127
128
129
130
131
134
135
136
137
138
139QProcessEnvironment::QProcessEnvironment() : d(
new QProcessEnvironmentPrivate) { }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159QProcessEnvironment::QProcessEnvironment(QProcessEnvironment::Initialization)
noexcept { }
162
163
164QProcessEnvironment::~QProcessEnvironment()
169
170
171QProcessEnvironment::QProcessEnvironment(
const QProcessEnvironment &other)
177
178
179
180QProcessEnvironment &QProcessEnvironment::operator=(
const QProcessEnvironment &other)
187
188
189
190
193
194
195
196
197
198
201
202
203
204
205
206
207
208
209
210
211bool comparesEqual(
const QProcessEnvironment &lhs,
const QProcessEnvironment &rhs)
215 if (!(lhs.d && rhs.d))
218 return lhs.d->vars == rhs.d->vars;
222
223
224
225
226
227
228
229
230bool QProcessEnvironment::isEmpty()
const
233 return d ? d->vars.isEmpty() :
true;
237
238
239
240
241
242
243bool QProcessEnvironment::inheritsFromParent()
const
249
250
251
252
253
254
255
256
257void QProcessEnvironment::clear()
266
267
268
269
270
271
272bool QProcessEnvironment::contains(
const QString &name)
const
276 QProcessEnvironmentPrivate::MutexLocker locker(d);
277 return d->vars.contains(d->prepareName(name));
281
282
283
284
285
286
287
288
289
290
291
292void QProcessEnvironment::insert(
const QString &name,
const QString &value)
296 d->vars.insert(d->prepareName(name), d->prepareValue(value));
300
301
302
303
304
305
306
307void QProcessEnvironment::remove(
const QString &name)
310 QProcessEnvironmentPrivate *p = d.data();
311 p->vars.remove(p->prepareName(name));
316
317
318
319
320
321
322QString QProcessEnvironment::value(
const QString &name,
const QString &defaultValue)
const
327 QProcessEnvironmentPrivate::MutexLocker locker(d);
328 const auto it = d->vars.constFind(d->prepareName(name));
329 if (it == d->vars.constEnd())
332 return d->valueToString(it.value());
336
337
338
339
340
341
342
343
344
345
346
347
348QStringList QProcessEnvironment::toStringList()
const
351 return QStringList();
352 QProcessEnvironmentPrivate::MutexLocker locker(d);
357
358
359
360
361
362
363
364
365QStringList QProcessEnvironment::keys()
const
368 return QStringList();
369 QProcessEnvironmentPrivate::MutexLocker locker(d);
374
375
376
377
378
379
380void QProcessEnvironment::insert(
const QProcessEnvironment &e)
386 QProcessEnvironmentPrivate::MutexLocker locker(e.d);
390#if QT_CONFIG(process)
392void QProcessPrivate::Channel::clear()
397 process->stdinChannel.type = Normal;
398 process->stdinChannel.process =
nullptr;
402 process->stdoutChannel.type = Normal;
403 process->stdoutChannel.process =
nullptr;
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
699
700
701
702
703
704
705
706
707
708
709
710
711
712
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
812
813
814
815
816
817
818
819
820
821
822
823
824
825
828
829
830
831
832
833
834
835
836
837
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
870
871
872
873
874
875
876
877
878
879
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
984
985
986
987
988
989
992
993
994
995
996
999
1000
1001
1002
1003
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1019
1020
1021
1022
1023
1024
1025
1026
1029
1030
1031
1032
1033
1034
1035
1036
1037
1040
1041
1042QProcessPrivate::QProcessPrivate()
1044 readBufferChunkSize = QRINGBUFFER_CHUNKSIZE;
1046 writeBufferChunkSize = QRINGBUFFER_CHUNKSIZE;
1051
1052
1053QProcessPrivate::~QProcessPrivate()
1055 if (stdinChannel.process)
1056 stdinChannel.process->stdoutChannel.clear();
1057 if (stdoutChannel.process)
1058 stdoutChannel.process->stdinChannel.clear();
1062
1063
1064void QProcessPrivate::setError(QProcess::ProcessError error,
const QString &description)
1066 processError = error;
1067 if (description.isEmpty()) {
1069 case QProcess::FailedToStart:
1070 errorString = QProcess::tr(
"Process failed to start");
1072 case QProcess::Crashed:
1073 errorString = QProcess::tr(
"Process crashed");
1075 case QProcess::Timedout:
1076 errorString = QProcess::tr(
"Process operation timed out");
1078 case QProcess::ReadError:
1079 errorString = QProcess::tr(
"Error reading from process");
1081 case QProcess::WriteError:
1082 errorString = QProcess::tr(
"Error writing to process");
1084 case QProcess::UnknownError:
1085 errorString.clear();
1089 errorString = description;
1094
1095
1096void QProcessPrivate::setErrorAndEmit(QProcess::ProcessError error,
const QString &description)
1099 Q_ASSERT(error != QProcess::UnknownError);
1100 setError(error, description);
1101 emit q->errorOccurred(QProcess::ProcessError(processError));
1105
1106
1107bool QProcessPrivate::openChannels()
1110 if (inputChannelMode == QProcess::ForwardedInputChannel) {
1111 if (stdinChannel.type != Channel::Normal)
1112 qWarning(
"QProcess::openChannels: Inconsistent stdin channel configuration");
1113 }
else if (!openChannel(stdinChannel)) {
1118 if (processChannelMode == QProcess::ForwardedChannels
1119 || processChannelMode == QProcess::ForwardedOutputChannel) {
1120 if (stdoutChannel.type != Channel::Normal)
1121 qWarning(
"QProcess::openChannels: Inconsistent stdout channel configuration");
1122 }
else if (!openChannel(stdoutChannel)) {
1127 if (processChannelMode == QProcess::ForwardedChannels
1128 || processChannelMode == QProcess::ForwardedErrorChannel
1129 || processChannelMode == QProcess::MergedChannels) {
1130 if (stderrChannel.type != Channel::Normal)
1131 qWarning(
"QProcess::openChannels: Inconsistent stderr channel configuration");
1132 }
else if (!openChannel(stderrChannel)) {
1140
1141
1142void QProcessPrivate::closeChannels()
1144 closeChannel(&stdoutChannel);
1145 closeChannel(&stderrChannel);
1146 closeChannel(&stdinChannel);
1150
1151
1152bool QProcessPrivate::openChannelsForDetached()
1155 bool needToOpen = (stdinChannel.type == Channel::Redirect
1156 || stdinChannel.type == Channel::PipeSink);
1157 if (stdinChannel.type != Channel::Normal
1159 || inputChannelMode == QProcess::ForwardedInputChannel)) {
1160 qWarning(
"QProcess::openChannelsForDetached: Inconsistent stdin channel configuration");
1162 if (needToOpen && !openChannel(stdinChannel))
1166 needToOpen = (stdoutChannel.type == Channel::Redirect
1167 || stdoutChannel.type == Channel::PipeSource);
1168 if (stdoutChannel.type != Channel::Normal
1170 || processChannelMode == QProcess::ForwardedChannels
1171 || processChannelMode == QProcess::ForwardedOutputChannel)) {
1172 qWarning(
"QProcess::openChannelsForDetached: Inconsistent stdout channel configuration");
1174 if (needToOpen && !openChannel(stdoutChannel))
1178 needToOpen = (stderrChannel.type == Channel::Redirect);
1179 if (stderrChannel.type != Channel::Normal
1181 || processChannelMode == QProcess::ForwardedChannels
1182 || processChannelMode == QProcess::ForwardedErrorChannel
1183 || processChannelMode == QProcess::MergedChannels)) {
1184 qWarning(
"QProcess::openChannelsForDetached: Inconsistent stderr channel configuration");
1186 if (needToOpen && !openChannel(stderrChannel))
1193
1194
1195
1196bool QProcessPrivate::tryReadFromChannel(Channel *channel)
1199 if (channel->pipe[0] == INVALID_Q_PIPE)
1202 qint64 available = bytesAvailableInChannel(channel);
1206 QProcess::ProcessChannel channelIdx = (channel == &stdoutChannel
1207 ? QProcess::StandardOutput
1208 : QProcess::StandardError);
1209 Q_ASSERT(readBuffers.size() >
int(channelIdx));
1210 QRingBuffer &readBuffer = readBuffers[
int(channelIdx)];
1211 char *ptr = readBuffer.reserve(available);
1212 qint64 readBytes = readFromChannel(channel, ptr, available);
1214 readBuffer.chop(available);
1215 if (readBytes == -2) {
1219 if (readBytes == -1) {
1220 setErrorAndEmit(QProcess::ReadError);
1221#if defined QPROCESS_DEBUG
1222 qDebug(
"QProcessPrivate::tryReadFromChannel(%d), failed to read from the process",
1223 int(channel - &stdinChannel));
1227 if (readBytes == 0) {
1229 closeChannel(channel);
1230#if defined QPROCESS_DEBUG
1231 qDebug(
"QProcessPrivate::tryReadFromChannel(%d), 0 bytes available",
1232 int(channel - &stdinChannel));
1236#if defined QPROCESS_DEBUG
1237 qDebug(
"QProcessPrivate::tryReadFromChannel(%d), read %lld bytes from the process' output",
1238 int(channel - &stdinChannel), readBytes);
1241 if (channel->closed) {
1242 readBuffer.chop(readBytes);
1246 readBuffer.chop(available - readBytes);
1248 bool didRead =
false;
1249 if (currentReadChannel == channelIdx) {
1251 if (!emittedReadyRead) {
1252 QScopedValueRollback<
bool> guard(emittedReadyRead,
true);
1253 emit q->readyRead();
1256 emit q->channelReadyRead(
int(channelIdx));
1257 if (channelIdx == QProcess::StandardOutput)
1258 emit q->readyReadStandardOutput(QProcess::QPrivateSignal());
1260 emit q->readyReadStandardError(QProcess::QPrivateSignal());
1265
1266
1267bool QProcessPrivate::_q_canReadStandardOutput()
1269 return tryReadFromChannel(&stdoutChannel);
1273
1274
1275bool QProcessPrivate::_q_canReadStandardError()
1277 return tryReadFromChannel(&stderrChannel);
1281
1282
1283void QProcessPrivate::_q_processDied()
1285#if defined QPROCESS_DEBUG
1286 qDebug(
"QProcessPrivate::_q_processDied()");
1295 _q_canReadStandardOutput();
1296 _q_canReadStandardError();
1302 if (processState != QProcess::NotRunning)
1307
1308
1309void QProcessPrivate::processFinished()
1312#if defined QPROCESS_DEBUG
1313 qDebug(
"QProcessPrivate::processFinished()");
1324 if (exitStatus == QProcess::CrashExit)
1325 setErrorAndEmit(QProcess::Crashed);
1328 emit q->readChannelFinished();
1333 emit q->finished(exitCode, QProcess::ExitStatus(exitStatus));
1335#if defined QPROCESS_DEBUG
1336 qDebug(
"QProcessPrivate::processFinished(): process is dead");
1341
1342
1343bool QProcessPrivate::_q_startupNotification()
1346#if defined QPROCESS_DEBUG
1347 qDebug(
"QProcessPrivate::startupNotification()");
1350 QString errorMessage;
1351 if (processStarted(&errorMessage)) {
1352 q->setProcessState(QProcess::Running);
1353 emit q->started(QProcess::QPrivateSignal());
1357 q->setProcessState(QProcess::NotRunning);
1358 setErrorAndEmit(QProcess::FailedToStart, errorMessage);
1367
1368
1369void QProcessPrivate::closeWriteChannel()
1371#if defined QPROCESS_DEBUG
1372 qDebug(
"QProcessPrivate::closeWriteChannel()");
1375 closeChannel(&stdinChannel);
1379
1380
1381QProcess::QProcess(QObject *parent)
1382 : QIODevice(*
new QProcessPrivate, parent)
1384#if defined QPROCESS_DEBUG
1385 qDebug(
"QProcess::QProcess(%p)", parent);
1390
1391
1392
1393
1394
1395QProcess::~QProcess()
1398 if (d->processState != NotRunning) {
1399 qWarning().nospace()
1400 <<
"QProcess: Destroyed while process (" << QDir::toNativeSeparators(program()) <<
") is still running.";
1408
1409
1410
1411
1412
1413
1414
1415QProcess::ProcessChannelMode QProcess::processChannelMode()
const
1417 Q_D(
const QProcess);
1418 return ProcessChannelMode(d->processChannelMode);
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432void QProcess::setProcessChannelMode(ProcessChannelMode mode)
1435 d->processChannelMode = mode;
1439
1440
1441
1442
1443
1444
1445QProcess::InputChannelMode QProcess::inputChannelMode()
const
1447 Q_D(
const QProcess);
1448 return InputChannelMode(d->inputChannelMode);
1452
1453
1454
1455
1456
1457
1458
1459
1460void QProcess::setInputChannelMode(InputChannelMode mode)
1463 d->inputChannelMode = mode;
1467
1468
1469
1470
1471QProcess::ProcessChannel QProcess::readChannel()
const
1473 Q_D(
const QProcess);
1474 return ProcessChannel(d->currentReadChannel);
1478
1479
1480
1481
1482
1483
1484
1485void QProcess::setReadChannel(ProcessChannel channel)
1487 QIODevice::setCurrentReadChannel(
int(channel));
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500void QProcess::closeReadChannel(ProcessChannel channel)
1504 if (channel == StandardOutput)
1505 d->stdoutChannel.closed =
true;
1507 d->stderrChannel.closed =
true;
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528void QProcess::closeWriteChannel()
1531 d->stdinChannel.closed =
true;
1532 if (bytesToWrite() == 0)
1533 d->closeWriteChannel();
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557void QProcess::setStandardInputFile(
const QString &fileName)
1560 d->stdinChannel = fileName;
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592void QProcess::setStandardOutputFile(
const QString &fileName, OpenMode mode)
1594 Q_ASSERT(mode == Append || mode == Truncate);
1597 d->stdoutChannel = fileName;
1598 d->stdoutChannel.append = mode == Append;
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619void QProcess::setStandardErrorFile(
const QString &fileName, OpenMode mode)
1621 Q_ASSERT(mode == Append || mode == Truncate);
1624 d->stderrChannel = fileName;
1625 d->stderrChannel.append = mode == Append;
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640void QProcess::setStandardOutputProcess(QProcess *destination)
1642 QProcessPrivate *dfrom = d_func();
1643 QProcessPrivate *dto = destination->d_func();
1644 dfrom->stdoutChannel.pipeTo(dto);
1645 dto->stdinChannel.pipeFrom(dfrom);
1648#if defined(Q_OS_WIN) || defined(Q_QDOC)
1651
1652
1653
1654
1655
1656
1657
1658
1659QString QProcess::nativeArguments()
const
1661 Q_D(
const QProcess);
1662 return d->nativeArguments;
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682void QProcess::setNativeArguments(
const QString &arguments)
1685 d->nativeArguments = arguments;
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698QProcess::CreateProcessArgumentModifier QProcess::createProcessArgumentsModifier()
const
1700 Q_D(
const QProcess);
1701 return d->modifyCreateProcessArgs;
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715void QProcess::setCreateProcessArgumentsModifier(CreateProcessArgumentModifier modifier)
1718 d->modifyCreateProcessArgs = modifier;
1723#if defined(Q_OS_UNIX) || defined(Q_QDOC)
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734std::function<
void(
void)> QProcess::childProcessModifier()
const
1736 Q_D(
const QProcess);
1737 return d->unixExtras ? d->unixExtras->childProcessModifier : std::function<
void(
void)>();
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787void QProcess::setChildProcessModifier(
const std::function<
void(
void)> &modifier)
1791 d->unixExtras.reset(
new QProcessPrivate::UnixExtras);
1792 d->unixExtras->childProcessModifier = modifier;
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845auto QProcess::unixProcessParameters()
const noexcept -> UnixProcessParameters
1847 Q_D(
const QProcess);
1848 return d->unixExtras ? d->unixExtras->processParameters : UnixProcessParameters{};
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869void QProcess::setUnixProcessParameters(
const UnixProcessParameters ¶ms)
1873 d->unixExtras.reset(
new QProcessPrivate::UnixExtras);
1874 d->unixExtras->processParameters = params;
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888void QProcess::setUnixProcessParameters(UnixProcessFlags flagsOnly)
1892 d->unixExtras.reset(
new QProcessPrivate::UnixExtras);
1893 d->unixExtras->processParameters = { flagsOnly };
1898
1899
1900
1901
1902
1903
1904
1905
1906QString QProcess::workingDirectory()
const
1908 Q_D(
const QProcess);
1909 return d->workingDirectory;
1913
1914
1915
1916
1917
1918
1919void QProcess::setWorkingDirectory(
const QString &dir)
1922 d->workingDirectory = dir;
1926
1927
1928
1929
1930
1931qint64 QProcess::processId()
const
1933 Q_D(
const QProcess);
1935 return d->pid ? d->pid->dwProcessId : 0;
1942
1943
1944
1945
1946void QProcess::close()
1949 emit aboutToClose();
1950 while (waitForBytesWritten(-1))
1953 waitForFinished(-1);
1954 d->setWriteChannelCount(0);
1959
1960bool QProcess::isSequential()
const
1966
1967qint64 QProcess::bytesToWrite()
const
1970 return d_func()->pipeWriterBytesToWrite();
1972 return QIODevice::bytesToWrite();
1977
1978
1979
1980
1981QProcess::ProcessError QProcess::error()
const
1983 Q_D(
const QProcess);
1984 return ProcessError(d->processError);
1988
1989
1990
1991
1992QProcess::ProcessState QProcess::state()
const
1994 Q_D(
const QProcess);
1995 return ProcessState(d->processState);
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012void QProcess::setEnvironment(
const QStringList &environment)
2014 setProcessEnvironment(QProcessEnvironmentPrivate::fromList(environment));
2018
2019
2020
2021
2022
2023
2024
2025
2026QStringList QProcess::environment()
const
2028 Q_D(
const QProcess);
2029 return d->environment.toStringList();
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045void QProcess::setProcessEnvironment(
const QProcessEnvironment &environment)
2048 d->environment = environment;
2052
2053
2054
2055
2056
2057
2058
2059
2060QProcessEnvironment QProcess::processEnvironment()
const
2062 Q_D(
const QProcess);
2063 return d->environment;
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086bool QProcess::waitForStarted(
int msecs)
2089 if (d->processState == QProcess::Starting)
2090 return d->waitForStarted(QDeadlineTimer(msecs));
2092 return d->processState == QProcess::Running;
2096
2097bool QProcess::waitForReadyRead(
int msecs)
2101 if (d->processState == QProcess::NotRunning)
2103 if (d->currentReadChannel == QProcess::StandardOutput && d->stdoutChannel.closed)
2105 if (d->currentReadChannel == QProcess::StandardError && d->stderrChannel.closed)
2108 QDeadlineTimer deadline(msecs);
2109 if (d->processState == QProcess::Starting) {
2110 bool started = d->waitForStarted(deadline);
2115 return d->waitForReadyRead(deadline);
2119
2120bool QProcess::waitForBytesWritten(
int msecs)
2123 if (d->processState == QProcess::NotRunning)
2126 QDeadlineTimer deadline(msecs);
2127 if (d->processState == QProcess::Starting) {
2128 bool started = d->waitForStarted(deadline);
2133 return d->waitForBytesWritten(deadline);
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155bool QProcess::waitForFinished(
int msecs)
2158 if (d->processState == QProcess::NotRunning)
2161 QDeadlineTimer deadline(msecs);
2162 if (d->processState == QProcess::Starting) {
2163 bool started = d->waitForStarted(deadline);
2168 return d->waitForFinished(deadline);
2172
2173
2174
2175
2176void QProcess::setProcessState(ProcessState state)
2179 if (d->processState == state)
2181 d->processState = state;
2182 emit stateChanged(state, QPrivateSignal());
2185#if QT_VERSION < QT_VERSION_CHECK(7
,0
,0
)
2187
2188
2189auto QProcess::setupChildProcess() -> Use_setChildProcessModifier_Instead
2191 Q_UNREACHABLE_RETURN({});
2196
2197qint64 QProcess::readData(
char *data, qint64 maxlen)
2203 if (d->processState == QProcess::NotRunning)
2209
2210
2211
2212
2213
2214
2215QByteArray QProcess::readAllStandardOutput()
2217 ProcessChannel tmp = readChannel();
2218 setReadChannel(StandardOutput);
2219 QByteArray data = readAll();
2220 setReadChannel(tmp);
2225
2226
2227
2228
2229
2230
2231QByteArray QProcess::readAllStandardError()
2235 if (d->processChannelMode == MergedChannels) {
2236 qWarning(
"QProcess::readAllStandardError: Called with MergedChannels");
2238 ProcessChannel tmp = readChannel();
2239 setReadChannel(StandardError);
2241 setReadChannel(tmp);
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285void QProcess::start(
const QString &program,
const QStringList &arguments, OpenMode mode)
2288 if (d->processState != NotRunning) {
2289 qWarning(
"QProcess::start: Process is already running");
2292 if (program.isEmpty()) {
2293 d->setErrorAndEmit(QProcess::FailedToStart, tr(
"No program defined"));
2297 d->program = program;
2298 d->arguments = arguments;
2304
2305
2306
2307
2308
2309
2310
2311
2312void QProcess::start(OpenMode mode)
2315 if (d->processState != NotRunning) {
2316 qWarning(
"QProcess::start: Process is already running");
2319 if (d->program.isEmpty()) {
2320 d->setErrorAndEmit(QProcess::FailedToStart, tr(
"No program defined"));
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361void QProcess::startCommand(
const QString &command, OpenMode mode)
2363 QStringList args = splitCommand(command);
2364 if (args.isEmpty()) {
2365 qWarning(
"QProcess::startCommand: empty or whitespace-only command was provided");
2368 const QString program = args.takeFirst();
2370 start(program, args, mode);
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420bool QProcess::startDetached(qint64 *pid)
2423 if (d->processState != NotRunning) {
2424 qWarning(
"QProcess::startDetached: Process is already running");
2427 if (d->program.isEmpty()) {
2428 d->setErrorAndEmit(QProcess::FailedToStart, tr(
"No program defined"));
2431 return d->startDetached(pid);
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445bool QProcess::open(OpenMode mode)
2448 if (d->processState != NotRunning) {
2449 qWarning(
"QProcess::start: Process is already running");
2452 if (d->program.isEmpty()) {
2453 qWarning(
"QProcess::start: program not set");
2461void QProcessPrivate::start(QIODevice::OpenMode mode)
2464#if defined QPROCESS_DEBUG
2465 qDebug() <<
"QProcess::start(" << program <<
',' << arguments <<
',' << mode <<
')';
2468 if (stdinChannel.type != QProcessPrivate::Channel::Normal)
2469 mode &= ~QIODevice::WriteOnly;
2470 if (stdoutChannel.type != QProcessPrivate::Channel::Normal &&
2471 (stderrChannel.type != QProcessPrivate::Channel::Normal ||
2472 processChannelMode == QProcess::MergedChannels))
2473 mode &= ~QIODevice::ReadOnly;
2475 mode = QIODevice::Unbuffered;
2476 if ((mode & QIODevice::ReadOnly) == 0) {
2477 if (stdoutChannel.type == QProcessPrivate::Channel::Normal)
2478 q->setStandardOutputFile(q->nullDevice());
2479 if (stderrChannel.type == QProcessPrivate::Channel::Normal
2480 && processChannelMode != QProcess::MergedChannels)
2481 q->setStandardErrorFile(q->nullDevice());
2484 q->QIODevice::open(mode);
2486 if (q->isReadable() && processChannelMode != QProcess::MergedChannels)
2487 setReadChannelCount(2);
2489 stdinChannel.closed =
false;
2490 stdoutChannel.closed =
false;
2491 stderrChannel.closed =
false;
2494 exitStatus = QProcess::NormalExit;
2495 processError = QProcess::UnknownError;
2496 errorString.clear();
2502
2503
2504
2505
2506
2507
2508
2509
2515 bool inQuote =
false;
2520 for (
int i = 0; i < command.size(); ++i) {
2521 if (command.at(i) == u'"') {
2523 if (quoteCount == 3) {
2526 tmp += command.at(i);
2531 if (quoteCount == 1)
2535 if (!inQuote && command.at(i).isSpace()) {
2536 if (!tmp.isEmpty()) {
2541 tmp += command.at(i);
2550#if QT_CONFIG(process)
2552
2553
2554
2555
2556
2557
2558QString QProcess::program()
const
2560 Q_D(
const QProcess);
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577void QProcess::setProgram(
const QString &program)
2580 if (d->processState != NotRunning) {
2581 qWarning(
"QProcess::setProgram: Process is already running");
2584 d->program = program;
2588
2589
2590
2591
2592
2593
2594QStringList QProcess::arguments()
const
2596 Q_D(
const QProcess);
2597 return d->arguments;
2601
2602
2603
2604
2605
2606
2607
2608void QProcess::setArguments(
const QStringList &arguments)
2611 if (d->processState != NotRunning) {
2612 qWarning(
"QProcess::setProgram: Process is already running");
2615 d->arguments = arguments;
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634void QProcess::terminate()
2637 d->terminateProcess();
2641
2642
2643
2644
2645
2646
2647
2648void QProcess::kill()
2655
2656
2657
2658
2659int QProcess::exitCode()
const
2661 Q_D(
const QProcess);
2666
2667
2668
2669
2670
2671
2672
2673
2674QProcess::ExitStatus QProcess::exitStatus()
const
2676 Q_D(
const QProcess);
2677 return ExitStatus(d->exitStatus);
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697int QProcess::execute(
const QString &program,
const QStringList &arguments)
2700 process.setProcessChannelMode(ForwardedChannels);
2702 process.start(program, arguments);
2703 if (!process.waitForFinished(-1) || process.error() == FailedToStart)
2705 return process.exitStatus() == QProcess::NormalExit ? process.exitCode() : -1;
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727bool QProcess::startDetached(
const QString &program,
2728 const QStringList &arguments,
2729 const QString &workingDirectory,
2734 process.setProgram(program);
2735 process.setArguments(arguments);
2736 process.setWorkingDirectory(workingDirectory);
2737 return process.startDetached(pid);
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760QStringList QProcess::systemEnvironment()
2762 return QProcessEnvironment::systemEnvironment().toStringList();
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794QString QProcess::nullDevice()
2797 return QStringLiteral(
"\\\\.\\NUL");
2798#elif defined(_PATH_DEVNULL)
2799 return QStringLiteral(_PATH_DEVNULL);
2801 return QStringLiteral(
"/dev/null");
2809#if QT_CONFIG(process)
2810#include "moc_qprocess.cpp"
void insert(const QProcessEnvironmentPrivate &other)
Combined button and popup list for selecting options.
bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs)