Asynchronous agent requests on Windows. Actually, I've kept the
[u/mdw/putty] / psftp.c
diff --git a/psftp.c b/psftp.c
index 8ed4e20..c93d7be 100644 (file)
--- a/psftp.c
+++ b/psftp.c
@@ -34,6 +34,7 @@ static int do_sftp_init(void);
 char *pwd, *homedir;
 static Backend *back;
 static void *backhandle;
+static Config cfg;
 
 /* ----------------------------------------------------------------------
  * Higher-level helper functions used in commands.
@@ -244,8 +245,7 @@ int sftp_cmd_ls(struct sftp_command *cmd)
 
            if (nnames + names->nnames >= namesize) {
                namesize += names->nnames + 128;
-               ournames =
-                   srealloc(ournames, namesize * sizeof(*ournames));
+               ournames = sresize(ournames, namesize, struct fxp_name *);
            }
 
            for (i = 0; i < names->nnames; i++)
@@ -832,7 +832,7 @@ int sftp_cmd_chmod(struct sftp_command *cmd)
            if (!(subset & 06777) && (perms &~ subset)) {
                printf("chmod: file mode '%.*s' contains no user/group/other"
                       " specifier and permissions other than 't' \n",
-                      strcspn(modebegin, ","), modebegin, *mode);
+                      strcspn(modebegin, ","), modebegin);
                return 0;
            }
            perms &= subset;
@@ -933,10 +933,10 @@ static int sftp_cmd_lcd(struct sftp_command *cmd)
        return 0;
     }
 
-    currdir = smalloc(256);
+    currdir = snewn(256, char);
     len = GetCurrentDirectory(256, currdir);
     if (len > 256)
-       currdir = srealloc(currdir, len);
+       currdir = sresize(currdir, len, char);
     GetCurrentDirectory(len, currdir);
     printf("New local directory is %s\n", currdir);
     sfree(currdir);
@@ -949,10 +949,10 @@ static int sftp_cmd_lpwd(struct sftp_command *cmd)
     char *currdir;
     int len;
 
-    currdir = smalloc(256);
+    currdir = snewn(256, char);
     len = GetCurrentDirectory(256, currdir);
     if (len > 256)
-       currdir = srealloc(currdir, len);
+       currdir = sresize(currdir, len, char);
     GetCurrentDirectory(len, currdir);
     printf("Current local directory is %s\n", currdir);
     sfree(currdir);
@@ -1253,7 +1253,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags)
     }
     fflush(stdout);
 
-    cmd = smalloc(sizeof(struct sftp_command));
+    cmd = snew(struct sftp_command);
     cmd->words = NULL;
     cmd->nwords = 0;
     cmd->wordssize = 0;
@@ -1265,7 +1265,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags)
        char *ret;
 
        linesize += 512;
-       line = srealloc(line, linesize);
+       line = sresize(line, linesize, char);
        ret = fgets(line + linelen, linesize - linelen, fp);
 
        if (!ret || (linelen == 0 && line[0] == '\0')) {
@@ -1297,7 +1297,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags)
         * containing everything else on the line.
         */
        cmd->nwords = cmd->wordssize = 2;
-       cmd->words = srealloc(cmd->words, cmd->wordssize * sizeof(char *));
+       cmd->words = sresize(cmd->words, cmd->wordssize, char *);
        cmd->words[0] = "!";
        cmd->words[1] = p+1;
     } else {
@@ -1340,8 +1340,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags)
            *r = '\0';
            if (cmd->nwords >= cmd->wordssize) {
                cmd->wordssize = cmd->nwords + 16;
-               cmd->words =
-                   srealloc(cmd->words, cmd->wordssize * sizeof(char *));
+               cmd->words = sresize(cmd->words, cmd->wordssize, char *);
            }
            cmd->words[cmd->nwords++] = q;
        }
