70aab6de4ce6569ded9c1800b698924ea7c9f464
2 * PLink - a command-line (stdin/stdout) variant of PuTTY.
10 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
13 void fatalbox (char *p
, ...) {
15 fprintf(stderr
, "FATAL ERROR: ", p
);
17 vfprintf(stderr
, p
, ap
);
32 while (reap
< inbuf_head
) {
33 if (!WriteFile(outhandle
, inbuf
+reap
, inbuf_head
-reap
, &ret
, NULL
))
34 return; /* give up in panic */
46 static int get_password(const char *prompt
, char *str
, int maxlen
)
51 #if 0 /* this allows specifying a password some other way */
53 static int tried_once
= 0;
58 strncpy(str
, password
, maxlen
);
66 hin
= GetStdHandle(STD_INPUT_HANDLE
);
67 hout
= GetStdHandle(STD_OUTPUT_HANDLE
);
68 if (hin
== INVALID_HANDLE_VALUE
|| hout
== INVALID_HANDLE_VALUE
) {
69 fprintf(stderr
, "Cannot get standard input/output handles");
73 GetConsoleMode(hin
, &savemode
);
74 SetConsoleMode(hin
, (savemode
& (~ENABLE_ECHO_INPUT
)) |
75 ENABLE_PROCESSED_INPUT
| ENABLE_LINE_INPUT
);
77 WriteFile(hout
, prompt
, strlen(prompt
), &i
, NULL
);
78 ReadFile(hin
, str
, maxlen
-1, &i
, NULL
);
80 SetConsoleMode(hin
, savemode
);
82 if ((int)i
> maxlen
) i
= maxlen
-1; else i
= i
- 2;
85 WriteFile(hout
, "\r\n", 2, &i
, NULL
);
90 int WINAPI
stdin_read_thread(void *param
) {
91 struct input_data
*idata
= (struct input_data
*)param
;
94 inhandle
= GetStdHandle(STD_INPUT_HANDLE
);
96 while (ReadFile(inhandle
, idata
->buffer
, sizeof(idata
->buffer
),
98 SetEvent(idata
->event
);
102 SetEvent(idata
->event
);
107 int main(int argc
, char **argv
) {
110 WSAEVENT netevent
, stdinevent
;
114 struct input_data idata
;
117 ssh_get_password
= get_password
;
121 * Process the command line.
123 default_protocol
= DEFAULT_PROTOCOL
;
124 default_port
= DEFAULT_PORT
;
129 if (!strcmp(p
, "-ssh")) {
130 default_protocol
= cfg
.protocol
= PROT_SSH
;
131 default_port
= cfg
.port
= 22;
132 } else if (!strcmp(p
, "-v")) {
133 flags
|= FLAG_VERBOSE
;
134 } else if (!strcmp(p
, "-log")) {
135 logfile
= "putty.log";
141 * If the hostname starts with "telnet:", set the
142 * protocol to Telnet and process the string as a
145 if (!strncmp(q
, "telnet:", 7)) {
149 if (q
[0] == '/' && q
[1] == '/')
151 cfg
.protocol
= PROT_TELNET
;
153 while (*p
&& *p
!= ':' && *p
!= '/') p
++;
161 strncpy (cfg
.host
, q
, sizeof(cfg
.host
)-1);
162 cfg
.host
[sizeof(cfg
.host
)-1] = '\0';
165 * Three cases. Either (a) there's a nonzero
166 * length string followed by an @, in which
167 * case that's user and the remainder is host.
168 * Or (b) there's only one string, not counting
169 * a potential initial @, and it exists in the
170 * saved-sessions database. Or (c) only one
171 * string and it _doesn't_ exist in the
174 char *r
= strrchr(p
, '@');
175 if (r
== p
) p
++, r
= NULL
; /* discount initial @ */
181 if (cfg
.host
[0] == '\0') {
182 /* No settings for this host; use defaults */
183 strncpy(cfg
.host
, p
, sizeof(cfg
.host
)-1);
184 cfg
.host
[sizeof(cfg
.host
)-1] = '\0';
189 strncpy(cfg
.username
, p
, sizeof(cfg
.username
)-1);
190 cfg
.username
[sizeof(cfg
.username
)-1] = '\0';
191 strncpy(cfg
.host
, r
, sizeof(cfg
.host
)-1);
192 cfg
.host
[sizeof(cfg
.host
)-1] = '\0';
197 int len
= sizeof(cfg
.remote_cmd
) - 1;
198 char *cp
= cfg
.remote_cmd
;
201 strncpy(cp
, p
, len
); cp
[len
] = '\0';
202 len2
= strlen(cp
); len
-= len2
; cp
+= len2
;
206 strncpy(cp
, *++argv
, len
); cp
[len
] = '\0';
207 len2
= strlen(cp
); len
-= len2
; cp
+= len2
;
209 cfg
.nopty
= TRUE
; /* command => no terminal */
210 cfg
.ldisc_term
= TRUE
; /* use stdin like a line buffer */
211 break; /* done with cmdline */
216 if (!*cfg
.remote_cmd
)
217 flags
|= FLAG_INTERACTIVE
;
220 * Select protocol. This is farmed out into a table in a
221 * separate file to enable an ssh-free variant.
226 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
227 if (backends
[i
].protocol
== cfg
.protocol
) {
228 back
= backends
[i
].backend
;
232 fprintf(stderr
, "Internal fault: Unsupported protocol found\n");
238 * Initialise WinSock.
240 winsock_ver
= MAKEWORD(2, 0);
241 if (WSAStartup(winsock_ver
, &wsadata
)) {
242 MessageBox(NULL
, "Unable to initialise WinSock", "WinSock Error",
243 MB_OK
| MB_ICONEXCLAMATION
);
246 if (LOBYTE(wsadata
.wVersion
) != 2 || HIBYTE(wsadata
.wVersion
) != 0) {
247 MessageBox(NULL
, "WinSock version is incompatible with 2.0",
248 "WinSock Error", MB_OK
| MB_ICONEXCLAMATION
);
254 * Start up the connection.
260 error
= back
->init (NULL
, cfg
.host
, cfg
.port
, &realhost
);
262 fprintf(stderr
, "Unable to open connection:\n%s", error
);
267 netevent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
268 stdinevent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
271 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), ENABLE_PROCESSED_INPUT
);
272 outhandle
= GetStdHandle(STD_OUTPUT_HANDLE
);
275 * Now we must send the back end oodles of stuff.
277 socket
= back
->socket();
279 * Turn off ECHO and LINE input modes. We don't care if this
280 * call fails, because we know we aren't necessarily running in
283 WSAEventSelect(socket
, netevent
, FD_READ
| FD_CLOSE
);
284 handles
[0] = netevent
;
285 handles
[1] = stdinevent
;
289 n
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
291 WSANETWORKEVENTS things
;
292 if (!WSAEnumNetworkEvents(socket
, netevent
, &things
)) {
293 if (things
.lNetworkEvents
& FD_READ
)
294 back
->msg(0, FD_READ
);
295 if (things
.lNetworkEvents
& FD_CLOSE
) {
296 back
->msg(0, FD_CLOSE
);
301 if (!sending
&& back
->sendok()) {
303 * Create a separate thread to read from stdin.
304 * This is a total pain, but I can't find another
307 * - an overlapped ReadFile or ReadFileEx just
308 * doesn't happen; we get failure from
309 * ReadFileEx, and ReadFile blocks despite being
310 * given an OVERLAPPED structure. Perhaps we
311 * can't do overlapped reads on consoles. WHY
314 * - WaitForMultipleObjects(netevent, console)
315 * doesn't work, because it signals the console
316 * when _anything_ happens, including mouse
317 * motions and other things that don't cause
318 * data to be readable - so we're back to
321 idata
.event
= stdinevent
;
322 if (!CreateThread(NULL
, 0, stdin_read_thread
,
323 &idata
, 0, &threadid
)) {
324 fprintf(stderr
, "Unable to create second thread\n");
331 back
->send(idata
.buffer
, idata
.len
);
333 back
->special(TS_EOF
);