home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / pcomm-2.0.2 / part03 / x_menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-13  |  7.2 KB  |  336 lines

  1. /*
  2.  * Open a window to display the choices of file transfer protocols and
  3.  * prompt for the file name(s).
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <curses.h>
  10. #include "extrnl.h"
  11. #include "misc.h"
  12. #include "xmodem.h"
  13.  
  14. static char *get_names(), *get_extrnl();
  15.  
  16. void
  17. xfer_menu(up)
  18. int up;
  19. {
  20.     extern int fd;
  21.     extern char *null_ptr;
  22.     WINDOW *xm_win, *newwin();
  23.     char buf[2048], *list, *strcat(), *strcpy();
  24.     int type, is_batch, i, ans, num_extrnl, n;
  25.     void xfer_win(), xfer_ascii(), do_extrnl(), error_win();
  26.  
  27.     num_extrnl = (up) ? extrnl->up_entries : extrnl->dn_entries;
  28.     xm_win = newwin(14+num_extrnl, 20, 2, 45);
  29.  
  30.     mvwaddstr(xm_win, 2, 3, "1) xmodem");
  31.     mvwaddstr(xm_win, 3, 3, "2) xmodem-1k");
  32.     mvwaddstr(xm_win, 4, 3, "3) modem7");
  33.     mvwaddstr(xm_win, 5, 3, "4) ymodem");
  34.     mvwaddstr(xm_win, 6, 3, "5) ymodem-g");
  35.     mvwaddstr(xm_win, 7, 3, "6) ASCII");
  36.  
  37.     for (i=0; i<num_extrnl; i++)
  38.         mvwprintw(xm_win, i+8, 3, "%d) %-12.12s", i+7, extrnl->name[up][i]);
  39.     mvwaddstr(xm_win, i+8, 3, "E) (external)");
  40.     mvwaddstr(xm_win, i+10, 3, "<ESC> to Abort");
  41.     mvwaddstr(xm_win, i+11, 3, "Protocol:");
  42.     box(xm_win, VERT, HORZ);
  43.     if (up)
  44.         mvwattrstr(xm_win, 0, 6, A_BOLD, " Upload ");
  45.     else
  46.         mvwattrstr(xm_win, 0, 5, A_BOLD, " Download ");
  47.  
  48.     wmove(xm_win, i+11, 13);
  49.     wrefresh(xm_win);
  50.                     /* get the protocol */
  51.     type = -1;
  52.     while ((ans = wgetch(xm_win)) != ESC) {
  53.         switch (ans) {
  54.             case '1':
  55.                 type = XMODEM;
  56.                 break;
  57.             case '2':
  58.                 type = XMODEM_1k;
  59.                 break;
  60.             case '3':
  61.                 type = MODEM7;
  62.                 break;
  63.             case '4':
  64.                 type = YMODEM;
  65.                 break;
  66.             case '5':
  67.                 type = YMODEM_G;
  68.                 break;
  69.             case '6':
  70.                 type = XASCII;
  71.                 break;
  72.             case '7':
  73.                 if (num_extrnl >= 1)
  74.                     type = EXT_1;
  75.                 else
  76.                     beep();
  77.                 break;
  78.             case '8':
  79.                 if (num_extrnl >= 2)
  80.                     type = EXT_2;
  81.                 else
  82.                     beep();
  83.                 break;
  84.             case '9':
  85.                 if (num_extrnl >= 3)
  86.                     type = EXT_3;
  87.                 else
  88.                     beep();
  89.                 break;
  90.             case 'e':
  91.             case 'E':
  92.                 type = EXT_MANUAL;
  93.                 break;
  94.             default:
  95.                 beep();
  96.         }
  97.         if (type != -1)
  98.             break;
  99.     }
  100.     werase(xm_win);
  101.     wrefresh(xm_win);
  102.     delwin(xm_win);
  103.                     /* chicken'd out */
  104.     if (type == -1)
  105.         return;
  106.  
  107.     if (fd == -1) {
  108.         error_win(0, "Not currently connected to any host", "");
  109.         return;
  110.     }
  111.                     /* which protocol? */
  112.     is_batch = 0;
  113.     switch(type) {
  114.         case MODEM7:
  115.         case YMODEM:
  116.         case YMODEM_G:        /* built-in protocols */
  117.             is_batch++;
  118.             /* FALLTHRU */
  119.         case XMODEM:
  120.         case XMODEM_1k:        /* non-batch built-ins */
  121.             list = null_ptr;
  122.             /*
  123.              * When receiving in a batch mode, don't prompt
  124.              * for file names.
  125.              */
  126.             if (up || !is_batch) {
  127.                 if (!(list = get_names(up, type, is_batch)))
  128.                     break;
  129.             }
  130.             xfer_win(list, up, type);
  131.             break;
  132.         case XASCII:        /* ascii xfer, yuck! */
  133.             if (list = get_names(up, type, FALSE))
  134.                 xfer_ascii(list, up);
  135.             break;
  136.         case EXT_1:
  137.         case EXT_2:
  138.         case EXT_3:        /* one of the externals */
  139.             n = type -NUM_INTERNAL -1;
  140.             strcpy(buf, extrnl->command[up][n]);
  141.                     /* see if we need to prompt for files */
  142.             if (extrnl->prompt[up][n] == 'Y') {
  143.                 if (list = get_names(up, type, TRUE)) {
  144.                     strcat(buf, " ");
  145.                     strcat(buf, list);
  146.                 }
  147.                 else
  148.                     break;
  149.             }
  150.             do_extrnl(buf);
  151.             break;
  152.         case EXT_MANUAL:    /* the manual external protocol */
  153.             if (list = get_extrnl(up))
  154.                 do_extrnl(list);
  155.             break;
  156.     }
  157.     return;
  158. }
  159.  
  160. char *protocol[NUM_INTERNAL] = {"xmodem", "xmodem-1k", "modem7", "ymodem",
  161.     "ymodem-g", "ASCII"};
  162.  
  163. /*
  164.  * Prompt for a list of files for the transfer programs.  Since expand()
  165.  * is used, it returns a pointer to a static area.  Returns a NULL if 
  166.  * you chicken'd out.
  167.  */
  168.  
  169. static char *
  170. get_names(up, type, is_batch)
  171. int up, type, is_batch;
  172. {
  173.     int got_it, n;
  174.     WINDOW *gn_win, *newwin();
  175.     char *ans, *file, *list, buf[40], *expand(), *get_str(), *strtok();
  176.     void st_line();
  177.     struct stat stbuf;
  178.  
  179.     touchwin(stdscr);
  180.     refresh();
  181.     st_line("");
  182.  
  183.     n = type -NUM_INTERNAL -1;
  184.  
  185.     gn_win = newwin(7, 70, 5, 5);
  186.     mvwaddstr(gn_win, 3, 4, "Enter filename: ");
  187.     box(gn_win, VERT, HORZ);
  188.     if (up) {
  189.         if (type <= NUM_INTERNAL)
  190.             sprintf(buf, " Send %s ", protocol[type -1]);
  191.         else
  192.             sprintf(buf, " Send %s ", extrnl->name[up][n]);
  193.     }
  194.     else {
  195.         if (type <= NUM_INTERNAL)
  196.             sprintf(buf, " Receive %s ", protocol[type -1]);
  197.         else
  198.             sprintf(buf, " Receive %s ", extrnl->name[up][n]);
  199.     }
  200.     mvwattrstr(gn_win, 0, 3, A_BOLD, buf);
  201.  
  202.     /* CONSTCOND */
  203.     while (1) {
  204.         wmove(gn_win, 3, 20);
  205.         wrefresh(gn_win);
  206.                     /* get the answers */
  207.         if (is_batch)
  208.             ans = get_str(gn_win, 60, "", "\n");
  209.         else
  210.             ans = get_str(gn_win, 60, "", " \t\n");
  211.  
  212.         if (ans == NULL || *ans == '\0') {
  213.             list = NULL;
  214.             break;
  215.         }
  216.         list = expand(ans);
  217.                     /* batches are checked on-the-fly */
  218.         if (is_batch)
  219.             break;
  220.         /*
  221.          * Here we have the opportunity to determine the read and
  222.          * write permissions before things get started.  Much nicer
  223.          * than finding out later when there's no way to fix it.
  224.          * Only checks the first file.
  225.          */
  226.         file = strtok(list, " \t");
  227.                     /* sanity checking */
  228.         if (!stat(file, &stbuf)) {
  229.             if ((stbuf.st_mode & S_IFREG) != S_IFREG) {
  230.                 beep();
  231.                 clear_line(gn_win, 4, 15, TRUE);
  232.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "Not a regular file");
  233.                 wrefresh(gn_win);
  234.                 wait_key(gn_win, 3);
  235.                 clear_line(gn_win, 4, 15, TRUE);
  236.                 clear_line(gn_win, 3, 20, TRUE);
  237.                 continue;
  238.             }
  239.         }
  240.                     /* check read permission */
  241.         if (up) {
  242.             if (access(file, 0)) {
  243.                 beep();
  244.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "Can't find file");
  245.                 wrefresh(gn_win);
  246.                 wait_key(gn_win, 3);
  247.                 clear_line(gn_win, 4, 15, TRUE);
  248.                 clear_line(gn_win, 3, 20, TRUE);
  249.                 continue;
  250.             }
  251.             if (access(file, 4)) {
  252.                 beep();
  253.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "No read permission");
  254.                 wrefresh(gn_win);
  255.                 wait_key(gn_win, 3);
  256.                 clear_line(gn_win, 4, 15, TRUE);
  257.                 clear_line(gn_win, 3, 20, TRUE);
  258.                 continue;
  259.             }
  260.             break;
  261.         }
  262.                     /* check write permission */
  263.         got_it = 0;
  264.         switch(can_write(file)) {
  265.             case DENIED:
  266.                 beep();
  267.                 clear_line(gn_win, 4, 15, TRUE);
  268.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "No write permission");
  269.                 wrefresh(gn_win);
  270.                 wait_key(gn_win, 3);
  271.                 clear_line(gn_win, 4, 15, TRUE);
  272.                 clear_line(gn_win, 3, 20, TRUE);
  273.                 break;
  274.             case OK_BUT_EXISTS:
  275.                 if (!yes_prompt(gn_win, 4, 15, A_BOLD, "File exists, overwrite")) {
  276.                     clear_line(gn_win, 4, 15, TRUE);
  277.                     clear_line(gn_win, 3, 20, TRUE);
  278.                     break;
  279.                 }
  280.                 /* FALLTHRU */
  281.             case WRITE_OK:
  282.                 got_it++;
  283.                 break;
  284.         }
  285.         if (got_it)
  286.             break;
  287.     }
  288.     werase(gn_win);
  289.     wrefresh(gn_win);
  290.     delwin(gn_win);
  291.  
  292.     return(list);
  293. }
  294.  
  295. /*
  296.  * Prompt for the Unix command line to be used as an external file
  297.  * transfer program.  Since expand() is used, it returns a pointer to
  298.  * a static area.
  299.  */
  300.  
  301. static char *
  302. get_extrnl(up)
  303. int up;
  304. {
  305.     WINDOW *ge_win, *newwin();
  306.     char *ans, *cmd, *get_str(), *expand();
  307.     void st_line();
  308.  
  309.     touchwin(stdscr);
  310.     refresh();
  311.     st_line("");
  312.                     /* prompt for command line */
  313.     ge_win = newwin(7, 70, 5, 5);
  314.     mvwaddstr(ge_win, 3, 4, "Enter Unix command: ");
  315.     box(ge_win, VERT, HORZ);
  316.  
  317.     if (up)
  318.         mvwattrstr(ge_win, 0, 3, A_BOLD, " Send (external) ");
  319.     else
  320.         mvwattrstr(ge_win, 0, 3, A_BOLD, " Receive (external) ");
  321.  
  322.     wmove(ge_win, 3, 24);
  323.     wrefresh(ge_win);
  324.                     /* get the line */
  325.     ans = get_str(ge_win, 60, "", "\n");
  326.     if (ans == NULL || *ans == '\0')
  327.         cmd = NULL;
  328.     else
  329.         cmd = expand(ans);
  330.  
  331.     werase(ge_win);
  332.     wrefresh(ge_win);
  333.     delwin(ge_win);
  334.     return(cmd);
  335. }
  336.