OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / outputcollector.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "outputcollector.h"
34
35 #ifdef Q_OS_WIN
36
37 #include <QtNetwork/QLocalServer>
38 #include <QtNetwork/QLocalSocket>
39 #include <QtCore/QCoreApplication>
40
41 #include <stdlib.h>
42
43 #else
44
45 #include <QtCore/QFile>
46 #include <QtCore/QSocketNotifier>
47 #include <QtCore/QTemporaryFile>
48 #include <QtCore/QVarLengthArray>
49
50 #include <sys/ioctl.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #ifdef Q_OS_SOLARIS
54 # include <sys/filio.h> // FIONREAD
55 #endif
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #endif
62
63 namespace Debugger {
64 namespace Internal {
65
66 OutputCollector::OutputCollector(QObject *parent)
67         : QObject(parent)
68 {
69 #ifdef Q_OS_WIN
70     m_server = 0;
71     m_socket = 0;
72 #endif
73 }
74
75 OutputCollector::~OutputCollector()
76 {
77     shutdown();
78 }
79
80 bool OutputCollector::listen()
81 {
82 #ifdef Q_OS_WIN
83     if (m_server)
84         return m_server->isListening();
85     m_server = new QLocalServer(this);
86     connect(m_server, SIGNAL(newConnection()), SLOT(newConnectionAvailable()));
87     return m_server->listen(QString::fromLatin1("creator-%1-%2")
88                             .arg(QCoreApplication::applicationPid())
89                             .arg(rand()));
90 #else
91     if (!m_serverPath.isEmpty())
92         return true;
93     QByteArray codedServerPath;
94     forever {
95         {
96             QTemporaryFile tf;
97             if (!tf.open()) {
98                 m_errorString = tr("Cannot create temporary file: %1").arg(tf.errorString());
99                 m_serverPath.clear();
100                 return false;
101             }
102             m_serverPath = tf.fileName();
103         }
104         // By now the temp file was deleted again
105         codedServerPath = QFile::encodeName(m_serverPath);
106         if (!::mkfifo(codedServerPath.constData(), 0600))
107             break;
108         if (errno != EEXIST) {
109             m_errorString = tr("Cannot create FiFo %1: %2").arg(m_serverPath, strerror(errno));
110             m_serverPath.clear();
111             return false;
112         }
113     }
114     if ((m_serverFd = ::open(codedServerPath.constData(), O_RDONLY|O_NONBLOCK)) < 0) {
115         m_errorString = tr("Cannot open FiFo %1: %2").arg(m_serverPath, strerror(errno));
116         m_serverPath.clear();
117         return false;
118     }
119     m_serverNotifier = new QSocketNotifier(m_serverFd, QSocketNotifier::Read, this);
120     connect(m_serverNotifier, SIGNAL(activated(int)), SLOT(bytesAvailable()));
121     return true;
122 #endif
123 }
124
125 void OutputCollector::shutdown()
126 {
127 #ifdef Q_OS_WIN
128     delete m_server; // Deletes socket as well (QObject parent)
129     m_server = 0;
130     m_socket = 0;
131 #else
132     if (!m_serverPath.isEmpty()) {
133         ::close(m_serverFd);
134         ::unlink(QFile::encodeName(m_serverPath).constData());
135         delete m_serverNotifier;
136         m_serverPath.clear();
137     }
138 #endif
139 }
140
141 QString OutputCollector::errorString() const
142 {
143 #ifdef Q_OS_WIN
144     return m_socket ? m_socket->errorString() : m_server->errorString();
145 #else
146     return m_errorString;
147 #endif
148 }
149
150 QString OutputCollector::serverName() const
151 {
152 #ifdef Q_OS_WIN
153     return m_server->fullServerName();
154 #else
155     return m_serverPath;
156 #endif
157 }
158
159 #ifdef Q_OS_WIN
160 void OutputCollector::newConnectionAvailable()
161 {
162     if (m_socket)
163         return;
164     m_socket = m_server->nextPendingConnection();
165     connect(m_socket, SIGNAL(readyRead()), SLOT(bytesAvailable()));
166 }
167 #endif
168
169 void OutputCollector::bytesAvailable()
170 {
171 #ifdef Q_OS_WIN
172     emit byteDelivery(m_socket->readAll());
173 #else
174     size_t nbytes = 0;
175     if (::ioctl(m_serverFd, FIONREAD, (char *) &nbytes) < 0)
176         return;
177     QVarLengthArray<char, 8192> buff(nbytes);
178     if (::read(m_serverFd, buff.data(), nbytes) != (int)nbytes)
179         return;
180     if (nbytes) // Skip EOF notifications
181         emit byteDelivery(QByteArray::fromRawData(buff.data(), nbytes));
182 #endif
183 }
184
185 } // namespace Internal
186 } // namespace Debugger