home *** CD-ROM | disk | FTP | other *** search
- /*
- * Open a window to display the choices of file transfer protocols and
- * prompt for the file name(s). A return code of 1 means turn the
- * input routine back on.
- */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <curses.h>
- #include "config.h"
- #include "misc.h"
- #include "xmodem.h"
-
- int
- xfer_menu(up)
- int up;
- {
- extern int fd;
- extern char *null_ptr;
- WINDOW *xm_win, *newwin();
- char *list, *get_names(), *get_extrnl();
- int type, is_batch;
- void xfer_win(), xfer_ascii(), free_ptr(), extrnl(), error_win();
-
- xm_win = newwin(15, 20, 2, 45);
-
- mvwaddstr(xm_win, 2, 3, "1) xmodem");
- mvwaddstr(xm_win, 3, 3, "2) xmodem-1k");
- mvwaddstr(xm_win, 4, 3, "3) modem7");
- mvwaddstr(xm_win, 5, 3, "4) ymodem");
- mvwaddstr(xm_win, 6, 3, "5) ymodem-g");
- mvwaddstr(xm_win, 7, 3, "6) ASCII");
- mvwaddstr(xm_win, 8, 3, "7) (external)");
- mvwaddstr(xm_win, 11, 3, "<ESC> to Abort");
- mvwaddstr(xm_win, 13, 3, "Protocol:");
- box(xm_win, VERT, HORZ);
- if (up)
- mvwattrstr(xm_win, 0, 6, A_BOLD, " Upload ");
- else
- mvwattrstr(xm_win, 0, 5, A_BOLD, " Download ");
-
- wmove(xm_win, 13, 13);
- wrefresh(xm_win);
- /* get the protocol */
- while ((type = get_num(xm_win, 1)) != -1) {
- if (type >= 1 && type <= PROTOCOLS)
- break;
- beep();
- mvwaddch(xm_win, 13, 13, (chtype) ' ');
- wmove(xm_win, 13, 13);
- wrefresh(xm_win);
- }
- type--;
- werase(xm_win);
- wrefresh(xm_win);
- delwin(xm_win);
- /* chickened out */
- if (type < 0)
- return(0);
-
- if (fd == -1) {
- error_win(0, "Not currently connected to any host", "");
- return(0);
- }
- /* is the external protocol? */
- if (type == EXTRNL) {
- /* get the command line */
- if (!(list = get_extrnl(up)))
- return(0);
- extrnl(list);
- return(1);
- }
- /* is a batch protocol? */
- is_batch = 0;
- switch (type) {
- case MODEM7:
- case YMODEM:
- case YMODEM_G:
- is_batch++;
- break;
- default:
- break;
- }
-
- /*
- * When receiving files in one of the batch modes, there is no
- * need to prompt for a list of file names.
- */
- list = null_ptr;
- if (up || !is_batch) {
- if (!(list = get_names(up, type, is_batch)))
- return(0);
- }
- /* if ascii transfer */
- if (type == XASCII) {
- xfer_ascii(list, up);
- free_ptr(list);
- if (up)
- return(0);
- return(1);
- }
- xfer_win(list, up, type);
- free_ptr(list);
- return(1);
- }
-
- char *protocol[PROTOCOLS] = {"xmodem", "xmodem-1k", "modem7", "ymodem",
- "ymodem-g", "ASCII", "(external)"};
-
- /*
- * Prompt for a list of files for the transfer programs. A NULL return
- * code means you chickened out.
- */
-
- static char *
- get_names(up, type, is_batch)
- int up, type, is_batch;
- {
- int can;
- WINDOW *gn_win, *newwin();
- char *list, *ans, *file, buf[40], *expand(), *get_str(), *strtok();
- void st_line();
- struct stat stbuf;
-
- touchwin(stdscr);
- refresh();
- st_line("");
-
- gn_win = newwin(7, 70, 5, 5);
- mvwaddstr(gn_win, 3, 4, "Enter filename: ");
- box(gn_win, VERT, HORZ);
- if (up)
- sprintf(buf, " Send %s ", protocol[type]);
- else
- sprintf(buf, " Receive %s ", protocol[type]);
- mvwattrstr(gn_win, 0, 3, A_BOLD, buf);
-
- while (1) {
- wmove(gn_win, 3, 20);
- wrefresh(gn_win);
- /* get the answers */
- if (is_batch)
- ans = get_str(gn_win, 60, "", "");
- else
- ans = get_str(gn_win, 60, "", " ");
-
- if (ans == NULL || *ans == NULL) {
- list = NULL;
- break;
- }
- list = expand(ans);
-
- if (is_batch)
- break;
- /*
- * Here we have the opportunity to determine the read and
- * write permissions before things get started. Much nicer
- * than finding out later when there's no way to fix it.
- */
- file = strtok(list, " ");
- /* sanity checking */
- if (!stat(file, &stbuf)) {
- if ((stbuf.st_mode & S_IFREG) != S_IFREG) {
- beep();
- clear_line(gn_win, 4, 15, 1);
- mvwattrstr(gn_win, 4, 15, A_BOLD, "Not a regular file");
- wrefresh(gn_win);
- wait_key(gn_win, 3);
- clear_line(gn_win, 4, 15, 1);
- clear_line(gn_win, 3, 20, 1);
- continue;
- }
- }
- /* check read permission */
- if (up) {
- if (access(file, 0)) {
- beep();
- mvwattrstr(gn_win, 4, 15, A_BOLD, "Can't find file");
- wrefresh(gn_win);
- wait_key(gn_win, 3);
- clear_line(gn_win, 4, 15, 1);
- clear_line(gn_win, 3, 20, 1);
- continue;
- }
- if (access(file, 4)) {
- beep();
- mvwattrstr(gn_win, 4, 15, A_BOLD, "No read permission");
- wrefresh(gn_win);
- wait_key(gn_win, 3);
- clear_line(gn_win, 4, 15, 1);
- clear_line(gn_win, 3, 20, 1);
- continue;
- }
- break;
- }
- /* check write permission */
- if (!(can = can_write(file))) {
- beep();
- clear_line(gn_win, 4, 15, 1);
- mvwattrstr(gn_win, 4, 15, A_BOLD, "No write permission");
- wrefresh(gn_win);
- wait_key(gn_win, 3);
- clear_line(gn_win, 4, 15, 1);
- clear_line(gn_win, 3, 20, 1);
- continue;
- }
- if (can == 2) {
- if (!yes_prompt(gn_win, 4, 15, A_BOLD, "File exists, overwrite")) {
- clear_line(gn_win, 4, 15, 1);
- clear_line(gn_win, 3, 20, 1);
- continue;
- }
- break;
- }
- break;
- }
- werase(gn_win);
- wrefresh(gn_win);
- delwin(gn_win);
-
- return(list);
- }
-
- /*
- * Prompt for the Unix command line to be used as an external file
- * transfer program. A return code of NULL means forget it.
- */
-
- static char *
- get_extrnl(up)
- int up;
- {
- WINDOW *ge_win, *newwin();
- char *cmd, *ans, *get_str(), *expand();
- void st_line();
-
- touchwin(stdscr);
- refresh();
- st_line("");
- /* prompt for command line */
- ge_win = newwin(7, 70, 5, 5);
- mvwaddstr(ge_win, 3, 4, "Enter Unix command: ");
- box(ge_win, VERT, HORZ);
- if (up)
- mvwattrstr(ge_win, 0, 3, A_BOLD, " Send (external) ");
- else
- mvwattrstr(ge_win, 0, 3, A_BOLD, " Receive (external) ");
- wmove(ge_win, 3, 24);
- wrefresh(ge_win);
- /* get the line */
- ans = get_str(ge_win, 60, "", "");
- cmd = expand(ans);
- if (*cmd == NULL)
- cmd = NULL;
-
- werase(ge_win);
- wrefresh(ge_win);
- delwin(ge_win);
- return(cmd);
- }
-