blob: 46cbd4a129eb59cbf66cce1b39626537d0d50a41 [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
Damien Miller3db5f532002-02-13 14:10:32 +11002 * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
Damien Miller33804262001-02-04 23:20:18 +11003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
Damien Millerd7686fd2001-02-10 00:40:03 +110025/* XXX: globbed ls */
Damien Miller33804262001-02-04 23:20:18 +110026/* XXX: recursive operations */
27
28#include "includes.h"
Ben Lindstromcb1f60e2002-03-22 02:47:28 +000029RCSID("$OpenBSD: sftp-int.c,v 1.45 2002/03/19 06:32:56 mpech Exp $");
Damien Miller4870afd2001-03-14 10:27:09 +110030
Damien Miller33804262001-02-04 23:20:18 +110031#include "buffer.h"
32#include "xmalloc.h"
33#include "log.h"
34#include "pathnames.h"
35
36#include "sftp.h"
37#include "sftp-common.h"
Damien Miller4870afd2001-03-14 10:27:09 +110038#include "sftp-glob.h"
Damien Miller33804262001-02-04 23:20:18 +110039#include "sftp-client.h"
40#include "sftp-int.h"
41
Damien Miller058316f2001-03-08 10:08:49 +110042/* File to read commands from */
43extern FILE *infile;
44
Damien Miller8829d362002-02-08 22:04:05 +110045/* Size of buffer used when copying files */
46extern size_t copy_buffer_len;
47
Damien Miller16a13332002-02-13 14:03:56 +110048/* Number of concurrent outstanding requests */
49extern int num_requests;
50
Damien Miller33804262001-02-04 23:20:18 +110051/* Seperators for interactive commands */
52#define WHITESPACE " \t\r\n"
53
54/* Commands for interactive mode */
55#define I_CHDIR 1
56#define I_CHGRP 2
57#define I_CHMOD 3
58#define I_CHOWN 4
59#define I_GET 5
60#define I_HELP 6
61#define I_LCHDIR 7
62#define I_LLS 8
63#define I_LMKDIR 9
64#define I_LPWD 10
65#define I_LS 11
66#define I_LUMASK 12
67#define I_MKDIR 13
68#define I_PUT 14
69#define I_PWD 15
70#define I_QUIT 16
71#define I_RENAME 17
72#define I_RM 18
73#define I_RMDIR 19
74#define I_SHELL 20
Damien Miller058316f2001-03-08 10:08:49 +110075#define I_SYMLINK 21
Ben Lindstromf78682d2001-03-14 21:26:27 +000076#define I_VERSION 22
Damien Miller33804262001-02-04 23:20:18 +110077
78struct CMD {
Damien Miller33804262001-02-04 23:20:18 +110079 const char *c;
Kevin Steves62c45db2001-02-05 13:42:43 +000080 const int n;
Damien Miller33804262001-02-04 23:20:18 +110081};
82
83const struct CMD cmds[] = {
Ben Lindstrom59e12492001-08-15 23:22:56 +000084 { "bye", I_QUIT },
Damien Millerd7686fd2001-02-10 00:40:03 +110085 { "cd", I_CHDIR },
86 { "chdir", I_CHDIR },
87 { "chgrp", I_CHGRP },
88 { "chmod", I_CHMOD },
89 { "chown", I_CHOWN },
90 { "dir", I_LS },
91 { "exit", I_QUIT },
92 { "get", I_GET },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +000093 { "mget", I_GET },
Damien Millerd7686fd2001-02-10 00:40:03 +110094 { "help", I_HELP },
95 { "lcd", I_LCHDIR },
96 { "lchdir", I_LCHDIR },
97 { "lls", I_LLS },
98 { "lmkdir", I_LMKDIR },
Damien Miller058316f2001-03-08 10:08:49 +110099 { "ln", I_SYMLINK },
Damien Millerd7686fd2001-02-10 00:40:03 +1100100 { "lpwd", I_LPWD },
101 { "ls", I_LS },
102 { "lumask", I_LUMASK },
103 { "mkdir", I_MKDIR },
104 { "put", I_PUT },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +0000105 { "mput", I_PUT },
Damien Millerd7686fd2001-02-10 00:40:03 +1100106 { "pwd", I_PWD },
107 { "quit", I_QUIT },
108 { "rename", I_RENAME },
109 { "rm", I_RM },
110 { "rmdir", I_RMDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100111 { "symlink", I_SYMLINK },
Ben Lindstromf78682d2001-03-14 21:26:27 +0000112 { "version", I_VERSION },
Kevin Steves62c45db2001-02-05 13:42:43 +0000113 { "!", I_SHELL },
114 { "?", I_HELP },
115 { NULL, -1}
Damien Miller33804262001-02-04 23:20:18 +1100116};
117
Ben Lindstrombba81212001-06-25 05:01:22 +0000118static void
Damien Miller33804262001-02-04 23:20:18 +1100119help(void)
120{
121 printf("Available commands:\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100122 printf("cd path Change remote directory to 'path'\n");
123 printf("lcd path Change local directory to 'path'\n");
124 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
125 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
126 printf("chown own path Change owner of file 'path' to 'own'\n");
127 printf("help Display this help text\n");
128 printf("get remote-path [local-path] Download file\n");
129 printf("lls [ls-options [path]] Display local directory listing\n");
Damien Miller058316f2001-03-08 10:08:49 +1100130 printf("ln oldpath newpath Symlink remote file\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100131 printf("lmkdir path Create local directory\n");
132 printf("lpwd Print local working directory\n");
133 printf("ls [path] Display remote directory listing\n");
134 printf("lumask umask Set local umask to 'umask'\n");
135 printf("mkdir path Create remote directory\n");
136 printf("put local-path [remote-path] Upload file\n");
137 printf("pwd Display remote working directory\n");
138 printf("exit Quit sftp\n");
139 printf("quit Quit sftp\n");
140 printf("rename oldpath newpath Rename remote file\n");
141 printf("rmdir path Remove remote directory\n");
142 printf("rm path Delete remote file\n");
Damien Miller058316f2001-03-08 10:08:49 +1100143 printf("symlink oldpath newpath Symlink remote file\n");
Ben Lindstromf78682d2001-03-14 21:26:27 +0000144 printf("version Show SFTP version\n");
Damien Miller33804262001-02-04 23:20:18 +1100145 printf("!command Execute 'command' in local shell\n");
146 printf("! Escape to local shell\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100147 printf("? Synonym for help\n");
Damien Miller33804262001-02-04 23:20:18 +1100148}
149
Ben Lindstrombba81212001-06-25 05:01:22 +0000150static void
Damien Miller33804262001-02-04 23:20:18 +1100151local_do_shell(const char *args)
152{
Ben Lindstrom206941f2001-04-15 14:27:16 +0000153 int status;
Damien Miller33804262001-02-04 23:20:18 +1100154 char *shell;
155 pid_t pid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000156
Damien Miller33804262001-02-04 23:20:18 +1100157 if (!*args)
158 args = NULL;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000159
Damien Miller33804262001-02-04 23:20:18 +1100160 if ((shell = getenv("SHELL")) == NULL)
161 shell = _PATH_BSHELL;
162
163 if ((pid = fork()) == -1)
164 fatal("Couldn't fork: %s", strerror(errno));
165
166 if (pid == 0) {
167 /* XXX: child has pipe fds to ssh subproc open - issue? */
168 if (args) {
169 debug3("Executing %s -c \"%s\"", shell, args);
Damien Millerefb1edf2001-07-14 12:19:36 +1000170 execl(shell, shell, "-c", args, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100171 } else {
172 debug3("Executing %s", shell);
Damien Millerefb1edf2001-07-14 12:19:36 +1000173 execl(shell, shell, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100174 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000175 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
Damien Miller33804262001-02-04 23:20:18 +1100176 strerror(errno));
177 _exit(1);
178 }
179 if (waitpid(pid, &status, 0) == -1)
180 fatal("Couldn't wait for child: %s", strerror(errno));
181 if (!WIFEXITED(status))
182 error("Shell exited abormally");
183 else if (WEXITSTATUS(status))
184 error("Shell exited with status %d", WEXITSTATUS(status));
185}
186
Ben Lindstrombba81212001-06-25 05:01:22 +0000187static void
Damien Miller33804262001-02-04 23:20:18 +1100188local_do_ls(const char *args)
189{
190 if (!args || !*args)
Damien Millerd7686fd2001-02-10 00:40:03 +1100191 local_do_shell(_PATH_LS);
Damien Miller33804262001-02-04 23:20:18 +1100192 else {
Damien Millerd7686fd2001-02-10 00:40:03 +1100193 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
194 char *buf = xmalloc(len);
Damien Miller33804262001-02-04 23:20:18 +1100195
196 /* XXX: quoting - rip quoting code from ftp? */
Damien Millerd7686fd2001-02-10 00:40:03 +1100197 snprintf(buf, len, _PATH_LS " %s", args);
Damien Miller33804262001-02-04 23:20:18 +1100198 local_do_shell(buf);
Damien Millerd7686fd2001-02-10 00:40:03 +1100199 xfree(buf);
Damien Miller33804262001-02-04 23:20:18 +1100200 }
201}
202
Ben Lindstrombba81212001-06-25 05:01:22 +0000203static char *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000204path_append(char *p1, char *p2)
205{
206 char *ret;
Ben Lindstromcf00df62001-03-17 00:37:31 +0000207 int len = strlen(p1) + strlen(p2) + 2;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000208
Ben Lindstromcf00df62001-03-17 00:37:31 +0000209 ret = xmalloc(len);
210 strlcpy(ret, p1, len);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100211 if (strcmp(p1, "/") != 0)
Ben Lindstrom95148e32001-08-06 21:30:53 +0000212 strlcat(ret, "/", len);
Ben Lindstromcf00df62001-03-17 00:37:31 +0000213 strlcat(ret, p2, len);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000214
215 return(ret);
216}
217
Ben Lindstrombba81212001-06-25 05:01:22 +0000218static char *
Damien Miller33804262001-02-04 23:20:18 +1100219make_absolute(char *p, char *pwd)
220{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000221 char *abs;
Damien Miller33804262001-02-04 23:20:18 +1100222
223 /* Derelativise */
224 if (p && p[0] != '/') {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000225 abs = path_append(pwd, p);
Damien Miller33804262001-02-04 23:20:18 +1100226 xfree(p);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000227 return(abs);
228 } else
229 return(p);
230}
231
Ben Lindstrombba81212001-06-25 05:01:22 +0000232static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000233infer_path(const char *p, char **ifp)
234{
235 char *cp;
236
237 cp = strrchr(p, '/');
238 if (cp == NULL) {
239 *ifp = xstrdup(p);
240 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100241 }
242
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000243 if (!cp[1]) {
244 error("Invalid path");
245 return(-1);
246 }
247
248 *ifp = xstrdup(cp + 1);
249 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100250}
251
Ben Lindstrombba81212001-06-25 05:01:22 +0000252static int
Damien Miller33804262001-02-04 23:20:18 +1100253parse_getput_flags(const char **cpp, int *pflag)
254{
255 const char *cp = *cpp;
256
257 /* Check for flags */
258 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100259 switch (cp[1]) {
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000260 case 'p':
Damien Miller33804262001-02-04 23:20:18 +1100261 case 'P':
262 *pflag = 1;
263 break;
264 default:
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000265 error("Invalid flag -%c", cp[1]);
Damien Miller33804262001-02-04 23:20:18 +1100266 return(-1);
267 }
268 cp += 2;
269 *cpp = cp + strspn(cp, WHITESPACE);
270 }
271
272 return(0);
273}
274
Ben Lindstrombba81212001-06-25 05:01:22 +0000275static int
Damien Miller33804262001-02-04 23:20:18 +1100276get_pathname(const char **cpp, char **path)
277{
Damien Millerd7686fd2001-02-10 00:40:03 +1100278 const char *cp = *cpp, *end;
279 char quot;
Damien Miller33804262001-02-04 23:20:18 +1100280 int i;
281
282 cp += strspn(cp, WHITESPACE);
283 if (!*cp) {
284 *cpp = cp;
285 *path = NULL;
Damien Millerd7686fd2001-02-10 00:40:03 +1100286 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100287 }
288
289 /* Check for quoted filenames */
290 if (*cp == '\"' || *cp == '\'') {
Damien Millerd7686fd2001-02-10 00:40:03 +1100291 quot = *cp++;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000292
Damien Millerd7686fd2001-02-10 00:40:03 +1100293 end = strchr(cp, quot);
294 if (end == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100295 error("Unterminated quote");
Damien Millerd7686fd2001-02-10 00:40:03 +1100296 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100297 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100298 if (cp == end) {
Damien Miller33804262001-02-04 23:20:18 +1100299 error("Empty quotes");
Damien Millerd7686fd2001-02-10 00:40:03 +1100300 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100301 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100302 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
303 } else {
304 /* Read to end of filename */
305 end = strpbrk(cp, WHITESPACE);
306 if (end == NULL)
307 end = strchr(cp, '\0');
308 *cpp = end + strspn(end, WHITESPACE);
Damien Miller33804262001-02-04 23:20:18 +1100309 }
310
Damien Millerd7686fd2001-02-10 00:40:03 +1100311 i = end - cp;
Damien Miller33804262001-02-04 23:20:18 +1100312
313 *path = xmalloc(i + 1);
314 memcpy(*path, cp, i);
315 (*path)[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100316 return(0);
Damien Millerd7686fd2001-02-10 00:40:03 +1100317
318 fail:
319 *path = NULL;
320 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100321}
322
Ben Lindstrombba81212001-06-25 05:01:22 +0000323static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000324is_dir(char *path)
Damien Miller33804262001-02-04 23:20:18 +1100325{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000326 struct stat sb;
Damien Miller33804262001-02-04 23:20:18 +1100327
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000328 /* XXX: report errors? */
329 if (stat(path, &sb) == -1)
Damien Miller33804262001-02-04 23:20:18 +1100330 return(0);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000331
332 return(sb.st_mode & S_IFDIR);
333}
334
Ben Lindstrombba81212001-06-25 05:01:22 +0000335static int
Damien Miller3db5f532002-02-13 14:10:32 +1100336remote_is_dir(struct sftp_conn *conn, char *path)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000337{
338 Attrib *a;
339
340 /* XXX: report errors? */
Damien Miller3db5f532002-02-13 14:10:32 +1100341 if ((a = do_stat(conn, path, 1)) == NULL)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000342 return(0);
343 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
344 return(0);
345 return(a->perm & S_IFDIR);
346}
347
Ben Lindstrombba81212001-06-25 05:01:22 +0000348static int
Damien Miller3db5f532002-02-13 14:10:32 +1100349process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000350{
351 char *abs_src = NULL;
352 char *abs_dst = NULL;
353 char *tmp;
354 glob_t g;
355 int err = 0;
356 int i;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000357
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000358 abs_src = xstrdup(src);
359 abs_src = make_absolute(abs_src, pwd);
360
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000361 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000362 debug3("Looking up %s", abs_src);
Damien Miller3db5f532002-02-13 14:10:32 +1100363 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000364 error("File \"%s\" not found.", abs_src);
365 err = -1;
366 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100367 }
368
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000369 /* Only one match, dst may be file, directory or unspecified */
370 if (g.gl_pathv[0] && g.gl_matchc == 1) {
371 if (dst) {
372 /* If directory specified, append filename */
373 if (is_dir(dst)) {
374 if (infer_path(g.gl_pathv[0], &tmp)) {
375 err = 1;
376 goto out;
377 }
378 abs_dst = path_append(dst, tmp);
379 xfree(tmp);
380 } else
381 abs_dst = xstrdup(dst);
382 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
383 err = -1;
384 goto out;
385 }
386 printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100387 err = do_download(conn, g.gl_pathv[0], abs_dst, pflag);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000388 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100389 }
390
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000391 /* Multiple matches, dst may be directory or unspecified */
392 if (dst && !is_dir(dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000393 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000394 dst);
395 err = -1;
396 goto out;
397 }
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000398
Damien Miller9f0f5c62001-12-21 14:45:46 +1100399 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000400 if (infer_path(g.gl_pathv[i], &tmp)) {
401 err = -1;
402 goto out;
403 }
404 if (dst) {
405 abs_dst = path_append(dst, tmp);
406 xfree(tmp);
407 } else
408 abs_dst = tmp;
409
410 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100411 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000412 err = -1;
413 xfree(abs_dst);
414 abs_dst = NULL;
415 }
416
417out:
418 xfree(abs_src);
419 if (abs_dst)
420 xfree(abs_dst);
421 globfree(&g);
422 return(err);
423}
424
Ben Lindstrombba81212001-06-25 05:01:22 +0000425static int
Damien Miller3db5f532002-02-13 14:10:32 +1100426process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000427{
428 char *tmp_dst = NULL;
429 char *abs_dst = NULL;
430 char *tmp;
431 glob_t g;
432 int err = 0;
433 int i;
434
435 if (dst) {
436 tmp_dst = xstrdup(dst);
437 tmp_dst = make_absolute(tmp_dst, pwd);
438 }
439
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000440 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000441 debug3("Looking up %s", src);
442 if (glob(src, 0, NULL, &g)) {
443 error("File \"%s\" not found.", src);
444 err = -1;
445 goto out;
446 }
447
448 /* Only one match, dst may be file, directory or unspecified */
449 if (g.gl_pathv[0] && g.gl_matchc == 1) {
450 if (tmp_dst) {
451 /* If directory specified, append filename */
Damien Miller3db5f532002-02-13 14:10:32 +1100452 if (remote_is_dir(conn, tmp_dst)) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000453 if (infer_path(g.gl_pathv[0], &tmp)) {
454 err = 1;
455 goto out;
456 }
457 abs_dst = path_append(tmp_dst, tmp);
458 xfree(tmp);
459 } else
460 abs_dst = xstrdup(tmp_dst);
Ben Lindstrom7527f8b2001-03-24 00:39:12 +0000461 } else {
462 if (infer_path(g.gl_pathv[0], &abs_dst)) {
463 err = -1;
464 goto out;
465 }
466 abs_dst = make_absolute(abs_dst, pwd);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000467 }
468 printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100469 err = do_upload(conn, g.gl_pathv[0], abs_dst, pflag);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000470 goto out;
471 }
472
473 /* Multiple matches, dst may be directory or unspecified */
Damien Miller3db5f532002-02-13 14:10:32 +1100474 if (tmp_dst && !remote_is_dir(conn, tmp_dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000475 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000476 tmp_dst);
477 err = -1;
478 goto out;
479 }
480
Damien Miller9f0f5c62001-12-21 14:45:46 +1100481 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000482 if (infer_path(g.gl_pathv[i], &tmp)) {
483 err = -1;
484 goto out;
485 }
486 if (tmp_dst) {
487 abs_dst = path_append(tmp_dst, tmp);
488 xfree(tmp);
489 } else
490 abs_dst = make_absolute(tmp, pwd);
491
492 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100493 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000494 err = -1;
495 }
496
497out:
498 if (abs_dst)
499 xfree(abs_dst);
500 if (tmp_dst)
501 xfree(tmp_dst);
502 return(err);
Damien Miller33804262001-02-04 23:20:18 +1100503}
504
Ben Lindstrombba81212001-06-25 05:01:22 +0000505static int
Damien Miller33804262001-02-04 23:20:18 +1100506parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
507 char **path1, char **path2)
508{
509 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000510 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000511 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000512 long l;
Damien Miller33804262001-02-04 23:20:18 +1100513 int i, cmdnum;
514
515 /* Skip leading whitespace */
516 cp = cp + strspn(cp, WHITESPACE);
517
518 /* Ignore blank lines */
519 if (!*cp)
520 return(-1);
521
522 /* Figure out which command we have */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100523 for (i = 0; cmds[i].c; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100524 int cmdlen = strlen(cmds[i].c);
525
526 /* Check for command followed by whitespace */
527 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
528 strchr(WHITESPACE, cp[cmdlen])) {
529 cp += cmdlen;
530 cp = cp + strspn(cp, WHITESPACE);
531 break;
532 }
533 }
534 cmdnum = cmds[i].n;
535 cmd = cmds[i].c;
536
537 /* Special case */
538 if (*cp == '!') {
539 cp++;
540 cmdnum = I_SHELL;
541 } else if (cmdnum == -1) {
542 error("Invalid command.");
543 return(-1);
544 }
545
546 /* Get arguments and parse flags */
547 *pflag = *n_arg = 0;
548 *path1 = *path2 = NULL;
549 switch (cmdnum) {
550 case I_GET:
551 case I_PUT:
552 if (parse_getput_flags(&cp, pflag))
553 return(-1);
554 /* Get first pathname (mandatory) */
555 if (get_pathname(&cp, path1))
556 return(-1);
557 if (*path1 == NULL) {
558 error("You must specify at least one path after a "
559 "%s command.", cmd);
560 return(-1);
561 }
562 /* Try to get second pathname (optional) */
563 if (get_pathname(&cp, path2))
564 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100565 break;
566 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100567 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100568 if (get_pathname(&cp, path1))
569 return(-1);
570 if (get_pathname(&cp, path2))
571 return(-1);
572 if (!*path1 || !*path2) {
573 error("You must specify two paths after a %s "
574 "command.", cmd);
575 return(-1);
576 }
577 break;
578 case I_RM:
579 case I_MKDIR:
580 case I_RMDIR:
581 case I_CHDIR:
582 case I_LCHDIR:
583 case I_LMKDIR:
584 /* Get pathname (mandatory) */
585 if (get_pathname(&cp, path1))
586 return(-1);
587 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000588 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100589 cmd);
590 return(-1);
591 }
592 break;
593 case I_LS:
594 /* Path is optional */
595 if (get_pathname(&cp, path1))
596 return(-1);
597 break;
598 case I_LLS:
599 case I_SHELL:
600 /* Uses the rest of the line */
601 break;
602 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000603 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100604 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000605 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100606 case I_CHOWN:
607 case I_CHGRP:
608 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000609 l = strtol(cp, &cp2, base);
610 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
611 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100612 error("You must supply a numeric argument "
613 "to the %s command.", cmd);
614 return(-1);
615 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000616 cp = cp2;
617 *n_arg = l;
618 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
619 break;
620 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100621 error("You must supply a numeric argument "
622 "to the %s command.", cmd);
623 return(-1);
624 }
625 cp += strspn(cp, WHITESPACE);
626
627 /* Get pathname (mandatory) */
628 if (get_pathname(&cp, path1))
629 return(-1);
630 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000631 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100632 cmd);
633 return(-1);
634 }
635 break;
636 case I_QUIT:
637 case I_PWD:
638 case I_LPWD:
639 case I_HELP:
Ben Lindstromf78682d2001-03-14 21:26:27 +0000640 case I_VERSION:
Damien Miller33804262001-02-04 23:20:18 +1100641 break;
642 default:
643 fatal("Command not implemented");
644 }
645
646 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100647 return(cmdnum);
648}
649
Ben Lindstrombba81212001-06-25 05:01:22 +0000650static int
Damien Miller3db5f532002-02-13 14:10:32 +1100651parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd)
Damien Miller33804262001-02-04 23:20:18 +1100652{
Damien Millerd7686fd2001-02-10 00:40:03 +1100653 char *path1, *path2, *tmp;
Damien Miller4870afd2001-03-14 10:27:09 +1100654 int pflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100655 unsigned long n_arg;
656 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000657 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000658 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100659 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100660
661 path1 = path2 = NULL;
662 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
663
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000664 memset(&g, 0, sizeof(g));
665
Damien Miller33804262001-02-04 23:20:18 +1100666 /* Perform command */
667 switch (cmdnum) {
668 case -1:
669 break;
670 case I_GET:
Damien Miller3db5f532002-02-13 14:10:32 +1100671 err = process_get(conn, path1, path2, *pwd, pflag);
Damien Miller33804262001-02-04 23:20:18 +1100672 break;
673 case I_PUT:
Damien Miller3db5f532002-02-13 14:10:32 +1100674 err = process_put(conn, path1, path2, *pwd, pflag);
Ben Lindstroma3700052001-04-05 23:26:32 +0000675 break;
676 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100677 path1 = make_absolute(path1, *pwd);
678 path2 = make_absolute(path2, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100679 err = do_rename(conn, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100680 break;
Damien Miller058316f2001-03-08 10:08:49 +1100681 case I_SYMLINK:
Damien Miller3db5f532002-02-13 14:10:32 +1100682 path2 = make_absolute(path2, *pwd);
683 err = do_symlink(conn, path1, path2);
Damien Miller058316f2001-03-08 10:08:49 +1100684 break;
Damien Miller33804262001-02-04 23:20:18 +1100685 case I_RM:
686 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100687 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100688 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100689 printf("Removing %s\n", g.gl_pathv[i]);
Damien Miller3db5f532002-02-13 14:10:32 +1100690 if (do_rm(conn, g.gl_pathv[i]) == -1)
Damien Miller4870afd2001-03-14 10:27:09 +1100691 err = -1;
692 }
Damien Miller33804262001-02-04 23:20:18 +1100693 break;
694 case I_MKDIR:
695 path1 = make_absolute(path1, *pwd);
696 attrib_clear(&a);
697 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
698 a.perm = 0777;
Damien Miller3db5f532002-02-13 14:10:32 +1100699 err = do_mkdir(conn, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100700 break;
701 case I_RMDIR:
702 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100703 err = do_rmdir(conn, path1);
Damien Miller33804262001-02-04 23:20:18 +1100704 break;
705 case I_CHDIR:
706 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100707 if ((tmp = do_realpath(conn, path1)) == NULL) {
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000708 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100709 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000710 }
Damien Miller3db5f532002-02-13 14:10:32 +1100711 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100712 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000713 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100714 break;
715 }
716 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
717 error("Can't change directory: Can't check target");
718 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000719 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100720 break;
721 }
722 if (!S_ISDIR(aa->perm)) {
723 error("Can't change directory: \"%s\" is not "
724 "a directory", tmp);
725 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000726 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100727 break;
728 }
Damien Miller33804262001-02-04 23:20:18 +1100729 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100730 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100731 break;
732 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100733 if (!path1) {
Damien Miller3db5f532002-02-13 14:10:32 +1100734 do_ls(conn, *pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100735 break;
736 }
Damien Miller33804262001-02-04 23:20:18 +1100737 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100738 if ((tmp = do_realpath(conn, path1)) == NULL)
Damien Millerd7686fd2001-02-10 00:40:03 +1100739 break;
740 xfree(path1);
741 path1 = tmp;
Damien Miller3db5f532002-02-13 14:10:32 +1100742 if ((aa = do_stat(conn, path1, 0)) == NULL)
Damien Millerd7686fd2001-02-10 00:40:03 +1100743 break;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000744 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Millerd7686fd2001-02-10 00:40:03 +1100745 !S_ISDIR(aa->perm)) {
746 error("Can't ls: \"%s\" is not a directory", path1);
747 break;
748 }
Damien Miller3db5f532002-02-13 14:10:32 +1100749 do_ls(conn, path1);
Damien Miller33804262001-02-04 23:20:18 +1100750 break;
751 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000752 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +1100753 error("Couldn't change local directory to "
754 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000755 err = 1;
756 }
Damien Miller33804262001-02-04 23:20:18 +1100757 break;
758 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000759 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100760 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +1100761 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000762 err = 1;
763 }
Damien Miller33804262001-02-04 23:20:18 +1100764 break;
765 case I_LLS:
766 local_do_ls(cmd);
767 break;
768 case I_SHELL:
769 local_do_shell(cmd);
770 break;
771 case I_LUMASK:
772 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +0000773 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +1100774 break;
775 case I_CHMOD:
776 path1 = make_absolute(path1, *pwd);
777 attrib_clear(&a);
778 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
779 a.perm = n_arg;
Damien Miller3db5f532002-02-13 14:10:32 +1100780 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100781 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100782 printf("Changing mode on %s\n", g.gl_pathv[i]);
Damien Miller3db5f532002-02-13 14:10:32 +1100783 do_setstat(conn, g.gl_pathv[i], &a);
Damien Miller4870afd2001-03-14 10:27:09 +1100784 }
Kevin Steves62c45db2001-02-05 13:42:43 +0000785 break;
Damien Miller33804262001-02-04 23:20:18 +1100786 case I_CHOWN:
787 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100788 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100789 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller3db5f532002-02-13 14:10:32 +1100790 if (!(aa = do_stat(conn, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100791 continue;
792 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
793 error("Can't get current ownership of "
794 "remote file \"%s\"", g.gl_pathv[i]);
795 continue;
796 }
797 printf("Changing owner on %s\n", g.gl_pathv[i]);
798 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
799 aa->uid = n_arg;
Damien Miller3db5f532002-02-13 14:10:32 +1100800 do_setstat(conn, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100801 }
Damien Miller33804262001-02-04 23:20:18 +1100802 break;
803 case I_CHGRP:
804 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100805 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100806 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller3db5f532002-02-13 14:10:32 +1100807 if (!(aa = do_stat(conn, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100808 continue;
809 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
810 error("Can't get current ownership of "
811 "remote file \"%s\"", g.gl_pathv[i]);
812 continue;
813 }
814 printf("Changing group on %s\n", g.gl_pathv[i]);
815 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
816 aa->gid = n_arg;
Damien Miller3db5f532002-02-13 14:10:32 +1100817 do_setstat(conn, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100818 }
Damien Miller33804262001-02-04 23:20:18 +1100819 break;
820 case I_PWD:
821 printf("Remote working directory: %s\n", *pwd);
822 break;
823 case I_LPWD:
824 if (!getcwd(path_buf, sizeof(path_buf)))
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000825 error("Couldn't get local cwd: %s",
Damien Miller33804262001-02-04 23:20:18 +1100826 strerror(errno));
827 else
828 printf("Local working directory: %s\n",
829 path_buf);
830 break;
831 case I_QUIT:
832 return(-1);
833 case I_HELP:
834 help();
835 break;
Ben Lindstromf78682d2001-03-14 21:26:27 +0000836 case I_VERSION:
Damien Miller3db5f532002-02-13 14:10:32 +1100837 printf("SFTP protocol version %d\n", sftp_proto_version(conn));
Ben Lindstromf78682d2001-03-14 21:26:27 +0000838 break;
Damien Miller33804262001-02-04 23:20:18 +1100839 default:
840 fatal("%d is not implemented", cmdnum);
841 }
842
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000843 if (g.gl_pathc)
844 globfree(&g);
Damien Miller33804262001-02-04 23:20:18 +1100845 if (path1)
846 xfree(path1);
847 if (path2)
848 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000849
850 /* If an error occurs in batch mode we should abort. */
851 if (infile != stdin && err > 0)
852 return -1;
853
Damien Miller33804262001-02-04 23:20:18 +1100854 return(0);
855}
856
857void
Ben Lindstrom63667f62001-04-13 00:00:14 +0000858interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
Damien Miller33804262001-02-04 23:20:18 +1100859{
860 char *pwd;
Ben Lindstrom63667f62001-04-13 00:00:14 +0000861 char *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100862 char cmd[2048];
Damien Miller3db5f532002-02-13 14:10:32 +1100863 struct sftp_conn *conn;
Damien Miller33804262001-02-04 23:20:18 +1100864
Damien Miller3db5f532002-02-13 14:10:32 +1100865 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
866 if (conn == NULL)
Damien Miller058316f2001-03-08 10:08:49 +1100867 fatal("Couldn't initialise connection to server");
868
Damien Miller3db5f532002-02-13 14:10:32 +1100869 pwd = do_realpath(conn, ".");
Damien Miller33804262001-02-04 23:20:18 +1100870 if (pwd == NULL)
871 fatal("Need cwd");
872
Ben Lindstrom63667f62001-04-13 00:00:14 +0000873 if (file1 != NULL) {
874 dir = xstrdup(file1);
875 dir = make_absolute(dir, pwd);
876
Damien Miller3db5f532002-02-13 14:10:32 +1100877 if (remote_is_dir(conn, dir) && file2 == NULL) {
Ben Lindstrom63667f62001-04-13 00:00:14 +0000878 printf("Changing to: %s\n", dir);
879 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
Damien Miller3db5f532002-02-13 14:10:32 +1100880 parse_dispatch_command(conn, cmd, &pwd);
Ben Lindstrom63667f62001-04-13 00:00:14 +0000881 } else {
882 if (file2 == NULL)
883 snprintf(cmd, sizeof cmd, "get %s", dir);
884 else
885 snprintf(cmd, sizeof cmd, "get %s %s", dir,
886 file2);
887
Damien Miller3db5f532002-02-13 14:10:32 +1100888 parse_dispatch_command(conn, cmd, &pwd);
Ben Lindstromcb1f60e2002-03-22 02:47:28 +0000889 xfree(dir);
Ben Lindstrom63667f62001-04-13 00:00:14 +0000890 return;
891 }
Ben Lindstromcb1f60e2002-03-22 02:47:28 +0000892 xfree(dir);
Ben Lindstrom63667f62001-04-13 00:00:14 +0000893 }
Ben Lindstrom6aebb342001-05-09 00:38:19 +0000894#if HAVE_SETVBUF
Damien Millerd7686fd2001-02-10 00:40:03 +1100895 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000896 setvbuf(infile, NULL, _IOLBF, 0);
Ben Lindstrom6aebb342001-05-09 00:38:19 +0000897#else
898 setlinebuf(stdout);
899 setlinebuf(infile);
900#endif
Damien Miller33804262001-02-04 23:20:18 +1100901
Damien Miller9f0f5c62001-12-21 14:45:46 +1100902 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +1100903 char *cp;
904
905 printf("sftp> ");
906
907 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000908 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100909 printf("\n");
910 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000911 } else if (infile != stdin) /* Bluff typing */
912 printf("%s", cmd);
913
Damien Miller33804262001-02-04 23:20:18 +1100914 cp = strrchr(cmd, '\n');
915 if (cp)
916 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000917
Damien Miller3db5f532002-02-13 14:10:32 +1100918 if (parse_dispatch_command(conn, cmd, &pwd))
Damien Miller33804262001-02-04 23:20:18 +1100919 break;
920 }
921 xfree(pwd);
922}