@@ -1515,6 +1514,16 @@ char *do_select(SOCKET skt, int startup)
 extern int select_result(WPARAM, LPARAM);
 
 /*
+ * In psftp, all agent requests should be synchronous, so this is a
+ * never-called stub.
+ */
+void agent_schedule_callback(void (*callback)(void *, void *, int),
+                            void *callback_ctx, void *data, int len)
+{
+    assert(!"We shouldn't be here");
+}
+
+/*
  * Receive a block of data from the SSH link. Block until all data
  * is available.
  *
@@ -1527,7 +1536,7 @@ static unsigned char *outptr;            /* where to put the data */
 static unsigned outlen;                       /* how much data required */
 static unsigned char *pending = NULL;  /* any spare data */
 static unsigned pendlen = 0, pendsize = 0;     /* length and phys. size of buffer */
-int from_backend(void *frontend, int is_stderr, char *data, int datalen)
+int from_backend(void *frontend, int is_stderr, const char *data, int datalen)
 {
     unsigned char *p = (unsigned char *) data;
     unsigned len = (unsigned) datalen;
@@ -1563,10 +1572,7 @@ int from_backend(void *frontend, int is_stderr, char *data, int datalen)
     if (len > 0) {
        if (pendsize < pendlen + len) {
            pendsize = pendlen + len + 4096;
-           pending = (pending ? srealloc(pending, pendsize) :
-                      smalloc(pendsize));
-           if (!pending)
-               fatalbox("Out of memory");
+           pending = sresize(pending, pendsize, unsigned char);
        }
        memcpy(pending + pendlen, p, len);
        pendlen += len;
@@ -1722,7 +1728,7 @@ static int psftp_connect(char *userhost, char *user, int portnumber)
     /*
      * Enact command-line overrides.
      */
-    cmdline_run_saved();
+    cmdline_run_saved(&cfg);
 
     /*
      * Trim leading whitespace off the hostname if it's there.
@@ -1828,13 +1834,14 @@ static int psftp_connect(char *userhost, char *user, int portnumber)
 
     back = &ssh_backend;
 
-    err = back->init(NULL, &backhandle, cfg.host, cfg.port, &realhost, 0);
+    err = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, &realhost,0);
     if (err != NULL) {
        fprintf(stderr, "ssh_init: %s\n", err);
        return 1;
     }
-    logctx = log_init(NULL);
+    logctx = log_init(NULL, &cfg);
     back->provide_logctx(backhandle, logctx);
+    console_provide_logctx(logctx);
     ssh_sftp_init();
     if (verbose && realhost != NULL)
        printf("Connected to %s\n", realhost);
@@ -1844,11 +1851,11 @@ static int psftp_connect(char *userhost, char *user, int portnumber)
 void cmdline_error(char *p, ...)
 {
     va_list ap;
-    fprintf(stderr, "pscp: ");
+    fprintf(stderr, "psftp: ");
     va_start(ap, p);
     vfprintf(stderr, p, ap);
     va_end(ap);
-    fputc('\n', stderr);
+    fprintf(stderr, "\n       try typing \"psftp -h\" for help\n");
     exit(1);
 }
 
@@ -1863,8 +1870,9 @@ int main(int argc, char *argv[])
     int mode = 0;
     int modeflags = 0;
     char *batchfile = NULL;
+    int errors = 0;
 
-    flags = FLAG_STDERR | FLAG_INTERACTIVE;
+    flags = FLAG_STDERR | FLAG_INTERACTIVE | FLAG_SYNCAGENT;
     cmdline_tooltype = TOOLTYPE_FILETRANSFER;
     ssh_get_line = &console_get_line;
     init_winsock();
@@ -1872,6 +1880,7 @@ int main(int argc, char *argv[])
 
     userhost = user = NULL;
 
+    errors = 0;
     for (i = 1; i < argc; i++) {
        int ret;
        if (argv[i][0] != '-') {
@@ -1881,7 +1890,7 @@ int main(int argc, char *argv[])
                 userhost = dupstr(argv[i]);
            continue;
        }
-       ret = cmdline_process_param(argv[i], i+1<argc?argv[i+1]:NULL, 1);
+       ret = cmdline_process_param(argv[i], i+1<argc?argv[i+1]:NULL, 1, &cfg);
        if (ret == -2) {
            cmdline_error("option \"%s\" requires an argument", argv[i]);
        } else if (ret == 2) {
@@ -1906,7 +1915,7 @@ int main(int argc, char *argv[])
            i++;
            break;
        } else {
-           usage();
+           cmdline_error("unknown option \"%s\"", argv[i]);
        }
     }
     argc -= i;