source: trunk/src/kernel32/hmstd.cpp

Last change on this file was 8807, checked in by sandervl, 23 years ago

small fixes

File size: 7.8 KB
RevLine 
[8807]1/* $Id: hmstd.cpp,v 1.12 2002-06-30 09:57:13 sandervl Exp $ */
[5019]2
3/*
4 * Handle Manager class for standard in, out & error handles
5 *
6 * Project Odin Software License can be found in LICENSE.TXT
7 * Win32 Unified Handle Manager for OS/2
8 * Copyright 1999 Patrick Haller (haller@zebra.fh-weingarten.de)
9 */
10
11
12/*****************************************************************************
13 * Remark *
14 *****************************************************************************
15
16 */
17
18
19/*****************************************************************************
20 * Includes *
21 *****************************************************************************/
22
23#include <os2win.h>
24#include <stdlib.h>
[8806]25#include <stdio.h>
[5019]26#include <string.h>
[6511]27#include <unicode.h>
[8806]28#include <dbglog.h>
[5019]29
30#include "HandleManager.H"
31#include "hmstd.h"
[6511]32#include "winexebase.h"
[5019]33
34#define DBG_LOCALLOG DBG_hmstd
35#include "dbglocal.h"
36
37/*****************************************************************************
38 * Defines *
39 *****************************************************************************/
40
41/*****************************************************************************
42 * Structures *
43 *****************************************************************************/
44
45/*****************************************************************************
46 * Local Prototypes *
47 *****************************************************************************/
48
49
50
51/*****************************************************************************
52 * Name : BOOL HMDeviceStandardClass::ReadFile
53 * Purpose : read data from handle / device
54 * Parameters: PHMHANDLEDATA pHMHandleData,
55 * LPCVOID lpBuffer,
56 * DWORD nNumberOfBytesToRead,
57 * LPDWORD lpNumberOfBytesRead,
58 * LPOVERLAPPED lpOverlapped
59 * Variables :
60 * Result : Boolean
61 * Remark :
62 * Status :
63 *
64 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
65 *****************************************************************************/
66
67BOOL HMDeviceStandardClass::ReadFile(PHMHANDLEDATA pHMHandleData,
68 LPCVOID lpBuffer,
69 DWORD nNumberOfBytesToRead,
70 LPDWORD lpNumberOfBytesRead,
[7549]71 LPOVERLAPPED lpOverlapped,
72 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
[5019]73{
[8806]74 BOOL bRC;
75 DWORD bytesread;
[5019]76
[8806]77 dprintf2(("KERNEL32: HMDeviceStandardClass::ReadFile %s(%08x,%08x,%08x,%08x,%08x) - NOT IMPLEMENTED\n",
78 lpHMDeviceName, pHMHandleData, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped));
[5019]79
[8806]80 if(lpCompletionRoutine) {
81 dprintf(("!WARNING!: lpCompletionRoutine not supported -> fall back to sync IO"));
82 }
83
84 if(lpNumberOfBytesRead == NULL) {
85 lpNumberOfBytesRead = &bytesread;
86 }
87 if(pHMHandleData->dwUserData != STD_INPUT_HANDLE) {
88 return FALSE;
89 }
90 return O32_ReadFile(pHMHandleData->hHMHandle, (LPVOID)lpBuffer, nNumberOfBytesToRead,
91 lpNumberOfBytesRead, lpOverlapped);
[5019]92}
93
94
95/*****************************************************************************
96 * Name : BOOL HMDeviceStandardClass::WriteFile
97 * Purpose : write data to handle / device
98 * Parameters: PHMHANDLEDATA pHMHandleData,
99 * LPCVOID lpBuffer,
100 * DWORD nNumberOfBytesToWrite,
101 * LPDWORD lpNumberOfBytesWritten,
102 * LPOVERLAPPED lpOverlapped
103 * Variables :
104 * Result : Boolean
105 * Remark :
106 * Status :
107 *
108 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
109 *****************************************************************************/
[8806]110#define CARRIAGE_RETURN 0xD
111#define LINE_FEED 0xA
[5019]112
113BOOL HMDeviceStandardClass::WriteFile(PHMHANDLEDATA pHMHandleData,
114 LPCVOID lpBuffer,
115 DWORD nNumberOfBytesToWrite,
116 LPDWORD lpNumberOfBytesWritten,
[7549]117 LPOVERLAPPED lpOverlapped,
118 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
[5019]119{
[8773]120 DWORD byteswritten;
121 LPVOID lpLowMemBuffer;
[5019]122
[8773]123 dprintf(("KERNEL32: HMDeviceStandardClass::WriteFile %s(%08x,%08x,%08x,%08x,%08x)",
[8806]124 lpHMDeviceName, pHMHandleData, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten,
[8773]125 lpOverlapped));
[8806]126
127 if(lpNumberOfBytesWritten == NULL)
[8773]128 lpNumberOfBytesWritten = &byteswritten;
[8806]129
130 if(lpCompletionRoutine)
[8773]131 {
132 dprintf(("!WARNING!: lpCompletionRoutine not supported -> fall back to sync IO"));
133 }
[7550]134
[8806]135 if(pHMHandleData->dwUserData == STD_INPUT_HANDLE)
[8773]136 return FALSE;
[8806]137
138 //count linefeed without carriage return occurances
139 char *src, *dest;
140 int i = 0;
141 int missingCR = 0;
142 char prevchar = 0;
143
144 src = (char *)lpBuffer;
145 while(i < nNumberOfBytesToWrite) {
146 if(src[i] == LINE_FEED && prevchar != CARRIAGE_RETURN) {
147 missingCR++;
148 }
149 prevchar = src[i];
150 i++;
151 }
152
[8807]153 lpLowMemBuffer = alloca(nNumberOfBytesToWrite+missingCR);
[8806]154 if(lpLowMemBuffer == NULL)
[8773]155 {
156 DebugInt3();
157 return FALSE;
158 }
159
[8806]160 //convert linefeed without carriage return into LF+CR
161 i = 0;
162 missingCR = 0;
163 prevchar = 0;
164 src = (char *)lpBuffer;
165 dest = (char *)lpLowMemBuffer;
166
167 while(i < nNumberOfBytesToWrite) {
168 if(src[i] == LINE_FEED && prevchar != CARRIAGE_RETURN) {
169 dest[i+missingCR] = CARRIAGE_RETURN;
170 missingCR++;
171 dest[i+missingCR] = LINE_FEED;
172 }
173 else {
174 dest[i+missingCR] = src[i];
175 }
176 prevchar = src[i];
177 i++;
178 }
[8807]179 nNumberOfBytesToWrite += missingCR;
[8806]180
181 if(WinExe && !WinExe->isConsoleApp() && O32_GetFileType(pHMHandleData->hHMHandle) == FILE_TYPE_UNKNOWN) /* kso */
[8773]182 {
183 //DosWrite returns error 436 when PM apps try to write to std out
184 //kso - Jun 23 2002 2:54am:
185 //Yeah, cause PM programs doesn't have working STD* handles unless you redirect them!
186 //So, we should rather check if valid handle than !console.
[8795]187 dprintf(("%s (GUI): %.*s", pHMHandleData->dwUserData == STD_ERROR_HANDLE ? "STDERR" : "STDOUT",
[8773]188 nNumberOfBytesToWrite, lpLowMemBuffer));
189 return TRUE;
190 }
191
[8795]192 dprintf(("%s: %.*s", pHMHandleData->dwUserData == STD_ERROR_HANDLE ? "STDERR" : "STDOUT",
[8773]193 nNumberOfBytesToWrite, lpLowMemBuffer));
[8806]194
195
196 if(!O32_WriteFile(pHMHandleData->hHMHandle, lpLowMemBuffer, nNumberOfBytesToWrite,
197 lpNumberOfBytesWritten, lpOverlapped))
[8773]198 {
199 dprintf(("STD*: failed with lasterror=%d\n", GetLastError()));
200 return FALSE;
201 }
202 return TRUE;
[5019]203}
204
205
206/*****************************************************************************
207 * Name : DWORD HMDeviceStandardClass::GetFileType
208 * Purpose : determine the handle type
209 * Parameters: PHMHANDLEDATA pHMHandleData
210 * Variables :
211 * Result : API returncode
212 * Remark :
213 * Status :
214 *
215 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
216 *****************************************************************************/
217
218DWORD HMDeviceStandardClass::GetFileType(PHMHANDLEDATA pHMHandleData)
219{
220 dprintf2(("KERNEL32: HMDeviceStandardClass::GetFileType %s(%08x)\n",
221 lpHMDeviceName,
222 pHMHandleData));
[8773]223 #if 0
[5019]224 return FILE_TYPE_CHAR;
[8773]225 #else
226 return O32_GetFileType(pHMHandleData->hHMHandle);
227 #endif
[5019]228}
Note: See TracBrowser for help on using the repository browser.