home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume16 / pcomm2 / part07 / x_menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-14  |  5.7 KB  |  262 lines

  1. /*
  2.  * Open a window to display the choices of file transfer protocols and
  3.  * prompt for the file name(s).  A return code of 1 means turn the
  4.  * input routine back on.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <curses.h>
  11. #include "config.h"
  12. #include "misc.h"
  13. #include "xmodem.h"
  14.  
  15. int
  16. xfer_menu(up)
  17. int up;
  18. {
  19.     extern int fd;
  20.     extern char *null_ptr;
  21.     WINDOW *xm_win, *newwin();
  22.     char *list, *get_names(), *get_extrnl();
  23.     int type, is_batch;
  24.     void xfer_win(), xfer_ascii(), free_ptr(), extrnl(), error_win();
  25.  
  26.     xm_win = newwin(15, 20, 2, 45);
  27.  
  28.     mvwaddstr(xm_win, 2, 3, "1) xmodem");
  29.     mvwaddstr(xm_win, 3, 3, "2) xmodem-1k");
  30.     mvwaddstr(xm_win, 4, 3, "3) modem7");
  31.     mvwaddstr(xm_win, 5, 3, "4) ymodem");
  32.     mvwaddstr(xm_win, 6, 3, "5) ymodem-g");
  33.     mvwaddstr(xm_win, 7, 3, "6) ASCII");
  34.     mvwaddstr(xm_win, 8, 3, "7) (external)");
  35.     mvwaddstr(xm_win, 11, 3, "<ESC> to Abort");
  36.     mvwaddstr(xm_win, 13, 3, "Protocol:");
  37.     box(xm_win, VERT, HORZ);
  38.     if (up)
  39.         mvwattrstr(xm_win, 0, 6, A_BOLD, " Upload ");
  40.     else
  41.         mvwattrstr(xm_win, 0, 5, A_BOLD, " Download ");
  42.  
  43.     wmove(xm_win, 13, 13);
  44.     wrefresh(xm_win);
  45.                     /* get the protocol */
  46.     while ((type = get_num(xm_win, 1)) != -1) {
  47.         if (type >= 1 && type <= PROTOCOLS)
  48.             break;
  49.         beep();
  50.         mvwaddch(xm_win, 13, 13, (chtype) ' ');
  51.         wmove(xm_win, 13, 13);
  52.         wrefresh(xm_win);
  53.     }
  54.     type--;
  55.     werase(xm_win);
  56.     wrefresh(xm_win);
  57.     delwin(xm_win);
  58.                     /* chickened out */
  59.     if (type < 0)
  60.         return(0);
  61.  
  62.     if (fd == -1) {
  63.         error_win(0, "Not currently connected to any host", "");
  64.         return(0);
  65.     }
  66.                     /* is the external protocol? */
  67.     if (type == EXTRNL) {
  68.                     /* get the command line */
  69.         if (!(list = get_extrnl(up)))
  70.             return(0);
  71.         extrnl(list);
  72.         return(1);
  73.     }
  74.                     /* is a batch protocol? */
  75.     is_batch = 0;
  76.     switch (type) {
  77.         case MODEM7:
  78.         case YMODEM:
  79.         case YMODEM_G:
  80.             is_batch++;
  81.             break;
  82.         default:
  83.             break;
  84.     }
  85.  
  86.     /*
  87.      * When receiving files in one of the batch modes, there is no
  88.      * need to prompt for a list of file names.
  89.      */
  90.     list = null_ptr;
  91.     if (up || !is_batch) {
  92.         if (!(list = get_names(up, type, is_batch)))
  93.             return(0);
  94.     }
  95.                     /* if ascii transfer */
  96.     if (type == XASCII) {
  97.         xfer_ascii(list, up);
  98.         free_ptr(list);
  99.         if (up)
  100.             return(0);
  101.         return(1);
  102.     }
  103.     xfer_win(list, up, type);
  104.     free_ptr(list);
  105.     return(1);
  106. }
  107.  
  108. char *protocol[PROTOCOLS] = {"xmodem", "xmodem-1k", "modem7", "ymodem",
  109.     "ymodem-g", "ASCII", "(external)"};
  110.  
  111. /*
  112.  * Prompt for a list of files for the transfer programs.  A NULL return
  113.  * code means you chickened out.
  114.  */
  115.  
  116. static char *
  117. get_names(up, type, is_batch)
  118. int up, type, is_batch;
  119. {
  120.     int can;
  121.     WINDOW *gn_win, *newwin();
  122.     char *list, *ans, *file, buf[40], *expand(), *get_str(), *strtok();
  123.     void st_line();
  124.     struct stat stbuf;
  125.  
  126.     touchwin(stdscr);
  127.     refresh();
  128.     st_line("");
  129.  
  130.     gn_win = newwin(7, 70, 5, 5);
  131.     mvwaddstr(gn_win, 3, 4, "Enter filename: ");
  132.     box(gn_win, VERT, HORZ);
  133.     if (up)
  134.         sprintf(buf, " Send %s ", protocol[type]);
  135.     else
  136.         sprintf(buf, " Receive %s ", protocol[type]);
  137.     mvwattrstr(gn_win, 0, 3, A_BOLD, buf);
  138.  
  139.     while (1) {
  140.         wmove(gn_win, 3, 20);
  141.         wrefresh(gn_win);
  142.                     /* get the answers */
  143.         if (is_batch)
  144.             ans = get_str(gn_win, 60, "", "");
  145.         else
  146.             ans = get_str(gn_win, 60, "", "     ");
  147.  
  148.         if (ans == NULL || *ans == NULL) {
  149.             list = NULL;
  150.             break;
  151.         }
  152.         list = expand(ans);
  153.  
  154.         if (is_batch)
  155.             break;
  156.         /*
  157.          * Here we have the opportunity to determine the read and
  158.          * write permissions before things get started.  Much nicer
  159.          * than finding out later when there's no way to fix it.
  160.          */
  161.         file = strtok(list, "     ");
  162.                     /* sanity checking */
  163.         if (!stat(file, &stbuf)) {
  164.             if ((stbuf.st_mode & S_IFREG) != S_IFREG) {
  165.                 beep();
  166.                 clear_line(gn_win, 4, 15, 1);
  167.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "Not a regular file");
  168.                 wrefresh(gn_win);
  169.                 wait_key(gn_win, 3);
  170.                 clear_line(gn_win, 4, 15, 1);
  171.                 clear_line(gn_win, 3, 20, 1);
  172.                 continue;
  173.             }
  174.         }
  175.                     /* check read permission */
  176.         if (up) {
  177.             if (access(file, 0)) {
  178.                 beep();
  179.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "Can't find file");
  180.                 wrefresh(gn_win);
  181.                 wait_key(gn_win, 3);
  182.                 clear_line(gn_win, 4, 15, 1);
  183.                 clear_line(gn_win, 3, 20, 1);
  184.                 continue;
  185.             }
  186.             if (access(file, 4)) {
  187.                 beep();
  188.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "No read permission");
  189.                 wrefresh(gn_win);
  190.                 wait_key(gn_win, 3);
  191.                 clear_line(gn_win, 4, 15, 1);
  192.                 clear_line(gn_win, 3, 20, 1);
  193.                 continue;
  194.             }
  195.             break;
  196.         }
  197.                     /* check write permission */
  198.         if (!(can = can_write(file))) {
  199.             beep();
  200.             clear_line(gn_win, 4, 15, 1);
  201.             mvwattrstr(gn_win, 4, 15, A_BOLD, "No write permission");
  202.             wrefresh(gn_win);
  203.             wait_key(gn_win, 3);
  204.             clear_line(gn_win, 4, 15, 1);
  205.             clear_line(gn_win, 3, 20, 1);
  206.             continue;
  207.         }
  208.         if (can == 2) {
  209.             if (!yes_prompt(gn_win, 4, 15, A_BOLD, "File exists, overwrite")) {
  210.                 clear_line(gn_win, 4, 15, 1);
  211.                 clear_line(gn_win, 3, 20, 1);
  212.                 continue;
  213.             }
  214.             break;
  215.         }
  216.         break;
  217.     }
  218.     werase(gn_win);
  219.     wrefresh(gn_win);
  220.     delwin(gn_win);
  221.  
  222.     return(list);
  223. }
  224.  
  225. /*
  226.  * Prompt for the Unix command line to be used as an external file
  227.  * transfer program.  A return code of NULL means forget it.
  228.  */
  229.  
  230. static char *
  231. get_extrnl(up)
  232. int up;
  233. {
  234.     WINDOW *ge_win, *newwin();
  235.     char *cmd, *ans, *get_str(), *expand();
  236.     void st_line();
  237.  
  238.     touchwin(stdscr);
  239.     refresh();
  240.     st_line("");
  241.                     /* prompt for command line */
  242.     ge_win = newwin(7, 70, 5, 5);
  243.     mvwaddstr(ge_win, 3, 4, "Enter Unix command: ");
  244.     box(ge_win, VERT, HORZ);
  245.     if (up)
  246.         mvwattrstr(ge_win, 0, 3, A_BOLD, " Send (external) ");
  247.     else
  248.         mvwattrstr(ge_win, 0, 3, A_BOLD, " Receive (external) ");
  249.     wmove(ge_win, 3, 24);
  250.     wrefresh(ge_win);
  251.                     /* get the line */
  252.     ans = get_str(ge_win, 60, "", "");
  253.     cmd = expand(ans);
  254.     if (*cmd == NULL)
  255.         cmd = NULL;
  256.  
  257.     werase(ge_win);
  258.     wrefresh(ge_win);
  259.     delwin(ge_win);
  260.     return(cmd);
  261. }
  262.