diff options
Diffstat (limited to 'sftp.c')
-rw-r--r-- | sftp.c | 65 |
1 files changed, 56 insertions, 9 deletions
@@ -16,7 +16,13 @@ | |||
16 | 16 | ||
17 | #include "includes.h" | 17 | #include "includes.h" |
18 | 18 | ||
19 | RCSID("$OpenBSD: sftp.c,v 1.56 2004/07/11 17:48:47 deraadt Exp $"); | 19 | RCSID("$OpenBSD: sftp.c,v 1.57 2004/11/05 12:19:56 djm Exp $"); |
20 | |||
21 | #ifdef USE_LIBEDIT | ||
22 | #include <histedit.h> | ||
23 | #else | ||
24 | typedef void EditLine; | ||
25 | #endif | ||
20 | 26 | ||
21 | #include "buffer.h" | 27 | #include "buffer.h" |
22 | #include "xmalloc.h" | 28 | #include "xmalloc.h" |
@@ -1206,6 +1212,14 @@ parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, | |||
1206 | return (0); | 1212 | return (0); |
1207 | } | 1213 | } |
1208 | 1214 | ||
1215 | #ifdef USE_LIBEDIT | ||
1216 | static char * | ||
1217 | prompt(EditLine *el) | ||
1218 | { | ||
1219 | return ("sftp> "); | ||
1220 | } | ||
1221 | #endif | ||
1222 | |||
1209 | int | 1223 | int |
1210 | interactive_loop(int fd_in, int fd_out, char *file1, char *file2) | 1224 | interactive_loop(int fd_in, int fd_out, char *file1, char *file2) |
1211 | { | 1225 | { |
@@ -1214,6 +1228,27 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2) | |||
1214 | char cmd[2048]; | 1228 | char cmd[2048]; |
1215 | struct sftp_conn *conn; | 1229 | struct sftp_conn *conn; |
1216 | int err; | 1230 | int err; |
1231 | EditLine *el = NULL; | ||
1232 | #ifdef USE_LIBEDIT | ||
1233 | History *hl = NULL; | ||
1234 | HistEvent hev; | ||
1235 | extern char *__progname; | ||
1236 | |||
1237 | if (!batchmode && isatty(STDIN_FILENO)) { | ||
1238 | if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL) | ||
1239 | fatal("Couldn't initialise editline"); | ||
1240 | if ((hl = history_init()) == NULL) | ||
1241 | fatal("Couldn't initialise editline history"); | ||
1242 | history(hl, &hev, H_SETSIZE, 100); | ||
1243 | el_set(el, EL_HIST, history, hl); | ||
1244 | |||
1245 | el_set(el, EL_PROMPT, prompt); | ||
1246 | el_set(el, EL_EDITOR, "emacs"); | ||
1247 | el_set(el, EL_TERMINAL, NULL); | ||
1248 | el_set(el, EL_SIGNAL, 1); | ||
1249 | el_source(el, NULL); | ||
1250 | } | ||
1251 | #endif /* USE_LIBEDIT */ | ||
1217 | 1252 | ||
1218 | conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests); | 1253 | conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests); |
1219 | if (conn == NULL) | 1254 | if (conn == NULL) |
@@ -1261,17 +1296,29 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2) | |||
1261 | 1296 | ||
1262 | signal(SIGINT, SIG_IGN); | 1297 | signal(SIGINT, SIG_IGN); |
1263 | 1298 | ||
1264 | printf("sftp> "); | 1299 | if (el == NULL) { |
1300 | printf("sftp> "); | ||
1301 | if (fgets(cmd, sizeof(cmd), infile) == NULL) { | ||
1302 | printf("\n"); | ||
1303 | break; | ||
1304 | } | ||
1305 | if (batchmode) /* Echo command */ | ||
1306 | printf("%s", cmd); | ||
1307 | } else { | ||
1308 | #ifdef USE_LIBEDIT | ||
1309 | const char *line; | ||
1310 | int count = 0; | ||
1265 | 1311 | ||
1266 | /* XXX: use libedit */ | 1312 | if ((line = el_gets(el, &count)) == NULL || count <= 0) |
1267 | if (fgets(cmd, sizeof(cmd), infile) == NULL) { | 1313 | break; |
1268 | printf("\n"); | 1314 | history(hl, &hev, H_ENTER, line); |
1269 | break; | 1315 | if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) { |
1316 | fprintf(stderr, "Error: input line too long\n"); | ||
1317 | continue; | ||
1318 | } | ||
1319 | #endif /* USE_LIBEDIT */ | ||
1270 | } | 1320 | } |
1271 | 1321 | ||
1272 | if (batchmode) /* Echo command */ | ||
1273 | printf("%s", cmd); | ||
1274 | |||
1275 | cp = strrchr(cmd, '\n'); | 1322 | cp = strrchr(cmd, '\n'); |
1276 | if (cp) | 1323 | if (cp) |
1277 | *cp = '\0'; | 1324 | *cp = '\0'; |