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