version 1.56, 2004/07/11 17:48:47 |
version 1.56.2.1, 2005/03/10 16:28:28 |
|
|
RCSID("$OpenBSD$"); |
RCSID("$OpenBSD$"); |
|
|
#include <glob.h> |
#include <glob.h> |
|
#include <histedit.h> |
|
|
#include "buffer.h" |
#include "buffer.h" |
#include "xmalloc.h" |
#include "xmalloc.h" |
|
|
static void |
static void |
killchild(int signo) |
killchild(int signo) |
{ |
{ |
if (sshpid > 1) |
if (sshpid > 1) { |
kill(sshpid, SIGTERM); |
kill(sshpid, SIGTERM); |
|
waitpid(sshpid, NULL, 0); |
|
} |
|
|
_exit(1); |
_exit(1); |
} |
} |
|
|
cmd_interrupt(int signo) |
cmd_interrupt(int signo) |
{ |
{ |
const char msg[] = "\rInterrupt \n"; |
const char msg[] = "\rInterrupt \n"; |
|
int olderrno = errno; |
|
|
write(STDERR_FILENO, msg, sizeof(msg) - 1); |
write(STDERR_FILENO, msg, sizeof(msg) - 1); |
interrupted = 1; |
interrupted = 1; |
|
errno = olderrno; |
} |
} |
|
|
static void |
static void |
|
|
return (xstrdup(path)); |
return (xstrdup(path)); |
|
|
len = strlen(strip); |
len = strlen(strip); |
if (strip != NULL && strncmp(path, strip, len) == 0) { |
if (strncmp(path, strip, len) == 0) { |
if (strip[len - 1] != '/' && path[len] == '/') |
if (strip[len - 1] != '/' && path[len] == '/') |
len++; |
len++; |
return (xstrdup(path + len)); |
return (xstrdup(path + len)); |
|
|
{ |
{ |
glob_t g; |
glob_t g; |
int i, c = 1, colspace = 0, columns = 1; |
int i, c = 1, colspace = 0, columns = 1; |
Attrib *a; |
Attrib *a = NULL; |
|
|
memset(&g, 0, sizeof(g)); |
memset(&g, 0, sizeof(g)); |
|
|
if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE, |
if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE, |
NULL, &g)) { |
NULL, &g) || (g.gl_pathc && !g.gl_matchc)) { |
|
if (g.gl_pathc) |
|
globfree(&g); |
error("Can't ls: \"%s\" not found", path); |
error("Can't ls: \"%s\" not found", path); |
return (-1); |
return (-1); |
} |
} |
|
|
goto out; |
goto out; |
|
|
/* |
/* |
* If the glob returns a single match, which is the same as the |
* If the glob returns a single match and it is a directory, |
* input glob, and it is a directory, then just list its contents |
* then just list its contents. |
*/ |
*/ |
if (g.gl_pathc == 1 && |
if (g.gl_matchc == 1) { |
strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) { |
if ((a = do_lstat(conn, g.gl_pathv[0], 1)) == NULL) { |
if ((a = do_lstat(conn, path, 1)) == NULL) { |
|
globfree(&g); |
globfree(&g); |
return (-1); |
return (-1); |
} |
} |
if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
S_ISDIR(a->perm)) { |
S_ISDIR(a->perm)) { |
|
int err; |
|
|
|
err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag); |
globfree(&g); |
globfree(&g); |
return (do_ls_dir(conn, path, strip_path, lflag)); |
return (err); |
} |
} |
} |
} |
|
|
|
|
colspace = width / columns; |
colspace = width / columns; |
} |
} |
|
|
for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
for (i = 0; g.gl_pathv[i] && !interrupted; i++, a = NULL) { |
char *fname; |
char *fname; |
|
|
fname = path_strip(g.gl_pathv[i], strip_path); |
fname = path_strip(g.gl_pathv[i], strip_path); |
|
|
* that the server returns as well as the filenames. |
* that the server returns as well as the filenames. |
*/ |
*/ |
memset(&sb, 0, sizeof(sb)); |
memset(&sb, 0, sizeof(sb)); |
a = do_lstat(conn, g.gl_pathv[i], 1); |
if (a == NULL) |
|
a = do_lstat(conn, g.gl_pathv[i], 1); |
if (a != NULL) |
if (a != NULL) |
attrib_to_stat(a, &sb); |
attrib_to_stat(a, &sb); |
lname = ls_file(fname, &sb, 1); |
lname = ls_file(fname, &sb, 1); |
|
|
return (0); |
return (0); |
} |
} |
|
|
|
static char * |
|
prompt(EditLine *el) |
|
{ |
|
return ("sftp> "); |
|
} |
|
|
int |
int |
interactive_loop(int fd_in, int fd_out, char *file1, char *file2) |
interactive_loop(int fd_in, int fd_out, char *file1, char *file2) |
{ |
{ |
|
|
char cmd[2048]; |
char cmd[2048]; |
struct sftp_conn *conn; |
struct sftp_conn *conn; |
int err; |
int err; |
|
EditLine *el = NULL; |
|
History *hl = NULL; |
|
HistEvent hev; |
|
extern char *__progname; |
|
|
|
if (!batchmode && isatty(STDIN_FILENO)) { |
|
if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL) |
|
fatal("Couldn't initialise editline"); |
|
if ((hl = history_init()) == NULL) |
|
fatal("Couldn't initialise editline history"); |
|
history(hl, &hev, H_SETSIZE, 100); |
|
el_set(el, EL_HIST, history, hl); |
|
|
|
el_set(el, EL_PROMPT, prompt); |
|
el_set(el, EL_EDITOR, "emacs"); |
|
el_set(el, EL_TERMINAL, NULL); |
|
el_set(el, EL_SIGNAL, 1); |
|
el_source(el, NULL); |
|
} |
|
|
conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests); |
conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests); |
if (conn == NULL) |
if (conn == NULL) |
fatal("Couldn't initialise connection to server"); |
fatal("Couldn't initialise connection to server"); |
|
|
if (remote_is_dir(conn, dir) && file2 == NULL) { |
if (remote_is_dir(conn, dir) && file2 == NULL) { |
printf("Changing to: %s\n", dir); |
printf("Changing to: %s\n", dir); |
snprintf(cmd, sizeof cmd, "cd \"%s\"", dir); |
snprintf(cmd, sizeof cmd, "cd \"%s\"", dir); |
if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) |
if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) { |
|
xfree(dir); |
|
xfree(pwd); |
return (-1); |
return (-1); |
|
} |
} else { |
} else { |
if (file2 == NULL) |
if (file2 == NULL) |
snprintf(cmd, sizeof cmd, "get %s", dir); |
snprintf(cmd, sizeof cmd, "get %s", dir); |
|
|
err = 0; |
err = 0; |
for (;;) { |
for (;;) { |
char *cp; |
char *cp; |
|
const char *line; |
|
int count = 0; |
|
|
signal(SIGINT, SIG_IGN); |
signal(SIGINT, SIG_IGN); |
|
|
printf("sftp> "); |
if (el == NULL) { |
|
printf("sftp> "); |
/* XXX: use libedit */ |
if (fgets(cmd, sizeof(cmd), infile) == NULL) { |
if (fgets(cmd, sizeof(cmd), infile) == NULL) { |
printf("\n"); |
printf("\n"); |
break; |
break; |
} |
|
if (batchmode) /* Echo command */ |
|
printf("%s", cmd); |
|
} else { |
|
if ((line = el_gets(el, &count)) == NULL || count <= 0) |
|
break; |
|
history(hl, &hev, H_ENTER, line); |
|
if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) { |
|
fprintf(stderr, "Error: input line too long\n"); |
|
continue; |
|
} |
} |
} |
|
|
if (batchmode) /* Echo command */ |
|
printf("%s", cmd); |
|
|
|
cp = strrchr(cmd, '\n'); |
cp = strrchr(cmd, '\n'); |
if (cp) |
if (cp) |
*cp = '\0'; |
*cp = '\0'; |
|
|
fatal("%s (%s).", strerror(errno), optarg); |
fatal("%s (%s).", strerror(errno), optarg); |
showprogress = 0; |
showprogress = 0; |
batchmode = 1; |
batchmode = 1; |
|
addargs(&args, "-obatchmode yes"); |
break; |
break; |
case 'P': |
case 'P': |
sftp_direct = optarg; |
sftp_direct = optarg; |