2 * PLink - a command-line (stdin/stdout) variant of PuTTY.
12 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
15 void fatalbox (char *p
, ...) {
17 fprintf(stderr
, "FATAL ERROR: ", p
);
19 vfprintf(stderr
, p
, ap
);
25 void connection_fatal (char *p
, ...) {
27 fprintf(stderr
, "FATAL ERROR: ", p
);
29 vfprintf(stderr
, p
, ap
);
37 * Stubs for linking with other modules.
39 void write_clip (void *data
, int len
) { }
40 void term_deselect(void) { }
43 DWORD orig_console_mode
;
45 void begin_session(void) {
47 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), ENABLE_PROCESSED_INPUT
);
49 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), orig_console_mode
);
57 while (reap
< inbuf_head
) {
58 if (!WriteFile(outhandle
, inbuf
+reap
, inbuf_head
-reap
, &ret
, NULL
))
59 return; /* give up in panic */
71 static int get_password(const char *prompt
, char *str
, int maxlen
)
76 #if 0 /* this allows specifying a password some other way */
78 static int tried_once
= 0;
83 strncpy(str
, password
, maxlen
);
91 hin
= GetStdHandle(STD_INPUT_HANDLE
);
92 hout
= GetStdHandle(STD_OUTPUT_HANDLE
);
93 if (hin
== INVALID_HANDLE_VALUE
|| hout
== INVALID_HANDLE_VALUE
) {
94 fprintf(stderr
, "Cannot get standard input/output handles");
98 GetConsoleMode(hin
, &savemode
);
99 SetConsoleMode(hin
, (savemode
& (~ENABLE_ECHO_INPUT
)) |
100 ENABLE_PROCESSED_INPUT
| ENABLE_LINE_INPUT
);
102 WriteFile(hout
, prompt
, strlen(prompt
), &i
, NULL
);
103 ReadFile(hin
, str
, maxlen
-1, &i
, NULL
);
105 SetConsoleMode(hin
, savemode
);
107 if ((int)i
> maxlen
) i
= maxlen
-1; else i
= i
- 2;
110 WriteFile(hout
, "\r\n", 2, &i
, NULL
);
115 int WINAPI
stdin_read_thread(void *param
) {
116 struct input_data
*idata
= (struct input_data
*)param
;
119 inhandle
= GetStdHandle(STD_INPUT_HANDLE
);
121 while (ReadFile(inhandle
, idata
->buffer
, sizeof(idata
->buffer
),
122 &idata
->len
, NULL
)) {
123 SetEvent(idata
->event
);
127 SetEvent(idata
->event
);
132 int main(int argc
, char **argv
) {
135 WSAEVENT netevent
, stdinevent
;
139 struct input_data idata
;
142 ssh_get_password
= get_password
;
146 * Process the command line.
148 default_protocol
= DEFAULT_PROTOCOL
;
149 default_port
= DEFAULT_PORT
;
154 if (!strcmp(p
, "-ssh")) {
155 default_protocol
= cfg
.protocol
= PROT_SSH
;
156 default_port
= cfg
.port
= 22;
157 } else if (!strcmp(p
, "-v")) {
158 flags
|= FLAG_VERBOSE
;
159 } else if (!strcmp(p
, "-log")) {
160 logfile
= "putty.log";
166 * If the hostname starts with "telnet:", set the
167 * protocol to Telnet and process the string as a
170 if (!strncmp(q
, "telnet:", 7)) {
174 if (q
[0] == '/' && q
[1] == '/')
176 cfg
.protocol
= PROT_TELNET
;
178 while (*p
&& *p
!= ':' && *p
!= '/') p
++;
186 strncpy (cfg
.host
, q
, sizeof(cfg
.host
)-1);
187 cfg
.host
[sizeof(cfg
.host
)-1] = '\0';
190 * Three cases. Either (a) there's a nonzero
191 * length string followed by an @, in which
192 * case that's user and the remainder is host.
193 * Or (b) there's only one string, not counting
194 * a potential initial @, and it exists in the
195 * saved-sessions database. Or (c) only one
196 * string and it _doesn't_ exist in the
199 char *r
= strrchr(p
, '@');
200 if (r
== p
) p
++, r
= NULL
; /* discount initial @ */
206 if (cfg
.host
[0] == '\0') {
207 /* No settings for this host; use defaults */
208 strncpy(cfg
.host
, p
, sizeof(cfg
.host
)-1);
209 cfg
.host
[sizeof(cfg
.host
)-1] = '\0';
214 strncpy(cfg
.username
, p
, sizeof(cfg
.username
)-1);
215 cfg
.username
[sizeof(cfg
.username
)-1] = '\0';
216 strncpy(cfg
.host
, r
, sizeof(cfg
.host
)-1);
217 cfg
.host
[sizeof(cfg
.host
)-1] = '\0';
222 int len
= sizeof(cfg
.remote_cmd
) - 1;
223 char *cp
= cfg
.remote_cmd
;
226 strncpy(cp
, p
, len
); cp
[len
] = '\0';
227 len2
= strlen(cp
); len
-= len2
; cp
+= len2
;
231 strncpy(cp
, *++argv
, len
); cp
[len
] = '\0';
232 len2
= strlen(cp
); len
-= len2
; cp
+= len2
;
234 cfg
.nopty
= TRUE
; /* command => no terminal */
235 cfg
.ldisc_term
= TRUE
; /* use stdin like a line buffer */
236 break; /* done with cmdline */
241 if (!*cfg
.remote_cmd
)
242 flags
|= FLAG_INTERACTIVE
;
245 * Select protocol. This is farmed out into a table in a
246 * separate file to enable an ssh-free variant.
251 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
252 if (backends
[i
].protocol
== cfg
.protocol
) {
253 back
= backends
[i
].backend
;
257 fprintf(stderr
, "Internal fault: Unsupported protocol found\n");
263 * Initialise WinSock.
265 winsock_ver
= MAKEWORD(2, 0);
266 if (WSAStartup(winsock_ver
, &wsadata
)) {
267 MessageBox(NULL
, "Unable to initialise WinSock", "WinSock Error",
268 MB_OK
| MB_ICONEXCLAMATION
);
271 if (LOBYTE(wsadata
.wVersion
) != 2 || HIBYTE(wsadata
.wVersion
) != 0) {
272 MessageBox(NULL
, "WinSock version is incompatible with 2.0",
273 "WinSock Error", MB_OK
| MB_ICONEXCLAMATION
);
279 * Start up the connection.
285 error
= back
->init (NULL
, cfg
.host
, cfg
.port
, &realhost
);
287 fprintf(stderr
, "Unable to open connection:\n%s", error
);
292 netevent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
293 stdinevent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
295 GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), &orig_console_mode
);
296 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), ENABLE_PROCESSED_INPUT
);
297 outhandle
= GetStdHandle(STD_OUTPUT_HANDLE
);
300 * Now we must send the back end oodles of stuff.
302 socket
= back
->socket();
304 * Turn off ECHO and LINE input modes. We don't care if this
305 * call fails, because we know we aren't necessarily running in
308 WSAEventSelect(socket
, netevent
, FD_READ
| FD_CLOSE
);
309 handles
[0] = netevent
;
310 handles
[1] = stdinevent
;
314 n
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
316 WSANETWORKEVENTS things
;
317 if (!WSAEnumNetworkEvents(socket
, netevent
, &things
)) {
318 if (things
.lNetworkEvents
& FD_READ
)
319 back
->msg(0, FD_READ
);
320 if (things
.lNetworkEvents
& FD_CLOSE
) {
321 back
->msg(0, FD_CLOSE
);
326 if (!sending
&& back
->sendok()) {
328 * Create a separate thread to read from stdin.
329 * This is a total pain, but I can't find another
332 * - an overlapped ReadFile or ReadFileEx just
333 * doesn't happen; we get failure from
334 * ReadFileEx, and ReadFile blocks despite being
335 * given an OVERLAPPED structure. Perhaps we
336 * can't do overlapped reads on consoles. WHY
339 * - WaitForMultipleObjects(netevent, console)
340 * doesn't work, because it signals the console
341 * when _anything_ happens, including mouse
342 * motions and other things that don't cause
343 * data to be readable - so we're back to
346 idata
.event
= stdinevent
;
347 if (!CreateThread(NULL
, 0, stdin_read_thread
,
348 &idata
, 0, &threadid
)) {
349 fprintf(stderr
, "Unable to create second thread\n");
356 back
->send(idata
.buffer
, idata
.len
);
358 back
->special(TS_EOF
);