1 ///////////////////////////////////////////////////////////////////////////////
2 //Telnet Win32 : an ANSI telnet client.
3 //Copyright (C) 1998-2000 Paul Brannan
4 //Copyright (C) 1998 I.Ioannou
5 //Copyright (C) 1997 Brad Johnson
6 //
7 //This program is free software; you can redistribute it and/or
8 //modify it under the terms of the GNU General Public License
9 //as published by the Free Software Foundation; either version 2
10 //of the License, or (at your option) any later version.
11 //
12 //This program is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //GNU General Public License for more details.
16 //
17 //You should have received a copy of the GNU General Public License
18 //along with this program; if not, write to the Free Software
19 //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 //I.Ioannou
22 //roryt@hol.gr
23 //
24 ///////////////////////////////////////////////////////////////////////////
25 
26 ///////////////////////////////////////////////////////////////////////////////
27 //
28 // Module:		tnerror.cpp
29 //
30 // Contents:	error reporting
31 //
32 // Product:		telnet
33 //
34 // Revisions: June 15, 1998 Paul Brannan <pbranna@clemson.edu>
35 //            May 15, 1998 Paul Brannan
36 //            5.April.1997 jbj@nounname.com
37 //            5.Dec.1996 jbj@nounname.com
38 //            Version 2.0
39 //
40 //            02.Apr.1995	igor.milavec@uni-lj.si
41 //					  Original code
42 //
43 ///////////////////////////////////////////////////////////////////////////////
44 
45 #include "precomp.h"
46 
47 #include <time.h>
48 
49 #ifndef LANG_USER_DEFAULT
50 #define LANG_USER_DEFAULT 400
51 #endif
52 
53 // This has been moved to tnconfig.cpp
54 // int Telnet_Redir = 0;
55 // Telnet_Redir is set to the value of the environment variable TELNET_REDIR
56 // in main.
57 
printit(const char * it)58 int printit(const char * it){
59 	DWORD numwritten;
60 	if (!ini.get_output_redir()) {
61 		if (!WriteConsole(
62 			GetStdHandle(STD_OUTPUT_HANDLE),	// handle of a console screen buffer
63 			it,	// address of buffer to write from
64 			strlen(it),	// number of characters to write
65 			&numwritten,	// address of number of characters written
66 			0 	// reserved
67 			)) return -1;
68 		// FIX ME!!! We need to tell the console that the cursor has moved.
69 		// Does this mean making Console global?
70 		// Paul Brannan 6/14/98
71 		// Console.sync();
72 	}else{
73 		if (!WriteFile(
74 			GetStdHandle(STD_OUTPUT_HANDLE),	// handle of a console screen buffer
75 			it,	// address of buffer to write from
76 			strlen(it),	// number of characters to write
77 			&numwritten,	// address of number of characters written
78 			NULL // no overlapped I/O
79 			)) return -1;
80 	}
81 	return 0;
82 }
83 
84 #ifdef __REACTOS__
wprintit(LPCWSTR it)85 int wprintit(LPCWSTR it)
86 {
87     DWORD numwritten;
88     if (!ini.get_output_redir())
89     {
90         if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
91                            it, wcslen(it), &numwritten, NULL))
92         {
93             return -1;
94         }
95     }
96     else
97     {
98         // calculate the number of bytes needed to store the UTF-8 string
99         int cbMultibyte = WideCharToMultiByte(CP_UTF8, 0, it, -1, NULL, 0, NULL, NULL);
100         if (cbMultibyte == 0)
101             return 0;
102         if (cbMultibyte < 0)
103             return -1;
104         // allocate the buffer for the UTF-8 string
105         char* szBuffer = new char[cbMultibyte];
106         if (!szBuffer)
107             return -1;
108 
109         bool bSuccess = false;
110         if (WideCharToMultiByte(CP_UTF8, 0, it, -1, szBuffer, cbMultibyte, NULL, NULL) &&
111             WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),
112                       szBuffer, cbMultibyte, &numwritten, NULL))
113         {
114             bSuccess = true;
115         }
116 
117         delete[] szBuffer;
118         if (!bSuccess)
119             return -1;
120     }
121     return 0;
122 }
123 #endif
124 
printm(LPTSTR szModule,BOOL fSystem,DWORD dwMessageId,...)125 int printm(LPTSTR szModule, BOOL fSystem, DWORD dwMessageId, ...)
126 {
127 	int Result = 0;
128 
129 	HMODULE hModule = 0;
130 	if (szModule)
131 		hModule = LoadLibrary(szModule);
132 
133 	va_list Ellipsis;
134 	va_start(Ellipsis, dwMessageId);
135 #ifdef __REACTOS__
136 	LPWSTR pszMessage = NULL;
137 	DWORD dwMessage = 0;
138 
139 	if(fSystem) {
140 		dwMessage = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
141 			FORMAT_MESSAGE_FROM_SYSTEM, hModule, dwMessageId,
142 			LANG_USER_DEFAULT, (LPWSTR)&pszMessage, 128, &Ellipsis);
143 	} else {
144 		// we will use a string table.
145 		WCHAR wszString[256];
146 		if(LoadStringW(0, dwMessageId, wszString, sizeof(wszString) / sizeof(*wszString)))
147 			dwMessage = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
148 				FORMAT_MESSAGE_FROM_STRING, wszString, dwMessageId,
149 				LANG_USER_DEFAULT, (LPWSTR)&pszMessage, sizeof(wszString) / sizeof(*wszString), &Ellipsis);
150 	}
151 #else
152 	LPTSTR pszMessage = 0;
153 	DWORD dwMessage = 0;
154 	if(fSystem) {
155 		dwMessage = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
156 			FORMAT_MESSAGE_FROM_SYSTEM, hModule, dwMessageId,
157 			LANG_USER_DEFAULT, (LPTSTR)&pszMessage, 128, &Ellipsis);
158 	} else {
159 		// we will use a string table.
160 		char szString[256];
161 		if(LoadString(0, dwMessageId, szString, sizeof(szString)))
162 			dwMessage = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
163 				FORMAT_MESSAGE_FROM_STRING, szString, dwMessageId,
164 				LANG_USER_DEFAULT, (LPTSTR)&pszMessage, 256, &Ellipsis);
165 	}
166 #endif
167 
168 	va_end(Ellipsis);
169 
170 	if (szModule)
171 		FreeLibrary(hModule);
172 
173 	if (dwMessage) {
174 #ifdef __REACTOS__
175 		Result = wprintit(pszMessage);
176 #else
177 		Result = printit(pszMessage);
178 #endif
179 		LocalFree(pszMessage);
180 	}
181 
182 	return Result;
183 }
184 
185 
LogErrorConsole(LPTSTR szError)186 void LogErrorConsole(LPTSTR szError)
187 {
188 	DWORD dwLastError = GetLastError();
189 
190 	const int cbLastError = 1024;
191 	TCHAR szLastError[cbLastError];
192 	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwLastError, LANG_USER_DEFAULT,
193 		szLastError, cbLastError, 0);
194 
195 	LPTSTR lpszStrings[2];
196 	lpszStrings[0] = szError;
197 	lpszStrings[1] = szLastError;
198 
199 	const int cbErrorString = 1024;
200 	TCHAR szErrorString[cbErrorString];
201 	FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
202 		0, MSG_ERROR, LANG_USER_DEFAULT,
203 		szErrorString, cbErrorString, (va_list*)lpszStrings);
204 
205 	time_t dwTime;
206 	time(&dwTime);
207 	char* szTime = ctime(&dwTime);
208 	szTime[19] = 0;
209 
210 	//	printf("E %s %s", szTime + 11, szErrorString);
211 	char * buf;
212 	buf = new char [ 3 + strlen(szTime) - 11 + strlen(szErrorString) + 5 ];
213 	sprintf( buf,"E %s %s", szTime + 11, szErrorString);
214 	printit(buf);
215 	delete [] buf;
216 }
217 
218 
LogWarningConsole(DWORD dwEvent,LPTSTR szWarning)219 void LogWarningConsole(DWORD dwEvent, LPTSTR szWarning)
220 {
221 	DWORD dwLastError = GetLastError();
222 
223 	const int cbLastError = 1024;
224 	TCHAR szLastError[cbLastError];
225 	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwLastError, LANG_USER_DEFAULT,
226 		szLastError, cbLastError, 0);
227 
228 	LPTSTR lpszStrings[2];
229 	lpszStrings[0] = szWarning;
230 	lpszStrings[1] = szLastError;
231 
232 	const int cbWarningString = 1024;
233 	TCHAR szWarningString[cbWarningString];
234 	FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
235 		0, dwEvent, LANG_USER_DEFAULT,
236 		szWarningString, cbWarningString, (va_list*)lpszStrings);
237 
238 	time_t dwTime;
239 	time(&dwTime);
240 	char* szTime = ctime(&dwTime);
241 	szTime[19] = 0;
242 
243 	//	printf("W %s %s", szTime + 11, szWarningString);
244 	char * buf;
245 	buf = new char [ 3 + strlen(szTime) - 11 + strlen(szWarningString) + 5 ];
246 	sprintf(buf ,"W %s %s", szTime + 11, szWarningString);
247 	printit(buf);
248 	delete [] buf;
249 
250 }
251 
252 
LogInfoConsole(DWORD dwEvent,LPTSTR szInformation)253 void LogInfoConsole(DWORD dwEvent, LPTSTR szInformation)
254 {
255 	LPTSTR lpszStrings[1];
256 	lpszStrings[0] = szInformation;
257 
258 	const int cbInfoString = 1024;
259 	TCHAR szInfoString[cbInfoString];
260 	FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
261 		0, dwEvent, LANG_USER_DEFAULT,
262 		szInfoString, cbInfoString, (va_list*)lpszStrings);
263 
264 	time_t dwTime;
265 	time(&dwTime);
266 	char* szTime = ctime(&dwTime);
267 	szTime[19] = 0;
268 
269 	//	printf("I %s %s", szTime + 11, szInfoString);
270 	char * buf;
271 	buf = new char [ 3 + strlen(szTime) - 11 + strlen(szInfoString) + 5 ];
272 	sprintf(buf,"I %s %s", szTime + 11, szInfoString);
273 	printit(buf);
274 	delete [] buf;
275 
276 }
277 
278