File indexing completed on 2024-04-14 14:26:24

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2004, 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2004, 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2004, 2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2004, 2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include "kjseglobal.h"
0024 
0025 #ifdef QT_ONLY
0026 # include <QObject>
0027 # include <cstdio>
0028 #endif
0029 
0030 #if defined(_WIN32) || defined(_WIN64)
0031 # include <windows.h>
0032 # include <fcntl.h>
0033 # include <io.h>
0034 # include <ios>
0035 # include <QFile>
0036 # include <QTextStream>
0037 #endif
0038 
0039 static QTextStream *kjsembed_err = nullptr;
0040 static QTextStream *kjsembed_in = nullptr;
0041 static QTextStream *kjsembed_out = nullptr;
0042 
0043 #if defined(_WIN32) || defined(_WIN64)
0044 static QFile win32_stdin;
0045 static QFile win32_stdout;
0046 static QFile win32_stderr;
0047 
0048 static const WORD MAX_CONSOLE_LINES = 500;
0049 
0050 void RedirectIOToConsole()
0051 {
0052     int hConHandle;
0053     intptr_t lStdHandle;
0054     CONSOLE_SCREEN_BUFFER_INFO coninfo;
0055     AllocConsole();
0056     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
0057     coninfo.dwSize.Y = MAX_CONSOLE_LINES;
0058     SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
0059 
0060     lStdHandle = (intptr_t)GetStdHandle(STD_INPUT_HANDLE);
0061     hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
0062     win32_stdin.open(hConHandle, QIODevice::ReadOnly);
0063 
0064     lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
0065     hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
0066     win32_stdout.open(hConHandle, QIODevice::WriteOnly);
0067 
0068     lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE);
0069     hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
0070     win32_stderr.open(hConHandle, QIODevice::WriteOnly);
0071 
0072     std::ios::sync_with_stdio();
0073 
0074 }
0075 
0076 #endif
0077 
0078 QTextStream &consoleOut()
0079 {
0080     return *KJSEmbed::conout();
0081 }
0082 
0083 QTextStream &consoleError()
0084 {
0085     return *KJSEmbed::conerr();
0086 }
0087 
0088 QTextStream &consoleIn()
0089 {
0090     return *KJSEmbed::conin();
0091 }
0092 
0093 #ifdef QT_ONLY
0094 QTextStream &kdDebug(int area)
0095 {
0096 #ifndef QT_DEBUG
0097     return consoleError() << "DEBUG: (" << area << ") ";
0098 #else
0099     return consoleOut();
0100 #endif
0101 
0102 }
0103 
0104 QTextStream &kdWarning(int area)
0105 {
0106     return consoleOut() << "WARNING: (" << area << ") ";
0107 }
0108 
0109 QString i18n(const char *string)
0110 {
0111     return QCoreApplication::translate("KJSEmbed", string, "qjsembed string");
0112 }
0113 
0114 #endif
0115 
0116 QTextStream *KJSEmbed::conin()
0117 {
0118     if (!kjsembed_in) {
0119 #ifdef _WIN32
0120         kjsembed_in = new QTextStream(&win32_stdin);
0121 #else
0122         kjsembed_in = new QTextStream(stdin, QIODevice::ReadOnly);
0123 #endif
0124     }
0125     return kjsembed_in;
0126 }
0127 
0128 QTextStream *KJSEmbed::conout()
0129 {
0130     if (!kjsembed_out) {
0131 #ifdef _WIN32
0132         kjsembed_out = new QTextStream(&win32_stdout);
0133 #else
0134         kjsembed_out = new QTextStream(stdout, QIODevice::WriteOnly);
0135 #endif
0136     }
0137     return kjsembed_out;
0138 
0139 }
0140 
0141 QTextStream *KJSEmbed::conerr()
0142 {
0143     if (!kjsembed_err) {
0144 #ifdef _WIN32
0145         kjsembed_err = new QTextStream(&win32_stderr);
0146 #else
0147         kjsembed_err = new QTextStream(stderr, QIODevice::WriteOnly);
0148 #endif
0149     }
0150     return kjsembed_err;
0151 }
0152