Warning: That file was not part of the compilation database. It may have many parsing errors.
1 | /* |
---|---|
2 | |
3 | xconsole widget for KDM |
4 | |
5 | Copyright (C) 2002-2003 Oswald Buddenhagen <ossi@kde.org> |
6 | |
7 | |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2 of the License, or |
11 | (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21 | |
22 | */ |
23 | |
24 | #include "kconsole.h" |
25 | #include "kdmconfig.h" |
26 | #include "kdm_greet.h" |
27 | |
28 | #include <klocale.h> |
29 | #include <kpty.h> |
30 | |
31 | #include <QSocketNotifier> |
32 | #include <QScrollBar> |
33 | |
34 | #include <stdlib.h> |
35 | #include <stdio.h> |
36 | #include <unistd.h> |
37 | #include <fcntl.h> |
38 | #include <errno.h> |
39 | #include <sys/ioctl.h> |
40 | |
41 | #ifdef HAVE_TERMIOS_H |
42 | /* for HP-UX (some versions) the extern C is needed, and for other |
43 | platforms it doesn't hurt */ |
44 | extern "C" { |
45 | #include <termios.h> |
46 | } |
47 | #endif |
48 | #if !defined(__osf__) |
49 | #ifdef HAVE_TERMIO_H |
50 | /* needed at least on AIX */ |
51 | #include <termio.h> |
52 | #endif |
53 | #endif |
54 | |
55 | #if defined (_HPUX_SOURCE) |
56 | #define _TERMIOS_INCLUDED |
57 | #include <bsdtty.h> |
58 | #endif |
59 | |
60 | KConsole::KConsole(QWidget *_parent) |
61 | : inherited(_parent) |
62 | , pty(0) |
63 | , notifier(0) |
64 | , fd(-1) |
65 | { |
66 | setReadOnly(true); |
67 | setLineWrapMode(NoWrap); |
68 | |
69 | if (!openConsole()) |
70 | append(i18n("*** Cannot connect to console log ***")); |
71 | } |
72 | |
73 | KConsole::~KConsole() |
74 | { |
75 | closeConsole(); |
76 | } |
77 | |
78 | int |
79 | KConsole::openConsole() |
80 | { |
81 | #ifdef TIOCCONS |
82 | static const char on = 1; |
83 | #endif |
84 | |
85 | if (*_logSource) { |
86 | if ((fd = open(_logSource, O_RDONLY | O_NONBLOCK)) >= 0) |
87 | goto gotcon; |
88 | logError("Cannot open log source %s, " |
89 | "falling back to /dev/console.\n", _logSource); |
90 | } |
91 | |
92 | pty = new KPty; |
93 | if (!pty->open()) { |
94 | delete pty; |
95 | pty = 0; |
96 | return 0; |
97 | } |
98 | |
99 | #ifdef TIOCCONS |
100 | if (ioctl(pty->slaveFd(), TIOCCONS, &on) < 0) { |
101 | perror("ioctl TIOCCONS"); |
102 | delete pty; |
103 | pty = 0; |
104 | return 0; |
105 | } |
106 | #else |
107 | int consfd; |
108 | if ((consfd = open("/dev/console", O_RDONLY)) < 0) { |
109 | perror("opening /dev/console"); |
110 | delete pty; |
111 | pty = 0; |
112 | return 0; |
113 | } |
114 | if (ioctl(consfd, SRIOCSREDIR, pty->slaveFd()) < 0) { |
115 | perror("ioctl SRIOCSREDIR"); |
116 | ::close(consfd); |
117 | delete pty; |
118 | pty = 0; |
119 | return 0; |
120 | } |
121 | ::close(consfd); |
122 | #endif |
123 | fd = pty->masterFd(); |
124 | |
125 | gotcon: |
126 | notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this); |
127 | connect(notifier, SIGNAL(activated(int)), SLOT(slotData())); |
128 | return 1; |
129 | } |
130 | |
131 | void |
132 | KConsole::closeConsole() |
133 | { |
134 | delete notifier; |
135 | notifier = 0; |
136 | if (pty) { |
137 | delete pty; |
138 | pty = 0; |
139 | } else { |
140 | ::close(fd); |
141 | } |
142 | fd = -1; |
143 | } |
144 | |
145 | void |
146 | KConsole::slotData() |
147 | { |
148 | int n; |
149 | char buffer[1024]; |
150 | |
151 | if ((n = read(fd, buffer, sizeof(buffer))) <= 0) { |
152 | closeConsole(); |
153 | if (n || !openConsole()) |
154 | append(i18n("\n*** Lost connection with console log ***")); |
155 | } else { |
156 | QString str(QString::fromLocal8Bit(buffer, n).remove( |
157 | int pos, opos; |
158 | for (opos = 0; (pos = str.indexOf( |
159 | if (document()->blockCount() == 100) { |
160 | QTextCursor cur(document()); |
161 | cur.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor); |
162 | cur.removeSelectedText(); |
163 | } |
164 | if (!leftover.isEmpty()) { |
165 | append(leftover + str.mid(opos, pos - opos)); |
166 | leftover.clear(); |
167 | } else { |
168 | append(str.mid(opos, pos - opos)); |
169 | } |
170 | } |
171 | leftover += str.mid(opos); |
172 | } |
173 | } |
174 | |
175 | #include "kconsole.moc" |
176 |
Warning: That file was not part of the compilation database. It may have many parsing errors.