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

  1. /*
  2.  * The dialing window routines.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <curses.h>
  7. #include "dial_dir.h"
  8. #include "misc.h"
  9. #include "modem.h"
  10. #include "param.h"
  11. #include "status.h"
  12.  
  13. static int can_sync();
  14. static void disp_queue();
  15.  
  16. /*
  17.  * The dialing window.  Its job is to get a port, cycle thru the entries
  18.  * in the queue, while interpreting both the user's requests and the
  19.  * modem's responses.  A non-zero return code means we failed to connect.
  20.  */
  21.  
  22. int
  23. dial_win(repeat)
  24. int repeat;
  25. {
  26.     extern int fd;
  27.     WINDOW *di_win, *newwin();
  28.     int i, j, key, want_out, pass, tic;
  29.     long now, time();
  30.     char *tbuf, *ctime(), *str, cr=13, *read_codes();
  31.     void dial_it(), delay_times();
  32.     void error_win(), line_set(), hang_up(), vs_clear(), log_calls();
  33.     unsigned int baud, sleep();
  34.                     /* are we already talking? */
  35.     hang_up(VERBOSE);
  36.  
  37.     touchwin(stdscr);
  38.     refresh();
  39.  
  40.     /*
  41.      * If the phone number points to NULL, then either you're on a
  42.      * direct line, or you want to do the dialing yourself.
  43.      */
  44.     dir->d_cur = dir->q_num[0];
  45.     if (*dir->number[dir->d_cur] == '\0') {
  46.                     /* check LD permission */
  47.         if (limit_ld(0))
  48.             return(1);
  49.  
  50.         if (get_port())
  51.             return(1);
  52.                     /* can't talk directly to OBM */
  53.         if (!strcmp(modem->mname[modem->m_cur], "OBM")) {
  54.             error_win(0, "Can't access the On Board Modem directly",
  55.              "You must use the automatic dialing feature");
  56.             return(1);
  57.         }
  58.  
  59.         vs_clear(1);
  60.         touchwin(stdscr);
  61.         clear();
  62.         printw("Connected to /dev/%s at %d baud...\n", modem->tty[modem->t_cur], dir->baud[0]);
  63.         refresh();
  64.                     /* We have to assume... */
  65.         status->connected = 1;
  66.         return(0);
  67.     }
  68.  
  69.     di_win = newwin(17, 70, 3, 5);
  70.                     /* the basic window */
  71.     mvwattrstr(di_win, 1, 20, A_BOLD, "D I A L I N G       W I N D O W");
  72.     horizontal(di_win, 2, 0, 70);
  73.     mvwaddstr(di_win, 4, 23, "System name:");
  74.     mvwaddstr(di_win, 5, 23, "Pass number:");
  75.     mvwaddstr(di_win, 6, 14, "Elapse time this try:");
  76.     mvwaddstr(di_win, 7, 13, "Time at start of dial:");
  77.     mvwaddstr(di_win, 8, 9, "Time at start of this try:");
  78.     mvwaddstr(di_win, 9, 16, "Connect delay time:");
  79.     mvwaddstr(di_win, 10, 17, "Redial delay time:");
  80.     mvwaddstr(di_win, 11, 25, "Auxiliary:");
  81.     mvwaddstr(di_win, 12, 16, "Result of last try:");
  82.  
  83.     mvwaddstr(di_win, 14, 3, "<SPACE>: Recycle");
  84.     mvwaddstr(di_win, 14, 22, "<DEL>: Remove from queue");
  85.     mvwaddstr(di_win, 14, 49, "E: Change delays");
  86.  
  87.                     /* the start time */
  88.     time(&now);
  89.     tbuf = ctime(&now);
  90.     tbuf[19] = '\0';
  91.     mvwaddstr(di_win, 7, 36, &tbuf[11]);
  92.  
  93.     mvwprintw(di_win, 9, 36, "%-4d", param->c_delay);
  94.     mvwprintw(di_win, 10, 36, "%-4d", param->r_delay);
  95.  
  96.     box(di_win, VERT, HORZ);
  97.     mvwaddstr(di_win, 16, 24, " Press <ESC> to abort ");
  98.  
  99.     pass = 0;
  100.     i = 0;
  101.     want_out = 0;
  102.     while (!want_out && pass <= repeat) {
  103.         key = -1;
  104.         pass++;
  105.                     /* check LD permission */
  106.         if (limit_ld(i)) {
  107.             want_out++;
  108.             break;
  109.         }
  110.                     /* update the d_cur variable */
  111.         dir->d_cur = dir->q_num[i];
  112.  
  113.                     /* get a port */
  114.         if (get_port()) {
  115.             want_out++;
  116.             break;
  117.         }
  118.                     /* fill in the window */
  119.         disp_queue(di_win, dir->d_cur, pass, now);
  120.         now = 0L;
  121.  
  122.         /*
  123.          * The actual dial routine.  The "i" is the index into the
  124.          * queue, not the entry number.  Returns immediately without
  125.          * waiting for a carrier.
  126.          */
  127.         dial_it(i);
  128.         tty_flush(fd, 0);
  129.  
  130.         /*
  131.          * Here we do a time-slice between reading the result codes
  132.          * from the modem and reading the keyboard.  The one second
  133.          * granularity won't be too accurate, but who cares?
  134.          */
  135.         tic = 0;
  136.         while (tic < param->c_delay) {
  137.             if ((str = read_codes(tic)) == NULL) {
  138.                 mvwprintw(di_win, 6, 36, "%-4d", ++tic);
  139.                 wrefresh(di_win);
  140.             }
  141.             else {
  142.                 /*
  143.                  * A return code that converts to an number
  144.                  * that is less than 300 is probably an error
  145.                  * message.
  146.                  */
  147.                 baud = (unsigned int) atoi(str);
  148.                 if (baud < 300) {
  149.                     mvwprintw(di_win, 12, 36, "%-20.20s", str);
  150.                     wmove(di_win, 12, 36);
  151.                     wrefresh(di_win);
  152.                     break;
  153.                 }
  154.                     /* we're connected */
  155.                 beep();
  156.                 clear_line(di_win, 12, 36, TRUE);
  157.                 wattrstr(di_win, A_BLINK, "CONNECTED");
  158.                 wmove(di_win, 12, 36);
  159.                 wrefresh(di_win);
  160.                 wait_key(di_win, 2);
  161.                 delwin(di_win);
  162.  
  163.                 /*
  164.                  * Did the modem sync at a different baud
  165.                  * rate than what we expected?
  166.                  */
  167.                 if (dir->baud[0] != baud) {
  168.                     if (can_sync(baud)) {
  169.                         dir->baud[0] = baud;
  170.                         line_set();
  171.                     }
  172.                 }
  173.  
  174.                 vs_clear(1);
  175.                 touchwin(stdscr);
  176.                 clear();
  177.                 printw("Connected to %s at %d baud...\n",
  178.                  dir->name[dir->d_cur], dir->baud[0]);
  179.                 refresh();
  180.                 status->connected = 1;
  181.  
  182.                     /* log the call */
  183.                 log_calls(i);
  184.                 return(0);
  185.             }
  186.             if (tic == param->c_delay)
  187.                 break;
  188.                     /* ok... try the keyboard */
  189.             if ((key = wait_key(di_win, 1)) != -1)
  190.                 break;
  191.  
  192.             mvwprintw(di_win, 6, 36, "%-4d", ++tic);
  193.             wrefresh(di_win);
  194.         }
  195.         /*
  196.          * If the modem did not return a code, then we need to
  197.          * stop it.  Sending a CR will stop most modems cold,
  198.          * except of course for the OBM...
  199.          */
  200.         if (str == NULL) {
  201.             if (!strcmp(modem->mname[modem->m_cur], "OBM"))
  202.                 hang_up(QUIET);
  203.             else
  204.                 write(fd, &cr, 1);
  205.             sleep(2);
  206.         }
  207.                     /* if we get here, no key was pressed */
  208.         if (key == -1) {
  209.             clear_line(di_win, 6, 14, TRUE);
  210.             mvwaddstr(di_win, 6, 27, "Pausing:");
  211.                     /* no return code? */
  212.             if (str == NULL) {
  213.                 clear_line(di_win, 12, 36, TRUE);
  214.                 waddstr(di_win, "TIMED OUT");
  215.                 wmove(di_win, 12, 36);
  216.             }
  217.                     /* do the pause */
  218.             tic = 0;
  219.             while (tic < param->r_delay) {
  220.                 if ((key = wait_key(di_win, 1)) != -1)
  221.                     break;
  222.                 mvwprintw(di_win, 6, 36, "%-4d", ++tic);
  223.                 wrefresh(di_win);
  224.             }
  225.             clear_line(di_win, 6, 14, TRUE);
  226.             waddstr(di_win, "Elapse time this try:");
  227.         }
  228.         mvwaddstr(di_win, 6, 36, "0   ");
  229.                     /* Process the keystroke */
  230.         switch (key) {
  231.             case ' ':    /* next in the queue */
  232.                 clear_line(di_win, 12, 36, TRUE);
  233.                 waddstr(di_win, "RECYCLED");
  234.                 wmove(di_win, 12, 36);
  235.                 wrefresh(di_win);
  236.                 /* FALLTHRU */
  237.             case -1:    /* no key was pressed */
  238.                 i++;
  239.                 if (i > NUM_QUEUE)
  240.                     i = 0;
  241.                 if (dir->q_num[i] == -1)
  242.                     i = 0;
  243.                 break;
  244.             case DEL:    /* <DEL> key, remove from queue */
  245.                 if (dir->q_num[1] == -1) {
  246.                     beep();
  247.                     clear_line(di_win, 12, 36, TRUE);
  248.                     waddstr(di_win, "NO MORE ENTRIES");
  249.                     wmove(di_win, 12, 36);
  250.                     wrefresh(di_win);
  251.                     wait_key(di_win, 3);
  252.                     break;
  253.                 }
  254.                 clear_line(di_win, 12, 36, TRUE);
  255.                 waddstr(di_win, "ENTRY DELETED");
  256.                 wmove(di_win, 12, 36);
  257.                 wrefresh(di_win);
  258.                 wait_key(di_win, 3);
  259.  
  260.                     /* compact the queue */
  261.                 for (j=i; j<NUM_QUEUE-1; j++)
  262.                     dir->q_num[j] = dir->q_num[j+1];
  263.                 dir->q_num[NUM_QUEUE-1] = -1;
  264.  
  265.                 if (dir->q_num[i] == -1)
  266.                     i = 0;
  267.                 break;
  268.             case 'e':
  269.             case 'E':    /* change delay time */
  270.                 delay_times();
  271.                 touchwin(di_win);
  272.                 mvwprintw(di_win, 9, 36, "%-4d", param->c_delay);
  273.                 mvwprintw(di_win, 10, 36, "%-4d", param->r_delay);
  274.                 break;
  275.             case ESC:    /* <ESC> key */
  276.                 beep();
  277.                 clear_line(di_win, 12, 36, TRUE);
  278.                 wattrstr(di_win, A_BLINK, "DIAL ABORTED");
  279.                 wmove(di_win, 12, 36);
  280.                 wrefresh(di_win);
  281.                 wait_key(di_win, 3);
  282.                 want_out++;
  283.                 break;
  284.             default:
  285.                 beep();
  286.                 break;
  287.         }
  288.     }
  289.                     /* clean up and go home */
  290.     werase(di_win);
  291.     wrefresh(di_win);
  292.     delwin(di_win);
  293.     if (!want_out)
  294.         error_win(0, "Exceeded the maximum number of dialing attempts", "");
  295.     return(1);
  296. }
  297.  
  298. /*
  299.  * Display what info we know at this time.
  300.  */
  301.  
  302. static void
  303. disp_queue(win, entry, pass, first)
  304. WINDOW *win;
  305. int entry, pass;
  306. long first;
  307. {
  308.     long now, time();
  309.     char *tbuf, *ctime();
  310.     void st_line();
  311.                     /* redo the status line */
  312.     st_line("");
  313.                     /* system name */
  314.     clear_line(win, 4, 36, TRUE);
  315.     waddstr(win, dir->name[entry]);
  316.                     /* pass number */
  317.     mvwprintw(win, 5, 36, "%-4d", pass);
  318.                     /* time of this call */
  319.     if (first)
  320.         now = first;
  321.     else
  322.         time(&now);
  323.  
  324.     tbuf = ctime(&now);
  325.     tbuf[19] = '\0';
  326.     mvwaddstr(win, 8, 36, &tbuf[11]);
  327.                     /* the aux field */
  328.     clear_line(win, 11, 36, TRUE);
  329.     waddstr(win, dir->aux[entry]);
  330.  
  331.     wmove(win, 12, 36);
  332.     wrefresh(win);
  333.     return;
  334. }
  335.  
  336. /*
  337.  * Determine if the modem can detect the synchronization of the connected
  338.  * baud rate.  We check the modem database and see if the connect string
  339.  * is unique.  A non-zero return code means the modem can sync.
  340.  */
  341.  
  342. static int
  343. can_sync(baud)
  344. unsigned int baud;
  345. {
  346.     int i;
  347.     char *str;
  348.                     /* feature disabled? */
  349.     if (modem->auto_baud[modem->m_cur] != 'Y')
  350.         return(0);
  351.                     /* re-construct the string */
  352.     switch (baud) {
  353.         case 300:
  354.             str = modem->con_3[modem->m_cur];
  355.             break;
  356.         case 1200:
  357.             str = modem->con_12[modem->m_cur];
  358.             break;
  359.         case 2400:
  360.             str = modem->con_24[modem->m_cur];
  361.             break;
  362.         case 4800:
  363.             str = modem->con_48[modem->m_cur];
  364.             break;
  365.         case 7200:        /* not really the DTE speed */
  366.             if (modem->lock_sp[modem->t_cur])
  367.                 return(1);
  368.             return(0);
  369.         case 9600:
  370.             str = modem->con_96[modem->m_cur];
  371.             break;
  372.         case 12000:        /* not really the DTE speed */
  373.             if (modem->lock_sp[modem->t_cur])
  374.                 return(1);
  375.             return(0);
  376.         case 14400:        /* not really the DTE speed */
  377.             if (modem->lock_sp[modem->t_cur])
  378.                 return(1);
  379.             return(0);
  380.         case 19200:
  381.             str = modem->con_192[modem->m_cur];
  382.             break;
  383.         case 38400:
  384.             str = modem->con_384[modem->m_cur];
  385.             break;
  386.         default:
  387.             return(0);
  388.     }
  389.  
  390.     if (*str == '\0')
  391.         return(0);
  392.                     /* test "str" against all others */
  393.     i = 0;
  394.     if (!strcmp(str, modem->con_3[modem->m_cur]))
  395.         i++;
  396.     if (!strcmp(str, modem->con_12[modem->m_cur]))
  397.         i++;
  398.     if (!strcmp(str, modem->con_24[modem->m_cur]))
  399.         i++;
  400.     if (!strcmp(str, modem->con_48[modem->m_cur]))
  401.         i++;
  402.     if (!strcmp(str, modem->con_96[modem->m_cur]))
  403.         i++;
  404.     if (!strcmp(str, modem->con_192[modem->m_cur]))
  405.         i++;
  406.     if (!strcmp(str, modem->con_384[modem->m_cur]))
  407.         i++;
  408.                     /* should match only itself */
  409.     if (i == 1)
  410.         return(1);
  411.     return(0);
  412. }
  413.