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 
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 int printm(LPTSTR szModule, BOOL fSystem, DWORD dwMessageId, ...)
85 {
86 	int Result = 0;
87 
88 	HMODULE hModule = 0;
89 	if (szModule)
90 		hModule = LoadLibrary(szModule);
91 
92 	va_list Ellipsis;
93 	va_start(Ellipsis, dwMessageId);
94 
95 	LPTSTR pszMessage = 0;
96 	DWORD dwMessage = 0;
97 	if(fSystem) {
98 		dwMessage = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
99 			FORMAT_MESSAGE_FROM_SYSTEM, hModule, dwMessageId,
100 			LANG_USER_DEFAULT, (LPTSTR)&pszMessage, 128, &Ellipsis);
101 	} else {
102 		// we will use a string table.
103 		char szString[256];
104 		if(LoadString(0, dwMessageId, szString, sizeof(szString)))
105 			dwMessage = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
106 				FORMAT_MESSAGE_FROM_STRING, szString, dwMessageId,
107 				LANG_USER_DEFAULT, (LPTSTR)&pszMessage, 256, &Ellipsis);
108 	}
109 
110 	va_end(Ellipsis);
111 
112 	if (szModule)
113 		FreeLibrary(hModule);
114 
115 	if (dwMessage) {
116 
117 		Result = printit(pszMessage);
118 		LocalFree(pszMessage);
119 	}
120 
121 	return Result;
122 }
123 
124 
125 void LogErrorConsole(LPTSTR szError)
126 {
127 	DWORD dwLastError = GetLastError();
128 
129 	const int cbLastError = 1024;
130 	TCHAR szLastError[cbLastError];
131 	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwLastError, LANG_USER_DEFAULT,
132 		szLastError, cbLastError, 0);
133 
134 	LPTSTR lpszStrings[2];
135 	lpszStrings[0] = szError;
136 	lpszStrings[1] = szLastError;
137 
138 	const int cbErrorString = 1024;
139 	TCHAR szErrorString[cbErrorString];
140 	FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
141 		0, MSG_ERROR, LANG_USER_DEFAULT,
142 		szErrorString, cbErrorString, (va_list*)lpszStrings);
143 
144 	time_t dwTime;
145 	time(&dwTime);
146 	char* szTime = ctime(&dwTime);
147 	szTime[19] = 0;
148 
149 	//	printf("E %s %s", szTime + 11, szErrorString);
150 	char * buf;
151 	buf = new char [ 3 + strlen(szTime) - 11 + strlen(szErrorString) + 5 ];
152 	sprintf( buf,"E %s %s", szTime + 11, szErrorString);
153 	printit(buf);
154 	delete [] buf;
155 }
156 
157 
158 void LogWarningConsole(DWORD dwEvent, LPTSTR szWarning)
159 {
160 	DWORD dwLastError = GetLastError();
161 
162 	const int cbLastError = 1024;
163 	TCHAR szLastError[cbLastError];
164 	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwLastError, LANG_USER_DEFAULT,
165 		szLastError, cbLastError, 0);
166 
167 	LPTSTR lpszStrings[2];
168 	lpszStrings[0] = szWarning;
169 	lpszStrings[1] = szLastError;
170 
171 	const int cbWarningString = 1024;
172 	TCHAR szWarningString[cbWarningString];
173 	FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
174 		0, dwEvent, LANG_USER_DEFAULT,
175 		szWarningString, cbWarningString, (va_list*)lpszStrings);
176 
177 	time_t dwTime;
178 	time(&dwTime);
179 	char* szTime = ctime(&dwTime);
180 	szTime[19] = 0;
181 
182 	//	printf("W %s %s", szTime + 11, szWarningString);
183 	char * buf;
184 	buf = new char [ 3 + strlen(szTime) - 11 + strlen(szWarningString) + 5 ];
185 	sprintf(buf ,"W %s %s", szTime + 11, szWarningString);
186 	printit(buf);
187 	delete [] buf;
188 
189 }
190 
191 
192 void LogInfoConsole(DWORD dwEvent, LPTSTR szInformation)
193 {
194 	LPTSTR lpszStrings[1];
195 	lpszStrings[0] = szInformation;
196 
197 	const int cbInfoString = 1024;
198 	TCHAR szInfoString[cbInfoString];
199 	FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
200 		0, dwEvent, LANG_USER_DEFAULT,
201 		szInfoString, cbInfoString, (va_list*)lpszStrings);
202 
203 	time_t dwTime;
204 	time(&dwTime);
205 	char* szTime = ctime(&dwTime);
206 	szTime[19] = 0;
207 
208 	//	printf("I %s %s", szTime + 11, szInfoString);
209 	char * buf;
210 	buf = new char [ 3 + strlen(szTime) - 11 + strlen(szInfoString) + 5 ];
211 	sprintf(buf,"I %s %s", szTime + 11, szInfoString);
212 	printit(buf);
213 	delete [] buf;
214 
215 }
216 
217