home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / pcomm / part06 / x_menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-18  |  3.9 KB  |  183 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 <curses.h>
  9. #include "misc.h"
  10. #include "xmodem.h"
  11.  
  12. int
  13. xfer_menu(up)
  14. int up;
  15. {
  16.     WINDOW *xm_win, *newwin();
  17.     char *list, *get_names();
  18.     int type, is_batch;
  19.     extern char *null_ptr;
  20.     void xfer_win(), xfer_ascii(), free_ptr();
  21.  
  22.     xm_win = newwin(15, 20, 2, 45);
  23.  
  24.     mvwaddstr(xm_win, 2, 3, "1) xmodem");
  25.     mvwaddstr(xm_win, 3, 3, "2) xmodem-1k");
  26.     mvwaddstr(xm_win, 4, 3, "3) modem7");
  27.     mvwaddstr(xm_win, 5, 3, "4) ymodem");
  28.     mvwaddstr(xm_win, 6, 3, "5) ymodem-g");
  29.     mvwaddstr(xm_win, 7, 3, "6) ASCII");
  30.     mvwaddstr(xm_win, 11, 3, "ESC to Abort");
  31.     mvwaddstr(xm_win, 13, 3, "Protocol:");
  32.     box(xm_win, '|', '-');
  33.     if (up)
  34.         mvwattrstr(xm_win, 0, 6, A_BOLD, " Upload ");
  35.     else
  36.         mvwattrstr(xm_win, 0, 5, A_BOLD, " Download ");
  37.  
  38.     wmove(xm_win, 13, 13);
  39.     wrefresh(xm_win);
  40.                     /* get the protocol */
  41.     while ((type = get_num(xm_win, 1)) != -1) {
  42.         if (type >=1 && type <= PROTOCOLS)
  43.             break;
  44.         beep();
  45.         mvwaddch(xm_win, 13, 13, ' ');
  46.         wmove(xm_win, 13, 13);
  47.         wrefresh(xm_win);
  48.     }
  49.                     /* is a batch protocol ? */
  50.     is_batch = 0;
  51.     switch (type-1) {
  52.         case MODEM7:
  53.         case YMODEM:
  54.         case YMODEM_G:
  55.             is_batch++;
  56.             break;
  57.         default:
  58.             break;
  59.     }
  60.     werase(xm_win);
  61.     wrefresh(xm_win);
  62.     delwin(xm_win);
  63.  
  64.     touchwin(stdscr);
  65.     refresh();
  66.  
  67.     if (type == -1)
  68.         return(0);
  69.     type--;
  70.  
  71.     /*
  72.      * When receiving files in one of the batch modes, there is
  73.      * need to prompt for a list of file names.
  74.      */
  75.     list = null_ptr;
  76.     if (up || !is_batch) {
  77.         if (!(list = get_names(up, type, is_batch)))
  78.             return(0);
  79.     }
  80.                     /* if ascii transfer */
  81.     if (type == XASCII) {
  82.         xfer_ascii(list, up);
  83.         free_ptr(list);
  84.         if (up)
  85.             return(0);
  86.         return(1);
  87.     }
  88.     xfer_win(list, up, type);
  89.     free_ptr(list);
  90.     return(1);
  91. }
  92.  
  93. /*
  94.  * Prompt for a list of files for the transfer programs.  A NULL return
  95.  * code means you chickened out.
  96.  */
  97.  
  98. char *
  99. get_names(up, type, is_batch)
  100. int up, type, is_batch;
  101. {
  102.     int can;
  103.     WINDOW *gn_win, *newwin();
  104.     char *ans, *list, *file, buf[40], *expand(), *get_str(), *strtok();
  105.     static char *direction[2] = {"Receive", "Send"};
  106.     static char *protocol[PROTOCOLS] = {"xmodem", "xmodem-1k", "modem7",
  107.     "ymodem", "ymodem-g", "ASCII"};
  108.                     /* prompt for file spec */
  109.     gn_win = newwin(7, 70, 5, 5);
  110.  
  111.     mvwaddstr(gn_win, 3, 4, "Enter filename: ");
  112.     box(gn_win, '|', '-');
  113.     sprintf(buf, " %s %s ", direction[up], protocol[type]);
  114.  
  115.     while (1) {
  116.         mvwattrstr(gn_win, 0, 3, A_BOLD, buf);
  117.         wmove(gn_win, 3, 20);
  118.         wrefresh(gn_win);
  119.                     /* get the answers */
  120.         if (is_batch)
  121.             ans = get_str(gn_win, 60, NULL, NULL);
  122.         else
  123.             ans = get_str(gn_win, 60, NULL, "     ");
  124.  
  125.         if (ans == NULL || *ans == NULL) {
  126.             list = NULL;
  127.             break;
  128.         }
  129.         list = expand(ans);
  130.                     /* batchs check "on the fly" */
  131.         if (is_batch)
  132.             break;
  133.         /*
  134.          * The non-batch protocols don't check read and write
  135.          * permission on the fly, so we check 'em here.  Since
  136.          * they aren't batch, we use only one file.
  137.          */
  138.         file = strtok(list, "     ");
  139.                     /* check read permission */
  140.         if (up) {
  141.             if (access(file, 4)) {
  142.                 beep();
  143.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "Can't find or no read permission");
  144.                 wrefresh(gn_win);
  145.                 wait_key(gn_win, 3);
  146.                 clear_line(gn_win, 4, 15, 1);
  147.                 clear_line(gn_win, 3, 20, 1);
  148.                 wrefresh(gn_win);
  149.             }
  150.             else
  151.                 break;
  152.         }
  153.                     /* check write permission */
  154.         else {
  155.             if (!(can = can_write(file))) {
  156.                 beep();
  157.                 clear_line(gn_win, 4, 15, 1);
  158.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "No write permission");
  159.                 wrefresh(gn_win);
  160.                 wait_key(gn_win, 3);
  161.                 clear_line(gn_win, 4, 15, 1);
  162.                 clear_line(gn_win, 3, 20, 1);
  163.                 wrefresh(gn_win);
  164.             }
  165.             if (can == 2) {
  166.                 if (!yes_prompt(gn_win, 4, 15, A_BOLD, "File exists, overwrite")) {
  167.                     list = NULL;
  168.                     break;
  169.                 }
  170.             }
  171.             if (can)
  172.                 break;
  173.         }
  174.     }
  175.     werase(gn_win);
  176.     wrefresh(gn_win);
  177.     delwin(gn_win);
  178.  
  179.     touchwin(stdscr);
  180.     refresh();
  181.     return(list);
  182. }
  183.