home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume16 / pcomm2 / part04 / di_win.c next >
C/C++ Source or Header  |  1988-09-14  |  9KB  |  383 lines

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