summaryrefslogtreecommitdiffabout
path: root/kinsole.cpp
authorMichael Krelin <hacker@klever.net>2004-07-11 04:29:12 (UTC)
committer Michael Krelin <hacker@klever.net>2004-07-11 04:29:12 (UTC)
commitd6f931eb800d16d0bf116765cbaeef83d232cfad (patch) (unidiff)
tree811e8513e303825ebf028ea9df332a243cdc38f8 /kinsole.cpp
parent885f8cc426a8840ae61023b75f3f0e4a1e268082 (diff)
downloadkinsole-d6f931eb800d16d0bf116765cbaeef83d232cfad.zip
kinsole-d6f931eb800d16d0bf116765cbaeef83d232cfad.tar.gz
kinsole-d6f931eb800d16d0bf116765cbaeef83d232cfad.tar.bz2
prepared for kinsole 1.1 release1.1
git-svn-id: http://svn.klever.net/kin/kinsole/trunk@7 fe716a7a-6dde-0310-88d9-d003556173a8
Diffstat (limited to 'kinsole.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kinsole.cpp6
1 files changed, 3 insertions, 3 deletions
diff --git a/kinsole.cpp b/kinsole.cpp
index 463db1c..01286f3 100644
--- a/kinsole.cpp
+++ b/kinsole.cpp
@@ -1,736 +1,736 @@
1#include <stdio.h> 1#include <stdio.h>
2 2
3 #ifdef_DEBUG 3 #ifdef_DEBUG
4#include <afxwin.h> 4#include <afxwin.h>
5#else 5#else
6#define ASSERT(f) ((void)0) 6#define ASSERT(f) ((void)0)
7#define VERIFY(f) ((void)(f)) 7#define VERIFY(f) ((void)(f))
8#define TRACE0(sz) 8#define TRACE0(sz)
9#define TRACE1(sz, p1) 9#define TRACE1(sz, p1)
10#define TRACE2(sz, p1, p2) 10#define TRACE2(sz, p1, p2)
11#define TRACE3(sz, p1, p2, p3) 11#define TRACE3(sz, p1, p2, p3)
12#endif 12#endif
13 13
14#include <winsock.h> 14#include <winsock.h>
15#include "resource.h" 15#include "resource.h"
16#include "windowsx.h" 16#include "windowsx.h"
17 17
18 #define DAMN_KIN_NAME "KINSole" 18 #define DAMN_KIN_NAME "KINSole"
19 #define DAMN_KIN_VERSION"v1.00 Beta 3" 19 #define DAMN_KIN_VERSION"1.1"
20 20
21#ifdef _DEBUG 21#ifdef _DEBUG
22#define new DEBUG_NEW 22#define new DEBUG_NEW
23#undef THIS_FILE 23#undef THIS_FILE
24static char THIS_FILE[] = __FILE__; 24static char THIS_FILE[] = __FILE__;
25#endif 25#endif
26 26
27enum { 27enum {
28 WM_USERSOCKET = WM_USER+16, 28 WM_USERSOCKET = WM_USER+16,
29 WM_USERKEY, 29 WM_USERKEY,
30 WM_USERNOP 30 WM_USERNOP
31}; 31};
32enum { 32enum {
33 tnIAC = 255, 33 tnIAC = 255,
34 tnSE = 240, 34 tnSE = 240,
35 tnNOP = 241, 35 tnNOP = 241,
36 tnDM = 242, 36 tnDM = 242,
37 tnBreak = 243, 37 tnBreak = 243,
38 tnIP = 244, 38 tnIP = 244,
39 tnAO = 245, 39 tnAO = 245,
40 tnAYT = 246, 40 tnAYT = 246,
41 tnEC = 247, 41 tnEC = 247,
42 tnEL = 248, 42 tnEL = 248,
43 tnGA = 249, 43 tnGA = 249,
44 tnSB = 250, 44 tnSB = 250,
45 tnWILL = 251, 45 tnWILL = 251,
46 tnWONT = 252, 46 tnWONT = 252,
47 tnDO = 253, 47 tnDO = 253,
48 tnDONT = 254 48 tnDONT = 254
49}; 49};
50 50
51WSADATA wsaData; 51WSADATA wsaData;
52ATOM wsaWC = NULL; 52ATOM wsaWC = NULL;
53HWND wsaW = NULL; 53HWND wsaW = NULL;
54 54
55CHAR remoteHost[256]; 55CHAR remoteHost[256];
56CHAR remoteProt[256]; 56CHAR remoteProt[256];
57sockaddr_in remoteSIN; 57sockaddr_in remoteSIN;
58 58
59 59
60SOCKET telnetSocket; 60SOCKET telnetSocket;
61HANDLE hConsoleInput; 61HANDLE hConsoleInput;
62HANDLE hConsoleOutput; 62HANDLE hConsoleOutput;
63HWND hConsoleWindow; 63HWND hConsoleWindow;
64HANDLE hConsoleThread; 64HANDLE hConsoleThread;
65DWORD consoleThreadID; 65DWORD consoleThreadID;
66HANDLE hDispatchThread; 66HANDLE hDispatchThread;
67DWORD dispatchThreadID; 67DWORD dispatchThreadID;
68BOOL bTelnet,bTermPulled; 68BOOL bTelnet,bTermPulled;
69 69
70 enum_cState { 70 enum_cState {
71 cstateNone = 0, cstateIAC, cstateDO, cstateSB, cstateSBData, 71 cstateNone = 0, cstateIAC, cstateDO, cstateSB, cstateSBData,
72 cstateSBDataIAC, cstateWILL, cstateDONT,cstateWONT, 72 cstateSBDataIAC, cstateWILL, cstateDONT,cstateWONT,
73 }connState = cstateNone; 73 }connState = cstateNone;
74BYTE negOption = 0; 74BYTE negOption = 0;
75 75
76 76
77BOOL SelectSocket() { 77BOOL SelectSocket() {
78 return WSAAsyncSelect(telnetSocket,wsaW,WM_USERSOCKET,FD_READ|FD_OOB|FD_CLOSE)!=SOCKET_ERROR; 78 return WSAAsyncSelect(telnetSocket,wsaW,WM_USERSOCKET,FD_READ|FD_OOB|FD_CLOSE)!=SOCKET_ERROR;
79} 79}
80 80
81BOOL ShowWill(BYTE o) 81BOOL ShowWill(BYTE o)
82{ 82{
83 TRACE1("We're WILLing to %d\n",(WORD)o); 83 TRACE1("We're WILLing to %d\n",(WORD)o);
84static 84static
85BYTE d[3] = {tnIAC,tnWILL,0}; 85BYTE d[3] = {tnIAC,tnWILL,0};
86 d[2] = o; 86 d[2] = o;
87BOOL rv = send(telnetSocket,(char*)d,sizeof(d),0)==sizeof(d); 87BOOL rv = send(telnetSocket,(char*)d,sizeof(d),0)==sizeof(d);
88 SelectSocket(); 88 SelectSocket();
89 return rv; 89 return rv;
90} 90}
91BOOL ShowUnwill(BYTE o) 91BOOL ShowUnwill(BYTE o)
92{ 92{
93 TRACE1("We're NOT WILLing to %d\n",(WORD)o); 93 TRACE1("We're NOT WILLing to %d\n",(WORD)o);
94static 94static
95BYTE d[3] = {tnIAC,tnWONT,0}; 95BYTE d[3] = {tnIAC,tnWONT,0};
96 d[2] = o; 96 d[2] = o;
97BOOL rv = send(telnetSocket,(char*)d,sizeof(d),0)==sizeof(d); 97BOOL rv = send(telnetSocket,(char*)d,sizeof(d),0)==sizeof(d);
98 SelectSocket(); 98 SelectSocket();
99 return rv; 99 return rv;
100} 100}
101BOOL BegDo(BYTE o) 101BOOL BegDo(BYTE o)
102{ 102{
103 TRACE1("We beg to DO %d\n",(WORD)o); 103 TRACE1("We beg to DO %d\n",(WORD)o);
104static 104static
105BYTE d[3] = {tnIAC,tnDO,0}; 105BYTE d[3] = {tnIAC,tnDO,0};
106 d[2] = o; 106 d[2] = o;
107BOOL rv = send(telnetSocket,(char*)d,sizeof(d),0)==sizeof(d); 107BOOL rv = send(telnetSocket,(char*)d,sizeof(d),0)==sizeof(d);
108 SelectSocket(); 108 SelectSocket();
109 return rv; 109 return rv;
110} 110}
111BOOL BegDont(BYTE o) 111BOOL BegDont(BYTE o)
112{ 112{
113 TRACE1("We beg DONT'T %d\n",(WORD)o); 113 TRACE1("We beg DONT'T %d\n",(WORD)o);
114static 114static
115BYTE d[3] = {tnIAC,tnDONT,0}; 115BYTE d[3] = {tnIAC,tnDONT,0};
116 d[2] = o; 116 d[2] = o;
117BOOL rv = send(telnetSocket,(char*)d,sizeof(d),0)==sizeof(d); 117BOOL rv = send(telnetSocket,(char*)d,sizeof(d),0)==sizeof(d);
118 SelectSocket(); 118 SelectSocket();
119 return rv; 119 return rv;
120} 120}
121BOOL SubNegotiate(BYTE o,LPBYTE data,UINT size) 121BOOL SubNegotiate(BYTE o,LPBYTE data,UINT size)
122{ 122{
123LPBYTE d = new BYTE[3+size*2+2]; 123LPBYTE d = new BYTE[3+size*2+2];
124int ds = 0; 124int ds = 0;
125 d[ds++]=tnIAC; d[ds++]=tnSB; d[ds++]=o; 125 d[ds++]=tnIAC; d[ds++]=tnSB; d[ds++]=o;
126 for(UINT tmp=0;tmp<size;tmp++) 126 for(UINT tmp=0;tmp<size;tmp++)
127 if(data[tmp]!=tnIAC) 127 if(data[tmp]!=tnIAC)
128 d[ds++]=data[tmp]; 128 d[ds++]=data[tmp];
129 else{ 129 else{
130 d[ds++]=tnIAC; d[ds++]=tnIAC; 130 d[ds++]=tnIAC; d[ds++]=tnIAC;
131 } 131 }
132 d[ds++]=tnIAC;d[ds++]=tnSE; 132 d[ds++]=tnIAC;d[ds++]=tnSE;
133BOOL rv = send(telnetSocket,(char*)d,ds,0)==ds; 133BOOL rv = send(telnetSocket,(char*)d,ds,0)==ds;
134 delete d; 134 delete d;
135 SelectSocket(); 135 SelectSocket();
136 return rv; 136 return rv;
137} 137}
138BOOL SendLiteral(CHAR c) 138BOOL SendLiteral(CHAR c)
139{ 139{
140BYTE d[2] = {tnIAC,0}; 140BYTE d[2] = {tnIAC,0};
141BOOL rv = FALSE; 141BOOL rv = FALSE;
142 if(c==tnIAC){ 142 if(c==tnIAC){
143 d[1]=c; 143 d[1]=c;
144 rv = send(telnetSocket,(char*)d,2,0)==2; 144 rv = send(telnetSocket,(char*)d,2,0)==2;
145 }else 145 }else
146 rv = send(telnetSocket,&c,1,0)==1; 146 rv = send(telnetSocket,&c,1,0)==1;
147 return rv; 147 return rv;
148} 148}
149BOOL SendLiteral(LPCTSTR c,UINT size) 149BOOL SendLiteral(LPCTSTR c,UINT size)
150{ 150{
151 for(UINT tmp=0;tmp<size;tmp++) 151 for(UINT tmp=0;tmp<size;tmp++)
152 SendLiteral(c[tmp]); 152 SendLiteral(c[tmp]);
153 return TRUE; 153 return TRUE;
154} 154}
155BOOL SendLiteral(LPCTSTR c) 155BOOL SendLiteral(LPCTSTR c)
156{ 156{
157 return SendLiteral(c,strlen(c)); 157 return SendLiteral(c,strlen(c));
158} 158}
159BOOL SendCommand(BYTE c) 159BOOL SendCommand(BYTE c)
160{ 160{
161 TRACE1("Issuing %d command\n",(WORD)c); 161 TRACE1("Issuing %d command\n",(WORD)c);
162static 162static
163BYTE d[2] = {tnIAC,0}; 163BYTE d[2] = {tnIAC,0};
164 d[1] = c; 164 d[1] = c;
165BOOL rv = send(telnetSocket,(char*)d,sizeof(d),0)==sizeof(d); 165BOOL rv = send(telnetSocket,(char*)d,sizeof(d),0)==sizeof(d);
166 SelectSocket(); 166 SelectSocket();
167 return rv; 167 return rv;
168 168
169} 169}
170 170
171BOOL CALLBACK consoleCtrlHandler(DWORD dwCtrlType) 171BOOL CALLBACK consoleCtrlHandler(DWORD dwCtrlType)
172{ 172{
173 switch(dwCtrlType){ 173 switch(dwCtrlType){
174 case CTRL_BREAK_EVENT: 174 case CTRL_BREAK_EVENT:
175 SendCommand(tnIP); 175 SendCommand(tnIP);
176 return TRUE; 176 return TRUE;
177 case CTRL_CLOSE_EVENT: 177 case CTRL_CLOSE_EVENT:
178 case CTRL_LOGOFF_EVENT: 178 case CTRL_LOGOFF_EVENT:
179 case CTRL_SHUTDOWN_EVENT: 179 case CTRL_SHUTDOWN_EVENT:
180 PostMessage(wsaW,WM_QUIT,0,0); 180 PostMessage(wsaW,WM_QUIT,0,0);
181 return TRUE; 181 return TRUE;
182 } 182 }
183 return FALSE; 183 return FALSE;
184} 184}
185 185
186#include "options.cpp" 186#include "options.cpp"
187 187
188BOOL ProcessConsoleInput() 188BOOL ProcessConsoleInput()
189{ 189{
190INPUT_RECORD ir[512]; 190INPUT_RECORD ir[512];
191DWORD got; 191DWORD got;
192 while(GetNumberOfConsoleInputEvents(hConsoleInput,&got) && got){ 192 while(GetNumberOfConsoleInputEvents(hConsoleInput,&got) && got){
193 VERIFY(ReadConsoleInput(hConsoleInput,ir,(sizeof(ir)/sizeof(*ir)),&got)); 193 VERIFY(ReadConsoleInput(hConsoleInput,ir,(sizeof(ir)/sizeof(*ir)),&got));
194 for(DWORD tmp=0;tmp<got;tmp++){ 194 for(DWORD tmp=0;tmp<got;tmp++){
195 if(ir[tmp].EventType==KEY_EVENT && ir[tmp].Event.KeyEvent.bKeyDown){ 195 if(ir[tmp].EventType==KEY_EVENT && ir[tmp].Event.KeyEvent.bKeyDown){
196 if(( 196 if((
197 ir[tmp].Event.KeyEvent.wVirtualKeyCode=='X' || 197 ir[tmp].Event.KeyEvent.wVirtualKeyCode=='X' ||
198 ir[tmp].Event.KeyEvent.wVirtualKeyCode=='Q' 198 ir[tmp].Event.KeyEvent.wVirtualKeyCode=='Q'
199 ) && ir[tmp].Event.KeyEvent.dwControlKeyState&(LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED) 199 ) && ir[tmp].Event.KeyEvent.dwControlKeyState&(LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED)
200 ){ 200 ){
201 PostMessage(wsaW,WM_QUIT,0,0); 201 PostMessage(wsaW,WM_QUIT,0,0);
202 return FALSE; 202 return FALSE;
203 }else if( 203 }else if(
204 ir[tmp].Event.KeyEvent.wVirtualKeyCode=='O' 204 ir[tmp].Event.KeyEvent.wVirtualKeyCode=='O'
205 && ir[tmp].Event.KeyEvent.dwControlKeyState&(LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED) 205 && ir[tmp].Event.KeyEvent.dwControlKeyState&(LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED)
206 ){ 206 ){
207 SendCommand(tnAO); 207 SendCommand(tnAO);
208 }else if( 208 }else if(
209 ir[tmp].Event.KeyEvent.wVirtualKeyCode=='Y' 209 ir[tmp].Event.KeyEvent.wVirtualKeyCode=='Y'
210 && ir[tmp].Event.KeyEvent.dwControlKeyState&(LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED) 210 && ir[tmp].Event.KeyEvent.dwControlKeyState&(LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED)
211 ){ 211 ){
212 SendCommand(tnAYT); 212 SendCommand(tnAYT);
213 }else if( 213 }else if(
214 ir[tmp].Event.KeyEvent.wVirtualKeyCode=='T' 214 ir[tmp].Event.KeyEvent.wVirtualKeyCode=='T'
215 && ir[tmp].Event.KeyEvent.dwControlKeyState&(LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED) 215 && ir[tmp].Event.KeyEvent.dwControlKeyState&(LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED)
216 ){ 216 ){
217 BegDo(toTimingMark); 217 BegDo(toTimingMark);
218 }else if( 218 }else if(
219 ir[tmp].Event.KeyEvent.wVirtualKeyCode==VK_INSERT 219 ir[tmp].Event.KeyEvent.wVirtualKeyCode==VK_INSERT
220 && ir[tmp].Event.KeyEvent.dwControlKeyState&SHIFT_PRESSED 220 && ir[tmp].Event.KeyEvent.dwControlKeyState&SHIFT_PRESSED
221 ){ 221 ){
222 if(IsClipboardFormatAvailable(CF_OEMTEXT)){ 222 if(IsClipboardFormatAvailable(CF_OEMTEXT)){
223 ASSERT(wsaW); 223 ASSERT(wsaW);
224 if(OpenClipboard(wsaW)){ 224 if(OpenClipboard(wsaW)){
225 HANDLE h = GetClipboardData(CF_OEMTEXT); 225 HANDLE h = GetClipboardData(CF_OEMTEXT);
226 LPVOID gl = GlobalLock(h); 226 LPVOID gl = GlobalLock(h);
227 TerminalIn((LPCTSTR)gl); 227 TerminalIn((LPCTSTR)gl);
228 GlobalUnlock(h); 228 GlobalUnlock(h);
229 // GlobalFree(h); 229 // GlobalFree(h);
230 CloseClipboard(); 230 CloseClipboard();
231 } 231 }
232 } 232 }
233 }else if( 233 }else if(
234 ir[tmp].Event.KeyEvent.wVirtualKeyCode==VK_INSERT 234 ir[tmp].Event.KeyEvent.wVirtualKeyCode==VK_INSERT
235 && ir[tmp].Event.KeyEvent.dwControlKeyState&(LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED) 235 && ir[tmp].Event.KeyEvent.dwControlKeyState&(LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED)
236 ){ 236 ){
237 if(hConsoleWindow) 237 if(hConsoleWindow)
238 SendNotifyMessage(hConsoleWindow,WM_COMMAND,MAKELONG(0xE003,0),NULL); 238 SendNotifyMessage(hConsoleWindow,WM_COMMAND,MAKELONG(0xE003,0),NULL);
239 }else 239 }else
240 TerminalIn(ir[tmp].Event.KeyEvent); 240 TerminalIn(ir[tmp].Event.KeyEvent);
241 } 241 }
242 } 242 }
243 } 243 }
244 244
245 return TRUE; 245 return TRUE;
246} 246}
247 247
248ULONG CALLBACK ConsoleThreadProc(LPVOID) 248ULONG CALLBACK ConsoleThreadProc(LPVOID)
249{ 249{
250 for(;;){ 250 for(;;){
251 DWORD eves; 251 DWORD eves;
252 if(!(GetNumberOfConsoleInputEvents(hConsoleInput,&eves) && eves)) 252 if(!(GetNumberOfConsoleInputEvents(hConsoleInput,&eves) && eves))
253 WaitForSingleObject(hConsoleInput,INFINITE); 253 WaitForSingleObject(hConsoleInput,INFINITE);
254 ASSERT(wsaW); 254 ASSERT(wsaW);
255 SendMessage(wsaW,WM_USERKEY,0,0); 255 SendMessage(wsaW,WM_USERKEY,0,0);
256 } 256 }
257 return 0; 257 return 0;
258} 258}
259 259
260BOOL SINTelnet(sockaddr_in& sin) 260BOOL SINTelnet(sockaddr_in& sin)
261{ 261{
262protoent* pe = getprotobyname("tcp"); 262protoent* pe = getprotobyname("tcp");
263short proto = pe?pe->p_proto:6; 263short proto = pe?pe->p_proto:6;
264 telnetSocket = socket(sin.sin_family,SOCK_STREAM,proto); 264 telnetSocket = socket(sin.sin_family,SOCK_STREAM,proto);
265 if(telnetSocket==INVALID_SOCKET){ 265 if(telnetSocket==INVALID_SOCKET){
266 printf("Failed to create socket\n"); 266 printf("Failed to create socket\n");
267 return FALSE; 267 return FALSE;
268 } 268 }
269static 269static
270BOOL bOOBInline = FALSE; 270BOOL bOOBInline = FALSE;
271 if(setsockopt(telnetSocket,SOL_SOCKET,SO_OOBINLINE,(const char*)&bOOBInline,sizeof(bOOBInline))){ 271 if(setsockopt(telnetSocket,SOL_SOCKET,SO_OOBINLINE,(const char*)&bOOBInline,sizeof(bOOBInline))){
272 TRACE0("Failed to setsockopt for OOB data\n"); 272 TRACE0("Failed to setsockopt for OOB data\n");
273 } 273 }
274 printf("Trying %s..",inet_ntoa(sin.sin_addr)); 274 printf("Trying %s..",inet_ntoa(sin.sin_addr));
275 if(connect(telnetSocket,(sockaddr*)&sin,sizeof(sin))){ 275 if(connect(telnetSocket,(sockaddr*)&sin,sizeof(sin))){
276 switch(WSAGetLastError()){ 276 switch(WSAGetLastError()){
277 case WSAECONNREFUSED: printf("\nConnection refused\n"); break; 277 case WSAECONNREFUSED: printf("\nConnection refused\n"); break;
278 case WSAEHOSTUNREACH: printf("\nNo route to host\n"); break; 278 case WSAEHOSTUNREACH: printf("\nNo route to host\n"); break;
279 case WSAENETDOWN: printf("\nNetwork is down\n"); break; 279 case WSAENETDOWN: printf("\nNetwork is down\n"); break;
280 case WSAENETUNREACH: printf("\nNetwork is unreachable\n"); break; 280 case WSAENETUNREACH: printf("\nNetwork is unreachable\n"); break;
281 case WSAETIMEDOUT: printf("\nConnection timed out\n"); break; 281 case WSAETIMEDOUT: printf("\nConnection timed out\n"); break;
282 default: printf("\nFailed to connect\n"); break; 282 default: printf("\nFailed to connect\n"); break;
283 } 283 }
284 return FALSE; 284 return FALSE;
285 } 285 }
286 printf("\nConnected. Alt-X/Alt-Q - Close telnet connection\n"); 286 printf("\nConnected. Alt-X/Alt-Q - Close telnet connection\n");
287 //***hConsoleInput = ::GetStdHandle(STD_INPUT_HANDLE); 287 //***hConsoleInput = ::GetStdHandle(STD_INPUT_HANDLE);
288 //***hConsoleOutput = ::GetStdHandle(STD_OUTPUT_HANDLE); 288 //***hConsoleOutput = ::GetStdHandle(STD_OUTPUT_HANDLE);
289 hConsoleInput = CreateFile("CONIN$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE, 289 hConsoleInput = CreateFile("CONIN$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
290 NULL,OPEN_EXISTING,0,NULL); 290 NULL,OPEN_EXISTING,0,NULL);
291 hConsoleOutput = CreateFile("CONOUT$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE, 291 hConsoleOutput = CreateFile("CONOUT$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
292 NULL,OPEN_EXISTING,0,NULL); 292 NULL,OPEN_EXISTING,0,NULL);
293 ASSERT(hConsoleInput!=INVALID_HANDLE_VALUE && 293 ASSERT(hConsoleInput!=INVALID_HANDLE_VALUE &&
294 hConsoleOutput!=INVALID_HANDLE_VALUE); 294 hConsoleOutput!=INVALID_HANDLE_VALUE);
295 if(!(bTelnet || bTermPulled)) 295 if(!(bTelnet || bTermPulled))
296 TerminalPullType("TTY"); 296 TerminalPullType("TTY");
297 InitOptionsTable(); 297 InitOptionsTable();
298 TRACE0("Connected\n"); 298 TRACE0("Connected\n");
299 VERIFY(SetConsoleCtrlHandler(&consoleCtrlHandler,TRUE)); 299 VERIFY(SetConsoleCtrlHandler(&consoleCtrlHandler,TRUE));
300 hConsoleThread = CreateThread(NULL,0,ConsoleThreadProc,NULL,0,&consoleThreadID); 300 hConsoleThread = CreateThread(NULL,0,ConsoleThreadProc,NULL,0,&consoleThreadID);
301 ASSERT(hConsoleThread); 301 ASSERT(hConsoleThread);
302 if(bTelnet){ 302 if(bTelnet){
303 AskDo(toSuppressGA); 303 AskDo(toSuppressGA);
304 AskWill(toTerminalType); 304 AskWill(toTerminalType);
305 AskWill(toNAWS); 305 AskWill(toNAWS);
306 AskUnwill(toEcho); 306 AskUnwill(toEcho);
307 AskDo(toEcho); 307 AskDo(toEcho);
308 if(Envars && nEnvars) 308 if(Envars && nEnvars)
309 AskWill(toNewEnviron);// *** Or better (what's better?) 309 AskWill(toNewEnviron);// *** Or better (what's better?)
310 //AskWill(toLineMode); 310 //AskWill(toLineMode);
311 // *** STATUS 311 // *** STATUS
312 } 312 }
313MSG msg; 313MSG msg;
314int rvgm; 314int rvgm;
315 PostMessage(wsaW,WM_USERNOP,0,0); 315 PostMessage(wsaW,WM_USERNOP,0,0);
316 VERIFY(SelectSocket()); 316 VERIFY(SelectSocket());
317 while(rvgm=GetMessage(&msg,NULL,NULL,NULL)){ 317 while(rvgm=GetMessage(&msg,NULL,NULL,NULL)){
318 if(rvgm<0) 318 if(rvgm<0)
319 break;// Some wheeping needed 319 break;// Some wheeping needed
320 TranslateMessage(&msg); 320 TranslateMessage(&msg);
321 DispatchMessage(&msg); 321 DispatchMessage(&msg);
322 //LRESULT CALLBACK WSWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam); 322 //LRESULT CALLBACK WSWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
323 // WSWndProc(msg.hwnd,msg.message,msg.wParam,msg.lParam); 323 // WSWndProc(msg.hwnd,msg.message,msg.wParam,msg.lParam);
324 VERIFY(SelectSocket()); 324 VERIFY(SelectSocket());
325 } 325 }
326 VERIFY(TerminateThread(hConsoleThread,0xFFFFFFFF)); 326 VERIFY(TerminateThread(hConsoleThread,0xFFFFFFFF));
327 VERIFY(SetConsoleCtrlHandler(&consoleCtrlHandler,FALSE)); 327 VERIFY(SetConsoleCtrlHandler(&consoleCtrlHandler,FALSE));
328 closesocket(telnetSocket); 328 closesocket(telnetSocket);
329 return TRUE; 329 return TRUE;
330} 330}
331 331
332BOOL Telnet(LPCTSTR hostName,UINT port) 332BOOL Telnet(LPCTSTR hostName,UINT port)
333{ 333{
334 memset(&remoteSIN,0,sizeof(remoteSIN)); 334 memset(&remoteSIN,0,sizeof(remoteSIN));
335 remoteSIN.sin_family = AF_INET; 335 remoteSIN.sin_family = AF_INET;
336 remoteSIN.sin_port = htons(port); 336 remoteSIN.sin_port = htons(port);
337 337
338 remoteSIN.sin_addr.s_addr = inet_addr(hostName); 338 remoteSIN.sin_addr.s_addr = inet_addr(hostName);
339 if(remoteSIN.sin_addr.s_addr==INADDR_NONE){ 339 if(remoteSIN.sin_addr.s_addr==INADDR_NONE){
340 hostent* he = gethostbyname(hostName); 340 hostent* he = gethostbyname(hostName);
341 if(!he){ 341 if(!he){
342 printf("Failed to resolve host name\n"); 342 printf("Failed to resolve host name\n");
343 return FALSE; 343 return FALSE;
344 } 344 }
345 ASSERT(he->h_addrtype==AF_INET); 345 ASSERT(he->h_addrtype==AF_INET);
346 ASSERT(he->h_length==sizeof(remoteSIN.sin_addr)); 346 ASSERT(he->h_length==sizeof(remoteSIN.sin_addr));
347 memmove(&remoteSIN.sin_addr,*he->h_addr_list,sizeof(remoteSIN.sin_addr)); 347 memmove(&remoteSIN.sin_addr,*he->h_addr_list,sizeof(remoteSIN.sin_addr));
348 } 348 }
349 strcpy(remoteHost,hostName); 349 strcpy(remoteHost,hostName);
350 return SINTelnet(remoteSIN); 350 return SINTelnet(remoteSIN);
351} 351}
352 352
353 353
354void ProcessIACByte(BYTE c) 354void ProcessIACByte(BYTE c)
355{ 355{
356 connState = cstateNone; 356 connState = cstateNone;
357 switch(c){ 357 switch(c){
358 case tnIAC: 358 case tnIAC:
359 TerminalOut(c); 359 TerminalOut(c);
360 break; 360 break;
361 case tnSE: 361 case tnSE:
362 TRACE0("SE\n");break; 362 TRACE0("SE\n");break;
363 case tnNOP: 363 case tnNOP:
364 TRACE0("NOP\n");break; 364 TRACE0("NOP\n");break;
365 case tnDM: 365 case tnDM:
366 TRACE0("DM\n");break; 366 TRACE0("DM\n");break;
367 case tnBreak: 367 case tnBreak:
368 TRACE0("Break\n");break; 368 TRACE0("Break\n");break;
369 case tnIP: 369 case tnIP:
370 TRACE0("IP\n");break; 370 TRACE0("IP\n");break;
371 case tnAO: 371 case tnAO:
372 TRACE0("AO\n");break; 372 TRACE0("AO\n");break;
373 case tnAYT: 373 case tnAYT:
374 TRACE0("AYT\n");break; 374 TRACE0("AYT\n");break;
375 case tnEC: 375 case tnEC:
376 TRACE0("EC\n");break; 376 TRACE0("EC\n");break;
377 case tnEL: 377 case tnEL:
378 TRACE0("EL\n");break; 378 TRACE0("EL\n");break;
379 case tnGA: 379 case tnGA:
380 TRACE0("GA\n");break; 380 TRACE0("GA\n");break;
381 case tnSB: 381 case tnSB:
382 connState = cstateSB; 382 connState = cstateSB;
383 break; 383 break;
384 case tnWILL: 384 case tnWILL:
385 connState = cstateWILL; 385 connState = cstateWILL;
386 break; 386 break;
387 case tnWONT: 387 case tnWONT:
388 connState = cstateWONT; 388 connState = cstateWONT;
389 break; 389 break;
390 case tnDO: 390 case tnDO:
391 connState = cstateDO; 391 connState = cstateDO;
392 break; 392 break;
393 case tnDONT: 393 case tnDONT:
394 connState = cstateDONT; 394 connState = cstateDONT;
395 break; 395 break;
396 default: 396 default:
397 TRACE1("Unknown OpCode = %d\n",(WORD)c); 397 TRACE1("Unknown OpCode = %d\n",(WORD)c);
398 break; 398 break;
399 } 399 }
400} 400}
401 401
402void ProcessNetByte(BYTE c) 402void ProcessNetByte(BYTE c)
403{ 403{
404 //TRACE1("<%d>",connState); 404 //TRACE1("<%d>",connState);
405 switch(connState){ 405 switch(connState){
406 case cstateWONT: 406 case cstateWONT:
407 ProcessWONT(c); 407 ProcessWONT(c);
408 break; 408 break;
409 case cstateDO: 409 case cstateDO:
410 ProcessDO(c); 410 ProcessDO(c);
411 break; 411 break;
412 case cstateWILL: 412 case cstateWILL:
413 ProcessWILL(c); 413 ProcessWILL(c);
414 break; 414 break;
415 case cstateDONT: 415 case cstateDONT:
416 ProcessDONT(c); 416 ProcessDONT(c);
417 break; 417 break;
418 case cstateSB: 418 case cstateSB:
419 negOption = c; 419 negOption = c;
420 connState = cstateSBData; 420 connState = cstateSBData;
421 break; 421 break;
422 case cstateSBData: 422 case cstateSBData:
423 case cstateSBDataIAC: 423 case cstateSBDataIAC:
424 ProcessSBData(c); 424 ProcessSBData(c);
425 break; 425 break;
426 case cstateIAC: 426 case cstateIAC:
427 ProcessIACByte(c); 427 ProcessIACByte(c);
428 break; 428 break;
429 case cstateNone: 429 case cstateNone:
430 default: 430 default:
431 ASSERT(connState==cstateNone); 431 ASSERT(connState==cstateNone);
432 if(c==tnIAC) 432 if(c==tnIAC)
433 connState=cstateIAC; 433 connState=cstateIAC;
434 else 434 else
435 TerminalOut(c); 435 TerminalOut(c);
436 break; 436 break;
437 } 437 }
438} 438}
439 439
440LRESULT WSMessage(WPARAM wP,LPARAM lP) 440LRESULT WSMessage(WPARAM wP,LPARAM lP)
441{ 441{
442 if(WSAGETSELECTERROR(lP)){ 442 if(WSAGETSELECTERROR(lP)){
443 TRACE0("SelectError\n"); 443 TRACE0("SelectError\n");
444 PostMessage(wsaW,WM_QUIT,0,0); 444 PostMessage(wsaW,WM_QUIT,0,0);
445 return 0; 445 return 0;
446 } 446 }
447 if(WSAGETSELECTEVENT(lP)&FD_READ){ 447 if(WSAGETSELECTEVENT(lP)&FD_READ){
448 //?? TRACE0("FD_READ\n"); 448 //?? TRACE0("FD_READ\n");
449 BYTE input[80*12]; 449 BYTE input[80*12];
450 int got; 450 int got;
451 TerminalPreO(); 451 TerminalPreO();
452 //?? TRACE0("rv\n"); 452 //?? TRACE0("rv\n");
453 got=recv(telnetSocket,(CHAR*)input,sizeof(input),0); 453 got=recv(telnetSocket,(CHAR*)input,sizeof(input),0);
454 //?? TRACE1("/rv %d\n",got); 454 //?? TRACE1("/rv %d\n",got);
455 for(int tmp=0;tmp<got;tmp++) 455 for(int tmp=0;tmp<got;tmp++)
456 ProcessNetByte(input[tmp]); 456 ProcessNetByte(input[tmp]);
457 TerminalPostO(); 457 TerminalPostO();
458 //?? TRACE0("/FD_READ\n"); 458 //?? TRACE0("/FD_READ\n");
459 return 0; 459 return 0;
460 } 460 }
461 if(WSAGETSELECTEVENT(lP)&FD_OOB){ 461 if(WSAGETSELECTEVENT(lP)&FD_OOB){
462 TRACE0("OOB\n"); 462 TRACE0("OOB\n");
463 } 463 }
464 if(WSAGETSELECTEVENT(lP)&FD_CLOSE){ 464 if(WSAGETSELECTEVENT(lP)&FD_CLOSE){
465 TRACE0("CLOSE\n"); 465 TRACE0("CLOSE\n");
466 PostMessage(wsaW,WM_QUIT,0,0); 466 PostMessage(wsaW,WM_QUIT,0,0);
467 return 0; 467 return 0;
468 } 468 }
469 VERIFY(SelectSocket()); 469 VERIFY(SelectSocket());
470 return 0; 470 return 0;
471} 471}
472 472
473LRESULT CALLBACK WSWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) 473LRESULT CALLBACK WSWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
474{ 474{
475 switch(uMsg){ 475 switch(uMsg){
476 case WM_USERSOCKET: 476 case WM_USERSOCKET:
477 return WSMessage(wParam,lParam); 477 return WSMessage(wParam,lParam);
478 case WM_USERKEY: 478 case WM_USERKEY:
479 return ProcessConsoleInput(); 479 return ProcessConsoleInput();
480 default: 480 default:
481 TRACE0("DEFWINDOWPROC\n"); 481 TRACE0("DEFWINDOWPROC\n");
482 return ::DefWindowProc(hWnd,uMsg,wParam,lParam); 482 return ::DefWindowProc(hWnd,uMsg,wParam,lParam);
483 } 483 }
484 return 0; 484 return 0;
485} 485}
486 486
487 487
488BOOL InitializeWinsock() 488BOOL InitializeWinsock()
489{ 489{
490 if(WSAStartup(0x101,&wsaData)){ 490 if(WSAStartup(0x101,&wsaData)){
491 printf("Failed to initialize winsock services\n"); 491 printf("Failed to initialize winsock services\n");
492 return FALSE; 492 return FALSE;
493 } 493 }
494WNDCLASS wc; 494WNDCLASS wc;
495 memset(&wc,0,sizeof(wc)); 495 memset(&wc,0,sizeof(wc));
496 wc.lpfnWndProc=WSWndProc; wc.hInstance=::GetModuleHandle(NULL); 496 wc.lpfnWndProc=WSWndProc; wc.hInstance=::GetModuleHandle(NULL);
497 wc.lpszClassName = "_WSTFWC_"; 497 wc.lpszClassName = "_WSTFWC_";
498 wsaWC = RegisterClass(&wc); 498 wsaWC = RegisterClass(&wc);
499 if(!wsaWC){ 499 if(!wsaWC){
500 printf("Failed to initialize winsock services - 1\n"); 500 printf("Failed to initialize winsock services - 1\n");
501 return FALSE; 501 return FALSE;
502 } 502 }
503 wsaW = ::CreateWindow("_WSTFWC_","KIN Sole Mio",0,0,0,0,0,NULL,NULL,::GetModuleHandle(NULL),NULL); 503 wsaW = ::CreateWindow("_WSTFWC_","KIN Sole Mio",0,0,0,0,0,NULL,NULL,::GetModuleHandle(NULL),NULL);
504 if(!wsaW){ 504 if(!wsaW){
505 printf("Failed to initialize winsock services\n"); 505 printf("Failed to initialize winsock services\n");
506 return FALSE; 506 return FALSE;
507 } 507 }
508 return TRUE; 508 return TRUE;
509} 509}
510void DeinitializeWinsock() 510void DeinitializeWinsock()
511{ 511{
512 if(wsaW) 512 if(wsaW)
513 ::DestroyWindow(wsaW); 513 ::DestroyWindow(wsaW);
514 wsaW=NULL; 514 wsaW=NULL;
515 if(wsaWC) 515 if(wsaWC)
516 ::UnregisterClass("_WSTFWC_",::GetModuleHandle(NULL)); 516 ::UnregisterClass("_WSTFWC_",::GetModuleHandle(NULL));
517 wsaWC=NULL; 517 wsaWC=NULL;
518 WSACleanup(); 518 WSACleanup();
519} 519}
520 520
521HWND GetThisConsoleWnd() 521HWND GetThisConsoleWnd()
522{ 522{
523DWORD pid = GetCurrentProcessId(); 523DWORD pid = GetCurrentProcessId();
524CHAR title[512]; 524CHAR title[512];
525CHAR* t = title; 525CHAR* t = title;
526 if(!GetConsoleTitle(title,sizeof(title))) 526 if(!GetConsoleTitle(title,sizeof(title)))
527 t = NULL; 527 t = NULL;
528HWND hrv = FindWindowEx(NULL,NULL,"tty",t); 528HWND hrv = FindWindowEx(NULL,NULL,"tty",t);
529HWND nopro = NULL; 529HWND nopro = NULL;
530UINT nopros=0; 530UINT nopros=0;
531 do{ 531 do{
532 DWORD wpid; 532 DWORD wpid;
533 if(!GetWindowThreadProcessId(hrv,&wpid)) 533 if(!GetWindowThreadProcessId(hrv,&wpid))
534 continue; 534 continue;
535 if(wpid==pid) 535 if(wpid==pid)
536 return hrv; 536 return hrv;
537 nopro=hrv; 537 nopro=hrv;
538 nopros++; 538 nopros++;
539 hrv = FindWindowEx(NULL,hrv,"tty",t); 539 hrv = FindWindowEx(NULL,hrv,"tty",t);
540 }while(hrv); 540 }while(hrv);
541 if(nopros==1){ 541 if(nopros==1){
542 ASSERT(nopro); 542 ASSERT(nopro);
543 return nopro; 543 return nopro;
544 } 544 }
545 return NULL; 545 return NULL;
546} 546}
547 547
548main(int argc,char*argv[]) 548main(int argc,char*argv[])
549{ 549{
550 if(argc<2){ 550 if(argc<2){
551usagebye: 551usagebye:
552 printf( 552 printf(
553 DAMN_KIN_NAME " " DAMN_KIN_VERSION ", Copyright (c) 1998, 2002 Klever Group (http://www.klever.net/)\n\n" 553 DAMN_KIN_NAME " " DAMN_KIN_VERSION ", Copyright (c) 1998-2004 Klever Group (http://www.klever.net/)\n\n"
554 "Usage:\t" DAMN_KIN_NAME " [<options> ]<host-name/ip-address>[ <port>]\n\n" 554 "Usage:\t" DAMN_KIN_NAME " [<options> ]<host-name/ip-address>[ <port>]\n\n"
555 "Options are:\n" 555 "Options are:\n"
556 "-r## or -##\tSet number of rows in console screenbuffer\n" 556 "-r## or -##\tSet number of rows in console screenbuffer\n"
557 "-c##\t\tSet number of columns in console screenbuffer\n" 557 "-c##\t\tSet number of columns in console screenbuffer\n"
558 "\tnote: changing console screenbuffer size may not work properly\n" 558 "\tnote: changing console screenbuffer size may not work properly\n"
559 "\twhen in full-screen mode\n" 559 "\twhen in full-screen mode\n"
560 "-l<user>\tPass username to remote server in environment\n" 560 "-l<user>\tPass username to remote server in environment\n"
561 "-e<var>=<val>\tPass environment variable to remote server\n" 561 "-e<var>=<val>\tPass environment variable to remote server\n"
562 "-v<var>=<val>\tPass user environment variable to remote server\n" 562 "-v<var>=<val>\tPass user environment variable to remote server\n"
563 "-t<termtype>\tChange preferred terminal type\n" 563 "-t<termtype>\tChange preferred terminal type\n"
564 "\tnote: there are only two different terminal emulations in this\n" 564 "\tnote: there are only two different terminal emulations in this\n"
565 "\tbeta release - one for dumb terminal and one for vt terminal\n" 565 "\trelease - one for dumb terminal and one for vt terminal\n"
566 ); 566 );
567 CleanEnvars(); 567 CleanEnvars();
568 return 1; 568 return 1;
569 } 569 }
570 if(!InitializeWinsock()){ 570 if(!InitializeWinsock()){
571 DeinitializeWinsock(); 571 DeinitializeWinsock();
572 return 2; 572 return 2;
573 } 573 }
574CONSOLE_SCREEN_BUFFER_INFO csbi; 574CONSOLE_SCREEN_BUFFER_INFO csbi;
575int ac = 0; 575int ac = 0;
576CHAR *ho = NULL, *po = NULL; 576CHAR *ho = NULL, *po = NULL;
577HANDLE hConsole = CreateFile("CONOUT$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE, 577HANDLE hConsole = CreateFile("CONOUT$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
578 NULL,OPEN_EXISTING,0,NULL); 578 NULL,OPEN_EXISTING,0,NULL);
579 ASSERT(hConsole); 579 ASSERT(hConsole);
580 // *** GetStdHandle(STD_OUTPUT_HANDLE);// *?*?* Do something about redirections and not only here. 580 // *** GetStdHandle(STD_OUTPUT_HANDLE);// *?*?* Do something about redirections and not only here.
581 VERIFY(GetConsoleScreenBufferInfo(hConsole,&csbi)); 581 VERIFY(GetConsoleScreenBufferInfo(hConsole,&csbi));
582BOOL bSized=FALSE; 582BOOL bSized=FALSE;
583 bTermPulled=FALSE; 583 bTermPulled=FALSE;
584 for(int tmp=1;tmp<argc;tmp++){ 584 for(int tmp=1;tmp<argc;tmp++){
585 char* v = argv[tmp]; 585 char* v = argv[tmp];
586 ASSERT(v && *v); 586 ASSERT(v && *v);
587 if((*v)=='/' || (*v)=='-'){ 587 if((*v)=='/' || (*v)=='-'){
588 int lines = atoi(&v[1]); 588 int lines = atoi(&v[1]);
589 if(lines){ 589 if(lines){
590 if(lines<csbi.dwSize.Y){ 590 if(lines<csbi.dwSize.Y){
591 SMALL_RECT wi = {0,0,csbi.dwSize.X-1,lines-1}; 591 SMALL_RECT wi = {0,0,csbi.dwSize.X-1,lines-1};
592 VERIFY(SetConsoleWindowInfo(hConsole,TRUE,&wi)); 592 VERIFY(SetConsoleWindowInfo(hConsole,TRUE,&wi));
593 } 593 }
594 COORD ns = {csbi.dwSize.X,lines}; 594 COORD ns = {csbi.dwSize.X,lines};
595 if(SetConsoleScreenBufferSize(hConsole,ns)) 595 if(SetConsoleScreenBufferSize(hConsole,ns))
596 bSized=TRUE; 596 bSized=TRUE;
597 }else if(v[1]=='r'){ 597 }else if(v[1]=='r'){
598 int lines = atoi(&v[2]); 598 int lines = atoi(&v[2]);
599 if(lines){ 599 if(lines){
600 if(lines<csbi.dwSize.Y){ 600 if(lines<csbi.dwSize.Y){
601 SMALL_RECT wi = {0,0,csbi.dwSize.X-1,lines-1}; 601 SMALL_RECT wi = {0,0,csbi.dwSize.X-1,lines-1};
602 VERIFY(SetConsoleWindowInfo(hConsole,TRUE,&wi)); 602 VERIFY(SetConsoleWindowInfo(hConsole,TRUE,&wi));
603 } 603 }
604 COORD ns = {csbi.dwSize.X,lines}; 604 COORD ns = {csbi.dwSize.X,lines};
605 if(SetConsoleScreenBufferSize(hConsole,ns)) 605 if(SetConsoleScreenBufferSize(hConsole,ns))
606 bSized=TRUE; 606 bSized=TRUE;
607 }else 607 }else
608 goto usagebye; 608 goto usagebye;
609 }else if(v[1]=='c'){ 609 }else if(v[1]=='c'){
610 int rows = atoi(&v[2]); 610 int rows = atoi(&v[2]);
611 if(rows){ 611 if(rows){
612 if(rows<csbi.dwSize.X){ 612 if(rows<csbi.dwSize.X){
613 SMALL_RECT wi = {0,0,rows-1,csbi.dwSize.Y-1}; 613 SMALL_RECT wi = {0,0,rows-1,csbi.dwSize.Y-1};
614 VERIFY(SetConsoleWindowInfo(hConsole,TRUE,&wi)); 614 VERIFY(SetConsoleWindowInfo(hConsole,TRUE,&wi));
615 } 615 }
616 COORD ns = {rows,csbi.dwSize.Y}; 616 COORD ns = {rows,csbi.dwSize.Y};
617 if(SetConsoleScreenBufferSize(hConsole,ns)) 617 if(SetConsoleScreenBufferSize(hConsole,ns))
618 bSized=TRUE; 618 bSized=TRUE;
619 }else 619 }else
620 goto usagebye; 620 goto usagebye;
621 }else if(v[1]=='l'){ 621 }else if(v[1]=='l'){
622 CHAR* vv = &v[2]; 622 CHAR* vv = &v[2];
623 VERIFY(AddEnvar(nesbVar,"USER",vv)); 623 VERIFY(AddEnvar(nesbVar,"USER",vv));
624 }else if(v[1]=='e'){ // -e<name>=<value>VAR 624 }else if(v[1]=='e'){ // -e<name>=<value>VAR
625 CHAR* n = &v[2]; 625 CHAR* n = &v[2];
626 CHAR* vv = strchr(&v[2],'='); 626 CHAR* vv = strchr(&v[2],'=');
627 if(!vv) 627 if(!vv)
628 goto usagebye; 628 goto usagebye;
629 *(vv++)=0; 629 *(vv++)=0;
630 VERIFY(AddEnvar(nesbVar,n,vv)); 630 VERIFY(AddEnvar(nesbVar,n,vv));
631 }else if(v[1]=='v'){ // -v<name>=<value>USERVAR 631 }else if(v[1]=='v'){ // -v<name>=<value>USERVAR
632 CHAR* n = &v[2]; 632 CHAR* n = &v[2];
633 CHAR* vv = strchr(n,'='); 633 CHAR* vv = strchr(n,'=');
634 if(!vv) 634 if(!vv)
635 goto usagebye; 635 goto usagebye;
636 *(vv++)=0; 636 *(vv++)=0;
637 VERIFY(AddEnvar(nesbUserVar,n,vv)); 637 VERIFY(AddEnvar(nesbUserVar,n,vv));
638 }else if(v[1]=='t'){// -t<ttype> -t<tname>=<ttype> 638 }else if(v[1]=='t'){// -t<ttype> -t<tname>=<ttype>
639 CHAR* n = &v[2]; 639 CHAR* n = &v[2];
640 CHAR* nn = strchr(n,'='); 640 CHAR* nn = strchr(n,'=');
641 if(nn){ 641 if(nn){
642 *(nn++)=0; 642 *(nn++)=0;
643 if(!*nn) 643 if(!*nn)
644 nn=NULL; 644 nn=NULL;
645 } 645 }
646 if(!TerminalPullType(nn?nn:n,nn?n:NULL)){ 646 if(!TerminalPullType(nn?nn:n,nn?n:NULL)){
647 printf("Available terminal types are:"); 647 printf("Available terminal types are:");
648 TerminalPrintTypes(); 648 TerminalPrintTypes();
649 printf("\n"); 649 printf("\n");
650 goto usagebye; 650 goto usagebye;
651 } 651 }
652 bTermPulled=TRUE; 652 bTermPulled=TRUE;
653 }else if(v[1]=='#'){ 653 }else if(v[1]=='#'){
654 int cp = atoi(&v[2]); 654 int cp = atoi(&v[2]);
655 #ifdef_DEBUG 655 #ifdef_DEBUG
656 TRACE2("SetCP(%d)=%d\n",cp,SetConsoleCP(cp)); 656 TRACE2("SetCP(%d)=%d\n",cp,SetConsoleCP(cp));
657 TRACE2("SetOutCP(%d)=%d\n",cp,SetConsoleOutputCP(cp)); 657 TRACE2("SetOutCP(%d)=%d\n",cp,SetConsoleOutputCP(cp));
658#else 658#else
659 SetConsoleCP(cp); 659 SetConsoleCP(cp);
660 SetConsoleOutputCP(cp); 660 SetConsoleOutputCP(cp);
661#endif 661#endif
662 TRACE2("CP,OCP=%d,%d\n",GetConsoleCP(),GetConsoleOutputCP()); 662 TRACE2("CP,OCP=%d,%d\n",GetConsoleCP(),GetConsoleOutputCP());
663 }else 663 }else
664 goto usagebye; 664 goto usagebye;
665 }else{ 665 }else{
666 if(ac==0){ 666 if(ac==0){
667 ho = v; 667 ho = v;
668 ac++; 668 ac++;
669 }else if(ac==1){ 669 }else if(ac==1){
670 po = v; 670 po = v;
671 ac++; 671 ac++;
672 }else 672 }else
673 goto usagebye; 673 goto usagebye;
674 } 674 }
675 } 675 }
676 if(!ho) 676 if(!ho)
677 goto usagebye; 677 goto usagebye;
678servent* se = getservbyname("telnet","tcp"); 678servent* se = getservbyname("telnet","tcp");
679UINT port = po?atol(po):(se?ntohs(se->s_port):23); 679UINT port = po?atol(po):(se?ntohs(se->s_port):23);
680 if(port==23 || (se && port==ntohs(se->s_port))) 680 if(port==23 || (se && port==ntohs(se->s_port)))
681 bTelnet = TRUE; 681 bTelnet = TRUE;
682 if(po && !port){ 682 if(po && !port){
683 se = getservbyname(po,"tcp"); 683 se = getservbyname(po,"tcp");
684 if(!se){ 684 if(!se){
685 printf("Failed to resolve tcp-service port name\n"); 685 printf("Failed to resolve tcp-service port name\n");
686 DeinitializeWinsock(); 686 DeinitializeWinsock();
687 return 2; 687 return 2;
688 } 688 }
689 port = ntohs(se->s_port); 689 port = ntohs(se->s_port);
690 if(!stricmp(po,"telnet")) 690 if(!stricmp(po,"telnet"))
691 bTelnet = TRUE; 691 bTelnet = TRUE;
692 else 692 else
693 bTelnet = FALSE; 693 bTelnet = FALSE;
694 }else{ 694 }else{
695 se = getservbyport(htons(port),"tcp"); 695 se = getservbyport(htons(port),"tcp");
696 if(se){ 696 if(se){
697 po = se->s_name; 697 po = se->s_name;
698 if(!stricmp(po,"telnet")) 698 if(!stricmp(po,"telnet"))
699 bTelnet=TRUE; 699 bTelnet=TRUE;
700 }else{ 700 }else{
701 VERIFY(_itoa(port,remoteProt,10)); 701 VERIFY(_itoa(port,remoteProt,10));
702 po = NULL; 702 po = NULL;
703 bTelnet=FALSE; 703 bTelnet=FALSE;
704 } 704 }
705 } 705 }
706 if(po) 706 if(po)
707 strcpy(remoteProt,po); 707 strcpy(remoteProt,po);
708HICON hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_IKON)); 708HICON hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_IKON));
709LONG oldBIcon = NULL, oldSIcon = NULL; 709LONG oldBIcon = NULL, oldSIcon = NULL;
710 hConsoleWindow = GetThisConsoleWnd(); 710 hConsoleWindow = GetThisConsoleWnd();
711 if(hConsoleWindow){ 711 if(hConsoleWindow){
712 oldBIcon = SendMessage(hConsoleWindow,WM_SETICON,ICON_BIG,(LPARAM)hIcon); 712 oldBIcon = SendMessage(hConsoleWindow,WM_SETICON,ICON_BIG,(LPARAM)hIcon);
713 oldSIcon = SendMessage(hConsoleWindow,WM_SETICON,ICON_SMALL,(LPARAM)hIcon); 713 oldSIcon = SendMessage(hConsoleWindow,WM_SETICON,ICON_SMALL,(LPARAM)hIcon);
714 } 714 }
715 Telnet(ho,port); 715 Telnet(ho,port);
716 CleanEnvars(); 716 CleanEnvars();
717 if(hConsoleWindow){ 717 if(hConsoleWindow){
718 SendMessage(hConsoleWindow,WM_SETICON,ICON_BIG,(LPARAM)oldBIcon); 718 SendMessage(hConsoleWindow,WM_SETICON,ICON_BIG,(LPARAM)oldBIcon);
719 SendMessage(hConsoleWindow,WM_SETICON,ICON_SMALL,(LPARAM)oldSIcon); 719 SendMessage(hConsoleWindow,WM_SETICON,ICON_SMALL,(LPARAM)oldSIcon);
720 } 720 }
721 Sleep(150); 721 Sleep(150);
722 if(bSized){ 722 if(bSized){
723 CONSOLE_SCREEN_BUFFER_INFO CSBI; 723 CONSOLE_SCREEN_BUFFER_INFO CSBI;
724 VERIFY(GetConsoleScreenBufferInfo(hConsole,&CSBI)); 724 VERIFY(GetConsoleScreenBufferInfo(hConsole,&CSBI));
725 if(CSBI.dwSize.Y>csbi.dwSize.Y || CSBI.dwSize.X>csbi.dwSize.X){ 725 if(CSBI.dwSize.Y>csbi.dwSize.Y || CSBI.dwSize.X>csbi.dwSize.X){
726 SMALL_RECT wi = {0,0,csbi.dwSize.X-1,csbi.dwSize.Y-1}; 726 SMALL_RECT wi = {0,0,csbi.dwSize.X-1,csbi.dwSize.Y-1};
727 VERIFY(SetConsoleWindowInfo(hConsole,TRUE,&wi)); 727 VERIFY(SetConsoleWindowInfo(hConsole,TRUE,&wi));
728 } 728 }
729 COORD ns = {csbi.dwSize.X,csbi.dwSize.Y}; 729 COORD ns = {csbi.dwSize.X,csbi.dwSize.Y};
730 VERIFY(SetConsoleScreenBufferSize(hConsole,ns)); 730 VERIFY(SetConsoleScreenBufferSize(hConsole,ns));
731 } 731 }
732 Sleep(100); 732 Sleep(100);
733 DeinitializeWinsock(); 733 DeinitializeWinsock();
734 Sleep(100); 734 Sleep(100);
735 return 0; 735 return 0;
736} 736}