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 int WINAPI
stdin_read_thread(void *param
) {
47 struct input_data
*idata
= (struct input_data
*)param
;
50 inhandle
= GetStdHandle(STD_INPUT_HANDLE
);
52 while (ReadFile(inhandle
, idata
->buffer
, sizeof(idata
->buffer
),
54 SetEvent(idata
->event
);
58 SetEvent(idata
->event
);
63 int main(int argc
, char **argv
) {
66 WSAEVENT netevent
, stdinevent
;
70 struct input_data idata
;
73 flags
= FLAG_CONNECTION
;
75 * Process the command line.
77 default_protocol
= DEFAULT_PROTOCOL
;
78 default_port
= DEFAULT_PORT
;
83 if (!strcmp(p
, "-ssh")) {
84 default_protocol
= cfg
.protocol
= PROT_SSH
;
85 default_port
= cfg
.port
= 22;
86 } else if (!strcmp(p
, "-log")) {
87 logfile
= "putty.log";
93 * If the hostname starts with "telnet:", set the
94 * protocol to Telnet and process the string as a
97 if (!strncmp(q
, "telnet:", 7)) {
101 if (q
[0] == '/' && q
[1] == '/')
103 cfg
.protocol
= PROT_TELNET
;
105 while (*p
&& *p
!= ':' && *p
!= '/') p
++;
113 strncpy (cfg
.host
, q
, sizeof(cfg
.host
)-1);
114 cfg
.host
[sizeof(cfg
.host
)-1] = '\0';
117 * Three cases. Either (a) there's a nonzero
118 * length string followed by an @, in which
119 * case that's user and the remainder is host.
120 * Or (b) there's only one string, not counting
121 * a potential initial @, and it exists in the
122 * saved-sessions database. Or (c) only one
123 * string and it _doesn't_ exist in the
126 char *r
= strrchr(p
, '@');
127 if (r
== p
) p
++, r
= NULL
; /* discount initial @ */
133 if (cfg
.host
[0] == '\0') {
134 /* No settings for this host; use defaults */
135 strncpy(cfg
.host
, p
, sizeof(cfg
.host
)-1);
136 cfg
.host
[sizeof(cfg
.host
)-1] = '\0';
141 strncpy(cfg
.username
, p
, sizeof(cfg
.username
)-1);
142 cfg
.username
[sizeof(cfg
.username
)-1] = '\0';
143 strncpy(cfg
.host
, r
, sizeof(cfg
.host
)-1);
144 cfg
.host
[sizeof(cfg
.host
)-1] = '\0';
149 int len
= sizeof(cfg
.remote_cmd
) - 1;
150 char *cp
= cfg
.remote_cmd
;
153 strncpy(cp
, p
, len
); cp
[len
] = '\0';
154 len2
= strlen(cp
); len
-= len2
; cp
+= len2
;
158 strncpy(cp
, *++argv
, len
); cp
[len
] = '\0';
159 len2
= strlen(cp
); len
-= len2
; cp
+= len2
;
161 cfg
.nopty
= TRUE
; /* command => no terminal */
162 cfg
.ldisc_term
= TRUE
; /* use stdin like a line buffer */
163 break; /* done with cmdline */
169 * Select protocol. This is farmed out into a table in a
170 * separate file to enable an ssh-free variant.
175 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
176 if (backends
[i
].protocol
== cfg
.protocol
) {
177 back
= backends
[i
].backend
;
181 fprintf(stderr
, "Internal fault: Unsupported protocol found\n");
187 * Initialise WinSock.
189 winsock_ver
= MAKEWORD(2, 0);
190 if (WSAStartup(winsock_ver
, &wsadata
)) {
191 MessageBox(NULL
, "Unable to initialise WinSock", "WinSock Error",
192 MB_OK
| MB_ICONEXCLAMATION
);
195 if (LOBYTE(wsadata
.wVersion
) != 2 || HIBYTE(wsadata
.wVersion
) != 0) {
196 MessageBox(NULL
, "WinSock version is incompatible with 2.0",
197 "WinSock Error", MB_OK
| MB_ICONEXCLAMATION
);
203 * Start up the connection.
209 error
= back
->init (NULL
, cfg
.host
, cfg
.port
, &realhost
);
211 fprintf(stderr
, "Unable to open connection:\n%s", error
);
216 netevent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
217 stdinevent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
220 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), ENABLE_PROCESSED_INPUT
);
221 outhandle
= GetStdHandle(STD_OUTPUT_HANDLE
);
224 * Now we must send the back end oodles of stuff.
226 socket
= back
->socket();
228 * Turn off ECHO and LINE input modes. We don't care if this
229 * call fails, because we know we aren't necessarily running in
232 WSAEventSelect(socket
, netevent
, FD_READ
| FD_CLOSE
);
233 handles
[0] = netevent
;
234 handles
[1] = stdinevent
;
238 n
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
240 WSANETWORKEVENTS things
;
241 if (!WSAEnumNetworkEvents(socket
, netevent
, &things
)) {
242 if (things
.lNetworkEvents
& FD_READ
)
243 back
->msg(0, FD_READ
);
244 if (things
.lNetworkEvents
& FD_CLOSE
) {
245 back
->msg(0, FD_CLOSE
);
250 if (!sending
&& back
->sendok()) {
252 * Create a separate thread to read from stdin.
253 * This is a total pain, but I can't find another
256 * - an overlapped ReadFile or ReadFileEx just
257 * doesn't happen; we get failure from
258 * ReadFileEx, and ReadFile blocks despite being
259 * given an OVERLAPPED structure. Perhaps we
260 * can't do overlapped reads on consoles. WHY
263 * - WaitForMultipleObjects(netevent, console)
264 * doesn't work, because it signals the console
265 * when _anything_ happens, including mouse
266 * motions and other things that don't cause
267 * data to be readable - so we're back to
270 idata
.event
= stdinevent
;
271 if (!CreateThread(NULL
, 0, stdin_read_thread
,
272 &idata
, 0, &threadid
)) {
273 fprintf(stderr
, "Unable to create second thread\n");
280 back
->send(idata
.buffer
, idata
.len
);
282 back
->special(TS_EOF
);