Yet more global-removal. The static variables in logging.c are now
[u/mdw/putty] / plink.c
diff --git a/plink.c b/plink.c
index 9229fa8..528eee6 100644 (file)
--- a/plink.c
+++ b/plink.c
@@ -29,7 +29,18 @@ void fatalbox(char *p, ...)
     WSACleanup();
     cleanup_exit(1);
 }
-void connection_fatal(char *p, ...)
+void modalfatalbox(char *p, ...)
+{
+    va_list ap;
+    fprintf(stderr, "FATAL ERROR: ");
+    va_start(ap, p);
+    vfprintf(stderr, p, ap);
+    va_end(ap);
+    fputc('\n', stderr);
+    WSACleanup();
+    cleanup_exit(1);
+}
+void connection_fatal(void *frontend, char *p, ...)
 {
     va_list ap;
     fprintf(stderr, "FATAL ERROR: ");
@@ -58,11 +69,14 @@ DWORD orig_console_mode;
 
 WSAEVENT netevent;
 
-int term_ldisc(int mode)
+static Backend *back;
+static void *backhandle;
+
+int term_ldisc(Terminal *term, int mode)
 {
     return FALSE;
 }
-void ldisc_update(int echo, int edit)
+void ldisc_update(void *frontend, int echo, int edit)
 {
     /* Update stdin read mode to reflect changes in line discipline. */
     DWORD mode;
@@ -153,7 +167,7 @@ void try_output(int is_stderr)
     }
 }
 
-int from_backend(int is_stderr, char *data, int len)
+int from_backend(void *frontend_handle, int is_stderr, char *data, int len)
 {
     HANDLE h = (is_stderr ? errhandle : outhandle);
     int osize, esize;
@@ -291,8 +305,6 @@ int main(int argc, char **argv)
                continue;
            } else if (!strcmp(p, "-batch")) {
                console_batch_mode = 1;
-           } else if (!strcmp(p, "-log")) {
-               logfile = "putty.log";
            }
        } else if (*p) {
            if (!*cfg.host) {
@@ -449,6 +461,21 @@ int main(int argc, char **argv)
      */
     cfg.host[strcspn(cfg.host, ":")] = '\0';
 
+    /*
+     * Remove any remaining whitespace from the hostname.
+     */
+    {
+       int p1 = 0, p2 = 0;
+       while (cfg.host[p2] != '\0') {
+           if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
+               cfg.host[p1] = cfg.host[p2];
+               p1++;
+           }
+           p2++;
+       }
+       cfg.host[p1] = '\0';
+    }
+
     if (!*cfg.remote_cmd_ptr)
        flags |= FLAG_INTERACTIVE;
 
@@ -505,11 +532,14 @@ int main(int argc, char **argv)
        int nodelay = cfg.tcp_nodelay &&
            (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR);
 
-       error = back->init(cfg.host, cfg.port, &realhost, nodelay);
+       error = back->init(NULL, &backhandle, cfg.host, cfg.port,
+                          &realhost, nodelay);
        if (error) {
            fprintf(stderr, "Unable to open connection:\n%s", error);
            return 1;
        }
+       logctx = log_init(NULL);
+       back->provide_logctx(backhandle, logctx);
        sfree(realhost);
     }
     connopen = 1;
@@ -561,7 +591,7 @@ int main(int argc, char **argv)
     while (1) {
        int n;
 
-       if (!sending && back->sendok()) {
+       if (!sending && back->sendok(backhandle)) {
            /*
             * Create a separate thread to read from stdin. This is
             * a total pain, but I can't find another way to do it:
@@ -653,11 +683,11 @@ int main(int argc, char **argv)
        } else if (n == 1) {
            reading = 0;
            noise_ultralight(idata.len);
-           if (connopen && back->socket() != NULL) {
+           if (connopen && back->socket(backhandle) != NULL) {
                if (idata.len > 0) {
-                   back->send(idata.buffer, idata.len);
+                   back->send(backhandle, idata.buffer, idata.len);
                } else {
-                   back->special(TS_EOF);
+                   back->special(backhandle, TS_EOF);
                }
            }
        } else if (n == 2) {
@@ -669,8 +699,8 @@ int main(int argc, char **argv)
            bufchain_consume(&stdout_data, odata.lenwritten);
            if (bufchain_size(&stdout_data) > 0)
                try_output(0);
-           if (connopen && back->socket() != NULL) {
-               back->unthrottle(bufchain_size(&stdout_data) +
+           if (connopen && back->socket(backhandle) != NULL) {
+               back->unthrottle(backhandle, bufchain_size(&stdout_data) +
                                 bufchain_size(&stderr_data));
            }
        } else if (n == 3) {
@@ -682,22 +712,22 @@ int main(int argc, char **argv)
            bufchain_consume(&stderr_data, edata.lenwritten);
            if (bufchain_size(&stderr_data) > 0)
                try_output(1);
-           if (connopen && back->socket() != NULL) {
-               back->unthrottle(bufchain_size(&stdout_data) +
+           if (connopen && back->socket(backhandle) != NULL) {
+               back->unthrottle(backhandle, bufchain_size(&stdout_data) +
                                 bufchain_size(&stderr_data));
            }
        }
-       if (!reading && back->sendbuffer() < MAX_STDIN_BACKLOG) {
+       if (!reading && back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) {
            SetEvent(idata.eventback);
            reading = 1;
        }
-       if ((!connopen || back->socket() == NULL) &&
+       if ((!connopen || back->socket(backhandle) == NULL) &&
            bufchain_size(&stdout_data) == 0 &&
            bufchain_size(&stderr_data) == 0)
            break;                     /* we closed the connection */
     }
     WSACleanup();
-    exitcode = back->exitcode();
+    exitcode = back->exitcode(backhandle);
     if (exitcode < 0) {
        fprintf(stderr, "Remote process exit code unavailable\n");
        exitcode = 1;                  /* this is an error condition */